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,
"SCREEN_FLAT_PMOS") == 0 ||
277 strcmp(tag,
"STANDARD_PMOS" ) == 0 ||
278 strcmp(tag,
"SCIENCE_PMOS" ) == 0 ||
279 strcmp(tag,
"SCIENCE_MOS" ) == 0 ||
280 strcmp(tag,
"SCIENCE_MXU" ) == 0 ||
281 strcmp(tag,
"SCIENCE_LSS" ) == 0 ||
282 strcmp(tag,
"STANDARD_MOS" ) == 0 ||
283 strcmp(tag,
"STANDARD_MXU" ) == 0 ||
284 strcmp(tag,
"STANDARD_LSS" ) == 0 ||
285 strcmp(tag, SCIENCE_IMG ) == 0 ||
286 strcmp(tag,
"SCREEN_FLAT_MXU" ) == 0 ||
287 strcmp(tag,
"SCREEN_FLAT_MOS" ) == 0 ||
288 strcmp(tag,
"SCREEN_FLAT_LSS" ) == 0 ) {
289 cpl_frame_set_group(f, CPL_FRAME_GROUP_RAW);
291 else if (strcmp(tag, MASTER_BIAS ) == 0 ||
292 strcmp(tag, MASTER_DARK ) == 0 ||
293 strcmp(tag, MASTER_SCREEN_FLAT_IMG ) == 0 ||
294 strcmp(tag, MASTER_SKY_FLAT_IMG ) == 0 ||
295 strcmp(tag, ALIGNED_PHOT ) == 0 ||
296 strcmp(tag,
"MASTER_NORM_FLAT_PMOS" ) == 0 ||
297 strcmp(tag,
"DISP_COEFF_PMOS" ) == 0 ||
298 strcmp(tag,
"CURV_COEFF_PMOS" ) == 0 ||
299 strcmp(tag,
"SLIT_LOCATION_PMOS" ) == 0 ||
300 strcmp(tag,
"MASTER_NORM_FLAT_MOS" ) == 0 ||
301 strcmp(tag,
"MASTER_NORM_FLAT_MXU" ) == 0 ||
302 strcmp(tag,
"MASTER_NORM_FLAT_LSS" ) == 0 ||
303 strcmp(tag,
"MASTER_NORM_FLAT_LONG_MOS" ) == 0 ||
304 strcmp(tag,
"SLIT_LOCATION_MOS" ) == 0 ||
305 strcmp(tag,
"SLIT_LOCATION_MXU" ) == 0 ||
306 strcmp(tag,
"SLIT_LOCATION_LSS" ) == 0 ||
307 strcmp(tag,
"SLIT_LOCATION_LONG_MOS" ) == 0 ||
308 strcmp(tag,
"CURV_COEFF_MOS" ) == 0 ||
309 strcmp(tag,
"CURV_COEFF_MXU" ) == 0 ||
310 strcmp(tag,
"CURV_COEFF_LSS" ) == 0 ||
311 strcmp(tag,
"DISP_COEFF_MOS" ) == 0 ||
312 strcmp(tag,
"DISP_COEFF_MXU" ) == 0 ||
313 strcmp(tag,
"DISP_COEFF_LSS" ) == 0 ||
314 strcmp(tag,
"DISP_COEFF_LONG_MOS" ) == 0 ||
315 strcmp(tag,
"FLAT_SED_MOS" ) == 0 ||
316 strcmp(tag,
"FLAT_SED_MXU" ) == 0 ||
317 strcmp(tag,
"FLAT_SED_LSS" ) == 0 ||
318 strcmp(tag,
"FLAT_SED_LONG_MOS" ) == 0 ||
320 strcmp(tag, FLX_STD_IMG ) == 0 ||
321 strcmp(tag,
"EXTINCT_TABLE" ) == 0 ||
322 strcmp(tag,
"MASTER_LINECAT" ) == 0 ||
323 strcmp(tag,
"MASTER_DISTORTION_TABLE" ) == 0 ||
324 strcmp(tag,
"GLOBAL_DISTORTION_TABLE" ) == 0 ||
325 strcmp(tag,
"RETARDER_WAVEPLATE_CHROMATISM") == 0 ||
326 strcmp(tag,
"GRISM_TABLE" ) == 0 ||
327 strcmp(tag,
"STD_PMOS_TABLE" ) == 0 ||
328 strcmp(tag,
"TELLURIC_CONTAMINATION" ) == 0 ||
329 strcmp(tag,
"STD_FLUX_TABLE" ) == 0 ||
330 strcmp(tag,
"SPECPHOT_TABLE" ) == 0 ||
331 strcmp(tag, PHOT_TABLE ) == 0) {
332 cpl_frame_set_group(f, CPL_FRAME_GROUP_CALIB);
335 cpl_msg_warning(cpl_func,
"Unrecognized frame tag: '%s'",
358 const char **instrument_version)
360 const char *instrume = NULL;
362 instrume = cpl_propertylist_get_string(header,
363 FORS_PFITS_INSTRUME);
364 assure( !cpl_error_get_code(),
return NULL,
365 "Missing keyword %s in input header", FORS_PFITS_INSTRUME);
367 assure( strlen(instrume) >= 5,
return NULL,
368 "%s keyword must be 'fors1' or 'fors2', not '%s'",
369 FORS_PFITS_INSTRUME, instrume);
371 assure( instrume[4] ==
'1' || instrume[4] ==
'2',
return NULL,
372 "Unrecognized %s: %s", FORS_PFITS_INSTRUME, instrume);
374 if (instrument_version != NULL) {
375 *instrument_version = cpl_sprintf(
"%s", instrume);
378 return cpl_sprintf(
"fors%c/%s", instrume[4], PACKAGE_VERSION);
405 const cpl_table *defaults)
407 const char *func =
"dfs_get_parameter_int";
410 cpl_parameter *param;
413 if (parlist == NULL) {
414 cpl_msg_error(func,
"Missing input parameter list");
415 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
420 cpl_msg_error(func,
"Missing input parameter name");
421 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
425 param = cpl_parameterlist_find(parlist, name);
428 cpl_msg_error(func,
"Wrong parameter name: %s", name);
429 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
433 if (cpl_parameter_get_type(param) != CPL_TYPE_INT) {
434 cpl_msg_error(func,
"Unexpected type for parameter "
435 "\"%s\": it should be integer", name);
436 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
440 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
444 cpl_parameter_get_default_int(param) == cpl_parameter_get_int(param)) {
446 if (cpl_table_has_column(defaults, alias)) {
447 if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_INT) {
448 cpl_msg_error(func,
"Unexpected type for GRISM_TABLE "
449 "column \"%s\": it should be integer", alias);
450 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
453 if (cpl_table_is_valid(defaults, alias, 0)) {
454 cpl_parameter_set_int(param, cpl_table_get_int(defaults,
458 cpl_msg_error(func,
"Invalid parameter value in table "
459 "column \"%s\"", alias);
460 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
465 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
466 "- using recipe default", alias);
470 cpl_msg_info(func,
"%s:", alias);
471 cpl_msg_info(func,
"%s: %d",
472 cpl_parameter_get_help(param), cpl_parameter_get_int(param));
474 return cpl_parameter_get_int(param);
502 const char *name,
const cpl_table *defaults)
504 const char *func =
"dfs_get_parameter_double";
507 cpl_parameter *param;
510 if (parlist == NULL) {
511 cpl_msg_error(func,
"Missing input parameter list");
512 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
517 cpl_msg_error(func,
"Missing input parameter name");
518 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
522 param = cpl_parameterlist_find(parlist, name);
525 cpl_msg_error(func,
"Wrong parameter name: %s", name);
526 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
530 if (cpl_parameter_get_type(param) != CPL_TYPE_DOUBLE) {
531 cpl_msg_error(func,
"Unexpected type for parameter "
532 "\"%s\": it should be double", name);
533 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
537 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
541 cpl_parameter_get_default_double(param) ==
542 cpl_parameter_get_double(param)) {
544 if (cpl_table_has_column(defaults, alias)) {
545 if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_DOUBLE) {
546 cpl_msg_error(func,
"Unexpected type for GRISM_TABL "
547 "column \"%s\": it should be double", alias);
548 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
551 if (cpl_table_is_valid(defaults, alias, 0)) {
552 cpl_parameter_set_double(param, cpl_table_get_double(defaults,
556 cpl_msg_error(func,
"Invalid parameter value in table "
557 "column \"%s\"", alias);
558 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
563 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
564 "- using recipe default", alias);
568 cpl_msg_info(func,
"%s:", alias);
569 cpl_msg_info(func,
"%s: %f",
570 cpl_parameter_get_help(param), cpl_parameter_get_double(param));
572 return cpl_parameter_get_double(param);
601 const cpl_table *defaults)
603 const char *func =
"dfs_get_parameter_string";
606 cpl_parameter *param;
609 if (parlist == NULL) {
610 cpl_msg_error(func,
"Missing input parameter list");
611 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
616 cpl_msg_error(func,
"Missing input parameter name");
617 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
621 param = cpl_parameterlist_find(parlist, name);
624 cpl_msg_error(func,
"Wrong parameter name: %s", name);
625 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
629 if (cpl_parameter_get_type(param) != CPL_TYPE_STRING) {
630 cpl_msg_error(func,
"Unexpected type for parameter "
631 "\"%s\": it should be string", name);
632 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
636 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
640 strcmp(cpl_parameter_get_default_string(param),
641 cpl_parameter_get_string(param)) == 0) {
643 if (cpl_table_has_column(defaults, alias)) {
644 if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_STRING) {
645 cpl_msg_error(func,
"Unexpected type for GRISM_TABLE "
646 "column \"%s\": it should be string", alias);
647 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
650 if (cpl_table_is_valid(defaults, alias, 0)) {
651 cpl_parameter_set_string(param, cpl_table_get_string(defaults,
655 cpl_msg_error(func,
"Invalid parameter value in table "
656 "column \"%s\"", alias);
657 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
662 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
663 "- using recipe default", alias);
667 cpl_msg_info(func,
"%s:", alias);
668 cpl_msg_info(func,
"%s: %s", cpl_parameter_get_help(param),
669 cpl_parameter_get_string(param));
671 return cpl_parameter_get_string(param);
699 const cpl_table *defaults)
701 const char *func =
"dfs_get_parameter_bool";
704 cpl_parameter *param;
708 if (parlist == NULL) {
709 cpl_msg_error(func,
"Missing input parameter list");
710 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
715 cpl_msg_error(func,
"Missing input parameter name");
716 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
720 param = cpl_parameterlist_find(parlist, name);
723 cpl_msg_error(func,
"Wrong parameter name: %s", name);
724 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
728 if (cpl_parameter_get_type(param) != CPL_TYPE_BOOL) {
729 cpl_msg_error(func,
"Unexpected type for parameter "
730 "\"%s\": it should be boolean", name);
731 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
735 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
739 cpl_parameter_get_default_bool(param) ==
740 cpl_parameter_get_bool(param)) {
742 if (cpl_table_has_column(defaults, alias)) {
743 if (cpl_table_get_column_type(defaults, alias) != CPL_TYPE_INT) {
744 cpl_msg_error(func,
"Unexpected type for GRISM_TABLE "
745 "column \"%s\": it should be integer", alias);
746 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
749 if (cpl_table_is_valid(defaults, alias, 0)) {
750 value = cpl_table_get_int(defaults, alias, 0, NULL);
751 if (value < 0 || value > 1) {
752 cpl_msg_error(func,
"Illegal parameter value in table "
753 "column \"%s\": it should be either 0 or 1",
755 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
758 cpl_parameter_set_bool(param, value);
761 cpl_msg_error(func,
"Invalid parameter value in table "
762 "column \"%s\"", alias);
763 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
768 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
769 "- using recipe default", alias);
773 value = cpl_parameter_get_bool(param);
776 cpl_msg_info(func,
"%s:", alias);
777 cpl_msg_info(func,
"%s: TRUE", cpl_parameter_get_help(param));
780 cpl_msg_info(func,
"%s:", alias);
781 cpl_msg_info(func,
"%s: FALSE", cpl_parameter_get_help(param));
858 cpl_type type,
int ext,
int calib)
860 const char *func =
"dfs_load_image";
862 cpl_frame *frame = NULL;
863 cpl_image *image = NULL;
866 frame = cpl_frameset_find(frameset, category);
869 image = cpl_image_load(cpl_frame_get_filename(frame), type, 0, ext);
871 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
872 cpl_msg_error(func,
"Cannot load image %s",
873 cpl_frame_get_filename(frame));
877 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB);
879 cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
915 const char *func =
"dfs_load_table";
917 cpl_frame *frame = NULL;
918 cpl_table *table = NULL;
921 frame = cpl_frameset_find(frameset, category);
924 table = cpl_table_load(cpl_frame_get_filename(frame), ext, 1);
926 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
927 cpl_msg_error(func,
"Cannot load table %s",
928 cpl_frame_get_filename(frame));
962 const char *category,
int ext)
964 const char *func =
"dfs_load_header";
966 cpl_frame *frame = NULL;
967 cpl_propertylist *plist = NULL;
970 frame = cpl_frameset_find(frameset, category);
973 plist = cpl_propertylist_load(cpl_frame_get_filename(frame), ext);
975 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
976 cpl_msg_error(func,
"Cannot load header from %s",
977 cpl_frame_get_filename(frame));
993 dfs_save(cpl_frameset *frameset,
const void *
object, fors_type type,
994 const char *category, cpl_propertylist *header,
995 const cpl_parameterlist *parlist,
const char *recipename,
996 const cpl_frame *inherit_frame)
1000 cpl_propertylist *plist;
1001 const char *version = NULL;
1002 cpl_propertylist *raw_header = NULL;
1005 if (category == NULL || frameset == NULL ||
object == NULL ||
1006 inherit_frame == NULL) {
1007 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
1008 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
1012 if (type == FORS_TYPE_TABLE) {
1013 cpl_msg_debug(cpl_func,
"Saving %s table to disk...", category);
1016 cpl_msg_debug(cpl_func,
"Saving %s image to disk...", category);
1021 const char *raw_filename =
1022 cpl_frame_get_filename(inherit_frame);
1024 raw_header = cpl_propertylist_load(raw_filename, 0);
1025 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1026 cpl_msg_error(cpl_func,
"Could not read %s primary header", raw_filename);
1031 cpl_propertylist_delete(raw_header);
1033 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1034 cpl_msg_error(cpl_func,
"Could not identify instrument version from %s header",
1040 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1042 strlower(strcpy(filename, category));
1043 strcat(filename,
".fits");
1045 frame = cpl_frame_new();
1047 cpl_frame_set_filename(frame, filename);
1048 cpl_frame_set_tag(frame, category);
1049 cpl_frame_set_type(frame, CPL_FRAME_TYPE_ANY);
1050 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1051 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1052 if (cpl_error_get_code()) {
1053 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
1054 cpl_msg_error(cpl_func,
"Cannot initialise the product frame");
1055 cpl_frame_delete(frame);
1057 cpl_free((
void *)version);
1067 plist = cpl_propertylist_new();
1071 if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1072 recipename, version,
"PRO-1.15",
1074 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1075 cpl_error_get_where(), cpl_error_get_message());
1076 cpl_msg_error(cpl_func,
"Problem with product %s FITS header definition",
1079 cpl_propertylist_delete(plist);
1080 cpl_frame_delete(frame);
1082 cpl_free((
void *)version);
1086 cpl_free((
void *)version);
1092 if (type == FORS_TYPE_IMAGE_ERR) {
1095 else if (type == FORS_TYPE_IMAGE) {
1096 cpl_image_save((cpl_image *)
object, filename, CPL_BPP_IEEE_FLOAT, plist,
1100 cpl_table_save((cpl_table *)
object,
1101 plist, NULL, filename, CPL_IO_DEFAULT);
1104 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1105 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1106 cpl_error_get_where(), cpl_error_get_message());
1107 cpl_msg_error(cpl_func,
"Cannot save product %s to disk", filename);
1109 cpl_propertylist_delete(plist);
1110 cpl_frame_delete(frame);
1116 cpl_propertylist_delete(plist);
1120 cpl_frameset_insert(frameset, frame);
1149 const char *category, cpl_propertylist *header,
1150 const cpl_parameterlist *parlist,
const char *recipename,
1151 const cpl_frame *inherit_frame)
1153 dfs_save(frameset, image, FORS_TYPE_IMAGE,
1155 parlist, recipename,
1184 const cpl_image *mask,
const char *category,
1185 cpl_propertylist *header,
1186 const cpl_parameterlist *parlist,
const char *recipename,
1187 const cpl_frame *inherit_frame)
1190 cpl_propertylist * extension_header;
1192 dfs_save(frameset, image, FORS_TYPE_IMAGE,
1194 parlist, recipename,
1197 extension_header = cpl_propertylist_new();
1198 cpl_propertylist_append_string(extension_header,
1199 "EXTNAME",
"IMAGE.BPM");
1201 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1203 strlower(strcpy(filename, category));
1204 strcat(filename,
".fits");
1206 cpl_image_save(mask, filename, CPL_BPP_IEEE_FLOAT, extension_header,
1208 cpl_propertylist_delete(extension_header);
1237 const char *category, cpl_propertylist *header,
1238 const cpl_parameterlist *parlist,
const char *recipename,
1239 const cpl_frame *inherit_frame)
1241 dfs_save(frameset, image, FORS_TYPE_IMAGE_ERR,
1243 parlist, recipename,
1273 const cpl_image *mask,
const char *category,
1274 cpl_propertylist *header,
1275 const cpl_parameterlist *parlist,
const char *recipename,
1276 const cpl_frame *inherit_frame)
1279 cpl_propertylist * extension_header;
1281 dfs_save(frameset, image, FORS_TYPE_IMAGE_ERR,
1283 parlist, recipename,
1286 extension_header = cpl_propertylist_new();
1287 cpl_propertylist_append_string(extension_header,
1288 "EXTNAME",
"IMAGE.BPM");
1290 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1292 strlower(strcpy(filename, category));
1293 strcat(filename,
".fits");
1295 cpl_image_save(mask, filename, CPL_BPP_IEEE_FLOAT, extension_header,
1297 cpl_propertylist_delete(extension_header);
1304 cpl_propertylist_delete(wcs_header); \
1314 bool invert =
false;
1317 cpl_propertylist *wcs_header =
1318 cpl_propertylist_load_regexp(cpl_frame_get_filename(frame),
1319 extension, WCS_KEYS, invert);
1321 cpl_propertylist_copy_property_regexp(header, wcs_header,
".*", invert);
1323 double crpix1 = cpl_propertylist_get_double(header, FORS_PFITS_CRPIX1);
1325 assure( !cpl_error_get_code(),
return,
1326 "Could not read %s from %s", FORS_PFITS_CRPIX1,
1327 cpl_frame_get_filename(frame));
1329 double crpix2 = cpl_propertylist_get_double(header, FORS_PFITS_CRPIX2);
1331 assure( !cpl_error_get_code(),
return,
1332 "Could not read %s from %s", FORS_PFITS_CRPIX2,
1333 cpl_frame_get_filename(frame));
1335 cpl_propertylist_update_double(header, FORS_PFITS_CRPIX1,
1336 crpix1 - setting->prescan_x);
1338 cpl_propertylist_update_double(header, FORS_PFITS_CRPIX2,
1339 crpix2 - setting->prescan_y);
1350 cpl_propertylist_delete(time_header); \
1368 bool invert =
false;
1371 cpl_propertylist *time_header = NULL;
1376 cpl_propertylist_load_regexp(cpl_frame_get_filename(frame),
1377 extension,
"EXPTIME", invert);
1380 cpl_propertylist_copy_property_regexp(header,
1381 time_header,
".*", invert);
1388 while (cpl_propertylist_erase(header,
"EXPTIME"));
1389 cpl_propertylist_update_double(header,
"EXPTIME", exptime);
1406 const char *category, cpl_propertylist *header,
1407 const cpl_parameterlist *parlist,
const char *recipename,
1408 const cpl_frame *inherit_frame)
1410 dfs_save(frameset, table, FORS_TYPE_TABLE,
1412 parlist, recipename,
1450 const char *category, cpl_propertylist *header,
1451 const cpl_parameterlist *parlist,
const char *recipename,
1452 const char *version)
1454 const char *func =
"dfs_save_image";
1458 cpl_propertylist *plist;
1461 if (category == NULL || frameset == NULL || image == NULL) {
1462 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1463 cpl_error_get_where(), cpl_error_get_message());
1464 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1468 cpl_msg_info(func,
"Saving %s image to disk...", category);
1470 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1472 strlower(strcpy(filename, category));
1473 strcat(filename,
".fits");
1475 frame = cpl_frame_new();
1477 cpl_frame_set_filename(frame, filename);
1478 cpl_frame_set_tag(frame, category);
1479 cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
1480 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1481 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1482 if (cpl_error_get_code()) {
1483 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1484 cpl_error_get_where(), cpl_error_get_message());
1485 cpl_msg_error(func,
"Cannot initialise the product frame");
1486 cpl_frame_delete(frame);
1497 plist = cpl_propertylist_new();
1501 if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1502 recipename, version,
"PRO-1.15", NULL)) {
1503 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1504 cpl_error_get_where(), cpl_error_get_message());
1505 cpl_msg_error(func,
"Problem with product %s FITS header definition",
1508 cpl_propertylist_delete(plist);
1509 cpl_frame_delete(frame);
1518 if (cpl_image_save(image, filename, CPL_BPP_IEEE_FLOAT, plist,
1520 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1521 cpl_error_get_where(), cpl_error_get_message());
1522 cpl_msg_error(func,
"Cannot save product %s to disk", filename);
1524 cpl_propertylist_delete(plist);
1525 cpl_frame_delete(frame);
1531 cpl_propertylist_delete(plist);
1535 cpl_frameset_insert(frameset, frame);
1574 const char *category, cpl_propertylist *header,
1575 const cpl_parameterlist *parlist,
const char *recipename,
1576 const char *version)
1578 const char *func =
"dfs_save_table";
1582 cpl_propertylist *plist;
1585 if (category == NULL || frameset == NULL || table == NULL) {
1586 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1587 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1588 cpl_error_get_where(), cpl_error_get_message());
1592 cpl_msg_info(func,
"Saving %s table to disk...", category);
1594 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1596 strlower(strcpy(filename, category));
1598 strcat(filename,
".fits");
1600 frame = cpl_frame_new();
1602 cpl_frame_set_filename(frame, filename);
1603 cpl_frame_set_tag(frame, category);
1604 cpl_frame_set_type(frame, CPL_FRAME_TYPE_TABLE);
1605 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1606 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1607 if (cpl_error_get_code()) {
1608 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1609 cpl_error_get_where(), cpl_error_get_message());
1610 cpl_msg_error(func,
"Cannot initialise the product frame");
1611 cpl_frame_delete(frame);
1622 plist = cpl_propertylist_new();
1626 if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1627 recipename, version,
"PRO-1.15", NULL)) {
1628 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1629 cpl_error_get_where(), cpl_error_get_message());
1630 cpl_msg_error(func,
"Problem with product %s FITS header definition",
1633 cpl_propertylist_delete(plist);
1634 cpl_frame_delete(frame);
1643 if (cpl_table_save(table, plist, NULL, filename, CPL_IO_DEFAULT)) {
1644 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1645 cpl_error_get_where(), cpl_error_get_message());
1646 cpl_msg_error(func,
"Cannot save product %s to disk", filename);
1648 cpl_propertylist_delete(plist);
1649 cpl_frame_delete(frame);
1655 cpl_propertylist_delete(plist);
1658 cpl_frameset_insert(frameset, frame);
1683 const char *func =
"dfs_equal_keyword";
1686 cpl_propertylist *reference;
1689 const char *rstring;
1696 if (frameset == NULL || keyword == NULL) {
1697 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1701 if (cpl_frameset_is_empty(frameset)) {
1702 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
1706 frame = cpl_frameset_get_first(frameset);
1712 reference = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
1713 if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
1715 frame = cpl_frameset_get_next(frameset);
1719 if (cpl_propertylist_has(reference, keyword)) {
1720 rtype = cpl_propertylist_get_type(reference, keyword);
1722 if (rtype == CPL_TYPE_STRING) {
1724 rstring = cpl_strdup(cpl_propertylist_get_string(reference,
1726 cpl_propertylist_delete(reference);
1730 if (rtype == CPL_TYPE_INT) {
1732 rintero = cpl_propertylist_get_int(reference, keyword);
1733 cpl_propertylist_delete(reference);
1737 cpl_propertylist_delete(reference);
1741 cpl_propertylist_delete(reference);
1743 frame = cpl_frameset_get_next(frameset);
1750 frame = cpl_frameset_get_first(frameset);
1754 reference = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
1755 if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
1757 frame = cpl_frameset_get_next(frameset);
1761 if (cpl_propertylist_has(reference, keyword)) {
1763 type = cpl_propertylist_get_type(reference, keyword);
1765 if (rtype != type) {
1766 cpl_propertylist_delete(reference);
1770 if (rtype == CPL_TYPE_STRING) {
1771 string = cpl_propertylist_get_string(reference,
1773 if (strncmp(rstring,
string, 15)) {
1774 cpl_propertylist_delete(reference);
1779 if (rtype == CPL_TYPE_INT) {
1780 intero = cpl_propertylist_get_int(reference, keyword);
1781 if (rintero - intero) {
1782 cpl_propertylist_delete(reference);
1788 cpl_propertylist_delete(reference);
1790 frame = cpl_frameset_get_next(frameset);
1793 if (rtype == CPL_TYPE_STRING)
1794 cpl_free((
void *)rstring);
1811 cpl_propertylist * extheader)
1813 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1814 cpl_propertylist * header;
1817 header = cpl_propertylist_duplicate(extheader);
1819 cpl_propertylist_erase_regexp(header,
1820 "^ESO DPR |^ARCFILE$|^ORIGFILE$", 0);
1825 strlower(strcpy(filename, tag));
1826 strcat(filename,
".fits");
1828 if (cpl_table_save(table, NULL, header, filename, CPL_IO_EXTEND)) {
1830 cpl_ensure_code(0, CPL_ERROR_FILE_IO);
1833 cpl_propertylist_delete(header);
1836 return CPL_ERROR_NONE;
1850 cpl_propertylist * extheader)
1852 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1854 cpl_propertylist * header;
1857 header = cpl_propertylist_duplicate(extheader);
1859 cpl_propertylist_erase_regexp(header,
1860 "^ESO DPR |^ARCFILE$|^ORIGFILE$", 0);
1865 strlower(strcpy(filename, tag));
1866 strcat(filename,
".fits");
1868 if (cpl_image_save(image, filename, CPL_BPP_IEEE_FLOAT,
1869 header, CPL_IO_EXTEND)) {
1871 cpl_ensure_code(0, CPL_ERROR_FILE_IO);
1874 cpl_propertylist_delete(header);
1877 return CPL_ERROR_NONE;
1892 cpl_parameterlist * parlist,
1894 const char * recipename,
1895 const char * version)
1898 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1900 cpl_error_code error;
1902 cpl_propertylist * pro = cpl_propertylist_new();
1904 cpl_propertylist_append_string(pro,
"ESO PRO CATG", tag);
1906 strlower(strcpy(filename, tag));
1907 strcat(filename,
".fits");
1909 error = cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, NULL,
1910 CPL_BPP_IEEE_FLOAT, recipename, pro,
1911 NULL, version, filename);
1914 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, 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)
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.
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)
void fors_image_save(const fors_image *image, const cpl_propertylist *header, const char *filename)
Save image.
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
double dfs_get_parameter_double_const(const cpl_parameterlist *parlist, const char *name)