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:", alias);
474 cpl_msg_info(func,
"%s: %d",
475 cpl_parameter_get_help(param), cpl_parameter_get_int(param));
477 return cpl_parameter_get_int(param);
505 const char *name,
const cpl_table *defaults)
507 const char *func =
"dfs_get_parameter_double";
510 cpl_parameter *param;
513 if (parlist == NULL) {
514 cpl_msg_error(func,
"Missing input parameter list");
515 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
520 cpl_msg_error(func,
"Missing input parameter name");
521 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
525 param = cpl_parameterlist_find(parlist, name);
528 cpl_msg_error(func,
"Wrong parameter name: %s", name);
529 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
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);
540 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
544 cpl_parameter_get_default_double(param) ==
545 cpl_parameter_get_double(param)) {
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);
554 if (cpl_table_is_valid(defaults, alias, 0)) {
555 cpl_parameter_set_double(param, cpl_table_get_double(defaults,
559 cpl_msg_error(func,
"Invalid parameter value in table "
560 "column \"%s\"", alias);
561 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
566 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
567 "- using recipe default", alias);
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));
575 return cpl_parameter_get_double(param);
604 const cpl_table *defaults)
606 const char *func =
"dfs_get_parameter_string";
609 cpl_parameter *param;
612 if (parlist == NULL) {
613 cpl_msg_error(func,
"Missing input parameter list");
614 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
619 cpl_msg_error(func,
"Missing input parameter name");
620 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
624 param = cpl_parameterlist_find(parlist, name);
627 cpl_msg_error(func,
"Wrong parameter name: %s", name);
628 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
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);
639 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
643 strcmp(cpl_parameter_get_default_string(param),
644 cpl_parameter_get_string(param)) == 0) {
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);
653 if (cpl_table_is_valid(defaults, alias, 0)) {
654 cpl_parameter_set_string(param, cpl_table_get_string(defaults,
658 cpl_msg_error(func,
"Invalid parameter value in table "
659 "column \"%s\"", alias);
660 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
665 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
666 "- using recipe default", alias);
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));
674 return cpl_parameter_get_string(param);
702 const cpl_table *defaults)
704 const char *func =
"dfs_get_parameter_bool";
707 cpl_parameter *param;
711 if (parlist == NULL) {
712 cpl_msg_error(func,
"Missing input parameter list");
713 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
718 cpl_msg_error(func,
"Missing input parameter name");
719 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
723 param = cpl_parameterlist_find(parlist, name);
726 cpl_msg_error(func,
"Wrong parameter name: %s", name);
727 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
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);
738 alias = cpl_parameter_get_alias(param, CPL_PARAMETER_MODE_CLI);
742 cpl_parameter_get_default_bool(param) ==
743 cpl_parameter_get_bool(param)) {
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);
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",
758 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
761 cpl_parameter_set_bool(param, value);
764 cpl_msg_error(func,
"Invalid parameter value in table "
765 "column \"%s\"", alias);
766 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
771 cpl_msg_warning(func,
"Parameter \"%s\" not found in GRISM_TABLE "
772 "- using recipe default", alias);
776 value = cpl_parameter_get_bool(param);
779 cpl_msg_info(func,
"%s:", alias);
780 cpl_msg_info(func,
"%s: TRUE", cpl_parameter_get_help(param));
783 cpl_msg_info(func,
"%s:", alias);
784 cpl_msg_info(func,
"%s: FALSE", cpl_parameter_get_help(param));
861 cpl_type type,
int ext,
int calib)
863 const char *func =
"dfs_load_image";
865 cpl_frame *frame = NULL;
866 cpl_image *image = NULL;
869 frame = cpl_frameset_find(frameset, category);
872 image = cpl_image_load(cpl_frame_get_filename(frame), type, 0, ext);
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));
880 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB);
882 cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW);
918 const char *func =
"dfs_load_table";
920 cpl_frame *frame = NULL;
921 cpl_table *table = NULL;
924 frame = cpl_frameset_find(frameset, category);
927 table = cpl_table_load(cpl_frame_get_filename(frame), ext, 1);
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));
965 const char *category,
int ext)
967 const char *func =
"dfs_load_header";
969 cpl_frame *frame = NULL;
970 cpl_propertylist *plist = NULL;
973 frame = cpl_frameset_find(frameset, category);
976 plist = cpl_propertylist_load(cpl_frame_get_filename(frame), ext);
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));
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)
1003 cpl_propertylist *plist;
1004 const char *version = NULL;
1005 cpl_propertylist *raw_header = NULL;
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);
1015 if (type == FORS_TYPE_TABLE) {
1016 cpl_msg_debug(cpl_func,
"Saving %s table to disk...", category);
1019 cpl_msg_debug(cpl_func,
"Saving %s image to disk...", category);
1024 const char *raw_filename =
1025 cpl_frame_get_filename(inherit_frame);
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);
1034 cpl_propertylist_delete(raw_header);
1036 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1037 cpl_msg_error(cpl_func,
"Could not identify instrument version from %s header",
1043 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1045 strlower(strcpy(filename, category));
1046 strcat(filename,
".fits");
1048 frame = cpl_frame_new();
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);
1060 cpl_free((
void *)version);
1070 plist = cpl_propertylist_new();
1074 if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1075 recipename, version,
"PRO-1.15",
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",
1082 cpl_propertylist_delete(plist);
1083 cpl_frame_delete(frame);
1085 cpl_free((
void *)version);
1089 cpl_free((
void *)version);
1095 if (type == FORS_TYPE_IMAGE_ERR) {
1098 else if (type == FORS_TYPE_IMAGE) {
1099 cpl_image_save((cpl_image *)
object, filename, CPL_BPP_IEEE_FLOAT, plist,
1103 cpl_table_save((cpl_table *)
object,
1104 plist, NULL, filename, CPL_IO_DEFAULT);
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);
1112 cpl_propertylist_delete(plist);
1113 cpl_frame_delete(frame);
1119 cpl_propertylist_delete(plist);
1123 cpl_frameset_insert(frameset, frame);
1152 const char *category, cpl_propertylist *header,
1153 const cpl_parameterlist *parlist,
const char *recipename,
1154 const cpl_frame *inherit_frame)
1156 dfs_save(frameset, image, FORS_TYPE_IMAGE,
1158 parlist, recipename,
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)
1193 cpl_propertylist * extension_header;
1195 dfs_save(frameset, image, FORS_TYPE_IMAGE,
1197 parlist, recipename,
1200 extension_header = cpl_propertylist_new();
1201 cpl_propertylist_append_string(extension_header,
1202 "EXTNAME",
"IMAGE.BPM");
1204 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1206 strlower(strcpy(filename, category));
1207 strcat(filename,
".fits");
1209 cpl_image_save(mask, filename, CPL_BPP_IEEE_FLOAT, extension_header,
1211 cpl_propertylist_delete(extension_header);
1240 const char *category, cpl_propertylist *header,
1241 const cpl_parameterlist *parlist,
const char *recipename,
1242 const cpl_frame *inherit_frame)
1244 dfs_save(frameset, image, FORS_TYPE_IMAGE_ERR,
1246 parlist, recipename,
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)
1282 cpl_propertylist * extension_header;
1284 dfs_save(frameset, image, FORS_TYPE_IMAGE_ERR,
1286 parlist, recipename,
1289 extension_header = cpl_propertylist_new();
1290 cpl_propertylist_append_string(extension_header,
1291 "EXTNAME",
"IMAGE.BPM");
1293 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1295 strlower(strcpy(filename, category));
1296 strcat(filename,
".fits");
1298 cpl_image_save(mask, filename, CPL_BPP_IEEE_FLOAT, extension_header,
1300 cpl_propertylist_delete(extension_header);
1307 cpl_propertylist_delete(wcs_header); \
1317 bool invert =
false;
1320 cpl_propertylist *wcs_header =
1321 cpl_propertylist_load_regexp(cpl_frame_get_filename(frame),
1322 extension, WCS_KEYS, invert);
1324 cpl_propertylist_copy_property_regexp(header, wcs_header,
".*", invert);
1326 double crpix1 = cpl_propertylist_get_double(header, FORS_PFITS_CRPIX1);
1328 assure( !cpl_error_get_code(),
return,
1329 "Could not read %s from %s", FORS_PFITS_CRPIX1,
1330 cpl_frame_get_filename(frame));
1332 double crpix2 = cpl_propertylist_get_double(header, FORS_PFITS_CRPIX2);
1334 assure( !cpl_error_get_code(),
return,
1335 "Could not read %s from %s", FORS_PFITS_CRPIX2,
1336 cpl_frame_get_filename(frame));
1338 cpl_propertylist_update_double(header, FORS_PFITS_CRPIX1,
1339 crpix1 - setting->prescan_x);
1341 cpl_propertylist_update_double(header, FORS_PFITS_CRPIX2,
1342 crpix2 - setting->prescan_y);
1353 cpl_propertylist_delete(time_header); \
1371 bool invert =
false;
1374 cpl_propertylist *time_header = NULL;
1379 cpl_propertylist_load_regexp(cpl_frame_get_filename(frame),
1380 extension,
"EXPTIME", invert);
1383 cpl_propertylist_copy_property_regexp(header,
1384 time_header,
".*", invert);
1391 while (cpl_propertylist_erase(header,
"EXPTIME"));
1392 cpl_propertylist_update_double(header,
"EXPTIME", exptime);
1409 const char *category, cpl_propertylist *header,
1410 const cpl_parameterlist *parlist,
const char *recipename,
1411 const cpl_frame *inherit_frame)
1413 dfs_save(frameset, table, FORS_TYPE_TABLE,
1415 parlist, recipename,
1453 const char *category, cpl_propertylist *header,
1454 const cpl_parameterlist *parlist,
const char *recipename,
1455 const char *version)
1457 const char *func =
"dfs_save_image";
1461 cpl_propertylist *plist;
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);
1471 cpl_msg_info(func,
"Saving %s image to disk...", category);
1473 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1475 strlower(strcpy(filename, category));
1476 strcat(filename,
".fits");
1478 frame = cpl_frame_new();
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);
1500 plist = cpl_propertylist_new();
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",
1511 cpl_propertylist_delete(plist);
1512 cpl_frame_delete(frame);
1521 if (cpl_image_save(image, filename, CPL_BPP_IEEE_FLOAT, plist,
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);
1527 cpl_propertylist_delete(plist);
1528 cpl_frame_delete(frame);
1534 cpl_propertylist_delete(plist);
1538 cpl_frameset_insert(frameset, frame);
1577 const char *category, cpl_propertylist *header,
1578 const cpl_parameterlist *parlist,
const char *recipename,
1579 const char *version)
1581 const char *func =
"dfs_save_table";
1585 cpl_propertylist *plist;
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());
1595 cpl_msg_info(func,
"Saving %s table to disk...", category);
1597 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1599 strlower(strcpy(filename, category));
1601 strcat(filename,
".fits");
1603 frame = cpl_frame_new();
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);
1625 plist = cpl_propertylist_new();
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",
1636 cpl_propertylist_delete(plist);
1637 cpl_frame_delete(frame);
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);
1651 cpl_propertylist_delete(plist);
1652 cpl_frame_delete(frame);
1658 cpl_propertylist_delete(plist);
1661 cpl_frameset_insert(frameset, frame);
1686 const char *func =
"dfs_equal_keyword";
1689 cpl_propertylist *reference;
1692 const char *rstring;
1699 if (frameset == NULL || keyword == NULL) {
1700 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1704 if (cpl_frameset_is_empty(frameset)) {
1705 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
1709 frame = cpl_frameset_get_first(frameset);
1715 reference = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
1716 if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
1718 frame = cpl_frameset_get_next(frameset);
1722 if (cpl_propertylist_has(reference, keyword)) {
1723 rtype = cpl_propertylist_get_type(reference, keyword);
1725 if (rtype == CPL_TYPE_STRING) {
1727 rstring = cpl_strdup(cpl_propertylist_get_string(reference,
1729 cpl_propertylist_delete(reference);
1733 if (rtype == CPL_TYPE_INT) {
1735 rintero = cpl_propertylist_get_int(reference, keyword);
1736 cpl_propertylist_delete(reference);
1740 cpl_propertylist_delete(reference);
1744 cpl_propertylist_delete(reference);
1746 frame = cpl_frameset_get_next(frameset);
1753 frame = cpl_frameset_get_first(frameset);
1757 reference = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
1758 if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
1760 frame = cpl_frameset_get_next(frameset);
1764 if (cpl_propertylist_has(reference, keyword)) {
1766 type = cpl_propertylist_get_type(reference, keyword);
1768 if (rtype != type) {
1769 cpl_propertylist_delete(reference);
1773 if (rtype == CPL_TYPE_STRING) {
1774 string = cpl_propertylist_get_string(reference,
1776 if (strncmp(rstring,
string, 15)) {
1777 cpl_propertylist_delete(reference);
1782 if (rtype == CPL_TYPE_INT) {
1783 intero = cpl_propertylist_get_int(reference, keyword);
1784 if (rintero - intero) {
1785 cpl_propertylist_delete(reference);
1791 cpl_propertylist_delete(reference);
1793 frame = cpl_frameset_get_next(frameset);
1796 if (rtype == CPL_TYPE_STRING)
1797 cpl_free((
void *)rstring);
1814 cpl_propertylist * extheader)
1816 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1817 cpl_propertylist * header;
1820 header = cpl_propertylist_duplicate(extheader);
1822 cpl_propertylist_erase_regexp(header,
1823 "^ESO DPR |^ARCFILE$|^ORIGFILE$", 0);
1828 strlower(strcpy(filename, tag));
1829 strcat(filename,
".fits");
1831 if (cpl_table_save(table, NULL, header, filename, CPL_IO_EXTEND)) {
1833 cpl_ensure_code(0, CPL_ERROR_FILE_IO);
1836 cpl_propertylist_delete(header);
1839 return CPL_ERROR_NONE;
1853 cpl_propertylist * extheader)
1855 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1857 cpl_propertylist * header;
1860 header = cpl_propertylist_duplicate(extheader);
1862 cpl_propertylist_erase_regexp(header,
1863 "^ESO DPR |^ARCFILE$|^ORIGFILE$", 0);
1868 strlower(strcpy(filename, tag));
1869 strcat(filename,
".fits");
1871 if (cpl_image_save(image, filename, CPL_BPP_IEEE_FLOAT,
1872 header, CPL_IO_EXTEND)) {
1874 cpl_ensure_code(0, CPL_ERROR_FILE_IO);
1877 cpl_propertylist_delete(header);
1880 return CPL_ERROR_NONE;
1895 cpl_parameterlist * parlist,
1897 const char * recipename,
1898 const char * version)
1901 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1903 cpl_error_code error;
1905 cpl_propertylist * pro = cpl_propertylist_new();
1907 cpl_propertylist_append_string(pro,
"ESO PRO CATG", tag);
1909 strlower(strcpy(filename, tag));
1910 strcat(filename,
".fits");
1912 error = cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, NULL,
1913 CPL_BPP_IEEE_FLOAT, recipename, pro,
1914 NULL, version, filename);
1917 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)