MOONS Pipeline Reference Manual 0.13.2
moo_detlist.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_detlist.h"
32#include "moo_utils.h"
33/*----------------------------------------------------------------------------*/
39/*----------------------------------------------------------------------------*/
40
43/*-----------------------------------------------------------------------------
44 Function codes
45 -----------------------------------------------------------------------------*/
46
47/*----------------------------------------------------------------------------*/
55moo_detlist *
57{
58 return (moo_detlist *)cpl_calloc(1, sizeof(moo_detlist));
59}
60
61/*----------------------------------------------------------------------------*/
70moo_detlist *
71moo_detlist_create(cpl_frameset *frameset)
72{
73 cpl_ensure(frameset != NULL, CPL_ERROR_NULL_INPUT, NULL);
74 int i;
75 moo_detlist *res = moo_detlist_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_det *det = moo_det_create(current_frame);
81 moo_detlist_push(res, det);
82 }
83
84 return res;
85}
86
87/*----------------------------------------------------------------------------*/
105/*----------------------------------------------------------------------------*/
106cpl_error_code
107moo_detlist_load_single(const moo_detlist *self,
109 int num,
110 int level)
111{
112 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
113 cpl_ensure_code(num >= 1 && num <= 2, CPL_ERROR_ILLEGAL_INPUT);
114
115 int i;
116
117 for (i = 0; i < self->size; i++) {
118 moo_det *det = self->list[i];
119 moo_det_load_single(det, type, num, level);
120 }
121 return CPL_ERROR_NONE;
122}
123
124/*----------------------------------------------------------------------------*/
141/*----------------------------------------------------------------------------*/
142cpl_error_code
143moo_detlist_free_single(const moo_detlist *self,
145 int num)
146{
147 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
148 cpl_ensure_code(num >= 1 && num <= 2, CPL_ERROR_ILLEGAL_INPUT);
149
150 int i;
151 for (i = 0; i < self->size; i++) {
152 moo_det *det = self->list[i];
153 cpl_error_code status = moo_det_free_single(det, type, num);
154 if (status != CPL_ERROR_NONE) {
155 return status;
156 }
157 }
158 return CPL_ERROR_NONE;
159}
160
161/*----------------------------------------------------------------------------*/
171/*----------------------------------------------------------------------------*/
172cpl_size
173moo_detlist_get_size(const moo_detlist *self)
174{
175 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, -1);
176
177 assert(self->size >= 0);
178
179 return self->size;
180}
181
182/*----------------------------------------------------------------------------*/
194/*----------------------------------------------------------------------------*/
195moo_det *
196moo_detlist_get(moo_detlist *self, int i)
197{
198 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
199 cpl_ensure(i >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
200 int size = moo_detlist_get_size(self);
201 cpl_ensure(i < size, CPL_ERROR_ILLEGAL_INPUT, NULL);
202
203 return self->list[i];
204}
205
206/*----------------------------------------------------------------------------*/
223/*----------------------------------------------------------------------------*/
224hdrl_imagelist *
225moo_detlist_get_image(const moo_detlist *self, moo_detector_type type, int num)
226{
227 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
228 cpl_ensure(num >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
229 cpl_ensure(num <= 2, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
230
231 cpl_errorstate prestate = cpl_errorstate_get();
232
233 hdrl_imagelist *res = NULL;
234 int i;
235
236 res = hdrl_imagelist_new();
237
238 for (i = 0; i < self->size; i++) {
239 moo_det *det = self->list[i];
240 moo_single *single = NULL;
241 moo_try_check(single = moo_det_get_single(det, type, num), " ");
242
243 if (single != NULL) {
244 hdrl_image *img = moo_single_get_image(single);
245 hdrl_imagelist_set(res, img, i);
246 }
247 }
248
249moo_try_cleanup:
250 if (!cpl_errorstate_is_equal(prestate)) {
251 cpl_msg_error(__func__, "Error in detlist");
252 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
253 hdrl_imagelist_delete(res);
254 res = NULL;
255 }
256 return res;
257 return res;
258}
259
260/*----------------------------------------------------------------------------*/
277/*----------------------------------------------------------------------------*/
278cpl_imagelist *
279moo_detlist_get_single_data(const moo_detlist *self,
281 int num)
282{
283 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
284 cpl_ensure(num >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
285 cpl_ensure(num <= 2, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
286
287 cpl_imagelist *res = cpl_imagelist_new();
288 int i;
289
290 for (i = 0; i < self->size; i++) {
291 moo_det *det = self->list[i];
292 moo_single *single = moo_det_get_single(det, type, num);
293 if (single != NULL) {
294 cpl_imagelist_set(res, moo_single_get_data(single), i);
295 }
296 }
297
298 return res;
299}
300
301/*----------------------------------------------------------------------------*/
318/*----------------------------------------------------------------------------*/
319cpl_imagelist *
320moo_detlist_get_single_qual(const moo_detlist *self,
322 int num)
323{
324 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
325 cpl_ensure(num >= 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
326 cpl_ensure(num <= 2, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
327
328 cpl_imagelist *res = cpl_imagelist_new();
329 int i;
330
331 for (i = 0; i < self->size; i++) {
332 moo_det *det = self->list[i];
333 moo_single *single = moo_det_get_single(det, type, num);
334 if (single != NULL) {
335 cpl_imagelist_set(res, moo_single_get_qual(single), i);
336 }
337 }
338
339 return res;
340}
341
342/*----------------------------------------------------------------------------*/
373/*----------------------------------------------------------------------------*/
374cpl_error_code
375moo_detlist_set(moo_detlist *self, moo_det *det, cpl_size pos)
376{
377 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
378 cpl_ensure_code(det, CPL_ERROR_NULL_INPUT);
379 cpl_ensure_code(pos >= 0, CPL_ERROR_ILLEGAL_INPUT);
380 cpl_ensure_code(pos <= self->size, CPL_ERROR_ACCESS_OUT_OF_RANGE);
381
382 /* Do nothing if the det is already there */
383 if (pos < self->size && det == self->list[pos])
384 return CPL_ERROR_NONE;
385
386 if (pos == self->size) {
387 self->size++;
388 self->list =
389 cpl_realloc(self->list, (size_t)self->size * sizeof(moo_det *));
390 }
391 else {
392 /* Check if the det at the position to be overwritten
393 is present in only one position */
394 int i;
395
396 for (i = 0; i < self->size; i++) {
397 if (i != pos && self->list[i] == self->list[pos])
398 break;
399 }
400 if (i == self->size) {
401 /* The image at the position to be overwritten
402 is present in only one position, so delete it */
403 moo_det_delete(self->list[pos]);
404 }
405 }
406
407 self->list[pos] = det;
408
409 return CPL_ERROR_NONE;
410}
411
412/*----------------------------------------------------------------------------*/
437/*----------------------------------------------------------------------------*/
438cpl_error_code
439moo_detlist_push(moo_detlist *self, moo_det *det)
440{
441 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
442 return moo_detlist_set(self, det, self->size);
443}
444/*----------------------------------------------------------------------------*/
463/*----------------------------------------------------------------------------*/
464moo_det *
465moo_detlist_unset(moo_detlist *self, cpl_size pos)
466{
467 moo_det *out;
468 cpl_size i;
469
470 cpl_ensure(self, CPL_ERROR_NULL_INPUT, NULL);
471 cpl_ensure(pos >= 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
472 cpl_ensure(pos < self->size, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
473
474 /* Get pointer to DET to be removed */
475 out = self->list[pos];
476
477 /* Move the following DET one position towards zero */
478 for (i = pos + 1; i < self->size; i++) {
479 self->list[i - 1] = self->list[i];
480 }
481
482 /* Decrement of the size */
483 self->size--;
484
485 return out;
486}
487
488/*----------------------------------------------------------------------------*/
501/*----------------------------------------------------------------------------*/
502void
503moo_detlist_empty(moo_detlist *self)
504{
505 if (self != NULL) {
506 while (self->size > 0) { /* An iteration may unset more than 1 image */
507 int i = self->size - 1;
508 moo_det *del = moo_detlist_unset(self, i);
509
510 moo_det_delete(del);
511
512 /* If this image was inserted more than once into the list,
513 the other insertions must be unset without a delete. */
514 while (--i >= 0) {
515 if (self->list[i] == del) {
516 /* This image was inserted more than once in the list */
517 (void)moo_detlist_unset(self, i);
518 }
519 }
520 }
521 }
522
523 return;
524}
525
526/*----------------------------------------------------------------------------*/
537/*----------------------------------------------------------------------------*/
538void
539moo_detlist_unwrap(moo_detlist *self)
540{
541 if (self != NULL) {
542 cpl_free(self->list);
543 cpl_free(self);
544 }
545
546 return;
547}
548
549/*----------------------------------------------------------------------------*/
558void
559moo_detlist_delete(moo_detlist *self)
560{
561 if (self != NULL) {
562 moo_detlist_empty(self);
563 moo_detlist_unwrap(self);
564 }
565
566 return;
567}
568
moo_single * moo_det_load_single(moo_det *self, moo_detector_type type, int num, int level)
Load the type part in DET and return it.
Definition: moo_det.c:197
cpl_error_code moo_det_free_single(moo_det *self, moo_detector_type type, int num)
Free the given type part in DET.
Definition: moo_det.c:231
moo_det * moo_det_create(const cpl_frame *frame)
Create a new moo_det from the given DET frame.
Definition: moo_det.c:91
void moo_det_delete(moo_det *self)
Delete a moo_det.
Definition: moo_det.c:472
moo_single * moo_det_get_single(moo_det *self, moo_detector_type type, int num)
Get the type part in DET and return it.
Definition: moo_det.c:359
enum _moo_detector_type_ moo_detector_type
The type code type.
Definition: moo_detector.h:64
hdrl_imagelist * moo_detlist_get_image(const moo_detlist *self, moo_detector_type type, int num)
Get the all the images of the type part in the detlist.
Definition: moo_detlist.c:225
cpl_error_code moo_detlist_push(moo_detlist *self, moo_det *det)
Insert a DET a the end of moo_detlist.
Definition: moo_detlist.c:439
moo_detlist * moo_detlist_create(cpl_frameset *frameset)
Create a new moo_detlist from the given DET frameset.
Definition: moo_detlist.c:71
cpl_size moo_detlist_get_size(const moo_detlist *self)
Get the number of DET in the detlist.
Definition: moo_detlist.c:173
moo_det * moo_detlist_unset(moo_detlist *self, cpl_size pos)
Remove a DET from a DET list.
Definition: moo_detlist.c:465
moo_detlist * moo_detlist_new(void)
Create a new moo_detlist.
Definition: moo_detlist.c:56
void moo_detlist_empty(moo_detlist *self)
Empty an moo_detlist and deallocate all its DET.
Definition: moo_detlist.c:503
void moo_detlist_unwrap(moo_detlist *self)
Free memory used by a moo_detlist object, except the DET.
Definition: moo_detlist.c:539
cpl_error_code moo_detlist_free_single(const moo_detlist *self, moo_detector_type type, int num)
Free the type part for all DET in the detlist.
Definition: moo_detlist.c:143
cpl_imagelist * moo_detlist_get_single_data(const moo_detlist *self, moo_detector_type type, int num)
Get the type data part for all DET in the detlist.
Definition: moo_detlist.c:279
cpl_imagelist * moo_detlist_get_single_qual(const moo_detlist *self, moo_detector_type type, int num)
Get the type QUAL part for all DET in the detlist.
Definition: moo_detlist.c:320
cpl_error_code moo_detlist_load_single(const moo_detlist *self, moo_detector_type type, int num, int level)
Load the type part for all DET in the detlist.
Definition: moo_detlist.c:107
void moo_detlist_delete(moo_detlist *self)
Free all memory used by a moo_detlist object including the DET.
Definition: moo_detlist.c:559
moo_det * moo_detlist_get(moo_detlist *self, int i)
Get the DET at the position i in the list.
Definition: moo_detlist.c:196
cpl_error_code moo_detlist_set(moo_detlist *self, moo_det *det, cpl_size pos)
Insert a DET into an moo_detlist.
Definition: moo_detlist.c:375
hdrl_image * moo_single_get_image(moo_single *self)
Get the IMAGE part (DATA,ERR) of single DET.
Definition: moo_single.c:330
cpl_image * moo_single_get_data(moo_single *self)
Get the DATA part of single DET.
Definition: moo_single.c:239
cpl_image * moo_single_get_qual(moo_single *self)
Get the QUAL part of single DET.
Definition: moo_single.c:312