MOONS Pipeline Reference Manual 0.13.1
moo_scilist.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_scilist.h"
32#include "moo_utils.h"
33/*----------------------------------------------------------------------------*/
39/*----------------------------------------------------------------------------*/
40
43/*-----------------------------------------------------------------------------
44 Function codes
45 -----------------------------------------------------------------------------*/
46
47/*----------------------------------------------------------------------------*/
55moo_scilist *
57{
58 return (moo_scilist *)cpl_calloc(1, sizeof(moo_scilist));
59}
60
61/*----------------------------------------------------------------------------*/
80/*----------------------------------------------------------------------------*/
81moo_sci *
82moo_scilist_unset(moo_scilist *self, cpl_size pos)
83{
84 moo_sci *out;
85 cpl_size i;
86
87 cpl_ensure(self, CPL_ERROR_NULL_INPUT, NULL);
88 cpl_ensure(pos >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
89 cpl_ensure(pos < self->size, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
90
91 /* Get pointer to SCI to be removed */
92 out = self->list[pos];
93
94 /* Move the following SCI one position towards zero */
95 for (i = pos + 1; i < self->size; i++) {
96 self->list[i - 1] = self->list[i];
97 }
98
99 /* Decrement of the size */
100 self->size--;
101
102 return out;
103}
104
105/*----------------------------------------------------------------------------*/
118/*----------------------------------------------------------------------------*/
119void
120moo_scilist_empty(moo_scilist *self)
121{
122 if (self != NULL) {
123 while (self->size > 0) { /* An iteration may unset more than 1 image */
124 int i = self->size - 1;
125 moo_sci *del = moo_scilist_unset(self, i);
126
127 moo_sci_delete(del);
128
129 /* If this image was inserted more than once into the list,
130 the other insertions must be unset without a delete. */
131 while (--i >= 0) {
132 if (self->list[i] == del) {
133 /* This image was inserted more than once in the list */
134 (void)moo_scilist_unset(self, i);
135 }
136 }
137 }
138 }
139
140 return;
141}
142
143/*----------------------------------------------------------------------------*/
154/*----------------------------------------------------------------------------*/
155void
156moo_scilist_unwrap(moo_scilist *self)
157{
158 if (self != NULL) {
159 cpl_free(self->list);
160 cpl_free(self);
161 }
162
163 return;
164}
165
166/*----------------------------------------------------------------------------*/
175void
176moo_scilist_delete(moo_scilist *self)
177{
178 if (self != NULL) {
179 moo_scilist_empty(self);
180 moo_scilist_unwrap(self);
181 }
182
183 return;
184}
185
186/*----------------------------------------------------------------------------*/
196/*----------------------------------------------------------------------------*/
197cpl_size
198moo_scilist_get_size(const moo_scilist *self)
199{
200 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, -1);
201
202 assert(self->size >= 0);
203
204 return self->size;
205}
206
207/*----------------------------------------------------------------------------*/
238/*----------------------------------------------------------------------------*/
239cpl_error_code
240moo_scilist_set(moo_scilist *self, moo_sci *sci, cpl_size pos)
241{
242 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
243 cpl_ensure_code(sci, CPL_ERROR_NULL_INPUT);
244 cpl_ensure_code(pos >= 0, CPL_ERROR_ILLEGAL_INPUT);
245 cpl_ensure_code(pos <= self->size, CPL_ERROR_ACCESS_OUT_OF_RANGE);
246
247 /* Do nothing if the sci is already there */
248 if (pos < self->size && sci == self->list[pos])
249 return CPL_ERROR_NONE;
250
251 if (pos == self->size) {
252 self->size++;
253 self->list =
254 cpl_realloc(self->list, (size_t)self->size * sizeof(moo_sci *));
255 }
256 else {
257 /* Check if the sci at the position to be overwritten
258 is present in only one position */
259 int i;
260
261 for (i = 0; i < self->size; i++) {
262 if (i != pos && self->list[i] == self->list[pos])
263 break;
264 }
265 if (i == self->size) {
266 /* The image at the position to be overwritten
267 is present in only one position, so delete it */
268 moo_sci_delete(self->list[pos]);
269 }
270 }
271
272 self->list[pos] = sci;
273
274 return CPL_ERROR_NONE;
275}
276
277/*----------------------------------------------------------------------------*/
302/*----------------------------------------------------------------------------*/
303cpl_error_code
304moo_scilist_push(moo_scilist *self, moo_sci *sci)
305{
306 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
307 moo_scilist_set(self, sci, self->size);
308 return cpl_error_get_code();
309}
310
311/*----------------------------------------------------------------------------*/
320moo_scilist *
321moo_scilist_create(cpl_frameset *frameset)
322{
323 cpl_ensure(frameset != NULL, CPL_ERROR_NULL_INPUT, NULL);
324 int i;
325 moo_scilist *res = moo_scilist_new();
326 for (i = 0; i < cpl_frameset_get_size(frameset); ++i) {
327 const cpl_frame *current_frame =
328 cpl_frameset_get_position_const(frameset, i);
329 moo_sci *sci = moo_sci_create(current_frame);
330 moo_scilist_push(res, sci);
331 }
332 return res;
333}
334
335/*----------------------------------------------------------------------------*/
344moo_scilist *
345moo_scilist_create_clean(cpl_frameset *frameset)
346{
347 cpl_ensure(frameset != NULL, CPL_ERROR_NULL_INPUT, NULL);
348 int i;
349 moo_scilist *res = moo_scilist_new();
350 for (i = 0; i < cpl_frameset_get_size(frameset); ++i) {
351 const cpl_frame *current_frame =
352 cpl_frameset_get_position_const(frameset, i);
353 moo_sci *sci = moo_sci_create(current_frame);
354 moo_target_table *target_table = moo_sci_get_target_table(sci);
355 if (target_table != NULL) {
356 int nrow = cpl_table_get_nrow(target_table->table);
357 if (nrow > 0) {
358 moo_scilist_push(res, sci);
359 }
360 else {
361 moo_sci_delete(sci);
362 }
363 }
364 else {
365 moo_sci_delete(sci);
366 }
367 }
368 return res;
369}
370/*----------------------------------------------------------------------------*/
382/*----------------------------------------------------------------------------*/
383moo_sci *
384moo_scilist_get(moo_scilist *self, int i)
385{
386 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
387 cpl_ensure(i >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
388 int size = moo_scilist_get_size(self);
389 cpl_ensure(i < size, CPL_ERROR_ILLEGAL_INPUT, NULL);
390
391 return self->list[i];
392}
393
394/*----------------------------------------------------------------------------*/
407/*----------------------------------------------------------------------------*/
408moo_sci_single *
409moo_scilist_get_single(moo_scilist *self, int i, moo_detector_type type)
410{
411 moo_sci_single *result = NULL;
412 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
413 cpl_ensure(i >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
414 int size = moo_scilist_get_size(self);
415 cpl_ensure(i < size, CPL_ERROR_ILLEGAL_INPUT, NULL);
416
417 if (self->list[i] != NULL) {
418 result = moo_sci_get_single(self->list[i], type);
419 }
420 return result;
421}
422
423
424/*----------------------------------------------------------------------------*/
437/*----------------------------------------------------------------------------*/
438cpl_error_code
439moo_scilist_load_single(const moo_scilist *self,
441 int level)
442{
443 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
444
445 int i;
446 for (i = 0; i < self->size; i++) {
447 moo_sci *sci = self->list[i];
448 moo_sci_load_single(sci, type, level);
449 }
450 return CPL_ERROR_NONE;
451}
452
453
454/*----------------------------------------------------------------------------*/
466/*----------------------------------------------------------------------------*/
467hdrl_imagelist *
468moo_scilist_get_image(const moo_scilist *self, moo_detector_type type)
469{
470 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
471
472 cpl_errorstate prestate = cpl_errorstate_get();
473
474 hdrl_imagelist *res = NULL;
475 int i;
476
477 res = hdrl_imagelist_new();
478
479 for (i = 0; i < self->size; i++) {
480 moo_sci *sci = self->list[i];
481 moo_sci_single *single = NULL;
482 moo_try_check(single = moo_sci_get_single(sci, type), " ");
483
484 if (single != NULL) {
485 hdrl_image *img = moo_sci_single_get_image(single);
486 hdrl_imagelist_set(res, img, i);
487 }
488 }
489
490moo_try_cleanup:
491 if (!cpl_errorstate_is_equal(prestate)) {
492 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
493 hdrl_imagelist_delete(res);
494 res = NULL;
495 }
496 return res;
497 return res;
498}
499
500/*----------------------------------------------------------------------------*/
512/*----------------------------------------------------------------------------*/
513cpl_imagelist *
514moo_scilist_get_qual(const moo_scilist *self, moo_detector_type type)
515{
516 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
517
518 cpl_errorstate prestate = cpl_errorstate_get();
519
520 cpl_imagelist *res = NULL;
521 int i;
522
523 res = cpl_imagelist_new();
524
525 for (i = 0; i < self->size; i++) {
526 moo_sci *sci = self->list[i];
527 moo_sci_single *single = NULL;
528 moo_try_check(single = moo_sci_get_single(sci, type), " ");
529
530 if (single != NULL) {
531 cpl_image *qual = moo_sci_single_get_qual(single);
532 cpl_imagelist_set(res, qual, i);
533 }
534 }
535
536moo_try_cleanup:
537 if (!cpl_errorstate_is_equal(prestate)) {
538 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
539 cpl_imagelist_delete(res);
540 res = NULL;
541 }
542 return res;
543 return res;
544}
545
546/*----------------------------------------------------------------------------*/
558/*----------------------------------------------------------------------------*/
559cpl_imagelist *
560moo_scilist_get_sky(const moo_scilist *self, moo_detector_type type)
561{
562 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
563
564 cpl_errorstate prestate = cpl_errorstate_get();
565
566 cpl_imagelist *res = NULL;
567 int i;
568
569 res = cpl_imagelist_new();
570
571 for (i = 0; i < self->size; i++) {
572 moo_sci *sci = self->list[i];
573 moo_sci_single *single = NULL;
574 moo_try_check(single = moo_sci_get_single(sci, type), " ");
575
576 if (single != NULL) {
577 cpl_image *sky = moo_sci_single_get_sky(single);
578 cpl_imagelist_set(res, sky, i);
579 }
580 }
581
582moo_try_cleanup:
583 if (!cpl_errorstate_is_equal(prestate)) {
584 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
585 cpl_imagelist_delete(res);
586 res = NULL;
587 }
588 return res;
589 return res;
590}
enum _moo_detector_type_ moo_detector_type
The type code type.
Definition: moo_detector.h:64
cpl_image * moo_sci_single_get_sky(moo_sci_single *self)
Get sky of sci single.
hdrl_image * moo_sci_single_get_image(moo_sci_single *self)
Get image of SCI_SINGLE.
cpl_image * moo_sci_single_get_qual(moo_sci_single *self)
Get image of qual.
moo_sci_single * moo_sci_load_single(moo_sci *self, moo_detector_type type, int level)
Load the type part in SCI and return it.
Definition: moo_sci.c:323
void moo_sci_delete(moo_sci *self)
Delete a moo_sci.
Definition: moo_sci.c:84
moo_sci * moo_sci_create(const cpl_frame *frame)
Create a new empty SCI filename.
Definition: moo_sci.c:249
moo_sci_single * moo_sci_get_single(moo_sci *self, moo_detector_type type)
Get the type part in SCI and return it.
Definition: moo_sci.c:351
moo_target_table * moo_sci_get_target_table(moo_sci *self)
Get the target table of SCI file.
Definition: moo_sci.c:284
moo_sci * moo_scilist_get(moo_scilist *self, int i)
Get the SCI at the position i in the list.
Definition: moo_scilist.c:384
moo_sci * moo_scilist_unset(moo_scilist *self, cpl_size pos)
Remove a SCI from a SCI list.
Definition: moo_scilist.c:82
hdrl_imagelist * moo_scilist_get_image(const moo_scilist *self, moo_detector_type type)
Get the all the images of the type part in the scilist.
Definition: moo_scilist.c:468
void moo_scilist_delete(moo_scilist *self)
Free all memory used by a moo_scilist object including the SCI.
Definition: moo_scilist.c:176
cpl_imagelist * moo_scilist_get_qual(const moo_scilist *self, moo_detector_type type)
Get the all the QUAL of the type part in the scilist.
Definition: moo_scilist.c:514
moo_sci_single * moo_scilist_get_single(moo_scilist *self, int i, moo_detector_type type)
Get the SCI SINGLE at the position i in the list for the given detector.
Definition: moo_scilist.c:409
cpl_error_code moo_scilist_load_single(const moo_scilist *self, moo_detector_type type, int level)
Load the type part for all SCI in the scilist.
Definition: moo_scilist.c:439
moo_scilist * moo_scilist_create(cpl_frameset *frameset)
Create a new moo_scilist from the given SCI frameset.
Definition: moo_scilist.c:321
cpl_error_code moo_scilist_set(moo_scilist *self, moo_sci *sci, cpl_size pos)
Insert a SCI into an moo_scilist.
Definition: moo_scilist.c:240
moo_scilist * moo_scilist_create_clean(cpl_frameset *frameset)
Create a new moo_scilist from the given SCI frameset ignoring SCI with 0 targets.
Definition: moo_scilist.c:345
moo_scilist * moo_scilist_new(void)
Create a new moo_scilist.
Definition: moo_scilist.c:56
void moo_scilist_unwrap(moo_scilist *self)
Free memory used by a moo_scilist object, except the SCI.
Definition: moo_scilist.c:156
void moo_scilist_empty(moo_scilist *self)
Empty an moo_scilist and deallocate all its SCI.
Definition: moo_scilist.c:120
cpl_size moo_scilist_get_size(const moo_scilist *self)
Get the number of SCI in the scilist.
Definition: moo_scilist.c:198
cpl_error_code moo_scilist_push(moo_scilist *self, moo_sci *sci)
Insert a SCI a the end of moo_scilist.
Definition: moo_scilist.c:304
cpl_imagelist * moo_scilist_get_sky(const moo_scilist *self, moo_detector_type type)
Get the all the SKY of the type part in the scilist.
Definition: moo_scilist.c:560
the different type of detectors