MOONS Pipeline Reference Manual 0.13.1
moo_extlist.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_extlist.h"
32#include "moo_badpix.h"
33#include "moo_utils.h"
34/*----------------------------------------------------------------------------*/
40/*----------------------------------------------------------------------------*/
41
44/*-----------------------------------------------------------------------------
45 Function codes
46 -----------------------------------------------------------------------------*/
47
48/*----------------------------------------------------------------------------*/
56moo_extlist *
58{
59 return (moo_extlist *)cpl_calloc(1, sizeof(moo_extlist));
60}
61
62/*----------------------------------------------------------------------------*/
71moo_extlist *
72moo_extlist_create(cpl_frameset *frameset)
73{
74 cpl_ensure(frameset != NULL, CPL_ERROR_NULL_INPUT, NULL);
75 int i;
76 moo_extlist *res = moo_extlist_new();
77
78 for (i = 0; i < cpl_frameset_get_size(frameset); ++i) {
79 const cpl_frame *current_frame =
80 cpl_frameset_get_position_const(frameset, i);
81 moo_ext *ext = moo_ext_create(current_frame);
82 moo_extlist_push(res, ext);
83 }
84
85 return res;
86}
87
88/*----------------------------------------------------------------------------*/
106/*----------------------------------------------------------------------------*/
107cpl_error_code
108moo_extlist_load_single(const moo_extlist *self,
110 int num,
111 int level)
112{
113 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
114 cpl_ensure_code(num >= 1 && num <= 2, CPL_ERROR_ILLEGAL_INPUT);
115
116 int i;
117
118 for (i = 0; i < self->size; i++) {
119 moo_ext *ext = self->list[i];
120 moo_ext_load_single(ext, type, num, level);
121 }
122 return CPL_ERROR_NONE;
123}
124
125/*----------------------------------------------------------------------------*/
142/*----------------------------------------------------------------------------*/
143cpl_error_code
144moo_extlist_free_single(const moo_extlist *self,
146 int num)
147{
148 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
149 cpl_ensure_code(num >= 1 && num <= 2, CPL_ERROR_ILLEGAL_INPUT);
150
151 int i;
152 for (i = 0; i < self->size; i++) {
153 moo_ext *ext = self->list[i];
154 cpl_error_code status = moo_ext_free_single(ext, type, num);
155 if (status != CPL_ERROR_NONE) {
156 return status;
157 }
158 }
159 return CPL_ERROR_NONE;
160}
161
162/*----------------------------------------------------------------------------*/
172/*----------------------------------------------------------------------------*/
173cpl_size
174moo_extlist_get_size(const moo_extlist *self)
175{
176 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, -1);
177
178 assert(self->size >= 0);
179
180 return self->size;
181}
182
183/*----------------------------------------------------------------------------*/
195/*----------------------------------------------------------------------------*/
196moo_ext *
197moo_extlist_get(moo_extlist *self, int i)
198{
199 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
200 cpl_ensure(i >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
201 int size = moo_extlist_get_size(self);
202 cpl_ensure(i < size, CPL_ERROR_ILLEGAL_INPUT, NULL);
203
204 return self->list[i];
205}
206
207/*----------------------------------------------------------------------------*/
224/*----------------------------------------------------------------------------*/
225hdrl_imagelist *
226moo_extlist_get_image(const moo_extlist *self, moo_detector_type type, int num)
227{
228 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
229 cpl_ensure(num >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
230 cpl_ensure(num <= 2, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
231
232 cpl_errorstate prestate = cpl_errorstate_get();
233
234 hdrl_imagelist *res = NULL;
235 int i;
236
237 res = hdrl_imagelist_new();
238
239 for (i = 0; i < self->size; i++) {
240 moo_ext *ext = self->list[i];
241 moo_ext_single *single = NULL;
242 moo_try_check(single = moo_ext_get_single(ext, type, num), " ");
243
244 if (single != NULL) {
245 hdrl_image *img = moo_ext_single_get_image(single);
246 hdrl_imagelist_set(res, img, i);
247 }
248 }
249
250moo_try_cleanup:
251 if (!cpl_errorstate_is_equal(prestate)) {
252 cpl_msg_error(__func__, "Error in extlist");
253 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
254 hdrl_imagelist_delete(res);
255 res = NULL;
256 }
257 return res;
258 return res;
259}
260
261/*----------------------------------------------------------------------------*/
278/*----------------------------------------------------------------------------*/
279cpl_imagelist *
280moo_extlist_get_single_data(const moo_extlist *self,
282 int num)
283{
284 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
285 cpl_ensure(num >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
286 cpl_ensure(num <= 2, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
287
288 cpl_imagelist *res = cpl_imagelist_new();
289 int i;
290
291 for (i = 0; i < self->size; i++) {
292 moo_ext *ext = self->list[i];
293 moo_ext_single *single = moo_ext_get_single(ext, type, num);
294 if (single != NULL) {
295 cpl_imagelist_set(res, moo_ext_single_get_data(single), i);
296 }
297 }
298
299 return res;
300}
301
302/*----------------------------------------------------------------------------*/
319/*----------------------------------------------------------------------------*/
320cpl_imagelist *
321moo_extlist_get_single_qual(const moo_extlist *self,
323 int num)
324{
325 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
326 cpl_ensure(num >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
327 cpl_ensure(num <= 2, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
328
329 cpl_imagelist *res = cpl_imagelist_new();
330 int i;
331
332 for (i = 0; i < self->size; i++) {
333 moo_ext *ext = self->list[i];
334 moo_ext_single *single = moo_ext_get_single(ext, type, num);
335 if (single != NULL) {
336 cpl_imagelist_set(res, moo_ext_single_get_qual(single), i);
337 }
338 }
339
340 return res;
341}
342
343/*----------------------------------------------------------------------------*/
374/*----------------------------------------------------------------------------*/
375cpl_error_code
376moo_extlist_set(moo_extlist *self, moo_ext *ext, cpl_size pos)
377{
378 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
379 cpl_ensure_code(ext, CPL_ERROR_NULL_INPUT);
380 cpl_ensure_code(pos >= 0, CPL_ERROR_ILLEGAL_INPUT);
381 cpl_ensure_code(pos <= self->size, CPL_ERROR_ACCESS_OUT_OF_RANGE);
382
383 /* Do nothing if the ext is already there */
384 if (pos < self->size && ext == self->list[pos])
385 return CPL_ERROR_NONE;
386
387 if (pos == self->size) {
388 self->size++;
389 self->list =
390 cpl_realloc(self->list, (size_t)self->size * sizeof(moo_ext *));
391 }
392 else {
393 /* Check if the ext at the position to be overwritten
394 is present in only one position */
395 int i;
396
397 for (i = 0; i < self->size; i++) {
398 if (i != pos && self->list[i] == self->list[pos])
399 break;
400 }
401 if (i == self->size) {
402 /* The image at the position to be overwritten
403 is present in only one position, so delete it */
404 moo_ext_delete(self->list[pos]);
405 }
406 }
407
408 self->list[pos] = ext;
409
410 return CPL_ERROR_NONE;
411}
412
413/*----------------------------------------------------------------------------*/
438/*----------------------------------------------------------------------------*/
439cpl_error_code
440moo_extlist_push(moo_extlist *self, moo_ext *ext)
441{
442 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
443 moo_extlist_set(self, ext, self->size);
444 return cpl_error_get_code();
445}
446/*----------------------------------------------------------------------------*/
465/*----------------------------------------------------------------------------*/
466moo_ext *
467moo_extlist_unset(moo_extlist *self, cpl_size pos)
468{
469 moo_ext *out;
470 cpl_size i;
471
472 cpl_ensure(self, CPL_ERROR_NULL_INPUT, NULL);
473 cpl_ensure(pos >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
474 cpl_ensure(pos < self->size, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
475
476 /* Get pointer to DET to be removed */
477 out = self->list[pos];
478
479 /* Move the following DET one position towards zero */
480 for (i = pos + 1; i < self->size; i++) {
481 self->list[i - 1] = self->list[i];
482 }
483
484 /* Decrement of the size */
485 self->size--;
486
487 return out;
488}
489
490/*----------------------------------------------------------------------------*/
503/*----------------------------------------------------------------------------*/
504void
505moo_extlist_empty(moo_extlist *self)
506{
507 if (self != NULL) {
508 while (self->size > 0) { /* An iteration may unset more than 1 image */
509 int i = self->size - 1;
510 moo_ext *del = moo_extlist_unset(self, i);
511
512 moo_ext_delete(del);
513
514 /* If this image was inserted more than once into the list,
515 the other insertions must be unset without a delete. */
516 while (--i >= 0) {
517 if (self->list[i] == del) {
518 /* This image was inserted more than once in the list */
519 (void)moo_extlist_unset(self, i);
520 }
521 }
522 }
523 }
524
525 return;
526}
527
528/*----------------------------------------------------------------------------*/
539/*----------------------------------------------------------------------------*/
540void
541moo_extlist_unwrap(moo_extlist *self)
542{
543 if (self != NULL) {
544 cpl_free(self->list);
545 cpl_free(self);
546 }
547
548 return;
549}
550
551/*----------------------------------------------------------------------------*/
560void
561moo_extlist_delete(moo_extlist *self)
562{
563 if (self != NULL) {
564 moo_extlist_empty(self);
565 moo_extlist_unwrap(self);
566 }
567
568 return;
569}
570/*----------------------------------------------------------------------------*/
580moo_ext *
581moo_extlist_sum(moo_extlist *self, const char *filename)
582{
583 moo_ext *result = NULL;
584 cpl_frame *res_frame = NULL;
585 int size = 0;
586 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
587 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
588 moo_try_check(size = moo_extlist_get_size(self), " ");
589 if (size > 0) {
590 result = moo_extlist_get(self, 0);
593 moo_try_check(moo_ext_save(result, filename), " ");
594
595 res_frame = cpl_frame_new();
596 cpl_frame_set_filename(res_frame, filename);
597 result = moo_ext_create(res_frame);
600 for (int i = 1; i < size; i++) {
601 moo_ext *ext = moo_extlist_get(self, i);
603 moo_ext_sum(result, ext);
604 }
605 }
606moo_try_cleanup:
607 cpl_frame_delete(res_frame);
608 return result;
609}
#define MOO_BADPIX_GOOD
Definition: moo_badpix.h:36
cpl_error_code moo_ext_free_single(moo_ext *self, moo_detector_type type, int num)
Free the given type part in EXT.
Definition: moo_ext.c:486
enum _moo_detector_type_ moo_detector_type
The type code type.
Definition: moo_detector.h:64
hdrl_image * moo_ext_single_get_image(moo_ext_single *self)
Get image of EXT_SINGLE.
cpl_image * moo_ext_single_get_qual(moo_ext_single *self)
Get image of qual.
cpl_image * moo_ext_single_get_data(moo_ext_single *self)
Get image of data.
moo_ext_single * moo_ext_get_single(moo_ext *self, moo_detector_type type, int ntas)
Get a EXT single from EXT.
Definition: moo_ext.c:199
moo_ext_single * moo_ext_load_single(moo_ext *self, moo_detector_type type, int num, unsigned int level)
Load the type part in EXT and return it.
Definition: moo_ext.c:156
cpl_error_code moo_ext_load(moo_ext *self, unsigned int level)
Load all parts in EXT.
Definition: moo_ext.c:126
moo_ext * moo_ext_create(const cpl_frame *frame)
Create a new empty EXT filename.
Definition: moo_ext.c:86
cpl_error_code moo_ext_sum(moo_ext *self, moo_ext *ext)
Sum EXT structure.
Definition: moo_ext.c:514
cpl_table * moo_ext_get_fibre_table(moo_ext *self)
Get the FIBRE TABLE in EXT.
Definition: moo_ext.c:344
void moo_ext_save(moo_ext *self, const char *filename)
Save a moo_ext to a FITS file.
Definition: moo_ext.c:406
void moo_ext_delete(moo_ext *self)
Delete a moo_ext.
Definition: moo_ext.c:370
moo_ext * moo_extlist_get(moo_extlist *self, int i)
Get the DET at the position i in the list.
Definition: moo_extlist.c:197
cpl_imagelist * moo_extlist_get_single_data(const moo_extlist *self, moo_detector_type type, int num)
Get the type data part for all DET in the extlist.
Definition: moo_extlist.c:280
cpl_error_code moo_extlist_push(moo_extlist *self, moo_ext *ext)
Insert a DET a the end of moo_extlist.
Definition: moo_extlist.c:440
cpl_error_code moo_extlist_set(moo_extlist *self, moo_ext *ext, cpl_size pos)
Insert a DET into an moo_extlist.
Definition: moo_extlist.c:376
moo_ext * moo_extlist_unset(moo_extlist *self, cpl_size pos)
Remove a DET from a DET list.
Definition: moo_extlist.c:467
void moo_extlist_unwrap(moo_extlist *self)
Free memory used by a moo_extlist object, except the DET.
Definition: moo_extlist.c:541
void moo_extlist_empty(moo_extlist *self)
Empty an moo_extlist and deallocate all its DET.
Definition: moo_extlist.c:505
moo_ext * moo_extlist_sum(moo_extlist *self, const char *filename)
Sum EXT element from the list and save result in filename.
Definition: moo_extlist.c:581
cpl_error_code moo_extlist_free_single(const moo_extlist *self, moo_detector_type type, int num)
Free the type part for all DET in the extlist.
Definition: moo_extlist.c:144
cpl_imagelist * moo_extlist_get_single_qual(const moo_extlist *self, moo_detector_type type, int num)
Get the type QUAL part for all DET in the extlist.
Definition: moo_extlist.c:321
void moo_extlist_delete(moo_extlist *self)
Free all memory used by a moo_extlist object including the DET.
Definition: moo_extlist.c:561
hdrl_imagelist * moo_extlist_get_image(const moo_extlist *self, moo_detector_type type, int num)
Get the all the images of the type part in the extlist.
Definition: moo_extlist.c:226
moo_extlist * moo_extlist_create(cpl_frameset *frameset)
Create a new moo_extlist from the given DET frameset.
Definition: moo_extlist.c:72
cpl_size moo_extlist_get_size(const moo_extlist *self)
Get the number of DET in the extlist.
Definition: moo_extlist.c:174
cpl_error_code moo_extlist_load_single(const moo_extlist *self, moo_detector_type type, int num, int level)
Load the type part for all DET in the extlist.
Definition: moo_extlist.c:108
moo_extlist * moo_extlist_new(void)
Create a new moo_extlist.
Definition: moo_extlist.c:57