47 #include <uves_utils.h>
48 #include <uves_wavecal_utils.h>
49 #include <uves_pfits.h>
50 #include <uves_dump.h>
51 #include <uves_qclog.h>
53 #include <uves_utils_wrappers.h>
54 #include <uves_error.h>
57 #include <irplib_utils.h>
61 #include <uves_time.h>
68 #define FITS_MAGIC_SZ 6
74 static polynomial *load_polynomial(
const char* filename,
int extension);
75 static char *int_to_string(
int i);
78 load_raw_image(
const char *filename,
79 cpl_type type,
bool flames,
bool blue,
80 cpl_image *raw_image[2],
84 static int uves_is_fits_file(
const char *filename);
92 int uves_check_rec_status(
const int val) {
93 if(cpl_error_get_code() != CPL_ERROR_NONE) {
115 static int uves_is_fits_file(
const char *filename)
121 if ((fp = fopen(filename,
"r"))==NULL) {
126 magic = cpl_calloc(FITS_MAGIC_SZ+1,
sizeof(
char)) ;
127 (void)fread(magic, 1, FITS_MAGIC_SZ, fp) ;
129 magic[FITS_MAGIC_SZ] = (char)0 ;
130 if (strstr(magic,
"SIMPLE")!=NULL)
150 uves_dfs_files_dont_exist(cpl_frameset *frameset)
152 const char *func =
"dfs_files_dont_exist";
156 if (frameset == NULL) {
157 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
161 if (cpl_frameset_is_empty(frameset)) {
165 frame = cpl_frameset_get_first(frameset);
168 if (access(cpl_frame_get_filename(frame), F_OK)) {
169 cpl_msg_error(func,
"File %s (%s) was not found",
170 cpl_frame_get_filename(frame),
171 cpl_frame_get_tag(frame));
172 cpl_error_set(func, CPL_ERROR_FILE_NOT_FOUND);
175 frame = cpl_frameset_get_next(frameset);
178 if (cpl_error_get_code())
198 uves_contains_frames_kind(cpl_frameset * sof,
204 cpl_frame* frame = NULL;
205 cpl_frame* frame_dup = NULL;
209 nsof = cpl_frameset_get_size(sof);
210 for (i=0 ; i<nsof ; i++) {
211 frame = cpl_frameset_get_frame(sof,i);
212 name= (
char*) cpl_frame_get_filename(frame);
213 if(uves_is_fits_file(name) == 1) {
215 if(cpl_frame_get_tag(frame) != NULL) {
217 tag= (
char*) cpl_frame_get_tag(frame);
219 if(strstr(tag,type) != NULL) {
221 frame_dup = cpl_frame_duplicate(frame);
222 cpl_frameset_insert(raw,frame_dup);
251 const char *regression_name,
255 cpl_polynomial *pol = NULL;
256 int N = strlen(regression_name);
257 const char *coeffi_name = NULL;
270 coeffi_name = cpl_sprintf(
"%sI", regression_name);
276 case 1: cind=
'1';
break;
277 case 2: cind=
'2';
break;
278 case 3: cind=
'3';
break;
279 case 4: cind=
'4';
break;
280 case 5: cind=
'5';
break;
281 case 6: cind=
'6';
break;
282 case 7: cind=
'7';
break;
283 case 8: cind=
'8';
break;
284 case 9: cind=
'9';
break;
286 assure(
false, CPL_ERROR_ILLEGAL_INPUT,
287 "Illegal index %d, 1-9 expected", index);
292 coeffi_name = cpl_sprintf(
"%sI%d", regression_name, index);
295 check_nomsg( coeffi = uves_read_midas_array(plist, coeffi_name, &length, &type, NULL));
298 assure( type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
299 "Type of array %s is %s, integer expected",
310 assure( coeffi[1] == 2, CPL_ERROR_UNSUPPORTED_MODE,
311 "Regressions is %d-dimensional (2D expected)",
327 regression_name, degree1, degree2);
331 pol = cpl_polynomial_new(2);
335 for (i = 0; !found && i < plist_size; i++){
337 const char *name = cpl_property_get_name(p);
339 if (strcmp(name,
"HISTORY") == 0) {
341 check( value = cpl_property_get_string(p),
342 "Error reading property value");
349 (
int)strlen(value) >= 1+N+2 &&
352 value[1+N+1] ==
'\'')
357 (
int)strlen(value) >= 1+N+3 &&
360 value[1+N+1] == cind &&
361 value[1+N+2] ==
'\'') )
365 strncmp(value+1, regression_name, N) == 0
378 while (power[1] <= degree2){
380 coeff = strtod(value, &next);
384 cpl_polynomial_set_coeff(pol, power, coeff);
385 uves_msg_debug(
"Polynomial coefficient of order (%" CPL_SIZE_FORMAT
", %" CPL_SIZE_FORMAT
") is %e",
386 power[0], power[1], coeff);
389 if (power[0] > degree1){
400 assure(j < plist_size, CPL_ERROR_ILLEGAL_INPUT,
401 "Missing header data");
404 assure( cpl_property_get_type(p) == CPL_TYPE_STRING &&
405 strcmp(cpl_property_get_name(p),
"HISTORY") == 0,
406 CPL_ERROR_ILLEGAL_INPUT,
"Error parsing polynomial");
408 value = cpl_property_get_string(p);
418 assure( found, CPL_ERROR_ILLEGAL_INPUT,
"Could not find '%sD' in property list",
425 uves_free_int(&coeffi);
426 uves_free_string_const(&coeffi_name);
427 uves_free_polynomial(&pol);
428 if (cpl_error_get_code() != CPL_ERROR_NONE)
446 uves_frameset_merge(cpl_frameset * set1,
const cpl_frameset* set2)
449 const cpl_frame* frm_tmp=NULL;
450 cpl_frame* frm_dup=NULL;
452 passure(set1 != NULL,
"Wrong input set");
453 passure(set2 != NULL,
"Wrong input set");
455 for (frm_tmp = cpl_frameset_get_first_const(set2);
457 frm_tmp = cpl_frameset_get_next_const(set2))
459 frm_dup = cpl_frame_duplicate(frm_tmp);
460 cpl_frameset_insert(set1, frm_dup);
464 return cpl_error_get_code();
478 uves_extract_frames_group_type(
const cpl_frameset *
set, cpl_frameset** ext, cpl_frame_group type)
480 const cpl_frame* frm_tmp=NULL;
481 cpl_frame* frm_dup=NULL;
485 check_nomsg(frm_tmp = cpl_frameset_get_first_const(
set));
486 while (frm_tmp != NULL)
488 g=cpl_frame_get_group(frm_tmp);
490 frm_dup=cpl_frame_duplicate(frm_tmp);
491 cpl_frameset_insert(*ext,frm_dup);
492 uves_msg_debug(
"group %d insert file %s ",type,cpl_frame_get_filename(frm_dup));
494 frm_tmp = cpl_frameset_get_next_const(
set);
498 return cpl_error_get_code();
511 uves_sflats_get_encoder_steps(
const cpl_frameset *
set, cpl_table** enc,
int* nset)
514 const cpl_frame* frm=NULL;
521 const int threshold=5;
524 cpl_table* encoder_tbl=NULL;
525 ndata = cpl_frameset_get_size(
set);
526 encoder_tbl=cpl_table_new(ndata);
527 cpl_table_new_column(encoder_tbl,
"x1enc",CPL_TYPE_INT);
528 cpl_table_new_column(encoder_tbl,
"x2enc",CPL_TYPE_INT);
529 cpl_table_new_column(encoder_tbl,
"flag",CPL_TYPE_INT);
531 for(i=0;i<cpl_frameset_get_size(
set);i++)
533 check_nomsg(frm=cpl_frameset_get_frame_const(
set,i));
537 check_nomsg(cpl_table_set_int(encoder_tbl,
"x1enc",i,x1enc));
538 check_nomsg(cpl_table_set_int(encoder_tbl,
"x2enc",i,x2enc));
539 uves_free_propertylist(&plist);
542 check_nomsg(uves_sort_table_2(encoder_tbl,
"x1enc",
"x2enc",
false,
true));
544 check_nomsg(ref_x1enc=cpl_table_get_int(encoder_tbl,
"x1enc",0,&status));
545 check_nomsg(ref_x2enc=cpl_table_get_int(encoder_tbl,
"x2enc",0,&status));
547 *enc=cpl_table_new(1);
548 cpl_table_new_column(*enc,
"x1enc",CPL_TYPE_INT);
549 cpl_table_new_column(*enc,
"x2enc",CPL_TYPE_INT);
550 check_nomsg(cpl_table_set_int(*enc,
"x1enc",0,ref_x1enc));
551 check_nomsg(cpl_table_set_int(*enc,
"x2enc",0,ref_x2enc));
553 for(i=1;i<cpl_table_get_nrow(encoder_tbl);i++) {
554 check_nomsg(x1enc=cpl_table_get_int(encoder_tbl,
"x1enc",i,&status));
555 check_nomsg(x2enc=cpl_table_get_int(encoder_tbl,
"x2enc",i,&status));
556 if( (fabs(ref_x1enc -x1enc) > threshold) ||
557 (fabs(ref_x2enc -x2enc) > threshold) ) {
561 cpl_table_set_size(*enc,(*nset+1));
562 check_nomsg(cpl_table_set_int(*enc,
"x1enc",*nset,ref_x1enc));
563 check_nomsg(cpl_table_set_int(*enc,
"x2enc",*nset,ref_x2enc));
568 uves_msg(
"Number of sets = %d",*nset);
571 uves_free_table(&encoder_tbl);
572 uves_free_propertylist(&plist);
573 return cpl_error_get_code();
585 uves_dfs_set_groups(cpl_frameset *
set)
587 cpl_frame * cur_frame ;
591 assure(
set != NULL, CPL_ERROR_NULL_INPUT,
"Null input");
594 check( nframes = cpl_frameset_get_size(
set),
"Could not read frameset size");
597 for (cur_frame = cpl_frameset_get_first(
set);
599 cur_frame = cpl_frameset_get_next(
set))
602 bool is_calib =
false;
603 bool is_recognized =
false;
606 const char * tag = cpl_frame_get_tag(cur_frame);
608 assure( tag != NULL && strcmp(tag,
"") != 0, CPL_ERROR_ILLEGAL_INPUT,
609 "Frame has no tag!");
617 (strcmp(tag, UVES_ORDER_FLAT (flames,blue)) == 0 ||
618 strcmp(tag, UVES_BIAS (blue)) == 0 ||
619 strcmp(tag, UVES_DARK (blue)) == 0 ||
620 strcmp(tag, UVES_PDARK (blue)) == 0 ||
621 strcmp(tag, UVES_FLAT (blue)) == 0 ||
622 strcmp(tag, UVES_IFLAT (blue)) == 0 ||
623 strcmp(tag, UVES_DFLAT (blue)) == 0 ||
624 strcmp(tag, UVES_SFLAT (blue)) == 0 ||
625 strcmp(tag, UVES_TFLAT (blue)) == 0 ||
626 strcmp(tag, UVES_SCREEN_FLAT (blue)) == 0 ||
627 strcmp(tag, UVES_CD_ALIGN (blue)) == 0 ||
628 strcmp(tag, UVES_FORMATCHECK (flames,blue)) == 0 ||
629 strcmp(tag, UVES_STD_STAR (blue)) == 0 ||
630 strcmp(tag, UVES_SCIENCE (blue)) == 0 ||
631 strcmp(tag, UVES_SCI_EXTND (blue)) == 0 ||
632 strcmp(tag, UVES_SCI_POINT (blue)) == 0 ||
633 strcmp(tag, UVES_SCI_SLICER (blue)) == 0 ||
634 strcmp(tag, UVES_ARC_LAMP (flames,blue)) == 0 ||
635 strcmp(tag, UVES_ECH_ARC_LAMP(blue)) == 0 ||
636 strcmp(tag, RAW_IMA) == 0 ||
637 strcmp(tag, FLAMES_SCI_RED) == 0 ||
638 strcmp(tag, FLAMES_SCI_SIM_RED) == 0 ||
639 strcmp(tag, FLAMES_SCI_COM_RED) == 0 ||
640 strcmp(tag, FLAMES_FIB_FF_ODD) == 0 ||
641 strcmp(tag, FLAMES_FIB_FF_EVEN) == 0 ||
642 strcmp(tag, FLAMES_FIB_FF_ALL) == 0);
648 chip != UVES_CHIP_INVALID;
653 is_calib = is_calib ||
654 (strcmp(tag, UVES_DRS_SETUP(flames, chip)) == 0 ||
655 strcmp(tag, UVES_ORDER_TABLE(flames, chip)) == 0 ||
656 strcmp(tag, UVES_GUESS_ORDER_TABLE(flames,chip)) == 0 ||
657 strcmp(tag, UVES_MASTER_BIAS (chip)) == 0 ||
658 strcmp(tag, UVES_MASTER_DARK (chip)) == 0 ||
659 strcmp(tag, UVES_MASTER_PDARK (chip)) == 0 ||
660 strcmp(tag, UVES_MASTER_FLAT (chip)) == 0 ||
661 strcmp(tag, UVES_MASTER_DFLAT (chip)) == 0 ||
662 strcmp(tag, UVES_MASTER_SFLAT (chip)) == 0 ||
663 strcmp(tag, UVES_MASTER_IFLAT (chip)) == 0 ||
664 strcmp(tag, UVES_MASTER_TFLAT (chip)) == 0 ||
665 strcmp(tag, UVES_REF_TFLAT (chip)) == 0 ||
666 strcmp(tag, UVES_ORD_TAB(flames,chip)) == 0 ||
667 strcmp(tag, UVES_MASTER_SCREEN_FLAT(chip)) == 0 ||
668 strcmp(tag, UVES_MASTER_ARC_FORM(chip)) == 0 ||
669 strcmp(tag, UVES_WEIGHTS(chip)) == 0 ||
670 strcmp(tag, UVES_LINE_TABLE(flames,chip)) == 0 ||
671 strcmp(tag, UVES_GUESS_LINE_TABLE(flames,chip)) == 0 ||
672 strcmp(tag, UVES_INSTR_RESPONSE(chip)) == 0 ||
673 strcmp(tag, UVES_MASTER_RESPONSE(chip)) == 0 ||
674 strcmp(tag, UVES_LINE_REFER_TABLE ) == 0 ||
675 strcmp(tag, UVES_LINE_INTMON_TABLE ) == 0 ||
676 strcmp(tag, UVES_FLUX_STD_TABLE ) == 0 ||
677 strcmp(tag, UVES_EXTCOEFF_TABLE ) == 0 ||
678 strcmp(tag, FLAMES_LINE_TABLE(chip)) == 0 ||
679 strcmp(tag, FLAMES_SLIT_FF_DT1(chip)) == 0 ||
680 strcmp(tag, FLAMES_SLIT_FF_DT2(chip)) == 0 ||
681 strcmp(tag, FLAMES_SLIT_FF_DT3(chip)) == 0 ||
682 strcmp(tag, FLAMES_SLIT_FF_DTC(chip)) == 0 ||
683 strcmp(tag, FLAMES_SLIT_FF_BP1(chip)) == 0 ||
684 strcmp(tag, FLAMES_SLIT_FF_BP2(chip)) == 0 ||
685 strcmp(tag, FLAMES_SLIT_FF_BP3(chip)) == 0 ||
686 strcmp(tag, FLAMES_SLIT_FF_BPC(chip)) == 0 ||
687 strcmp(tag, FLAMES_SLIT_FF_BN1(chip)) == 0 ||
688 strcmp(tag, FLAMES_SLIT_FF_BN2(chip)) == 0 ||
689 strcmp(tag, FLAMES_SLIT_FF_BN3(chip)) == 0 ||
690 strcmp(tag, FLAMES_SLIT_FF_BNC(chip)) == 0 ||
691 strcmp(tag, FLAMES_SLIT_FF_SG1(chip)) == 0 ||
692 strcmp(tag, FLAMES_SLIT_FF_SG2(chip)) == 0 ||
693 strcmp(tag, FLAMES_SLIT_FF_SG3(chip)) == 0 ||
694 strcmp(tag, FLAMES_SLIT_FF_SGC(chip)) == 0 ||
695 strcmp(tag, FLAMES_SLIT_FF_COM(chip)) == 0 ||
696 strcmp(tag, FLAMES_SLIT_FF_NOR(chip)) == 0 ||
697 strcmp(tag, FLAMES_SLIT_FF_NSG(chip)) == 0 ||
698 strcmp(tag, FLAMES_FIB_FF_DT1(chip)) == 0 ||
699 strcmp(tag, FLAMES_FIB_FF_DT2(chip)) == 0 ||
700 strcmp(tag, FLAMES_FIB_FF_DT3(chip)) == 0 ||
701 strcmp(tag, FLAMES_FIB_FF_DTC(chip)) == 0 ||
702 strcmp(tag, FLAMES_FIB_FF_BP1(chip)) == 0 ||
703 strcmp(tag, FLAMES_FIB_FF_BP2(chip)) == 0 ||
704 strcmp(tag, FLAMES_FIB_FF_BP3(chip)) == 0 ||
705 strcmp(tag, FLAMES_FIB_FF_BPC(chip)) == 0 ||
706 strcmp(tag, FLAMES_FIB_FF_BN1(chip)) == 0 ||
707 strcmp(tag, FLAMES_FIB_FF_BN2(chip)) == 0 ||
708 strcmp(tag, FLAMES_FIB_FF_BN3(chip)) == 0 ||
709 strcmp(tag, FLAMES_FIB_FF_BNC(chip)) == 0 ||
710 strcmp(tag, FLAMES_FIB_FF_SG1(chip)) == 0 ||
711 strcmp(tag, FLAMES_FIB_FF_SG2(chip)) == 0 ||
712 strcmp(tag, FLAMES_FIB_FF_SG3(chip)) == 0 ||
713 strcmp(tag, FLAMES_FIB_FF_SGC(chip)) == 0 ||
714 strcmp(tag, FLAMES_FIB_FF_COM(chip)) == 0 ||
715 strcmp(tag, FLAMES_FIB_FF_NOR(chip)) == 0 ||
716 strcmp(tag, FLAMES_FIB_FF_NSG(chip)) == 0 ||
717 strcmp(tag, FLAMES_ORDEF(flames,chip)) == 0 ||
718 strcmp(tag, FLAMES_CORVEL_MASK) == 0);
720 for (window = 1; window <= 3; window++)
722 is_calib = is_calib ||
723 strcmp(tag, UVES_LINE_TABLE_MIDAS(chip, window)) == 0;
726 if (!flames && strcmp(tag, UVES_BACKGR_TABLE(chip)) == 0)
729 "Inter-order positions will be inferred "
730 "from the order table %s. "
731 "Use recipe parameters to define "
733 UVES_BACKGR_TABLE(chip),
734 UVES_ORDER_TABLE(flames, chip));
736 is_recognized =
true;
739 if (strcmp(tag, UVES_DRS_SETUP(flames, chip)) == 0)
742 "Use recipe parameters "
743 "to define data reduction parameters ",
744 UVES_DRS_SETUP(flames, chip));
746 is_recognized =
true;
755 is_recognized = is_recognized || is_raw || is_calib;
759 cpl_frame_set_group(cur_frame, CPL_FRAME_GROUP_RAW) ;
763 cpl_frame_set_group(cur_frame, CPL_FRAME_GROUP_CALIB) ;
765 else if (!is_recognized)
771 uves_dfs_files_dont_exist(
set);
775 return cpl_error_get_code();
792 bool blue, new_format;
800 chip != UVES_CHIP_INVALID;
814 "Error erasing keyword '%s'", UVES_PRESCANX(new_format, chip));
818 "Error erasing keyword '%s'", UVES_PRESCANY(new_format, chip));
822 "Error erasing keyword '%s'", UVES_OVRSCANX(new_format, chip));
826 "Error erasing keyword '%s'", UVES_OVRSCANY(new_format, chip));
828 while (n_erase_px > 0 ||
837 new_format = !new_format;
924 uves_frameset_insert(cpl_frameset *frames,
926 cpl_frame_group group,
928 cpl_frame_level level,
929 const char *filename,
934 const cpl_parameterlist *parameters,
936 const char *pipeline,
938 const char *start_time,
944 const char *origin =
"";
946 passure( !(type == CPL_FRAME_TYPE_IMAGE && table_header != NULL),
" ");
947 passure( raw_header != NULL,
" ");
948 passure( primary_header != NULL,
" ");
950 assure( type == CPL_FRAME_TYPE_IMAGE || stats_mask == 0,
951 CPL_ERROR_INCOMPATIBLE_INPUT,
952 "Cannot compute image statistics on table product" );
955 check(( f = cpl_frame_new(),
956 cpl_frame_set_filename(f, filename),
957 cpl_frame_set_tag (f, tag),
958 cpl_frame_set_type (f, type),
959 cpl_frame_set_group (f, group),
960 cpl_frame_set_level (f, level),
961 cpl_frameset_insert(frames, f)),
"Could not insert frame into frameset");
964 if (strchr(pipeline,
'/') == NULL)
967 "Pipeline-name/version", pipeline);
986 "Could not copy keywords");
990 "Could not copy keywords");
994 UVES_TIME_START(
"cpl_dfs_setup_product_header");
995 check( uves_dfs_setup_product_header(pl,
1002 "Error setting up product header");
1011 check( uves_get_property_value(pl,
"ORIGIN", CPL_TYPE_STRING, &origin),
1012 "Error reading ORIGIN from product header");
1014 if (strcmp(origin,
"ESO-MIDAS") == 0)
1020 if (type == CPL_FRAME_TYPE_IMAGE && stats_mask != 0)
1022 check( uves_dfs_write_statistics((cpl_image *)
object, pl, stats_mask),
1023 "Error adding image statistics keywords");
1031 "Could not propagate 'ESO DET*' keywords");
1035 check( remove_pre_over_scan(pl),
1036 "Error removing pre-, overscan keywords from product header");
1040 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_AIRMASS) );
1041 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_IMAGETYP) );
1042 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_UT) );
1043 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_ST) );
1044 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_EXPTIME) );
1045 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_EXTNAME) );
1046 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_DATE) );
1047 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_DATAMEAN) );
1048 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_DATAMED) );
1049 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_DATARMS) );
1050 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_OS_EXPOI) );
1058 "Could not propagate 'GRATi' keywords");
1060 "Could not propagate 'FILTERi' keywords");
1062 "Could not propagate 'WLENi' keywords");
1067 pl, raw_header,
"^((GRAT|FILTER|WLEN)[0-9]*)$", 0),
1068 "Could not propagate GRATi, FILTERi and WLENi keywords");
1086 bool invert =
false;
1088 "ESO PRO (REDLEVEL|REC[0-9]+ STATUS)|"
1089 "TM-START|MIDASFTP|FILENAME)$", invert);
1093 "Could not write recipe start time");
1096 "Could not write recipe stop time");
1104 for (i = 0; qc[i] != NULL; i++)
1111 if (strcmp(recipe, make_str(UVES_TFLAT_ID)) == 0 && i == 1)
1126 UVES_TIME_START(
"save product");
1129 if (type == CPL_FRAME_TYPE_IMAGE)
1131 bool use_bitpix16_for_int = (strcmp(recipe, make_str(FLAMES_CAL_ORDERPOS)) == 0);
1133 check( uves_save_image((cpl_image *)
object, filename, pl,
1134 use_bitpix16_for_int,
true),
1135 "Error saving image to file %s", filename);
1137 else if (type == CPL_FRAME_TYPE_TABLE)
1139 check( uves_table_save((cpl_table *)
object,
1144 "Error saving table to file '%s'", filename);
1148 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Unsupported frame type");
1154 uves_free_propertylist(&pl);
1156 return cpl_error_get_code();
1172 unsigned stats_mask)
1174 cpl_stats *stats = NULL;
1177 assure( (stats_mask & (CPL_STATS_MEAN | CPL_STATS_STDEV | CPL_STATS_MEDIAN |
1178 CPL_STATS_MIN | CPL_STATS_MAX)) == stats_mask,
1179 CPL_ERROR_UNSUPPORTED_MODE,
"Cannot compute mask %d",
1182 UVES_TIME_START(
"calculate stats");
1184 check( stats = cpl_stats_new_from_image(
1186 "Error reading image statistics");
1190 if (stats_mask & CPL_STATS_MEDIAN)
1193 "Could not write median flux");
1195 if (stats_mask & CPL_STATS_MEAN)
1198 "Could not write average flux");
1200 if (stats_mask & CPL_STATS_STDEV)
1203 "Could not write flux stdev");
1205 if (stats_mask & CPL_STATS_MIN)
1208 "Could not write min flux");
1210 if (stats_mask & CPL_STATS_MIN)
1213 "Could not write max flux");
1217 uves_free_stats(&stats);
1260 int *length, cpl_type *type,
int *ncards)
1262 void *result = NULL;
1263 unsigned result_size;
1264 int N = strlen(name);
1273 for (i = 0; !found && i < plist_size; i++)
1276 value = cpl_property_get_name(p);
1278 if (strcmp(value,
"HISTORY") == 0)
1281 check( value = cpl_property_get_string(p),
1282 "Error reading property value");
1286 if ((
int)strlen(value) >= 1+N+4 &&
1288 value[N+1] ==
'\'' &&
1289 value[N+2] ==
',' &&
1290 value[N+3] ==
'\'' &&
1291 strncmp(value+1, name, N) == 0
1294 switch(value[N+4]) {
1300 *type = CPL_TYPE_DOUBLE;
1302 if ((
int)strlen(value) >= 1+N+4+2 && value[N+4+1] ==
'*')
1304 switch(value[N+4+2]) {
1305 case '4': *type = CPL_TYPE_FLOAT;
break;
1306 case '8': *type = CPL_TYPE_DOUBLE;
break;
1308 assure(
false, CPL_ERROR_ILLEGAL_INPUT,
1309 "Unrecognized MIDAS type: 'R*%c'",
1315 case 'I': *type = CPL_TYPE_INT ; size =
sizeof(int);
break;
1316 case 'C': *type = CPL_TYPE_STRING; size =
sizeof(char);
break;
1318 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1319 "Unrecognized type '%c'", value[N+4]);
1327 assure( found, CPL_ERROR_ILLEGAL_INPUT,
"Could not find '%s' in property list", name);
1330 result_size =
sizeof(double) * 100;
1331 result = cpl_malloc(result_size);
1334 if (ncards != NULL) *ncards = 2;
1336 const cpl_property *p;
1338 if (ncards != NULL) *ncards += 1;
1340 assure(i < plist_size,
1341 CPL_ERROR_ILLEGAL_INPUT,
"Missing header data");
1343 assure( cpl_property_get_type(p) == CPL_TYPE_STRING &&
1344 strcmp(cpl_property_get_name(p),
"HISTORY") == 0,
1345 CPL_ERROR_ILLEGAL_INPUT,
"Error parsing array");
1350 if (*type == CPL_TYPE_STRING)
1352 assure( strlen(value) < 100, CPL_ERROR_UNSUPPORTED_MODE,
1353 "String too long. Max size is 100");
1359 int len = strlen(value);
1362 for (k = 0; k <= len; k++)
1365 ((
char*)result)[j] = value[k];
1373 value, (
char*)result);
1380 if (strcmp(value,
"") != 0) {
1381 double numberd = -1;
1384 const int base = 10;
1385 char *next = (
char *) value;
1390 case CPL_TYPE_DOUBLE:
1391 numberd = strtod(value, &next);
1394 case CPL_TYPE_FLOAT:
1395 numberf = strtod(value, &next);
1399 numberi = strtol(value, &next, base);
1410 if (*length *
sizeof(
double) > result_size)
1413 result = cpl_realloc(result, result_size);
1417 case CPL_TYPE_DOUBLE:
1418 ((
double *)result)[*length-1] = numberd;
1420 case CPL_TYPE_FLOAT:
1421 ((
float *)result)[*length-1] = numberf;
1424 ((
int *)result)[*length-1] = numberi;
1433 case CPL_TYPE_DOUBLE:
1434 numberd = strtod(value, &next);
1437 case CPL_TYPE_FLOAT:
1438 numberf = strtod(value, &next);
1442 numberi = strtol(value, &next, base);
1449 }
while (next != value);
1455 assure( strcmp(value,
"") == 0, CPL_ERROR_ILLEGAL_INPUT,
1456 "Cannot parse %s descriptor %s, remaining string: '%s'",
1463 if (cpl_property_get_type(p) == CPL_TYPE_STRING &&
1464 strcmp(cpl_property_get_name(p),
"HISTORY") == 0)
1466 value = cpl_property_get_string(
1469 if (*type == CPL_TYPE_STRING)
1471 if (strcmp(value,
"") != 0) {
1472 uves_msg_debug(
"String array %s with length > 1 found. Ignoring remaining values", name);
1473 while (strcmp(value,
"") != 0 && i+1 < plist_size) {
1476 value = cpl_property_get_string(
1478 if (ncards != NULL) *ncards += 1;
1485 }
while (strcmp(value,
"") != 0);
1488 if (cpl_error_get_code() != CPL_ERROR_NONE)
1490 cpl_free(result); result = NULL;
1516 uves_save_table_local(
const char *description,
const char *filename_prefix,
1517 const cpl_table *table,
1518 enum uves_chip chip,
int trace,
int window,
1521 char *filename = NULL;
1523 check( filename = uves_local_filename(filename_prefix, chip, trace, window),
1524 "Error getting filename");
1526 check( uves_table_save(table, pheader, eheader, filename, CPL_IO_DEFAULT),
1527 "Error saving table to file '%s'", filename);
1529 if (description != NULL)
uves_msg(
"%s saved to '%s'", description, filename);
1533 return cpl_error_get_code();
1559 uves_save_image_local(
const char *description,
const char *filename_prefix,
1560 const cpl_image *image,
1561 enum uves_chip chip,
int trace,
int window,
1563 bool use_bitpix16_for_int)
1565 char *filename = NULL;
1567 check( filename = uves_local_filename(filename_prefix, chip, trace, window),
1568 "Error getting filename");
1570 check( uves_save_image(image, filename, plist, use_bitpix16_for_int,
true),
1571 "Error saving image to file '%s'", filename);
1572 if (description != NULL)
uves_msg(
"%s saved to '%s'", description, filename);
1576 return cpl_error_get_code();
1591 cpl_image *uves_load_image(
const cpl_frame *f,
1596 cpl_image *image = NULL;
1598 const char *filename;
1602 cpl_vector * vector=NULL;
1607 assure( cpl_frame_get_type(f) == CPL_FRAME_TYPE_IMAGE,
1608 CPL_ERROR_TYPE_MISMATCH,
"Wrong type: %s",
1611 filename = cpl_frame_get_filename(f);
1614 "Could not load header from %s extension %d",
1615 filename, extension);
1618 "Could not read BITPIX from %s extension %d",
1619 filename, extension);
1621 if (bitpix == -32) type = CPL_TYPE_FLOAT;
1622 else if (bitpix == -64) type = CPL_TYPE_DOUBLE;
1623 else if (bitpix == 32) type = CPL_TYPE_INT;
1624 else if (bitpix == 16) type = CPL_TYPE_INT;
1627 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1628 "No CPL type to represent BITPIX = %d", bitpix);
1632 "could not get NAXIS" );
1636 check( vector = cpl_vector_load(filename,extension),
1637 "Could not load vector from extension %d of file '%s' ",
1638 extension, filename);
1639 cknull(image=uves_vector_to_image(vector,type),
1640 "could not convert vector to image");
1644 check( image = cpl_image_load(filename,
1648 "Could not load image from extension %d of file '%s' ",
1649 extension, filename);
1659 uves_free_vector(&vector);
1660 uves_free_propertylist(&plist);
1669 cpl_image *uves_load_image_file(
const char *filename,
1675 cpl_frame *f = cpl_frame_new();
1676 cpl_frame_set_filename(f, filename);
1677 cpl_frame_set_type(f, CPL_FRAME_TYPE_IMAGE);
1679 i = uves_load_image(f, plane, extension, header);
1681 uves_free_frame(&f);
1713 uves_save_image(
const cpl_image *image,
const char *filename,
const uves_propertylist *plist,
1714 bool use_bitpix16_for_int,
bool save1d)
1718 const cpl_vector *image_1d = NULL;
1720 cpl_image *thresholded = NULL;
1721 cpl_image *thresholded_double = NULL;
1723 if (image == NULL) {
1724 check( uves_image_save(image, filename, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT),
1725 "Error saving NULL image to file '%s'", filename);
1728 check( t = cpl_image_get_type(image),
"Error reading image type");
1729 if (t == CPL_TYPE_FLOAT ) bpp = CPL_BPP_IEEE_FLOAT;
1730 else if (t == CPL_TYPE_DOUBLE) bpp = CPL_BPP_IEEE_FLOAT;
1737 else if (t == CPL_TYPE_INT ) {
1738 if (use_bitpix16_for_int) bpp = CPL_BPP_16_UNSIGNED;
1739 else bpp = CPL_BPP_32_SIGNED;
1741 else assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1745 thresholded = cpl_image_duplicate(image);
1748 if (t == CPL_TYPE_DOUBLE)
1750 passure( bpp == CPL_BPP_IEEE_FLOAT,
"%d", bpp);
1766 -FLT_MAX, FLT_MAX) );
1770 double *data = cpl_image_get_data_double(thresholded);
1771 int nx = cpl_image_get_size_x(thresholded);
1772 int ny = cpl_image_get_size_y(thresholded);
1775 for (y = 0; y < ny; y++)
1776 for (x = 0; x < nx; x++)
1778 if (irplib_isnan(data[x + y*nx]))
1787 cpl_image_get_size_y(thresholded) == 1 &&
1788 (t == CPL_TYPE_DOUBLE ||
1789 t == CPL_TYPE_FLOAT)) {
1791 bool invert =
false;
1808 if (t == CPL_TYPE_FLOAT) {
1809 thresholded_double = cpl_image_cast(thresholded, CPL_TYPE_DOUBLE);
1812 thresholded_double = cpl_image_duplicate(thresholded);
1815 passure( cpl_image_get_type(thresholded_double) == CPL_TYPE_DOUBLE,
"%d",
1816 cpl_image_get_type(thresholded_double));
1818 image_1d = cpl_vector_wrap(
1819 cpl_image_get_size_x(thresholded_double),
1820 cpl_image_get_data_double(thresholded_double));
1822 check( uves_vector_save(image_1d, filename, bpp, header, CPL_IO_DEFAULT),
1823 "Error saving vector to file '%s'", filename );
1827 check( uves_image_save(thresholded, filename, bpp, plist, CPL_IO_DEFAULT),
1828 "Error saving image to file '%s'", filename);
1833 uves_unwrap_vector_const(&image_1d);
1834 uves_free_propertylist(&header);
1835 uves_free_image(&thresholded);
1836 uves_free_image(&thresholded_double);
1864 uves_save_imagelist(
const cpl_imagelist *iml,
const char *filename,
const uves_propertylist *plist)
1866 const cpl_image* img=NULL;
1869 const cpl_vector *image_1d = NULL;
1871 cpl_imagelist *thresholded = NULL;
1878 cknull(iml,
"Null input image");
1879 check(img=cpl_imagelist_get_const(iml,0),
"error reading image");
1885 check( t = cpl_image_get_type(img),
"Error reading image type");
1886 if (t == CPL_TYPE_FLOAT ) bpp = CPL_BPP_IEEE_FLOAT;
1887 else if (t == CPL_TYPE_DOUBLE) bpp = CPL_BPP_IEEE_FLOAT;
1890 else if (t == CPL_TYPE_INT ) bpp = CPL_BPP_16_UNSIGNED;
1891 else assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1895 thresholded = cpl_imagelist_duplicate(iml);
1898 if (t == CPL_TYPE_DOUBLE)
1900 passure( bpp == CPL_BPP_IEEE_FLOAT,
"%d", bpp);
1916 -FLT_MAX, FLT_MAX) );
1924 cpl_image* ima=NULL;
1925 for (z = 0; z < nz; z++) {
1926 ima=cpl_imagelist_get(thresholded,z);
1927 data = cpl_image_get_data_double(ima);
1929 for (y = 0; y < ny; y++) {
1930 for (x = 0; x < nx; x++) {
1931 if (irplib_isnan(data[x + y*nx])) {
1939 if (nz == 1 && t == CPL_TYPE_DOUBLE)
1943 bool invert =
false;
1968 check( uves_imagelist_save(thresholded, filename, bpp, plist, CPL_IO_DEFAULT),
1969 "Error saving image to file '%s'", filename);
1973 uves_unwrap_vector_const(&image_1d);
1974 uves_free_propertylist(&header);
1975 uves_free_imagelist(&thresholded);
1998 cpl_table *t = NULL;
2002 check( uves_table_save(t,
2008 "Error saving table to file '%s'", filename);
2011 uves_free_table(&t);
2012 return cpl_error_get_code();
2026 load_polynomial(
const char* filename,
int extension)
2029 cpl_table *t = NULL;
2031 check(t = cpl_table_load(filename,
2035 "Error loading polynomial from extension %d of file '%s'", extension, filename);
2037 assure( uves_erase_invalid_table_rows(t, NULL) == 0,
2038 CPL_ERROR_ILLEGAL_INPUT,
"Table contains invalid rows");
2043 uves_free_table(&t);
2044 if (cpl_error_get_code() != CPL_ERROR_NONE)
2065 identify_arm(
const cpl_frameset *frames,
const char *blue_tag,
const char *red_tag,
2068 const char *tag = NULL;
2070 const cpl_frame *frame = NULL;
2073 assure (!cpl_frameset_is_empty(frames), CPL_ERROR_ILLEGAL_INPUT,
"No input frames");
2076 frame = cpl_frameset_find_const(frames, blue_tag);
2077 *blue = (frame != NULL);
2081 frame = cpl_frameset_find_const(frames, red_tag);
2084 assure( frame != NULL, CPL_ERROR_ILLEGAL_INPUT,
2085 "No valid input frames "
2086 "('%s' or '%s') in frame set",
2089 assure( cpl_frameset_find_const(frames, blue_tag) == NULL ||
2090 cpl_frameset_find_const(frames, red_tag) == NULL,
2091 CPL_ERROR_INCOMPATIBLE_INPUT,
2092 "Multiple types of input frames ('%s' and '%s') in frame set",
2095 tag = cpl_frame_get_tag(frame);
2097 uves_msg(
"Input frames are '%s'", tag);
2125 enum uves_chip chip,
2129 cpl_image *result = NULL;
2130 int prescanx, ovrscanx;
2132 int x_0, y_0, x_1, y_1;
2135 const char *ctype1, *ctype2;
2136 const char *cunit1, *cunit2;
2139 double crval1, crval2;
2140 double crpix1, crpix2;
2141 double cdelt1, cdelt2;
2145 passure( header != NULL,
" ");
2146 passure( out_header != NULL,
" ");
2148 nx = cpl_image_get_size_x(image);
2149 ny = cpl_image_get_size_y(image);
2207 if (new_format || chip == UVES_CHIP_BLUE)
2210 x_1 = nx - ovrscanx;
2214 if (chip == UVES_CHIP_REDU)
2217 x_1 = nx/2 - ovrscanx;
2221 x_0 = nx/2 + prescanx + 1;
2222 x_1 = nx - ovrscanx;
2225 check( result = cpl_image_extract(image, x_0, y_0, x_1, y_1),
"Could not crop image");
2226 crpix1 = crpix1 - (x_0 - 1);
2227 crpix2 = crpix2 - (y_0 - 1);
2228 nx = (x_1 - x_0) + 1;
2229 ny = (y_1 - y_0) + 1;
2232 UVES_TIME_START(
"Rotation");
2240 int crpix1_old = crpix1;
2241 int crpix2_old = crpix2;
2242 int crval1_old = crval1;
2243 int crval2_old = crval2;
2244 int cdelt1_old = cdelt1;
2245 int cdelt2_old = cdelt2;
2246 const char *ctype1_old = ctype1;
2247 const char *ctype2_old = ctype2;
2249 if (chip == UVES_CHIP_BLUE)
2252 check( cpl_image_turn(result, -1),
"Could not turn image");
2254 crpix1 = ny - (crpix2_old - 1);
2255 crpix2 = crpix1_old;
2256 crval1 = crval2_old;
2257 crval2 = crval1_old;
2263 check( cpl_image_flip(result, 3),
"Could not flip image");
2265 crpix1 = ny - (crpix2_old - 1);
2266 crpix2 = nx - (crpix1_old - 1);
2267 crval1 = crval2_old;
2268 crval2 = crval1_old;
2273 ctype1 = ctype2_old;
2274 ctype2 = ctype1_old;
2275 cdelt1 = cdelt2_old;
2276 cdelt2 = cdelt1_old;
2288 if (chip == UVES_CHIP_BLUE || chip == UVES_CHIP_REDL)
2295 int physical_gap_between_chips = 64;
2298 passure( chip == UVES_CHIP_REDU ,
"%d", chip );
2311 physical_gap_between_chips,
2312 "Error reading REDL chip geometry");
2314 uves_msg_debug(
"Setting CRVAL2 = 1 + (%d - %d - %d) * %f + %d = %f",
2319 physical_gap_between_chips, crval2);
2330 physical_gap_between_chips,
2331 "Error reading REDL chip geometry");
2333 uves_msg_debug(
"Setting CRVAL2 = 1 + (%d - %d - %d) * %f + %d = %f",
2338 physical_gap_between_chips, crval2);
2351 "Error initializing header");
2358 uves_msg(
"Raw image cropped and rotated from %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
" to %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
"",
2360 cpl_image_get_size_x(result),
2361 cpl_image_get_size_y(result));
2364 if (cpl_error_get_code() != CPL_ERROR_NONE)
2366 uves_free_image(&result);
2367 if (out_header != NULL)
2369 uves_free_propertylist(out_header);
2393 const char *raw_chip_name,
enum uves_chip chip)
2395 const char *calib_chip_name;
2396 bool mismatch =
false;
2399 "Could not read chip name of calibration data");
2407 unsigned int calib_first, calib_last;
2408 unsigned int raw_first, raw_last;
2412 while (calib_chip_name[calib_first] ==
' ' && calib_first < strlen(calib_chip_name) - 1)
2416 while (raw_chip_name[raw_first] ==
' ' && raw_first < strlen(raw_chip_name) - 1)
2421 calib_last = strlen(calib_chip_name) - 1;
2422 raw_last = strlen(raw_chip_name) - 1;
2423 while (calib_chip_name[calib_last] ==
' ' && calib_last > 0)
2427 while (raw_chip_name[raw_last] ==
' ' && raw_last > 0)
2433 if (calib_last - calib_first != raw_last - raw_first)
2441 for (i = 0; i <= (calib_last - calib_first); i++)
2443 if (raw_chip_name[raw_first + i] !=
2444 calib_chip_name[calib_first + i])
2456 "not match raw frame chip ID '%s'",
2457 calib_chip_name, raw_chip_name);
2489 static cpl_error_code
2490 load_raw_image(
const char *filename,
2494 cpl_image *raw_image[2],
2500 cpl_image *image = NULL;
2503 int extension, nextensions;
2507 cpl_image* image1=NULL;
2508 cpl_image* image2=NULL;
2514 raw_image[0] = NULL;
2515 raw_image[1] = NULL;
2516 raw_header[0] = NULL;
2517 raw_header[1] = NULL;
2518 rotated_header[0] = NULL;
2519 rotated_header[1] = NULL;
2521 check( nextensions = uves_get_nextensions(filename),
2522 "Error reading number of extensions of file '%s'", filename);
2528 "Could not load header from extension %d of file '%s'",
2529 extension, filename);
2532 "Error determining new/old format of file %s", filename);
2534 uves_msg_low(
"Raw frame is %s, %s format, file '%s' has %d extensions",
2535 (blue) ?
"blue" :
"red", (new_format) ?
"new" :
"old",
2536 filename, nextensions);
2539 if (blue || !new_format)
2541 enum uves_chip chip;
2545 assure( nextensions == 0 ||
2546 (blue && nextensions == 2) ||
2547 (flames && nextensions == 2),
2548 CPL_ERROR_ILLEGAL_INPUT,
2549 "Unrecognized format of file '%s'. %d extensions expected. %d found.",
2551 ((flames||blue) && (nextensions ==2)) ? 2 : 0, nextensions);
2558 "Could not load header from extension %d of file '%s'",
2559 extension, filename);
2563 if(blue && nextensions == 2) {
2567 "Could not load header from extension %d of file '%s'",
2568 extension, filename);
2570 "Could not collate header from extension 1 to 0 of file '%s'",filename);
2571 uves_free_propertylist(&raw_header[1]);
2573 check( image1 = cpl_image_load(filename,
2577 ),
"Could not load image from extension %d of file '%s' ",
2578 extension, filename);
2579 cpl_image_save(image1,
"ima1.fits", CPL_BPP_IEEE_FLOAT,
2580 NULL,CPL_IO_DEFAULT);
2583 check( image2 = cpl_image_load(filename,
2587 ),
"Could not load image from extension %d of file '%s' ",
2588 extension, filename);
2597 uves_free_image(&image1);
2598 uves_free_image(&image2);
2607 check( image = cpl_image_load(filename,
2611 ),
"Could not load image from extension %d of file '%s' ",
2612 extension, filename);
2616 chip = (blue) ? UVES_CHIP_BLUE : UVES_CHIP_REDL;
2617 check( raw_image[0] = uves_crop_and_rotate(image, raw_header[0],
2618 chip, raw_header[0],
2620 &rotated_header[0]),
2621 "Error splitting image");
2629 "Error duplicating FITS header");
2632 chip = UVES_CHIP_REDU;
2633 redl_header = raw_header[0];
2634 check( raw_image[1] = uves_crop_and_rotate(image, raw_header[1],
2637 &rotated_header[1]),
2638 "Error splitting red image");
2642 raw_image[1] = NULL;
2643 raw_header[1] = NULL;
2644 rotated_header[1] = NULL;
2654 assure( nextensions >= 2, CPL_ERROR_UNSUPPORTED_MODE,
2655 "File '%s' (red frame) has %d extensions. 2+ extensions expected "
2657 filename, nextensions);
2660 (nextensions > 2) ?
"FLAMES" :
"FLAMES/UVES");
2664 for (extension = 1; extension <= 2; extension++)
2669 enum uves_chip chip = (extension == 1) ? UVES_CHIP_REDU : UVES_CHIP_REDL;
2673 uves_free_propertylist(&ext_header);
2676 "Could not load header from extension %d of file '%s'",
2677 extension, filename);
2681 "Error cloning primary header");
2686 ext_header,
".*", 0),
2687 "Error merging primary header with extension %d header",
2694 for (extension = 1; extension <= 2; extension++)
2696 enum uves_chip chip = (extension == 1) ? UVES_CHIP_REDU : UVES_CHIP_REDL;
2702 uves_free_image(&image);
2703 check( image = cpl_image_load(filename,
2707 "Could not load image from extension %d of file '%s' ",
2708 extension, filename);
2710 check( raw_image[indx] = uves_crop_and_rotate(image,
2714 &rotated_header[indx]),
2715 "Error splitting red image");
2723 uves_free_image(&image);
2724 uves_free_image(&image1);
2725 uves_free_image(&image2);
2727 uves_free_propertylist(&primary_header);
2728 uves_free_propertylist(&ext_header);
2730 if (cpl_error_get_code() != CPL_ERROR_NONE)
2732 uves_free_image (&raw_image[0]);
2733 uves_free_image (&raw_image[1]);
2734 uves_free_propertylist(&raw_header[0]);
2735 uves_free_propertylist(&raw_header[1]);
2736 uves_free_propertylist(&rotated_header[0]);
2737 uves_free_propertylist(&rotated_header[1]);
2740 return cpl_error_get_code();
2774 uves_load_raw_imagelist(
const cpl_frameset *frames,
2776 const char *blue_tag,
const char *red_tag, cpl_type type,
2777 cpl_imagelist *images[2],
2781 const char *tag = NULL;
2782 const cpl_frame *frame = NULL;
2783 cpl_image *temp_image[2] = {NULL, NULL};
2785 cpl_size number_of_frames = 0;
2786 int frameset_size = 0;
2790 raw_headers[0] = NULL;
2791 raw_headers[1] = NULL;
2793 check( frameset_size = cpl_frameset_get_size(frames),
2794 "Error reading frameset size");
2796 check( tag = identify_arm(frames, blue_tag, red_tag, blue),
2797 "Could not identify chip type");
2799 nchips = (*blue) ? 1 : 2;
2800 for(chip = 0; chip < nchips; chip++)
2802 images[chip] = NULL;
2803 rotated_header[chip] = NULL;
2805 images[chip] = cpl_imagelist_new();
2813 number_of_frames = 0;
2814 for(frame = cpl_frameset_get_first_const(frames);
2816 frame = cpl_frameset_get_next_const(frames))
2819 if ( strcmp(cpl_frame_get_tag(frame), tag) == 0)
2821 const char *filename = cpl_frame_get_filename(frame);
2824 uves_free_propertylist(&rotated_header[0]);
2825 uves_free_propertylist(&rotated_header[1]);
2827 check( load_raw_image(filename,
2834 "Could not load image from file '%s'", filename);
2837 for(chip = 0; chip < nchips; chip++)
2839 raw_headers[chip][number_of_frames] = temp_header[chip];
2840 temp_header[chip] = NULL;
2842 check( cpl_imagelist_set(images[chip],
2845 cpl_imagelist_get_size(images[chip])
2847 "Could not insert image into image list");
2850 temp_image[chip] = NULL;
2853 number_of_frames += 1;
2859 for(chip = 0; chip < nchips; chip++)
2862 assure (cpl_imagelist_is_uniform(images[chip]) == 0,
2863 CPL_ERROR_INCOMPATIBLE_INPUT,
2864 "Input images are not of same size and type");
2866 passure( cpl_imagelist_get_size(images[chip]) == number_of_frames,
2867 "%" CPL_SIZE_FORMAT
" %" CPL_SIZE_FORMAT
"", cpl_imagelist_get_size(images[0]), number_of_frames);
2873 if ( strcmp(UVES_BIAS (*blue), tag) != 0 &&
2874 strcmp(UVES_DARK (*blue), tag) != 0 &&
2875 strcmp(UVES_PDARK(*blue), tag) != 0) {
2876 enum uves_chip chip_id;
2881 chip_id != UVES_CHIP_INVALID;
2883 for (i = 0; i < number_of_frames; i++) {
2887 "Error reading central wavelength of input frame number %d", i+1);
2894 "Error reading central wavelength of input frame number %d", i+1);
2896 assure( fabs((w-wlen)/wlen) < 0.01, CPL_ERROR_INCOMPATIBLE_INPUT,
2897 "Mis-matching input frame central wavelengths: "
2898 "%e (frame 1) != %e (frame %d)", wlen, w, i+1);
2905 uves_free_image(&temp_image[0]);
2906 uves_free_image(&temp_image[1]);
2907 uves_free_propertylist(&temp_header[0]);
2908 uves_free_propertylist(&temp_header[1]);
2910 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2911 if (raw_headers[0] != NULL) {
2913 for (i = 0; i < frameset_size; i++) {
2914 if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
2915 if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
2918 cpl_free(raw_headers[0]); raw_headers[0] = NULL;
2919 cpl_free(raw_headers[1]); raw_headers[1] = NULL;
2921 uves_free_imagelist(&images[0]);
2922 uves_free_imagelist(&images[1]);
2924 uves_free_propertylist(&rotated_header[0]);
2925 uves_free_propertylist(&rotated_header[1]);
2928 return cpl_error_get_code();
2951 uves_load_orderpos(
const cpl_frameset *frames,
2953 const char **raw_filename,
2954 cpl_image *raw_image[2],
2958 const char *tags[4];
2960 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
2965 tags[0] = UVES_ORDER_FLAT(flames,
false);
2966 tags[1] = UVES_ORDER_FLAT(flames,
true);
2967 tags[2] = UVES_STD_STAR(
false);
2968 tags[3] = UVES_STD_STAR(
true);
2975 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
2977 "Could not find raw frame (%s) in SOF",
2983 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
2985 "Could not find raw frame (%s, %s, %s, or %s) in SOF",
2986 tags[0], tags[1], tags[2], tags[3]);
2988 *blue = (indx == 1) || (indx == 3);
2992 check( load_raw_image(*raw_filename,
2999 "Error loading image from file '%s'", *raw_filename);
3001 passure( !flames || !(*blue),
"%d %d",
3005 if (cpl_error_get_code() != CPL_ERROR_NONE)
3007 *raw_filename = NULL;
3010 return cpl_error_get_code();
3031 uves_load_formatcheck(
const cpl_frameset *frames,
3033 const char **raw_filename,
3034 cpl_image *raw_image[2],
3038 const char *tags[2];
3039 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3042 tags[0] = UVES_FORMATCHECK(flames,
false);
3043 tags[1] = UVES_FORMATCHECK(flames,
true);
3049 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3050 "Could not find raw frame (%s) in SOF",
3055 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3056 "Could not find raw frame (%s or %s) in SOF",
3059 *blue = (indx == 1);
3063 check( load_raw_image(*raw_filename,
3070 "Error loading image from file '%s'", *raw_filename);
3073 if (cpl_error_get_code() != CPL_ERROR_NONE)
3075 *raw_filename = NULL;
3077 return cpl_error_get_code();
3100 void uves_load_cd_align(
const cpl_frameset *frames,
3101 const char **raw_filename1,
3102 const char **raw_filename2,
3103 cpl_image *raw_image1[2],
3104 cpl_image *raw_image2[2],
3111 const char *tags[2];
3112 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3114 bool flames =
false;
3115 const cpl_frame *frame;
3117 tags[0] = UVES_CD_ALIGN(
false);
3118 tags[1] = UVES_CD_ALIGN(
true);
3120 check( *raw_filename1 = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3121 "Could not find raw frame (%s or %s) in SOF",
3124 *blue = (indx == 1);
3126 assure( cpl_frameset_count_tags(frames, tags[indx]) == 2,
3127 CPL_ERROR_ILLEGAL_INPUT,
3128 "%d %s frames found. Exactly 2 required",
3129 cpl_frameset_count_tags(frames, tags[indx]), tags[indx] );
3134 for (frame = cpl_frameset_get_first_const(frames);
3136 frame = cpl_frameset_get_next_const(frames))
3138 if (strcmp(cpl_frame_get_tag(frame), tags[indx]) == 0)
3142 *raw_filename1 = cpl_frame_get_filename(frame);
3146 *raw_filename2 = cpl_frame_get_filename(frame);
3149 check( load_raw_image(n == 1 ?
3164 "Error loading image from file '%s'",
3165 n == 1 ? *raw_filename1 : *raw_filename2);
3173 if (cpl_error_get_code() != CPL_ERROR_NONE)
3175 *raw_filename1 = NULL;
3176 *raw_filename2 = NULL;
3206 uves_load_arclamp(
const cpl_frameset *frames,
3208 const char **raw_filename,
3213 const char *tags[4];
3215 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3223 tags[0] = UVES_ARC_LAMP(flames,
true);
3224 tags[1] = FLAMES_FIB_SCI_SIM;
3229 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3230 "Could not find raw frame (%s or %s) in SOF",
3233 *sim_cal = (indx == 1);
3237 tags[0] = UVES_ARC_LAMP(flames,
true);
3238 tags[1] = UVES_ARC_LAMP(flames,
false);
3239 tags[2] = UVES_ECH_ARC_LAMP(
true);
3240 tags[3] = UVES_ECH_ARC_LAMP(
false);
3242 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3243 "Could not find raw frame (%s, %s, %s or %s) in SOF",
3244 tags[0], tags[1], tags[2], tags[3]);
3246 *blue = (indx == 0 || indx == 2);
3250 check( load_raw_image(*raw_filename,
3257 "Error loading image from file '%s'", *raw_filename);
3260 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3261 *raw_filename = NULL;
3262 uves_free_image (raw_image);
3263 uves_free_propertylist(raw_header);
3285 uves_load_science(
const cpl_frameset *frames,
const char **raw_filename,
3286 cpl_image *raw_image[2],
3290 const char **sci_type)
3293 const char *tags[] =
3295 UVES_SCIENCE(
true), UVES_SCIENCE(
false),
3296 UVES_SCI_EXTND(
true), UVES_SCI_EXTND(
false),
3297 UVES_SCI_POINT(
true), UVES_SCI_POINT(
false),
3298 UVES_SCI_SLICER(
true), UVES_SCI_SLICER(
false),
3299 UVES_TFLAT(
true), UVES_TFLAT(
false)
3302 const char *type[] =
3304 "SCIENCE",
"SCIENCE",
3305 "SCI_EXTND",
"SCI_EXTND",
3306 "SCI_POINT",
"SCI_POINT",
3307 "SCI_SLICER",
"SCI_SLICER",
3311 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3313 bool flames =
false;
3315 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3316 "No science frame (%s, %s, %s, %s, %s, %s, %s, %s, %s or %s) in SOF",
3317 tags[0], tags[1], tags[2], tags[3],
3318 tags[4], tags[5], tags[6], tags[7], tags[7], tags[8]);
3320 *blue = (indx % 2 == 0);
3321 *sci_type = type[indx];
3324 check( load_raw_image(*raw_filename,
3331 "Error loading image from file '%s'", *raw_filename);
3333 if (cpl_error_get_code() != CPL_ERROR_NONE)
3335 *raw_filename = NULL;
3336 uves_free_image (raw_image);
3337 uves_free_propertylist(raw_header);
3339 return cpl_error_get_code();
3361 uves_load_standard(
const cpl_frameset *frames,
const char **raw_filename,
3362 cpl_image *raw_image[2],
3366 const char *tags[] = { UVES_STD_STAR(
true), UVES_STD_STAR(
false) };
3367 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3369 bool flames =
false;
3371 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3372 "Could not identify raw frame (%s or %s) in SOF", tags[0], tags[1]);
3374 *blue = (indx == 0);
3377 check( load_raw_image(*raw_filename,
3384 "Error loading image from file '%s'", *raw_filename);
3387 if (cpl_error_get_code() != CPL_ERROR_NONE)
3389 *raw_filename = NULL;
3390 uves_free_image (raw_image);
3391 uves_free_propertylist(raw_header);
3393 return cpl_error_get_code();
3415 uves_load_drs(
const cpl_frameset *frames,
3417 const char *chip_name,
3418 const char **drs_filename,
3420 enum uves_chip chip)
3422 const char *tags[1];
3423 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3428 tags[0] = UVES_DRS_SETUP(flames, chip);
3429 extension = UVES_DRS_SETUP_EXTENSION(chip);
3431 check( *drs_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3432 "Could not find DRS table (%s) in SOF", tags[0]);
3437 "Could not load header from extension %d of file '%s'", extension, *drs_filename);
3439 check_nomsg( uves_warn_if_chip_names_dont_match(*drs_header, chip_name, chip) );
3442 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3443 *drs_filename = NULL;
3444 uves_free_propertylist(drs_header);
3446 return cpl_error_get_code();
3459 uves_load_weights(
const cpl_frameset *frames,
const char **weights_filename,
3460 enum uves_chip chip)
3462 cpl_image *weights = NULL;
3463 const char *tags[1];
3464 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3468 assure( weights_filename != NULL, CPL_ERROR_NULL_INPUT,
"Null filename");
3470 tags[0] = UVES_WEIGHTS(chip);
3472 check( *weights_filename = uves_find_frame(frames,
3473 tags, number_of_tags, &indx, NULL),
3474 "Could not find '%s' in frame set", tags[0]);
3476 check( weights = cpl_image_load(*weights_filename,
3481 "Could not load master bias from extension %d of file '%s'",
3482 extension, *weights_filename);
3508 uves_load_mbias(
const cpl_frameset *frames,
const char *chip_name,
3509 const char **mbias_filename,
3512 const char *tags[1];
3513 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3518 *mbias_header = NULL;
3520 tags[0] = UVES_MASTER_BIAS (chip);
3521 extension = UVES_MASTER_BIAS_EXTENSION(chip);
3523 check( *mbias_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3524 "Could not find '%s' in frame set", tags[0]);
3527 check( *mbias = cpl_image_load(*mbias_filename,
3532 "Could not load master bias from extension %d of file '%s'",
3533 extension, *mbias_filename);
3538 "Could not load header from extension %d of file '%s'",
3539 extension, *mbias_filename);
3541 check_nomsg( uves_warn_if_chip_names_dont_match(*mbias_header, chip_name, chip) );
3544 if (cpl_error_get_code() != CPL_ERROR_NONE)
3546 *mbias_filename = NULL;
3547 uves_free_image(mbias);
3548 uves_free_propertylist(mbias_header);
3550 return cpl_error_get_code();
3573 uves_load_master_formatcheck(
const cpl_frameset *frames,
const char *chip_name,
3574 const char **mform_filename,
3577 const char *tags[1];
3578 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3583 *mform_header = NULL;
3585 tags[0] = UVES_MASTER_ARC_FORM (chip);
3586 extension = UVES_MASTER_ARC_FORM_EXTENSION(chip);
3588 check( *mform_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3589 "Could not find '%s' in frame set", tags[0]);
3592 check( *mform = cpl_image_load(*mform_filename,
3597 "Could not load master formatcheck from extension %d of file '%s'",
3598 extension, *mform_filename);
3604 "Could not load header from extension %d of file '%s'",
3605 extension, *mform_filename);
3607 check_nomsg( uves_warn_if_chip_names_dont_match(*mform_header, chip_name, chip) );
3610 if (cpl_error_get_code() != CPL_ERROR_NONE)
3612 *mform_filename = NULL;
3613 uves_free_image(mform);
3614 uves_free_propertylist(mform_header);
3616 return cpl_error_get_code();
3638 uves_load_mdark(
const cpl_frameset *frames,
const char *chip_name,
3639 const char **mdark_filename, cpl_image **mdark,
3642 const char *tags[2];
3643 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3648 *mdark_header = NULL;
3650 tags[0] = UVES_MASTER_DARK (chip);
3651 tags[1] = UVES_MASTER_PDARK (chip);
3652 extension = UVES_MASTER_DARK_EXTENSION(chip);
3654 check( *mdark_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3655 "Could not find %s or %s in frame set", tags[0], tags[1]);
3658 check( *mdark = cpl_image_load(*mdark_filename,
3663 "Could not load master dark from extension %d of file '%s'",
3664 extension, *mdark_filename);
3669 "Could not load header from extension %d of file '%s'",
3670 extension, *mdark_filename);
3672 check_nomsg( uves_warn_if_chip_names_dont_match(*mdark_header, chip_name, chip) );
3675 if (cpl_error_get_code() != CPL_ERROR_NONE)
3677 *mdark_filename = NULL;
3678 uves_free_image(mdark);
3679 uves_free_propertylist(mdark_header);
3681 return cpl_error_get_code();
3701 uves_load_ref_flat(
const cpl_frameset *frames,
const char *chip_name,
3702 const char **filename, cpl_image **rflat,
3705 const char *tags[1];
3706 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3711 *rflat_header = NULL;
3713 tags[0] = UVES_REF_TFLAT(chip);
3714 extension = UVES_MASTER_FLAT_EXTENSION(chip);
3716 check( *filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3717 "Could not find %s in frame set", tags[0]);
3719 check( *rflat = cpl_image_load(*filename,
3724 "Could not load reference dark from extension %d of file '%s'",
3725 extension, *filename);
3729 "Could not load header from extension %d of file '%s'",
3730 extension, *filename);
3732 check_nomsg( uves_warn_if_chip_names_dont_match(*rflat_header, chip_name, chip) );
3735 if (cpl_error_get_code() != CPL_ERROR_NONE)
3738 uves_free_image(rflat);
3739 uves_free_propertylist(rflat_header);
3764 uves_load_mflat_const(
const cpl_frameset *frames,
const char *chip_name,
3765 const char **mflat_filename,
3767 enum uves_chip chip,
3768 const cpl_frame **mflat_frame)
3770 const char *tags[6];
3771 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3776 *mflat_header = NULL;
3778 tags[0] = UVES_REF_TFLAT (chip);
3779 tags[1] = UVES_MASTER_FLAT (chip);
3780 tags[2] = UVES_MASTER_DFLAT (chip);
3781 tags[3] = UVES_MASTER_IFLAT (chip);
3782 tags[4] = UVES_MASTER_TFLAT (chip);
3783 tags[5] = UVES_MASTER_SCREEN_FLAT (chip);
3784 extension = UVES_MASTER_FLAT_EXTENSION(chip);
3786 check( *mflat_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
3788 "Could not find '%s', '%s', '%s', '%s' or '%s' in frame set",
3789 tags[0], tags[1], tags[2], tags[3], tags[4]);
3792 check( *mflat = cpl_image_load(*mflat_filename,
3797 "Could not load master flat from extension %d of file '%s'",
3798 extension, *mflat_filename);
3803 "Could not load header from extension %d of file '%s'",
3804 extension, *mflat_filename);
3806 check_nomsg( uves_warn_if_chip_names_dont_match(*mflat_header, chip_name, chip) );
3809 if (cpl_error_get_code() != CPL_ERROR_NONE)
3811 *mflat_filename = NULL;
3812 uves_free_image(mflat);
3813 uves_free_propertylist(mflat_header);
3815 return cpl_error_get_code();
3835 uves_load_mflat(cpl_frameset *frames,
const char *chip_name,
3836 const char **mflat_filename,
3838 cpl_frame **mflat_frame)
3840 return uves_load_mflat_const((
const cpl_frameset *)frames,
3843 mflat, mflat_header, chip,
3844 (
const cpl_frame **) mflat_frame);
3881 uves_load_ordertable(
const cpl_frameset *frames,
3883 const char *chip_name,
3884 const char **ordertable_filename,
3885 cpl_table **ordertable,
3890 int *tab_in_out_oshift,
3891 double *tab_in_out_yshift,
3894 enum uves_chip chip,
3899 const char *tags[1];
3900 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3901 bool format_is_midas;
3903 double *tioy = NULL;
3906 double *fibre_pos = NULL;
3907 int *fibre_mask = NULL;
3911 tags[0] = UVES_GUESS_ORDER_TABLE(flames, chip);
3915 tags[0] = UVES_ORDER_TABLE(flames, chip);
3918 check( *ordertable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3919 "No order table (%s) found in SOF", tags[0]);
3921 check( *ordertable = cpl_table_load(*ordertable_filename,
3922 UVES_ORDER_TABLE_EXTENSION,
3925 "Error loading order table from extension %d of file '%s'",
3926 UVES_ORDER_TABLE_EXTENSION, *ordertable_filename);
3928 assure(ordertable_header != NULL,CPL_ERROR_NULL_INPUT,
3929 "NULL primary header uves_propertylist variable header");
3931 "Could not load header from extension 0 of '%s'", *ordertable_filename);
3933 if(ordertable_xheader != NULL) {
3936 "Could not load header from extension 1 of '%s'", *ordertable_filename);
3941 check_nomsg( uves_warn_if_chip_names_dont_match(*ordertable_header, chip_name, chip) );
3943 check(uves_check_if_format_is_midas(*ordertable_header,&format_is_midas),
3944 "Error getting FITS format");
3947 if (!format_is_midas && !flames)
3958 if (cpl_table_has_column(*ordertable,
"ORDER"))
3960 cpl_table_name_column(*ordertable,
"ORDER",
"Order");
3962 if (cpl_table_has_column(*ordertable,
"YFIT"))
3964 cpl_table_name_column(*ordertable,
"YFIT",
"Yfit");
3967 if (order_locations != NULL)
3969 check( *order_locations =
3970 load_polynomial(*ordertable_filename, UVES_ORDER_TABLE_EXTENSION_POLY),
3971 "Could not read polynomial from extension %d of file '%s'",
3972 UVES_ORDER_TABLE_EXTENSION_POLY, *ordertable_filename);
3977 check( *traces = cpl_table_load(*ordertable_filename,
3978 UVES_ORDER_TABLE_EXTENSION_FIBRE,
3981 "Error loading fibre table from extension %d of file '%s'",
3982 UVES_ORDER_TABLE_EXTENSION_FIBRE, *ordertable_filename);
3989 check(( cpl_table_cast_column (*ordertable,
"ORDER",
"Order", CPL_TYPE_INT),
3990 cpl_table_erase_column(*ordertable,
"ORDER")),
3991 "Error casting and renaming column 'ORDER'");
3993 check( cpl_table_name_column(*ordertable,
"YFIT",
"Yfit"),
3994 "Error renaming column 'YFIT'");
4000 "Could not load header from extension 1 of '%s'",
4001 *ordertable_filename);
4005 "Could not load header from extension 0 of '%s'",
4006 *ordertable_filename);
4011 if (order_locations != NULL)
4013 check( *order_locations =
4014 uves_polynomial_convert_from_plist_midas(midas_header,
"COEFF",-1),
4015 "Error reading polynomial from %s", *ordertable_filename);
4019 if (flames && tab_in_out_oshift != NULL )
4025 check( tioo = uves_read_midas_array(
4026 midas_header,
"TAB_IN_OUT_OSHIFT", &tioo_length,
4028 "Error reading TAB_IN_OUT_OSHIFT from MIDAS header");
4030 assure( tioo_type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4031 "Type of TAB_IN_OUT_OSHIFT is %s, double expected",
4034 if (tioo_length != 1)
4037 "%d expected", tioo_length, 1);
4040 *tab_in_out_oshift = tioo[0];
4046 if (flames && tab_in_out_yshift != NULL)
4052 check( tioy = uves_read_midas_array(
4053 midas_header,
"TAB_IN_OUT_YSHIFT", &tioy_length,
4055 "Error reading TAB_IN_OUT_YSHIFT from MIDAS header");
4057 assure( tioy_type == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
4058 "Type of TAB_IN_OUT_YSHIFT is %s, double expected",
4061 if (tioy_length != 1)
4064 "%d expected", tioy_length, 1);
4067 *tab_in_out_yshift = tioy[0];
4080 double fibre_offset = 0.0;
4091 int fibre_pos_length;
4092 int fibre_mask_length;
4093 cpl_type fibre_pos_type;
4094 cpl_type fibre_mask_type;
4097 check( fibre_pos = uves_read_midas_array(
4098 midas_header,
"FIBREPOS", &fibre_pos_length,
4099 &fibre_pos_type, NULL),
4100 "Error reading FIBREPOS from MIDAS header");
4102 assure( fibre_pos_type == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
4103 "Type of FIBREPOS is %s, double expected",
4106 check( fibre_mask = uves_read_midas_array(
4107 midas_header,
"FIBREMASK", &fibre_mask_length,
4108 &fibre_mask_type, NULL),
4109 "Error reading FIBREMASK from MIDAS header");
4111 assure( fibre_mask_type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4112 "Type of FIBREMASK is %s, double expected",
4115 assure( fibre_pos_length == fibre_mask_length,
4116 CPL_ERROR_INCOMPATIBLE_INPUT,
4117 "FIBREMASK has length %d, but "
4118 "FIBREPOS has length %d",
4119 fibre_mask_length, fibre_pos_length );
4121 *fib_pos= cpl_malloc(
sizeof(
double) * fibre_pos_length);
4122 *fib_msk= cpl_malloc(
sizeof(
int) * fibre_mask_length);
4124 for (fibre_ID = 0; fibre_ID < fibre_mask_length; fibre_ID++)
4127 fibre_ID, fibre_pos[fibre_ID],
4128 fibre_mask[fibre_ID] ?
4129 "enabled" :
"disabled");
4132 fibre_pos[fibre_ID],
4133 fibre_mask[fibre_ID]);
4134 (*fib_pos)[fibre_ID]=fibre_pos[fibre_ID];
4135 (*fib_msk)[fibre_ID]=fibre_mask[fibre_ID];
4142 uves_free_propertylist(&midas_header);
4143 uves_free_double(&fibre_pos);
4144 uves_free_int(&fibre_mask);
4145 uves_free_int(&tioo);
4146 uves_free_double(&tioy);
4147 uves_free_propertylist(&prime_header);
4149 if (cpl_error_get_code() != CPL_ERROR_NONE)
4151 *ordertable_filename = NULL;
4152 uves_free_table (ordertable);
4153 uves_free_propertylist(ordertable_header);
4155 if (traces != NULL) uves_free_table (traces);
4157 return cpl_error_get_code();
4182 const char* drs_id=NULL;
4185 if (strstr(drs_id,
"CPL") != NULL ||
4186 strstr(drs_id,
"cpl") != NULL) {
4187 *format_is_midas =
false;
4189 }
else if (strstr(drs_id,
"MIDAS") != NULL ||
4190 strstr(drs_id,
"midas") != NULL) {
4191 *format_is_midas =
true;
4194 assure (
false, CPL_ERROR_ILLEGAL_INPUT,
4195 "Unrecognized order table format, DRS_ID = '%s'", drs_id);
4199 *format_is_midas =
true;
4200 uves_msg_debug(
"No '%s' keyword found. Assuming MIDAS format", UVES_DRS_ID);
4204 return cpl_error_get_code();
4220 static cpl_error_code
4221 create_column_pixelsize(cpl_table *linetable)
4224 cpl_table *t = NULL;
4230 check( t = uves_extract_table_rows(linetable,
"Ident", CPL_GREATER_THAN, 0.1),
4231 "Error deleting rows with Ident=0");
4234 check(( cpl_table_duplicate_column(t,
"Aux", t,
"Ident"),
4235 cpl_table_multiply_columns(t,
"Aux",
"Order")),
4236 "Error creating 'Aux' column");
4244 "Regression failed");
4247 "Error reading polynomial coefficient");
4250 "Error reading polynomial coefficient");
4252 cpl_table_new_column(linetable, LINETAB_PIXELSIZE, CPL_TYPE_DOUBLE);
4254 for (i = 0; i < cpl_table_get_nrow(linetable); i++)
4261 check(( x = cpl_table_get_double(linetable,
"X", i, NULL),
4262 order = cpl_table_get_int (linetable,
"Order", i, NULL),
4263 ident = cpl_table_get_double(linetable,
"Ident", i, NULL)),
4264 "Error reading line table");
4266 assure( order != 0, CPL_ERROR_ILLEGAL_INPUT,
"Illegal order number: %d", order);
4274 pixelsize = (d1 + 2*d2* x) / order;
4279 cpl_table_set_double(linetable, LINETAB_PIXELSIZE, i, pixelsize);
4283 cpl_table_set_invalid(linetable, LINETAB_PIXELSIZE, i);
4288 uves_free_table(&t);
4290 return cpl_error_get_code();
4324 align_order_line_table(cpl_table *linetable,
const polynomial *absolute_order,
4326 const polynomial *order_locations,
int minorder,
int maxorder)
4330 assure ( order_locations != NULL, CPL_ERROR_NULL_INPUT,
4331 "Null order locations polynomial!");
4333 assure ( absolute_order != NULL, CPL_ERROR_NULL_INPUT,
4334 "Null absolute order pllynomial!");
4335 assure( cpl_table_has_column(linetable,
"X" ), CPL_ERROR_DATA_NOT_FOUND,
4336 "Missing line table column 'X'");
4337 assure( cpl_table_has_column(linetable,
"Ynew"), CPL_ERROR_DATA_NOT_FOUND,
4338 "Missing line table column 'Ynew'");
4339 assure( cpl_table_has_column(linetable,
"Order"), CPL_ERROR_DATA_NOT_FOUND,
4340 "Missing line table column 'Order'");
4342 assure( cpl_table_get_column_type(linetable,
"X") == CPL_TYPE_DOUBLE,
4343 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'X' has type %s (double expected))",
4346 assure( cpl_table_get_column_type(linetable,
"Ynew") == CPL_TYPE_DOUBLE,
4347 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'Ynew' has type %s (double expected))",
4350 assure( cpl_table_get_column_type(linetable,
"Y") == CPL_TYPE_INT,
4351 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'Y' has type %s (integer expected))",
4355 if (linetable_header != NULL)
4358 int line_first, line_last;
4359 int ord_first, ord_last;
4364 int x, y, order, absorder;
4368 maxx = uves_round_double(cpl_table_get_column_max(linetable,
"X"));
4370 minx = uves_round_double(cpl_table_get_column_min(linetable,
"X"));
4372 assure( 1 <= minx && minx <= maxx, CPL_ERROR_ILLEGAL_INPUT,
4373 "Illegal min/max line x positions: %d/%d, must be > 1",
4377 x = (minx + maxx) / 2;
4378 order = (minorder + maxorder) / 2;
4388 assure ( order_locations != NULL, CPL_ERROR_NULL_INPUT,
4389 "Null order locations polynomial!");
4396 "rounding to %d", x, y,
4399 ord_first = absorder + (minorder - order) * coeff;
4400 ord_last = absorder + (maxorder - order) * coeff;
4405 "Could not read order number from line table header");
4409 "Could not read order number from line table header");
4411 uves_msg_debug(
"Order table range: %d - %d. Line table range: %d - %d",
4412 ord_first, ord_last, line_first, line_last);
4414 if (line_first != ord_first ||
4415 line_last != ord_last)
4418 "Line table contains orders %d - %d. "
4419 "Order table contains orders %d - %d. "
4420 "Correcting on the fly",
4421 line_first, line_last, ord_first, ord_last);
4425 "Could not write corrected first absolute order number");
4428 "Could not write corrected first absolute order number");
4431 ord_first, ord_last);
4440 double epsilon = 0.01;
4445 if (fabs(cpl_table_get_column_median(linetable,
"Y") -
4446 cpl_table_get_column_median(linetable,
"Order")) > epsilon)
4458 cpl_table_erase_column(linetable,
"Y");
4508 uves_load_linetable(
const cpl_frameset *frames,
4510 const char *chip_name,
4511 const polynomial *order_locations,
int minorder,
int maxorder,
4512 const char **linetable_filename,
4513 cpl_table **linetable,
4517 enum uves_chip chip,
int trace_id,
int window)
4522 int *absorders = NULL;
4523 cpl_table *temp = NULL;
4525 const char *tags[3];
4526 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
4528 bool format_is_midas;
4535 tags[0] = UVES_GUESS_LINE_TABLE(flames, chip);
4536 tags[1] = UVES_LINE_TABLE(flames, chip);
4537 tags[2] = UVES_LINE_TABLE(flames, chip);
4540 check( *linetable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
4541 "No line table (%s, %s or %s) found in SOF", tags[0], tags[1], tags[2]);
4545 tags[0] = UVES_LINE_TABLE(flames, chip);
4546 tags[1] = UVES_LINE_TABLE(flames, chip);
4547 tags[2] = UVES_GUESS_LINE_TABLE(flames, chip);
4551 if (cpl_frameset_find_const(frames, tags[0]) == NULL &&
4552 cpl_frameset_find_const(frames, tags[1]) == NULL &&
4553 cpl_frameset_find_const(frames, tags[2]) == NULL)
4561 tags[0] = UVES_LINE_TABLE_MIDAS(chip, window);
4562 tags[1] = UVES_LINE_TABLE_MIDAS(chip, window);
4563 tags[2] = UVES_LINE_TABLE_MIDAS(chip, window);
4570 tags[0] = UVES_LINE_TABLE_MIDAS(chip, 1);
4571 tags[1] = UVES_LINE_TABLE_MIDAS(chip, 2);
4572 tags[2] = UVES_LINE_TABLE_MIDAS(chip, 3);
4574 uves_msg_debug(
"Trying %s, %s or %s", tags[0], tags[1], tags[2]);
4578 check( *linetable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
4579 "No line table (%s, %s or %s) found in SOF", tags[0], tags[1], tags[2]);
4584 "Could not load primary header of '%s'", *linetable_filename);
4586 check_nomsg( uves_warn_if_chip_names_dont_match(primary_header, chip_name, chip) );
4592 if (strstr(drs_id,
"CPL") != NULL || strstr(drs_id,
"cpl") != NULL)
4594 format_is_midas =
false;
4597 else if (strstr(drs_id,
"MIDAS") != NULL || strstr(drs_id,
"midas") != NULL)
4599 format_is_midas =
true;
4605 CPL_ERROR_ILLEGAL_INPUT,
4606 "Unrecognized line table format, DRS_ID = '%s'", drs_id);
4611 format_is_midas =
true;
4612 uves_msg_debug(
"No '%s' keyword found. Assuming MIDAS format", UVES_DRS_ID);
4615 if (format_is_midas || flames)
4619 assure( trace_id == 0 && (window == -1 || (1 <= window && window <= 3)),
4620 CPL_ERROR_UNSUPPORTED_MODE,
4621 "Cannot read (fibre, window) = (%d, %d) from MIDAS line table",
4631 assure( ((1<= trace_id && trace_id <= 9) && (window == -1)),
4632 CPL_ERROR_UNSUPPORTED_MODE,
4633 "Cannot read (fibre, window) = (%d, %d) from MIDAS line table",
4652 check( nextensions = uves_get_nextensions(*linetable_filename),
4653 "Error reading number of extensions of file '%s'", *linetable_filename);
4659 for (base_extension = 1; base_extension < nextensions && !found; base_extension++)
4665 check(( uves_free_propertylist(&header),
4667 "Could not header of extension %d of '%s'",
4668 base_extension, *linetable_filename);
4671 "Error reading trace ID from header of extension %d of '%s'",
4672 base_extension, *linetable_filename);
4675 "Error reading window number from header of extension %d of '%s'",
4676 base_extension, *linetable_filename);
4678 uves_msg_debug(
"Found (trace, window) = (%d, %d), need (%d, %d)",
4679 header_trace, header_window,
4682 found = ( (trace_id == header_trace) &&
4683 (window == -1 || window == header_window) );
4687 CPL_ERROR_ILLEGAL_INPUT,
4688 "Line table (trace, window) = (%d, %d) is not present in file '%s'",
4689 trace_id, window, *linetable_filename);
4693 base_extension -= 2;
4697 check( *linetable = cpl_table_load(*linetable_filename,
4698 base_extension + UVES_LINE_TABLE_EXTENSION,
4701 "Error loading line table from extension %d of file '%s'",
4702 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4705 if (linetable_header != NULL)
4707 check( *linetable_header =
4709 base_extension + UVES_LINE_TABLE_EXTENSION),
4710 "Could not load header of extension %d of '%s'",
4711 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4713 if (format_is_midas)
4717 absorders = uves_read_midas_array(*linetable_header,
"ORDER", &size,
4720 assure( type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4721 "Type of ORDER is %s, int expected",
4725 CPL_ERROR_ILLEGAL_INPUT,
4726 "'ORDER' array has size %d. Size 2 expected.", size);
4729 "Error updating table header");
4734 if (format_is_midas)
4737 check(( cpl_table_cast_column(*linetable,
"X",
"xxxx", CPL_TYPE_DOUBLE),
4738 cpl_table_erase_column(*linetable,
"X"),
4739 cpl_table_name_column(*linetable,
"xxxx",
"X")),
4740 "Error casting and renaming column 'X'");
4742 check(( cpl_table_cast_column(*linetable,
"YNEW",
"xxxx", CPL_TYPE_DOUBLE),
4743 cpl_table_erase_column(*linetable,
"YNEW"),
4744 cpl_table_name_column(*linetable,
"xxxx",
"Ynew")),
4745 "Error casting and renaming column 'YNEW'");
4747 check(( cpl_table_cast_column(*linetable,
"Y",
"xxxx", CPL_TYPE_INT),
4748 cpl_table_erase_column(*linetable,
"Y"),
4749 cpl_table_name_column(*linetable,
"xxxx",
"Y")),
4750 "Error casting and renaming column 'Y'");
4752 check(( cpl_table_cast_column(*linetable,
"ORDER",
"Order", CPL_TYPE_INT),
4753 cpl_table_erase_column(*linetable,
"ORDER")),
4754 "Error casting and renaming column 'ORDER'");
4756 check( cpl_table_name_column(*linetable,
"IDENT",
"Ident"),
4757 "Error renaming column 'IDENT'");
4760 *linetable_filename,
4761 base_extension + UVES_LINE_TABLE_EXTENSION),
4762 "Could not load header of extension %d of '%s'",
4763 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4765 if (dispersion_relation != NULL) {
4767 check( *dispersion_relation =
4768 uves_polynomial_convert_from_plist_midas(midas_header,
4770 "Error reading polynomial 'REGR%d' from '%s'",
4772 *linetable_filename);
4775 check( *dispersion_relation =
4776 uves_polynomial_convert_from_plist_midas(midas_header,
4778 "Error reading polynomial 'REGR' from '%s'",
4779 *linetable_filename);
4784 check( absolute_order_local =
4785 uves_polynomial_convert_from_plist_midas(midas_header,
"RORD",-1),
4786 "Error reading polynomial 'RORD' from '%s'", *linetable_filename);
4801 if (cpl_table_has_column(*linetable,
"YNEW"))
4803 cpl_table_name_column(*linetable,
"YNEW",
"Ynew");
4806 if (dispersion_relation != NULL)
4808 check( *dispersion_relation = load_polynomial(
4809 *linetable_filename,
4810 base_extension + UVES_LINE_TABLE_EXTENSION_DISPERSION),
4811 "Could not read polynomial from extension %d of file '%s'",
4812 base_extension + UVES_LINE_TABLE_EXTENSION_DISPERSION,
4813 *linetable_filename);
4816 check( absolute_order_local =
4817 load_polynomial(*linetable_filename,
4818 base_extension + UVES_LINE_TABLE_EXTENSION_ABSORDER),
4819 "Could not read polynomial from extension %d of file '%s'",
4820 base_extension + UVES_LINE_TABLE_EXTENSION_ABSORDER, *linetable_filename);
4823 if (absolute_order != NULL)
4829 check( align_order_line_table(
4830 *linetable, absolute_order_local, linetable_header,
4831 order_locations, minorder, maxorder),
4832 "Error while aligning line/order tables");
4837 const char *colname;
4845 uves_free_table(&temp);
4846 check(( temp = cpl_table_new(0),
4847 cpl_table_copy_structure(temp, *linetable)),
4848 "Error duplicating line table column structure");
4850 colname = cpl_table_get_column_name(temp);
4851 while (colname != NULL)
4853 if (!(strcmp(colname,
"X" ) == 0 ||
4854 strcmp(colname,
"Order" ) == 0 ||
4855 strcmp(colname,
"Ident" ) == 0 ||
4856 strcmp(colname,
"FIBRE" ) == 0 ||
4857 strcmp(colname,
"Fibre" ) == 0 ||
4858 strcmp(colname, LINETAB_PIXELSIZE) == 0))
4860 cpl_table_erase_column(*linetable, colname);
4865 colname = cpl_table_get_column_name(NULL);
4872 if ( !cpl_table_has_column(*linetable, LINETAB_PIXELSIZE) )
4874 check( create_column_pixelsize(*linetable),
4875 "Error adding 'Pixelsize' column");
4879 check( uves_erase_invalid_table_rows(*linetable,
"Ident"),
4880 "Error deleting rows with illegal 'Ident' value");
4882 check( uves_erase_table_rows(*linetable,
"Ident", CPL_LESS_THAN, 0.01),
4883 "Error deleting rows with illegal 'Ident' value");
4886 assure( uves_erase_invalid_table_rows(*linetable, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
4887 "After deleting rows with invalid 'Ident' values, "
4888 "the table in extension %d of file '%s' still contains invalid rows",
4889 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4892 check( uves_sort_table_2(*linetable,
"Order",
"X",
false,
false),
"Error sorting line table");
4895 uves_free_propertylist(&primary_header);
4896 uves_free_propertylist(&header);
4897 uves_free_propertylist(&midas_header);
4898 uves_free_table(&temp);
4900 cpl_free(absorders);
4901 if (cpl_error_get_code() != CPL_ERROR_NONE) {
4902 *linetable_filename = NULL;
4903 uves_free_table(linetable);
4916 uves_load_linetable_const(
const cpl_frameset *frames,
4918 const char *chip_name,
4919 const polynomial *order_locations,
int minorder,
int maxorder,
4920 const char **linetable_filename,
4921 const cpl_table **linetable,
4925 enum uves_chip chip,
int trace_id,
int window)
4927 uves_load_linetable(frames, flames, chip_name, order_locations,
4930 (cpl_table **)linetable,
4934 chip, trace_id, window);
4957 uves_load_response_curve(
const cpl_frameset *frames,
const char *chip_name,
4958 const char **response_filename,
4959 cpl_image **response_curve,
4960 cpl_table **master_response,
4963 const char *tags[2];
4964 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
4968 *response_curve = NULL;
4969 *response_header = NULL;
4970 *master_response = NULL;
4972 tags[0] = UVES_INSTR_RESPONSE (chip);
4973 tags[1] = UVES_MASTER_RESPONSE(chip);
4975 check( *response_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
4977 "Could not find '%s' in frame set", tags[0]);
4982 extension = UVES_INSTR_RESPONSE_EXTENSION(chip);
4991 check( *response_curve = uves_load_image_file(*response_filename,
4998 "Could not load response curve from extension %d of file '%s'",
4999 extension, *response_filename);
5008 check_nomsg( uves_warn_if_chip_names_dont_match(*response_header, chip_name, chip) );
5013 extension = UVES_MASTER_RESPONSE_EXTENSION(chip);
5015 check( *master_response = cpl_table_load(*response_filename,
5016 UVES_LINE_INTMON_TABLE_EXTENSION,
5019 "Error master response curve from extension %d of file '%s'",
5020 extension, *response_filename);
5023 check(( cpl_table_cast_column(*master_response,
"LAMBDA",
"LAMBDA_double",
5025 cpl_table_erase_column(*master_response,
"LAMBDA"),
5026 cpl_table_name_column(*master_response,
"LAMBDA_double",
"LAMBDA")),
5027 "Could not cast column 'LAMBDA'");
5029 check(( cpl_table_cast_column(*master_response,
"FLUX_CONV",
"FLUX_CONV_double",
5031 cpl_table_erase_column(*master_response,
"FLUX_CONV"),
5032 cpl_table_name_column(*master_response,
"FLUX_CONV_double",
"FLUX_CONV")),
5033 "Could not cast column 'FLUX_CONV'");
5040 if (cpl_error_get_code() != CPL_ERROR_NONE)
5042 *response_filename = NULL;
5043 uves_free_image(response_curve);
5044 uves_free_propertylist(response_header);
5046 return cpl_error_get_code();
5061 cpl_error_code uves_load_lineintmon(
const cpl_frameset *frames,
5062 const char **line_intmon_filename,
5063 cpl_table **line_intmon)
5065 const char *tags[1] = {UVES_LINE_INTMON_TABLE};
5067 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5071 check( *line_intmon_filename = uves_find_frame(frames, tags, number_of_tags,
5073 "No line intensity table (%s) found in SOF", tags[0]);
5076 check( *line_intmon = cpl_table_load(*line_intmon_filename,
5077 UVES_LINE_INTMON_TABLE_EXTENSION,
5080 "Error loading line reference table from extension %d of file '%s'",
5081 UVES_LINE_INTMON_TABLE_EXTENSION, *line_intmon_filename);
5083 check(( cpl_table_cast_column(*line_intmon,
"WAVE",
"Wave", CPL_TYPE_DOUBLE),
5084 cpl_table_erase_column(*line_intmon,
"WAVE")),
5085 "Could not cast and rename column");
5088 check( uves_sort_table_1(*line_intmon,
"Wave",
false),
"Error sorting table");
5091 if (cpl_error_get_code() != CPL_ERROR_NONE)
5093 *line_intmon_filename = NULL;
5094 uves_free_table(line_intmon);
5096 return cpl_error_get_code();
5114 uves_load_corvel(
const cpl_frameset *frames,
5117 const char **corvel_filename)
5119 const char *tags[1];
5120 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5124 tags[0] = FLAMES_CORVEL_MASK;
5127 assure_nomsg( corvel_filename != NULL, CPL_ERROR_NULL_INPUT );
5130 check( *corvel_filename = uves_find_frame(frames, tags, number_of_tags,
5132 "No velocity correction table (%s) found in SOF", tags[0]);
5136 check( *corvel = cpl_table_load(*corvel_filename,
5140 "Error loading line reference table from extension %d of file '%s'",
5141 extension, *corvel_filename);
5144 if (corvel_header != NULL)
5149 "Could not load header from extension %d of file %s",
5150 extension, *corvel_filename);
5155 if (cpl_error_get_code() != CPL_ERROR_NONE)
5157 *corvel_filename = NULL;
5158 uves_free_table(corvel);
5181 uves_load_linerefertable(
const cpl_frameset *frames,
5182 const char **line_refer_filename,
5185 const char *tags[1] = {UVES_LINE_REFER_TABLE};
5187 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5191 check( *line_refer_filename = uves_find_frame(frames, tags, number_of_tags,
5193 "No line reference table (%s) found in SOF", tags[0]);
5196 check( *line_refer = cpl_table_load(*line_refer_filename,
5197 UVES_LINE_REFER_TABLE_EXTENSION,
5200 "Error loading line reference table from extension %d of file '%s'",
5201 UVES_LINE_REFER_TABLE_EXTENSION, *line_refer_filename);
5204 if (line_refer_header != NULL)
5207 "Could not load header of line_refer table in '%s'", *line_refer_filename);
5210 assure( uves_erase_invalid_table_rows(*line_refer, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
5211 "Table in extension %d of file '%s' contains invalid rows",
5212 UVES_LINE_REFER_TABLE_EXTENSION, *line_refer_filename);
5214 check(( cpl_table_cast_column(*line_refer,
"WAVE",
"Wave", CPL_TYPE_DOUBLE),
5215 cpl_table_erase_column(*line_refer,
"WAVE")),
5216 "Could not cast and rename column");
5229 check(( cpl_table_duplicate_column(*line_refer,
"dWave", *line_refer,
"Wave"),
5230 cpl_table_divide_scalar (*line_refer,
"dWave", 300000*10)),
5231 "Error writing wavelength uncertainties");
5234 check(( cpl_table_new_column(*line_refer,
"dWave", CPL_TYPE_DOUBLE),
5235 cpl_table_fill_column_window(*line_refer,
5238 cpl_table_get_nrow(*line_refer), 0.002)),
5239 "Error writing wavelength uncertainties");
5243 check( uves_sort_table_1(*line_refer,
"Wave",
false),
"Error sorting table");
5246 if (cpl_error_get_code() != CPL_ERROR_NONE) {
5247 *line_refer_filename = NULL;
5248 uves_free_table (line_refer);
5249 if (line_refer_header != NULL) uves_free_propertylist(line_refer_header);
5251 return cpl_error_get_code();
5270 uves_load_flux_table(
const cpl_frameset *frames,
const char **flux_table_filename,
5271 cpl_table **flux_table)
5273 const char *tags[1] = {UVES_FLUX_STD_TABLE};
5275 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5279 check( *flux_table_filename = uves_find_frame(frames, tags, number_of_tags,
5281 "No standard star flux table (%s) in SOF", tags[0]);
5284 check( *flux_table = cpl_table_load(*flux_table_filename,
5285 UVES_FLUX_STD_TABLE_EXTENSION,
5288 "Error loading flux table from extension %d of file '%s'",
5289 UVES_FLUX_STD_TABLE_EXTENSION, *flux_table_filename);
5296 if (uves_erase_invalid_table_rows(*flux_table, NULL) != 0)
5298 uves_msg_warning(
"Table in extension %d of file '%s' contains null values",
5299 UVES_FLUX_STD_TABLE_EXTENSION, *flux_table_filename);
5305 for (i = 0; i < cpl_table_get_nrow(*flux_table); i++)
5307 if (cpl_table_get_string(*flux_table,
"TYPE", i) == NULL)
5309 cpl_table_set_string(*flux_table,
"TYPE", i,
"NULL");
5316 if (cpl_error_get_code() != CPL_ERROR_NONE)
5318 *flux_table_filename = NULL;
5319 uves_free_table(flux_table);
5321 return cpl_error_get_code();
5341 uves_load_atmo_ext(
const cpl_frameset *frames,
const char **atmext_table_filename,
5342 cpl_table **atmext_table)
5344 const char *tags[1] = {UVES_EXTCOEFF_TABLE};
5346 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5350 check( *atmext_table_filename = uves_find_frame(frames, tags, number_of_tags,
5352 "No atmospheric extinction table (%s) found in SOF", tags[0]);
5355 check( *atmext_table = cpl_table_load(*atmext_table_filename,
5356 UVES_EXTCOEFF_TABLE_EXTENSION,
5359 "Error loading atmospheric extinction table from extension %d of file '%s'",
5360 UVES_EXTCOEFF_TABLE_EXTENSION, *atmext_table_filename);
5362 assure( uves_erase_invalid_table_rows(*atmext_table, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
5363 "Table in extension %d of file '%s' contains invalid rows",
5364 UVES_EXTCOEFF_TABLE_EXTENSION, *atmext_table_filename);
5366 check( uves_sort_table_1(*atmext_table,
"LAMBDA",
false),
5367 "Error sorting table");
5370 check(( cpl_table_cast_column(*atmext_table,
"LAMBDA",
"LAMBDA_double", CPL_TYPE_DOUBLE),
5371 cpl_table_erase_column(*atmext_table,
"LAMBDA"),
5372 cpl_table_name_column(*atmext_table,
"LAMBDA_double",
"LAMBDA")),
5373 "Could not cast column 'LAMBDA'");
5375 check(( cpl_table_cast_column(*atmext_table,
"LA_SILLA",
"LA_SILLA_double", CPL_TYPE_DOUBLE),
5376 cpl_table_erase_column(*atmext_table,
"LA_SILLA"),
5377 cpl_table_name_column(*atmext_table,
"LA_SILLA_double",
"LA_SILLA")),
5378 "Could not cast column 'LA_SILLA'");
5381 if (cpl_error_get_code() != CPL_ERROR_NONE)
5383 *atmext_table_filename = NULL;
5384 uves_free_table(atmext_table);
5386 return cpl_error_get_code();
5398 uves_guess_order_table_filename(
enum uves_chip chip)
5400 return uves_local_filename(
"orderguesstable", chip, -1, -1);
5413 uves_order_table_filename(
enum uves_chip chip)
5415 return uves_local_filename(
"ordertable", chip, -1, -1);
5426 char *uves_ordef_filename(
enum uves_chip chip)
5428 return uves_local_filename(
"order_def", chip, -1, -1);
5441 uves_masterdark_filename(
enum uves_chip chip)
5443 return uves_local_filename(
"masterdark", chip, -1, -1);
5455 uves_flat_ratio_filename(
enum uves_chip chip)
5457 return uves_local_filename(
"ratio", chip, -1, -1);
5468 char *uves_cd_align_filename(
enum uves_chip chip)
5470 return uves_local_filename(
"cd_align", chip, -1, -1);
5483 uves_masterflat_filename(
enum uves_chip chip)
5485 return uves_local_filename(
"masterflat", chip, -1, -1);
5497 uves_masterflat_bkg_filename(
enum uves_chip chip)
5499 return uves_local_filename(
"masterflat_bkg", chip, -1, -1);
5512 uves_masterbias_filename(
enum uves_chip chip)
5514 return uves_local_filename(
"masterbias", chip, -1, -1);
5527 uves_guess_line_table_filename(
enum uves_chip chip)
5529 return uves_local_filename(
"lineguesstable", chip, -1, -1);
5541 uves_line_table_filename(
enum uves_chip chip)
5543 return uves_local_filename(
"linetable", chip, -1, -1);
5556 uves_line_table_filename_paf(
enum uves_chip chip)
5558 return uves_local_filename(
"linetable_paf", chip, -1, -1);
5571 uves_response_curve_filename(
enum uves_chip chip)
5573 return uves_local_filename(
"response", chip, -1, -1);
5586 uves_response_curve_2d_filename(
enum uves_chip chip)
5588 return uves_local_filename(
"response_2d", chip, -1, -1);
5601 uves_response_red_standard_filename(
enum uves_chip chip)
5603 return uves_local_filename(
"red_std", chip, -1, -1);
5617 uves_response_red_noappend_standard_filename(
enum uves_chip chip)
5619 return uves_local_filename(
"red_nonmerged", chip, -1, -1);
5632 uves_response_bkg_standard_filename(
enum uves_chip chip)
5634 return uves_local_filename(
"bkg_std", chip, -1, -1);
5648 uves_order_extract_qc_standard_filename(
enum uves_chip chip)
5650 return uves_local_filename(
"order_extract_qc", chip, -1, -1);
5663 uves_response_efficiency_filename(
enum uves_chip chip)
5665 return uves_local_filename(
"efficiency", chip, -1, -1);
5679 uves_scired_red_2d_science_filename(
enum uves_chip chip)
5681 return uves_local_filename(
"red_2d_science", chip, -1, -1);
5696 uves_scired_red_science_filename(
enum uves_chip chip)
5698 return uves_local_filename(
"red_science", chip, -1, -1);
5712 uves_scired_red_noappend_science_filename(
enum uves_chip chip)
5714 return uves_local_filename(
"red_nonmerged_science", chip, -1, -1);
5726 uves_scired_red_error_filename(
enum uves_chip chip)
5728 return uves_local_filename(
"error_red_science", chip, -1, -1);
5741 uves_scired_red_noappend_error_filename(
enum uves_chip chip)
5743 return uves_local_filename(
"error_red_nonmerged_science", chip, -1, -1);
5756 uves_scired_red_2d_error_filename(
enum uves_chip chip)
5758 return uves_local_filename(
"error_2d_science", chip, -1, -1);
5772 uves_scired_fluxcal_science_filename(
enum uves_chip chip)
5774 return uves_local_filename(
"fluxcal_science", chip, -1, -1);
5788 uves_scired_fluxcal_science_noappend_filename(
enum uves_chip chip)
5790 return uves_local_filename(
"fluxcal_nonmerged_science", chip, -1, -1);
5802 uves_scired_fluxcal_error_filename(
enum uves_chip chip)
5804 return uves_local_filename(
"fluxcal_error_science", chip, -1, -1);
5818 uves_scired_fluxcal_error_noappend_filename(
enum uves_chip chip)
5820 return uves_local_filename(
"fluxcal_error_nonmerged_science", chip, -1, -1);
5834 uves_scired_fluxcal_science_2d_filename(
enum uves_chip chip)
5836 return uves_local_filename(
"fluxcal_2d_science", chip, -1, -1);
5848 uves_scired_fluxcal_error_2d_filename(
enum uves_chip chip)
5850 return uves_local_filename(
"fluxcal_error_2d_science", chip, -1, -1);
5862 uves_scired_ff_variance_filename(
enum uves_chip chip)
5864 return uves_local_filename(
"variance_ff_science", chip, -1, -1);
5877 uves_scired_ff_variance_2d_filename(
enum uves_chip chip)
5879 return uves_local_filename(
"variance_ff_2d_science", chip, -1, -1);
5891 uves_scired_merged_2d_science_filename(
enum uves_chip chip)
5893 return uves_local_filename(
"merged_2d_science", chip, -1, -1);
5905 uves_scired_merged_science_filename(
enum uves_chip chip)
5907 return uves_local_filename(
"merged_science", chip, -1, -1);
5919 uves_scired_merged_sky_filename(
enum uves_chip chip)
5921 return uves_local_filename(
"merged_sky", chip, -1, -1);
5934 uves_scired_background_filename(
enum uves_chip chip)
5936 return uves_local_filename(
"background", chip, -1, -1);
5949 uves_scired_resampled_filename(
enum uves_chip chip)
5951 return uves_local_filename(
"resampled_science", chip, -1, -1);
5966 uves_scired_resampled_2d_filename(
enum uves_chip chip)
5968 return uves_local_filename(
"resampled_2d_science", chip, -1, -1);
5982 uves_scired_resampledmf_filename(
enum uves_chip chip)
5984 return uves_local_filename(
"resampled_mflat", chip, -1, -1);
5998 uves_scired_rebinned_filename(
enum uves_chip chip)
6000 return uves_local_filename(
"resampled_ff_science", chip, -1, -1);
6014 uves_scired_rebinned_2d_filename(
enum uves_chip chip)
6016 return uves_local_filename(
"resampled_ff_2d_science", chip, -1, -1);
6028 uves_scired_ordertrace_filename(
enum uves_chip chip)
6030 return uves_local_filename(
"ordertrace", chip, -1, -1);
6043 uves_scired_crmask_filename(
enum uves_chip chip)
6045 return uves_local_filename(
"cr_mask", chip, -1, -1);
6058 uves_scired_wmap_filename(
enum uves_chip chip)
6060 return uves_local_filename(
"wave_map", chip, -1, -1);
6072 char *uves_scired_ext2d_filename(
enum uves_chip chip)
6074 return uves_local_filename(
"ext_2d_science", chip, -1, -1);
6086 char *uves_scired_ff2d_filename(
enum uves_chip chip)
6088 return uves_local_filename(
"ff_2d_science", chip, -1, -1);
6114 uves_local_filename(
const char *prefix,
enum uves_chip chip,
int trace,
int window)
6116 char *result = NULL;
6117 const char *chip_string;
6118 const char *suffix =
".fits";
6122 assure( (trace < 0 && window < 0) ||
6123 (trace < 0 && window > 0) ||
6124 (trace >= 0 && window > 0),
6125 CPL_ERROR_ILLEGAL_INPUT,
"Illegal trace and window numbers: (%d, %d)",
6132 check(( t = int_to_string(trace),
6133 w = int_to_string(window)),
6134 "Error creating substrings");
6150 result = uves_sprintf(
"%s_%s%s%s%s", prefix, chip_string, t, w, suffix);
6156 if (cpl_error_get_code() != CPL_ERROR_NONE)
6158 cpl_free(result); result = NULL;
6176 int_to_string(
int i)
6178 char *result = NULL;
6180 assure( -1 <= i, CPL_ERROR_ILLEGAL_INPUT,
"Illegal number (%d)", i);
6185 result = cpl_calloc(1,
sizeof(
char));
6190 result = uves_sprintf(
"_%d", i);
6194 if (cpl_error_get_code() != CPL_ERROR_NONE){
6195 cpl_free(result); result = NULL;
6214 uves_vector_to_image(
const cpl_vector* vector,cpl_type type)
6217 cpl_image* image=NULL;
6219 const double* pv=NULL;
6225 size=cpl_vector_get_size(vector);
6226 image=cpl_image_new(size,1,type);
6227 pv=cpl_vector_get_data_const(vector);
6228 if(type == CPL_TYPE_INT) {
6229 pi=cpl_image_get_data_int(image);
6230 for(i=0;i<size;i++) {
6233 }
else if (type == CPL_TYPE_FLOAT) {
6234 pf=cpl_image_get_data_float(image);
6235 for(i=0;i<size;i++) {
6238 }
else if (type == CPL_TYPE_DOUBLE) {
6239 pd=cpl_image_get_data_double(image);
6240 for(i=0;i<size;i++) {
6244 assure(
false, CPL_ERROR_INVALID_TYPE,
6245 "No CPL type to represent BITPIX = %d", type);
6249 if (cpl_error_get_code() != CPL_ERROR_NONE){
6250 uves_free_image(&image);