FORS Pipeline Reference Manual  5.0.2
fors_dfs.c
1 /* $Id: fors_dfs.c,v 1.47 2013-10-09 15:58:42 cgarcia Exp $
2  *
3  * This file is part of the FORS library
4  * Copyright (C) 2002-2010 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: cgarcia $
23  * $Date: 2013-10-09 15:58:42 $
24  * $Revision: 1.47 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 #include <fors_dfs.h>
33 
34 #include <fors_utils.h>
35 #include <fors_image.h>
36 #include <fors_pfits.h>
37 
38 #include <cpl.h>
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <ctype.h>
43 #include <stdbool.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 /*----------------------------------------------------------------------------*/
54 /*----------------------------------------------------------------------------*/
55 
58 #define WCS_KEYS "^((CRVAL|CRPIX|CTYPE|CDELT)[0-9]|RADECSYS|CD[0-9]_[0-9])$"
59 
60 /*------------------------------------------------------------------------------
61  Prototypes
62  -----------------------------------------------------------------------------*/
63 /*------------------------------------------------------------------------------
64  Implementation
65  -----------------------------------------------------------------------------*/
66 
67 /*----------------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------------*/
69 static char *strlower(char *s)
70 {
71 
72  char *t = s;
73 
74  while (*t) {
75  *t = tolower(*t);
76  t++;
77  }
78 
79  return s;
80 
81 }
82 
83 /*----------------------------------------------------------------------------*/
84 /*----------------------------------------------------------------------------*/
85 char *dfs_generate_filename(const char *category)
86 {
87  char *filename = cpl_calloc(strlen(category) + 6, sizeof(char));
88 
89  strlower(strcpy(filename, category));
90  strcat(filename, ".fits");
91 
92  return filename;
93 }
94 
95 /*----------------------------------------------------------------------------*/
106 /*----------------------------------------------------------------------------*/
107 static void
108 errorstate_dump_one(unsigned self, unsigned first, unsigned last)
109 {
110 
111  const cpl_boolean is_reverse = first > last ? CPL_TRUE : CPL_FALSE;
112  const unsigned newest = is_reverse ? first : last;
113  const unsigned oldest = is_reverse ? last : first;
114  const char * revmsg = is_reverse ? " in reverse order" : "";
115 
116  /* Cannot use internal CPL functions
117  cx_assert( oldest <= self );
118  cx_assert( newest >= self ); */
119 
120  if (newest == 0) {
121  cpl_msg_info(cpl_func, "No error(s) to dump");
122  /* cx_assert( oldest == 0); */
123  } else {
124  /* cx_assert( oldest > 0);
125  cx_assert( newest >= oldest); */
126  if (self == first) {
127  if (oldest == 1) {
128  cpl_msg_debug(cpl_func, "Dumping all %u error(s)%s:", newest,
129  revmsg);
130  } else {
131  cpl_msg_error(cpl_func, "Dumping the %u most recent error(s) "
132  "out of a total of %u errors%s:",
133  newest - oldest + 1, newest, revmsg);
134  }
135  }
136 
137  const char *message_from_cpl = cpl_error_get_message();
138 
139  if (message_from_cpl == NULL) {
140  /* This should never happen */
141  cpl_msg_error(cpl_func, "Unspecified error");
142  }
143 
144  /* Skip the standard (non-informative) CPL message string,
145  which usually terminates with ': '
146 
147  If no user-defined error message is given,
148  print the CPL standard message
149  */
150  while (*message_from_cpl != '\0' && *message_from_cpl != ':') {
151  message_from_cpl += 1;
152  }
153 
154  if (*message_from_cpl != '\0') {
155  message_from_cpl += 1;
156 
157  if (*message_from_cpl == ' ') message_from_cpl++;
158 
159  if (*message_from_cpl != '\0') {
160  /* Still something left of the string */
161  cpl_msg_error(cpl_func, "%s [%s]", message_from_cpl,
162  cpl_error_get_where());
163  }
164  else {
165  cpl_msg_error(cpl_func, "%s [%s]",
166  cpl_error_get_message(), cpl_error_get_where());
167  }
168  }
169  else {
170  /* Found no ':' is CPL message string */
171  cpl_msg_error(cpl_func, "%s [%s]",
172  cpl_error_get_message(), cpl_error_get_where());
173  }
174  }
175 
176  return;
177 }
178 
179 
180 /*----------------------------------------------------------------------------*/
190 /*----------------------------------------------------------------------------*/
191 void fors_begin(cpl_frameset *frames,
192  const char *description_short)
193 {
194  cpl_msg_info(cpl_func, "%s", PACKAGE_STRING);
195  cpl_msg_info(cpl_func, "%s", description_short);
196 
197  fors_dfs_set_groups(frames);
198  cpl_msg_info(cpl_func, "Input frame%s:",
199  cpl_frameset_get_size(frames) != 1 ? "s" : "");
200  fors_frameset_print(frames);
201 
202  return;
203 }
204 
205 /*----------------------------------------------------------------------------*/
218 /*----------------------------------------------------------------------------*/
219 int fors_end(const cpl_frameset *frames, cpl_errorstate before_exec)
220 {
221  if (cpl_error_get_code() == CPL_ERROR_NONE) {
222 
223  const cpl_frame *f;
224 
225  cpl_msg_info(cpl_func, "Product frame%s:",
226  cpl_frameset_get_size(frames) != 1 ? "s" : "");
227 
228  for (f = cpl_frameset_get_first_const(frames);
229  f != NULL;
230  f = cpl_frameset_get_next_const(frames)) {
231  if (cpl_frame_get_group(f) == CPL_FRAME_GROUP_PRODUCT) {
232  fors_frame_print(f);
233  }
234  }
235 
236  /* Shut up EsoRex */
237  //cpl_msg_set_level(CPL_MSG_WARNING);
238  return 0;
239  }
240  else {
241 
242  cpl_errorstate_dump(before_exec, CPL_FALSE, errorstate_dump_one);
243 
244  return 1;
245  }
246 }
247 
248 #undef cleanup
249 #define cleanup
250 /*----------------------------------------------------------------------------*/
255 /*----------------------------------------------------------------------------*/
256 void
257 fors_dfs_set_groups(cpl_frameset * set)
258 {
259  cpl_frame *f;
260 
261  assure( set != NULL, return, NULL );
262 
263  for (f = cpl_frameset_get_first(set);
264  f != NULL;
265  f = cpl_frameset_get_next(set)) {
266 
267  const char *tag = cpl_frame_get_tag(f);
268 
269  if (tag != NULL) {
270  if (strcmp(tag, BIAS ) == 0 ||
271  strcmp(tag, DARK ) == 0 ||
272  strcmp(tag, SCREEN_FLAT_IMG ) == 0 ||
273  strcmp(tag, SKY_FLAT_IMG ) == 0 ||
274  strcmp(tag, STANDARD_IMG ) == 0 ||
275  strcmp(tag, "LAMP_PMOS" ) == 0 ||
276  strcmp(tag, "LAMP_MXU" ) == 0 ||
277  strcmp(tag, "LAMP_MOS" ) == 0 ||
278  strcmp(tag, "LAMP_LSS" ) == 0 ||
279  strcmp(tag, "SCREEN_FLAT_PMOS") == 0 ||
280  strcmp(tag, "STANDARD_PMOS" ) == 0 ||
281  strcmp(tag, "SCIENCE_PMOS" ) == 0 ||
282  strcmp(tag, "SCIENCE_MOS" ) == 0 ||
283  strcmp(tag, "SCIENCE_MXU" ) == 0 ||
284  strcmp(tag, "SCIENCE_LSS" ) == 0 ||
285  strcmp(tag, "STANDARD_MOS" ) == 0 ||
286  strcmp(tag, "STANDARD_MXU" ) == 0 ||
287  strcmp(tag, "STANDARD_LSS" ) == 0 ||
288  strcmp(tag, SCIENCE_IMG ) == 0 ||
289  strcmp(tag, "SCREEN_FLAT_MXU" ) == 0 ||
290  strcmp(tag, "SCREEN_FLAT_MOS" ) == 0 ||
291  strcmp(tag, "SCREEN_FLAT_LSS" ) == 0 ) {
292  cpl_frame_set_group(f, CPL_FRAME_GROUP_RAW);
293  }
294  else if (strcmp(tag, MASTER_BIAS ) == 0 ||
295  strcmp(tag, MASTER_DARK ) == 0 ||
296  strcmp(tag, MASTER_SCREEN_FLAT_IMG ) == 0 ||
297  strcmp(tag, MASTER_SKY_FLAT_IMG ) == 0 ||
298  strcmp(tag, ALIGNED_PHOT ) == 0 ||
299  strcmp(tag, "MASTER_NORM_FLAT_PMOS" ) == 0 ||
300  strcmp(tag, "DISP_COEFF_PMOS" ) == 0 ||
301  strcmp(tag, "CURV_COEFF_PMOS" ) == 0 ||
302  strcmp(tag, "SLIT_LOCATION_PMOS" ) == 0 ||
303  strcmp(tag, "MASTER_NORM_FLAT_MOS" ) == 0 ||
304  strcmp(tag, "MASTER_NORM_FLAT_MXU" ) == 0 ||
305  strcmp(tag, "MASTER_NORM_FLAT_LSS" ) == 0 ||
306  strcmp(tag, "MASTER_NORM_FLAT_LONG_MOS" ) == 0 ||
307  strcmp(tag, "SLIT_LOCATION_MOS" ) == 0 ||
308  strcmp(tag, "SLIT_LOCATION_MXU" ) == 0 ||
309  strcmp(tag, "SLIT_LOCATION_LSS" ) == 0 ||
310  strcmp(tag, "SLIT_LOCATION_LONG_MOS" ) == 0 ||
311  strcmp(tag, "CURV_COEFF_MOS" ) == 0 ||
312  strcmp(tag, "CURV_COEFF_MXU" ) == 0 ||
313  strcmp(tag, "CURV_COEFF_LSS" ) == 0 ||
314  strcmp(tag, "DISP_COEFF_MOS" ) == 0 ||
315  strcmp(tag, "DISP_COEFF_MXU" ) == 0 ||
316  strcmp(tag, "DISP_COEFF_LSS" ) == 0 ||
317  strcmp(tag, "DISP_COEFF_LONG_MOS" ) == 0 ||
318  strcmp(tag, "FLAT_SED_MOS" ) == 0 ||
319  strcmp(tag, "FLAT_SED_MXU" ) == 0 ||
320  strcmp(tag, "FLAT_SED_LSS" ) == 0 ||
321  strcmp(tag, "FLAT_SED_LONG_MOS" ) == 0 ||
322  /* static calibration */
323  strcmp(tag, FLX_STD_IMG ) == 0 ||
324  strcmp(tag, "EXTINCT_TABLE" ) == 0 ||
325  strcmp(tag, "MASTER_LINECAT" ) == 0 ||
326  strcmp(tag, "MASTER_DISTORTION_TABLE" ) == 0 ||
327  strcmp(tag, "GLOBAL_DISTORTION_TABLE" ) == 0 ||
328  strcmp(tag, "RETARDER_WAVEPLATE_CHROMATISM") == 0 ||
329  strcmp(tag, "GRISM_TABLE" ) == 0 ||
330  strcmp(tag, "STD_PMOS_TABLE" ) == 0 ||
331  strcmp(tag, "TELLURIC_CONTAMINATION" ) == 0 ||
332  strcmp(tag, "STD_FLUX_TABLE" ) == 0 ||
333  strcmp(tag, "SPECPHOT_TABLE" ) == 0 ||
334  strcmp(tag, PHOT_TABLE ) == 0) {
335  cpl_frame_set_group(f, CPL_FRAME_GROUP_CALIB);
336  }
337  else {
338  cpl_msg_warning(cpl_func, "Unrecognized frame tag: '%s'",
339  tag);
340  }
341  }
342  }
343 
344  return;
345 }
346 
347 /*----------------------------------------------------------------------------*/
348 #undef cleanup
349 #define cleanup
350 
358 /*----------------------------------------------------------------------------*/
359 const char *
360 fors_dfs_pipeline_version(const cpl_propertylist *header,
361  const char **instrument_version)
362 {
363  const char *instrume = NULL;
364 
365  instrume = cpl_propertylist_get_string(header,
366  FORS_PFITS_INSTRUME);
367  assure( !cpl_error_get_code(), return NULL,
368  "Missing keyword %s in input header", FORS_PFITS_INSTRUME);
369 
370  assure( strlen(instrume) >= 5, return NULL,
371  "%s keyword must be 'fors1' or 'fors2', not '%s'",
372  FORS_PFITS_INSTRUME, instrume);
373 
374  assure( instrume[4] == '1' || instrume[4] == '2', return NULL,
375  "Unrecognized %s: %s", FORS_PFITS_INSTRUME, instrume);
376 
377  if (instrument_version != NULL) {
378  *instrument_version = cpl_sprintf("%s", instrume);
379  }
380 
381  return cpl_sprintf("fors%c/%s", instrume[4], PACKAGE_VERSION);
382 }
383 
384 /*----------------------------------------------------------------------------*/
406 /*----------------------------------------------------------------------------*/
407 int dfs_get_parameter_int(cpl_parameterlist *parlist, const char *name,
408  const cpl_table *defaults)
409 {
410  const char *func = "dfs_get_parameter_int";
411 
412  const char *alias;
413  cpl_parameter *param;
414 
415 
416  if (parlist == NULL) {
417  cpl_msg_error(func, "Missing input parameter list");
418  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
419  return 0;
420  }
421 
422  if (name == NULL) {
423  cpl_msg_error(func, "Missing input parameter name");
424  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
425  return 0;
426  }
427 
428  param = cpl_parameterlist_find(parlist, name);
429 
430  if (param == NULL) {
431  cpl_msg_error(func, "Wrong parameter name: %s", name);
432  cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
433  return 0;
434  }
435 
436  if (cpl_parameter_get_type(param) != CPL_TYPE_INT) {
437  cpl_msg_error(func, "Unexpected type for parameter "
438  "\"%s\": it should be integer", name);
439  cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
440  return 0;
441  }
442 
443  alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
444 
445 // if (defaults && cpl_parameter_get_default_flag(param) == 0) {
446  if (defaults &&
447  cpl_parameter_get_default_int(param) == cpl_parameter_get_int(param)) {
448 
449  if (cpl_table_has_column(defaults, alias)) {
450  if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_INT) {
451  cpl_msg_error(func, "Unexpected type for GRISM_TABLE "
452  "column \"%s\": it should be integer", alias);
453  cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
454  return 0;
455  }
456  if (cpl_table_is_valid(defaults, alias, 0)) {
457  cpl_parameter_set_int(param, cpl_table_get_int(defaults,
458  alias, 0, NULL));
459  }
460  else {
461  cpl_msg_error(func, "Invalid parameter value in table "
462  "column \"%s\"", alias);
463  cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
464  return 0;
465  }
466  }
467  else {
468  cpl_msg_warning(func, "Parameter \"%s\" not found in GRISM_TABLE "
469  "- using recipe default", alias);
470  }
471  }
472 
473  cpl_msg_info(func, "%s:", alias);
474  cpl_msg_info(func, "%s: %d",
475  cpl_parameter_get_help(param), cpl_parameter_get_int(param));
476 
477  return cpl_parameter_get_int(param);
478 
479 }
480 
481 /*----------------------------------------------------------------------------*/
503 /*----------------------------------------------------------------------------*/
504 double dfs_get_parameter_double(cpl_parameterlist *parlist,
505  const char *name, const cpl_table *defaults)
506 {
507  const char *func = "dfs_get_parameter_double";
508 
509  const char *alias;
510  cpl_parameter *param;
511 
512 
513  if (parlist == NULL) {
514  cpl_msg_error(func, "Missing input parameter list");
515  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
516  return 0;
517  }
518 
519  if (name == NULL) {
520  cpl_msg_error(func, "Missing input parameter name");
521  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
522  return 0;
523  }
524 
525  param = cpl_parameterlist_find(parlist, name);
526 
527  if (param == NULL) {
528  cpl_msg_error(func, "Wrong parameter name: %s", name);
529  cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
530  return 0;
531  }
532 
533  if (cpl_parameter_get_type(param) != CPL_TYPE_DOUBLE) {
534  cpl_msg_error(func, "Unexpected type for parameter "
535  "\"%s\": it should be double", name);
536  cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
537  return 0;
538  }
539 
540  alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
541 
542 // if (defaults && cpl_parameter_get_default_flag(param) == 0) {
543  if (defaults &&
544  cpl_parameter_get_default_double(param) ==
545  cpl_parameter_get_double(param)) {
546 
547  if (cpl_table_has_column(defaults, alias)) {
548  if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_DOUBLE) {
549  cpl_msg_error(func, "Unexpected type for GRISM_TABL "
550  "column \"%s\": it should be double", alias);
551  cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
552  return 0;
553  }
554  if (cpl_table_is_valid(defaults, alias, 0)) {
555  cpl_parameter_set_double(param, cpl_table_get_double(defaults,
556  alias, 0, NULL));
557  }
558  else {
559  cpl_msg_error(func, "Invalid parameter value in table "
560  "column \"%s\"", alias);
561  cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
562  return 0;
563  }
564  }
565  else {
566  cpl_msg_warning(func, "Parameter \"%s\" not found in GRISM_TABLE "
567  "- using recipe default", alias);
568  }
569  }
570 
571  cpl_msg_info(func, "%s:", alias);
572  cpl_msg_info(func, "%s: %f",
573  cpl_parameter_get_help(param), cpl_parameter_get_double(param));
574 
575  return cpl_parameter_get_double(param);
576 
577 }
578 
579 /*----------------------------------------------------------------------------*/
601 /*----------------------------------------------------------------------------*/
602 const char *dfs_get_parameter_string(cpl_parameterlist *parlist,
603  const char *name,
604  const cpl_table *defaults)
605 {
606  const char *func = "dfs_get_parameter_string";
607 
608  const char *alias;
609  cpl_parameter *param;
610 
611 
612  if (parlist == NULL) {
613  cpl_msg_error(func, "Missing input parameter list");
614  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
615  return 0;
616  }
617 
618  if (name == NULL) {
619  cpl_msg_error(func, "Missing input parameter name");
620  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
621  return 0;
622  }
623 
624  param = cpl_parameterlist_find(parlist, name);
625 
626  if (param == NULL) {
627  cpl_msg_error(func, "Wrong parameter name: %s", name);
628  cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
629  return 0;
630  }
631 
632  if (cpl_parameter_get_type(param) != CPL_TYPE_STRING) {
633  cpl_msg_error(func, "Unexpected type for parameter "
634  "\"%s\": it should be string", name);
635  cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
636  return 0;
637  }
638 
639  alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
640 
641 // if (defaults && cpl_parameter_get_default_flag(param) == 0) {
642  if (defaults &&
643  strcmp(cpl_parameter_get_default_string(param),
644  cpl_parameter_get_string(param)) == 0) {
645 
646  if (cpl_table_has_column(defaults, alias)) {
647  if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_STRING) {
648  cpl_msg_error(func, "Unexpected type for GRISM_TABLE "
649  "column \"%s\": it should be string", alias);
650  cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
651  return 0;
652  }
653  if (cpl_table_is_valid(defaults, alias, 0)) {
654  cpl_parameter_set_string(param, cpl_table_get_string(defaults,
655  alias, 0));
656  }
657  else {
658  cpl_msg_error(func, "Invalid parameter value in table "
659  "column \"%s\"", alias);
660  cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
661  return 0;
662  }
663  }
664  else {
665  cpl_msg_warning(func, "Parameter \"%s\" not found in GRISM_TABLE "
666  "- using recipe default", alias);
667  }
668  }
669 
670  cpl_msg_info(func, "%s:", alias);
671  cpl_msg_info(func, "%s: %s", cpl_parameter_get_help(param),
672  cpl_parameter_get_string(param));
673 
674  return cpl_parameter_get_string(param);
675 
676 }
677 
678 /*----------------------------------------------------------------------------*/
700 /*----------------------------------------------------------------------------*/
701 int dfs_get_parameter_bool(cpl_parameterlist *parlist, const char *name,
702  const cpl_table *defaults)
703 {
704  const char *func = "dfs_get_parameter_bool";
705 
706  const char *alias;
707  cpl_parameter *param;
708  int value;
709 
710 
711  if (parlist == NULL) {
712  cpl_msg_error(func, "Missing input parameter list");
713  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
714  return 0;
715  }
716 
717  if (name == NULL) {
718  cpl_msg_error(func, "Missing input parameter name");
719  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
720  return 0;
721  }
722 
723  param = cpl_parameterlist_find(parlist, name);
724 
725  if (param == NULL) {
726  cpl_msg_error(func, "Wrong parameter name: %s", name);
727  cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
728  return 0;
729  }
730 
731  if (cpl_parameter_get_type(param) != CPL_TYPE_BOOL) {
732  cpl_msg_error(func, "Unexpected type for parameter "
733  "\"%s\": it should be boolean", name);
734  cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
735  return 0;
736  }
737 
738  alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
739 
740 // if (defaults && cpl_parameter_get_default_flag(param) == 0) {
741  if (defaults &&
742  cpl_parameter_get_default_bool(param) ==
743  cpl_parameter_get_bool(param)) {
744 
745  if (cpl_table_has_column(defaults, alias)) {
746  if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_INT) {
747  cpl_msg_error(func, "Unexpected type for GRISM_TABLE "
748  "column \"%s\": it should be integer", alias);
749  cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
750  return 0;
751  }
752  if (cpl_table_is_valid(defaults, alias, 0)) {
753  value = cpl_table_get_int(defaults, alias, 0, NULL);
754  if (value < 0 || value > 1) {
755  cpl_msg_error(func, "Illegal parameter value in table "
756  "column \"%s\": it should be either 0 or 1",
757  alias);
758  cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
759  return 0;
760  }
761  cpl_parameter_set_bool(param, value);
762  }
763  else {
764  cpl_msg_error(func, "Invalid parameter value in table "
765  "column \"%s\"", alias);
766  cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
767  return 0;
768  }
769  }
770  else {
771  cpl_msg_warning(func, "Parameter \"%s\" not found in GRISM_TABLE "
772  "- using recipe default", alias);
773  }
774  }
775 
776  value = cpl_parameter_get_bool(param);
777 
778  if (value) {
779  cpl_msg_info(func, "%s:", alias);
780  cpl_msg_info(func, "%s: TRUE", cpl_parameter_get_help(param));
781  }
782  else {
783  cpl_msg_info(func, "%s:", alias);
784  cpl_msg_info(func, "%s: FALSE", cpl_parameter_get_help(param));
785  }
786 
787  return value;
788 
789 }
790 
791 /*----------------------------------------------------------------------------*/
795 /*----------------------------------------------------------------------------*/
796 int dfs_get_parameter_bool_const(const cpl_parameterlist *parlist, const char *name)
797 {
798  return dfs_get_parameter_bool((cpl_parameterlist *)parlist, name, NULL);
799 }
800 
801 /*----------------------------------------------------------------------------*/
805 /*----------------------------------------------------------------------------*/
806 int dfs_get_parameter_int_const(const cpl_parameterlist *parlist, const char *name)
807 {
808  return dfs_get_parameter_int((cpl_parameterlist *)parlist, name, NULL);
809 }
810 
811 /*----------------------------------------------------------------------------*/
815 /*----------------------------------------------------------------------------*/
816 double dfs_get_parameter_double_const(const cpl_parameterlist *parlist, const char *name)
817 {
818  return dfs_get_parameter_double((cpl_parameterlist *)parlist, name, NULL);
819 }
820 
821 /*----------------------------------------------------------------------------*/
825 /*----------------------------------------------------------------------------*/
826 const char *dfs_get_parameter_string_const(const cpl_parameterlist *parlist, const char *name)
827 {
828  return dfs_get_parameter_string((cpl_parameterlist *)parlist, name, NULL);
829 }
830 
831 /*----------------------------------------------------------------------------*/
859 /*----------------------------------------------------------------------------*/
860 cpl_image *dfs_load_image(cpl_frameset *frameset, const char *category,
861  cpl_type type, int ext, int calib)
862 {
863  const char *func = "dfs_load_image";
864 
865  cpl_frame *frame = NULL;
866  cpl_image *image = NULL;
867 
868 
869  frame = cpl_frameset_find(frameset, category);
870 
871  if (frame) {
872  image = cpl_image_load(cpl_frame_get_filename(frame), type, 0, ext);
873  if (image == NULL) {
874  cpl_msg_error(cpl_error_get_where(),"%s", cpl_error_get_message());
875  cpl_msg_error(func, "Cannot load image %s",
876  cpl_frame_get_filename(frame));
877  }
878  else {
879  if (calib)
880  cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB);
881  else
882  cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
883  }
884  }
885 
886  return image;
887 }
888 
889 /*----------------------------------------------------------------------------*/
915 /*----------------------------------------------------------------------------*/
916 cpl_table *dfs_load_table(cpl_frameset *frameset, const char *category, int ext)
917 {
918  const char *func = "dfs_load_table";
919 
920  cpl_frame *frame = NULL;
921  cpl_table *table = NULL;
922 
923 
924  frame = cpl_frameset_find(frameset, category);
925 
926  if (frame) {
927  table = cpl_table_load(cpl_frame_get_filename(frame), ext, 1);
928  if (table == NULL) {
929  cpl_msg_error(cpl_error_get_where(), "%s", cpl_error_get_message());
930  cpl_msg_error(func, "Cannot load table %s",
931  cpl_frame_get_filename(frame));
932  }
933  }
934 
935  return table;
936 }
937 
938 /*----------------------------------------------------------------------------*/
963 /*----------------------------------------------------------------------------*/
964 cpl_propertylist *dfs_load_header(cpl_frameset *frameset,
965  const char *category, int ext)
966 {
967  const char *func = "dfs_load_header";
968 
969  cpl_frame *frame = NULL;
970  cpl_propertylist *plist = NULL;
971 
972 
973  frame = cpl_frameset_find(frameset, category);
974 
975  if (frame) {
976  plist = cpl_propertylist_load(cpl_frame_get_filename(frame), ext);
977  if (plist == NULL) {
978  cpl_msg_error(cpl_error_get_where(), "%s", cpl_error_get_message());
979  cpl_msg_error(func, "Cannot load header from %s",
980  cpl_frame_get_filename(frame));
981  }
982  }
983 
984  return plist;
985 }
986 
987 /*----------------------------------------------------------------------------*/
994 /*----------------------------------------------------------------------------*/
995 static void
996 dfs_save(cpl_frameset *frameset, const void *object, fors_type type,
997  const char *category, cpl_propertylist *header,
998  const cpl_parameterlist *parlist, const char *recipename,
999  const cpl_frame *inherit_frame)
1000 {
1001  char *filename;
1002  cpl_frame *frame;
1003  cpl_propertylist *plist;
1004  const char *version = NULL;
1005  cpl_propertylist *raw_header = NULL;
1006 
1007 
1008  if (category == NULL || frameset == NULL || object == NULL ||
1009  inherit_frame == NULL) {
1010  cpl_msg_error(cpl_error_get_where(), "%s", cpl_error_get_message());
1011  cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
1012  return;
1013  }
1014 
1015  if (type == FORS_TYPE_TABLE) {
1016  cpl_msg_debug(cpl_func, "Saving %s table to disk...", category);
1017  }
1018  else {
1019  cpl_msg_debug(cpl_func, "Saving %s image to disk...", category);
1020  }
1021 
1022  /* Read instrument version from raw frame */
1023  {
1024  const char *raw_filename =
1025  cpl_frame_get_filename(inherit_frame);
1026 
1027  raw_header = cpl_propertylist_load(raw_filename, 0);
1028  if (cpl_error_get_code() != CPL_ERROR_NONE) {
1029  cpl_msg_error(cpl_func, "Could not read %s primary header", raw_filename);
1030  return;
1031  }
1032 
1033  version = fors_dfs_pipeline_version(raw_header, NULL);
1034  cpl_propertylist_delete(raw_header);
1035 
1036  if (cpl_error_get_code() != CPL_ERROR_NONE) {
1037  cpl_msg_error(cpl_func, "Could not identify instrument version from %s header",
1038  raw_filename);
1039  return;
1040  }
1041  }
1042 
1043  filename = cpl_calloc(strlen(category) + 6, sizeof(char));
1044 
1045  strlower(strcpy(filename, category));
1046  strcat(filename, ".fits");
1047 
1048  frame = cpl_frame_new();
1049 
1050  cpl_frame_set_filename(frame, filename);
1051  cpl_frame_set_tag(frame, category);
1052  cpl_frame_set_type(frame, CPL_FRAME_TYPE_ANY);
1053  cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1054  cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1055  if (cpl_error_get_code()) {
1056  cpl_msg_error(cpl_error_get_where(), "%s", cpl_error_get_message());
1057  cpl_msg_error(cpl_func, "Cannot initialise the product frame");
1058  cpl_frame_delete(frame);
1059  cpl_free(filename);
1060  cpl_free((void *)version);
1061  return;
1062  }
1063 
1064 
1065  /*
1066  * Produce DFS compliant FITS header for product
1067  */
1068 
1069  if (header == NULL)
1070  plist = cpl_propertylist_new();
1071  else
1072  plist = header;
1073 
1074  if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1075  recipename, version, "PRO-1.15",
1076  inherit_frame)) {
1077  cpl_msg_error(cpl_func, "Error found in %s: %s",
1078  cpl_error_get_where(), cpl_error_get_message());
1079  cpl_msg_error(cpl_func, "Problem with product %s FITS header definition",
1080  category);
1081  if (header == NULL)
1082  cpl_propertylist_delete(plist);
1083  cpl_frame_delete(frame);
1084  cpl_free(filename);
1085  cpl_free((void *)version);
1086  return;
1087  }
1088 
1089  cpl_free((void *)version);
1090 
1091  /*
1092  * Write to disk
1093  */
1094 
1095  if (type == FORS_TYPE_IMAGE_ERR) {
1096  fors_image_save((fors_image *)object, plist, filename);
1097  }
1098  else if (type == FORS_TYPE_IMAGE) {
1099  cpl_image_save((cpl_image *)object, filename, CPL_BPP_IEEE_FLOAT, plist,
1100  CPL_IO_DEFAULT);
1101  }
1102  else {
1103  cpl_table_save((cpl_table *)object,
1104  plist, NULL, filename, CPL_IO_DEFAULT);
1105  }
1106 
1107  if (cpl_error_get_code() != CPL_ERROR_NONE) {
1108  cpl_msg_error(cpl_func, "Error found in %s: %s",
1109  cpl_error_get_where(), cpl_error_get_message());
1110  cpl_msg_error(cpl_func, "Cannot save product %s to disk", filename);
1111  if (header == NULL)
1112  cpl_propertylist_delete(plist);
1113  cpl_frame_delete(frame);
1114  cpl_free(filename);
1115  return;
1116  }
1117 
1118  if (header == NULL)
1119  cpl_propertylist_delete(plist);
1120 
1121  cpl_free(filename);
1122 
1123  cpl_frameset_insert(frameset, frame);
1124 
1125  return;
1126 }
1127 
1128 /*----------------------------------------------------------------------------*/
1149 /*----------------------------------------------------------------------------*/
1150 void
1151 fors_dfs_save_image(cpl_frameset *frameset, const cpl_image *image,
1152  const char *category, cpl_propertylist *header,
1153  const cpl_parameterlist *parlist, const char *recipename,
1154  const cpl_frame *inherit_frame)
1155 {
1156  dfs_save(frameset, image, FORS_TYPE_IMAGE,
1157  category, header,
1158  parlist, recipename,
1159  inherit_frame);
1160 }
1161 
1162 /*----------------------------------------------------------------------------*/
1184 /*----------------------------------------------------------------------------*/
1185 void
1186 fors_dfs_save_image_mask(cpl_frameset *frameset, const cpl_image *image,
1187  const cpl_image *mask, const char *category,
1188  cpl_propertylist *header,
1189  const cpl_parameterlist *parlist, const char *recipename,
1190  const cpl_frame *inherit_frame)
1191 {
1192  char *filename;
1193  cpl_propertylist * extension_header;
1194 
1195  dfs_save(frameset, image, FORS_TYPE_IMAGE,
1196  category, header,
1197  parlist, recipename,
1198  inherit_frame);
1199 
1200  extension_header = cpl_propertylist_new();
1201  cpl_propertylist_append_string(extension_header,
1202  "EXTNAME", "IMAGE.BPM");
1203 
1204  filename = cpl_calloc(strlen(category) + 6, sizeof(char));
1205 
1206  strlower(strcpy(filename, category));
1207  strcat(filename, ".fits");
1208 
1209  cpl_image_save(mask, filename, CPL_BPP_IEEE_FLOAT, extension_header,
1210  CPL_IO_EXTEND);
1211  cpl_propertylist_delete(extension_header);
1212  cpl_free(filename);
1213 
1214 }
1215 
1216 /*----------------------------------------------------------------------------*/
1237 /*----------------------------------------------------------------------------*/
1238 void
1239 fors_dfs_save_image_err(cpl_frameset *frameset, const fors_image *image,
1240  const char *category, cpl_propertylist *header,
1241  const cpl_parameterlist *parlist, const char *recipename,
1242  const cpl_frame *inherit_frame)
1243 {
1244  dfs_save(frameset, image, FORS_TYPE_IMAGE_ERR,
1245  category, header,
1246  parlist, recipename,
1247  inherit_frame);
1248 }
1249 
1250 /*----------------------------------------------------------------------------*/
1273 /*----------------------------------------------------------------------------*/
1274 void
1275 fors_dfs_save_image_err_mask(cpl_frameset *frameset, const fors_image *image,
1276  const cpl_image *mask, const char *category,
1277  cpl_propertylist *header,
1278  const cpl_parameterlist *parlist, const char *recipename,
1279  const cpl_frame *inherit_frame)
1280 {
1281  char *filename;
1282  cpl_propertylist * extension_header;
1283 
1284  dfs_save(frameset, image, FORS_TYPE_IMAGE_ERR,
1285  category, header,
1286  parlist, recipename,
1287  inherit_frame);
1288 
1289  extension_header = cpl_propertylist_new();
1290  cpl_propertylist_append_string(extension_header,
1291  "EXTNAME", "IMAGE.BPM");
1292 
1293  filename = cpl_calloc(strlen(category) + 6, sizeof(char));
1294 
1295  strlower(strcpy(filename, category));
1296  strcat(filename, ".fits");
1297 
1298  cpl_image_save(mask, filename, CPL_BPP_IEEE_FLOAT, extension_header,
1299  CPL_IO_EXTEND);
1300  cpl_propertylist_delete(extension_header);
1301  cpl_free(filename);
1302 }
1303 
1304 #undef cleanup
1305 #define cleanup \
1306 do { \
1307  cpl_propertylist_delete(wcs_header); \
1308 } while (0)
1309 
1314 void fors_dfs_add_wcs(cpl_propertylist *header, const cpl_frame *frame,
1315  const fors_setting *setting)
1316 {
1317  bool invert = false;
1318  int extension = 0;
1319 
1320  cpl_propertylist *wcs_header =
1321  cpl_propertylist_load_regexp(cpl_frame_get_filename(frame),
1322  extension, WCS_KEYS, invert);
1323 
1324  cpl_propertylist_copy_property_regexp(header, wcs_header, ".*", invert);
1325 
1326  double crpix1 = cpl_propertylist_get_double(header, FORS_PFITS_CRPIX1);
1327 
1328  assure( !cpl_error_get_code(), return,
1329  "Could not read %s from %s", FORS_PFITS_CRPIX1,
1330  cpl_frame_get_filename(frame));
1331 
1332  double crpix2 = cpl_propertylist_get_double(header, FORS_PFITS_CRPIX2);
1333 
1334  assure( !cpl_error_get_code(), return,
1335  "Could not read %s from %s", FORS_PFITS_CRPIX2,
1336  cpl_frame_get_filename(frame));
1337 
1338  cpl_propertylist_update_double(header, FORS_PFITS_CRPIX1,
1339  crpix1 - setting->prescan_x);
1340 
1341  cpl_propertylist_update_double(header, FORS_PFITS_CRPIX2,
1342  crpix2 - setting->prescan_y);
1343 
1344 
1345  cleanup;
1346  return;
1347 }
1348 
1349 /*----------------------------------------------------------------------------*/
1350 #undef cleanup
1351 #define cleanup \
1352 do { \
1353  cpl_propertylist_delete(time_header); \
1354 } while (0)
1355 
1367 /*----------------------------------------------------------------------------*/
1368 void fors_dfs_add_exptime(cpl_propertylist *header, const cpl_frame *frame,
1369  double exptime)
1370 {
1371  bool invert = false;
1372  int extension = 0;
1373 
1374  cpl_propertylist *time_header = NULL;
1375 
1376  if (frame) {
1377 
1378  time_header =
1379  cpl_propertylist_load_regexp(cpl_frame_get_filename(frame),
1380  extension, "EXPTIME", invert);
1381 
1382  if (time_header) {
1383  cpl_propertylist_copy_property_regexp(header,
1384  time_header, ".*", invert);
1385  }
1386  else {
1387  cpl_error_reset();
1388  }
1389  }
1390  else {
1391  while (cpl_propertylist_erase(header, "EXPTIME"));
1392  cpl_propertylist_update_double(header, "EXPTIME", exptime);
1393  }
1394 
1395  cleanup;
1396  return;
1397 }
1398 
1399 /*----------------------------------------------------------------------------*/
1406 /*----------------------------------------------------------------------------*/
1407 void
1408 fors_dfs_save_table(cpl_frameset *frameset, const cpl_table *table,
1409  const char *category, cpl_propertylist *header,
1410  const cpl_parameterlist *parlist, const char *recipename,
1411  const cpl_frame *inherit_frame)
1412 {
1413  dfs_save(frameset, table, FORS_TYPE_TABLE,
1414  category, header,
1415  parlist, recipename,
1416  inherit_frame);
1417 }
1418 
1419 /*----------------------------------------------------------------------------*/
1451 /*----------------------------------------------------------------------------*/
1452 int dfs_save_image(cpl_frameset *frameset, const cpl_image *image,
1453  const char *category, cpl_propertylist *header,
1454  const cpl_parameterlist *parlist, const char *recipename,
1455  const char *version)
1456 {
1457  const char *func = "dfs_save_image";
1458 
1459  char *filename;
1460  cpl_frame *frame;
1461  cpl_propertylist *plist;
1462 
1463 
1464  if (category == NULL || frameset == NULL || image == NULL) {
1465  cpl_msg_error(cpl_func, "Error found in %s: %s",
1466  cpl_error_get_where(), cpl_error_get_message());
1467  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1468  return -1;
1469  }
1470 
1471  cpl_msg_info(func, "Saving %s image to disk...", category);
1472 
1473  filename = cpl_calloc(strlen(category) + 6, sizeof(char));
1474 
1475  strlower(strcpy(filename, category));
1476  strcat(filename, ".fits");
1477 
1478  frame = cpl_frame_new();
1479 
1480  cpl_frame_set_filename(frame, filename);
1481  cpl_frame_set_tag(frame, category);
1482  cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
1483  cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1484  cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1485  if (cpl_error_get_code()) {
1486  cpl_msg_error(cpl_func, "Error found in %s: %s",
1487  cpl_error_get_where(), cpl_error_get_message());
1488  cpl_msg_error(func, "Cannot initialise the product frame");
1489  cpl_frame_delete(frame);
1490  cpl_free(filename);
1491  return -1;
1492  }
1493 
1494 
1495  /*
1496  * Produce DFS compliant FITS header for image
1497  */
1498 
1499  if (header == NULL)
1500  plist = cpl_propertylist_new();
1501  else
1502  plist = header;
1503 
1504  if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1505  recipename, version, "PRO-1.15", NULL)) {
1506  cpl_msg_error(cpl_func, "Error found in %s: %s",
1507  cpl_error_get_where(), cpl_error_get_message());
1508  cpl_msg_error(func, "Problem with product %s FITS header definition",
1509  category);
1510  if (header == NULL)
1511  cpl_propertylist_delete(plist);
1512  cpl_frame_delete(frame);
1513  cpl_free(filename);
1514  return -1;
1515  }
1516 
1517  /*
1518  * Write image to disk
1519  */
1520 
1521  if (cpl_image_save(image, filename, CPL_BPP_IEEE_FLOAT, plist,
1522  CPL_IO_DEFAULT)) {
1523  cpl_msg_error(cpl_func, "Error found in %s: %s",
1524  cpl_error_get_where(), cpl_error_get_message());
1525  cpl_msg_error(func, "Cannot save product %s to disk", filename);
1526  if (header == NULL)
1527  cpl_propertylist_delete(plist);
1528  cpl_frame_delete(frame);
1529  cpl_free(filename);
1530  return -1;
1531  }
1532 
1533  if (header == NULL)
1534  cpl_propertylist_delete(plist);
1535 
1536  cpl_free(filename);
1537 
1538  cpl_frameset_insert(frameset, frame);
1539 
1540  return 0;
1541 }
1542 
1543 /*----------------------------------------------------------------------------*/
1575 /*----------------------------------------------------------------------------*/
1576 int dfs_save_table(cpl_frameset *frameset, const cpl_table *table,
1577  const char *category, cpl_propertylist *header,
1578  const cpl_parameterlist *parlist, const char *recipename,
1579  const char *version)
1580 {
1581  const char *func = "dfs_save_table";
1582 
1583  char *filename;
1584  cpl_frame *frame;
1585  cpl_propertylist *plist;
1586 
1587 
1588  if (category == NULL || frameset == NULL || table == NULL) {
1589  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1590  cpl_msg_error(cpl_func, "Error found in %s: %s",
1591  cpl_error_get_where(), cpl_error_get_message());
1592  return -1;
1593  }
1594 
1595  cpl_msg_info(func, "Saving %s table to disk...", category);
1596 
1597  filename = cpl_calloc(strlen(category) + 6, sizeof(char));
1598 
1599  strlower(strcpy(filename, category));
1600 
1601  strcat(filename, ".fits");
1602 
1603  frame = cpl_frame_new();
1604 
1605  cpl_frame_set_filename(frame, filename);
1606  cpl_frame_set_tag(frame, category);
1607  cpl_frame_set_type(frame, CPL_FRAME_TYPE_TABLE);
1608  cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1609  cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1610  if (cpl_error_get_code()) {
1611  cpl_msg_error(cpl_func, "Error found in %s: %s",
1612  cpl_error_get_where(), cpl_error_get_message());
1613  cpl_msg_error(func, "Cannot initialise the product frame");
1614  cpl_frame_delete(frame);
1615  cpl_free(filename);
1616  return -1;
1617  }
1618 
1619 
1620  /*
1621  * Produce DFS compliant FITS header for table
1622  */
1623 
1624  if (header == NULL)
1625  plist = cpl_propertylist_new();
1626  else
1627  plist = header;
1628 
1629  if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1630  recipename, version, "PRO-1.15", NULL)) {
1631  cpl_msg_error(cpl_func, "Error found in %s: %s",
1632  cpl_error_get_where(), cpl_error_get_message());
1633  cpl_msg_error(func, "Problem with product %s FITS header definition",
1634  category);
1635  if (header == NULL)
1636  cpl_propertylist_delete(plist);
1637  cpl_frame_delete(frame);
1638  cpl_free(filename);
1639  return -1;
1640  }
1641 
1642  /*
1643  * Write table to disk
1644  */
1645 
1646  if (cpl_table_save(table, plist, NULL, filename, CPL_IO_DEFAULT)) {
1647  cpl_msg_error(cpl_func, "Error found in %s: %s",
1648  cpl_error_get_where(), cpl_error_get_message());
1649  cpl_msg_error(func, "Cannot save product %s to disk", filename);
1650  if (header == NULL)
1651  cpl_propertylist_delete(plist);
1652  cpl_frame_delete(frame);
1653  cpl_free(filename);
1654  return -1;
1655  }
1656 
1657  if (header == NULL)
1658  cpl_propertylist_delete(plist);
1659  cpl_free(filename);
1660 
1661  cpl_frameset_insert(frameset, frame);
1662 
1663  return 0;
1664 }
1665 
1666 /*----------------------------------------------------------------------------*/
1683 /*----------------------------------------------------------------------------*/
1684 int dfs_equal_keyword(cpl_frameset *frameset, const char *keyword)
1685 {
1686  const char *func = "dfs_equal_keyword";
1687 
1688  cpl_frame *frame;
1689  cpl_propertylist *reference;
1690  cpl_type rtype;
1691  cpl_type type;
1692  const char *rstring;
1693  const char *string;
1694  int rintero;
1695  int intero;
1696  int found;
1697 
1698 
1699  if (frameset == NULL || keyword == NULL) {
1700  cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1701  return 0;
1702  }
1703 
1704  if (cpl_frameset_is_empty(frameset)) {
1705  cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
1706  return 0;
1707  }
1708 
1709  frame = cpl_frameset_get_first(frameset);
1710 
1711  found = 0;
1712 
1713  while (frame) {
1714 
1715  reference = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
1716  if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
1717  cpl_error_reset();
1718  frame = cpl_frameset_get_next(frameset);
1719  continue;
1720  }
1721 
1722  if (cpl_propertylist_has(reference, keyword)) {
1723  rtype = cpl_propertylist_get_type(reference, keyword);
1724 
1725  if (rtype == CPL_TYPE_STRING) {
1726  found = 1;
1727  rstring = cpl_strdup(cpl_propertylist_get_string(reference,
1728  keyword));
1729  cpl_propertylist_delete(reference);
1730  break;
1731  }
1732 
1733  if (rtype == CPL_TYPE_INT) {
1734  found = 1;
1735  rintero = cpl_propertylist_get_int(reference, keyword);
1736  cpl_propertylist_delete(reference);
1737  break;
1738  }
1739 
1740  cpl_propertylist_delete(reference);
1741  return 0;
1742  }
1743 
1744  cpl_propertylist_delete(reference);
1745 
1746  frame = cpl_frameset_get_next(frameset);
1747  }
1748 
1749 
1750  if (!found)
1751  return 1;
1752 
1753  frame = cpl_frameset_get_first(frameset);
1754 
1755  while (frame) {
1756 
1757  reference = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
1758  if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
1759  cpl_error_reset();
1760  frame = cpl_frameset_get_next(frameset);
1761  continue;
1762  }
1763 
1764  if (cpl_propertylist_has(reference, keyword)) {
1765 
1766  type = cpl_propertylist_get_type(reference, keyword);
1767 
1768  if (rtype != type) {
1769  cpl_propertylist_delete(reference);
1770  return 0;
1771  }
1772 
1773  if (rtype == CPL_TYPE_STRING) {
1774  string = cpl_propertylist_get_string(reference,
1775  keyword);
1776  if (strncmp(rstring, string, 15)) {
1777  cpl_propertylist_delete(reference);
1778  return 0;
1779  }
1780  }
1781 
1782  if (rtype == CPL_TYPE_INT) {
1783  intero = cpl_propertylist_get_int(reference, keyword);
1784  if (rintero - intero) {
1785  cpl_propertylist_delete(reference);
1786  return 0;
1787  }
1788  }
1789  }
1790 
1791  cpl_propertylist_delete(reference);
1792 
1793  frame = cpl_frameset_get_next(frameset);
1794  }
1795 
1796  if (rtype == CPL_TYPE_STRING)
1797  cpl_free((void *)rstring);
1798 
1799  return 1;
1800 
1801 }
1802 
1812 cpl_error_code dfs_save_table_ext(cpl_table * table,
1813  const char * tag,
1814  cpl_propertylist * extheader)
1815 {
1816  char * filename = cpl_calloc(strlen(tag) + 6, sizeof(char));
1817  cpl_propertylist * header;
1818 
1819  if (extheader) {
1820  header = cpl_propertylist_duplicate(extheader);
1821 
1822  cpl_propertylist_erase_regexp(header,
1823  "^ESO DPR |^ARCFILE$|^ORIGFILE$", 0);
1824  } else {
1825  header = NULL;
1826  }
1827 
1828  strlower(strcpy(filename, tag));
1829  strcat(filename, ".fits");
1830 
1831  if (cpl_table_save(table, NULL, header, filename, CPL_IO_EXTEND)) {
1832  cpl_free(filename);
1833  cpl_ensure_code(0, CPL_ERROR_FILE_IO);
1834  }
1835 
1836  cpl_propertylist_delete(header);
1837  cpl_free(filename);
1838 
1839  return CPL_ERROR_NONE;
1840 }
1841 
1851 cpl_error_code dfs_save_image_ext(cpl_image * image,
1852  const char * tag,
1853  cpl_propertylist * extheader)
1854 {
1855  char * filename = cpl_calloc(strlen(tag) + 6, sizeof(char));
1856 
1857  cpl_propertylist * header;
1858 
1859  if (extheader) {
1860  header = cpl_propertylist_duplicate(extheader);
1861 
1862  cpl_propertylist_erase_regexp(header,
1863  "^ESO DPR |^ARCFILE$|^ORIGFILE$", 0);
1864  } else {
1865  header = NULL;
1866  }
1867 
1868  strlower(strcpy(filename, tag));
1869  strcat(filename, ".fits");
1870 
1871  if (cpl_image_save(image, filename, CPL_BPP_IEEE_FLOAT,
1872  header, CPL_IO_EXTEND)) {
1873  cpl_free(filename);
1874  cpl_ensure_code(0, CPL_ERROR_FILE_IO);
1875  }
1876 
1877  cpl_propertylist_delete(header);
1878  cpl_free(filename);
1879 
1880  return CPL_ERROR_NONE;
1881 }
1882 
1894 cpl_error_code dfs_save_image_null(cpl_frameset * frameset,
1895  cpl_parameterlist * parlist,
1896  const char * tag,
1897  const char * recipename,
1898  const char * version)
1899 {
1900 
1901  char * filename = cpl_calloc(strlen(tag) + 6, sizeof(char));
1902 
1903  cpl_error_code error;
1904 
1905  cpl_propertylist * pro = cpl_propertylist_new();
1906 
1907  cpl_propertylist_append_string(pro, "ESO PRO CATG", tag);
1908 
1909  strlower(strcpy(filename, tag));
1910  strcat(filename, ".fits");
1911 
1912  error = cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, NULL,
1913  CPL_BPP_IEEE_FLOAT, recipename, pro,
1914  NULL, version, filename);
1915 
1916  cpl_free(filename);
1917  cpl_propertylist_delete(pro);
1918 
1919  return error;
1920 }
1921 
1922 
static void errorstate_dump_one(unsigned self, unsigned first, unsigned last)
Dump a single CPL error.
Definition: fors_dfs.c:108
void fors_dfs_save_image_err_mask(cpl_frameset *frameset, const fors_image *image, const cpl_image *mask, const char *category, cpl_propertylist *header, const cpl_parameterlist *parlist, const char *recipename, const cpl_frame *inherit_frame)
Save DFS product (image) with it error data and a bad pixel mask.
Definition: fors_dfs.c:1275
int fors_end(const cpl_frameset *frames, cpl_errorstate before_exec)
End recipe execution.
Definition: fors_dfs.c:219
cpl_image * dfs_load_image(cpl_frameset *frameset, const char *category, cpl_type type, int ext, int calib)
Loading image data of given category.
Definition: fors_dfs.c:860
const char * dfs_get_parameter_string(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe string parameter value.
Definition: fors_dfs.c:602
void fors_frameset_print(const cpl_frameset *frames)
Print a frame set.
Definition: fors_utils.c:393
cpl_propertylist * dfs_load_header(cpl_frameset *frameset, const char *category, int ext)
Loading header associated to data of given category.
Definition: fors_dfs.c:964
void fors_dfs_add_exptime(cpl_propertylist *header, const cpl_frame *frame, double exptime)
Add keyword EXPTIME to header.
Definition: fors_dfs.c:1368
cpl_error_code dfs_save_image_null(cpl_frameset *frameset, cpl_parameterlist *parlist, const char *tag, const char *recipename, const char *version)
Save a product with an empty primary extension.
Definition: fors_dfs.c:1894
cpl_error_code dfs_save_image_ext(cpl_image *image, const char *tag, cpl_propertylist *extheader)
Save an image in a extension.
Definition: fors_dfs.c:1851
void fors_begin(cpl_frameset *frames, const char *description_short)
Start recipe execution.
Definition: fors_dfs.c:191
void fors_dfs_save_image(cpl_frameset *frameset, const cpl_image *image, const char *category, cpl_propertylist *header, const cpl_parameterlist *parlist, const char *recipename, const cpl_frame *inherit_frame)
Save DFS product (image)
Definition: fors_dfs.c:1151
static void dfs_save(cpl_frameset *frameset, const void *object, fors_type type, const char *category, cpl_propertylist *header, const cpl_parameterlist *parlist, const char *recipename, const cpl_frame *inherit_frame)
Save DFS product.
Definition: fors_dfs.c:996
#define assure(EXPR)
Definition: list.c:101
void fors_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: fors_dfs.c:257
int dfs_get_parameter_bool(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe boolean parameter value.
Definition: fors_dfs.c:701
int dfs_equal_keyword(cpl_frameset *frameset, const char *keyword)
Saving table data of given category.
Definition: fors_dfs.c:1684
int dfs_get_parameter_int_const(const cpl_parameterlist *parlist, const char *name)
Definition: fors_dfs.c:806
cpl_error_code dfs_save_table_ext(cpl_table *table, const char *tag, cpl_propertylist *extheader)
Save a table in a extension (different from the first one)
Definition: fors_dfs.c:1812
void fors_dfs_save_image_err(cpl_frameset *frameset, const fors_image *image, const char *category, cpl_propertylist *header, const cpl_parameterlist *parlist, const char *recipename, const cpl_frame *inherit_frame)
Save DFS product (image) with it error data.
Definition: fors_dfs.c:1239
int dfs_get_parameter_bool_const(const cpl_parameterlist *parlist, const char *name)
Definition: fors_dfs.c:796
void fors_dfs_save_table(cpl_frameset *frameset, const cpl_table *table, const char *category, cpl_propertylist *header, const cpl_parameterlist *parlist, const char *recipename, const cpl_frame *inherit_frame)
Save DFS product (table)
Definition: fors_dfs.c:1408
void fors_dfs_save_image_mask(cpl_frameset *frameset, const cpl_image *image, const cpl_image *mask, const char *category, cpl_propertylist *header, const cpl_parameterlist *parlist, const char *recipename, const cpl_frame *inherit_frame)
Save DFS product (image)
Definition: fors_dfs.c:1186
int dfs_save_image(cpl_frameset *frameset, const cpl_image *image, const char *category, cpl_propertylist *header, const cpl_parameterlist *parlist, const char *recipename, const char *version)
Saving image data of given category.
Definition: fors_dfs.c:1452
cpl_table * dfs_load_table(cpl_frameset *frameset, const char *category, int ext)
Loading table data of given category.
Definition: fors_dfs.c:916
int dfs_get_parameter_int(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe integer parameter value.
Definition: fors_dfs.c:407
int dfs_save_table(cpl_frameset *frameset, const cpl_table *table, const char *category, cpl_propertylist *header, const cpl_parameterlist *parlist, const char *recipename, const char *version)
Saving table data of given category.
Definition: fors_dfs.c:1576
const char * dfs_get_parameter_string_const(const cpl_parameterlist *parlist, const char *name)
Definition: fors_dfs.c:826
void fors_image_save(const fors_image *image, const cpl_propertylist *header, const char *filename)
Save image.
Definition: fors_image.c:377
double dfs_get_parameter_double(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe double parameter value.
Definition: fors_dfs.c:504
void fors_frame_print(const cpl_frame *f)
Print a frame.
Definition: fors_utils.c:427
const char * fors_dfs_pipeline_version(const cpl_propertylist *header, const char **instrument_version)
Get pipeline and instrument versions.
Definition: fors_dfs.c:360
void fors_dfs_add_wcs(cpl_propertylist *header, const cpl_frame *frame, const fors_setting *setting)
add WCS keywords to header
Definition: fors_dfs.c:1314
double dfs_get_parameter_double_const(const cpl_parameterlist *parlist, const char *name)
Definition: fors_dfs.c:816