ERIS Pipeline Reference Manual 1.9.2
eris_nix_dfs.c
1/* $Id$
2 *
3 * This file is part of the ERIS/NIX Pipeline
4 * Copyright (C) 2017 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * $Author$
23 * $Date$
24 * $Rev$
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*-----------------------------------------------------------------------------
32 Includes
33 -----------------------------------------------------------------------------*/
34
35#include <string.h>
36#include <libgen.h>
37#include <cpl.h>
38#include <hdrl.h>
39#include "eris_nix_dfs.h"
40#include "eris_nix_utils.h"
41#include "eris_pfits.h"
42#include "eris_utils.h"
43/*----------------------------------------------------------------------------*/
47/*----------------------------------------------------------------------------*/
48
51/*----------------------------------------------------------------------------*/
57/*----------------------------------------------------------------------------*/
58cpl_error_code eris_nix_dfs_set_groups(cpl_frameset * set) {
59
60 if (cpl_error_get_code() != CPL_ERROR_NONE) return cpl_error_get_code();
61
62 cpl_frame * frame = NULL;
63 int i = 0;
64
65 /* Loop on frames */
66
67 cpl_frameset_iterator * fsiter = cpl_frameset_iterator_new(set);
68 frame = cpl_frameset_iterator_get(fsiter);
69 while (frame) {
70
71 const char * tag = cpl_frame_get_tag(frame);
72 int data_found = 0;
73
74 if (tag == NULL) {
75 cpl_msg_error(cpl_func, "Frame %d has no tag", i);
76 } else if (!strcmp(tag, ERIS_NIX_RAW_DARK_DO_CATG) ||
77 !strcmp(tag, ERIS_NIX_RAW_PERSISTENCE_IMG_DO_CATG) ||
78 !strcmp(tag, ERIS_NIX_RAW_PERSISTENCE_CUBE_DO_CATG) ||
79 !strcmp(tag, ERIS_NIX_RAW_FLAT_LAMP_ON_DO_CATG) ||
80 !strcmp(tag, ERIS_NIX_RAW_FLAT_LAMP_OFF_DO_CATG) ||
81 !strcmp(tag, ERIS_NIX_RAW_FLAT_SKY_DO_CATG) ||
82 !strcmp(tag, ERIS_NIX_RAW_FLAT_SKY_LSS_DO_CATG) ||
83 !strcmp(tag, ERIS_NIX_RAW_FLAT_TWILIGHT_DO_CATG) ||
84 !strcmp(tag, ERIS_NIX_RAW_PUPIL_LAMP_DO_CATG) ||
85 !strcmp(tag, ERIS_NIX_RAW_PUPIL_SKY_DO_CATG) ||
86 !strcmp(tag, ERIS_NIX_RAW_PUPIL_DARK_DO_CATG) ||
87 !strcmp(tag, ERIS_NIX_RAW_PUPIL_BKG_DO_CATG) ||
88 !strcmp(tag, ERIS_NIX_RAW_PUPIL_LAMP_OPEN_DO_CATG) ||
89 !strcmp(tag, ERIS_NIX_RAW_PUPIL_LAMP_MASK_DO_CATG) ||
90 !strcmp(tag, ERIS_NIX_RAW_PUPIL_SKY_OPEN_DO_CATG) ||
91 !strcmp(tag, ERIS_NIX_RAW_PUPIL_SKY_MASK_DO_CATG) ||
92 !strcmp(tag, ERIS_NIX_RAW_PUPIL_DARK_OPEN_DO_CATG) ||
93 !strcmp(tag, ERIS_NIX_RAW_PUPIL_DARK_MASK_DO_CATG) ||
94 !strcmp(tag, ERIS_NIX_RAW_OBJECT_APP_DO_CATG) ||
95 !strcmp(tag, ERIS_NIX_RAW_OBJECT_FPC_DO_CATG) ||
96 !strcmp(tag, ERIS_NIX_RAW_OBJECT_SAM_DO_CATG) ||
97 !strcmp(tag, ERIS_NIX_RAW_OBJECT_JITTER_DO_CATG) ||
98 !strcmp(tag, ERIS_NIX_RAW_SKY_FPC_DO_CATG) ||
99 !strcmp(tag, ERIS_NIX_RAW_SKY_APP_DO_CATG) ||
100 !strcmp(tag, ERIS_NIX_RAW_SKY_JITTER_DO_CATG) ||
101 !strcmp(tag, ERIS_NIX_RAW_STD_JITTER_DO_CATG) ||
102 !strcmp(tag, ERIS_NIX_RAW_OBJECT_LSS_JITTER_DO_CATG) ||
103 !strcmp(tag, ERIS_NIX_RAW_STD_APP_DO_CATG) ||
104 !strcmp(tag, ERIS_NIX_RAW_STD_FPC_DO_CATG) ||
105 !strcmp(tag, ERIS_NIX_RAW_STD_SAM_DO_CATG) ||
106 !strcmp(tag, ERIS_NIX_RAW_SKY_LSS_JITTER_DO_CATG) ||
107 !strcmp(tag, ERIS_NIX_RAW_STD_LSS_JITTER_DO_CATG)) {
108
109 cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
110 data_found = 1;
111
112 } else if (!strcmp(tag, ERIS_NIX_COEFFS_CUBE_PRO_CATG) ||
113 !strcmp(tag, ERIS_NIX_DET_LIN_INFO_PRO_CATG) ||
114 !strcmp(tag, ERIS_NIX_GAIN_PRO_CATG) ||
115 !strcmp(tag, ERIS_NIX_MASTER_BPM_LAMP_PRO_CATG) ||
116 !strcmp(tag, ERIS_NIX_MASTER_BPM_LSS_PRO_CATG) ||
117 !strcmp(tag, ERIS_NIX_MASTER_BPM_SKY_PRO_CATG) ||
118 !strcmp(tag, ERIS_NIX_MASTER_DARK_IMG_PRO_CATG) ||
119 !strcmp(tag, ERIS_NIX_MASTER_DARK_IFU_PRO_CATG) ||
120 !strcmp(tag, ERIS_NIX_MASTER_FLAT_LAMP_HIFREQ_PRO_CATG) ||
121 !strcmp(tag, ERIS_NIX_MASTER_FLAT_LAMP_LOFREQ_PRO_CATG) ||
122 !strcmp(tag, ERIS_NIX_MASTER_FLAT_LSS_HIFREQ_PRO_CATG) ||
123 !strcmp(tag, ERIS_NIX_MASTER_FLAT_LSS_LOFREQ_PRO_CATG) ||
124 !strcmp(tag, ERIS_NIX_MASTER_FLAT_SKY_LOFREQ_PRO_CATG) ||
125 !strcmp(tag, ERIS_NIX_MASTER_FLAT_SKY_HIFREQ_PRO_CATG) ||
126 !strcmp(tag, ERIS_NIX_MASTER_FLAT_TWILIGHT_LOFREQ_PRO_CATG)||
127 !strcmp(tag, ERIS_NIX_MASTER_STARTRACE_PRO_CATG)||
128 !strcmp(tag, ERIS_NIX_MASTER_WAVE_PRO_CATG)||
129 !strcmp(tag, ERIS_NIX_NL_BPM_PRO_CATG) ||
130 !strcmp(tag, ERIS_NIX_PHOT_DATA_PRO_CATG) ||
131 !strcmp(tag, ERIS_NIX_WCS_MATCHED_CATALOGUE_PRO_CATG) ||
132 !strcmp(tag, ERIS_NIX_WCS_REFINE_PRO_CATG) ||
133 !strcmp(tag, ERIS_NIX_REFERENCE_DARK_PRO_CATG)) {
134
135 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB);
136 data_found = 1;
137
138 } else if (!strcmp(tag, ERIS_NIX_CALIB_OBJECT_JITTER_PRO_CATG) ||
139 !strcmp(tag, ERIS_NIX_CAL_DET_OBJECT_JITTER_PRO_CATG) ||
140 !strcmp(tag, ERIS_NIX_CAL_DET_SKY_JITTER_PRO_CATG) ||
141 !strcmp(tag, ERIS_NIX_CAL_DET_STD_JITTER_PRO_CATG) ||
142 !strcmp(tag, ERIS_NIX_CAL_DET_OBJECT_LSS_JITTER_PRO_CATG) ||
143 !strcmp(tag, ERIS_NIX_CAL_DET_SKY_LSS_JITTER_PRO_CATG) ||
144 !strcmp(tag, ERIS_NIX_CAL_DET_STD_LSS_JITTER_PRO_CATG) ||
145 !strcmp(tag, ERIS_NIX_CAL_PHOT_CATALOGUE_PRO_CATG) ||
146 !strcmp(tag, ERIS_NIX_CAL_PHOT_MATCH_CATALOGUE_PRO_CATG) ||
147 !strcmp(tag, ERIS_NIX_CAL_PHOT_OBJECT_JITTER_PRO_CATG) ||
148 !strcmp(tag, ERIS_NIX_CAL_PHOT_REF_CATALOGUE_PRO_CATG) ||
149 !strcmp(tag, ERIS_NIX_CAL_PHOT_STD_JITTER_PRO_CATG) ||
150 !strcmp(tag, ERIS_NIX_CAL_WCS_CATALOGUE_PRO_CATG) ||
151 !strcmp(tag, ERIS_NIX_CAL_WCS_MATCH_CATALOGUE_PRO_CATG) ||
152 !strcmp(tag, ERIS_NIX_CAL_WCS_OBJECT_JITTER_PRO_CATG) ||
153 !strcmp(tag, ERIS_NIX_CAL_WCS_REF_CATALOGUE_PRO_CATG) ||
154 !strcmp(tag, ERIS_NIX_CAL_WCS_REPORT_PRO_CATG) ||
155 !strcmp(tag, ERIS_NIX_CAL_WCS_STD_JITTER_PRO_CATG) ||
156 !strcmp(tag, ERIS_NIX_CORRECTED_OBJECT_LSS_JITTER_PRO_CATG) ||
157 !strcmp(tag, ERIS_NIX_CORRECTED_STD_LSS_JITTER_PRO_CATG) ||
158 !strcmp(tag, ERIS_NIX_IMG_CATALOGUE_PRO_CATG) ||
159 !strcmp(tag, ERIS_NIX_IMG_OBS_COMBINED_PRO_CATG) ||
160 !strcmp(tag, ERIS_NIX_IMG_STD_COMBINED_PRO_CATG) ||
161 !strcmp(tag, ERIS_NIX_LSS_OBS_COMBINED_PRO_CATG) ||
162 !strcmp(tag, ERIS_NIX_LSS_STD_COMBINED_PRO_CATG) ||
163 !strcmp(tag, ERIS_NIX_SKYSUB_OBJECT_JITTER_PRO_CATG) ||
164 !strcmp(tag, ERIS_NIX_SKYSUB_STD_JITTER_PRO_CATG) ||
165 !strcmp(tag, ERIS_NIX_SKYSUB_OBJECT_LSS_JITTER_PRO_CATG) ||
166 !strcmp(tag, ERIS_NIX_SKYSUB_STD_LSS_JITTER_PRO_CATG)) {
167
168/*
169 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
170*/
171 cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
172 data_found = 1;
173 } else {
174 cpl_frame_set_group(frame, CPL_FRAME_GROUP_NONE);
175 data_found = 1;
176 }
177
178 if (!data_found) {
179 if(tag != NULL) {
180 cpl_error_set_message("eris_nix_dfs_set_groups",
181 CPL_ERROR_UNSUPPORTED_MODE,
182 "data tag not recognised: %s", tag);
183 }
184 break;
185 }
186
187 cpl_frameset_iterator_advance(fsiter, 1);
188 frame = cpl_frameset_iterator_get(fsiter);
189 i++;
190 }
191 enu_check_error_code("Could not identify RAW and CALIB frames");
192
193 cleanup:
194 cpl_frameset_iterator_delete(fsiter);
195
196 return cpl_error_get_code();
197}
198
199
200/*----------------------------------------------------------------------------*/
217/*----------------------------------------------------------------------------*/
218cpl_error_code enu_dfs_save_bpm(
219 const char * procat,
220 cpl_frameset * allframes,
221 const cpl_parameterlist * parlist,
222 const cpl_frameset * usedframes,
223 const master_bpm * mbad_pix_map,
224 const char * recipe,
225 const cpl_propertylist * applist,
226 const char * pipe_id,
227 const char * filename) {
228
229 if (cpl_error_get_code() != CPL_ERROR_NONE) return cpl_error_get_code();
230
231 cpl_propertylist * plist = NULL;
232
233 if (applist == NULL) {
234 plist = cpl_propertylist_new();
235 } else {
236 plist = cpl_propertylist_duplicate(applist);
237 }
238 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, procat);
239 cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL,
240 mbad_pix_map->bpm, CPL_TYPE_INT, recipe, plist, NULL, pipe_id, filename);
241 cpl_ensure_code(!cpl_error_get_code(), cpl_error_get_code());
242
243 /* Cleanup */
244
245 cpl_propertylist_delete(plist);
246
247 return cpl_error_get_code();
248}
249
250
251/*----------------------------------------------------------------------------*/
272/*----------------------------------------------------------------------------*/
273
274cpl_error_code enu_dfs_save_catalogue(cpl_frameset * frameset,
275 cpl_frame * image,
276 const cpl_parameterlist * parlist,
277 const cpl_table * catalogue,
278 const cpl_propertylist * tablelist,
279 const char * cat_name,
280 const char * recipe,
281 const char * pipe_id,
282 const char * preface,
283 const char * procat) {
284
285 /* immediate return for some conditions */
286
287 if (cpl_error_get_code() != CPL_ERROR_NONE) return cpl_error_get_code();
288 if (!catalogue) return CPL_ERROR_NONE;
289
290 /* otherwise, check inputs */
291
292 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
293 cpl_ensure_code(cat_name, CPL_ERROR_NULL_INPUT);
294 cpl_ensure_code(preface, CPL_ERROR_NULL_INPUT);
295
296 cpl_propertylist * applist = NULL;
297 cpl_propertylist * image_plist = NULL;
298 char * prodname = NULL;
299 cpl_frame * product_frame = NULL;
300 cpl_propertylist * tablelist_copy = NULL;
301
302 /* ..create a product frameset containing the input image of the
303 catalogue */
304
305 cpl_frameset * provenance = cpl_frameset_new();
306 cpl_frameset_insert(provenance, cpl_frame_duplicate(image));
307
308 /* ..create the product filename and frame */
309
310 prodname = enu_repreface(cpl_frame_get_filename(image), preface);
311 product_frame = cpl_frame_new();
312 cpl_frame_set_filename(product_frame, prodname);
313 cpl_frame_set_tag(product_frame, procat);
314 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
315 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
316 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
317
318 /* ..set up the phase3/IDP keywords for the primary header */
319
320 applist = cpl_propertylist_new();
321
322 /* ..copy some items from the image header */
323
324 image_plist = cpl_propertylist_load(cpl_frame_get_filename(image), 0);
325 cpl_propertylist_copy_property_regexp(applist,
326 image_plist,
327 "ORIGIN|TELESCOP|INSTRUME|FILTER|"
328 "OBJECT|RA|DEC|EQUINOX|RADESYS|"
329 "TIMESYS|EXPTIME|TEXPTIME|MJD-OBS|"
330 "MJD-END|PROG_ID|OBID[1-9]([0-9])*|"
331 "NCOMBINE|OBSTECH|PROCSOFT|"
332 "REFERENC",
333 CPL_FALSE);
334 enu_check_error_code("error copying image HDU info to applist");
335
336 /* ..add other standard keywords */
337
339 product_frame,
340 frameset,
341 parlist,
342 recipe,
343 pipe_id,
344 image,
345 provenance,
346 CPL_FALSE);
347
348 /* ..set new catg */
349
350 cpl_propertylist_update_string(applist, "PRODCATG", "SCIENCE.SRCTBL");
351 cpl_propertylist_update_string(applist, CPL_DFS_PRO_CATG, procat);
352
353 /* ..Point the PROV1 keyword to the associated image */
354
355 if (cpl_frame_get_filename(image)) {
356 char * filename = cpl_strdup(cpl_frame_get_filename(image));
357 cpl_propertylist_update_string(applist, "PROV1", basename(filename));
358 cpl_free(filename);
359 }
360
361 /* ..and set the plist for the table extension */
362
363 if (tablelist != NULL) {
364 tablelist_copy = cpl_propertylist_duplicate(tablelist);
365 } else {
366 tablelist_copy = cpl_propertylist_new();
367 }
368 cpl_propertylist_update_string(tablelist_copy, "EXTNAME", "PHASE3CATALOG");
369
370 /* OK, save the table */
371
372 cpl_table_save(catalogue,
373 applist,
374 tablelist_copy,
375 prodname,
376 CPL_IO_CREATE);
377
378 enu_check_error_code("error saving dfs table");
379
380 /* save the product frame to the overall frameset so that DFS knows a new
381 product has been created */
382
383 cpl_frameset_insert(frameset, cpl_frame_duplicate(product_frame));
384
385 cleanup:
386
387 cpl_propertylist_delete(applist);
388 cpl_propertylist_delete(image_plist);
389 cpl_free(prodname);
390 cpl_frame_delete(product_frame);
391 cpl_propertylist_delete(tablelist_copy);
392 cpl_frameset_delete(provenance);
393 eris_check_error_code("enu_dfs_save_catalogue");
394 return cpl_error_get_code();
395}
396
397
398/*----------------------------------------------------------------------------*/
422/*----------------------------------------------------------------------------*/
423cpl_error_code enu_dfs_save_himage(cpl_frameset * allframes,
424 const cpl_parameterlist * parlist,
425 const cpl_frameset * provenance,
426 const cpl_boolean prov_raw,
427 const hdrl_image * image,
428 const hdrl_imagelist * imagelist,
429 const mef_extension_list* mefs,
430 const char * recipe,
431 const cpl_frame * inherit_frame,
432 const cpl_propertylist * applist,
433 const cpl_propertylist * wcs_plist,
434 const char * pipe_id,
435 const char * filename) {
436
437 if (cpl_error_get_code() != CPL_ERROR_NONE) return cpl_error_get_code();
438
439 cpl_msg_debug(cpl_func, "enu_dfs_save_himage entry");
440
441 cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
442 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
443 cpl_ensure_code(provenance, CPL_ERROR_NULL_INPUT);
444 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
445 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
446 cpl_ensure_code(pipe_id, CPL_ERROR_NULL_INPUT);
447 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
448
449 const cpl_frame * inherit = NULL;
450 cpl_propertylist * inherit_plist = NULL;
451 cpl_frameset * product_frameset = NULL;
452 cpl_propertylist * plist = NULL;
453
454 int conf = CPL_FALSE;
455 int bkgd = CPL_FALSE;
456 int bkge = CPL_FALSE;
457 int bkgc = CPL_FALSE;
458
459 cpl_propertylist * dicd = NULL;
460
461 /* find what optional extensions are present */
462
463 if (mefs) {
464 for (cpl_size i=0; i<mefs->size; i++) {
465 cpl_msg_info(cpl_func, "%d", (int)i);
466 cpl_msg_info(cpl_func, "%s", mefs->mef[i]->name);
467 if (!strcmp(mefs->mef[i]->name, "CONFIDENCE")) conf = CPL_TRUE;
468 if (!strcmp(mefs->mef[i]->name, "BKG_DATA")) bkgd = CPL_TRUE;
469 if (!strcmp(mefs->mef[i]->name, "BKG_ERR")) bkge = CPL_TRUE;
470 if (!strcmp(mefs->mef[i]->name, "BKG_CONF")) bkgc = CPL_TRUE;
471 }
472 }
473
474 /* the code generally follows the procedure of cpl_dfs_product_save */
475
476 /* ..report what is happening */
477
478 const char * procat = cpl_propertylist_get_string(applist,
479 CPL_DFS_PRO_CATG);
480 cpl_ensure_code(procat != NULL, cpl_error_get_code());
481 cpl_msg_info(cpl_func, "Writing MEF product(%s): %s", procat, filename);
482
483 /* ..construct a frameset for the product */
484
485 product_frameset = cpl_frameset_duplicate(provenance);
486
487 /* ..if inherit_frame is NULL, use the first frame in the frameset */
488
489 if (inherit_frame) {
490 inherit = inherit_frame;
491 } else {
492 cpl_frameset_iterator * frameset_iter = cpl_frameset_iterator_new(
493 allframes);
494 inherit = cpl_frameset_iterator_get_const(frameset_iter);
495 cpl_frameset_iterator_delete(frameset_iter);
496 }
497
498 /* ..create product frame and insert it in the product frameset */
499
500 cpl_frame * product_frame = cpl_frame_new();
501 cpl_frame_set_filename(product_frame, filename);
502 cpl_frame_set_tag(product_frame, procat);
503 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
504 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
505 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
506
507 cpl_frameset_insert(product_frameset, product_frame);
508
509 /* ..construct the propertylist for the primary header... */
510
511 plist = cpl_propertylist_duplicate(applist);
513 product_frame,
514 product_frameset,
515 parlist,
516 recipe,
517 pipe_id,
518 inherit,
519 provenance,
520 prov_raw);
521
522 /* ..save the propertylist to HDU 0 */
523 cpl_propertylist_update_string(plist, "ESO PRO REC1 ID", recipe);
524
525 cpl_propertylist_save(plist, filename, CPL_IO_CREATE);
526 enu_check_error_code("error saving header 0");
527
528 /* Now for the extensions
529 ..construct a list of the DICD keywords common to all extensions */
530
531 dicd = cpl_propertylist_new();
532 cpl_propertylist_append_string(dicd, "HDUCLASS", "ESO");
533 cpl_propertylist_append_string(dicd, "HDUDOC", "SDP");
534 cpl_propertylist_append_string(dicd, "HDUVERS", "SDP version 8");
535 cpl_propertylist_append_string(dicd, "SCIDATA", "DATA");
536 cpl_propertylist_append_string(dicd, "ERRDATA", "ERR");
537 cpl_propertylist_append_string(dicd, "QUALDATA", "DQ");
538 if (conf) {
539 cpl_propertylist_append_string(dicd, "CONFDATA", "CONFIDENCE");
540 }
541 if (bkgd) {
542 cpl_propertylist_append_string(dicd, "BKGDATA", "BKG_DATA");
543 }
544 if (bkge) {
545 cpl_propertylist_append_string(dicd, "BKGERR", "BKG_ERR");
546 }
547 if (bkgc) {
548 cpl_propertylist_append_string(dicd, "BKGCONF", "BKG_CONF");
549 }
550 cpl_size hlist_sz = 0;
551 if (image || imagelist) {
552
553 /* ..save image data to HDU 1, copy wcs header info only */
554
555 cpl_propertylist * header = cpl_propertylist_duplicate(dicd);
556 cpl_propertylist_update_string(header, "EXTNAME", "DATA");
557 cpl_propertylist_update_string(header, "HDUCLAS1", "IMAGE");
558 cpl_propertylist_update_string(header, "HDUCLAS2", "DATA");
559 cpl_propertylist_erase(header, "SCIDATA");
560
561 if (wcs_plist) cpl_propertylist_append(header, wcs_plist);
562
563 /* ..copy mandatory keywords from the applist to the extension
564 header */
565
566 const char * data_mandatory[] = {"BUNIT",
567 "CUNIT1",
568 "CUNIT2",
569 "FLUXCAL"};
570 const int count_data_mandatory = sizeof(data_mandatory) /
571 sizeof(char *);
572
573 const char * data_mandatory2[] = {"PHOTSYS",
574 "ABMAGLIM",
575 "ABMAGSAT",
576 "PSF_FWHM",
577 "PHOTZP",
578 "PHOTZPER",
579 "ZPMETHOD"};
580 const int count_data_mandatory2 = sizeof(data_mandatory2) /
581 sizeof(char *);
582
583
584 const char * ins_mode = "ins_mode";
585 if(cpl_propertylist_has(plist,"ESO INS1 MODE")) {
586 ins_mode = cpl_propertylist_get_string(plist, "ESO INS1 MODE");
587 }
588 //cpl_msg_info(cpl_func,"ins_mode: %s",ins_mode);
589
590 for (int mandi=0; mandi < count_data_mandatory; mandi++) {
591 if (!cpl_propertylist_has(header, data_mandatory[mandi])) {
592 if (cpl_propertylist_has(applist, data_mandatory[mandi])) {
593 cpl_propertylist_copy_property(header, applist,
594 data_mandatory[mandi]);
595 }
596 }
597 }
598
599 if(strcmp(ins_mode,"nixLSS") != 0) {
600 //cpl_msg_info(cpl_func,"add mandatory 2");
601 for (int mandi2=0; mandi2 < count_data_mandatory2; mandi2++) {
602 if (!cpl_propertylist_has(header, data_mandatory2[mandi2])) {
603 if (cpl_propertylist_has(applist, data_mandatory2[mandi2])) {
604 cpl_propertylist_copy_property(header, applist,
605 data_mandatory2[mandi2]);
606 }
607 }
608 }
609 }
610
611 /* ..fill mandatory keywords that are still missing */
612 for (int mandi=0; mandi < count_data_mandatory; mandi++) {
613 if (!cpl_propertylist_has(header, data_mandatory[mandi])) {
614 if (!strcmp(data_mandatory[mandi], "BUNIT")) {
615 cpl_propertylist_update_string(header, "BUNIT",
616 "unknown");
617 } else if (!strcmp(data_mandatory[mandi], "CUNIT1")) {
618 cpl_propertylist_update_string(header, "CUNIT1", "deg");
619 } else if (!strcmp(data_mandatory[mandi], "CUNIT2")) {
620 cpl_propertylist_update_string(header, "CUNIT2", "deg");
621 } else if (!strcmp(data_mandatory[mandi], "FLUXCAL")) {
622 cpl_propertylist_update_string(header, "FLUXCAL",
623 "UNCALIBRATED");
624 } else {
625 cpl_msg_info(cpl_func,
626 "missing extension keyword %s not handled",
627 data_mandatory[mandi]);
628 }
629 }
630 }
631 /* if not nixLSS..fill mandatory keywords that are still missing */
632 if(strcmp(ins_mode,"nixLSS") != 0) {
633 //cpl_msg_info(cpl_func,"add2 mandatory 2");
634 for (int mandi2=0; mandi2 < count_data_mandatory2; mandi2++) {
635 if (!cpl_propertylist_has(header, data_mandatory2[mandi2])) {
636
637 if (!strcmp(data_mandatory2[mandi2], "PHOTSYS")) {
638 cpl_propertylist_update_string(header, "PHOTSYS", "VEGA");
639 } else if (!strcmp(data_mandatory2[mandi2], "ABMAGLIM")) {
640 cpl_msg_info(cpl_func, "dummy value for ABMAGLIM");
641 cpl_propertylist_update_double(header, "ABMAGLIM", 0.0);
642 } else if (!strcmp(data_mandatory2[mandi2], "ABMAGSAT")) {
643 cpl_msg_info(cpl_func, "dummy value for ABMAGSAT");
644 cpl_propertylist_update_double(header, "ABMAGSAT", 0.0);
645 } else if (!strcmp(data_mandatory2[mandi2], "PSF_FWHM")) {
646 cpl_msg_info(cpl_func, "dummy value for PSF_FWHM");
647 cpl_propertylist_update_double(header, "PSF_FWHM", -1.0);
648 } else if (!strcmp(data_mandatory2[mandi2], "PHOTZP")) {
649 cpl_msg_info(cpl_func, "PHOTZP not found, set to 0.0");
650 cpl_propertylist_update_double(header, "PHOTZP", 0.0);
651 cpl_propertylist_set_comment(header, "PHOTZP", "Photometric "
652 "zeropoint so that MAG=-2.5*"
653 "log(data)+PHOTZP+APCOR");
654 } else if (!strcmp(data_mandatory2[mandi2], "PHOTZPER")) {
655 cpl_msg_info(cpl_func, "PHOTZPER not found, set to 0.0");
656 cpl_propertylist_update_double(header, "PHOTZPER", 0.0);
657 cpl_propertylist_set_comment(header, "PHOTZPER", "Error on "
658 "PHOTZP");
659 } else if (!strcmp(data_mandatory2[mandi2], "ZPMETHOD")) {
660 cpl_msg_info(cpl_func, "ZPMETHOD not found");
661 cpl_propertylist_update_string(header, "ZPMETHOD",
662 "PHOTZP not calculated");
663 cpl_propertylist_set_comment(header, "ZPMETHOD",
664 "Method of ZP calculation");
665 } else {
666 cpl_msg_info(cpl_func,
667 "missing extension keyword %s not handled",
668 data_mandatory[mandi2]);
669 }
670 }
671 }
672 }
673
674 if (image) {
675 cpl_image_save(hdrl_image_get_image_const(image),
676 filename,
677 HDRL_TYPE_DATA,
678 header,
679 CPL_IO_EXTEND);
680 }
681 //exit(0);
682 if (imagelist) {
683
684 /* create a temporary cpl_imagelist of the image planes */
685
686 cpl_imagelist * tlist = cpl_imagelist_new();
687 hlist_sz = hdrl_imagelist_get_size(imagelist);
688 if(hlist_sz > 1) {
689 for (cpl_size i = 0; i < hlist_sz; i++) {
690 const hdrl_image * timage = hdrl_imagelist_get(imagelist, i);
691
692 //char* filename2 = cpl_sprintf("warp4_%s", filename);
693 //cpl_image_save(hdrl_image_get_image((hdrl_image *) timage),
694 // filename2, CPL_TYPE_UNSPECIFIED, NULL,
695 // CPL_IO_CREATE);
696 //cpl_free(filename2);
697
698 cpl_imagelist_set(tlist,
699 (cpl_image *) hdrl_image_get_image_const(timage), i);
700 }
701
702 cpl_imagelist_save(tlist,
703 filename,
704 CPL_TYPE_DOUBLE,
705 header,
706 CPL_IO_EXTEND);
707
708 cpl_imagelist_unwrap(tlist);
709 } else {
710 const hdrl_image * timage = hdrl_imagelist_get(imagelist, 0);
711 eris_ifu_plist_erase_wcs3D(header);
712 cpl_image_save(hdrl_image_get_image_const(timage),
713 filename,
714 HDRL_TYPE_DATA,
715 header,
716 CPL_IO_EXTEND);
717 }
718 }
719
720 cpl_propertylist_delete(header);
721 enu_check_error_code("failed to save header 1");
722
723 /* ..save image err to HDU 2 */
724
725 header = cpl_propertylist_duplicate(dicd);
726 cpl_propertylist_update_string(header, "EXTNAME", "ERR");
727 cpl_propertylist_update_string(header, "HDUCLAS1", "IMAGE");
728 cpl_propertylist_update_string(header, "HDUCLAS2", "ERROR");
729 cpl_propertylist_update_string(header, "HDUCLAS3", "RMSE");
730 cpl_propertylist_erase(header, "ERRDATA");
731 if (cpl_propertylist_has(applist, "BUNIT")) {
732 cpl_propertylist_copy_property(header, applist, "BUNIT");
733 }
734
735 if (wcs_plist) cpl_propertylist_append(header, wcs_plist);
736
737 if (image) {
738 cpl_image_save(hdrl_image_get_error_const(image), filename,
739 HDRL_TYPE_ERROR, header, CPL_IO_EXTEND);
740 }
741 if (imagelist) {
742 cpl_imagelist * tlist = cpl_imagelist_new();
743 hlist_sz = hdrl_imagelist_get_size(imagelist);
744 if(hlist_sz > 1) {
745 for (cpl_size i = 0; i < hlist_sz; i++) {
746 const hdrl_image * timage = hdrl_imagelist_get(imagelist, i);
747 cpl_imagelist_set(tlist,
748 (cpl_image *) hdrl_image_get_error_const(timage),
749 i);
750 }
751 cpl_imagelist_save(tlist,
752 filename,
753 CPL_TYPE_DOUBLE,
754 header,
755 CPL_IO_EXTEND);
756 cpl_imagelist_unwrap(tlist);
757 } else {
758 hdrl_image* timage = hdrl_imagelist_get(imagelist, 0);
759 eris_ifu_plist_erase_wcs3D(header);
760 cpl_image_save(hdrl_image_get_image_const(timage),
761 filename,
762 CPL_TYPE_DOUBLE,
763 header,
764 CPL_IO_EXTEND);
765 }
766 }
767
768 cpl_propertylist_delete(header);
769 enu_check_error_code("failed to save header 2");
770
771 /* ..save image mask to HDU 3 */
772
773 header = cpl_propertylist_duplicate(dicd);
774 cpl_propertylist_update_string(header, "EXTNAME", "DQ");
775 cpl_propertylist_update_string(header, "HDUCLAS1", "IMAGE");
776 cpl_propertylist_update_string(header, "HDUCLAS2", "QUALITY");
777 cpl_propertylist_update_string(header, "HDUCLAS3", "MASKZERO");
778 cpl_propertylist_erase(header, "QUALDATA");
779
780 if (wcs_plist) cpl_propertylist_append(header, wcs_plist);
781
782 /* get the mask 'non const' as in this case one is created if it
783 does not exist */
784
785 if (image) {
786 cpl_mask_save(hdrl_image_get_mask((hdrl_image *) image), filename,
787 header, CPL_IO_EXTEND);
788 }
789 if (imagelist) {
790 const hdrl_image * timage = hdrl_imagelist_get(imagelist, 0);
791 if(hlist_sz == 1) {
792 eris_ifu_plist_erase_wcs3D(header);
793 }
794 cpl_mask_save(hdrl_image_get_mask((hdrl_image *) timage), filename,
795 header, CPL_IO_EXTEND);
796 }
797
798 cpl_propertylist_delete(header);
799 enu_check_error_code("failed to save header 3");
800 }
801
802 /* save the optional extensions */
803
804 if (mefs) {
805 cpl_msg_info(cpl_func, "..there are %d mefs", (int) (mefs->size));
806
807 for (cpl_size i=0; i<mefs->size; i++) {
808 cpl_propertylist * header = NULL;
809 if (mefs->mef[i]->plist) {
810 header = cpl_propertylist_duplicate(mefs->mef[i]->plist);
811 } else {
812 header = cpl_propertylist_new();
813 }
814
815 /* keywords for ESO DICD conformance */
816
817 cpl_propertylist_copy_property_regexp(header, dicd, ".+",
818 CPL_FALSE);
819 cpl_propertylist_update_string(header, "EXTNAME",
820 mefs->mef[i]->name);
821
822 cpl_msg_info(cpl_func, "..saving mef %s", mefs->mef[i]->name);
823 if (!strcmp(mefs->mef[i]->name, "CONFIDENCE")) {
824 cpl_propertylist_update_string(header, "HDUCLAS1", "IMAGE");
825 cpl_propertylist_update_string(header, "HDUCLAS2", "CONF");
826 cpl_propertylist_erase(header, "CONFDATA");
827 } else if (!strcmp(mefs->mef[i]->name, "BKG_DATA")) {
828 cpl_propertylist_update_string(header, "HDUCLAS1", "IMAGE");
829 cpl_propertylist_update_string(header, "HDUCLAS2", "BKG_DATA");
830 cpl_propertylist_erase(header, "BKGDATA");
831 if (cpl_propertylist_has(applist, "BUNIT")) {
832 cpl_propertylist_copy_property(header, applist, "BUNIT");
833 }
834 } else if (!strcmp(mefs->mef[i]->name, "BKG_ERR")) {
835 cpl_propertylist_update_string(header, "HDUCLAS1", "IMAGE");
836 cpl_propertylist_update_string(header, "HDUCLAS2", "BKG_ERROR");
837 cpl_propertylist_update_string(header, "HDUCLAS3", "RMSE");
838 cpl_propertylist_erase(header, "BKGERR");
839 if (cpl_propertylist_has(applist, "BUNIT")) {
840 cpl_propertylist_copy_property(header, applist, "BUNIT");
841 }
842 } else if (!strcmp(mefs->mef[i]->name, "BKG_CONF")) {
843 cpl_propertylist_update_string(header, "HDUCLAS1", "IMAGE");
844 cpl_propertylist_update_string(header, "HDUCLAS2", "BKG_CONF");
845 cpl_propertylist_erase(header, "BKGCONF");
846 } else if (!strcmp(mefs->mef[i]->data_type,
847 MEF_EXTENSION_CONTAINING_IMAGE)) {
848 cpl_propertylist_update_string(header, "HDUCLAS1",
849 MEF_EXTENSION_CONTAINING_IMAGE);
850 } else if (!strcmp(mefs->mef[i]->data_type,
851 MEF_EXTENSION_CONTAINING_MASK)) {
852 cpl_propertylist_update_string(header, "HDUCLAS1",
853 MEF_EXTENSION_CONTAINING_MASK);
854 } else if (!strcmp(mefs->mef[i]->data_type,
855 MEF_EXTENSION_CONTAINING_TABLE)) {
856 cpl_propertylist_update_string(header, "HDUCLAS1",
857 MEF_EXTENSION_CONTAINING_TABLE);
858 }
859
860 /* copy wcs if present */
861
862 if (wcs_plist) cpl_propertylist_append(header, wcs_plist);
863
864 /* save the header */
865 if(hlist_sz == 1) {
866 eris_ifu_plist_erase_wcs3D(header);
867 }
868 enu_mef_extension_save(mefs->mef[i], filename, header,
869 CPL_IO_EXTEND);
870 cpl_propertylist_delete(header);
871 enu_check_error_code("failed to save MEF extension %d", (int) i);
872 }
873 }
874
875 /* save the new frame to the overall frameset so that DFS knows a new
876 product has been created */
877
878 cpl_frameset_insert(allframes, cpl_frame_duplicate(product_frame));
879
880 cleanup:
881
882 cpl_propertylist_delete(dicd);
883 cpl_propertylist_delete(inherit_plist);
884 cpl_propertylist_delete(plist);
885 cpl_frameset_delete(product_frameset);
886 eris_check_error_code("enu_dfs_save_himage");
887 return cpl_error_get_code();
888}
889
890
891/*----------------------------------------------------------------------------*/
907/*----------------------------------------------------------------------------*/
908
909cpl_error_code enu_dfs_save_limage(cpl_frameset * allframes,
910 const cpl_parameterlist * parlist,
911 const cpl_frameset * provenance,
912 const cpl_boolean prov_raw,
913 const located_image * limage,
914 const char * recipe,
915 const cpl_frame * inherit,
916 cpl_propertylist * applist,
917 const char * pipe_id,
918 const char * filename) {
919
920 if (cpl_error_get_code() != CPL_ERROR_NONE) return cpl_error_get_code();
921
922 cpl_msg_debug(cpl_func, "enu_dfs_save_limage entry");
923
924 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
925 cpl_ensure_code(limage, CPL_ERROR_NULL_INPUT);
926 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
927
928 cpl_propertylist * wcs_plist = NULL;
929
930 /* how many mefs? */
931
932 int nmefs = 0;
933 if (limage->confidence) nmefs++;
934 if (limage->bkg) nmefs = nmefs + 2;
935 if (limage->bkg_confidence) nmefs++;
936
937 mef_extension_list * mefs = NULL;
938 if (nmefs > 0) mefs = enu_mef_extension_list_new(nmefs);
939 cpl_msg_info(cpl_func, "nmefs %d", nmefs);
940
941 int imef = 0;
942 if (limage->confidence) {
943 cpl_msg_info(cpl_func, "CONF");
944 mefs->mef[imef] = enu_mef_new_image("CONFIDENCE",
945 limage->confidence, NULL);
946 imef++;
947 }
948 if (limage->bkg) {
949 cpl_msg_info(cpl_func, "BKG");
950 mefs->mef[imef] = enu_mef_new_image("BKG_DATA",
952 limage->bkg), NULL);
953 imef++;
954 cpl_msg_info(cpl_func, "BKG_ERR");
955 mefs->mef[imef] = enu_mef_new_image("BKG_ERR",
957 limage->bkg), NULL);
958 imef++;
959 }
960 if (limage->bkg_confidence) {
961 cpl_msg_info(cpl_func, "BKG_CONF");
962 mefs->mef[imef] = enu_mef_new_image("BKG_CONF",
963 limage->bkg_confidence, NULL);
964 imef++;
965 }
966
967 /* extract the wcs propertylist from the located image */
968
969 wcs_plist = cpl_propertylist_duplicate(limage->plist);
970 cpl_propertylist_erase_regexp(wcs_plist,
971 "CRPIX1|CRPIX2|CRPIX3|"
972 "CRVAL1|CRVAL2|CRVAL3|"
973 "CDELT1|CDELT2|CDELT3|"
974 "CD1_1|CD1_2|CD1_3|"
975 "CD2_1|CD2_2|CD2_3|"
976 "CD3_1|CD3_2|CD3_3|"
977 "CTYPE1|CTYPE2|CTYPE3|"
978 "CUNIT1|CUNIT2|CUNIT3",
979 CPL_TRUE);
980
981 /* copy other important keywords to applist unless an overriding value
982 is already there */
983
984 if (!cpl_propertylist_has(applist, "BUNIT")) {
985 cpl_propertylist_copy_property(applist, limage->plist, "BUNIT");
986 }
987 if (!cpl_propertylist_has(applist, "PSF_FWHM")) {
988 if(cpl_propertylist_has(limage->plist, "PSF_FWHM")) {
989 cpl_propertylist_copy_property(applist, limage->plist, "PSF_FWHM");
990 }
991 }
992 if (!cpl_propertylist_has(applist, "ABMAGLIM")) {
993 if (cpl_propertylist_has(limage->plist, "ABMAGLIM")) {
994 cpl_propertylist_copy_property(applist, limage->plist, "ABMAGLIM");
995 }
996 }
997
998 cpl_msg_info(cpl_func, ".. writing %s", filename);
999 enu_dfs_save_himage(allframes,
1000 parlist,
1001 provenance,
1002 prov_raw,
1003 limage->himage,
1004 limage->himagelist,
1005 mefs,
1006 recipe,
1007 inherit,
1008 applist,
1009 wcs_plist,
1010 pipe_id,
1011 filename);
1012
1014 cpl_propertylist_delete(wcs_plist);
1015
1016 eris_check_error_code("enu_dfs_save_limage");
1017 return cpl_error_get_code();
1018}
1019
1020
1021/*----------------------------------------------------------------------------*/
1036/*----------------------------------------------------------------------------*/
1037
1038cpl_error_code enu_dfs_setup_product_header(cpl_propertylist * header,
1039 const cpl_frame * product_frame,
1040 const cpl_frameset * framelist,
1041 const cpl_parameterlist * parlist,
1042 const char * recipe_id,
1043 const char * pipe_id,
1044 const cpl_frame * inherit,
1045 const cpl_frameset * provenance,
1046 cpl_boolean prov_raw) {
1047
1048 if (cpl_error_get_code() != CPL_ERROR_NONE) return cpl_error_get_code();
1049
1050 cpl_ensure_code(header, CPL_ERROR_NULL_INPUT);
1051 cpl_ensure_code(product_frame, CPL_ERROR_NULL_INPUT);
1052 cpl_ensure_code(framelist, CPL_ERROR_NULL_INPUT);
1053 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
1054 cpl_ensure_code(recipe_id, CPL_ERROR_NULL_INPUT);
1055 cpl_ensure_code(pipe_id, CPL_ERROR_NULL_INPUT);
1056 cpl_ensure_code(inherit, CPL_ERROR_NULL_INPUT);
1057 cpl_ensure_code(provenance, CPL_ERROR_NULL_INPUT);
1058
1059 cpl_propertylist * inherit_plist = NULL;
1060 cpl_propertylist * input_header = NULL;
1061
1062 /* ..add old-style DFS keywords to header but do not overwrite RA,DEC
1063 keywords if already present */
1064
1065 input_header = cpl_propertylist_duplicate(header);
1066 cpl_dfs_setup_product_header(header,
1067 product_frame,
1068 framelist,
1069 parlist,
1070 recipe_id,
1071 pipe_id,
1072 CPL_DFS_PRO_DID,
1073 inherit);
1074 cpl_propertylist_copy_property_regexp(header,
1075 input_header,
1076 "RA|DEC",
1077 CPL_FALSE);
1078 if (cpl_propertylist_has(header, "RA")) {
1079 cpl_propertylist_set_comment(header, "RA",
1080 "[deg] RA (J2000) pointing");
1081 }
1082 if (cpl_propertylist_has(header, "DEC")) {
1083 cpl_propertylist_set_comment(header, "DEC",
1084 "[deg] DEC (J2000) pointing");
1085 }
1086
1087 /* ..remove any obsolete DICD, data-specific, WCS or keywords that
1088 should just not be in the primary header */
1089
1090 cpl_propertylist_erase_regexp(header,
1091 "HDUCLASS|HDUDOC|HDUVERS|SCIDATA|ERRDATA|"
1092 "QUALDATA|CONFDATA|BKGDATA|BKGERR|BKGCONF|"
1093 "CRPIX1|CRPIX2|CRVAL1|CRVAL2|CDELT1|CDELT2|"
1094 "CTYPE1|CTYPE2|CUNIT1|CUNIT2|"
1095 "CD1_1|CD1_2|CD2_1|CD2_2|"
1096 "PHOTSYS|PHOTZP|PHOTZERR|ABMAGSAT|ABMAGLIM|"
1097 "MJD-OBS|PSF_FWHM|BUNIT",
1098 CPL_FALSE);
1099
1100 /* ..add the PROVIi keywords from the provenance frames */
1101
1102 int iprov = 0;
1103 cpl_frameset_iterator * it = cpl_frameset_iterator_new(provenance);
1104 const cpl_frame * frame = cpl_frameset_iterator_get_const(it);
1105 while (frame) {
1106 iprov++;
1107 /* get the name of the raw data file */
1108 char * filename = cpl_strdup(cpl_frame_get_filename(frame));
1109 char * pch = NULL;
1110 if (prov_raw) {
1111 /* want name of raw data file */
1112 pch = strstr(filename, "ERIS.20");
1113 if (!pch) {
1114 pch = filename;
1115 }
1116 } else {
1117 pch = filename;
1118 }
1119 char * pname = cpl_sprintf("PROV%.0i", iprov);
1120 cpl_propertylist_update_string(header, pname, basename(pch));
1121 cpl_free(filename);
1122 cpl_free(pname);
1123 cpl_frameset_iterator_advance(it, 1);
1124 frame = cpl_frameset_iterator_get(it);
1125 }
1126 cpl_frameset_iterator_delete(it); it = NULL;
1127 enu_check_error_code("error storing PROVenance keywords");
1128
1129 /* ..copy any missing mandatory keywords from the propertylist
1130 associated with inherit */
1131
1132 const char * inherit_fname = cpl_frame_get_filename(inherit);
1133 inherit_plist = cpl_propertylist_load(inherit_fname, 0);
1134 enu_check_error_code("error reading propertylist from inherit");
1135
1136 const char * mandatory[] = {"TELESCOP",
1137 "INSTRUME",
1138 "FILTER",
1139 "OBJECT",
1140 "RA",
1141 "DEC",
1142 "EQUINOX",
1143 "RADESYS",
1144 "TIMESYS",
1145 "EXPTIME",
1146 "TEXPTIME",
1147 "MJD-OBS",
1148 "MJD-END",
1149 "PROG_ID",
1150 "NCOMBINE",
1151 "FLUXCAL",
1152 "PROCSOFT",
1153 "REFERENC",
1154 "OBSTECH",
1155 "OBID1"};
1156
1157 const int count_mandatory = sizeof(mandatory) / sizeof(char *);
1158 for (int mandi=0; mandi < count_mandatory; mandi++) {
1159 if (!cpl_propertylist_has(header, mandatory[mandi])) {
1160 if (cpl_propertylist_has(inherit_plist, mandatory[mandi])) {
1161 cpl_msg_info(cpl_func, "copying from inherit %s",
1162 mandatory[mandi]);
1163 cpl_propertylist_copy_property(header, inherit_plist,
1164 mandatory[mandi]);
1165 }
1166 }
1167 }
1168
1169 /* ..try to fill mandatory keywords that are still missing */
1170
1171 for (int mandi=0; mandi < count_mandatory; mandi++) {
1172 if (!cpl_propertylist_has(header, mandatory[mandi])) {
1173 if (!strcmp(mandatory[mandi], "FILTER")) {
1174 const char * filter = NULL;
1175 if (inherit_plist) {
1176 filter = enu_get_filter(inherit_plist);
1177 }
1178 if (filter) {
1179 cpl_propertylist_update_string(header, "FILTER",
1180 filter);
1181 } else {
1182 cpl_propertylist_update_string(header, "FILTER",
1183 "unknown");
1184 }
1185 } else if (!strcmp(mandatory[mandi], "RADESYS")) {
1186 const char * radesys = NULL;
1187 if (inherit_plist && cpl_propertylist_has(inherit_plist,
1188 "RADECSYS")) {
1189 radesys = cpl_propertylist_get_string(inherit_plist,
1190 "RADECSYS");
1191 }
1192 if (radesys) {
1193 cpl_propertylist_update_string(header, "RADESYS",
1194 radesys);
1195 } else {
1196 cpl_propertylist_update_string(header, "RADESYS",
1197 "unknown");
1198 }
1199 } else if (!strcmp(mandatory[mandi], "TIMESYS")) {
1200 cpl_propertylist_update_string(header, "TIMESYS",
1201 "UTC");
1202 } else if (!strcmp(mandatory[mandi], "PROG_ID")) {
1203 const char * prog_id = NULL;
1204 if (inherit_plist && cpl_propertylist_has(inherit_plist,
1205 "ESO OBS PROG ID")) {
1206 prog_id = cpl_propertylist_get_string(inherit_plist,
1207 "ESO OBS PROG ID");
1208 }
1209 if (prog_id) {
1210 cpl_propertylist_update_string(header, "PROG_ID",
1211 prog_id);
1212 } else {
1213 cpl_propertylist_update_string(header, "PROG_ID",
1214 "unknown");
1215 }
1216 } else if (!strcmp(mandatory[mandi], "OBSTECH")) {
1217 const char * obstech = NULL;
1218 if (inherit_plist) {
1219 if (cpl_propertylist_has(inherit_plist, "ESO DPR TECH")) {
1220 obstech = cpl_propertylist_get_string(
1221 inherit_plist, "ESO DPR TECH");
1222 } else if (cpl_propertylist_has(inherit_plist, "ESO PRO TECH")) {
1223 obstech = cpl_propertylist_get_string(
1224 inherit_plist, "ESO PRO TECH");
1225 }
1226 }
1227 if (obstech) {
1228 cpl_propertylist_update_string(header, "OBSTECH",
1229 obstech);
1230 } else {
1231 cpl_propertylist_update_string(header, "OBSTECH",
1232 "unknown");
1233 }
1234 } else if (!strcmp(mandatory[mandi], "FLUXCAL")) {
1235 cpl_propertylist_update_string(header, "FLUXCAL",
1236 "UNCALIBRATED");
1237 } else if (!strcmp(mandatory[mandi], "PROCSOFT")) {
1238 cpl_propertylist_update_string(header, "PROCSOFT", pipe_id);
1239 } else if (!strcmp(mandatory[mandi], "REFERENC")) {
1240 cpl_propertylist_update_string(header, "REFERENC",
1241 " ");
1242 } else if (!strcmp(mandatory[mandi], "OBID1")) {
1243 int obid = -1;
1244 if (inherit_plist && cpl_propertylist_has(inherit_plist,
1245 "ESO OBS ID")) {
1246 obid = cpl_propertylist_get_int(inherit_plist, "ESO OBS ID");
1247 }
1248 cpl_propertylist_update_int(header, "OBID1", obid);
1249 } else {
1250 cpl_msg_warning(cpl_func, "missing %s", mandatory[mandi]);
1251 }
1252 }
1253 }
1254
1255 cleanup:
1256 cpl_propertylist_delete(inherit_plist);
1257 cpl_propertylist_delete(input_header);
1258 eris_check_error_code("enu_dfs_setup_product_header");
1259 return cpl_error_get_code();
1260}
1261
1262
1263
cpl_error_code enu_dfs_save_limage(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *provenance, const cpl_boolean prov_raw, const located_image *limage, const char *recipe, const cpl_frame *inherit, cpl_propertylist *applist, const char *pipe_id, const char *filename)
Save a located image structure to a MEF.
Definition: eris_nix_dfs.c:909
cpl_error_code eris_nix_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: eris_nix_dfs.c:58
cpl_error_code enu_dfs_save_bpm(const char *procat, cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const master_bpm *mbad_pix_map, const char *recipe, const cpl_propertylist *applist, const char *pipe_id, const char *filename)
Save a master_bpm as a DFS-compliant pipeline product.
Definition: eris_nix_dfs.c:218
cpl_error_code enu_dfs_setup_product_header(cpl_propertylist *header, const cpl_frame *product_frame, const cpl_frameset *framelist, const cpl_parameterlist *parlist, const char *recipe_id, const char *pipe_id, const cpl_frame *inherit, const cpl_frameset *provenance, cpl_boolean prov_raw)
Add phase3/IDP keywords to a pipeline product propertyist list.
cpl_error_code enu_dfs_save_catalogue(cpl_frameset *frameset, cpl_frame *image, const cpl_parameterlist *parlist, const cpl_table *catalogue, const cpl_propertylist *tablelist, const char *cat_name, const char *recipe, const char *pipe_id, const char *preface, const char *procat)
Save a catalogue as DFS product.
Definition: eris_nix_dfs.c:274
cpl_error_code enu_dfs_save_himage(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *provenance, const cpl_boolean prov_raw, const hdrl_image *image, const hdrl_imagelist *imagelist, const mef_extension_list *mefs, const char *recipe, const cpl_frame *inherit_frame, const cpl_propertylist *applist, const cpl_propertylist *wcs_plist, const char *pipe_id, const char *filename)
Save an hdrl_image/imagelist as a DFS-compliant MEF pipeline product.
Definition: eris_nix_dfs.c:423
mef_extension * enu_mef_new_image(const char *name, const cpl_image *data, const cpl_propertylist *plist)
Create a mef_extension to hold a cpl_image.
mef_extension_list * enu_mef_extension_list_new(cpl_size size)
Construct a new mef_extension_list.
cpl_error_code enu_mef_extension_save(const mef_extension *mef, const char *filename, const cpl_propertylist *plist, unsigned mode)
Save a mef_extension struct to FITS file.
char * enu_repreface(const char *filename, const char *preface)
Preface a raw filename with a string.
void enu_mef_extension_list_delete(mef_extension_list *list)
Delete a mef_extension_list and its contents.
const char * enu_get_filter(const cpl_propertylist *plist)
Get the filter used in an observation.
cpl_error_code eris_check_error_code(const char *func_id)
handle CPL errors
Definition: eris_utils.c:56
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
Definition: hdrl_image.c:157
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:131
const cpl_image * hdrl_image_get_error_const(const hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:144
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:105
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:118
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.