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 cpl_propertylist *extra_header,
999 const cpl_parameterlist *parlist,
const char *recipename,
1000 const cpl_frame *inherit_frame)
1004 cpl_propertylist *plist;
1005 const char *version = NULL;
1006 cpl_propertylist *raw_header = NULL;
1009 if (category == NULL || frameset == NULL ||
object == NULL ||
1010 inherit_frame == NULL) {
1011 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
1012 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
1016 if (type == FORS_TYPE_TABLE) {
1017 cpl_msg_debug(cpl_func,
"Saving %s table to disk...", category);
1020 cpl_msg_debug(cpl_func,
"Saving %s image to disk...", category);
1025 const char *raw_filename =
1026 cpl_frame_get_filename(inherit_frame);
1028 raw_header = cpl_propertylist_load(raw_filename, 0);
1029 if (raw_header == NULL) {
1030 cpl_msg_error(cpl_func,
"Could not read %s primary header", raw_filename);
1035 cpl_propertylist_delete(raw_header);
1037 if (cpl_error_get_code() != CPL_ERROR_NONE) {
1038 cpl_msg_error(cpl_func,
"Could not identify instrument version from %s header",
1044 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1046 strlower(strcpy(filename, category));
1047 strcat(filename,
".fits");
1049 frame = cpl_frame_new();
1051 cpl_frame_set_filename(frame, filename);
1052 cpl_frame_set_tag(frame, category);
1053 cpl_frame_set_type(frame, CPL_FRAME_TYPE_ANY);
1054 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1055 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1056 if (cpl_error_get_code()) {
1057 cpl_msg_error(cpl_error_get_where(),
"%s", cpl_error_get_message());
1058 cpl_msg_error(cpl_func,
"Cannot initialise the product frame");
1059 cpl_frame_delete(frame);
1061 cpl_free((
void *)version);
1071 plist = cpl_propertylist_new();
1073 plist = cpl_propertylist_duplicate(header);
1075 if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1076 recipename, version,
"PRO-1.15",
1078 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1079 cpl_error_get_where(), cpl_error_get_message());
1080 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);
1111 cpl_propertylist_delete(plist);
1112 cpl_frame_delete(frame);
1117 cpl_propertylist_delete(plist);
1121 cpl_frameset_insert(frameset, frame);
1150 const char *category, cpl_propertylist *header,
1151 const cpl_parameterlist *parlist,
const char *recipename,
1152 const cpl_frame *inherit_frame)
1154 dfs_save(frameset, image, FORS_TYPE_IMAGE,
1155 category, header, NULL,
1156 parlist, recipename,
1185 const cpl_image *mask,
const char *category,
1186 cpl_propertylist *header,
1187 const cpl_parameterlist *parlist,
const char *recipename,
1188 const cpl_frame *inherit_frame)
1191 cpl_propertylist * extension_header;
1193 dfs_save(frameset, image, FORS_TYPE_IMAGE,
1194 category, header, NULL,
1195 parlist, recipename,
1198 extension_header = cpl_propertylist_new();
1199 cpl_propertylist_append_string(extension_header,
1200 "EXTNAME",
"IMAGE.BPM");
1202 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1204 strlower(strcpy(filename, category));
1205 strcat(filename,
".fits");
1207 cpl_image_save(mask, filename, CPL_BPP_IEEE_FLOAT, extension_header,
1209 cpl_propertylist_delete(extension_header);
1238 const char *category, cpl_propertylist *header,
1239 cpl_propertylist *err_header,
1240 const cpl_parameterlist *parlist,
const char *recipename,
1241 const cpl_frame *inherit_frame)
1243 dfs_save(frameset, image, FORS_TYPE_IMAGE_ERR,
1244 category, header, err_header,
1245 parlist, recipename,
1275 const cpl_image *mask,
const char *category,
1276 cpl_propertylist *header,
1277 const cpl_parameterlist *parlist,
const char *recipename,
1278 const cpl_frame *inherit_frame)
1281 cpl_propertylist * extension_header;
1283 dfs_save(frameset, image, FORS_TYPE_IMAGE_ERR,
1284 category, header, NULL,
1285 parlist, recipename,
1288 extension_header = cpl_propertylist_new();
1289 cpl_propertylist_append_string(extension_header,
1290 "EXTNAME",
"IMAGE.BPM");
1292 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1294 strlower(strcpy(filename, category));
1295 strcat(filename,
".fits");
1297 cpl_image_save(mask, filename, CPL_BPP_IEEE_FLOAT, extension_header,
1299 cpl_propertylist_delete(extension_header);
1306 cpl_propertylist_delete(wcs_header); \
1316 bool invert =
false;
1319 cpl_propertylist *wcs_header =
1320 cpl_propertylist_load_regexp(cpl_frame_get_filename(frame),
1321 extension, WCS_KEYS, invert);
1323 cpl_propertylist_copy_property_regexp(header, wcs_header,
".*", invert);
1325 double crpix1 = cpl_propertylist_get_double(header, FORS_PFITS_CRPIX1);
1327 assure( !cpl_error_get_code(),
return,
1328 "Could not read %s from %s", FORS_PFITS_CRPIX1,
1329 cpl_frame_get_filename(frame));
1331 double crpix2 = cpl_propertylist_get_double(header, FORS_PFITS_CRPIX2);
1333 assure( !cpl_error_get_code(),
return,
1334 "Could not read %s from %s", FORS_PFITS_CRPIX2,
1335 cpl_frame_get_filename(frame));
1337 cpl_propertylist_update_double(header, FORS_PFITS_CRPIX1,
1338 crpix1 - setting->prescan_x);
1340 cpl_propertylist_update_double(header, FORS_PFITS_CRPIX2,
1341 crpix2 - setting->prescan_y);
1352 cpl_propertylist_delete(time_header); \
1370 bool invert =
false;
1373 cpl_propertylist *time_header = NULL;
1378 cpl_propertylist_load_regexp(cpl_frame_get_filename(frame),
1379 extension,
"EXPTIME", invert);
1382 cpl_propertylist_copy_property_regexp(header,
1383 time_header,
".*", invert);
1390 while (cpl_propertylist_erase(header,
"EXPTIME"));
1391 cpl_propertylist_update_double(header,
"EXPTIME", exptime);
1408 const char *category, cpl_propertylist *header,
1409 const cpl_parameterlist *parlist,
const char *recipename,
1410 const cpl_frame *inherit_frame)
1412 dfs_save(frameset, table, FORS_TYPE_TABLE,
1413 category, header, NULL,
1414 parlist, recipename,
1452 const char *category, cpl_propertylist *header,
1453 const cpl_parameterlist *parlist,
const char *recipename,
1454 const char *version)
1456 const char *func =
"dfs_save_image";
1460 cpl_propertylist *plist;
1463 if (category == NULL || frameset == NULL || image == NULL) {
1464 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1465 cpl_error_get_where(), cpl_error_get_message());
1466 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1470 cpl_msg_info(func,
"Saving %s image to disk...", category);
1472 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1474 strlower(strcpy(filename, category));
1475 strcat(filename,
".fits");
1477 frame = cpl_frame_new();
1479 cpl_frame_set_filename(frame, filename);
1480 cpl_frame_set_tag(frame, category);
1481 cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE);
1482 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1483 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1484 if (cpl_error_get_code()) {
1485 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1486 cpl_error_get_where(), cpl_error_get_message());
1487 cpl_msg_error(func,
"Cannot initialise the product frame");
1488 cpl_frame_delete(frame);
1499 plist = cpl_propertylist_new();
1503 if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1504 recipename, version,
"PRO-1.15", NULL)) {
1505 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1506 cpl_error_get_where(), cpl_error_get_message());
1507 cpl_msg_error(func,
"Problem with product %s FITS header definition",
1510 cpl_propertylist_delete(plist);
1511 cpl_frame_delete(frame);
1520 if (cpl_image_save(image, filename, CPL_BPP_IEEE_FLOAT, plist,
1522 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1523 cpl_error_get_where(), cpl_error_get_message());
1524 cpl_msg_error(func,
"Cannot save product %s to disk", filename);
1526 cpl_propertylist_delete(plist);
1527 cpl_frame_delete(frame);
1533 cpl_propertylist_delete(plist);
1537 cpl_frameset_insert(frameset, frame);
1576 const char *category, cpl_propertylist *header,
1577 const cpl_parameterlist *parlist,
const char *recipename,
1578 const char *version)
1580 const char *func =
"dfs_save_table";
1584 cpl_propertylist *plist;
1587 if (category == NULL || frameset == NULL || table == NULL) {
1588 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1589 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1590 cpl_error_get_where(), cpl_error_get_message());
1594 cpl_msg_info(func,
"Saving %s table to disk...", category);
1596 filename = cpl_calloc(strlen(category) + 6,
sizeof(
char));
1598 strlower(strcpy(filename, category));
1600 strcat(filename,
".fits");
1602 frame = cpl_frame_new();
1604 cpl_frame_set_filename(frame, filename);
1605 cpl_frame_set_tag(frame, category);
1606 cpl_frame_set_type(frame, CPL_FRAME_TYPE_TABLE);
1607 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
1608 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
1609 if (cpl_error_get_code()) {
1610 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1611 cpl_error_get_where(), cpl_error_get_message());
1612 cpl_msg_error(func,
"Cannot initialise the product frame");
1613 cpl_frame_delete(frame);
1624 plist = cpl_propertylist_new();
1628 if (cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
1629 recipename, version,
"PRO-1.15", NULL)) {
1630 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1631 cpl_error_get_where(), cpl_error_get_message());
1632 cpl_msg_error(func,
"Problem with product %s FITS header definition",
1635 cpl_propertylist_delete(plist);
1636 cpl_frame_delete(frame);
1645 if (cpl_table_save(table, plist, NULL, filename, CPL_IO_DEFAULT)) {
1646 cpl_msg_error(cpl_func,
"Error found in %s: %s",
1647 cpl_error_get_where(), cpl_error_get_message());
1648 cpl_msg_error(func,
"Cannot save product %s to disk", filename);
1650 cpl_propertylist_delete(plist);
1651 cpl_frame_delete(frame);
1657 cpl_propertylist_delete(plist);
1660 cpl_frameset_insert(frameset, frame);
1685 const char *func =
"dfs_equal_keyword";
1688 cpl_propertylist *reference;
1691 const char *rstring;
1698 if (frameset == NULL || keyword == NULL) {
1699 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1703 if (cpl_frameset_is_empty(frameset)) {
1704 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
1708 frame = cpl_frameset_get_first(frameset);
1714 reference = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
1715 if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
1717 frame = cpl_frameset_get_next(frameset);
1721 if (cpl_propertylist_has(reference, keyword)) {
1722 rtype = cpl_propertylist_get_type(reference, keyword);
1724 if (rtype == CPL_TYPE_STRING) {
1726 rstring = cpl_strdup(cpl_propertylist_get_string(reference,
1728 cpl_propertylist_delete(reference);
1732 if (rtype == CPL_TYPE_INT) {
1734 rintero = cpl_propertylist_get_int(reference, keyword);
1735 cpl_propertylist_delete(reference);
1739 cpl_propertylist_delete(reference);
1743 cpl_propertylist_delete(reference);
1745 frame = cpl_frameset_get_next(frameset);
1752 frame = cpl_frameset_get_first(frameset);
1756 reference = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
1757 if (cpl_error_get_code() == CPL_ERROR_BAD_FILE_FORMAT) {
1759 frame = cpl_frameset_get_next(frameset);
1763 if (cpl_propertylist_has(reference, keyword)) {
1765 type = cpl_propertylist_get_type(reference, keyword);
1767 if (rtype != type) {
1768 cpl_propertylist_delete(reference);
1772 if (rtype == CPL_TYPE_STRING) {
1773 string = cpl_propertylist_get_string(reference,
1775 if (strncmp(rstring,
string, 15)) {
1776 cpl_propertylist_delete(reference);
1781 if (rtype == CPL_TYPE_INT) {
1782 intero = cpl_propertylist_get_int(reference, keyword);
1783 if (rintero - intero) {
1784 cpl_propertylist_delete(reference);
1790 cpl_propertylist_delete(reference);
1792 frame = cpl_frameset_get_next(frameset);
1795 if (rtype == CPL_TYPE_STRING)
1796 cpl_free((
void *)rstring);
1813 cpl_propertylist * extheader)
1815 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1816 cpl_propertylist * header;
1819 header = cpl_propertylist_duplicate(extheader);
1821 cpl_propertylist_erase_regexp(header,
1822 "^ESO DPR |^ARCFILE$|^ORIGFILE$", 0);
1827 strlower(strcpy(filename, tag));
1828 strcat(filename,
".fits");
1830 if (cpl_table_save(table, NULL, header, filename, CPL_IO_EXTEND)) {
1832 cpl_ensure_code(0, CPL_ERROR_FILE_IO);
1835 cpl_propertylist_delete(header);
1838 return CPL_ERROR_NONE;
1852 cpl_propertylist * extheader)
1854 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1856 cpl_propertylist * header;
1859 header = cpl_propertylist_duplicate(extheader);
1861 cpl_propertylist_erase_regexp(header,
1862 "^ESO DPR |^ARCFILE$|^ORIGFILE$", 0);
1867 strlower(strcpy(filename, tag));
1868 strcat(filename,
".fits");
1870 if (cpl_image_save(image, filename, CPL_BPP_IEEE_FLOAT,
1871 header, CPL_IO_EXTEND)) {
1873 cpl_ensure_code(0, CPL_ERROR_FILE_IO);
1876 cpl_propertylist_delete(header);
1879 return CPL_ERROR_NONE;
1894 cpl_parameterlist * parlist,
1896 const char * recipename,
1897 const char * version)
1900 char * filename = cpl_calloc(strlen(tag) + 6,
sizeof(
char));
1902 cpl_error_code error;
1904 cpl_propertylist * pro = cpl_propertylist_new();
1906 cpl_propertylist_append_string(pro,
"ESO PRO CATG", tag);
1908 strlower(strcpy(filename, tag));
1909 strcat(filename,
".fits");
1911 error = cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, NULL,
1912 CPL_BPP_IEEE_FLOAT, recipename, pro,
1913 NULL, version, filename);
1916 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.