MOONS Pipeline Reference Manual 0.13.2
moo_rbnlist.c
1/*
2 * This file is part of the MOONS Pipeline
3 * Copyright (C) 2002-2016 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27#include <math.h>
28#include <cpl.h>
29#include <hdrl.h>
30#include <assert.h>
31#include "moo_rbnlist.h"
32#include "moo_utils.h"
33/*----------------------------------------------------------------------------*/
39/*----------------------------------------------------------------------------*/
40
43/*-----------------------------------------------------------------------------
44 Function codes
45 -----------------------------------------------------------------------------*/
46
47/*----------------------------------------------------------------------------*/
55moo_rbnlist *
57{
58 return (moo_rbnlist *)cpl_calloc(1, sizeof(moo_rbnlist));
59}
60
61/*----------------------------------------------------------------------------*/
70moo_rbnlist *
71moo_rbnlist_create(const cpl_frameset *frameset)
72{
73 cpl_ensure(frameset != NULL, CPL_ERROR_NULL_INPUT, NULL);
74 int i;
75 moo_rbnlist *res = moo_rbnlist_new();
76
77 for (i = 0; i < cpl_frameset_get_size(frameset); ++i) {
78 const cpl_frame *current_frame =
79 cpl_frameset_get_position_const(frameset, i);
80 moo_rbn *rbn = moo_rbn_create(current_frame);
81 moo_rbnlist_push(res, rbn);
82 }
83
84 return res;
85}
86
87/*----------------------------------------------------------------------------*/
102/*----------------------------------------------------------------------------*/
103cpl_error_code
104moo_rbnlist_load_single(const moo_rbnlist *self,
106 int level)
107{
108 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
109
110 int i;
111
112 for (i = 0; i < self->size; i++) {
113 moo_rbn *rbn = self->list[i];
114 moo_rbn_load_single(rbn, type, level);
115 }
116 return CPL_ERROR_NONE;
117}
118
119/*----------------------------------------------------------------------------*/
135/*----------------------------------------------------------------------------*/
136cpl_error_code
137moo_rbnlist_free_single(const moo_rbnlist *self, moo_detector_type type)
138{
139 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
140
141 int i;
142 for (i = 0; i < self->size; i++) {
143 moo_rbn *rbn = self->list[i];
144 cpl_error_code status = moo_rbn_free_single(rbn, type);
145 if (status != CPL_ERROR_NONE) {
146 return status;
147 }
148 }
149 return CPL_ERROR_NONE;
150}
151/*----------------------------------------------------------------------------*/
161/*----------------------------------------------------------------------------*/
162cpl_size
163moo_rbnlist_get_size(const moo_rbnlist *self)
164{
165 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, -1);
166
167 assert(self->size >= 0);
168
169 return self->size;
170}
171
172/*----------------------------------------------------------------------------*/
184/*----------------------------------------------------------------------------*/
185moo_rbn *
186moo_rbnlist_get(moo_rbnlist *self, int i)
187{
188 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
189 cpl_ensure(i >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
190 int size = moo_rbnlist_get_size(self);
191 cpl_ensure(i < size, CPL_ERROR_ILLEGAL_INPUT, NULL);
192
193 return self->list[i];
194}
195
196/*----------------------------------------------------------------------------*/
210/*----------------------------------------------------------------------------*/
211hdrl_imagelist *
212moo_rbnlist_get_image(const moo_rbnlist *self, moo_detector_type type)
213{
214 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
215
216 cpl_errorstate prestate = cpl_errorstate_get();
217
218 hdrl_imagelist *res = NULL;
219 int i;
220
221 res = hdrl_imagelist_new();
222
223 for (i = 0; i < self->size; i++) {
224 moo_rbn *rbn = self->list[i];
225 moo_rbn_single *single = NULL;
226 moo_try_check(single = moo_rbn_get_single(rbn, type), " ");
227
228 if (single != NULL) {
229 hdrl_image *img = moo_rbn_single_get_image(single);
230 hdrl_imagelist_set(res, img, i);
231 }
232 }
233
234moo_try_cleanup:
235 if (!cpl_errorstate_is_equal(prestate)) {
236 cpl_msg_error(__func__, "Error in rbnlist");
237 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
238 hdrl_imagelist_delete(res);
239 res = NULL;
240 }
241 return res;
242}
243
244/*----------------------------------------------------------------------------*/
258/*----------------------------------------------------------------------------*/
259cpl_imagelist *
260moo_rbnlist_get_single_data(const moo_rbnlist *self, moo_detector_type type)
261{
262 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
263
264 cpl_imagelist *res = cpl_imagelist_new();
265 int i;
266
267 for (i = 0; i < self->size; i++) {
268 moo_rbn *rbn = self->list[i];
269 moo_rbn_single *single = moo_rbn_get_single(rbn, type);
270 if (single != NULL) {
271 cpl_imagelist_set(res, moo_rbn_single_get_data(single), i);
272 }
273 }
274
275 return res;
276}
277
278/*----------------------------------------------------------------------------*/
292/*----------------------------------------------------------------------------*/
293cpl_imagelist *
294moo_rbnlist_get_single_qual(const moo_rbnlist *self, moo_detector_type type)
295{
296 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
297
298 cpl_imagelist *res = cpl_imagelist_new();
299 int i;
300
301 for (i = 0; i < self->size; i++) {
302 moo_rbn *rbn = self->list[i];
303 moo_rbn_single *single = moo_rbn_get_single(rbn, type);
304 if (single != NULL) {
305 cpl_imagelist_set(res, moo_rbn_single_get_qual(single), i);
306 }
307 }
308
309 return res;
310}
311
312/*----------------------------------------------------------------------------*/
343/*----------------------------------------------------------------------------*/
344cpl_error_code
345moo_rbnlist_set(moo_rbnlist *self, moo_rbn *rbn, cpl_size pos)
346{
347 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
348 cpl_ensure_code(rbn, CPL_ERROR_NULL_INPUT);
349 cpl_ensure_code(pos >= 0, CPL_ERROR_ILLEGAL_INPUT);
350 cpl_ensure_code(pos <= self->size, CPL_ERROR_ACCESS_OUT_OF_RANGE);
351
352 /* Do nothing if the rbn is already there */
353 if (pos < self->size && rbn == self->list[pos])
354 return CPL_ERROR_NONE;
355
356 if (pos == self->size) {
357 self->size++;
358 self->list =
359 cpl_realloc(self->list, (size_t)self->size * sizeof(moo_rbn *));
360 }
361 else {
362 /* Check if the rbn at the position to be overwritten
363 is present in only one position */
364 int i;
365
366 for (i = 0; i < self->size; i++) {
367 if (i != pos && self->list[i] == self->list[pos])
368 break;
369 }
370 if (i == self->size) {
371 /* The image at the position to be overwritten
372 is present in only one position, so delete it */
373 moo_rbn_delete(self->list[pos]);
374 }
375 }
376
377 self->list[pos] = rbn;
378
379 return CPL_ERROR_NONE;
380}
381
382/*----------------------------------------------------------------------------*/
407/*----------------------------------------------------------------------------*/
408cpl_error_code
409moo_rbnlist_push(moo_rbnlist *self, moo_rbn *rbn)
410{
411 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
412 return moo_rbnlist_set(self, rbn, self->size);
413}
414/*----------------------------------------------------------------------------*/
433/*----------------------------------------------------------------------------*/
434moo_rbn *
435moo_rbnlist_unset(moo_rbnlist *self, cpl_size pos)
436{
437 moo_rbn *out;
438 cpl_size i;
439
440 cpl_ensure(self, CPL_ERROR_NULL_INPUT, NULL);
441 cpl_ensure(pos >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
442 cpl_ensure(pos < self->size, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
443
444 /* Get pointer to RBN to be removed */
445 out = self->list[pos];
446
447 /* Move the following RBN one position towards zero */
448 for (i = pos + 1; i < self->size; i++) {
449 self->list[i - 1] = self->list[i];
450 }
451
452 /* Decrement of the size */
453 self->size--;
454
455 return out;
456}
457
458/*----------------------------------------------------------------------------*/
471/*----------------------------------------------------------------------------*/
472void
473moo_rbnlist_empty(moo_rbnlist *self)
474{
475 if (self != NULL) {
476 while (self->size > 0) { /* An iteration may unset more than 1 image */
477 int i = self->size - 1;
478 moo_rbn *del = moo_rbnlist_unset(self, i);
479
480 moo_rbn_delete(del);
481
482 /* If this image was inserted more than once into the list,
483 the other insertions must be unset without a delete. */
484 while (--i >= 0) {
485 if (self->list[i] == del) {
486 /* This image was inserted more than once in the list */
487 (void)moo_rbnlist_unset(self, i);
488 }
489 }
490 }
491 }
492
493 return;
494}
495
496/*----------------------------------------------------------------------------*/
507/*----------------------------------------------------------------------------*/
508void
509moo_rbnlist_unwrap(moo_rbnlist *self)
510{
511 if (self != NULL) {
512 cpl_free(self->list);
513 cpl_free(self);
514 }
515
516 return;
517}
518
519/*----------------------------------------------------------------------------*/
528void
529moo_rbnlist_delete(moo_rbnlist *self)
530{
531 if (self != NULL) {
532 moo_rbnlist_empty(self);
533 moo_rbnlist_unwrap(self);
534 }
535
536 return;
537}
538
enum _moo_detector_type_ moo_detector_type
The type code type.
Definition: moo_detector.h:64
cpl_error_code moo_rbnlist_free_single(const moo_rbnlist *self, moo_detector_type type)
Free the type part for all DET in the detlist.
Definition: moo_rbnlist.c:137
hdrl_image * moo_rbn_single_get_image(moo_rbn_single *self)
Get image of RBN_SINGLE.
cpl_image * moo_rbn_single_get_data(moo_rbn_single *self)
Get image of data.
cpl_image * moo_rbn_single_get_qual(moo_rbn_single *self)
Get image of qual.
moo_rbn_single * moo_rbn_load_single(moo_rbn *self, moo_detector_type type, unsigned int level)
Load the type part in RBN and return it.
Definition: moo_rbn.c:360
void moo_rbn_delete(moo_rbn *self)
Delete a moo_rbn.
Definition: moo_rbn.c:120
moo_rbn_single * moo_rbn_get_single(moo_rbn *self, moo_detector_type type)
Get a RBN single from RBN.
Definition: moo_rbn.c:325
moo_rbn * moo_rbn_create(const cpl_frame *frame)
Create a new empty RBN filename.
Definition: moo_rbn.c:85
cpl_error_code moo_rbn_free_single(moo_rbn *self, moo_detector_type type)
Free the given type part in RBN.
Definition: moo_rbn.c:160
void moo_rbnlist_delete(moo_rbnlist *self)
Free all memory used by a moo_rbnlist object including the RBN.
Definition: moo_rbnlist.c:529
moo_rbnlist * moo_rbnlist_new(void)
Create a new moo_rbnlist.
Definition: moo_rbnlist.c:56
cpl_size moo_rbnlist_get_size(const moo_rbnlist *self)
Get the number of RBN in the rbnlist.
Definition: moo_rbnlist.c:163
void moo_rbnlist_empty(moo_rbnlist *self)
Empty an moo_rbnlist and deallocate all its RBN.
Definition: moo_rbnlist.c:473
cpl_error_code moo_rbnlist_load_single(const moo_rbnlist *self, moo_detector_type type, int level)
Load the type part for all RBN in the rbnlist.
Definition: moo_rbnlist.c:104
cpl_error_code moo_rbnlist_push(moo_rbnlist *self, moo_rbn *rbn)
Insert a RBN a the end of moo_rbnlist.
Definition: moo_rbnlist.c:409
void moo_rbnlist_unwrap(moo_rbnlist *self)
Free memory used by a moo_rbnlist object, except the RBN.
Definition: moo_rbnlist.c:509
moo_rbnlist * moo_rbnlist_create(const cpl_frameset *frameset)
Create a new moo_rbnlist from the given RBN frameset.
Definition: moo_rbnlist.c:71
cpl_imagelist * moo_rbnlist_get_single_data(const moo_rbnlist *self, moo_detector_type type)
Get the type data part for all RBN in the rbnlist.
Definition: moo_rbnlist.c:260
moo_rbn * moo_rbnlist_unset(moo_rbnlist *self, cpl_size pos)
Remove a RBN from a RBN list.
Definition: moo_rbnlist.c:435
hdrl_imagelist * moo_rbnlist_get_image(const moo_rbnlist *self, moo_detector_type type)
Get the all the images of the type part in the rbnlist.
Definition: moo_rbnlist.c:212
cpl_imagelist * moo_rbnlist_get_single_qual(const moo_rbnlist *self, moo_detector_type type)
Get the type QUAL part for all RBN in the rbnlist.
Definition: moo_rbnlist.c:294
cpl_error_code moo_rbnlist_set(moo_rbnlist *self, moo_rbn *rbn, cpl_size pos)
Insert a RBN into an moo_rbnlist.
Definition: moo_rbnlist.c:345
moo_rbn * moo_rbnlist_get(moo_rbnlist *self, int i)
Get the RBN at the position i in the list.
Definition: moo_rbnlist.c:186