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,
205 cpl_frame* frame = NULL;
206 cpl_frame* frame_dup = NULL;
210 nsof = cpl_frameset_get_size(sof);
211 for (i=0 ; i<nsof ; i++) {
212 frame = cpl_frameset_get_frame(sof,i);
213 name= (
char*) cpl_frame_get_filename(frame);
214 if(uves_is_fits_file(name) == 1) {
216 if(cpl_frame_get_tag(frame) != NULL) {
218 tag= (
char*) cpl_frame_get_tag(frame);
220 if(strstr(tag,type) != NULL) {
222 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)
1129 UVES_TIME_START(
"save product");
1132 if (type == CPL_FRAME_TYPE_IMAGE)
1134 bool use_bitpix16_for_int = (strcmp(recipe, make_str(FLAMES_CAL_ORDERPOS)) == 0);
1136 check( uves_save_image((cpl_image *)
object, filename, pl,
1137 use_bitpix16_for_int,
true),
1138 "Error saving image to file %s", filename);
1140 else if (type == CPL_FRAME_TYPE_TABLE)
1142 check( uves_table_save((cpl_table *)
object,
1147 "Error saving table to file '%s'", filename);
1151 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Unsupported frame type");
1157 uves_free_propertylist(&pl);
1159 return cpl_error_get_code();
1175 unsigned stats_mask)
1177 cpl_stats *stats = NULL;
1180 assure( (stats_mask & (CPL_STATS_MEAN | CPL_STATS_STDEV | CPL_STATS_MEDIAN |
1181 CPL_STATS_MIN | CPL_STATS_MAX)) == stats_mask,
1182 CPL_ERROR_UNSUPPORTED_MODE,
"Cannot compute mask %d",
1185 UVES_TIME_START(
"calculate stats");
1187 check( stats = cpl_stats_new_from_image(
1189 "Error reading image statistics");
1193 if (stats_mask & CPL_STATS_MEDIAN)
1196 "Could not write median flux");
1198 if (stats_mask & CPL_STATS_MEAN)
1201 "Could not write average flux");
1203 if (stats_mask & CPL_STATS_STDEV)
1206 "Could not write flux stdev");
1208 if (stats_mask & CPL_STATS_MIN)
1211 "Could not write min flux");
1213 if (stats_mask & CPL_STATS_MIN)
1216 "Could not write max flux");
1220 uves_free_stats(&stats);
1263 int *length, cpl_type *type,
int *ncards)
1265 void *result = NULL;
1266 unsigned result_size;
1267 int N = strlen(name);
1276 for (i = 0; !found && i < plist_size; i++)
1279 value = cpl_property_get_name(p);
1281 if (strcmp(value,
"HISTORY") == 0)
1284 check( value = cpl_property_get_string(p),
1285 "Error reading property value");
1289 if ((
int)strlen(value) >= 1+N+4 &&
1291 value[N+1] ==
'\'' &&
1292 value[N+2] ==
',' &&
1293 value[N+3] ==
'\'' &&
1294 strncmp(value+1, name, N) == 0
1297 switch(value[N+4]) {
1303 *type = CPL_TYPE_DOUBLE;
1305 if ((
int)strlen(value) >= 1+N+4+2 && value[N+4+1] ==
'*')
1307 switch(value[N+4+2]) {
1308 case '4': *type = CPL_TYPE_FLOAT;
break;
1309 case '8': *type = CPL_TYPE_DOUBLE;
break;
1311 assure(
false, CPL_ERROR_ILLEGAL_INPUT,
1312 "Unrecognized MIDAS type: 'R*%c'",
1318 case 'I': *type = CPL_TYPE_INT ; size =
sizeof(int);
break;
1319 case 'C': *type = CPL_TYPE_STRING; size =
sizeof(char);
break;
1321 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1322 "Unrecognized type '%c'", value[N+4]);
1330 assure( found, CPL_ERROR_ILLEGAL_INPUT,
"Could not find '%s' in property list", name);
1333 result_size =
sizeof(double) * 100;
1334 result = cpl_malloc(result_size);
1337 if (ncards != NULL) *ncards = 2;
1339 const cpl_property *p;
1341 if (ncards != NULL) *ncards += 1;
1343 assure(i < plist_size,
1344 CPL_ERROR_ILLEGAL_INPUT,
"Missing header data");
1346 assure( cpl_property_get_type(p) == CPL_TYPE_STRING &&
1347 strcmp(cpl_property_get_name(p),
"HISTORY") == 0,
1348 CPL_ERROR_ILLEGAL_INPUT,
"Error parsing array");
1353 if (*type == CPL_TYPE_STRING)
1355 assure( strlen(value) < 100, CPL_ERROR_UNSUPPORTED_MODE,
1356 "String too long. Max size is 100");
1362 int len = strlen(value);
1365 for (k = 0; k <= len; k++)
1368 ((
char*)result)[j] = value[k];
1376 value, (
char*)result);
1383 if (strcmp(value,
"") != 0) {
1384 double numberd = -1;
1387 const int base = 10;
1388 char *next = (
char *) value;
1393 case CPL_TYPE_DOUBLE:
1394 numberd = strtod(value, &next);
1397 case CPL_TYPE_FLOAT:
1398 numberf = strtod(value, &next);
1402 numberi = strtol(value, &next, base);
1413 if (*length *
sizeof(
double) > result_size)
1416 result = cpl_realloc(result, result_size);
1420 case CPL_TYPE_DOUBLE:
1421 ((
double *)result)[*length-1] = numberd;
1423 case CPL_TYPE_FLOAT:
1424 ((
float *)result)[*length-1] = numberf;
1427 ((
int *)result)[*length-1] = numberi;
1436 case CPL_TYPE_DOUBLE:
1437 numberd = strtod(value, &next);
1440 case CPL_TYPE_FLOAT:
1441 numberf = strtod(value, &next);
1445 numberi = strtol(value, &next, base);
1452 }
while (next != value);
1458 assure( strcmp(value,
"") == 0, CPL_ERROR_ILLEGAL_INPUT,
1459 "Cannot parse %s descriptor %s, remaining string: '%s'",
1466 if (cpl_property_get_type(p) == CPL_TYPE_STRING &&
1467 strcmp(cpl_property_get_name(p),
"HISTORY") == 0)
1469 value = cpl_property_get_string(
1472 if (*type == CPL_TYPE_STRING)
1474 if (strcmp(value,
"") != 0) {
1475 uves_msg_debug(
"String array %s with length > 1 found. Ignoring remaining values", name);
1476 while (strcmp(value,
"") != 0 && i+1 < plist_size) {
1479 value = cpl_property_get_string(
1481 if (ncards != NULL) *ncards += 1;
1488 }
while (strcmp(value,
"") != 0);
1491 if (cpl_error_get_code() != CPL_ERROR_NONE)
1493 cpl_free(result); result = NULL;
1519 uves_save_table_local(
const char *description,
const char *filename_prefix,
1520 const cpl_table *table,
1521 enum uves_chip chip,
int trace,
int window,
1524 char *filename = NULL;
1526 check( filename = uves_local_filename(filename_prefix, chip, trace, window),
1527 "Error getting filename");
1529 check( uves_table_save(table, pheader, eheader, filename, CPL_IO_DEFAULT),
1530 "Error saving table to file '%s'", filename);
1532 if (description != NULL)
uves_msg(
"%s saved to '%s'", description, filename);
1536 return cpl_error_get_code();
1562 uves_save_image_local(
const char *description,
const char *filename_prefix,
1563 const cpl_image *image,
1564 enum uves_chip chip,
int trace,
int window,
1566 bool use_bitpix16_for_int)
1568 char *filename = NULL;
1570 check( filename = uves_local_filename(filename_prefix, chip, trace, window),
1571 "Error getting filename");
1573 check( uves_save_image(image, filename, plist, use_bitpix16_for_int,
true),
1574 "Error saving image to file '%s'", filename);
1575 if (description != NULL)
uves_msg(
"%s saved to '%s'", description, filename);
1579 return cpl_error_get_code();
1594 cpl_image *uves_load_image(
const cpl_frame *f,
1599 cpl_image *image = NULL;
1601 const char *filename;
1605 cpl_vector * vector=NULL;
1610 assure( cpl_frame_get_type(f) == CPL_FRAME_TYPE_IMAGE,
1611 CPL_ERROR_TYPE_MISMATCH,
"Wrong type: %s",
1614 filename = cpl_frame_get_filename(f);
1617 "Could not load header from %s extension %d",
1618 filename, extension);
1621 "Could not read BITPIX from %s extension %d",
1622 filename, extension);
1624 if (bitpix == -32) type = CPL_TYPE_FLOAT;
1625 else if (bitpix == -64) type = CPL_TYPE_DOUBLE;
1626 else if (bitpix == 32) type = CPL_TYPE_INT;
1627 else if (bitpix == 16) type = CPL_TYPE_INT;
1630 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1631 "No CPL type to represent BITPIX = %d", bitpix);
1635 "could not get NAXIS" );
1639 check( vector = cpl_vector_load(filename,extension),
1640 "Could not load vector from extension %d of file '%s' ",
1641 extension, filename);
1642 cknull(image=uves_vector_to_image(vector,type),
1643 "could not convert vector to image");
1647 check( image = cpl_image_load(filename,
1651 "Could not load image from extension %d of file '%s' ",
1652 extension, filename);
1662 uves_free_vector(&vector);
1663 uves_free_propertylist(&plist);
1672 cpl_image *uves_load_image_file(
const char *filename,
1678 cpl_frame *f = cpl_frame_new();
1679 cpl_frame_set_filename(f, filename);
1680 cpl_frame_set_type(f, CPL_FRAME_TYPE_IMAGE);
1682 i = uves_load_image(f, plane, extension, header);
1684 uves_free_frame(&f);
1716 uves_save_image(
const cpl_image *image,
const char *filename,
const uves_propertylist *plist,
1717 bool use_bitpix16_for_int,
bool save1d)
1721 const cpl_vector *image_1d = NULL;
1723 cpl_image *thresholded = NULL;
1724 cpl_image *thresholded_double = NULL;
1726 if (image == NULL) {
1727 check( uves_image_save(image, filename, CPL_BPP_IEEE_FLOAT, plist, CPL_IO_DEFAULT),
1728 "Error saving NULL image to file '%s'", filename);
1731 check( t = cpl_image_get_type(image),
"Error reading image type");
1732 if (t == CPL_TYPE_FLOAT ) bpp = CPL_BPP_IEEE_FLOAT;
1733 else if (t == CPL_TYPE_DOUBLE) bpp = CPL_BPP_IEEE_FLOAT;
1740 else if (t == CPL_TYPE_INT ) {
1741 if (use_bitpix16_for_int) bpp = CPL_BPP_16_UNSIGNED;
1742 else bpp = CPL_BPP_32_SIGNED;
1744 else assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1748 thresholded = cpl_image_duplicate(image);
1751 if (t == CPL_TYPE_DOUBLE)
1753 passure( bpp == CPL_BPP_IEEE_FLOAT,
"%d", bpp);
1769 -FLT_MAX, FLT_MAX) );
1773 double *data = cpl_image_get_data_double(thresholded);
1774 int nx = cpl_image_get_size_x(thresholded);
1775 int ny = cpl_image_get_size_y(thresholded);
1778 for (y = 0; y < ny; y++)
1779 for (x = 0; x < nx; x++)
1781 if (irplib_isnan(data[x + y*nx]))
1790 cpl_image_get_size_y(thresholded) == 1 &&
1791 (t == CPL_TYPE_DOUBLE ||
1792 t == CPL_TYPE_FLOAT)) {
1794 bool invert =
false;
1815 if (t == CPL_TYPE_FLOAT) {
1816 thresholded_double = cpl_image_cast(thresholded, CPL_TYPE_DOUBLE);
1819 thresholded_double = cpl_image_duplicate(thresholded);
1822 passure( cpl_image_get_type(thresholded_double) == CPL_TYPE_DOUBLE,
"%d",
1823 cpl_image_get_type(thresholded_double));
1825 image_1d = cpl_vector_wrap(
1826 cpl_image_get_size_x(thresholded_double),
1827 cpl_image_get_data_double(thresholded_double));
1829 check( uves_vector_save(image_1d, filename, bpp, header, CPL_IO_DEFAULT),
1830 "Error saving vector to file '%s'", filename );
1835 if (plist != NULL) {
1849 check( uves_image_save(thresholded, filename, bpp, plist, CPL_IO_DEFAULT),
1850 "Error saving image to file '%s'", filename);
1855 uves_unwrap_vector_const(&image_1d);
1856 uves_free_propertylist(&header);
1857 uves_free_image(&thresholded);
1858 uves_free_image(&thresholded_double);
1886 uves_save_imagelist(
const cpl_imagelist *iml,
const char *filename,
const uves_propertylist *plist)
1888 const cpl_image* img=NULL;
1891 const cpl_vector *image_1d = NULL;
1893 cpl_imagelist *thresholded = NULL;
1900 cknull(iml,
"Null input image");
1901 check(img=cpl_imagelist_get_const(iml,0),
"error reading image");
1907 check( t = cpl_image_get_type(img),
"Error reading image type");
1908 if (t == CPL_TYPE_FLOAT ) bpp = CPL_BPP_IEEE_FLOAT;
1909 else if (t == CPL_TYPE_DOUBLE) bpp = CPL_BPP_IEEE_FLOAT;
1912 else if (t == CPL_TYPE_INT ) bpp = CPL_BPP_16_UNSIGNED;
1913 else assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1917 thresholded = cpl_imagelist_duplicate(iml);
1920 if (t == CPL_TYPE_DOUBLE)
1922 passure( bpp == CPL_BPP_IEEE_FLOAT,
"%d", bpp);
1938 -FLT_MAX, FLT_MAX) );
1946 cpl_image* ima=NULL;
1947 for (z = 0; z < nz; z++) {
1948 ima=cpl_imagelist_get(thresholded,z);
1949 data = cpl_image_get_data_double(ima);
1951 for (y = 0; y < ny; y++) {
1952 for (x = 0; x < nx; x++) {
1953 if (irplib_isnan(data[x + y*nx])) {
1961 if (nz == 1 && t == CPL_TYPE_DOUBLE)
1965 bool invert =
false;
1990 check( uves_imagelist_save(thresholded, filename, bpp, plist, CPL_IO_DEFAULT),
1991 "Error saving image to file '%s'", filename);
1995 uves_unwrap_vector_const(&image_1d);
1996 uves_free_propertylist(&header);
1997 uves_free_imagelist(&thresholded);
2020 cpl_table *t = NULL;
2024 check( uves_table_save(t,
2030 "Error saving table to file '%s'", filename);
2033 uves_free_table(&t);
2034 return cpl_error_get_code();
2048 load_polynomial(
const char* filename,
int extension)
2051 cpl_table *t = NULL;
2053 check(t = cpl_table_load(filename,
2057 "Error loading polynomial from extension %d of file '%s'", extension, filename);
2059 assure( uves_erase_invalid_table_rows(t, NULL) == 0,
2060 CPL_ERROR_ILLEGAL_INPUT,
"Table contains invalid rows");
2065 uves_free_table(&t);
2066 if (cpl_error_get_code() != CPL_ERROR_NONE)
2087 identify_arm(
const cpl_frameset *frames,
const char *blue_tag,
const char *red_tag,
2090 const char *tag = NULL;
2092 const cpl_frame *frame = NULL;
2095 assure (!cpl_frameset_is_empty(frames), CPL_ERROR_ILLEGAL_INPUT,
"No input frames");
2098 frame = cpl_frameset_find_const(frames, blue_tag);
2099 *blue = (frame != NULL);
2103 frame = cpl_frameset_find_const(frames, red_tag);
2106 assure( frame != NULL, CPL_ERROR_ILLEGAL_INPUT,
2107 "No valid input frames "
2108 "('%s' or '%s') in frame set",
2111 assure( cpl_frameset_find_const(frames, blue_tag) == NULL ||
2112 cpl_frameset_find_const(frames, red_tag) == NULL,
2113 CPL_ERROR_INCOMPATIBLE_INPUT,
2114 "Multiple types of input frames ('%s' and '%s') in frame set",
2117 tag = cpl_frame_get_tag(frame);
2119 uves_msg(
"Input frames are '%s'", tag);
2147 enum uves_chip chip,
2151 cpl_image *result = NULL;
2152 int prescanx, ovrscanx;
2154 int x_0, y_0, x_1, y_1;
2157 const char *ctype1, *ctype2;
2158 const char *cunit1, *cunit2;
2161 double crval1, crval2;
2162 double crpix1, crpix2;
2163 double cdelt1, cdelt2;
2167 passure( header != NULL,
" ");
2168 passure( out_header != NULL,
" ");
2170 nx = cpl_image_get_size_x(image);
2171 ny = cpl_image_get_size_y(image);
2229 if (new_format || chip == UVES_CHIP_BLUE)
2232 x_1 = nx - ovrscanx;
2236 if (chip == UVES_CHIP_REDU)
2239 x_1 = nx/2 - ovrscanx;
2243 x_0 = nx/2 + prescanx + 1;
2244 x_1 = nx - ovrscanx;
2247 check( result = cpl_image_extract(image, x_0, y_0, x_1, y_1),
"Could not crop image");
2248 crpix1 = crpix1 - (x_0 - 1);
2249 crpix2 = crpix2 - (y_0 - 1);
2250 nx = (x_1 - x_0) + 1;
2251 ny = (y_1 - y_0) + 1;
2254 UVES_TIME_START(
"Rotation");
2262 int crpix1_old = crpix1;
2263 int crpix2_old = crpix2;
2264 int crval1_old = crval1;
2265 int crval2_old = crval2;
2266 int cdelt1_old = cdelt1;
2267 int cdelt2_old = cdelt2;
2268 const char *ctype1_old = ctype1;
2269 const char *ctype2_old = ctype2;
2271 if (chip == UVES_CHIP_BLUE)
2274 check( cpl_image_turn(result, -1),
"Could not turn image");
2276 crpix1 = ny - (crpix2_old - 1);
2277 crpix2 = crpix1_old;
2278 crval1 = crval2_old;
2279 crval2 = crval1_old;
2285 check( cpl_image_flip(result, 3),
"Could not flip image");
2287 crpix1 = ny - (crpix2_old - 1);
2288 crpix2 = nx - (crpix1_old - 1);
2289 crval1 = crval2_old;
2290 crval2 = crval1_old;
2295 ctype1 = ctype2_old;
2296 ctype2 = ctype1_old;
2297 cdelt1 = cdelt2_old;
2298 cdelt2 = cdelt1_old;
2310 if (chip == UVES_CHIP_BLUE || chip == UVES_CHIP_REDL)
2317 int physical_gap_between_chips = 64;
2320 passure( chip == UVES_CHIP_REDU ,
"%d", chip );
2333 physical_gap_between_chips,
2334 "Error reading REDL chip geometry");
2336 uves_msg_debug(
"Setting CRVAL2 = 1 + (%d - %d - %d) * %f + %d = %f",
2341 physical_gap_between_chips, crval2);
2352 physical_gap_between_chips,
2353 "Error reading REDL chip geometry");
2355 uves_msg_debug(
"Setting CRVAL2 = 1 + (%d - %d - %d) * %f + %d = %f",
2360 physical_gap_between_chips, crval2);
2373 "Error initializing header");
2380 uves_msg(
"Raw image cropped and rotated from %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
" to %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
"",
2382 cpl_image_get_size_x(result),
2383 cpl_image_get_size_y(result));
2386 if (cpl_error_get_code() != CPL_ERROR_NONE)
2388 uves_free_image(&result);
2389 if (out_header != NULL)
2391 uves_free_propertylist(out_header);
2415 const char *raw_chip_name,
enum uves_chip chip)
2417 const char *calib_chip_name;
2418 bool mismatch =
false;
2421 "Could not read chip name of calibration data");
2429 unsigned int calib_first, calib_last;
2430 unsigned int raw_first, raw_last;
2434 while (calib_chip_name[calib_first] ==
' ' && calib_first < strlen(calib_chip_name) - 1)
2438 while (raw_chip_name[raw_first] ==
' ' && raw_first < strlen(raw_chip_name) - 1)
2443 calib_last = strlen(calib_chip_name) - 1;
2444 raw_last = strlen(raw_chip_name) - 1;
2445 while (calib_chip_name[calib_last] ==
' ' && calib_last > 0)
2449 while (raw_chip_name[raw_last] ==
' ' && raw_last > 0)
2455 if (calib_last - calib_first != raw_last - raw_first)
2463 for (i = 0; i <= (calib_last - calib_first); i++)
2465 if (raw_chip_name[raw_first + i] !=
2466 calib_chip_name[calib_first + i])
2478 "not match raw frame chip ID '%s'",
2479 calib_chip_name, raw_chip_name);
2511 static cpl_error_code
2512 load_raw_image(
const char *filename,
2516 cpl_image *raw_image[2],
2522 cpl_image *image = NULL;
2525 int extension, nextensions;
2529 cpl_image* image1=NULL;
2530 cpl_image* image2=NULL;
2536 raw_image[0] = NULL;
2537 raw_image[1] = NULL;
2538 raw_header[0] = NULL;
2539 raw_header[1] = NULL;
2540 rotated_header[0] = NULL;
2541 rotated_header[1] = NULL;
2543 check( nextensions = uves_get_nextensions(filename),
2544 "Error reading number of extensions of file '%s'", filename);
2550 "Could not load header from extension %d of file '%s'",
2551 extension, filename);
2554 "Error determining new/old format of file %s", filename);
2556 uves_msg_low(
"Raw frame is %s, %s format, file '%s' has %d extensions",
2557 (blue) ?
"blue" :
"red", (new_format) ?
"new" :
"old",
2558 filename, nextensions);
2561 if (blue || !new_format)
2563 enum uves_chip chip;
2567 assure( nextensions == 0 ||
2568 (blue && nextensions == 2) ||
2569 (flames && nextensions == 2),
2570 CPL_ERROR_ILLEGAL_INPUT,
2571 "Unrecognized format of file '%s'. %d extensions expected. %d found.",
2573 ((flames||blue) && (nextensions ==2)) ? 2 : 0, nextensions);
2580 "Could not load header from extension %d of file '%s'",
2581 extension, filename);
2585 if(blue && nextensions == 2) {
2589 "Could not load header from extension %d of file '%s'",
2590 extension, filename);
2592 "Could not collate header from extension 1 to 0 of file '%s'",filename);
2593 uves_free_propertylist(&raw_header[1]);
2595 check( image1 = cpl_image_load(filename,
2599 ),
"Could not load image from extension %d of file '%s' ",
2600 extension, filename);
2601 cpl_image_save(image1,
"ima1.fits", CPL_BPP_IEEE_FLOAT,
2602 NULL,CPL_IO_DEFAULT);
2605 check( image2 = cpl_image_load(filename,
2609 ),
"Could not load image from extension %d of file '%s' ",
2610 extension, filename);
2619 uves_free_image(&image1);
2620 uves_free_image(&image2);
2629 check( image = cpl_image_load(filename,
2633 ),
"Could not load image from extension %d of file '%s' ",
2634 extension, filename);
2638 chip = (blue) ? UVES_CHIP_BLUE : UVES_CHIP_REDL;
2639 check( raw_image[0] = uves_crop_and_rotate(image, raw_header[0],
2640 chip, raw_header[0],
2642 &rotated_header[0]),
2643 "Error splitting image");
2651 "Error duplicating FITS header");
2654 chip = UVES_CHIP_REDU;
2655 redl_header = raw_header[0];
2656 check( raw_image[1] = uves_crop_and_rotate(image, raw_header[1],
2659 &rotated_header[1]),
2660 "Error splitting red image");
2664 raw_image[1] = NULL;
2665 raw_header[1] = NULL;
2666 rotated_header[1] = NULL;
2676 assure( nextensions >= 2, CPL_ERROR_UNSUPPORTED_MODE,
2677 "File '%s' (red frame) has %d extensions. 2+ extensions expected "
2679 filename, nextensions);
2682 (nextensions > 2) ?
"FLAMES" :
"FLAMES/UVES");
2686 for (extension = 1; extension <= 2; extension++)
2691 enum uves_chip chip = (extension == 1) ? UVES_CHIP_REDU : UVES_CHIP_REDL;
2695 uves_free_propertylist(&ext_header);
2698 "Could not load header from extension %d of file '%s'",
2699 extension, filename);
2703 "Error cloning primary header");
2707 if(cpl_propertylist_has(ext_header,
"TM-START") &&
2708 cpl_propertylist_has(primary_header,
"TM-START") ) {
2711 if(cpl_propertylist_has(ext_header,
"DATE-OBS") &&
2712 cpl_propertylist_has(primary_header,
"DATE-OBS") ) {
2715 if(cpl_propertylist_has(ext_header,
"EXPTIME") &&
2716 cpl_propertylist_has(ext_header,
"EXPTIME")) {
2720 ext_header,
".*", 0),
2721 "Error merging primary header with extension %d header",
2728 for (extension = 1; extension <= 2; extension++)
2730 enum uves_chip chip = (extension == 1) ? UVES_CHIP_REDU : UVES_CHIP_REDL;
2736 uves_free_image(&image);
2737 check( image = cpl_image_load(filename,
2741 "Could not load image from extension %d of file '%s' ",
2742 extension, filename);
2744 check( raw_image[indx] = uves_crop_and_rotate(image,
2748 &rotated_header[indx]),
2749 "Error splitting red image");
2757 uves_free_image(&image);
2758 uves_free_image(&image1);
2759 uves_free_image(&image2);
2761 uves_free_propertylist(&primary_header);
2762 uves_free_propertylist(&ext_header);
2764 if (cpl_error_get_code() != CPL_ERROR_NONE)
2766 uves_free_image (&raw_image[0]);
2767 uves_free_image (&raw_image[1]);
2768 uves_free_propertylist(&raw_header[0]);
2769 uves_free_propertylist(&raw_header[1]);
2770 uves_free_propertylist(&rotated_header[0]);
2771 uves_free_propertylist(&rotated_header[1]);
2774 return cpl_error_get_code();
2808 uves_load_raw_imagelist(
const cpl_frameset *frames,
2810 const char *blue_tag,
const char *red_tag, cpl_type type,
2811 cpl_imagelist *images[2],
2815 const char *tag = NULL;
2816 const cpl_frame *frame = NULL;
2817 cpl_image *temp_image[2] = {NULL, NULL};
2819 cpl_size number_of_frames = 0;
2820 int frameset_size = 0;
2824 raw_headers[0] = NULL;
2825 raw_headers[1] = NULL;
2827 check( frameset_size = cpl_frameset_get_size(frames),
2828 "Error reading frameset size");
2830 check( tag = identify_arm(frames, blue_tag, red_tag, blue),
2831 "Could not identify chip type");
2833 nchips = (*blue) ? 1 : 2;
2834 for(chip = 0; chip < nchips; chip++)
2836 images[chip] = NULL;
2837 rotated_header[chip] = NULL;
2839 images[chip] = cpl_imagelist_new();
2847 number_of_frames = 0;
2850 nfrm=cpl_frameset_get_size(frames);
2853 frame=cpl_frameset_get_frame_const(frames,i);
2855 if ( strcmp(cpl_frame_get_tag(frame), tag) == 0)
2857 const char *filename = cpl_frame_get_filename(frame);
2860 uves_free_propertylist(&rotated_header[0]);
2861 uves_free_propertylist(&rotated_header[1]);
2863 check( load_raw_image(filename,
2870 "Could not load image from file '%s'", filename);
2873 for(chip = 0; chip < nchips; chip++)
2875 raw_headers[chip][number_of_frames] = temp_header[chip];
2876 temp_header[chip] = NULL;
2878 check( cpl_imagelist_set(images[chip],
2881 cpl_imagelist_get_size(images[chip])
2883 "Could not insert image into image list");
2886 temp_image[chip] = NULL;
2889 number_of_frames += 1;
2895 for(chip = 0; chip < nchips; chip++)
2898 assure (cpl_imagelist_is_uniform(images[chip]) == 0,
2899 CPL_ERROR_INCOMPATIBLE_INPUT,
2900 "Input images are not of same size and type");
2902 passure( cpl_imagelist_get_size(images[chip]) == number_of_frames,
2903 "%" CPL_SIZE_FORMAT
" %" CPL_SIZE_FORMAT
"", cpl_imagelist_get_size(images[0]), number_of_frames);
2909 if ( strcmp(UVES_BIAS (*blue), tag) != 0 &&
2910 strcmp(UVES_DARK (*blue), tag) != 0 &&
2911 strcmp(UVES_PDARK(*blue), tag) != 0) {
2912 enum uves_chip chip_id;
2917 chip_id != UVES_CHIP_INVALID;
2919 for (i = 0; i < number_of_frames; i++) {
2923 "Error reading central wavelength of input frame number %d", i+1);
2930 "Error reading central wavelength of input frame number %d", i+1);
2932 assure( fabs((w-wlen)/wlen) < 0.01, CPL_ERROR_INCOMPATIBLE_INPUT,
2933 "Mis-matching input frame central wavelengths: "
2934 "%e (frame 1) != %e (frame %d)", wlen, w, i+1);
2941 uves_free_image(&temp_image[0]);
2942 uves_free_image(&temp_image[1]);
2943 uves_free_propertylist(&temp_header[0]);
2944 uves_free_propertylist(&temp_header[1]);
2946 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2947 if (raw_headers[0] != NULL) {
2949 for (i = 0; i < frameset_size; i++) {
2950 if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
2951 if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
2954 cpl_free(raw_headers[0]); raw_headers[0] = NULL;
2955 cpl_free(raw_headers[1]); raw_headers[1] = NULL;
2957 uves_free_imagelist(&images[0]);
2958 uves_free_imagelist(&images[1]);
2960 uves_free_propertylist(&rotated_header[0]);
2961 uves_free_propertylist(&rotated_header[1]);
2964 return cpl_error_get_code();
2987 uves_load_orderpos(
const cpl_frameset *frames,
2989 const char **raw_filename,
2990 cpl_image *raw_image[2],
2994 const char *tags[4];
2996 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3001 tags[0] = UVES_ORDER_FLAT(flames,
false);
3002 tags[1] = UVES_ORDER_FLAT(flames,
true);
3003 tags[2] = UVES_STD_STAR(
false);
3004 tags[3] = UVES_STD_STAR(
true);
3011 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
3013 "Could not find raw frame (%s) in SOF",
3019 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
3021 "Could not find raw frame (%s, %s, %s, or %s) in SOF",
3022 tags[0], tags[1], tags[2], tags[3]);
3024 *blue = (indx == 1) || (indx == 3);
3028 check( load_raw_image(*raw_filename,
3035 "Error loading image from file '%s'", *raw_filename);
3037 passure( !flames || !(*blue),
"%d %d",
3041 if (cpl_error_get_code() != CPL_ERROR_NONE)
3043 *raw_filename = NULL;
3046 return cpl_error_get_code();
3067 uves_load_formatcheck(
const cpl_frameset *frames,
3069 const char **raw_filename,
3070 cpl_image *raw_image[2],
3074 const char *tags[2];
3075 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3078 tags[0] = UVES_FORMATCHECK(flames,
false);
3079 tags[1] = UVES_FORMATCHECK(flames,
true);
3085 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3086 "Could not find raw frame (%s) in SOF",
3091 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3092 "Could not find raw frame (%s or %s) in SOF",
3095 *blue = (indx == 1);
3099 check( load_raw_image(*raw_filename,
3106 "Error loading image from file '%s'", *raw_filename);
3109 if (cpl_error_get_code() != CPL_ERROR_NONE)
3111 *raw_filename = NULL;
3113 return cpl_error_get_code();
3136 void uves_load_cd_align(
const cpl_frameset *frames,
3137 const char **raw_filename1,
3138 const char **raw_filename2,
3139 cpl_image *raw_image1[2],
3140 cpl_image *raw_image2[2],
3147 const char *tags[2];
3148 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3150 bool flames =
false;
3151 const cpl_frame *frame;
3153 tags[0] = UVES_CD_ALIGN(
false);
3154 tags[1] = UVES_CD_ALIGN(
true);
3156 check( *raw_filename1 = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3157 "Could not find raw frame (%s or %s) in SOF",
3160 *blue = (indx == 1);
3162 assure( cpl_frameset_count_tags(frames, tags[indx]) == 2,
3163 CPL_ERROR_ILLEGAL_INPUT,
3164 "%d %s frames found. Exactly 2 required",
3165 cpl_frameset_count_tags(frames, tags[indx]), tags[indx] );
3171 int nfrm=cpl_frameset_get_size(frames);
3172 for (i = 0;i < nfrm;i++)
3174 frame=cpl_frameset_get_frame_const(frames,i);
3175 if (strcmp(cpl_frame_get_tag(frame), tags[indx]) == 0)
3179 *raw_filename1 = cpl_frame_get_filename(frame);
3183 *raw_filename2 = cpl_frame_get_filename(frame);
3186 check( load_raw_image(n == 1 ?
3201 "Error loading image from file '%s'",
3202 n == 1 ? *raw_filename1 : *raw_filename2);
3210 if (cpl_error_get_code() != CPL_ERROR_NONE)
3212 *raw_filename1 = NULL;
3213 *raw_filename2 = NULL;
3243 uves_load_arclamp(
const cpl_frameset *frames,
3245 const char **raw_filename,
3250 const char *tags[4];
3252 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3260 tags[0] = UVES_ARC_LAMP(flames,
true);
3261 tags[1] = FLAMES_FIB_SCI_SIM;
3266 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3267 "Could not find raw frame (%s or %s) in SOF",
3270 *sim_cal = (indx == 1);
3274 tags[0] = UVES_ARC_LAMP(flames,
true);
3275 tags[1] = UVES_ARC_LAMP(flames,
false);
3276 tags[2] = UVES_ECH_ARC_LAMP(
true);
3277 tags[3] = UVES_ECH_ARC_LAMP(
false);
3279 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3280 "Could not find raw frame (%s, %s, %s or %s) in SOF",
3281 tags[0], tags[1], tags[2], tags[3]);
3283 *blue = (indx == 0 || indx == 2);
3287 check( load_raw_image(*raw_filename,
3294 "Error loading image from file '%s'", *raw_filename);
3297 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3298 *raw_filename = NULL;
3299 uves_free_image (raw_image);
3300 uves_free_propertylist(raw_header);
3322 uves_load_science(
const cpl_frameset *frames,
const char **raw_filename,
3323 cpl_image *raw_image[2],
3327 const char **sci_type)
3330 const char *tags[] =
3332 UVES_SCIENCE(
true), UVES_SCIENCE(
false),
3333 UVES_SCI_EXTND(
true), UVES_SCI_EXTND(
false),
3334 UVES_SCI_POINT(
true), UVES_SCI_POINT(
false),
3335 UVES_SCI_SLICER(
true), UVES_SCI_SLICER(
false),
3336 UVES_TFLAT(
true), UVES_TFLAT(
false)
3339 const char *type[] =
3341 "SCIENCE",
"SCIENCE",
3342 "SCI_EXTND",
"SCI_EXTND",
3343 "SCI_POINT",
"SCI_POINT",
3344 "SCI_SLICER",
"SCI_SLICER",
3348 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3350 bool flames =
false;
3352 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3353 "No science frame (%s, %s, %s, %s, %s, %s, %s, %s, %s or %s) in SOF",
3354 tags[0], tags[1], tags[2], tags[3],
3355 tags[4], tags[5], tags[6], tags[7], tags[7], tags[8]);
3357 *blue = (indx % 2 == 0);
3358 *sci_type = type[indx];
3361 check( load_raw_image(*raw_filename,
3368 "Error loading image from file '%s'", *raw_filename);
3370 if (cpl_error_get_code() != CPL_ERROR_NONE)
3372 *raw_filename = NULL;
3373 uves_free_image (raw_image);
3374 uves_free_propertylist(raw_header);
3376 return cpl_error_get_code();
3398 uves_load_standard(
const cpl_frameset *frames,
const char **raw_filename,
3399 cpl_image *raw_image[2],
3403 const char *tags[] = { UVES_STD_STAR(
true), UVES_STD_STAR(
false) };
3404 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3406 bool flames =
false;
3408 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3409 "Could not identify raw frame (%s or %s) in SOF", tags[0], tags[1]);
3411 *blue = (indx == 0);
3414 check( load_raw_image(*raw_filename,
3421 "Error loading image from file '%s'", *raw_filename);
3424 if (cpl_error_get_code() != CPL_ERROR_NONE)
3426 *raw_filename = NULL;
3427 uves_free_image (raw_image);
3428 uves_free_propertylist(raw_header);
3430 return cpl_error_get_code();
3452 uves_load_drs(
const cpl_frameset *frames,
3454 const char *chip_name,
3455 const char **drs_filename,
3457 enum uves_chip chip)
3459 const char *tags[1];
3460 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3465 tags[0] = UVES_DRS_SETUP(flames, chip);
3466 extension = UVES_DRS_SETUP_EXTENSION(chip);
3468 check( *drs_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3469 "Could not find DRS table (%s) in SOF", tags[0]);
3474 "Could not load header from extension %d of file '%s'", extension, *drs_filename);
3476 check_nomsg( uves_warn_if_chip_names_dont_match(*drs_header, chip_name, chip) );
3479 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3480 *drs_filename = NULL;
3481 uves_free_propertylist(drs_header);
3483 return cpl_error_get_code();
3496 uves_load_weights(
const cpl_frameset *frames,
const char **weights_filename,
3497 enum uves_chip chip)
3499 cpl_image *weights = NULL;
3500 const char *tags[1];
3501 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3505 assure( weights_filename != NULL, CPL_ERROR_NULL_INPUT,
"Null filename");
3507 tags[0] = UVES_WEIGHTS(chip);
3509 check( *weights_filename = uves_find_frame(frames,
3510 tags, number_of_tags, &indx, NULL),
3511 "Could not find '%s' in frame set", tags[0]);
3513 check( weights = cpl_image_load(*weights_filename,
3518 "Could not load master bias from extension %d of file '%s'",
3519 extension, *weights_filename);
3545 uves_load_mbias(
const cpl_frameset *frames,
const char *chip_name,
3546 const char **mbias_filename,
3549 const char *tags[1];
3550 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3555 *mbias_header = NULL;
3557 tags[0] = UVES_MASTER_BIAS (chip);
3558 extension = UVES_MASTER_BIAS_EXTENSION(chip);
3560 check( *mbias_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3561 "Could not find '%s' in frame set", tags[0]);
3564 check( *mbias = cpl_image_load(*mbias_filename,
3569 "Could not load master bias from extension %d of file '%s'",
3570 extension, *mbias_filename);
3575 "Could not load header from extension %d of file '%s'",
3576 extension, *mbias_filename);
3578 check_nomsg( uves_warn_if_chip_names_dont_match(*mbias_header, chip_name, chip) );
3581 if (cpl_error_get_code() != CPL_ERROR_NONE)
3583 *mbias_filename = NULL;
3584 uves_free_image(mbias);
3585 uves_free_propertylist(mbias_header);
3587 return cpl_error_get_code();
3610 uves_load_master_formatcheck(
const cpl_frameset *frames,
const char *chip_name,
3611 const char **mform_filename,
3614 const char *tags[1];
3615 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3620 *mform_header = NULL;
3622 tags[0] = UVES_MASTER_ARC_FORM (chip);
3623 extension = UVES_MASTER_ARC_FORM_EXTENSION(chip);
3625 check( *mform_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3626 "Could not find '%s' in frame set", tags[0]);
3629 check( *mform = cpl_image_load(*mform_filename,
3634 "Could not load master formatcheck from extension %d of file '%s'",
3635 extension, *mform_filename);
3641 "Could not load header from extension %d of file '%s'",
3642 extension, *mform_filename);
3644 check_nomsg( uves_warn_if_chip_names_dont_match(*mform_header, chip_name, chip) );
3647 if (cpl_error_get_code() != CPL_ERROR_NONE)
3649 *mform_filename = NULL;
3650 uves_free_image(mform);
3651 uves_free_propertylist(mform_header);
3653 return cpl_error_get_code();
3675 uves_load_mdark(
const cpl_frameset *frames,
const char *chip_name,
3676 const char **mdark_filename, cpl_image **mdark,
3679 const char *tags[2];
3680 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3685 *mdark_header = NULL;
3687 tags[0] = UVES_MASTER_DARK (chip);
3688 tags[1] = UVES_MASTER_PDARK (chip);
3689 extension = UVES_MASTER_DARK_EXTENSION(chip);
3691 check( *mdark_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3692 "Could not find %s or %s in frame set", tags[0], tags[1]);
3695 check( *mdark = cpl_image_load(*mdark_filename,
3700 "Could not load master dark from extension %d of file '%s'",
3701 extension, *mdark_filename);
3706 "Could not load header from extension %d of file '%s'",
3707 extension, *mdark_filename);
3709 check_nomsg( uves_warn_if_chip_names_dont_match(*mdark_header, chip_name, chip) );
3712 if (cpl_error_get_code() != CPL_ERROR_NONE)
3714 *mdark_filename = NULL;
3715 uves_free_image(mdark);
3716 uves_free_propertylist(mdark_header);
3718 return cpl_error_get_code();
3738 uves_load_ref_flat(
const cpl_frameset *frames,
const char *chip_name,
3739 const char **filename, cpl_image **rflat,
3742 const char *tags[1];
3743 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3748 *rflat_header = NULL;
3750 tags[0] = UVES_REF_TFLAT(chip);
3751 extension = UVES_MASTER_FLAT_EXTENSION(chip);
3753 check( *filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3754 "Could not find %s in frame set", tags[0]);
3756 check( *rflat = cpl_image_load(*filename,
3761 "Could not load reference dark from extension %d of file '%s'",
3762 extension, *filename);
3766 "Could not load header from extension %d of file '%s'",
3767 extension, *filename);
3769 check_nomsg( uves_warn_if_chip_names_dont_match(*rflat_header, chip_name, chip) );
3772 if (cpl_error_get_code() != CPL_ERROR_NONE)
3775 uves_free_image(rflat);
3776 uves_free_propertylist(rflat_header);
3801 uves_load_mflat_const(
const cpl_frameset *frames,
const char *chip_name,
3802 const char **mflat_filename,
3804 enum uves_chip chip,
3805 const cpl_frame **mflat_frame)
3807 const char *tags[6];
3808 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3813 *mflat_header = NULL;
3815 tags[0] = UVES_REF_TFLAT (chip);
3816 tags[1] = UVES_MASTER_FLAT (chip);
3817 tags[2] = UVES_MASTER_DFLAT (chip);
3818 tags[3] = UVES_MASTER_IFLAT (chip);
3819 tags[4] = UVES_MASTER_TFLAT (chip);
3820 tags[5] = UVES_MASTER_SCREEN_FLAT (chip);
3821 extension = UVES_MASTER_FLAT_EXTENSION(chip);
3823 check( *mflat_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
3825 "Could not find '%s', '%s', '%s', '%s' or '%s' in frame set",
3826 tags[0], tags[1], tags[2], tags[3], tags[4]);
3829 check( *mflat = cpl_image_load(*mflat_filename,
3834 "Could not load master flat from extension %d of file '%s'",
3835 extension, *mflat_filename);
3840 "Could not load header from extension %d of file '%s'",
3841 extension, *mflat_filename);
3843 check_nomsg( uves_warn_if_chip_names_dont_match(*mflat_header, chip_name, chip) );
3846 if (cpl_error_get_code() != CPL_ERROR_NONE)
3848 *mflat_filename = NULL;
3849 uves_free_image(mflat);
3850 uves_free_propertylist(mflat_header);
3852 return cpl_error_get_code();
3872 uves_load_mflat(cpl_frameset *frames,
const char *chip_name,
3873 const char **mflat_filename,
3875 cpl_frame **mflat_frame)
3877 return uves_load_mflat_const((
const cpl_frameset *)frames,
3880 mflat, mflat_header, chip,
3881 (
const cpl_frame **) mflat_frame);
3918 uves_load_ordertable(
const cpl_frameset *frames,
3920 const char *chip_name,
3921 const char **ordertable_filename,
3922 cpl_table **ordertable,
3927 int *tab_in_out_oshift,
3928 double *tab_in_out_yshift,
3931 enum uves_chip chip,
3936 const char *tags[1];
3937 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3938 bool format_is_midas;
3940 double *tioy = NULL;
3943 double *fibre_pos = NULL;
3944 int *fibre_mask = NULL;
3948 tags[0] = UVES_GUESS_ORDER_TABLE(flames, chip);
3952 tags[0] = UVES_ORDER_TABLE(flames, chip);
3955 check( *ordertable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3956 "No order table (%s) found in SOF", tags[0]);
3958 check( *ordertable = cpl_table_load(*ordertable_filename,
3959 UVES_ORDER_TABLE_EXTENSION,
3962 "Error loading order table from extension %d of file '%s'",
3963 UVES_ORDER_TABLE_EXTENSION, *ordertable_filename);
3965 assure(ordertable_header != NULL,CPL_ERROR_NULL_INPUT,
3966 "NULL primary header uves_propertylist variable header");
3968 "Could not load header from extension 0 of '%s'", *ordertable_filename);
3970 if(ordertable_xheader != NULL) {
3973 "Could not load header from extension 1 of '%s'", *ordertable_filename);
3978 check_nomsg( uves_warn_if_chip_names_dont_match(*ordertable_header, chip_name, chip) );
3980 check(uves_check_if_format_is_midas(*ordertable_header,&format_is_midas),
3981 "Error getting FITS format");
3984 if (!format_is_midas && !flames)
3995 if (cpl_table_has_column(*ordertable,
"ORDER"))
3997 cpl_table_name_column(*ordertable,
"ORDER",
"Order");
3999 if (cpl_table_has_column(*ordertable,
"YFIT"))
4001 cpl_table_name_column(*ordertable,
"YFIT",
"Yfit");
4004 if (order_locations != NULL)
4006 check( *order_locations =
4007 load_polynomial(*ordertable_filename, UVES_ORDER_TABLE_EXTENSION_POLY),
4008 "Could not read polynomial from extension %d of file '%s'",
4009 UVES_ORDER_TABLE_EXTENSION_POLY, *ordertable_filename);
4014 check( *traces = cpl_table_load(*ordertable_filename,
4015 UVES_ORDER_TABLE_EXTENSION_FIBRE,
4018 "Error loading fibre table from extension %d of file '%s'",
4019 UVES_ORDER_TABLE_EXTENSION_FIBRE, *ordertable_filename);
4026 check(( cpl_table_cast_column (*ordertable,
"ORDER",
"Order", CPL_TYPE_INT),
4027 cpl_table_erase_column(*ordertable,
"ORDER")),
4028 "Error casting and renaming column 'ORDER'");
4030 check( cpl_table_name_column(*ordertable,
"YFIT",
"Yfit"),
4031 "Error renaming column 'YFIT'");
4037 "Could not load header from extension 1 of '%s'",
4038 *ordertable_filename);
4042 "Could not load header from extension 0 of '%s'",
4043 *ordertable_filename);
4048 if (order_locations != NULL)
4050 check( *order_locations =
4051 uves_polynomial_convert_from_plist_midas(midas_header,
"COEFF",-1),
4052 "Error reading polynomial from %s", *ordertable_filename);
4056 if (flames && tab_in_out_oshift != NULL )
4062 check( tioo = uves_read_midas_array(
4063 midas_header,
"TAB_IN_OUT_OSHIFT", &tioo_length,
4065 "Error reading TAB_IN_OUT_OSHIFT from MIDAS header");
4067 assure( tioo_type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4068 "Type of TAB_IN_OUT_OSHIFT is %s, double expected",
4071 if (tioo_length != 1)
4074 "%d expected", tioo_length, 1);
4077 *tab_in_out_oshift = tioo[0];
4083 if (flames && tab_in_out_yshift != NULL)
4089 check( tioy = uves_read_midas_array(
4090 midas_header,
"TAB_IN_OUT_YSHIFT", &tioy_length,
4092 "Error reading TAB_IN_OUT_YSHIFT from MIDAS header");
4094 assure( tioy_type == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
4095 "Type of TAB_IN_OUT_YSHIFT is %s, double expected",
4098 if (tioy_length != 1)
4101 "%d expected", tioy_length, 1);
4104 *tab_in_out_yshift = tioy[0];
4117 double fibre_offset = 0.0;
4128 int fibre_pos_length;
4129 int fibre_mask_length;
4130 cpl_type fibre_pos_type;
4131 cpl_type fibre_mask_type;
4134 check( fibre_pos = uves_read_midas_array(
4135 midas_header,
"FIBREPOS", &fibre_pos_length,
4136 &fibre_pos_type, NULL),
4137 "Error reading FIBREPOS from MIDAS header");
4139 assure( fibre_pos_type == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
4140 "Type of FIBREPOS is %s, double expected",
4143 check( fibre_mask = uves_read_midas_array(
4144 midas_header,
"FIBREMASK", &fibre_mask_length,
4145 &fibre_mask_type, NULL),
4146 "Error reading FIBREMASK from MIDAS header");
4148 assure( fibre_mask_type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4149 "Type of FIBREMASK is %s, double expected",
4152 assure( fibre_pos_length == fibre_mask_length,
4153 CPL_ERROR_INCOMPATIBLE_INPUT,
4154 "FIBREMASK has length %d, but "
4155 "FIBREPOS has length %d",
4156 fibre_mask_length, fibre_pos_length );
4158 *fib_pos= cpl_malloc(
sizeof(
double) * fibre_pos_length);
4159 *fib_msk= cpl_malloc(
sizeof(
int) * fibre_mask_length);
4161 for (fibre_ID = 0; fibre_ID < fibre_mask_length; fibre_ID++)
4164 fibre_ID, fibre_pos[fibre_ID],
4165 fibre_mask[fibre_ID] ?
4166 "enabled" :
"disabled");
4169 fibre_pos[fibre_ID],
4170 fibre_mask[fibre_ID]);
4171 (*fib_pos)[fibre_ID]=fibre_pos[fibre_ID];
4172 (*fib_msk)[fibre_ID]=fibre_mask[fibre_ID];
4179 uves_free_propertylist(&midas_header);
4180 uves_free_double(&fibre_pos);
4181 uves_free_int(&fibre_mask);
4182 uves_free_int(&tioo);
4183 uves_free_double(&tioy);
4184 uves_free_propertylist(&prime_header);
4186 if (cpl_error_get_code() != CPL_ERROR_NONE)
4188 *ordertable_filename = NULL;
4189 uves_free_table (ordertable);
4190 uves_free_propertylist(ordertable_header);
4192 if (traces != NULL) uves_free_table (traces);
4194 return cpl_error_get_code();
4219 const char* drs_id=NULL;
4222 if (strstr(drs_id,
"CPL") != NULL ||
4223 strstr(drs_id,
"cpl") != NULL) {
4224 *format_is_midas =
false;
4226 }
else if (strstr(drs_id,
"MIDAS") != NULL ||
4227 strstr(drs_id,
"midas") != NULL) {
4228 *format_is_midas =
true;
4231 assure (
false, CPL_ERROR_ILLEGAL_INPUT,
4232 "Unrecognized order table format, DRS_ID = '%s'", drs_id);
4236 *format_is_midas =
true;
4237 uves_msg_debug(
"No '%s' keyword found. Assuming MIDAS format", UVES_DRS_ID);
4241 return cpl_error_get_code();
4257 static cpl_error_code
4258 create_column_pixelsize(cpl_table *linetable)
4261 cpl_table *t = NULL;
4267 check( t = uves_extract_table_rows(linetable,
"Ident", CPL_GREATER_THAN, 0.1),
4268 "Error deleting rows with Ident=0");
4271 check(( cpl_table_duplicate_column(t,
"Aux", t,
"Ident"),
4272 cpl_table_multiply_columns(t,
"Aux",
"Order")),
4273 "Error creating 'Aux' column");
4281 "Regression failed");
4284 "Error reading polynomial coefficient");
4287 "Error reading polynomial coefficient");
4289 cpl_table_new_column(linetable, LINETAB_PIXELSIZE, CPL_TYPE_DOUBLE);
4291 for (i = 0; i < cpl_table_get_nrow(linetable); i++)
4298 check(( x = cpl_table_get_double(linetable,
"X", i, NULL),
4299 order = cpl_table_get_int (linetable,
"Order", i, NULL),
4300 ident = cpl_table_get_double(linetable,
"Ident", i, NULL)),
4301 "Error reading line table");
4303 assure( order != 0, CPL_ERROR_ILLEGAL_INPUT,
"Illegal order number: %d", order);
4311 pixelsize = (d1 + 2*d2* x) / order;
4316 cpl_table_set_double(linetable, LINETAB_PIXELSIZE, i, pixelsize);
4320 cpl_table_set_invalid(linetable, LINETAB_PIXELSIZE, i);
4325 uves_free_table(&t);
4327 return cpl_error_get_code();
4361 align_order_line_table(cpl_table *linetable,
const polynomial *absolute_order,
4363 const polynomial *order_locations,
int minorder,
int maxorder)
4367 assure ( order_locations != NULL, CPL_ERROR_NULL_INPUT,
4368 "Null order locations polynomial!");
4370 assure ( absolute_order != NULL, CPL_ERROR_NULL_INPUT,
4371 "Null absolute order pllynomial!");
4372 assure( cpl_table_has_column(linetable,
"X" ), CPL_ERROR_DATA_NOT_FOUND,
4373 "Missing line table column 'X'");
4374 assure( cpl_table_has_column(linetable,
"Ynew"), CPL_ERROR_DATA_NOT_FOUND,
4375 "Missing line table column 'Ynew'");
4376 assure( cpl_table_has_column(linetable,
"Order"), CPL_ERROR_DATA_NOT_FOUND,
4377 "Missing line table column 'Order'");
4379 assure( cpl_table_get_column_type(linetable,
"X") == CPL_TYPE_DOUBLE,
4380 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'X' has type %s (double expected))",
4383 assure( cpl_table_get_column_type(linetable,
"Ynew") == CPL_TYPE_DOUBLE,
4384 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'Ynew' has type %s (double expected))",
4387 assure( cpl_table_get_column_type(linetable,
"Y") == CPL_TYPE_INT,
4388 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'Y' has type %s (integer expected))",
4392 if (linetable_header != NULL)
4395 int line_first, line_last;
4396 int ord_first, ord_last;
4401 int x, y, order, absorder;
4405 maxx = uves_round_double(cpl_table_get_column_max(linetable,
"X"));
4407 minx = uves_round_double(cpl_table_get_column_min(linetable,
"X"));
4409 assure( 1 <= minx && minx <= maxx, CPL_ERROR_ILLEGAL_INPUT,
4410 "Illegal min/max line x positions: %d/%d, must be > 1",
4414 x = (minx + maxx) / 2;
4415 order = (minorder + maxorder) / 2;
4425 assure ( order_locations != NULL, CPL_ERROR_NULL_INPUT,
4426 "Null order locations polynomial!");
4433 "rounding to %d", x, y,
4436 ord_first = absorder + (minorder - order) * coeff;
4437 ord_last = absorder + (maxorder - order) * coeff;
4442 "Could not read order number from line table header");
4446 "Could not read order number from line table header");
4448 uves_msg_debug(
"Order table range: %d - %d. Line table range: %d - %d",
4449 ord_first, ord_last, line_first, line_last);
4451 if (line_first != ord_first ||
4452 line_last != ord_last)
4455 "Line table contains orders %d - %d. "
4456 "Order table contains orders %d - %d. "
4457 "Correcting on the fly",
4458 line_first, line_last, ord_first, ord_last);
4462 "Could not write corrected first absolute order number");
4465 "Could not write corrected first absolute order number");
4468 ord_first, ord_last);
4477 double epsilon = 0.01;
4482 if (fabs(cpl_table_get_column_median(linetable,
"Y") -
4483 cpl_table_get_column_median(linetable,
"Order")) > epsilon)
4495 cpl_table_erase_column(linetable,
"Y");
4545 uves_load_linetable(
const cpl_frameset *frames,
4547 const char *chip_name,
4548 const polynomial *order_locations,
int minorder,
int maxorder,
4549 const char **linetable_filename,
4550 cpl_table **linetable,
4554 enum uves_chip chip,
int trace_id,
int window)
4559 int *absorders = NULL;
4560 cpl_table *temp = NULL;
4562 const char *tags[3];
4563 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
4565 bool format_is_midas;
4572 tags[0] = UVES_GUESS_LINE_TABLE(flames, chip);
4573 tags[1] = UVES_LINE_TABLE(flames, chip);
4574 tags[2] = UVES_LINE_TABLE(flames, chip);
4577 check( *linetable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
4578 "No line table (%s, %s or %s) found in SOF", tags[0], tags[1], tags[2]);
4582 tags[0] = UVES_LINE_TABLE(flames, chip);
4583 tags[1] = UVES_LINE_TABLE(flames, chip);
4584 tags[2] = UVES_GUESS_LINE_TABLE(flames, chip);
4588 if (cpl_frameset_find_const(frames, tags[0]) == NULL &&
4589 cpl_frameset_find_const(frames, tags[1]) == NULL &&
4590 cpl_frameset_find_const(frames, tags[2]) == NULL)
4598 tags[0] = UVES_LINE_TABLE_MIDAS(chip, window);
4599 tags[1] = UVES_LINE_TABLE_MIDAS(chip, window);
4600 tags[2] = UVES_LINE_TABLE_MIDAS(chip, window);
4607 tags[0] = UVES_LINE_TABLE_MIDAS(chip, 1);
4608 tags[1] = UVES_LINE_TABLE_MIDAS(chip, 2);
4609 tags[2] = UVES_LINE_TABLE_MIDAS(chip, 3);
4611 uves_msg_debug(
"Trying %s, %s or %s", tags[0], tags[1], tags[2]);
4615 check( *linetable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
4616 "No line table (%s, %s or %s) found in SOF", tags[0], tags[1], tags[2]);
4621 "Could not load primary header of '%s'", *linetable_filename);
4623 check_nomsg( uves_warn_if_chip_names_dont_match(primary_header, chip_name, chip) );
4629 if (strstr(drs_id,
"CPL") != NULL || strstr(drs_id,
"cpl") != NULL)
4631 format_is_midas =
false;
4634 else if (strstr(drs_id,
"MIDAS") != NULL || strstr(drs_id,
"midas") != NULL)
4636 format_is_midas =
true;
4642 CPL_ERROR_ILLEGAL_INPUT,
4643 "Unrecognized line table format, DRS_ID = '%s'", drs_id);
4648 format_is_midas =
true;
4649 uves_msg_debug(
"No '%s' keyword found. Assuming MIDAS format", UVES_DRS_ID);
4652 if (format_is_midas || flames)
4656 assure( trace_id == 0 && (window == -1 || (1 <= window && window <= 3)),
4657 CPL_ERROR_UNSUPPORTED_MODE,
4658 "Cannot read (fibre, window) = (%d, %d) from MIDAS line table",
4668 assure( ((1<= trace_id && trace_id <= 9) && (window == -1)),
4669 CPL_ERROR_UNSUPPORTED_MODE,
4670 "Cannot read (fibre, window) = (%d, %d) from MIDAS line table",
4689 check( nextensions = uves_get_nextensions(*linetable_filename),
4690 "Error reading number of extensions of file '%s'", *linetable_filename);
4696 for (base_extension = 1; base_extension < nextensions && !found; base_extension++)
4702 check(( uves_free_propertylist(&header),
4704 "Could not header of extension %d of '%s'",
4705 base_extension, *linetable_filename);
4708 "Error reading trace ID from header of extension %d of '%s'",
4709 base_extension, *linetable_filename);
4712 "Error reading window number from header of extension %d of '%s'",
4713 base_extension, *linetable_filename);
4715 uves_msg_debug(
"Found (trace, window) = (%d, %d), need (%d, %d)",
4716 header_trace, header_window,
4719 found = ( (trace_id == header_trace) &&
4720 (window == -1 || window == header_window) );
4724 CPL_ERROR_ILLEGAL_INPUT,
4725 "Line table (trace, window) = (%d, %d) is not present in file '%s'",
4726 trace_id, window, *linetable_filename);
4730 base_extension -= 2;
4734 check( *linetable = cpl_table_load(*linetable_filename,
4735 base_extension + UVES_LINE_TABLE_EXTENSION,
4738 "Error loading line table from extension %d of file '%s'",
4739 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4742 if (linetable_header != NULL)
4744 check( *linetable_header =
4746 base_extension + UVES_LINE_TABLE_EXTENSION),
4747 "Could not load header of extension %d of '%s'",
4748 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4750 if (format_is_midas)
4754 absorders = uves_read_midas_array(*linetable_header,
"ORDER", &size,
4757 assure( type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4758 "Type of ORDER is %s, int expected",
4762 CPL_ERROR_ILLEGAL_INPUT,
4763 "'ORDER' array has size %d. Size 2 expected.", size);
4766 "Error updating table header");
4771 if (format_is_midas)
4774 check(( cpl_table_cast_column(*linetable,
"X",
"xxxx", CPL_TYPE_DOUBLE),
4775 cpl_table_erase_column(*linetable,
"X"),
4776 cpl_table_name_column(*linetable,
"xxxx",
"X")),
4777 "Error casting and renaming column 'X'");
4779 check(( cpl_table_cast_column(*linetable,
"YNEW",
"xxxx", CPL_TYPE_DOUBLE),
4780 cpl_table_erase_column(*linetable,
"YNEW"),
4781 cpl_table_name_column(*linetable,
"xxxx",
"Ynew")),
4782 "Error casting and renaming column 'YNEW'");
4784 check(( cpl_table_cast_column(*linetable,
"Y",
"xxxx", CPL_TYPE_INT),
4785 cpl_table_erase_column(*linetable,
"Y"),
4786 cpl_table_name_column(*linetable,
"xxxx",
"Y")),
4787 "Error casting and renaming column 'Y'");
4789 check(( cpl_table_cast_column(*linetable,
"ORDER",
"Order", CPL_TYPE_INT),
4790 cpl_table_erase_column(*linetable,
"ORDER")),
4791 "Error casting and renaming column 'ORDER'");
4793 check( cpl_table_name_column(*linetable,
"IDENT",
"Ident"),
4794 "Error renaming column 'IDENT'");
4797 *linetable_filename,
4798 base_extension + UVES_LINE_TABLE_EXTENSION),
4799 "Could not load header of extension %d of '%s'",
4800 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4802 if (dispersion_relation != NULL) {
4804 check( *dispersion_relation =
4805 uves_polynomial_convert_from_plist_midas(midas_header,
4807 "Error reading polynomial 'REGR%d' from '%s'",
4809 *linetable_filename);
4812 check( *dispersion_relation =
4813 uves_polynomial_convert_from_plist_midas(midas_header,
4815 "Error reading polynomial 'REGR' from '%s'",
4816 *linetable_filename);
4821 check( absolute_order_local =
4822 uves_polynomial_convert_from_plist_midas(midas_header,
"RORD",-1),
4823 "Error reading polynomial 'RORD' from '%s'", *linetable_filename);
4838 if (cpl_table_has_column(*linetable,
"YNEW"))
4840 cpl_table_name_column(*linetable,
"YNEW",
"Ynew");
4843 if (dispersion_relation != NULL)
4845 check( *dispersion_relation = load_polynomial(
4846 *linetable_filename,
4847 base_extension + UVES_LINE_TABLE_EXTENSION_DISPERSION),
4848 "Could not read polynomial from extension %d of file '%s'",
4849 base_extension + UVES_LINE_TABLE_EXTENSION_DISPERSION,
4850 *linetable_filename);
4853 check( absolute_order_local =
4854 load_polynomial(*linetable_filename,
4855 base_extension + UVES_LINE_TABLE_EXTENSION_ABSORDER),
4856 "Could not read polynomial from extension %d of file '%s'",
4857 base_extension + UVES_LINE_TABLE_EXTENSION_ABSORDER, *linetable_filename);
4860 if (absolute_order != NULL)
4866 check( align_order_line_table(
4867 *linetable, absolute_order_local, linetable_header,
4868 order_locations, minorder, maxorder),
4869 "Error while aligning line/order tables");
4874 const char *colname;
4882 uves_free_table(&temp);
4883 check(( temp = cpl_table_new(0),
4884 cpl_table_copy_structure(temp, *linetable)),
4885 "Error duplicating line table column structure");
4887 colname = cpl_table_get_column_name(temp);
4888 while (colname != NULL)
4890 if (!(strcmp(colname,
"X" ) == 0 ||
4891 strcmp(colname,
"Order" ) == 0 ||
4892 strcmp(colname,
"Ident" ) == 0 ||
4893 strcmp(colname,
"FIBRE" ) == 0 ||
4894 strcmp(colname,
"Fibre" ) == 0 ||
4895 strcmp(colname, LINETAB_PIXELSIZE) == 0))
4897 cpl_table_erase_column(*linetable, colname);
4902 colname = cpl_table_get_column_name(NULL);
4909 if ( !cpl_table_has_column(*linetable, LINETAB_PIXELSIZE) )
4911 check( create_column_pixelsize(*linetable),
4912 "Error adding 'Pixelsize' column");
4916 check( uves_erase_invalid_table_rows(*linetable,
"Ident"),
4917 "Error deleting rows with illegal 'Ident' value");
4919 check( uves_erase_table_rows(*linetable,
"Ident", CPL_LESS_THAN, 0.01),
4920 "Error deleting rows with illegal 'Ident' value");
4923 assure( uves_erase_invalid_table_rows(*linetable, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
4924 "After deleting rows with invalid 'Ident' values, "
4925 "the table in extension %d of file '%s' still contains invalid rows",
4926 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4929 check( uves_sort_table_2(*linetable,
"Order",
"X",
false,
false),
"Error sorting line table");
4932 uves_free_propertylist(&primary_header);
4933 uves_free_propertylist(&header);
4934 uves_free_propertylist(&midas_header);
4935 uves_free_table(&temp);
4937 cpl_free(absorders);
4938 if (cpl_error_get_code() != CPL_ERROR_NONE) {
4939 *linetable_filename = NULL;
4940 uves_free_table(linetable);
4953 uves_load_linetable_const(
const cpl_frameset *frames,
4955 const char *chip_name,
4956 const polynomial *order_locations,
int minorder,
int maxorder,
4957 const char **linetable_filename,
4958 const cpl_table **linetable,
4962 enum uves_chip chip,
int trace_id,
int window)
4964 uves_load_linetable(frames, flames, chip_name, order_locations,
4967 (cpl_table **)linetable,
4971 chip, trace_id, window);
4994 uves_load_response_curve(
const cpl_frameset *frames,
const char *chip_name,
4995 const char **response_filename,
4996 cpl_image **response_curve,
4997 cpl_table **master_response,
5000 const char *tags[2];
5001 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5005 *response_curve = NULL;
5006 *response_header = NULL;
5007 *master_response = NULL;
5009 tags[0] = UVES_INSTR_RESPONSE (chip);
5010 tags[1] = UVES_MASTER_RESPONSE(chip);
5012 check( *response_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
5014 "Could not find '%s' in frame set", tags[0]);
5019 extension = UVES_INSTR_RESPONSE_EXTENSION(chip);
5028 check( *response_curve = uves_load_image_file(*response_filename,
5035 "Could not load response curve from extension %d of file '%s'",
5036 extension, *response_filename);
5045 check_nomsg( uves_warn_if_chip_names_dont_match(*response_header, chip_name, chip) );
5050 extension = UVES_MASTER_RESPONSE_EXTENSION(chip);
5052 check( *master_response = cpl_table_load(*response_filename,
5053 UVES_LINE_INTMON_TABLE_EXTENSION,
5056 "Error master response curve from extension %d of file '%s'",
5057 extension, *response_filename);
5060 check(( cpl_table_cast_column(*master_response,
"LAMBDA",
"LAMBDA_double",
5062 cpl_table_erase_column(*master_response,
"LAMBDA"),
5063 cpl_table_name_column(*master_response,
"LAMBDA_double",
"LAMBDA")),
5064 "Could not cast column 'LAMBDA'");
5066 check(( cpl_table_cast_column(*master_response,
"FLUX_CONV",
"FLUX_CONV_double",
5068 cpl_table_erase_column(*master_response,
"FLUX_CONV"),
5069 cpl_table_name_column(*master_response,
"FLUX_CONV_double",
"FLUX_CONV")),
5070 "Could not cast column 'FLUX_CONV'");
5077 if (cpl_error_get_code() != CPL_ERROR_NONE)
5079 *response_filename = NULL;
5080 uves_free_image(response_curve);
5081 uves_free_propertylist(response_header);
5083 return cpl_error_get_code();
5098 cpl_error_code uves_load_lineintmon(
const cpl_frameset *frames,
5099 const char **line_intmon_filename,
5100 cpl_table **line_intmon)
5102 const char *tags[1] = {UVES_LINE_INTMON_TABLE};
5104 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5108 check( *line_intmon_filename = uves_find_frame(frames, tags, number_of_tags,
5110 "No line intensity table (%s) found in SOF", tags[0]);
5113 check( *line_intmon = cpl_table_load(*line_intmon_filename,
5114 UVES_LINE_INTMON_TABLE_EXTENSION,
5117 "Error loading line reference table from extension %d of file '%s'",
5118 UVES_LINE_INTMON_TABLE_EXTENSION, *line_intmon_filename);
5120 check(( cpl_table_cast_column(*line_intmon,
"WAVE",
"Wave", CPL_TYPE_DOUBLE),
5121 cpl_table_erase_column(*line_intmon,
"WAVE")),
5122 "Could not cast and rename column");
5125 check( uves_sort_table_1(*line_intmon,
"Wave",
false),
"Error sorting table");
5128 if (cpl_error_get_code() != CPL_ERROR_NONE)
5130 *line_intmon_filename = NULL;
5131 uves_free_table(line_intmon);
5133 return cpl_error_get_code();
5151 uves_load_corvel(
const cpl_frameset *frames,
5154 const char **corvel_filename)
5156 const char *tags[1];
5157 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5161 tags[0] = FLAMES_CORVEL_MASK;
5164 assure_nomsg( corvel_filename != NULL, CPL_ERROR_NULL_INPUT );
5167 check( *corvel_filename = uves_find_frame(frames, tags, number_of_tags,
5169 "No velocity correction table (%s) found in SOF", tags[0]);
5173 check( *corvel = cpl_table_load(*corvel_filename,
5177 "Error loading line reference table from extension %d of file '%s'",
5178 extension, *corvel_filename);
5181 if (corvel_header != NULL)
5186 "Could not load header from extension %d of file %s",
5187 extension, *corvel_filename);
5192 if (cpl_error_get_code() != CPL_ERROR_NONE)
5194 *corvel_filename = NULL;
5195 uves_free_table(corvel);
5218 uves_load_linerefertable(
const cpl_frameset *frames,
5219 const char **line_refer_filename,
5222 const char *tags[1] = {UVES_LINE_REFER_TABLE};
5224 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5228 check( *line_refer_filename = uves_find_frame(frames, tags, number_of_tags,
5230 "No line reference table (%s) found in SOF", tags[0]);
5233 check( *line_refer = cpl_table_load(*line_refer_filename,
5234 UVES_LINE_REFER_TABLE_EXTENSION,
5237 "Error loading line reference table from extension %d of file '%s'",
5238 UVES_LINE_REFER_TABLE_EXTENSION, *line_refer_filename);
5241 if (line_refer_header != NULL)
5244 "Could not load header of line_refer table in '%s'", *line_refer_filename);
5247 assure( uves_erase_invalid_table_rows(*line_refer, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
5248 "Table in extension %d of file '%s' contains invalid rows",
5249 UVES_LINE_REFER_TABLE_EXTENSION, *line_refer_filename);
5251 check(( cpl_table_cast_column(*line_refer,
"WAVE",
"Wave", CPL_TYPE_DOUBLE),
5252 cpl_table_erase_column(*line_refer,
"WAVE")),
5253 "Could not cast and rename column");
5266 check(( cpl_table_duplicate_column(*line_refer,
"dWave", *line_refer,
"Wave"),
5267 cpl_table_divide_scalar (*line_refer,
"dWave", 300000*10)),
5268 "Error writing wavelength uncertainties");
5271 check(( cpl_table_new_column(*line_refer,
"dWave", CPL_TYPE_DOUBLE),
5272 cpl_table_fill_column_window(*line_refer,
5275 cpl_table_get_nrow(*line_refer), 0.002)),
5276 "Error writing wavelength uncertainties");
5280 check( uves_sort_table_1(*line_refer,
"Wave",
false),
"Error sorting table");
5283 if (cpl_error_get_code() != CPL_ERROR_NONE) {
5284 *line_refer_filename = NULL;
5285 uves_free_table (line_refer);
5286 if (line_refer_header != NULL) uves_free_propertylist(line_refer_header);
5288 return cpl_error_get_code();
5307 uves_load_flux_table(
const cpl_frameset *frames,
const char **flux_table_filename,
5308 cpl_table **flux_table)
5310 const char *tags[1] = {UVES_FLUX_STD_TABLE};
5312 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5316 check( *flux_table_filename = uves_find_frame(frames, tags, number_of_tags,
5318 "No standard star flux table (%s) in SOF", tags[0]);
5321 check( *flux_table = cpl_table_load(*flux_table_filename,
5322 UVES_FLUX_STD_TABLE_EXTENSION,
5325 "Error loading flux table from extension %d of file '%s'",
5326 UVES_FLUX_STD_TABLE_EXTENSION, *flux_table_filename);
5333 if (uves_erase_invalid_table_rows(*flux_table, NULL) != 0)
5335 uves_msg_warning(
"Table in extension %d of file '%s' contains null values",
5336 UVES_FLUX_STD_TABLE_EXTENSION, *flux_table_filename);
5342 for (i = 0; i < cpl_table_get_nrow(*flux_table); i++)
5344 if (cpl_table_get_string(*flux_table,
"TYPE", i) == NULL)
5346 cpl_table_set_string(*flux_table,
"TYPE", i,
"NULL");
5353 if (cpl_error_get_code() != CPL_ERROR_NONE)
5355 *flux_table_filename = NULL;
5356 uves_free_table(flux_table);
5358 return cpl_error_get_code();
5378 uves_load_atmo_ext(
const cpl_frameset *frames,
const char **atmext_table_filename,
5379 cpl_table **atmext_table)
5381 const char *tags[1] = {UVES_EXTCOEFF_TABLE};
5383 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5387 check( *atmext_table_filename = uves_find_frame(frames, tags, number_of_tags,
5389 "No atmospheric extinction table (%s) found in SOF", tags[0]);
5392 check( *atmext_table = cpl_table_load(*atmext_table_filename,
5393 UVES_EXTCOEFF_TABLE_EXTENSION,
5396 "Error loading atmospheric extinction table from extension %d of file '%s'",
5397 UVES_EXTCOEFF_TABLE_EXTENSION, *atmext_table_filename);
5399 assure( uves_erase_invalid_table_rows(*atmext_table, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
5400 "Table in extension %d of file '%s' contains invalid rows",
5401 UVES_EXTCOEFF_TABLE_EXTENSION, *atmext_table_filename);
5403 check( uves_sort_table_1(*atmext_table,
"LAMBDA",
false),
5404 "Error sorting table");
5407 check(( cpl_table_cast_column(*atmext_table,
"LAMBDA",
"LAMBDA_double", CPL_TYPE_DOUBLE),
5408 cpl_table_erase_column(*atmext_table,
"LAMBDA"),
5409 cpl_table_name_column(*atmext_table,
"LAMBDA_double",
"LAMBDA")),
5410 "Could not cast column 'LAMBDA'");
5412 check(( cpl_table_cast_column(*atmext_table,
"LA_SILLA",
"LA_SILLA_double", CPL_TYPE_DOUBLE),
5413 cpl_table_erase_column(*atmext_table,
"LA_SILLA"),
5414 cpl_table_name_column(*atmext_table,
"LA_SILLA_double",
"LA_SILLA")),
5415 "Could not cast column 'LA_SILLA'");
5418 if (cpl_error_get_code() != CPL_ERROR_NONE)
5420 *atmext_table_filename = NULL;
5421 uves_free_table(atmext_table);
5423 return cpl_error_get_code();
5435 uves_guess_order_table_filename(
enum uves_chip chip)
5437 return uves_local_filename(
"orderguesstable", chip, -1, -1);
5450 uves_order_table_filename(
enum uves_chip chip)
5452 return uves_local_filename(
"ordertable", chip, -1, -1);
5463 char *uves_ordef_filename(
enum uves_chip chip)
5465 return uves_local_filename(
"order_def", chip, -1, -1);
5478 uves_masterdark_filename(
enum uves_chip chip)
5480 return uves_local_filename(
"masterdark", chip, -1, -1);
5492 uves_flat_ratio_filename(
enum uves_chip chip)
5494 return uves_local_filename(
"ratio", chip, -1, -1);
5505 char *uves_cd_align_filename(
enum uves_chip chip)
5507 return uves_local_filename(
"cd_align", chip, -1, -1);
5520 uves_masterflat_filename(
enum uves_chip chip)
5522 return uves_local_filename(
"masterflat", chip, -1, -1);
5534 uves_masterflat_bkg_filename(
enum uves_chip chip)
5536 return uves_local_filename(
"masterflat_bkg", chip, -1, -1);
5549 uves_masterbias_filename(
enum uves_chip chip)
5551 return uves_local_filename(
"masterbias", chip, -1, -1);
5564 uves_guess_line_table_filename(
enum uves_chip chip)
5566 return uves_local_filename(
"lineguesstable", chip, -1, -1);
5578 uves_line_table_filename(
enum uves_chip chip)
5580 return uves_local_filename(
"linetable", chip, -1, -1);
5593 uves_line_table_filename_paf(
enum uves_chip chip)
5595 return uves_local_filename(
"linetable_paf", chip, -1, -1);
5608 uves_response_curve_filename(
enum uves_chip chip)
5610 return uves_local_filename(
"response", chip, -1, -1);
5623 uves_response_curve_2d_filename(
enum uves_chip chip)
5625 return uves_local_filename(
"response_2d", chip, -1, -1);
5638 uves_response_red_standard_filename(
enum uves_chip chip)
5640 return uves_local_filename(
"red_std", chip, -1, -1);
5654 uves_response_red_noappend_standard_filename(
enum uves_chip chip)
5656 return uves_local_filename(
"red_nonmerged", chip, -1, -1);
5669 uves_response_bkg_standard_filename(
enum uves_chip chip)
5671 return uves_local_filename(
"bkg_std", chip, -1, -1);
5685 uves_order_extract_qc_standard_filename(
enum uves_chip chip)
5687 return uves_local_filename(
"order_extract_qc", chip, -1, -1);
5700 uves_response_efficiency_filename(
enum uves_chip chip)
5702 return uves_local_filename(
"efficiency", chip, -1, -1);
5716 uves_scired_red_2d_science_filename(
enum uves_chip chip)
5718 return uves_local_filename(
"red_2d_science", chip, -1, -1);
5733 uves_scired_red_science_filename(
enum uves_chip chip)
5735 return uves_local_filename(
"red_science", chip, -1, -1);
5749 uves_scired_red_noappend_science_filename(
enum uves_chip chip)
5751 return uves_local_filename(
"red_nonmerged_science", chip, -1, -1);
5763 uves_scired_red_error_filename(
enum uves_chip chip)
5765 return uves_local_filename(
"error_red_science", chip, -1, -1);
5778 uves_scired_red_noappend_error_filename(
enum uves_chip chip)
5780 return uves_local_filename(
"error_red_nonmerged_science", chip, -1, -1);
5793 uves_scired_red_2d_error_filename(
enum uves_chip chip)
5795 return uves_local_filename(
"error_2d_science", chip, -1, -1);
5809 uves_scired_fluxcal_science_filename(
enum uves_chip chip)
5811 return uves_local_filename(
"fluxcal_science", chip, -1, -1);
5825 uves_scired_fluxcal_science_noappend_filename(
enum uves_chip chip)
5827 return uves_local_filename(
"fluxcal_nonmerged_science", chip, -1, -1);
5839 uves_scired_fluxcal_error_filename(
enum uves_chip chip)
5841 return uves_local_filename(
"fluxcal_error_science", chip, -1, -1);
5855 uves_scired_fluxcal_error_noappend_filename(
enum uves_chip chip)
5857 return uves_local_filename(
"fluxcal_error_nonmerged_science", chip, -1, -1);
5871 uves_scired_fluxcal_science_2d_filename(
enum uves_chip chip)
5873 return uves_local_filename(
"fluxcal_2d_science", chip, -1, -1);
5885 uves_scired_fluxcal_error_2d_filename(
enum uves_chip chip)
5887 return uves_local_filename(
"fluxcal_error_2d_science", chip, -1, -1);
5899 uves_scired_ff_variance_filename(
enum uves_chip chip)
5901 return uves_local_filename(
"variance_ff_science", chip, -1, -1);
5914 uves_scired_ff_variance_2d_filename(
enum uves_chip chip)
5916 return uves_local_filename(
"variance_ff_2d_science", chip, -1, -1);
5928 uves_scired_merged_2d_science_filename(
enum uves_chip chip)
5930 return uves_local_filename(
"merged_2d_science", chip, -1, -1);
5942 uves_scired_merged_science_filename(
enum uves_chip chip)
5944 return uves_local_filename(
"merged_science", chip, -1, -1);
5956 uves_scired_merged_sky_filename(
enum uves_chip chip)
5958 return uves_local_filename(
"merged_sky", chip, -1, -1);
5971 uves_scired_background_filename(
enum uves_chip chip)
5973 return uves_local_filename(
"background", chip, -1, -1);
5986 uves_scired_resampled_filename(
enum uves_chip chip)
5988 return uves_local_filename(
"resampled_science", chip, -1, -1);
6003 uves_scired_resampled_2d_filename(
enum uves_chip chip)
6005 return uves_local_filename(
"resampled_2d_science", chip, -1, -1);
6019 uves_scired_resampledmf_filename(
enum uves_chip chip)
6021 return uves_local_filename(
"resampled_mflat", chip, -1, -1);
6035 uves_scired_rebinned_filename(
enum uves_chip chip)
6037 return uves_local_filename(
"resampled_ff_science", chip, -1, -1);
6051 uves_scired_rebinned_error_filename(
enum uves_chip chip)
6053 return uves_local_filename(
"resampled_error_ff_science", chip, -1, -1);
6067 uves_scired_rebinned_2d_filename(
enum uves_chip chip)
6069 return uves_local_filename(
"resampled_ff_2d_science", chip, -1, -1);
6082 uves_scired_rebinned_2d_error_filename(
enum uves_chip chip)
6084 return uves_local_filename(
"resampled_error_ff_2d_science", chip, -1, -1);
6096 uves_scired_ordertrace_filename(
enum uves_chip chip)
6098 return uves_local_filename(
"ordertrace", chip, -1, -1);
6111 uves_scired_crmask_filename(
enum uves_chip chip)
6113 return uves_local_filename(
"cr_mask", chip, -1, -1);
6126 uves_scired_wmap_filename(
enum uves_chip chip)
6128 return uves_local_filename(
"wave_map", chip, -1, -1);
6140 char *uves_scired_ext2d_filename(
enum uves_chip chip)
6142 return uves_local_filename(
"ext_2d_science", chip, -1, -1);
6154 char *uves_scired_ff2d_filename(
enum uves_chip chip)
6156 return uves_local_filename(
"ff_2d_science", chip, -1, -1);
6182 uves_local_filename(
const char *prefix,
enum uves_chip chip,
int trace,
int window)
6184 char *result = NULL;
6185 const char *chip_string;
6186 const char *suffix =
".fits";
6190 assure( (trace < 0 && window < 0) ||
6191 (trace < 0 && window > 0) ||
6192 (trace >= 0 && window > 0),
6193 CPL_ERROR_ILLEGAL_INPUT,
"Illegal trace and window numbers: (%d, %d)",
6200 check(( t = int_to_string(trace),
6201 w = int_to_string(window)),
6202 "Error creating substrings");
6218 result = uves_sprintf(
"%s_%s%s%s%s", prefix, chip_string, t, w, suffix);
6224 if (cpl_error_get_code() != CPL_ERROR_NONE)
6226 cpl_free(result); result = NULL;
6244 int_to_string(
int i)
6246 char *result = NULL;
6248 assure( -1 <= i, CPL_ERROR_ILLEGAL_INPUT,
"Illegal number (%d)", i);
6253 result = cpl_calloc(1,
sizeof(
char));
6258 result = uves_sprintf(
"_%d", i);
6262 if (cpl_error_get_code() != CPL_ERROR_NONE){
6263 cpl_free(result); result = NULL;
6282 uves_vector_to_image(
const cpl_vector* vector,cpl_type type)
6285 cpl_image* image=NULL;
6287 const double* pv=NULL;
6293 size=cpl_vector_get_size(vector);
6294 image=cpl_image_new(size,1,type);
6295 pv=cpl_vector_get_data_const(vector);
6296 if(type == CPL_TYPE_INT) {
6297 pi=cpl_image_get_data_int(image);
6298 for(i=0;i<size;i++) {
6301 }
else if (type == CPL_TYPE_FLOAT) {
6302 pf=cpl_image_get_data_float(image);
6303 for(i=0;i<size;i++) {
6306 }
else if (type == CPL_TYPE_DOUBLE) {
6307 pd=cpl_image_get_data_double(image);
6308 for(i=0;i<size;i++) {
6312 assure(
false, CPL_ERROR_INVALID_TYPE,
6313 "No CPL type to represent BITPIX = %d", type);
6317 if (cpl_error_get_code() != CPL_ERROR_NONE){
6318 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.