34 #include <fors_utils.h>
35 #include <fors_image.h>
36 #include <fors_pfits.h>
58 #define WCS_KEYS "^((CRVAL|CRPIX|CTYPE|CDELT)[0-9]|RADECSYS|CD[0-9]_[0-9])$"
69 static char *strlower(
char *s)
85 char *dfs_generate_filename(
const char *category)
87 char *filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
89 strlower(strcpy(filename, category));
90 strcat(filename,
".fits");
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" :
"";
121 cpl_msg_info(cpl_func,
"No error(s) to dump");
128 cpl_msg_debug(cpl_func,
"Dumping all %u error(s)%s:", newest,
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);
137 const char *message_from_cpl = cpl_error_get_message();
139 if (message_from_cpl == NULL) {
141 cpl_msg_error(cpl_func,
"Unspecified error");
150 while (*message_from_cpl !=
'\0' && *message_from_cpl !=
':') {
151 message_from_cpl += 1;
154 if (*message_from_cpl !=
'\0') {
155 message_from_cpl += 1;
157 if (*message_from_cpl ==
' ') message_from_cpl++;
159 if (*message_from_cpl !=
'\0') {
161 cpl_msg_error(cpl_func,
"%s [%s]", message_from_cpl,
162 cpl_error_get_where());
165 cpl_msg_error(cpl_func,
"%s [%s]",
166 cpl_error_get_message(), cpl_error_get_where());
171 cpl_msg_error(cpl_func,
"%s [%s]",
172 cpl_error_get_message(), cpl_error_get_where());
192 const char *description_short)
194 cpl_msg_info(cpl_func,
"%s", PACKAGE_STRING);
195 cpl_msg_info(cpl_func,
"%s", description_short);
198 cpl_msg_info(cpl_func,
"Input frame%s:",
199 cpl_frameset_get_size(frames) != 1 ?
"s" :
"");
219 int fors_end(
const cpl_frameset *frames, cpl_errorstate before_exec)
221 if (cpl_error_get_code() == CPL_ERROR_NONE) {
225 cpl_msg_info(cpl_func,
"Product frame%s:",
226 cpl_frameset_get_size(frames) != 1 ?
"s" :
"");
228 for (f = cpl_frameset_get_first_const(frames);
230 f = cpl_frameset_get_next_const(frames)) {
231 if (cpl_frame_get_group(f) == CPL_FRAME_GROUP_PRODUCT) {
261 assure( set != NULL,
return, NULL );
263 for (f = cpl_frameset_get_first(set);
265 f = cpl_frameset_get_next(set)) {
267 const char *tag = cpl_frame_get_tag(f);
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);
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 ||
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);
338 cpl_msg_warning(cpl_func,
"Unrecognized frame tag: '%s'",
361 const char **instrument_version)
363 const char *instrume = NULL;
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);
370 assure( strlen(instrume) >= 5,
return NULL,
371 "%s keyword must be 'fors1' or 'fors2', not '%s'",
372 FORS_PFITS_INSTRUME, instrume);
374 assure( instrume[4] ==
'1' || instrume[4] ==
'2',
return NULL,
375 "Unrecognized %s: %s", FORS_PFITS_INSTRUME, instrume);
377 if (instrument_version != NULL) {
378 *instrument_version = cpl_sprintf(
"%s", instrume);
381 return cpl_sprintf(
"fors%c/%s", instrume[4], PACKAGE_VERSION);
408 const cpl_table *defaults)
410 const char *func =
"dfs_get_parameter_int";
413 cpl_parameter *param;
416 if (parlist == NULL) {
417 cpl_msg_error(func,
"Missing input parameter list");
418 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
423 cpl_msg_error(func,
"Missing input parameter name");
424 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
428 param = cpl_parameterlist_find(parlist, name);
431 cpl_msg_error(func,
"Wrong parameter name: %s", name);
432 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
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);
443 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
447 cpl_parameter_get_default_int(param) == cpl_parameter_get_int(param)) {
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);
456 if (cpl_table_is_valid(defaults, alias, 0)) {
457 cpl_parameter_set_int(param, cpl_table_get_int(defaults,
461 cpl_msg_error(func,
"Invalid parameter value in table "
462 "column \"%s\"", alias);
463 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
468 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
469 "- using recipe default", alias);
473 cpl_msg_info(func,
"%s: %d", alias, cpl_parameter_get_int(param));
475 return cpl_parameter_get_int(param);
503 const char *name,
const cpl_table *defaults)
505 const char *func =
"dfs_get_parameter_double";
508 cpl_parameter *param;
511 if (parlist == NULL) {
512 cpl_msg_error(func,
"Missing input parameter list");
513 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
518 cpl_msg_error(func,
"Missing input parameter name");
519 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
523 param = cpl_parameterlist_find(parlist, name);
526 cpl_msg_error(func,
"Wrong parameter name: %s", name);
527 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
531 if (cpl_parameter_get_type(param) != CPL_TYPE_DOUBLE) {
532 cpl_msg_error(func,
"Unexpected type for parameter "
533 "\"%s\": it should be double", name);
534 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
538 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
542 cpl_parameter_get_default_double(param) ==
543 cpl_parameter_get_double(param)) {
545 if (cpl_table_has_column(defaults, alias)) {
546 if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_DOUBLE) {
547 cpl_msg_error(func,
"Unexpected type for GRISM_TABL "
548 "column \"%s\": it should be double", alias);
549 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
552 if (cpl_table_is_valid(defaults, alias, 0)) {
553 cpl_parameter_set_double(param, cpl_table_get_double(defaults,
557 cpl_msg_error(func,
"Invalid parameter value in table "
558 "column \"%s\"", alias);
559 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
564 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
565 "- using recipe default", alias);
569 cpl_msg_info(func,
"%s: %f", alias, cpl_parameter_get_double(param));
571 return cpl_parameter_get_double(param);
600 const cpl_table *defaults)
602 const char *func =
"dfs_get_parameter_string";
605 cpl_parameter *param;
608 if (parlist == NULL) {
609 cpl_msg_error(func,
"Missing input parameter list");
610 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
615 cpl_msg_error(func,
"Missing input parameter name");
616 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
620 param = cpl_parameterlist_find(parlist, name);
623 cpl_msg_error(func,
"Wrong parameter name: %s", name);
624 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
628 if (cpl_parameter_get_type(param) != CPL_TYPE_STRING) {
629 cpl_msg_error(func,
"Unexpected type for parameter "
630 "\"%s\": it should be string", name);
631 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
635 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
639 strcmp(cpl_parameter_get_default_string(param),
640 cpl_parameter_get_string(param)) == 0) {
642 if (cpl_table_has_column(defaults, alias)) {
643 if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_STRING) {
644 cpl_msg_error(func,
"Unexpected type for GRISM_TABLE "
645 "column \"%s\": it should be string", alias);
646 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
649 if (cpl_table_is_valid(defaults, alias, 0)) {
650 cpl_parameter_set_string(param, cpl_table_get_string(defaults,
654 cpl_msg_error(func,
"Invalid parameter value in table "
655 "column \"%s\"", alias);
656 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
661 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
662 "- using recipe default", alias);
666 cpl_msg_info(func,
"%s: %s", alias, cpl_parameter_get_string(param));
668 return cpl_parameter_get_string(param);
696 const cpl_table *defaults)
698 const char *func =
"dfs_get_parameter_bool";
701 cpl_parameter *param;
705 if (parlist == NULL) {
706 cpl_msg_error(func,
"Missing input parameter list");
707 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
712 cpl_msg_error(func,
"Missing input parameter name");
713 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
717 param = cpl_parameterlist_find(parlist, name);
720 cpl_msg_error(func,
"Wrong parameter name: %s", name);
721 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
725 if (cpl_parameter_get_type(param) != CPL_TYPE_BOOL) {
726 cpl_msg_error(func,
"Unexpected type for parameter "
727 "\"%s\": it should be boolean", name);
728 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
732 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
736 cpl_parameter_get_default_bool(param) ==
737 cpl_parameter_get_bool(param)) {
739 if (cpl_table_has_column(defaults, alias)) {
740 if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_INT) {
741 cpl_msg_error(func,
"Unexpected type for GRISM_TABLE "
742 "column \"%s\": it should be integer", alias);
743 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
746 if (cpl_table_is_valid(defaults, alias, 0)) {
747 value = cpl_table_get_int(defaults, alias, 0, NULL);
748 if (value < 0 || value > 1) {
749 cpl_msg_error(func,
"Illegal parameter value in table "
750 "column \"%s\": it should be either 0 or 1",
752 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
755 cpl_parameter_set_bool(param, value);
758 cpl_msg_error(func,
"Invalid parameter value in table "
759 "column \"%s\"", alias);
760 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
765 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
766 "- using recipe default", alias);
770 value = cpl_parameter_get_bool(param);
773 cpl_msg_info(func,
"%s: TRUE", alias);
776 cpl_msg_info(func,
"%s: FALSE", alias);
853 cpl_type type,
int ext,
int calib)
855 const char *func =
"dfs_load_image";
857 cpl_frame *frame = NULL;
858 cpl_image *image = NULL;
861 frame = cpl_frameset_find(frameset, category);
864 image = cpl_image_load(cpl_frame_get_filename(frame), type, 0, ext);
866 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
867 cpl_msg_error(func,
"Cannot load image %s",
868 cpl_frame_get_filename(frame));
872 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB);
874 cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
910 const char *func =
"dfs_load_table";
912 cpl_frame *frame = NULL;
913 cpl_table *table = NULL;
916 frame = cpl_frameset_find(frameset, category);
919 table = cpl_table_load(cpl_frame_get_filename(frame), ext, 1);
921 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
922 cpl_msg_error(func,
"Cannot load table %s",
923 cpl_frame_get_filename(frame));
957 const char *category,
int ext)
959 const char *func =
"dfs_load_header";
961 cpl_frame *frame = NULL;
962 cpl_propertylist *plist = NULL;
965 frame = cpl_frameset_find(frameset, category);
968 plist = cpl_propertylist_load(cpl_frame_get_filename(frame), ext);
970 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
971 cpl_msg_error(func,
"Cannot load header from %s",
972 cpl_frame_get_filename(frame));
988 dfs_save(cpl_frameset *frameset,
const void *
object, fors_type type,
989 const char *category, cpl_propertylist *header,
990 cpl_propertylist *extra_header,
991 const cpl_parameterlist *parlist,
const char *recipename,
992 const cpl_frame *inherit_frame)
996 cpl_propertylist *plist;
997 const char *version = NULL;
998 cpl_propertylist *raw_header = NULL;
1001 if (category == NULL || frameset == NULL ||
object == NULL ||
1002 inherit_frame == NULL) {
1003 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
1004 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
1008 if (type == FORS_TYPE_TABLE) {
1009 cpl_msg_debug(cpl_func,
"Saving %s table to disk...", category);
1012 cpl_msg_debug(cpl_func,
"Saving %s image to disk...", category);
1017 const char *raw_filename =
1018 cpl_frame_get_filename(inherit_frame);
1020 raw_header = cpl_propertylist_load(raw_filename, 0);
1021 if (raw_header == NULL) {
1022 cpl_msg_error(cpl_func,
"Could not read %s primary header", raw_filename);
1027 cpl_propertylist_delete(raw_header);
1029 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1030 cpl_msg_error(cpl_func,
"Could not identify instrument version from %s header",
1036 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1038 strlower(strcpy(filename, category));
1039 strcat(filename,
".fits");
1041 frame = cpl_frame_new();
1043 cpl_frame_set_filename(frame, filename);
1044 cpl_frame_set_tag(frame, category);
1045 cpl_frame_set_type(frame, CPL_FRAME_TYPE_ANY);
1046 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1047 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1048 if (cpl_error_get_code()) {
1049 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
1050 cpl_msg_error(cpl_func,
"Cannot initialise the product frame");
1051 cpl_frame_delete(frame);
1053 cpl_free((
void *)version);
1063 plist = cpl_propertylist_new();
1065 plist = cpl_propertylist_duplicate(header);
1067 if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1068 recipename, version,
"PRO-1.15",
1070 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1071 cpl_error_get_where(), cpl_error_get_message());
1072 cpl_msg_error(cpl_func,
"Problem with product %s FITS header definition",
1074 cpl_propertylist_delete(plist);
1075 cpl_frame_delete(frame);
1077 cpl_free((
void *)version);
1081 cpl_free((
void *)version);
1087 if (type == FORS_TYPE_IMAGE_ERR) {
1090 else if (type == FORS_TYPE_IMAGE) {
1091 cpl_image_save((cpl_image *)
object, filename, CPL_BPP_IEEE_FLOAT, plist,
1095 cpl_table_save((cpl_table *)
object,
1096 plist, NULL, filename, CPL_IO_DEFAULT);
1099 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1100 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1101 cpl_error_get_where(), cpl_error_get_message());
1102 cpl_msg_error(cpl_func,
"Cannot save product %s to disk", filename);
1103 cpl_propertylist_delete(plist);
1104 cpl_frame_delete(frame);
1109 cpl_propertylist_delete(plist);
1113 cpl_frameset_insert(frameset, frame);
1142 const char *category, cpl_propertylist *header,
1143 const cpl_parameterlist *parlist,
const char *recipename,
1144 const cpl_frame *inherit_frame)
1146 dfs_save(frameset, image, FORS_TYPE_IMAGE,
1147 category, header, NULL,
1148 parlist, recipename,
1177 const cpl_image *mask,
const char *category,
1178 cpl_propertylist *header,
1179 const cpl_parameterlist *parlist,
const char *recipename,
1180 const cpl_frame *inherit_frame)
1183 cpl_propertylist * extension_header;
1185 dfs_save(frameset, image, FORS_TYPE_IMAGE,
1186 category, header, NULL,
1187 parlist, recipename,
1190 extension_header = cpl_propertylist_new();
1191 cpl_propertylist_append_string(extension_header,
1192 "EXTNAME",
"IMAGE.BPM");
1194 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1196 strlower(strcpy(filename, category));
1197 strcat(filename,
".fits");
1199 cpl_image_save(mask, filename, CPL_BPP_IEEE_FLOAT, extension_header,
1201 cpl_propertylist_delete(extension_header);
1230 const char *category, cpl_propertylist *header,
1231 cpl_propertylist *err_header,
1232 const cpl_parameterlist *parlist,
const char *recipename,
1233 const cpl_frame *inherit_frame)
1235 dfs_save(frameset, image, FORS_TYPE_IMAGE_ERR,
1236 category, header, err_header,
1237 parlist, recipename,
1267 const cpl_image *mask,
const char *category,
1268 cpl_propertylist *header,
1269 const cpl_parameterlist *parlist,
const char *recipename,
1270 const cpl_frame *inherit_frame)
1273 cpl_propertylist * extension_header;
1275 dfs_save(frameset, image, FORS_TYPE_IMAGE_ERR,
1276 category, header, NULL,
1277 parlist, recipename,
1280 extension_header = cpl_propertylist_new();
1281 cpl_propertylist_append_string(extension_header,
1282 "EXTNAME",
"IMAGE.BPM");
1284 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1286 strlower(strcpy(filename, category));
1287 strcat(filename,
".fits");
1289 cpl_image_save(mask, filename, CPL_BPP_IEEE_FLOAT, extension_header,
1291 cpl_propertylist_delete(extension_header);
1298 cpl_propertylist_delete(wcs_header); \
1308 bool invert =
false;
1311 cpl_propertylist *wcs_header =
1312 cpl_propertylist_load_regexp(cpl_frame_get_filename(frame),
1313 extension, WCS_KEYS, invert);
1315 cpl_propertylist_copy_property_regexp(header, wcs_header,
".*", invert);
1317 double crpix1 = cpl_propertylist_get_double(header, FORS_PFITS_CRPIX1);
1319 assure( !cpl_error_get_code(),
return,
1320 "Could not read %s from %s", FORS_PFITS_CRPIX1,
1321 cpl_frame_get_filename(frame));
1323 double crpix2 = cpl_propertylist_get_double(header, FORS_PFITS_CRPIX2);
1325 assure( !cpl_error_get_code(),
return,
1326 "Could not read %s from %s", FORS_PFITS_CRPIX2,
1327 cpl_frame_get_filename(frame));
1329 cpl_propertylist_update_double(header, FORS_PFITS_CRPIX1,
1330 crpix1 - setting->prescan_x);
1332 cpl_propertylist_update_double(header, FORS_PFITS_CRPIX2,
1333 crpix2 - setting->prescan_y);
1344 cpl_propertylist_delete(time_header); \
1362 bool invert =
false;
1365 cpl_propertylist *time_header = NULL;
1370 cpl_propertylist_load_regexp(cpl_frame_get_filename(frame),
1371 extension,
"EXPTIME", invert);
1374 cpl_propertylist_copy_property_regexp(header,
1375 time_header,
".*", invert);
1382 while (cpl_propertylist_erase(header,
"EXPTIME"));
1383 cpl_propertylist_update_double(header,
"EXPTIME", exptime);
1400 const char *category, cpl_propertylist *header,
1401 const cpl_parameterlist *parlist,
const char *recipename,
1402 const cpl_frame *inherit_frame)
1404 dfs_save(frameset, table, FORS_TYPE_TABLE,
1405 category, header, NULL,
1406 parlist, recipename,
1444 const char *category, cpl_propertylist *header,
1445 const cpl_parameterlist *parlist,
const char *recipename,
1446 const char *version)
1448 const char *func =
"dfs_save_image";
1452 cpl_propertylist *plist;
1455 if (category == NULL || frameset == NULL || image == NULL) {
1456 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1457 cpl_error_get_where(), cpl_error_get_message());
1458 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1462 cpl_msg_info(func,
"Saving %s image to disk...", category);
1464 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1466 strlower(strcpy(filename, category));
1467 strcat(filename,
".fits");
1469 frame = cpl_frame_new();
1471 cpl_frame_set_filename(frame, filename);
1472 cpl_frame_set_tag(frame, category);
1473 cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
1474 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1475 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1476 if (cpl_error_get_code()) {
1477 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1478 cpl_error_get_where(), cpl_error_get_message());
1479 cpl_msg_error(func,
"Cannot initialise the product frame");
1480 cpl_frame_delete(frame);
1491 plist = cpl_propertylist_new();
1495 if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1496 recipename, version,
"PRO-1.15", NULL)) {
1497 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1498 cpl_error_get_where(), cpl_error_get_message());
1499 cpl_msg_error(func,
"Problem with product %s FITS header definition",
1502 cpl_propertylist_delete(plist);
1503 cpl_frame_delete(frame);
1512 if (cpl_image_save(image, filename, CPL_BPP_IEEE_FLOAT, plist,
1514 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1515 cpl_error_get_where(), cpl_error_get_message());
1516 cpl_msg_error(func,
"Cannot save product %s to disk", filename);
1518 cpl_propertylist_delete(plist);
1519 cpl_frame_delete(frame);
1525 cpl_propertylist_delete(plist);
1529 cpl_frameset_insert(frameset, frame);
1568 const char *category, cpl_propertylist *header,
1569 const cpl_parameterlist *parlist,
const char *recipename,
1570 const char *version)
1572 const char *func =
"dfs_save_table";
1576 cpl_propertylist *plist;
1579 if (category == NULL || frameset == NULL || table == NULL) {
1580 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1581 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1582 cpl_error_get_where(), cpl_error_get_message());
1586 cpl_msg_info(func,
"Saving %s table to disk...", category);
1588 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1590 strlower(strcpy(filename, category));
1592 strcat(filename,
".fits");
1594 frame = cpl_frame_new();
1596 cpl_frame_set_filename(frame, filename);
1597 cpl_frame_set_tag(frame, category);
1598 cpl_frame_set_type(frame, CPL_FRAME_TYPE_TABLE);
1599 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1600 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1601 if (cpl_error_get_code()) {
1602 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1603 cpl_error_get_where(), cpl_error_get_message());
1604 cpl_msg_error(func,
"Cannot initialise the product frame");
1605 cpl_frame_delete(frame);
1616 plist = cpl_propertylist_new();
1620 if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1621 recipename, version,
"PRO-1.15", NULL)) {
1622 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1623 cpl_error_get_where(), cpl_error_get_message());
1624 cpl_msg_error(func,
"Problem with product %s FITS header definition",
1627 cpl_propertylist_delete(plist);
1628 cpl_frame_delete(frame);
1637 if (cpl_table_save(table, plist, NULL, filename, CPL_IO_DEFAULT)) {
1638 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1639 cpl_error_get_where(), cpl_error_get_message());
1640 cpl_msg_error(func,
"Cannot save product %s to disk", filename);
1642 cpl_propertylist_delete(plist);
1643 cpl_frame_delete(frame);
1649 cpl_propertylist_delete(plist);
1652 cpl_frameset_insert(frameset, frame);
1677 const char *func =
"dfs_equal_keyword";
1680 cpl_propertylist *reference;
1683 const char *rstring;
1690 if (frameset == NULL || keyword == NULL) {
1691 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1695 if (cpl_frameset_is_empty(frameset)) {
1696 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
1700 frame = cpl_frameset_get_first(frameset);
1706 reference = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
1707 if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
1709 frame = cpl_frameset_get_next(frameset);
1713 if (cpl_propertylist_has(reference, keyword)) {
1714 rtype = cpl_propertylist_get_type(reference, keyword);
1716 if (rtype == CPL_TYPE_STRING) {
1718 rstring = cpl_strdup(cpl_propertylist_get_string(reference,
1720 cpl_propertylist_delete(reference);
1724 if (rtype == CPL_TYPE_INT) {
1726 rintero = cpl_propertylist_get_int(reference, keyword);
1727 cpl_propertylist_delete(reference);
1731 cpl_propertylist_delete(reference);
1735 cpl_propertylist_delete(reference);
1737 frame = cpl_frameset_get_next(frameset);
1744 frame = cpl_frameset_get_first(frameset);
1748 reference = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
1749 if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
1751 frame = cpl_frameset_get_next(frameset);
1755 if (cpl_propertylist_has(reference, keyword)) {
1757 type = cpl_propertylist_get_type(reference, keyword);
1759 if (rtype != type) {
1760 cpl_propertylist_delete(reference);
1764 if (rtype == CPL_TYPE_STRING) {
1765 string = cpl_propertylist_get_string(reference,
1767 if (strncmp(rstring,
string, 15)) {
1768 cpl_propertylist_delete(reference);
1773 if (rtype == CPL_TYPE_INT) {
1774 intero = cpl_propertylist_get_int(reference, keyword);
1775 if (rintero - intero) {
1776 cpl_propertylist_delete(reference);
1782 cpl_propertylist_delete(reference);
1784 frame = cpl_frameset_get_next(frameset);
1787 if (rtype == CPL_TYPE_STRING)
1788 cpl_free((
void *)rstring);
1805 cpl_propertylist * extheader)
1807 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1808 cpl_propertylist * header;
1811 header = cpl_propertylist_duplicate(extheader);
1813 cpl_propertylist_erase_regexp(header,
1814 "^ESO DPR |^ARCFILE$|^ORIGFILE$", 0);
1819 strlower(strcpy(filename, tag));
1820 strcat(filename,
".fits");
1822 if (cpl_table_save(table, NULL, header, filename, CPL_IO_EXTEND)) {
1824 cpl_ensure_code(0, CPL_ERROR_FILE_IO);
1827 cpl_propertylist_delete(header);
1830 return CPL_ERROR_NONE;
1844 cpl_propertylist * extheader)
1846 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1848 cpl_propertylist * header;
1851 header = cpl_propertylist_duplicate(extheader);
1853 cpl_propertylist_erase_regexp(header,
1854 "^ESO DPR |^ARCFILE$|^ORIGFILE$", 0);
1859 strlower(strcpy(filename, tag));
1860 strcat(filename,
".fits");
1862 if (cpl_image_save(image, filename, CPL_BPP_IEEE_FLOAT,
1863 header, CPL_IO_EXTEND)) {
1865 cpl_ensure_code(0, CPL_ERROR_FILE_IO);
1868 cpl_propertylist_delete(header);
1871 return CPL_ERROR_NONE;
1886 cpl_parameterlist * parlist,
1888 const char * recipename,
1889 const char * version)
1892 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1894 cpl_error_code error;
1896 cpl_propertylist * pro = cpl_propertylist_new();
1898 cpl_propertylist_append_string(pro,
"ESO PRO CATG", tag);
1900 strlower(strcpy(filename, tag));
1901 strcat(filename,
".fits");
1903 error = cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, NULL,
1904 CPL_BPP_IEEE_FLOAT, recipename, pro,
1905 NULL, version, filename);
1908 cpl_propertylist_delete(pro);
static void errorstate_dump_one(unsigned self, unsigned first, unsigned last)
Dump a single CPL error.
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.
int fors_end(const cpl_frameset *frames, cpl_errorstate before_exec)
End recipe execution.
cpl_image * dfs_load_image(cpl_frameset *frameset, const char *category, cpl_type type, int ext, int calib)
Loading image data of given category.
const char * dfs_get_parameter_string(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe string parameter value.
void fors_frameset_print(const cpl_frameset *frames)
Print a frame set.
cpl_propertylist * dfs_load_header(cpl_frameset *frameset, const char *category, int ext)
Loading header associated to data of given category.
void fors_dfs_add_exptime(cpl_propertylist *header, const cpl_frame *frame, double exptime)
Add keyword EXPTIME to header.
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.
cpl_error_code dfs_save_image_ext(cpl_image *image, const char *tag, cpl_propertylist *extheader)
Save an image in a extension.
void fors_begin(cpl_frameset *frames, const char *description_short)
Start recipe execution.
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)
static void dfs_save(cpl_frameset *frameset, const void *object, fors_type type, const char *category, cpl_propertylist *header, cpl_propertylist *extra_header, const cpl_parameterlist *parlist, const char *recipename, const cpl_frame *inherit_frame)
Save DFS product.
void fors_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
int dfs_get_parameter_bool(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe boolean parameter value.
int dfs_equal_keyword(cpl_frameset *frameset, const char *keyword)
Saving table data of given category.
int dfs_get_parameter_int_const(const cpl_parameterlist *parlist, const char *name)
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)
int dfs_get_parameter_bool_const(const cpl_parameterlist *parlist, const char *name)
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)
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)
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.
cpl_table * dfs_load_table(cpl_frameset *frameset, const char *category, int ext)
Loading table data of given category.
int dfs_get_parameter_int(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe integer parameter value.
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.
const char * dfs_get_parameter_string_const(const cpl_parameterlist *parlist, const char *name)
double dfs_get_parameter_double(cpl_parameterlist *parlist, const char *name, const cpl_table *defaults)
Reading a recipe double parameter value.
void fors_frame_print(const cpl_frame *f)
Print a frame.
const char * fors_dfs_pipeline_version(const cpl_propertylist *header, const char **instrument_version)
Get pipeline and instrument versions.
void fors_dfs_add_wcs(cpl_propertylist *header, const cpl_frame *frame, const fors_setting *setting)
add WCS keywords to header
void fors_image_save(const fors_image *image, const cpl_propertylist *header, const cpl_propertylist *err_header, const char *filename)
Save image.
double dfs_get_parameter_double_const(const cpl_parameterlist *parlist, const char *name)
void fors_dfs_save_image_err(cpl_frameset *frameset, const fors_image *image, const char *category, cpl_propertylist *header, cpl_propertylist *err_header, const cpl_parameterlist *parlist, const char *recipename, const cpl_frame *inherit_frame)
Save DFS product (image) with it error data.