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";
154 if (frameset == NULL) {
155 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
159 if (cpl_frameset_is_empty(frameset)) {
163 cpl_frameset_iterator* it = cpl_frameset_iterator_new(frameset);
164 cpl_frame*
frame = cpl_frameset_iterator_get(it);
167 if (access(cpl_frame_get_filename(frame), F_OK)) {
168 cpl_msg_error(func,
"File %s (%s) was not found",
169 cpl_frame_get_filename(frame),
170 cpl_frame_get_tag(frame));
171 cpl_error_set(func, CPL_ERROR_FILE_NOT_FOUND);
174 cpl_frameset_iterator_advance(it, 1);
175 frame = cpl_frameset_iterator_get(it);
178 cpl_frameset_iterator_delete(it);
179 if (cpl_error_get_code())
199 uves_contains_frames_kind(cpl_frameset * sof,
210 nsof = cpl_frameset_get_size(sof);
211 for (i=0 ; i<nsof ; i++) {
212 cpl_frame* frame = cpl_frameset_get_frame(sof,i);
213 char* name= (
char*) cpl_frame_get_filename(frame);
214 if(uves_is_fits_file(name) == 1) {
216 if(cpl_frame_get_tag(frame) != NULL) {
218 char* tag= (
char*) cpl_frame_get_tag(frame);
220 if(strstr(tag,type) != NULL) {
222 cpl_frame* frame_dup = cpl_frame_duplicate(frame);
223 cpl_frameset_insert(raw,frame_dup);
252 const char *regression_name,
256 cpl_polynomial *pol = NULL;
257 int N = strlen(regression_name);
258 const char *coeffi_name = NULL;
271 coeffi_name = cpl_sprintf(
"%sI", regression_name);
277 case 1: cind=
'1';
break;
278 case 2: cind=
'2';
break;
279 case 3: cind=
'3';
break;
280 case 4: cind=
'4';
break;
281 case 5: cind=
'5';
break;
282 case 6: cind=
'6';
break;
283 case 7: cind=
'7';
break;
284 case 8: cind=
'8';
break;
285 case 9: cind=
'9';
break;
287 assure(
false, CPL_ERROR_ILLEGAL_INPUT,
288 "Illegal index %d, 1-9 expected", index);
293 coeffi_name = cpl_sprintf(
"%sI%d", regression_name, index);
296 check_nomsg( coeffi = uves_read_midas_array(plist, coeffi_name, &length, &type, NULL));
299 assure( type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
300 "Type of array %s is %s, integer expected",
311 assure( coeffi[1] == 2, CPL_ERROR_UNSUPPORTED_MODE,
312 "Regressions is %d-dimensional (2D expected)",
328 regression_name, degree1, degree2);
332 pol = cpl_polynomial_new(2);
336 for (i = 0; !found && i < plist_size; i++){
338 const char *name = cpl_property_get_name(p);
340 if (strcmp(name,
"HISTORY") == 0) {
342 check( value = cpl_property_get_string(p),
343 "Error reading property value");
350 (
int)strlen(value) >= 1+N+2 &&
353 value[1+N+1] ==
'\'')
358 (
int)strlen(value) >= 1+N+3 &&
361 value[1+N+1] == cind &&
362 value[1+N+2] ==
'\'') )
366 strncmp(value+1, regression_name, N) == 0
379 while (power[1] <= degree2){
381 coeff = strtod(value, &next);
385 cpl_polynomial_set_coeff(pol, power, coeff);
386 uves_msg_debug(
"Polynomial coefficient of order (%" CPL_SIZE_FORMAT
", %" CPL_SIZE_FORMAT
") is %e",
387 power[0], power[1], coeff);
390 if (power[0] > degree1){
401 assure(j < plist_size, CPL_ERROR_ILLEGAL_INPUT,
402 "Missing header data");
405 assure( cpl_property_get_type(p) == CPL_TYPE_STRING &&
406 strcmp(cpl_property_get_name(p),
"HISTORY") == 0,
407 CPL_ERROR_ILLEGAL_INPUT,
"Error parsing polynomial");
409 value = cpl_property_get_string(p);
419 assure( found, CPL_ERROR_ILLEGAL_INPUT,
"Could not find '%sD' in property list",
426 uves_free_int(&coeffi);
427 uves_free_string_const(&coeffi_name);
428 uves_free_polynomial(&pol);
429 if (cpl_error_get_code() != CPL_ERROR_NONE)
447 uves_frameset_merge(cpl_frameset * set1,
const cpl_frameset* set2)
450 const cpl_frame* frm_tmp=NULL;
451 cpl_frame* frm_dup=NULL;
454 passure(set1 != NULL,
"Wrong input set");
455 passure(set2 != NULL,
"Wrong input set");
456 nfrm=cpl_frameset_get_size(set2);
457 for (i = 0; i < nfrm; i++ ) {
458 frm_tmp=cpl_frameset_get_frame(set2,i);
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)
481 cpl_frame* frm_dup=NULL;
485 cpl_frameset_iterator* it = cpl_frameset_iterator_new(set);
486 const cpl_frame *frm_tmp = cpl_frameset_iterator_get_const(it);
488 while (frm_tmp != NULL)
490 g=cpl_frame_get_group(frm_tmp);
492 frm_dup=cpl_frame_duplicate(frm_tmp);
493 cpl_frameset_insert(*ext,frm_dup);
494 uves_msg_debug(
"group %d insert file %s ",type,cpl_frame_get_filename(frm_dup));
496 cpl_frameset_iterator_advance(it, 1);
497 frm_tmp = cpl_frameset_iterator_get_const(it);
499 cpl_frameset_iterator_delete(it);
501 return cpl_error_get_code();
514 uves_sflats_get_encoder_steps(
const cpl_frameset * set, cpl_table** enc,
int* nset)
517 const cpl_frame* frm=NULL;
524 const int threshold=5;
527 cpl_table* encoder_tbl=NULL;
528 ndata = cpl_frameset_get_size(set);
529 encoder_tbl=cpl_table_new(ndata);
530 cpl_table_new_column(encoder_tbl,
"x1enc",CPL_TYPE_INT);
531 cpl_table_new_column(encoder_tbl,
"x2enc",CPL_TYPE_INT);
532 cpl_table_new_column(encoder_tbl,
"flag",CPL_TYPE_INT);
534 for(i=0;i<cpl_frameset_get_size(set);i++)
536 check_nomsg(frm=cpl_frameset_get_frame_const(set,i));
540 check_nomsg(cpl_table_set_int(encoder_tbl,
"x1enc",i,x1enc));
541 check_nomsg(cpl_table_set_int(encoder_tbl,
"x2enc",i,x2enc));
542 uves_free_propertylist(&plist);
545 check_nomsg(uves_sort_table_2(encoder_tbl,
"x1enc",
"x2enc",
false,
true));
547 check_nomsg(ref_x1enc=cpl_table_get_int(encoder_tbl,
"x1enc",0,&status));
548 check_nomsg(ref_x2enc=cpl_table_get_int(encoder_tbl,
"x2enc",0,&status));
550 *enc=cpl_table_new(1);
551 cpl_table_new_column(*enc,
"x1enc",CPL_TYPE_INT);
552 cpl_table_new_column(*enc,
"x2enc",CPL_TYPE_INT);
553 check_nomsg(cpl_table_set_int(*enc,
"x1enc",0,ref_x1enc));
554 check_nomsg(cpl_table_set_int(*enc,
"x2enc",0,ref_x2enc));
556 for(i=1;i<cpl_table_get_nrow(encoder_tbl);i++) {
557 check_nomsg(x1enc=cpl_table_get_int(encoder_tbl,
"x1enc",i,&status));
558 check_nomsg(x2enc=cpl_table_get_int(encoder_tbl,
"x2enc",i,&status));
559 if( (fabs(ref_x1enc -x1enc) > threshold) ||
560 (fabs(ref_x2enc -x2enc) > threshold) ) {
564 cpl_table_set_size(*enc,(*nset+1));
565 check_nomsg(cpl_table_set_int(*enc,
"x1enc",*nset,ref_x1enc));
566 check_nomsg(cpl_table_set_int(*enc,
"x2enc",*nset,ref_x2enc));
571 uves_msg(
"Number of sets = %d",*nset);
574 uves_free_table(&encoder_tbl);
575 uves_free_propertylist(&plist);
576 return cpl_error_get_code();
588 uves_dfs_set_groups(cpl_frameset * set)
590 cpl_frame * cur_frame ;
594 assure(set != NULL, CPL_ERROR_NULL_INPUT,
"Null input");
597 check( nframes = cpl_frameset_get_size(set),
"Could not read frameset size");
601 for (i = 0; i< nframes;i++)
603 cur_frame=cpl_frameset_get_frame(set,i);
605 bool is_calib =
false;
606 bool is_recognized =
false;
609 const char * tag = cpl_frame_get_tag(cur_frame);
611 assure( tag != NULL && strcmp(tag,
"") != 0, CPL_ERROR_ILLEGAL_INPUT,
612 "Frame has no tag!");
620 (strcmp(tag, UVES_ORDER_FLAT (flames,blue)) == 0 ||
621 strcmp(tag, UVES_BIAS (blue)) == 0 ||
622 strcmp(tag, UVES_DARK (blue)) == 0 ||
623 strcmp(tag, UVES_PDARK (blue)) == 0 ||
624 strcmp(tag, UVES_FLAT (blue)) == 0 ||
625 strcmp(tag, UVES_IFLAT (blue)) == 0 ||
626 strcmp(tag, UVES_DFLAT (blue)) == 0 ||
627 strcmp(tag, UVES_SFLAT (blue)) == 0 ||
628 strcmp(tag, UVES_TFLAT (blue)) == 0 ||
629 strcmp(tag, UVES_SCREEN_FLAT (blue)) == 0 ||
630 strcmp(tag, UVES_CD_ALIGN (blue)) == 0 ||
631 strcmp(tag, UVES_FORMATCHECK (flames,blue)) == 0 ||
632 strcmp(tag, UVES_STD_STAR (blue)) == 0 ||
633 strcmp(tag, UVES_SCIENCE (blue)) == 0 ||
634 strcmp(tag, UVES_SCI_EXTND (blue)) == 0 ||
635 strcmp(tag, UVES_SCI_POINT (blue)) == 0 ||
636 strcmp(tag, UVES_SCI_SLICER (blue)) == 0 ||
637 strcmp(tag, UVES_ARC_LAMP (flames,blue)) == 0 ||
638 strcmp(tag, UVES_ECH_ARC_LAMP(blue)) == 0 ||
639 strcmp(tag, RAW_IMA) == 0 ||
640 strcmp(tag, FLAMES_SCI_RED) == 0 ||
641 strcmp(tag, FLAMES_SCI_SIM_RED) == 0 ||
642 strcmp(tag, FLAMES_SCI_COM_RED) == 0 ||
643 strcmp(tag, FLAMES_FIB_FF_ODD) == 0 ||
644 strcmp(tag, FLAMES_FIB_FF_EVEN) == 0 ||
645 strcmp(tag, FLAMES_FIB_FF_ALL) == 0);
651 chip != UVES_CHIP_INVALID;
656 is_calib = is_calib ||
657 (strcmp(tag, UVES_DRS_SETUP(flames, chip)) == 0 ||
658 strcmp(tag, UVES_ORDER_TABLE(flames, chip)) == 0 ||
659 strcmp(tag, UVES_GUESS_ORDER_TABLE(flames,chip)) == 0 ||
660 strcmp(tag, UVES_MASTER_BIAS (chip)) == 0 ||
661 strcmp(tag, UVES_MASTER_DARK (chip)) == 0 ||
662 strcmp(tag, UVES_MASTER_PDARK (chip)) == 0 ||
663 strcmp(tag, UVES_MASTER_FLAT (chip)) == 0 ||
664 strcmp(tag, UVES_MASTER_DFLAT (chip)) == 0 ||
665 strcmp(tag, UVES_MASTER_SFLAT (chip)) == 0 ||
666 strcmp(tag, UVES_MASTER_IFLAT (chip)) == 0 ||
667 strcmp(tag, UVES_MASTER_TFLAT (chip)) == 0 ||
668 strcmp(tag, UVES_REF_TFLAT (chip)) == 0 ||
669 strcmp(tag, UVES_ORD_TAB(flames,chip)) == 0 ||
670 strcmp(tag, UVES_MASTER_SCREEN_FLAT(chip)) == 0 ||
671 strcmp(tag, UVES_MASTER_ARC_FORM(chip)) == 0 ||
672 strcmp(tag, UVES_WEIGHTS(chip)) == 0 ||
673 strcmp(tag, UVES_LINE_TABLE(flames,chip)) == 0 ||
674 strcmp(tag, UVES_GUESS_LINE_TABLE(flames,chip)) == 0 ||
675 strcmp(tag, UVES_INSTR_RESPONSE(chip)) == 0 ||
676 strcmp(tag, UVES_MASTER_RESPONSE(chip)) == 0 ||
677 strcmp(tag, UVES_LINE_REFER_TABLE ) == 0 ||
678 strcmp(tag, UVES_LINE_INTMON_TABLE ) == 0 ||
679 strcmp(tag, UVES_FLUX_STD_TABLE ) == 0 ||
680 strcmp(tag, UVES_EXTCOEFF_TABLE ) == 0 ||
681 strcmp(tag, FLAMES_LINE_TABLE(chip)) == 0 ||
682 strcmp(tag, FLAMES_SLIT_FF_DT1(chip)) == 0 ||
683 strcmp(tag, FLAMES_SLIT_FF_DT2(chip)) == 0 ||
684 strcmp(tag, FLAMES_SLIT_FF_DT3(chip)) == 0 ||
685 strcmp(tag, FLAMES_SLIT_FF_DTC(chip)) == 0 ||
686 strcmp(tag, FLAMES_SLIT_FF_BP1(chip)) == 0 ||
687 strcmp(tag, FLAMES_SLIT_FF_BP2(chip)) == 0 ||
688 strcmp(tag, FLAMES_SLIT_FF_BP3(chip)) == 0 ||
689 strcmp(tag, FLAMES_SLIT_FF_BPC(chip)) == 0 ||
690 strcmp(tag, FLAMES_SLIT_FF_BN1(chip)) == 0 ||
691 strcmp(tag, FLAMES_SLIT_FF_BN2(chip)) == 0 ||
692 strcmp(tag, FLAMES_SLIT_FF_BN3(chip)) == 0 ||
693 strcmp(tag, FLAMES_SLIT_FF_BNC(chip)) == 0 ||
694 strcmp(tag, FLAMES_SLIT_FF_SG1(chip)) == 0 ||
695 strcmp(tag, FLAMES_SLIT_FF_SG2(chip)) == 0 ||
696 strcmp(tag, FLAMES_SLIT_FF_SG3(chip)) == 0 ||
697 strcmp(tag, FLAMES_SLIT_FF_SGC(chip)) == 0 ||
698 strcmp(tag, FLAMES_SLIT_FF_COM(chip)) == 0 ||
699 strcmp(tag, FLAMES_SLIT_FF_NOR(chip)) == 0 ||
700 strcmp(tag, FLAMES_SLIT_FF_NSG(chip)) == 0 ||
701 strcmp(tag, FLAMES_FIB_FF_DT1(chip)) == 0 ||
702 strcmp(tag, FLAMES_FIB_FF_DT2(chip)) == 0 ||
703 strcmp(tag, FLAMES_FIB_FF_DT3(chip)) == 0 ||
704 strcmp(tag, FLAMES_FIB_FF_DTC(chip)) == 0 ||
705 strcmp(tag, FLAMES_FIB_FF_BP1(chip)) == 0 ||
706 strcmp(tag, FLAMES_FIB_FF_BP2(chip)) == 0 ||
707 strcmp(tag, FLAMES_FIB_FF_BP3(chip)) == 0 ||
708 strcmp(tag, FLAMES_FIB_FF_BPC(chip)) == 0 ||
709 strcmp(tag, FLAMES_FIB_FF_BN1(chip)) == 0 ||
710 strcmp(tag, FLAMES_FIB_FF_BN2(chip)) == 0 ||
711 strcmp(tag, FLAMES_FIB_FF_BN3(chip)) == 0 ||
712 strcmp(tag, FLAMES_FIB_FF_BNC(chip)) == 0 ||
713 strcmp(tag, FLAMES_FIB_FF_SG1(chip)) == 0 ||
714 strcmp(tag, FLAMES_FIB_FF_SG2(chip)) == 0 ||
715 strcmp(tag, FLAMES_FIB_FF_SG3(chip)) == 0 ||
716 strcmp(tag, FLAMES_FIB_FF_SGC(chip)) == 0 ||
717 strcmp(tag, FLAMES_FIB_FF_COM(chip)) == 0 ||
718 strcmp(tag, FLAMES_FIB_FF_NOR(chip)) == 0 ||
719 strcmp(tag, FLAMES_FIB_FF_NSG(chip)) == 0 ||
720 strcmp(tag, FLAMES_ORDEF(flames,chip)) == 0 ||
721 strcmp(tag, FLAMES_CORVEL_MASK) == 0);
723 for (window = 1; window <= 3; window++)
725 is_calib = is_calib ||
726 strcmp(tag, UVES_LINE_TABLE_MIDAS(chip, window)) == 0;
729 if (!flames && strcmp(tag, UVES_BACKGR_TABLE(chip)) == 0)
732 "Inter-order positions will be inferred "
733 "from the order table %s. "
734 "Use recipe parameters to define "
736 UVES_BACKGR_TABLE(chip),
737 UVES_ORDER_TABLE(flames, chip));
739 is_recognized =
true;
742 if (strcmp(tag, UVES_DRS_SETUP(flames, chip)) == 0)
745 "Use recipe parameters "
746 "to define data reduction parameters ",
747 UVES_DRS_SETUP(flames, chip));
749 is_recognized =
true;
758 is_recognized = is_recognized || is_raw || is_calib;
762 cpl_frame_set_group(cur_frame, CPL_FRAME_GROUP_RAW) ;
766 cpl_frame_set_group(cur_frame, CPL_FRAME_GROUP_CALIB) ;
768 else if (!is_recognized)
774 uves_dfs_files_dont_exist(set);
778 return cpl_error_get_code();
795 bool blue, new_format;
803 chip != UVES_CHIP_INVALID;
817 "Error erasing keyword '%s'", UVES_PRESCANX(new_format, chip));
821 "Error erasing keyword '%s'", UVES_PRESCANY(new_format, chip));
825 "Error erasing keyword '%s'", UVES_OVRSCANX(new_format, chip));
829 "Error erasing keyword '%s'", UVES_OVRSCANY(new_format, chip));
831 while (n_erase_px > 0 ||
840 new_format = !new_format;
927 uves_frameset_insert(cpl_frameset *frames,
929 cpl_frame_group group,
931 cpl_frame_level level,
932 const char *filename,
937 const cpl_parameterlist *parameters,
939 const char *pipeline,
941 const char *start_time,
947 const char *origin =
"";
949 passure( !(type == CPL_FRAME_TYPE_IMAGE && table_header != NULL),
" ");
950 passure( raw_header != NULL,
" ");
951 passure( primary_header != NULL,
" ");
953 assure( type == CPL_FRAME_TYPE_IMAGE || stats_mask == 0,
954 CPL_ERROR_INCOMPATIBLE_INPUT,
955 "Cannot compute image statistics on table product" );
958 check(( f = cpl_frame_new(),
959 cpl_frame_set_filename(f, filename),
960 cpl_frame_set_tag (f, tag),
961 cpl_frame_set_type (f, type),
962 cpl_frame_set_group (f, group),
963 cpl_frame_set_level (f, level),
964 cpl_frameset_insert(frames, f)),
"Could not insert frame into frameset");
967 if (strchr(pipeline,
'/') == NULL)
970 "Pipeline-name/version", pipeline);
989 "Could not copy keywords");
993 "Could not copy keywords");
997 UVES_TIME_START(
"cpl_dfs_setup_product_header");
998 check( uves_dfs_setup_product_header(pl,
1005 "Error setting up product header");
1014 check( uves_get_property_value(pl,
"ORIGIN", CPL_TYPE_STRING, &origin),
1015 "Error reading ORIGIN from product header");
1017 if (strcmp(origin,
"ESO-MIDAS") == 0)
1023 if (type == CPL_FRAME_TYPE_IMAGE && stats_mask != 0)
1025 check( uves_dfs_write_statistics((cpl_image *)
object, pl, stats_mask),
1026 "Error adding image statistics keywords");
1034 "Could not propagate 'ESO DET*' keywords");
1038 check( remove_pre_over_scan(pl),
1039 "Error removing pre-, overscan keywords from product header");
1043 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_AIRMASS) );
1044 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_IMAGETYP) );
1045 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_UT) );
1046 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_ST) );
1047 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_EXPTIME) );
1048 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_EXTNAME) );
1049 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_DATE) );
1050 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_DATAMEAN) );
1051 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_DATAMED) );
1052 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_DATARMS) );
1053 check_nomsg( uves_copy_if_possible(pl, raw_header, UVES_OS_EXPOI) );
1058 pl, raw_header,
"^((GRAT|FILTER|WLEN)[0-9]*)$", 0),
1059 "Could not propagate GRATi, FILTERi and WLENi keywords");
1077 bool invert =
false;
1079 "ESO PRO (REDLEVEL|REC[0-9]+ STATUS)|"
1080 "TM-START|MIDASFTP|FILENAME)$", invert);
1083 if( (strcmp(recipe,
"uves_obs_scired") == 0) ||
1084 (strcmp(recipe,
"uves_cal_response") == 0) ) {
1086 if(cpl_propertylist_has(pl,
"WLEN1")) {
1096 "Could not write recipe start time");
1099 "Could not write recipe stop time");
1107 for (i = 0; qc[i] != NULL; i++)
1114 if (strcmp(recipe, make_str(UVES_TFLAT_ID)) == 0 && i == 1)
1122 UVES_TIME_START(
"save product");
1125 if (type == CPL_FRAME_TYPE_IMAGE)
1127 bool use_bitpix16_for_int = (strcmp(recipe, make_str(FLAMES_CAL_ORDERPOS)) == 0);
1129 check( uves_save_image((cpl_image *)
object, filename, pl,
1130 use_bitpix16_for_int,
true),
1131 "Error saving image to file %s", filename);
1133 else if (type == CPL_FRAME_TYPE_TABLE)
1135 check( uves_table_save((cpl_table *)
object,
1140 "Error saving table to file '%s'", filename);
1144 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Unsupported frame type");
1150 uves_free_propertylist(&pl);
1152 return cpl_error_get_code();
1168 unsigned stats_mask)
1170 cpl_stats *stats = NULL;
1173 assure( (stats_mask & (CPL_STATS_MEAN | CPL_STATS_STDEV | CPL_STATS_MEDIAN |
1174 CPL_STATS_MIN | CPL_STATS_MAX)) == stats_mask,
1175 CPL_ERROR_UNSUPPORTED_MODE,
"Cannot compute mask %d",
1178 UVES_TIME_START(
"calculate stats");
1180 check( stats = cpl_stats_new_from_image(
1182 "Error reading image statistics");
1186 if (stats_mask & CPL_STATS_MEDIAN)
1189 "Could not write median flux");
1191 if (stats_mask & CPL_STATS_MEAN)
1194 "Could not write average flux");
1196 if (stats_mask & CPL_STATS_STDEV)
1199 "Could not write flux stdev");
1201 if (stats_mask & CPL_STATS_MIN)
1204 "Could not write min flux");
1206 if (stats_mask & CPL_STATS_MIN)
1209 "Could not write max flux");
1213 uves_free_stats(&stats);
1256 int *length, cpl_type *type,
int *ncards)
1258 void *result = NULL;
1259 unsigned result_size;
1260 int N = strlen(name);
1269 for (i = 0; !found && i < plist_size; i++)
1272 value = cpl_property_get_name(p);
1274 if (strcmp(value,
"HISTORY") == 0)
1277 check( value = cpl_property_get_string(p),
1278 "Error reading property value");
1282 if ((
int)strlen(value) >= 1+N+4 &&
1284 value[N+1] ==
'\'' &&
1285 value[N+2] ==
',' &&
1286 value[N+3] ==
'\'' &&
1287 strncmp(value+1, name, N) == 0
1290 switch(value[N+4]) {
1296 *type = CPL_TYPE_DOUBLE;
1298 if ((
int)strlen(value) >= 1+N+4+2 && value[N+4+1] ==
'*')
1300 switch(value[N+4+2]) {
1301 case '4': *type = CPL_TYPE_FLOAT;
break;
1302 case '8': *type = CPL_TYPE_DOUBLE;
break;
1304 assure(
false, CPL_ERROR_ILLEGAL_INPUT,
1305 "Unrecognized MIDAS type: 'R*%c'",
1311 case 'I': *type = CPL_TYPE_INT ; size =
sizeof(int);
break;
1312 case 'C': *type = CPL_TYPE_STRING; size =
sizeof(char);
break;
1314 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1315 "Unrecognized type '%c'", value[N+4]);
1323 assure( found, CPL_ERROR_ILLEGAL_INPUT,
"Could not find '%s' in property list", name);
1326 result_size =
sizeof(double) * 100;
1327 result = cpl_malloc(result_size);
1330 if (ncards != NULL) *ncards = 2;
1332 const cpl_property *p;
1334 if (ncards != NULL) *ncards += 1;
1336 assure(i < plist_size,
1337 CPL_ERROR_ILLEGAL_INPUT,
"Missing header data");
1339 assure( cpl_property_get_type(p) == CPL_TYPE_STRING &&
1340 strcmp(cpl_property_get_name(p),
"HISTORY") == 0,
1341 CPL_ERROR_ILLEGAL_INPUT,
"Error parsing array");
1346 if (*type == CPL_TYPE_STRING)
1348 assure( strlen(value) < 100, CPL_ERROR_UNSUPPORTED_MODE,
1349 "String too long. Max size is 100");
1355 int len = strlen(value);
1358 for (k = 0; k <= len; k++)
1361 ((
char*)result)[j] = value[k];
1369 value, (
char*)result);
1376 if (strcmp(value,
"") != 0) {
1377 double numberd = -1;
1380 const int base = 10;
1381 char *next = (
char *) value;
1386 case CPL_TYPE_DOUBLE:
1387 numberd = strtod(value, &next);
1390 case CPL_TYPE_FLOAT:
1391 numberf = strtod(value, &next);
1395 numberi = strtol(value, &next, base);
1406 if (*length *
sizeof(
double) > result_size)
1409 result = cpl_realloc(result, result_size);
1413 case CPL_TYPE_DOUBLE:
1414 ((
double *)result)[*length-1] = numberd;
1416 case CPL_TYPE_FLOAT:
1417 ((
float *)result)[*length-1] = numberf;
1420 ((
int *)result)[*length-1] = numberi;
1429 case CPL_TYPE_DOUBLE:
1430 numberd = strtod(value, &next);
1433 case CPL_TYPE_FLOAT:
1434 numberf = strtod(value, &next);
1438 numberi = strtol(value, &next, base);
1445 }
while (next != value);
1451 assure( strcmp(value,
"") == 0, CPL_ERROR_ILLEGAL_INPUT,
1452 "Cannot parse %s descriptor %s, remaining string: '%s'",
1459 if (cpl_property_get_type(p) == CPL_TYPE_STRING &&
1460 strcmp(cpl_property_get_name(p),
"HISTORY") == 0)
1462 value = cpl_property_get_string(
1465 if (*type == CPL_TYPE_STRING)
1467 if (strcmp(value,
"") != 0) {
1468 uves_msg_debug(
"String array %s with length > 1 found. Ignoring remaining values", name);
1469 while (strcmp(value,
"") != 0 && i+1 < plist_size) {
1472 value = cpl_property_get_string(
1474 if (ncards != NULL) *ncards += 1;
1481 }
while (strcmp(value,
"") != 0);
1484 if (cpl_error_get_code() != CPL_ERROR_NONE)
1486 cpl_free(result); result = NULL;
1512 uves_save_table_local(
const char *description,
const char *filename_prefix,
1513 const cpl_table *table,
1514 enum uves_chip chip,
int trace,
int window,
1517 char *filename = NULL;
1519 check( filename = uves_local_filename(filename_prefix, chip, trace, window),
1520 "Error getting filename");
1522 check( uves_table_save(table, pheader, eheader, filename, CPL_IO_DEFAULT),
1523 "Error saving table to file '%s'", filename);
1525 if (description != NULL)
uves_msg(
"%s saved to '%s'", description, filename);
1529 return cpl_error_get_code();
1555 uves_save_image_local(
const char *description,
const char *filename_prefix,
1556 const cpl_image *image,
1557 enum uves_chip chip,
int trace,
int window,
1559 bool use_bitpix16_for_int)
1561 char *filename = NULL;
1563 check( filename = uves_local_filename(filename_prefix, chip, trace, window),
1564 "Error getting filename");
1566 check( uves_save_image(image, filename, plist, use_bitpix16_for_int,
true),
1567 "Error saving image to file '%s'", filename);
1568 if (description != NULL)
uves_msg(
"%s saved to '%s'", description, filename);
1572 return cpl_error_get_code();
1587 cpl_image *uves_load_image(
const cpl_frame *f,
1592 cpl_image *image = NULL;
1594 const char *filename;
1598 cpl_vector * vector=NULL;
1603 assure( cpl_frame_get_type(f) == CPL_FRAME_TYPE_IMAGE,
1604 CPL_ERROR_TYPE_MISMATCH,
"Wrong type: %s",
1607 filename = cpl_frame_get_filename(f);
1610 "Could not load header from %s extension %d",
1611 filename, extension);
1614 "Could not read BITPIX from %s extension %d",
1615 filename, extension);
1617 if (bitpix == -32) type = CPL_TYPE_FLOAT;
1618 else if (bitpix == -64) type = CPL_TYPE_DOUBLE;
1619 else if (bitpix == 32) type = CPL_TYPE_INT;
1620 else if (bitpix == 16) type = CPL_TYPE_INT;
1623 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1624 "No CPL type to represent BITPIX = %d", bitpix);
1628 "could not get NAXIS" );
1632 check( vector = cpl_vector_load(filename,extension),
1633 "Could not load vector from extension %d of file '%s' ",
1634 extension, filename);
1635 cknull(image=uves_vector_to_image(vector,type),
1636 "could not convert vector to image");
1640 check( image = cpl_image_load(filename,
1644 "Could not load image from extension %d of file '%s' ",
1645 extension, filename);
1655 uves_free_vector(&vector);
1656 uves_free_propertylist(&plist);
1665 cpl_image *uves_load_image_file(
const char *filename,
1671 cpl_frame *f = cpl_frame_new();
1672 cpl_frame_set_filename(f, filename);
1673 cpl_frame_set_type(f, CPL_FRAME_TYPE_IMAGE);
1675 i = uves_load_image(f, plane, extension, header);
1677 uves_free_frame(&f);
1709 uves_save_image(
const cpl_image *image,
const char *filename,
const uves_propertylist *plist,
1710 bool use_bitpix16_for_int,
bool save1d)
1714 const cpl_vector *image_1d = NULL;
1716 cpl_image *thresholded = NULL;
1717 cpl_image *thresholded_double = NULL;
1719 if (image == NULL) {
1720 check( uves_image_save(image, filename, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT),
1721 "Error saving NULL image to file '%s'", filename);
1724 check( t = cpl_image_get_type(image),
"Error reading image type");
1725 if (t == CPL_TYPE_FLOAT ) bpp = CPL_BPP_IEEE_FLOAT;
1726 else if (t == CPL_TYPE_DOUBLE) bpp = CPL_BPP_IEEE_FLOAT;
1733 else if (t == CPL_TYPE_INT ) {
1734 if (use_bitpix16_for_int) bpp = CPL_BPP_16_UNSIGNED;
1735 else bpp = CPL_BPP_32_SIGNED;
1737 else assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1741 thresholded = cpl_image_duplicate(image);
1744 if (t == CPL_TYPE_DOUBLE)
1746 passure( bpp == CPL_BPP_IEEE_FLOAT,
"%d", bpp);
1762 -FLT_MAX, FLT_MAX) );
1766 double *data = cpl_image_get_data_double(thresholded);
1767 int nx = cpl_image_get_size_x(thresholded);
1768 int ny = cpl_image_get_size_y(thresholded);
1771 for (y = 0; y < ny; y++)
1772 for (x = 0; x < nx; x++)
1774 if (irplib_isnan(data[x + y*nx]))
1783 cpl_image_get_size_y(thresholded) == 1 &&
1784 (t == CPL_TYPE_DOUBLE ||
1785 t == CPL_TYPE_FLOAT)) {
1787 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 );
1828 if (plist != NULL) {
1842 check( uves_image_save(thresholded, filename, bpp, plist, CPL_IO_DEFAULT),
1843 "Error saving image to file '%s'", filename);
1848 uves_unwrap_vector_const(&image_1d);
1849 uves_free_propertylist(&header);
1850 uves_free_image(&thresholded);
1851 uves_free_image(&thresholded_double);
1879 uves_save_imagelist(
const cpl_imagelist *iml,
const char *filename,
const uves_propertylist *plist)
1881 const cpl_image* img=NULL;
1884 const cpl_vector *image_1d = NULL;
1886 cpl_imagelist *thresholded = NULL;
1893 cknull(iml,
"Null input image");
1894 check(img=cpl_imagelist_get_const(iml,0),
"error reading image");
1900 check( t = cpl_image_get_type(img),
"Error reading image type");
1901 if (t == CPL_TYPE_FLOAT ) bpp = CPL_BPP_IEEE_FLOAT;
1902 else if (t == CPL_TYPE_DOUBLE) bpp = CPL_BPP_IEEE_FLOAT;
1905 else if (t == CPL_TYPE_INT ) bpp = CPL_BPP_16_UNSIGNED;
1906 else assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1910 thresholded = cpl_imagelist_duplicate(iml);
1913 if (t == CPL_TYPE_DOUBLE)
1915 passure( bpp == CPL_BPP_IEEE_FLOAT,
"%d", bpp);
1931 -FLT_MAX, FLT_MAX) );
1940 for (z = 0; z < nz; z++) {
1941 cpl_image* ima=cpl_imagelist_get(thresholded,z);
1942 double* data = cpl_image_get_data_double(ima);
1944 for (y = 0; y < ny; y++) {
1945 for (x = 0; x < nx; x++) {
1946 if (irplib_isnan(data[x + y*nx])) {
1954 if (nz == 1 && t == CPL_TYPE_DOUBLE)
1962 bool invert =
false;
1983 check( uves_imagelist_save(thresholded, filename, bpp, plist, CPL_IO_DEFAULT),
1984 "Error saving image to file '%s'", filename);
1988 uves_unwrap_vector_const(&image_1d);
1989 uves_free_propertylist(&header);
1990 uves_free_imagelist(&thresholded);
2013 cpl_table *t = NULL;
2017 check( uves_table_save(t,
2023 "Error saving table to file '%s'", filename);
2026 uves_free_table(&t);
2027 return cpl_error_get_code();
2041 load_polynomial(
const char* filename,
int extension)
2044 cpl_table *t = NULL;
2046 check(t = cpl_table_load(filename,
2050 "Error loading polynomial from extension %d of file '%s'", extension, filename);
2052 assure( uves_erase_invalid_table_rows(t, NULL) == 0,
2053 CPL_ERROR_ILLEGAL_INPUT,
"Table contains invalid rows");
2058 uves_free_table(&t);
2059 if (cpl_error_get_code() != CPL_ERROR_NONE)
2080 identify_arm(
const cpl_frameset *frames,
const char *blue_tag,
const char *red_tag,
2083 const char *tag = NULL;
2085 const cpl_frame *frame = NULL;
2088 assure (!cpl_frameset_is_empty(frames), CPL_ERROR_ILLEGAL_INPUT,
"No input frames");
2091 frame = cpl_frameset_find_const(frames, blue_tag);
2092 *blue = (frame != NULL);
2096 frame = cpl_frameset_find_const(frames, red_tag);
2099 assure( frame != NULL, CPL_ERROR_ILLEGAL_INPUT,
2100 "No valid input frames "
2101 "('%s' or '%s') in frame set",
2104 assure( cpl_frameset_find_const(frames, blue_tag) == NULL ||
2105 cpl_frameset_find_const(frames, red_tag) == NULL,
2106 CPL_ERROR_INCOMPATIBLE_INPUT,
2107 "Multiple types of input frames ('%s' and '%s') in frame set",
2110 tag = cpl_frame_get_tag(frame);
2112 uves_msg(
"Input frames are '%s'", tag);
2140 enum uves_chip chip,
2144 cpl_image *result = NULL;
2145 int prescanx, ovrscanx;
2147 int x_0, y_0, x_1, y_1;
2150 const char *ctype1, *ctype2;
2151 const char *cunit1, *cunit2;
2154 double crval1, crval2;
2155 double crpix1, crpix2;
2156 double cdelt1, cdelt2;
2160 passure( header != NULL,
" ");
2161 passure( out_header != NULL,
" ");
2163 nx = cpl_image_get_size_x(image);
2164 ny = cpl_image_get_size_y(image);
2222 if (new_format || chip == UVES_CHIP_BLUE)
2225 x_1 = nx - ovrscanx;
2229 if (chip == UVES_CHIP_REDU)
2232 x_1 = nx/2 - ovrscanx;
2236 x_0 = nx/2 + prescanx + 1;
2237 x_1 = nx - ovrscanx;
2240 check( result = cpl_image_extract(image, x_0, y_0, x_1, y_1),
"Could not crop image");
2241 crpix1 = crpix1 - (x_0 - 1);
2242 crpix2 = crpix2 - (y_0 - 1);
2243 nx = (x_1 - x_0) + 1;
2244 ny = (y_1 - y_0) + 1;
2247 UVES_TIME_START(
"Rotation");
2255 int crpix1_old = crpix1;
2256 int crpix2_old = crpix2;
2257 int crval1_old = crval1;
2258 int crval2_old = crval2;
2259 int cdelt1_old = cdelt1;
2260 int cdelt2_old = cdelt2;
2261 const char *ctype1_old = ctype1;
2262 const char *ctype2_old = ctype2;
2264 if (chip == UVES_CHIP_BLUE)
2267 check( cpl_image_turn(result, -1),
"Could not turn image");
2269 crpix1 = ny - (crpix2_old - 1);
2270 crpix2 = crpix1_old;
2271 crval1 = crval2_old;
2272 crval2 = crval1_old;
2278 check( cpl_image_flip(result, 3),
"Could not flip image");
2280 crpix1 = ny - (crpix2_old - 1);
2281 crpix2 = nx - (crpix1_old - 1);
2282 crval1 = crval2_old;
2283 crval2 = crval1_old;
2288 ctype1 = ctype2_old;
2289 ctype2 = ctype1_old;
2290 cdelt1 = cdelt2_old;
2291 cdelt2 = cdelt1_old;
2303 if (chip == UVES_CHIP_BLUE || chip == UVES_CHIP_REDL)
2310 int physical_gap_between_chips = 64;
2313 passure( chip == UVES_CHIP_REDU ,
"%d", chip );
2326 physical_gap_between_chips,
2327 "Error reading REDL chip geometry");
2329 uves_msg_debug(
"Setting CRVAL2 = 1 + (%d - %d - %d) * %f + %d = %f",
2334 physical_gap_between_chips, crval2);
2345 physical_gap_between_chips,
2346 "Error reading REDL chip geometry");
2348 uves_msg_debug(
"Setting CRVAL2 = 1 + (%d - %d - %d) * %f + %d = %f",
2353 physical_gap_between_chips, crval2);
2366 "Error initializing header");
2373 uves_msg(
"Raw image cropped and rotated from %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
" to %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
"",
2375 cpl_image_get_size_x(result),
2376 cpl_image_get_size_y(result));
2379 if (cpl_error_get_code() != CPL_ERROR_NONE)
2381 uves_free_image(&result);
2382 if (out_header != NULL)
2384 uves_free_propertylist(out_header);
2408 const char *raw_chip_name,
enum uves_chip chip)
2410 const char *calib_chip_name;
2411 bool mismatch =
false;
2414 "Could not read chip name of calibration data");
2422 unsigned int calib_first, calib_last;
2423 unsigned int raw_first, raw_last;
2427 while (calib_first < strlen(calib_chip_name) - 1 && calib_chip_name[calib_first] ==
' ' )
2431 while (raw_first < strlen(raw_chip_name) - 1 && raw_chip_name[raw_first] ==
' ' )
2436 calib_last = strlen(calib_chip_name) - 1;
2437 raw_last = strlen(raw_chip_name) - 1;
2438 while (calib_chip_name[calib_last] ==
' ' && calib_last > 0)
2442 while (raw_chip_name[raw_last] ==
' ' && raw_last > 0)
2448 if (calib_last - calib_first != raw_last - raw_first)
2456 for (i = 0; i <= (calib_last - calib_first); i++)
2458 if (raw_chip_name[raw_first + i] !=
2459 calib_chip_name[calib_first + i])
2471 "not match raw frame chip ID '%s'",
2472 calib_chip_name, raw_chip_name);
2504 static cpl_error_code
2505 load_raw_image(
const char *filename,
2509 cpl_image *raw_image[2],
2515 cpl_image *image = NULL;
2518 int extension, nextensions;
2522 cpl_image* image1=NULL;
2523 cpl_image* image2=NULL;
2529 raw_image[0] = NULL;
2530 raw_image[1] = NULL;
2531 raw_header[0] = NULL;
2532 raw_header[1] = NULL;
2533 rotated_header[0] = NULL;
2534 rotated_header[1] = NULL;
2536 check( nextensions = uves_get_nextensions(filename),
2537 "Error reading number of extensions of file '%s'", filename);
2543 "Could not load header from extension %d of file '%s'",
2544 extension, filename);
2547 "Error determining new/old format of file %s", filename);
2549 uves_msg_low(
"Raw frame is %s, %s format, file '%s' has %d extensions",
2550 (blue) ?
"blue" :
"red", (new_format) ?
"new" :
"old",
2551 filename, nextensions);
2554 if (blue || !new_format)
2556 enum uves_chip chip;
2560 assure( nextensions == 0 ||
2561 (blue && nextensions == 2) ||
2562 (flames && nextensions == 2),
2563 CPL_ERROR_ILLEGAL_INPUT,
2564 "Unrecognized format of file '%s'. %d extensions expected. %d found.",
2566 ((flames||blue) && (nextensions ==2)) ? 2 : 0, nextensions);
2573 "Could not load header from extension %d of file '%s'",
2574 extension, filename);
2578 if(blue && nextensions == 2) {
2582 "Could not load header from extension %d of file '%s'",
2583 extension, filename);
2585 "Could not collate header from extension 1 to 0 of file '%s'",filename);
2586 uves_free_propertylist(&raw_header[1]);
2588 check( image1 = cpl_image_load(filename,
2592 ),
"Could not load image from extension %d of file '%s' ",
2593 extension, filename);
2594 cpl_image_save(image1,
"ima1.fits", CPL_BPP_IEEE_FLOAT,
2595 NULL,CPL_IO_DEFAULT);
2598 check( image2 = cpl_image_load(filename,
2602 ),
"Could not load image from extension %d of file '%s' ",
2603 extension, filename);
2612 uves_free_image(&image1);
2613 uves_free_image(&image2);
2622 check( image = cpl_image_load(filename,
2626 ),
"Could not load image from extension %d of file '%s' ",
2627 extension, filename);
2631 chip = (blue) ? UVES_CHIP_BLUE : UVES_CHIP_REDL;
2632 check( raw_image[0] = uves_crop_and_rotate(image, raw_header[0],
2633 chip, raw_header[0],
2635 &rotated_header[0]),
2636 "Error splitting image");
2644 "Error duplicating FITS header");
2647 chip = UVES_CHIP_REDU;
2648 redl_header = raw_header[0];
2649 check( raw_image[1] = uves_crop_and_rotate(image, raw_header[1],
2652 &rotated_header[1]),
2653 "Error splitting red image");
2657 raw_image[1] = NULL;
2658 raw_header[1] = NULL;
2659 rotated_header[1] = NULL;
2669 assure( nextensions >= 2, CPL_ERROR_UNSUPPORTED_MODE,
2670 "File '%s' (red frame) has %d extensions. 2+ extensions expected "
2672 filename, nextensions);
2675 (nextensions > 2) ?
"FLAMES" :
"FLAMES/UVES");
2679 for (extension = 1; extension <= 2; extension++)
2684 enum uves_chip chip = (extension == 1) ? UVES_CHIP_REDU : UVES_CHIP_REDL;
2688 uves_free_propertylist(&ext_header);
2691 "Could not load header from extension %d of file '%s'",
2692 extension, filename);
2696 "Error cloning primary header");
2700 if(cpl_propertylist_has(ext_header,
"TM-START") &&
2701 cpl_propertylist_has(primary_header,
"TM-START") ) {
2704 if(cpl_propertylist_has(ext_header,
"DATE-OBS") &&
2705 cpl_propertylist_has(primary_header,
"DATE-OBS") ) {
2708 if(cpl_propertylist_has(ext_header,
"EXPTIME") &&
2709 cpl_propertylist_has(ext_header,
"EXPTIME")) {
2713 ext_header,
".*", 0),
2714 "Error merging primary header with extension %d header",
2721 for (extension = 1; extension <= 2; extension++)
2723 enum uves_chip chip = (extension == 1) ? UVES_CHIP_REDU : UVES_CHIP_REDL;
2729 uves_free_image(&image);
2730 check( image = cpl_image_load(filename,
2734 "Could not load image from extension %d of file '%s' ",
2735 extension, filename);
2737 check( raw_image[indx] = uves_crop_and_rotate(image,
2741 &rotated_header[indx]),
2742 "Error splitting red image");
2750 uves_free_image(&image);
2751 uves_free_image(&image1);
2752 uves_free_image(&image2);
2754 uves_free_propertylist(&primary_header);
2755 uves_free_propertylist(&ext_header);
2757 if (cpl_error_get_code() != CPL_ERROR_NONE)
2759 uves_free_image (&raw_image[0]);
2760 uves_free_image (&raw_image[1]);
2761 uves_free_propertylist(&raw_header[0]);
2762 uves_free_propertylist(&raw_header[1]);
2763 uves_free_propertylist(&rotated_header[0]);
2764 uves_free_propertylist(&rotated_header[1]);
2767 return cpl_error_get_code();
2801 uves_load_raw_imagelist(
const cpl_frameset *frames,
2803 const char *blue_tag,
const char *red_tag, cpl_type type,
2804 cpl_imagelist *images[2],
2808 const char *tag = NULL;
2809 const cpl_frame *frame = NULL;
2810 cpl_image *temp_image[2] = {NULL, NULL};
2812 cpl_size number_of_frames = 0;
2813 int frameset_size = 0;
2817 raw_headers[0] = NULL;
2818 raw_headers[1] = NULL;
2820 check( frameset_size = cpl_frameset_get_size(frames),
2821 "Error reading frameset size");
2823 check( tag = identify_arm(frames, blue_tag, red_tag, blue),
2824 "Could not identify chip type");
2826 nchips = (*blue) ? 1 : 2;
2827 for(chip = 0; chip < nchips; chip++)
2829 images[chip] = NULL;
2830 rotated_header[chip] = NULL;
2832 images[chip] = cpl_imagelist_new();
2840 number_of_frames = 0;
2843 nfrm=cpl_frameset_get_size(frames);
2846 frame=cpl_frameset_get_frame_const(frames,i);
2848 if ( strcmp(cpl_frame_get_tag(frame), tag) == 0)
2850 const char *filename = cpl_frame_get_filename(frame);
2853 uves_free_propertylist(&rotated_header[0]);
2854 uves_free_propertylist(&rotated_header[1]);
2856 check( load_raw_image(filename,
2863 "Could not load image from file '%s'", filename);
2866 for(chip = 0; chip < nchips; chip++)
2868 raw_headers[chip][number_of_frames] = temp_header[chip];
2869 temp_header[chip] = NULL;
2871 check( cpl_imagelist_set(images[chip],
2874 cpl_imagelist_get_size(images[chip])
2876 "Could not insert image into image list");
2879 temp_image[chip] = NULL;
2882 number_of_frames += 1;
2888 for(chip = 0; chip < nchips; chip++)
2891 assure (cpl_imagelist_is_uniform(images[chip]) == 0,
2892 CPL_ERROR_INCOMPATIBLE_INPUT,
2893 "Input images are not of same size and type");
2895 passure( cpl_imagelist_get_size(images[chip]) == number_of_frames,
2896 "%" CPL_SIZE_FORMAT
" %" CPL_SIZE_FORMAT
"", cpl_imagelist_get_size(images[0]), number_of_frames);
2902 if ( strcmp(UVES_BIAS (*blue), tag) != 0 &&
2903 strcmp(UVES_DARK (*blue), tag) != 0 &&
2904 strcmp(UVES_PDARK(*blue), tag) != 0) {
2905 enum uves_chip chip_id;
2910 chip_id != UVES_CHIP_INVALID;
2912 for (i = 0; i < number_of_frames; i++) {
2916 "Error reading central wavelength of input frame number %d", i+1);
2923 "Error reading central wavelength of input frame number %d", i+1);
2925 assure( fabs((w-wlen)/wlen) < 0.01, CPL_ERROR_INCOMPATIBLE_INPUT,
2926 "Mis-matching input frame central wavelengths: "
2927 "%e (frame 1) != %e (frame %d)", wlen, w, i+1);
2934 uves_free_image(&temp_image[0]);
2935 uves_free_image(&temp_image[1]);
2936 uves_free_propertylist(&temp_header[0]);
2937 uves_free_propertylist(&temp_header[1]);
2939 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2940 if (raw_headers[0] != NULL) {
2942 for (i = 0; i < frameset_size; i++) {
2943 if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
2944 if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
2947 cpl_free(raw_headers[0]); raw_headers[0] = NULL;
2948 cpl_free(raw_headers[1]); raw_headers[1] = NULL;
2950 uves_free_imagelist(&images[0]);
2951 uves_free_imagelist(&images[1]);
2953 uves_free_propertylist(&rotated_header[0]);
2954 uves_free_propertylist(&rotated_header[1]);
2957 return cpl_error_get_code();
2980 uves_load_orderpos(
const cpl_frameset *frames,
2982 const char **raw_filename,
2983 cpl_image *raw_image[2],
2987 const char *tags[4];
2989 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
2994 tags[0] = UVES_ORDER_FLAT(flames,
false);
2995 tags[1] = UVES_ORDER_FLAT(flames,
true);
2996 tags[2] = UVES_STD_STAR(
false);
2997 tags[3] = UVES_STD_STAR(
true);
3004 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
3006 "Could not find raw frame (%s) in SOF",
3012 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
3014 "Could not find raw frame (%s, %s, %s, or %s) in SOF",
3015 tags[0], tags[1], tags[2], tags[3]);
3017 *blue = (indx == 1) || (indx == 3);
3021 check( load_raw_image(*raw_filename,
3028 "Error loading image from file '%s'", *raw_filename);
3030 passure( !flames || !(*blue),
"%d %d",
3034 if (cpl_error_get_code() != CPL_ERROR_NONE)
3036 *raw_filename = NULL;
3039 return cpl_error_get_code();
3060 uves_load_formatcheck(
const cpl_frameset *frames,
3062 const char **raw_filename,
3063 cpl_image *raw_image[2],
3067 const char *tags[2];
3068 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3071 tags[0] = UVES_FORMATCHECK(flames,
false);
3072 tags[1] = UVES_FORMATCHECK(flames,
true);
3078 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3079 "Could not find raw frame (%s) in SOF",
3084 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3085 "Could not find raw frame (%s or %s) in SOF",
3088 *blue = (indx == 1);
3092 check( load_raw_image(*raw_filename,
3099 "Error loading image from file '%s'", *raw_filename);
3102 if (cpl_error_get_code() != CPL_ERROR_NONE)
3104 *raw_filename = NULL;
3106 return cpl_error_get_code();
3129 void uves_load_cd_align(
const cpl_frameset *frames,
3130 const char **raw_filename1,
3131 const char **raw_filename2,
3132 cpl_image *raw_image1[2],
3133 cpl_image *raw_image2[2],
3140 const char *tags[2];
3141 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3143 bool flames =
false;
3144 const cpl_frame *frame;
3146 tags[0] = UVES_CD_ALIGN(
false);
3147 tags[1] = UVES_CD_ALIGN(
true);
3149 check( *raw_filename1 = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3150 "Could not find raw frame (%s or %s) in SOF",
3153 *blue = (indx == 1);
3155 assure( cpl_frameset_count_tags(frames, tags[indx]) == 2,
3156 CPL_ERROR_ILLEGAL_INPUT,
3157 "%d %s frames found. Exactly 2 required",
3158 cpl_frameset_count_tags(frames, tags[indx]), tags[indx] );
3164 int nfrm=cpl_frameset_get_size(frames);
3165 for (i = 0;i < nfrm;i++)
3167 frame=cpl_frameset_get_frame_const(frames,i);
3168 if (strcmp(cpl_frame_get_tag(frame), tags[indx]) == 0)
3172 *raw_filename1 = cpl_frame_get_filename(frame);
3176 *raw_filename2 = cpl_frame_get_filename(frame);
3179 check( load_raw_image(n == 1 ?
3194 "Error loading image from file '%s'",
3195 n == 1 ? *raw_filename1 : *raw_filename2);
3203 if (cpl_error_get_code() != CPL_ERROR_NONE)
3205 *raw_filename1 = NULL;
3206 *raw_filename2 = NULL;
3236 uves_load_arclamp(
const cpl_frameset *frames,
3238 const char **raw_filename,
3243 const char *tags[4];
3245 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3253 tags[0] = UVES_ARC_LAMP(flames,
true);
3254 tags[1] = FLAMES_FIB_SCI_SIM;
3259 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3260 "Could not find raw frame (%s or %s) in SOF",
3263 *sim_cal = (indx == 1);
3267 tags[0] = UVES_ARC_LAMP(flames,
true);
3268 tags[1] = UVES_ARC_LAMP(flames,
false);
3269 tags[2] = UVES_ECH_ARC_LAMP(
true);
3270 tags[3] = UVES_ECH_ARC_LAMP(
false);
3272 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3273 "Could not find raw frame (%s, %s, %s or %s) in SOF",
3274 tags[0], tags[1], tags[2], tags[3]);
3276 *blue = (indx == 0 || indx == 2);
3280 check( load_raw_image(*raw_filename,
3287 "Error loading image from file '%s'", *raw_filename);
3290 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3291 *raw_filename = NULL;
3292 uves_free_image (raw_image);
3293 uves_free_propertylist(raw_header);
3315 uves_load_science(
const cpl_frameset *frames,
const char **raw_filename,
3316 cpl_image *raw_image[2],
3320 const char **sci_type)
3323 const char *tags[] =
3325 UVES_SCIENCE(
true), UVES_SCIENCE(
false),
3326 UVES_SCI_EXTND(
true), UVES_SCI_EXTND(
false),
3327 UVES_SCI_POINT(
true), UVES_SCI_POINT(
false),
3328 UVES_SCI_SLICER(
true), UVES_SCI_SLICER(
false),
3329 UVES_TFLAT(
true), UVES_TFLAT(
false)
3332 const char *type[] =
3334 "SCIENCE",
"SCIENCE",
3335 "SCI_EXTND",
"SCI_EXTND",
3336 "SCI_POINT",
"SCI_POINT",
3337 "SCI_SLICER",
"SCI_SLICER",
3341 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3343 bool flames =
false;
3345 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3346 "No science frame (%s, %s, %s, %s, %s, %s, %s, %s, %s or %s) in SOF",
3347 tags[0], tags[1], tags[2], tags[3],
3348 tags[4], tags[5], tags[6], tags[7], tags[7], tags[8]);
3350 *blue = (indx % 2 == 0);
3351 *sci_type = type[indx];
3354 check( load_raw_image(*raw_filename,
3361 "Error loading image from file '%s'", *raw_filename);
3363 if (cpl_error_get_code() != CPL_ERROR_NONE)
3365 *raw_filename = NULL;
3366 uves_free_image (raw_image);
3367 uves_free_propertylist(raw_header);
3369 return cpl_error_get_code();
3391 uves_load_standard(
const cpl_frameset *frames,
const char **raw_filename,
3392 cpl_image *raw_image[2],
3396 const char *tags[] = { UVES_STD_STAR(
true), UVES_STD_STAR(
false) };
3397 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3399 bool flames =
false;
3401 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3402 "Could not identify raw frame (%s or %s) in SOF", tags[0], tags[1]);
3404 *blue = (indx == 0);
3407 check( load_raw_image(*raw_filename,
3414 "Error loading image from file '%s'", *raw_filename);
3417 if (cpl_error_get_code() != CPL_ERROR_NONE)
3419 *raw_filename = NULL;
3420 uves_free_image (raw_image);
3421 uves_free_propertylist(raw_header);
3423 return cpl_error_get_code();
3445 uves_load_drs(
const cpl_frameset *frames,
3447 const char *chip_name,
3448 const char **drs_filename,
3450 enum uves_chip chip)
3452 const char *tags[1];
3453 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3458 tags[0] = UVES_DRS_SETUP(flames, chip);
3459 extension = UVES_DRS_SETUP_EXTENSION(chip);
3461 check( *drs_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3462 "Could not find DRS table (%s) in SOF", tags[0]);
3467 "Could not load header from extension %d of file '%s'", extension, *drs_filename);
3469 check_nomsg( uves_warn_if_chip_names_dont_match(*drs_header, chip_name, chip) );
3472 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3473 *drs_filename = NULL;
3474 uves_free_propertylist(drs_header);
3476 return cpl_error_get_code();
3489 uves_load_weights(
const cpl_frameset *frames,
const char **weights_filename,
3490 enum uves_chip chip)
3492 cpl_image *weights = NULL;
3493 const char *tags[1];
3494 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3498 assure( weights_filename != NULL, CPL_ERROR_NULL_INPUT,
"Null filename");
3500 tags[0] = UVES_WEIGHTS(chip);
3502 check( *weights_filename = uves_find_frame(frames,
3503 tags, number_of_tags, &indx, NULL),
3504 "Could not find '%s' in frame set", tags[0]);
3506 check( weights = cpl_image_load(*weights_filename,
3511 "Could not load master bias from extension %d of file '%s'",
3512 extension, *weights_filename);
3538 uves_load_mbias(
const cpl_frameset *frames,
const char *chip_name,
3539 const char **mbias_filename,
3542 const char *tags[1];
3543 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3548 *mbias_header = NULL;
3550 tags[0] = UVES_MASTER_BIAS (chip);
3551 extension = UVES_MASTER_BIAS_EXTENSION(chip);
3553 check( *mbias_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3554 "Could not find '%s' in frame set", tags[0]);
3557 check( *mbias = cpl_image_load(*mbias_filename,
3562 "Could not load master bias from extension %d of file '%s'",
3563 extension, *mbias_filename);
3568 "Could not load header from extension %d of file '%s'",
3569 extension, *mbias_filename);
3571 check_nomsg( uves_warn_if_chip_names_dont_match(*mbias_header, chip_name, chip) );
3574 if (cpl_error_get_code() != CPL_ERROR_NONE)
3576 *mbias_filename = NULL;
3577 uves_free_image(mbias);
3578 uves_free_propertylist(mbias_header);
3580 return cpl_error_get_code();
3603 uves_load_master_formatcheck(
const cpl_frameset *frames,
const char *chip_name,
3604 const char **mform_filename,
3607 const char *tags[1];
3608 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3613 *mform_header = NULL;
3615 tags[0] = UVES_MASTER_ARC_FORM (chip);
3616 extension = UVES_MASTER_ARC_FORM_EXTENSION(chip);
3618 check( *mform_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3619 "Could not find '%s' in frame set", tags[0]);
3622 check( *mform = cpl_image_load(*mform_filename,
3627 "Could not load master formatcheck from extension %d of file '%s'",
3628 extension, *mform_filename);
3634 "Could not load header from extension %d of file '%s'",
3635 extension, *mform_filename);
3637 check_nomsg( uves_warn_if_chip_names_dont_match(*mform_header, chip_name, chip) );
3640 if (cpl_error_get_code() != CPL_ERROR_NONE)
3642 *mform_filename = NULL;
3643 uves_free_image(mform);
3644 uves_free_propertylist(mform_header);
3646 return cpl_error_get_code();
3668 uves_load_mdark(
const cpl_frameset *frames,
const char *chip_name,
3669 const char **mdark_filename, cpl_image **mdark,
3672 const char *tags[2];
3673 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3678 *mdark_header = NULL;
3680 tags[0] = UVES_MASTER_DARK (chip);
3681 tags[1] = UVES_MASTER_PDARK (chip);
3682 extension = UVES_MASTER_DARK_EXTENSION(chip);
3684 check( *mdark_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3685 "Could not find %s or %s in frame set", tags[0], tags[1]);
3688 check( *mdark = cpl_image_load(*mdark_filename,
3693 "Could not load master dark from extension %d of file '%s'",
3694 extension, *mdark_filename);
3699 "Could not load header from extension %d of file '%s'",
3700 extension, *mdark_filename);
3702 check_nomsg( uves_warn_if_chip_names_dont_match(*mdark_header, chip_name, chip) );
3705 if (cpl_error_get_code() != CPL_ERROR_NONE)
3707 *mdark_filename = NULL;
3708 uves_free_image(mdark);
3709 uves_free_propertylist(mdark_header);
3711 return cpl_error_get_code();
3731 uves_load_ref_flat(
const cpl_frameset *frames,
const char *chip_name,
3732 const char **filename, cpl_image **rflat,
3735 const char *tags[1];
3736 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3741 *rflat_header = NULL;
3743 tags[0] = UVES_REF_TFLAT(chip);
3744 extension = UVES_MASTER_FLAT_EXTENSION(chip);
3746 check( *filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3747 "Could not find %s in frame set", tags[0]);
3749 check( *rflat = cpl_image_load(*filename,
3754 "Could not load reference dark from extension %d of file '%s'",
3755 extension, *filename);
3759 "Could not load header from extension %d of file '%s'",
3760 extension, *filename);
3762 check_nomsg( uves_warn_if_chip_names_dont_match(*rflat_header, chip_name, chip) );
3765 if (cpl_error_get_code() != CPL_ERROR_NONE)
3768 uves_free_image(rflat);
3769 uves_free_propertylist(rflat_header);
3794 uves_load_mflat_const(
const cpl_frameset *frames,
const char *chip_name,
3795 const char **mflat_filename,
3797 enum uves_chip chip,
3798 const cpl_frame **mflat_frame)
3800 const char *tags[6];
3801 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3806 *mflat_header = NULL;
3808 tags[0] = UVES_REF_TFLAT (chip);
3809 tags[1] = UVES_MASTER_FLAT (chip);
3810 tags[2] = UVES_MASTER_DFLAT (chip);
3811 tags[3] = UVES_MASTER_IFLAT (chip);
3812 tags[4] = UVES_MASTER_TFLAT (chip);
3813 tags[5] = UVES_MASTER_SCREEN_FLAT (chip);
3814 extension = UVES_MASTER_FLAT_EXTENSION(chip);
3816 check( *mflat_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
3818 "Could not find '%s', '%s', '%s', '%s' or '%s' in frame set",
3819 tags[0], tags[1], tags[2], tags[3], tags[4]);
3822 check( *mflat = cpl_image_load(*mflat_filename,
3827 "Could not load master flat from extension %d of file '%s'",
3828 extension, *mflat_filename);
3833 "Could not load header from extension %d of file '%s'",
3834 extension, *mflat_filename);
3836 check_nomsg( uves_warn_if_chip_names_dont_match(*mflat_header, chip_name, chip) );
3839 if (cpl_error_get_code() != CPL_ERROR_NONE)
3841 *mflat_filename = NULL;
3842 uves_free_image(mflat);
3843 uves_free_propertylist(mflat_header);
3845 return cpl_error_get_code();
3865 uves_load_mflat(cpl_frameset *frames,
const char *chip_name,
3866 const char **mflat_filename,
3868 cpl_frame **mflat_frame)
3870 return uves_load_mflat_const((
const cpl_frameset *)frames,
3873 mflat, mflat_header, chip,
3874 (
const cpl_frame **) mflat_frame);
3911 uves_load_ordertable(
const cpl_frameset *frames,
3913 const char *chip_name,
3914 const char **ordertable_filename,
3915 cpl_table **ordertable,
3920 int *tab_in_out_oshift,
3921 double *tab_in_out_yshift,
3924 enum uves_chip chip,
3929 const char *tags[1];
3930 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3931 bool format_is_midas;
3933 double *tioy = NULL;
3936 double *fibre_pos = NULL;
3937 int *fibre_mask = NULL;
3941 tags[0] = UVES_GUESS_ORDER_TABLE(flames, chip);
3945 tags[0] = UVES_ORDER_TABLE(flames, chip);
3948 check( *ordertable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3949 "No order table (%s) found in SOF", tags[0]);
3951 check( *ordertable = cpl_table_load(*ordertable_filename,
3952 UVES_ORDER_TABLE_EXTENSION,
3955 "Error loading order table from extension %d of file '%s'",
3956 UVES_ORDER_TABLE_EXTENSION, *ordertable_filename);
3958 assure(ordertable_header != NULL,CPL_ERROR_NULL_INPUT,
3959 "NULL primary header uves_propertylist variable header");
3961 "Could not load header from extension 0 of '%s'", *ordertable_filename);
3963 if(ordertable_xheader != NULL) {
3966 "Could not load header from extension 1 of '%s'", *ordertable_filename);
3971 check_nomsg( uves_warn_if_chip_names_dont_match(*ordertable_header, chip_name, chip) );
3973 check(uves_check_if_format_is_midas(*ordertable_header,&format_is_midas),
3974 "Error getting FITS format");
3977 if (!format_is_midas && !flames)
3988 if (cpl_table_has_column(*ordertable,
"ORDER"))
3990 cpl_table_name_column(*ordertable,
"ORDER",
"Order");
3992 if (cpl_table_has_column(*ordertable,
"YFIT"))
3994 cpl_table_name_column(*ordertable,
"YFIT",
"Yfit");
3997 if (order_locations != NULL)
3999 check( *order_locations =
4000 load_polynomial(*ordertable_filename, UVES_ORDER_TABLE_EXTENSION_POLY),
4001 "Could not read polynomial from extension %d of file '%s'",
4002 UVES_ORDER_TABLE_EXTENSION_POLY, *ordertable_filename);
4007 check( *traces = cpl_table_load(*ordertable_filename,
4008 UVES_ORDER_TABLE_EXTENSION_FIBRE,
4011 "Error loading fibre table from extension %d of file '%s'",
4012 UVES_ORDER_TABLE_EXTENSION_FIBRE, *ordertable_filename);
4019 check(( cpl_table_cast_column (*ordertable,
"ORDER",
"Order", CPL_TYPE_INT),
4020 cpl_table_erase_column(*ordertable,
"ORDER")),
4021 "Error casting and renaming column 'ORDER'");
4023 check( cpl_table_name_column(*ordertable,
"YFIT",
"Yfit"),
4024 "Error renaming column 'YFIT'");
4030 "Could not load header from extension 1 of '%s'",
4031 *ordertable_filename);
4035 "Could not load header from extension 0 of '%s'",
4036 *ordertable_filename);
4041 if (order_locations != NULL)
4043 check( *order_locations =
4044 uves_polynomial_convert_from_plist_midas(midas_header,
"COEFF",-1),
4045 "Error reading polynomial from %s", *ordertable_filename);
4049 if (flames && tab_in_out_oshift != NULL )
4055 check( tioo = uves_read_midas_array(
4056 midas_header,
"TAB_IN_OUT_OSHIFT", &tioo_length,
4058 "Error reading TAB_IN_OUT_OSHIFT from MIDAS header");
4060 assure( tioo_type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4061 "Type of TAB_IN_OUT_OSHIFT is %s, double expected",
4064 if (tioo_length != 1)
4067 "%d expected", tioo_length, 1);
4070 *tab_in_out_oshift = tioo[0];
4076 if (flames && tab_in_out_yshift != NULL)
4082 check( tioy = uves_read_midas_array(
4083 midas_header,
"TAB_IN_OUT_YSHIFT", &tioy_length,
4085 "Error reading TAB_IN_OUT_YSHIFT from MIDAS header");
4087 assure( tioy_type == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
4088 "Type of TAB_IN_OUT_YSHIFT is %s, double expected",
4091 if (tioy_length != 1)
4094 "%d expected", tioy_length, 1);
4097 *tab_in_out_yshift = tioy[0];
4110 double fibre_offset = 0.0;
4121 int fibre_pos_length;
4122 int fibre_mask_length;
4123 cpl_type fibre_pos_type;
4124 cpl_type fibre_mask_type;
4127 check( fibre_pos = uves_read_midas_array(
4128 midas_header,
"FIBREPOS", &fibre_pos_length,
4129 &fibre_pos_type, NULL),
4130 "Error reading FIBREPOS from MIDAS header");
4132 assure( fibre_pos_type == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
4133 "Type of FIBREPOS is %s, double expected",
4136 check( fibre_mask = uves_read_midas_array(
4137 midas_header,
"FIBREMASK", &fibre_mask_length,
4138 &fibre_mask_type, NULL),
4139 "Error reading FIBREMASK from MIDAS header");
4141 assure( fibre_mask_type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4142 "Type of FIBREMASK is %s, double expected",
4145 assure( fibre_pos_length == fibre_mask_length,
4146 CPL_ERROR_INCOMPATIBLE_INPUT,
4147 "FIBREMASK has length %d, but "
4148 "FIBREPOS has length %d",
4149 fibre_mask_length, fibre_pos_length );
4151 *fib_pos= cpl_malloc(
sizeof(
double) * fibre_pos_length);
4152 *fib_msk= cpl_malloc(
sizeof(
int) * fibre_mask_length);
4154 for (fibre_ID = 0; fibre_ID < fibre_mask_length; fibre_ID++)
4157 fibre_ID, fibre_pos[fibre_ID],
4158 fibre_mask[fibre_ID] ?
4159 "enabled" :
"disabled");
4162 fibre_pos[fibre_ID],
4163 fibre_mask[fibre_ID]);
4164 (*fib_pos)[fibre_ID]=fibre_pos[fibre_ID];
4165 (*fib_msk)[fibre_ID]=fibre_mask[fibre_ID];
4172 uves_free_propertylist(&midas_header);
4173 uves_free_double(&fibre_pos);
4174 uves_free_int(&fibre_mask);
4175 uves_free_int(&tioo);
4176 uves_free_double(&tioy);
4177 uves_free_propertylist(&prime_header);
4179 if (cpl_error_get_code() != CPL_ERROR_NONE)
4181 *ordertable_filename = NULL;
4182 uves_free_table (ordertable);
4183 uves_free_propertylist(ordertable_header);
4185 if (traces != NULL) uves_free_table (traces);
4187 return cpl_error_get_code();
4212 const char* drs_id=NULL;
4215 if (strstr(drs_id,
"CPL") != NULL ||
4216 strstr(drs_id,
"cpl") != NULL) {
4217 *format_is_midas =
false;
4219 }
else if (strstr(drs_id,
"MIDAS") != NULL ||
4220 strstr(drs_id,
"midas") != NULL) {
4221 *format_is_midas =
true;
4224 assure (
false, CPL_ERROR_ILLEGAL_INPUT,
4225 "Unrecognized order table format, DRS_ID = '%s'", drs_id);
4229 *format_is_midas =
true;
4230 uves_msg_debug(
"No '%s' keyword found. Assuming MIDAS format", UVES_DRS_ID);
4234 return cpl_error_get_code();
4250 static cpl_error_code
4251 create_column_pixelsize(cpl_table *linetable)
4254 cpl_table *t = NULL;
4260 check( t = uves_extract_table_rows(linetable,
"Ident", CPL_GREATER_THAN, 0.1),
4261 "Error deleting rows with Ident=0");
4264 check(( cpl_table_duplicate_column(t,
"Aux", t,
"Ident"),
4265 cpl_table_multiply_columns(t,
"Aux",
"Order")),
4266 "Error creating 'Aux' column");
4274 "Regression failed");
4277 "Error reading polynomial coefficient");
4280 "Error reading polynomial coefficient");
4282 cpl_table_new_column(linetable, LINETAB_PIXELSIZE, CPL_TYPE_DOUBLE);
4284 for (i = 0; i < cpl_table_get_nrow(linetable); i++)
4291 check(( x = cpl_table_get_double(linetable,
"X", i, NULL),
4292 order = cpl_table_get_int (linetable,
"Order", i, NULL),
4293 ident = cpl_table_get_double(linetable,
"Ident", i, NULL)),
4294 "Error reading line table");
4296 assure( order != 0, CPL_ERROR_ILLEGAL_INPUT,
"Illegal order number: %d", order);
4304 pixelsize = (d1 + 2*d2* x) / order;
4309 cpl_table_set_double(linetable, LINETAB_PIXELSIZE, i, pixelsize);
4313 cpl_table_set_invalid(linetable, LINETAB_PIXELSIZE, i);
4318 uves_free_table(&t);
4320 return cpl_error_get_code();
4354 align_order_line_table(cpl_table *linetable,
const polynomial *absolute_order,
4356 const polynomial *order_locations,
int minorder,
int maxorder)
4360 assure ( order_locations != NULL, CPL_ERROR_NULL_INPUT,
4361 "Null order locations polynomial!");
4363 assure ( absolute_order != NULL, CPL_ERROR_NULL_INPUT,
4364 "Null absolute order pllynomial!");
4365 assure( cpl_table_has_column(linetable,
"X" ), CPL_ERROR_DATA_NOT_FOUND,
4366 "Missing line table column 'X'");
4367 assure( cpl_table_has_column(linetable,
"Ynew"), CPL_ERROR_DATA_NOT_FOUND,
4368 "Missing line table column 'Ynew'");
4369 assure( cpl_table_has_column(linetable,
"Order"), CPL_ERROR_DATA_NOT_FOUND,
4370 "Missing line table column 'Order'");
4372 assure( cpl_table_get_column_type(linetable,
"X") == CPL_TYPE_DOUBLE,
4373 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'X' has type %s (double expected))",
4376 assure( cpl_table_get_column_type(linetable,
"Ynew") == CPL_TYPE_DOUBLE,
4377 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'Ynew' has type %s (double expected))",
4380 assure( cpl_table_get_column_type(linetable,
"Y") == CPL_TYPE_INT,
4381 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'Y' has type %s (integer expected))",
4385 if (linetable_header != NULL)
4388 int line_first, line_last;
4389 int ord_first, ord_last;
4394 int x, y, order, absorder;
4398 maxx = uves_round_double(cpl_table_get_column_max(linetable,
"X"));
4400 minx = uves_round_double(cpl_table_get_column_min(linetable,
"X"));
4402 assure( 1 <= minx && minx <= maxx, CPL_ERROR_ILLEGAL_INPUT,
4403 "Illegal min/max line x positions: %d/%d, must be > 1",
4407 x = (minx + maxx) / 2;
4408 order = (minorder + maxorder) / 2;
4418 assure ( order_locations != NULL, CPL_ERROR_NULL_INPUT,
4419 "Null order locations polynomial!");
4426 "rounding to %d", x, y,
4429 ord_first = absorder + (minorder - order) * coeff;
4430 ord_last = absorder + (maxorder - order) * coeff;
4435 "Could not read order number from line table header");
4439 "Could not read order number from line table header");
4441 uves_msg_debug(
"Order table range: %d - %d. Line table range: %d - %d",
4442 ord_first, ord_last, line_first, line_last);
4444 if (line_first != ord_first ||
4445 line_last != ord_last)
4448 "Line table contains orders %d - %d. "
4449 "Order table contains orders %d - %d. "
4450 "Correcting on the fly",
4451 line_first, line_last, ord_first, ord_last);
4455 "Could not write corrected first absolute order number");
4458 "Could not write corrected first absolute order number");
4461 ord_first, ord_last);
4470 double epsilon = 0.01;
4475 if (fabs(cpl_table_get_column_median(linetable,
"Y") -
4476 cpl_table_get_column_median(linetable,
"Order")) > epsilon)
4488 cpl_table_erase_column(linetable,
"Y");
4538 uves_load_linetable(
const cpl_frameset *frames,
4540 const char *chip_name,
4541 const polynomial *order_locations,
int minorder,
int maxorder,
4542 const char **linetable_filename,
4543 cpl_table **linetable,
4547 enum uves_chip chip,
int trace_id,
int window)
4552 int *absorders = NULL;
4553 cpl_table *temp = NULL;
4555 const char *tags[3];
4556 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
4558 bool format_is_midas;
4565 tags[0] = UVES_GUESS_LINE_TABLE(flames, chip);
4566 tags[1] = UVES_LINE_TABLE(flames, chip);
4567 tags[2] = UVES_LINE_TABLE(flames, chip);
4570 check( *linetable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
4571 "No line table (%s, %s or %s) found in SOF", tags[0], tags[1], tags[2]);
4575 tags[0] = UVES_LINE_TABLE(flames, chip);
4576 tags[1] = UVES_LINE_TABLE(flames, chip);
4577 tags[2] = UVES_GUESS_LINE_TABLE(flames, chip);
4581 if (cpl_frameset_find_const(frames, tags[0]) == NULL &&
4582 cpl_frameset_find_const(frames, tags[1]) == NULL &&
4583 cpl_frameset_find_const(frames, tags[2]) == NULL)
4591 tags[0] = UVES_LINE_TABLE_MIDAS(chip, window);
4592 tags[1] = UVES_LINE_TABLE_MIDAS(chip, window);
4593 tags[2] = UVES_LINE_TABLE_MIDAS(chip, window);
4600 tags[0] = UVES_LINE_TABLE_MIDAS(chip, 1);
4601 tags[1] = UVES_LINE_TABLE_MIDAS(chip, 2);
4602 tags[2] = UVES_LINE_TABLE_MIDAS(chip, 3);
4604 uves_msg_debug(
"Trying %s, %s or %s", tags[0], tags[1], tags[2]);
4608 check( *linetable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
4609 "No line table (%s, %s or %s) found in SOF", tags[0], tags[1], tags[2]);
4614 "Could not load primary header of '%s'", *linetable_filename);
4616 check_nomsg( uves_warn_if_chip_names_dont_match(primary_header, chip_name, chip) );
4622 if (strstr(drs_id,
"CPL") != NULL || strstr(drs_id,
"cpl") != NULL)
4624 format_is_midas =
false;
4627 else if (strstr(drs_id,
"MIDAS") != NULL || strstr(drs_id,
"midas") != NULL)
4629 format_is_midas =
true;
4635 CPL_ERROR_ILLEGAL_INPUT,
4636 "Unrecognized line table format, DRS_ID = '%s'", drs_id);
4641 format_is_midas =
true;
4642 uves_msg_debug(
"No '%s' keyword found. Assuming MIDAS format", UVES_DRS_ID);
4645 if (format_is_midas || flames)
4649 assure( trace_id == 0 && (window == -1 || (1 <= window && window <= 3)),
4650 CPL_ERROR_UNSUPPORTED_MODE,
4651 "Cannot read (fibre, window) = (%d, %d) from MIDAS line table",
4661 assure( ((1<= trace_id && trace_id <= 9) && (window == -1)),
4662 CPL_ERROR_UNSUPPORTED_MODE,
4663 "Cannot read (fibre, window) = (%d, %d) from MIDAS line table",
4682 check( nextensions = uves_get_nextensions(*linetable_filename),
4683 "Error reading number of extensions of file '%s'", *linetable_filename);
4689 for (base_extension = 1; base_extension < nextensions && !found; base_extension++)
4695 check(( uves_free_propertylist(&header),
4697 "Could not header of extension %d of '%s'",
4698 base_extension, *linetable_filename);
4701 "Error reading trace ID from header of extension %d of '%s'",
4702 base_extension, *linetable_filename);
4705 "Error reading window number from header of extension %d of '%s'",
4706 base_extension, *linetable_filename);
4708 uves_msg_debug(
"Found (trace, window) = (%d, %d), need (%d, %d)",
4709 header_trace, header_window,
4712 found = ( (trace_id == header_trace) &&
4713 (window == -1 || window == header_window) );
4717 CPL_ERROR_ILLEGAL_INPUT,
4718 "Line table (trace, window) = (%d, %d) is not present in file '%s'",
4719 trace_id, window, *linetable_filename);
4723 base_extension -= 2;
4727 check( *linetable = cpl_table_load(*linetable_filename,
4728 base_extension + UVES_LINE_TABLE_EXTENSION,
4731 "Error loading line table from extension %d of file '%s'",
4732 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4735 if (linetable_header != NULL)
4737 check( *linetable_header =
4739 base_extension + UVES_LINE_TABLE_EXTENSION),
4740 "Could not load header of extension %d of '%s'",
4741 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4743 if (format_is_midas)
4747 absorders = uves_read_midas_array(*linetable_header,
"ORDER", &size,
4750 assure( type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4751 "Type of ORDER is %s, int expected",
4755 CPL_ERROR_ILLEGAL_INPUT,
4756 "'ORDER' array has size %d. Size 2 expected.", size);
4759 "Error updating table header");
4764 if (format_is_midas)
4767 check(( cpl_table_cast_column(*linetable,
"X",
"xxxx", CPL_TYPE_DOUBLE),
4768 cpl_table_erase_column(*linetable,
"X"),
4769 cpl_table_name_column(*linetable,
"xxxx",
"X")),
4770 "Error casting and renaming column 'X'");
4772 check(( cpl_table_cast_column(*linetable,
"YNEW",
"xxxx", CPL_TYPE_DOUBLE),
4773 cpl_table_erase_column(*linetable,
"YNEW"),
4774 cpl_table_name_column(*linetable,
"xxxx",
"Ynew")),
4775 "Error casting and renaming column 'YNEW'");
4777 check(( cpl_table_cast_column(*linetable,
"Y",
"xxxx", CPL_TYPE_INT),
4778 cpl_table_erase_column(*linetable,
"Y"),
4779 cpl_table_name_column(*linetable,
"xxxx",
"Y")),
4780 "Error casting and renaming column 'Y'");
4782 check(( cpl_table_cast_column(*linetable,
"ORDER",
"Order", CPL_TYPE_INT),
4783 cpl_table_erase_column(*linetable,
"ORDER")),
4784 "Error casting and renaming column 'ORDER'");
4786 check( cpl_table_name_column(*linetable,
"IDENT",
"Ident"),
4787 "Error renaming column 'IDENT'");
4790 *linetable_filename,
4791 base_extension + UVES_LINE_TABLE_EXTENSION),
4792 "Could not load header of extension %d of '%s'",
4793 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4795 if (dispersion_relation != NULL) {
4797 check( *dispersion_relation =
4798 uves_polynomial_convert_from_plist_midas(midas_header,
4800 "Error reading polynomial 'REGR%d' from '%s'",
4802 *linetable_filename);
4805 check( *dispersion_relation =
4806 uves_polynomial_convert_from_plist_midas(midas_header,
4808 "Error reading polynomial 'REGR' from '%s'",
4809 *linetable_filename);
4814 check( absolute_order_local =
4815 uves_polynomial_convert_from_plist_midas(midas_header,
"RORD",-1),
4816 "Error reading polynomial 'RORD' from '%s'", *linetable_filename);
4831 if (cpl_table_has_column(*linetable,
"YNEW"))
4833 cpl_table_name_column(*linetable,
"YNEW",
"Ynew");
4836 if (dispersion_relation != NULL)
4838 check( *dispersion_relation = load_polynomial(
4839 *linetable_filename,
4840 base_extension + UVES_LINE_TABLE_EXTENSION_DISPERSION),
4841 "Could not read polynomial from extension %d of file '%s'",
4842 base_extension + UVES_LINE_TABLE_EXTENSION_DISPERSION,
4843 *linetable_filename);
4846 check( absolute_order_local =
4847 load_polynomial(*linetable_filename,
4848 base_extension + UVES_LINE_TABLE_EXTENSION_ABSORDER),
4849 "Could not read polynomial from extension %d of file '%s'",
4850 base_extension + UVES_LINE_TABLE_EXTENSION_ABSORDER, *linetable_filename);
4853 if (absolute_order != NULL)
4859 check( align_order_line_table(
4860 *linetable, absolute_order_local, linetable_header,
4861 order_locations, minorder, maxorder),
4862 "Error while aligning line/order tables");
4867 const char *colname;
4875 uves_free_table(&temp);
4876 check(( temp = cpl_table_new(0),
4877 cpl_table_copy_structure(temp, *linetable)),
4878 "Error duplicating line table column structure");
4880 colname = cpl_table_get_column_name(temp);
4881 while (colname != NULL)
4883 if (!(strcmp(colname,
"X" ) == 0 ||
4884 strcmp(colname,
"Order" ) == 0 ||
4885 strcmp(colname,
"Ident" ) == 0 ||
4886 strcmp(colname,
"FIBRE" ) == 0 ||
4887 strcmp(colname,
"Fibre" ) == 0 ||
4888 strcmp(colname, LINETAB_PIXELSIZE) == 0))
4890 cpl_table_erase_column(*linetable, colname);
4895 colname = cpl_table_get_column_name(NULL);
4902 if ( !cpl_table_has_column(*linetable, LINETAB_PIXELSIZE) )
4904 check( create_column_pixelsize(*linetable),
4905 "Error adding 'Pixelsize' column");
4909 check( uves_erase_invalid_table_rows(*linetable,
"Ident"),
4910 "Error deleting rows with illegal 'Ident' value");
4912 check( uves_erase_table_rows(*linetable,
"Ident", CPL_LESS_THAN, 0.01),
4913 "Error deleting rows with illegal 'Ident' value");
4916 assure( uves_erase_invalid_table_rows(*linetable, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
4917 "After deleting rows with invalid 'Ident' values, "
4918 "the table in extension %d of file '%s' still contains invalid rows",
4919 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4922 check( uves_sort_table_2(*linetable,
"Order",
"X",
false,
false),
"Error sorting line table");
4925 uves_free_propertylist(&primary_header);
4926 uves_free_propertylist(&header);
4927 uves_free_propertylist(&midas_header);
4928 uves_free_table(&temp);
4930 cpl_free(absorders);
4931 if (cpl_error_get_code() != CPL_ERROR_NONE) {
4932 *linetable_filename = NULL;
4933 uves_free_table(linetable);
4946 uves_load_linetable_const(
const cpl_frameset *frames,
4948 const char *chip_name,
4949 const polynomial *order_locations,
int minorder,
int maxorder,
4950 const char **linetable_filename,
4951 const cpl_table **linetable,
4955 enum uves_chip chip,
int trace_id,
int window)
4957 uves_load_linetable(frames, flames, chip_name, order_locations,
4960 (cpl_table **)linetable,
4964 chip, trace_id, window);
4987 uves_load_response_curve(
const cpl_frameset *frames,
const char *chip_name,
4988 const char **response_filename,
4989 cpl_image **response_curve,
4990 cpl_table **master_response,
4993 const char *tags[2];
4994 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
4998 *response_curve = NULL;
4999 *response_header = NULL;
5000 *master_response = NULL;
5002 tags[0] = UVES_INSTR_RESPONSE (chip);
5003 tags[1] = UVES_MASTER_RESPONSE(chip);
5005 check( *response_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
5007 "Could not find '%s' in frame set", tags[0]);
5012 extension = UVES_INSTR_RESPONSE_EXTENSION(chip);
5021 check( *response_curve = uves_load_image_file(*response_filename,
5028 "Could not load response curve from extension %d of file '%s'",
5029 extension, *response_filename);
5038 check_nomsg( uves_warn_if_chip_names_dont_match(*response_header, chip_name, chip) );
5043 extension = UVES_MASTER_RESPONSE_EXTENSION(chip);
5045 check( *master_response = cpl_table_load(*response_filename,
5046 UVES_LINE_INTMON_TABLE_EXTENSION,
5049 "Error master response curve from extension %d of file '%s'",
5050 extension, *response_filename);
5053 check(( cpl_table_cast_column(*master_response,
"LAMBDA",
"LAMBDA_double",
5055 cpl_table_erase_column(*master_response,
"LAMBDA"),
5056 cpl_table_name_column(*master_response,
"LAMBDA_double",
"LAMBDA")),
5057 "Could not cast column 'LAMBDA'");
5059 check(( cpl_table_cast_column(*master_response,
"FLUX_CONV",
"FLUX_CONV_double",
5061 cpl_table_erase_column(*master_response,
"FLUX_CONV"),
5062 cpl_table_name_column(*master_response,
"FLUX_CONV_double",
"FLUX_CONV")),
5063 "Could not cast column 'FLUX_CONV'");
5070 if (cpl_error_get_code() != CPL_ERROR_NONE)
5072 *response_filename = NULL;
5073 uves_free_image(response_curve);
5074 uves_free_propertylist(response_header);
5076 return cpl_error_get_code();
5091 cpl_error_code uves_load_lineintmon(
const cpl_frameset *frames,
5092 const char **line_intmon_filename,
5093 cpl_table **line_intmon)
5095 const char *tags[1] = {UVES_LINE_INTMON_TABLE};
5097 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5101 check( *line_intmon_filename = uves_find_frame(frames, tags, number_of_tags,
5103 "No line intensity table (%s) found in SOF", tags[0]);
5106 check( *line_intmon = cpl_table_load(*line_intmon_filename,
5107 UVES_LINE_INTMON_TABLE_EXTENSION,
5110 "Error loading line reference table from extension %d of file '%s'",
5111 UVES_LINE_INTMON_TABLE_EXTENSION, *line_intmon_filename);
5113 check(( cpl_table_cast_column(*line_intmon,
"WAVE",
"Wave", CPL_TYPE_DOUBLE),
5114 cpl_table_erase_column(*line_intmon,
"WAVE")),
5115 "Could not cast and rename column");
5118 check( uves_sort_table_1(*line_intmon,
"Wave",
false),
"Error sorting table");
5121 if (cpl_error_get_code() != CPL_ERROR_NONE)
5123 *line_intmon_filename = NULL;
5124 uves_free_table(line_intmon);
5126 return cpl_error_get_code();
5144 uves_load_corvel(
const cpl_frameset *frames,
5147 const char **corvel_filename)
5149 const char *tags[1];
5150 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5154 tags[0] = FLAMES_CORVEL_MASK;
5157 assure_nomsg( corvel_filename != NULL, CPL_ERROR_NULL_INPUT );
5160 check( *corvel_filename = uves_find_frame(frames, tags, number_of_tags,
5162 "No velocity correction table (%s) found in SOF", tags[0]);
5166 check( *corvel = cpl_table_load(*corvel_filename,
5170 "Error loading line reference table from extension %d of file '%s'",
5171 extension, *corvel_filename);
5174 if (corvel_header != NULL)
5179 "Could not load header from extension %d of file %s",
5180 extension, *corvel_filename);
5185 if (cpl_error_get_code() != CPL_ERROR_NONE)
5187 *corvel_filename = NULL;
5188 uves_free_table(corvel);
5211 uves_load_linerefertable(
const cpl_frameset *frames,
5212 const char **line_refer_filename,
5215 const char *tags[1] = {UVES_LINE_REFER_TABLE};
5217 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5221 check( *line_refer_filename = uves_find_frame(frames, tags, number_of_tags,
5223 "No line reference table (%s) found in SOF", tags[0]);
5226 check( *line_refer = cpl_table_load(*line_refer_filename,
5227 UVES_LINE_REFER_TABLE_EXTENSION,
5230 "Error loading line reference table from extension %d of file '%s'",
5231 UVES_LINE_REFER_TABLE_EXTENSION, *line_refer_filename);
5234 if (line_refer_header != NULL)
5237 "Could not load header of line_refer table in '%s'", *line_refer_filename);
5240 assure( uves_erase_invalid_table_rows(*line_refer, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
5241 "Table in extension %d of file '%s' contains invalid rows",
5242 UVES_LINE_REFER_TABLE_EXTENSION, *line_refer_filename);
5244 check(( cpl_table_cast_column(*line_refer,
"WAVE",
"Wave", CPL_TYPE_DOUBLE),
5245 cpl_table_erase_column(*line_refer,
"WAVE")),
5246 "Could not cast and rename column");
5259 check(( cpl_table_duplicate_column(*line_refer,
"dWave", *line_refer,
"Wave"),
5260 cpl_table_divide_scalar (*line_refer,
"dWave", 300000*10)),
5261 "Error writing wavelength uncertainties");
5264 check(( cpl_table_new_column(*line_refer,
"dWave", CPL_TYPE_DOUBLE),
5265 cpl_table_fill_column_window(*line_refer,
5268 cpl_table_get_nrow(*line_refer), 0.002)),
5269 "Error writing wavelength uncertainties");
5273 check( uves_sort_table_1(*line_refer,
"Wave",
false),
"Error sorting table");
5276 if (cpl_error_get_code() != CPL_ERROR_NONE) {
5277 *line_refer_filename = NULL;
5278 uves_free_table (line_refer);
5279 if (line_refer_header != NULL) uves_free_propertylist(line_refer_header);
5281 return cpl_error_get_code();
5300 uves_load_flux_table(
const cpl_frameset *frames,
const char **flux_table_filename,
5301 cpl_table **flux_table)
5303 const char *tags[1] = {UVES_FLUX_STD_TABLE};
5305 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5309 check( *flux_table_filename = uves_find_frame(frames, tags, number_of_tags,
5311 "No standard star flux table (%s) in SOF", tags[0]);
5314 check( *flux_table = cpl_table_load(*flux_table_filename,
5315 UVES_FLUX_STD_TABLE_EXTENSION,
5318 "Error loading flux table from extension %d of file '%s'",
5319 UVES_FLUX_STD_TABLE_EXTENSION, *flux_table_filename);
5326 if (uves_erase_invalid_table_rows(*flux_table, NULL) != 0)
5328 uves_msg_warning(
"Table in extension %d of file '%s' contains null values",
5329 UVES_FLUX_STD_TABLE_EXTENSION, *flux_table_filename);
5335 for (i = 0; i < cpl_table_get_nrow(*flux_table); i++)
5337 if (cpl_table_get_string(*flux_table,
"TYPE", i) == NULL)
5339 cpl_table_set_string(*flux_table,
"TYPE", i,
"NULL");
5346 if (cpl_error_get_code() != CPL_ERROR_NONE)
5348 *flux_table_filename = NULL;
5349 uves_free_table(flux_table);
5351 return cpl_error_get_code();
5371 uves_load_atmo_ext(
const cpl_frameset *frames,
const char **atmext_table_filename,
5372 cpl_table **atmext_table)
5374 const char *tags[1] = {UVES_EXTCOEFF_TABLE};
5376 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5380 check( *atmext_table_filename = uves_find_frame(frames, tags, number_of_tags,
5382 "No atmospheric extinction table (%s) found in SOF", tags[0]);
5385 check( *atmext_table = cpl_table_load(*atmext_table_filename,
5386 UVES_EXTCOEFF_TABLE_EXTENSION,
5389 "Error loading atmospheric extinction table from extension %d of file '%s'",
5390 UVES_EXTCOEFF_TABLE_EXTENSION, *atmext_table_filename);
5392 assure( uves_erase_invalid_table_rows(*atmext_table, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
5393 "Table in extension %d of file '%s' contains invalid rows",
5394 UVES_EXTCOEFF_TABLE_EXTENSION, *atmext_table_filename);
5396 check( uves_sort_table_1(*atmext_table,
"LAMBDA",
false),
5397 "Error sorting table");
5400 check(( cpl_table_cast_column(*atmext_table,
"LAMBDA",
"LAMBDA_double", CPL_TYPE_DOUBLE),
5401 cpl_table_erase_column(*atmext_table,
"LAMBDA"),
5402 cpl_table_name_column(*atmext_table,
"LAMBDA_double",
"LAMBDA")),
5403 "Could not cast column 'LAMBDA'");
5405 check(( cpl_table_cast_column(*atmext_table,
"LA_SILLA",
"LA_SILLA_double", CPL_TYPE_DOUBLE),
5406 cpl_table_erase_column(*atmext_table,
"LA_SILLA"),
5407 cpl_table_name_column(*atmext_table,
"LA_SILLA_double",
"LA_SILLA")),
5408 "Could not cast column 'LA_SILLA'");
5411 if (cpl_error_get_code() != CPL_ERROR_NONE)
5413 *atmext_table_filename = NULL;
5414 uves_free_table(atmext_table);
5416 return cpl_error_get_code();
5428 uves_guess_order_table_filename(
enum uves_chip chip)
5430 return uves_local_filename(
"orderguesstable", chip, -1, -1);
5443 uves_order_table_filename(
enum uves_chip chip)
5445 return uves_local_filename(
"ordertable", chip, -1, -1);
5456 char *uves_ordef_filename(
enum uves_chip chip)
5458 return uves_local_filename(
"order_def", chip, -1, -1);
5471 uves_masterdark_filename(
enum uves_chip chip)
5473 return uves_local_filename(
"masterdark", chip, -1, -1);
5485 uves_flat_ratio_filename(
enum uves_chip chip)
5487 return uves_local_filename(
"ratio", chip, -1, -1);
5498 char *uves_cd_align_filename(
enum uves_chip chip)
5500 return uves_local_filename(
"cd_align", chip, -1, -1);
5513 uves_masterflat_filename(
enum uves_chip chip)
5515 return uves_local_filename(
"masterflat", chip, -1, -1);
5527 uves_masterflat_bkg_filename(
enum uves_chip chip)
5529 return uves_local_filename(
"masterflat_bkg", chip, -1, -1);
5542 uves_masterbias_filename(
enum uves_chip chip)
5544 return uves_local_filename(
"masterbias", chip, -1, -1);
5557 uves_guess_line_table_filename(
enum uves_chip chip)
5559 return uves_local_filename(
"lineguesstable", chip, -1, -1);
5571 uves_line_table_filename(
enum uves_chip chip)
5573 return uves_local_filename(
"linetable", chip, -1, -1);
5586 uves_line_table_filename_paf(
enum uves_chip chip)
5588 return uves_local_filename(
"linetable_paf", chip, -1, -1);
5601 uves_response_curve_filename(
enum uves_chip chip)
5603 return uves_local_filename(
"response", chip, -1, -1);
5616 uves_response_curve_2d_filename(
enum uves_chip chip)
5618 return uves_local_filename(
"response_2d", chip, -1, -1);
5631 uves_response_red_standard_filename(
enum uves_chip chip)
5633 return uves_local_filename(
"red_std", chip, -1, -1);
5647 uves_response_red_noappend_standard_filename(
enum uves_chip chip)
5649 return uves_local_filename(
"red_nonmerged", chip, -1, -1);
5662 uves_response_bkg_standard_filename(
enum uves_chip chip)
5664 return uves_local_filename(
"bkg_std", chip, -1, -1);
5678 uves_order_extract_qc_standard_filename(
enum uves_chip chip)
5680 return uves_local_filename(
"order_extract_qc", chip, -1, -1);
5693 uves_response_efficiency_filename(
enum uves_chip chip)
5695 return uves_local_filename(
"efficiency", chip, -1, -1);
5709 uves_scired_red_2d_science_filename(
enum uves_chip chip)
5711 return uves_local_filename(
"red_2d_science", chip, -1, -1);
5726 uves_scired_red_science_filename(
enum uves_chip chip)
5728 return uves_local_filename(
"red_science", chip, -1, -1);
5742 uves_scired_red_noappend_science_filename(
enum uves_chip chip)
5744 return uves_local_filename(
"red_nonmerged_science", chip, -1, -1);
5756 uves_scired_red_error_filename(
enum uves_chip chip)
5758 return uves_local_filename(
"error_red_science", chip, -1, -1);
5771 uves_scired_red_noappend_error_filename(
enum uves_chip chip)
5773 return uves_local_filename(
"error_red_nonmerged_science", chip, -1, -1);
5786 uves_scired_red_2d_error_filename(
enum uves_chip chip)
5788 return uves_local_filename(
"error_2d_science", chip, -1, -1);
5802 uves_scired_fluxcal_science_filename(
enum uves_chip chip)
5804 return uves_local_filename(
"fluxcal_science", chip, -1, -1);
5818 uves_scired_fluxcal_science_noappend_filename(
enum uves_chip chip)
5820 return uves_local_filename(
"fluxcal_nonmerged_science", chip, -1, -1);
5832 uves_scired_fluxcal_error_filename(
enum uves_chip chip)
5834 return uves_local_filename(
"fluxcal_error_science", chip, -1, -1);
5848 uves_scired_fluxcal_error_noappend_filename(
enum uves_chip chip)
5850 return uves_local_filename(
"fluxcal_error_nonmerged_science", chip, -1, -1);
5864 uves_scired_fluxcal_science_2d_filename(
enum uves_chip chip)
5866 return uves_local_filename(
"fluxcal_2d_science", chip, -1, -1);
5878 uves_scired_fluxcal_error_2d_filename(
enum uves_chip chip)
5880 return uves_local_filename(
"fluxcal_error_2d_science", chip, -1, -1);
5892 uves_scired_ff_variance_filename(
enum uves_chip chip)
5894 return uves_local_filename(
"variance_ff_science", chip, -1, -1);
5907 uves_scired_ff_variance_2d_filename(
enum uves_chip chip)
5909 return uves_local_filename(
"variance_ff_2d_science", chip, -1, -1);
5921 uves_scired_merged_2d_science_filename(
enum uves_chip chip)
5923 return uves_local_filename(
"merged_2d_science", chip, -1, -1);
5935 uves_scired_merged_science_filename(
enum uves_chip chip)
5937 return uves_local_filename(
"merged_science", chip, -1, -1);
5949 uves_scired_merged_sky_filename(
enum uves_chip chip)
5951 return uves_local_filename(
"merged_sky", chip, -1, -1);
5964 uves_scired_background_filename(
enum uves_chip chip)
5966 return uves_local_filename(
"background", chip, -1, -1);
5979 uves_scired_resampled_filename(
enum uves_chip chip)
5981 return uves_local_filename(
"resampled_science", chip, -1, -1);
5996 uves_scired_resampled_2d_filename(
enum uves_chip chip)
5998 return uves_local_filename(
"resampled_2d_science", chip, -1, -1);
6012 uves_scired_resampledmf_filename(
enum uves_chip chip)
6014 return uves_local_filename(
"resampled_mflat", chip, -1, -1);
6028 uves_scired_rebinned_filename(
enum uves_chip chip)
6030 return uves_local_filename(
"resampled_ff_science", chip, -1, -1);
6044 uves_scired_rebinned_error_filename(
enum uves_chip chip)
6046 return uves_local_filename(
"resampled_error_ff_science", chip, -1, -1);
6060 uves_scired_rebinned_2d_filename(
enum uves_chip chip)
6062 return uves_local_filename(
"resampled_ff_2d_science", chip, -1, -1);
6075 uves_scired_rebinned_2d_error_filename(
enum uves_chip chip)
6077 return uves_local_filename(
"resampled_error_ff_2d_science", chip, -1, -1);
6089 uves_scired_ordertrace_filename(
enum uves_chip chip)
6091 return uves_local_filename(
"ordertrace", chip, -1, -1);
6104 uves_scired_crmask_filename(
enum uves_chip chip)
6106 return uves_local_filename(
"cr_mask", chip, -1, -1);
6119 uves_scired_wmap_filename(
enum uves_chip chip)
6121 return uves_local_filename(
"wave_map", chip, -1, -1);
6133 char *uves_scired_ext2d_filename(
enum uves_chip chip)
6135 return uves_local_filename(
"ext_2d_science", chip, -1, -1);
6147 char *uves_scired_ff2d_filename(
enum uves_chip chip)
6149 return uves_local_filename(
"ff_2d_science", chip, -1, -1);
6175 uves_local_filename(
const char *prefix,
enum uves_chip chip,
int trace,
int window)
6177 char *result = NULL;
6178 const char *chip_string;
6179 const char *suffix =
".fits";
6183 assure( (trace < 0 && window < 0) ||
6184 (trace < 0 && window > 0) ||
6185 (trace >= 0 && window > 0),
6186 CPL_ERROR_ILLEGAL_INPUT,
"Illegal trace and window numbers: (%d, %d)",
6193 check(( t = int_to_string(trace),
6194 w = int_to_string(window)),
6195 "Error creating substrings");
6211 result = uves_sprintf(
"%s_%s%s%s%s", prefix, chip_string, t, w, suffix);
6217 if (cpl_error_get_code() != CPL_ERROR_NONE)
6219 cpl_free(result); result = NULL;
6237 int_to_string(
int i)
6239 char *result = NULL;
6241 assure( -1 <= i, CPL_ERROR_ILLEGAL_INPUT,
"Illegal number (%d)", i);
6246 result = cpl_calloc(1,
sizeof(
char));
6251 result = uves_sprintf(
"_%d", i);
6255 if (cpl_error_get_code() != CPL_ERROR_NONE){
6256 cpl_free(result); result = NULL;
6275 uves_vector_to_image(
const cpl_vector* vector,cpl_type type)
6278 cpl_image* image=NULL;
6280 const double* pv=NULL;
6286 size=cpl_vector_get_size(vector);
6287 image=cpl_image_new(size,1,type);
6288 pv=cpl_vector_get_data_const(vector);
6289 if(type == CPL_TYPE_INT) {
6290 pi=cpl_image_get_data_int(image);
6291 for(i=0;i<size;i++) {
6294 }
else if (type == CPL_TYPE_FLOAT) {
6295 pf=cpl_image_get_data_float(image);
6296 for(i=0;i<size;i++) {
6299 }
else if (type == CPL_TYPE_DOUBLE) {
6300 pd=cpl_image_get_data_double(image);
6301 for(i=0;i<size;i++) {
6305 assure(
false, CPL_ERROR_INVALID_TYPE,
6306 "No CPL type to represent BITPIX = %d", type);
6310 if (cpl_error_get_code() != CPL_ERROR_NONE){
6311 uves_free_image(&image);
int uves_pfits_get_prescanx(const uves_propertylist *plist, enum uves_chip chip)
Find out the x-prescan.
int uves_pfits_get_firstabsorder(const uves_propertylist *plist)
Get first absolute order number.
const cpl_property * uves_propertylist_get_const(const uves_propertylist *self, long position)
Access property list elements by index.
cpl_error_code uves_pfits_set_data_median(uves_propertylist *plist, double median)
Write the median pixel value.
#define uves_msg_error(...)
Print an error message.
void uves_polynomial_delete(polynomial **p)
Delete a polynomial.
char * uves_get_datetime_iso8601(void)
Returns the current date and time as a static string.
#define uves_msg_warning(...)
Print an warning message.
cpl_error_code uves_pfits_set_data_stddev(uves_propertylist *plist, double stddev)
Write the RMS error of pixel values.
int uves_pfits_get_ovrscanx(const uves_propertylist *plist, enum uves_chip chip)
Find out the x-overscan.
int uves_propertylist_erase(uves_propertylist *self, const char *name)
Erase the given property from a property list.
int uves_propertylist_is_empty(const uves_propertylist *self)
Check whether a property list is empty.
const char * uves_pfits_get_cunit1(const uves_propertylist *plist)
Find out the cunit1.
double uves_pfits_get_bscale(const uves_propertylist *plist)
Find out the bscale.
cpl_error_code uves_pfits_set_stoptime(uves_propertylist *plist, const char *stop_time)
Write the stop time.
int uves_pfits_get_lastabsorder(const uves_propertylist *plist)
Get last absolute order number.
const char * uves_pfits_get_cunit2(const uves_propertylist *plist)
Find out the cunit2.
int uves_pfits_get_windownumber(const uves_propertylist *plist)
Get the window number.
polynomial * uves_polynomial_convert_from_table(cpl_table *t)
Convert a table to a polynomial.
cpl_table * uves_ordertable_traces_new(void)
Create the table that describes fibre traces.
const char * uves_pfits_get_drs_id(const uves_propertylist *plist)
Find out the drs id.
double uves_pfits_get_cdelt2(const uves_propertylist *plist)
Find out the cdelt2.
#define passure(BOOL,...)
const char * uves_tostring_cpl_frame_type(cpl_frame_type ft)
Convert a frame type to a string.
double uves_pfits_get_gratwlen(const uves_propertylist *plist, enum uves_chip chip)
find out the central wavelength
cpl_table * uves_polynomial_convert_to_table(const polynomial *p)
Convert a polynomial to a table.
int uves_pfits_get_slit3_x2encoder(const uves_propertylist *plist)
find out the value of UVES_ENCODER_REF2
int uves_pfits_get_naxis(const uves_propertylist *plist)
Find out the NAXIS.
double uves_polynomial_derivative_2d(const polynomial *p, double x1, double x2, int varno)
Evaluate the partial derivative of a 2d polynomial.
uves_propertylist * uves_initialize_image_header(const char *ctype1, const char *ctype2, const char *cunit1, const char *cunit2, const char *bunit, const double bscale, double crval1, double crval2, double crpix1, double crpix2, double cdelt1, double cdelt2)
Initialize image header.
double uves_pfits_get_crpix2(const uves_propertylist *plist)
Find out the crpix2.
uves_propertylist * uves_propertylist_new(void)
Create an empty property list.
long uves_propertylist_get_size(const uves_propertylist *self)
Get the current size of a property list.
#define uves_msg(...)
Print a message on 'info' or 'debug' level.
uves_propertylist * uves_propertylist_load(const char *name, int position)
Create a property list from a file.
const char * uves_pfits_get_bunit(const uves_propertylist *plist)
Find out the bunit.
polynomial * uves_polynomial_duplicate(const polynomial *p)
Copy a polynomial.
cpl_error_code uves_propertylist_set_string(uves_propertylist *self, const char *name, const char *value)
Set the value of the given string property list entry.
int uves_pfits_get_slit3_x1encoder(const uves_propertylist *plist)
find out the value of UVES_ENCODER_REF1
cpl_error_code uves_pfits_set_data_max(uves_propertylist *plist, double max)
Write the max pixel value.
cpl_error_code uves_propertylist_copy_property_regexp(uves_propertylist *self, const uves_propertylist *other, const char *regexp, int invert)
Copy matching properties from another property list.
void uves_pfits_set_cd11(uves_propertylist *plist, double value)
Write the CD1_1 value.
cpl_error_code uves_pfits_set_starttime(uves_propertylist *plist, const char *start_time)
Write the start time.
polynomial * uves_polynomial_new(const cpl_polynomial *pol)
Create a polynomial.
void uves_pfits_set_cd12(uves_propertylist *plist, double value)
Write the CD1_2 value.
double uves_polynomial_evaluate_2d(const polynomial *p, double x1, double x2)
Evaluate a 2d polynomial.
int uves_chip_get_index(enum uves_chip chip)
Convert to integer.
double uves_polynomial_get_coeff_1d(const polynomial *p, int degree)
Get a coefficient of a 1D polynomial.
enum uves_chip uves_chip_get_first(bool blue)
Get first chip for blue or red arm.
void uves_pfits_set_ra(uves_propertylist *plist, double ra)
Write the right ascension.
cpl_error_code uves_ordertable_traces_add(cpl_table *traces, int fibre_ID, double fibre_offset, int fibre_mask)
Add a trace.
polynomial * uves_polynomial_regression_1d(cpl_table *t, const char *X, const char *Y, const char *sigmaY, int degree, const char *polynomial_fit, const char *residual_square, double *mean_squared_error, double kappa)
Fit a 1d polynomial to two table columns.
cpl_error_code uves_pfits_set_data_average(uves_propertylist *plist, double average)
Write the average pixel value.
const char * uves_tostring_cpl_type(cpl_type t)
Convert a CPL type to a string.
#define uves_msg_low(...)
Print a message on a lower message level.
bool uves_format_is_new(const uves_propertylist *plist)
Find out FITS header format.
const char * uves_chip_tostring_lower(enum uves_chip chip)
Convert to string.
cpl_error_code uves_polynomial_shift(polynomial *p, int varno, double shift)
Shift a polynomial.
int uves_propertylist_erase_regexp(uves_propertylist *self, const char *regexp, int invert)
Erase all properties with name matching a given regular expression.
void uves_pfits_set_lastabsorder(uves_propertylist *plist, int last_abs_order)
Write the last absolute order number.
enum uves_chip uves_chip_get_next(enum uves_chip chip)
Get next chip.
#define uves_msg_debug(...)
Print a debug message.
double uves_pfits_get_cdelt1(const uves_propertylist *plist)
Find out the cdelt1.
double uves_pfits_get_crval1(const uves_propertylist *plist)
Find out the crval1.
uves_propertylist * uves_propertylist_duplicate(const uves_propertylist *self)
Create a copy of the given property list.
#define assure_nomsg(BOOL, CODE)
int uves_pfits_get_naxis1(const uves_propertylist *plist)
Find out the NAXIS1.
cpl_error_code uves_pfits_set_wlen1(uves_propertylist *plist, double wlen1)
Write the WLEN1 value.
int uves_propertylist_contains(const uves_propertylist *self, const char *name)
Check whether a property is present in a property list.
cpl_error_code uves_pfits_set_data_min(uves_propertylist *plist, double min)
Write the min pixel value.
int uves_pfits_get_traceid(const uves_propertylist *plist)
Get the trace ID number.
double uves_pfits_get_crval2(const uves_propertylist *plist)
Find out the crval2.
void uves_pfits_set_dec(uves_propertylist *plist, double dec)
Write the declination.
int uves_pfits_get_bitpix(const uves_propertylist *plist)
Find out the BITPIX.
void uves_pfits_set_cd22(uves_propertylist *plist, double value)
Write the CD2_2 value.
const char * uves_pfits_get_chipid(const uves_propertylist *plist, enum uves_chip chip)
Find out the chip ID.
void uves_pfits_set_firstabsorder(uves_propertylist *plist, int first_abs_order)
Write the first absolute order number.
void uves_pfits_set_cd21(uves_propertylist *plist, double value)
Write the CD2_1 value.
double uves_pfits_get_crpix1(const uves_propertylist *plist)
Find out the crpix1.
double uves_pfits_get_wlen1(const uves_propertylist *plist)
find out the WLEN1 wavelength value
const char * uves_pfits_get_ctype2(const uves_propertylist *plist)
Find out the ctype2.
int uves_propertylist_has(const uves_propertylist *self, const char *name)
Check whether a property is present in a property list.
int uves_pfits_put_qc(uves_propertylist *plist, const cpl_table *qclog)
Add QC-LOG to FITS header.
const char * uves_pfits_get_ctype1(const uves_propertylist *plist)
Find out the ctype1.
cpl_error_code uves_propertylist_append(uves_propertylist *self, const uves_propertylist *other)
Append a property list..
cpl_error_code uves_propertylist_copy_property(uves_propertylist *self, const uves_propertylist *other, const char *name)
Copy a property from another property list.