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");
2708 ext_header,
".*", 0),
2709 "Error merging primary header with extension %d header",
2716 for (extension = 1; extension <= 2; extension++)
2718 enum uves_chip chip = (extension == 1) ? UVES_CHIP_REDU : UVES_CHIP_REDL;
2724 uves_free_image(&image);
2725 check( image = cpl_image_load(filename,
2729 "Could not load image from extension %d of file '%s' ",
2730 extension, filename);
2732 check( raw_image[indx] = uves_crop_and_rotate(image,
2736 &rotated_header[indx]),
2737 "Error splitting red image");
2745 uves_free_image(&image);
2746 uves_free_image(&image1);
2747 uves_free_image(&image2);
2749 uves_free_propertylist(&primary_header);
2750 uves_free_propertylist(&ext_header);
2752 if (cpl_error_get_code() != CPL_ERROR_NONE)
2754 uves_free_image (&raw_image[0]);
2755 uves_free_image (&raw_image[1]);
2756 uves_free_propertylist(&raw_header[0]);
2757 uves_free_propertylist(&raw_header[1]);
2758 uves_free_propertylist(&rotated_header[0]);
2759 uves_free_propertylist(&rotated_header[1]);
2762 return cpl_error_get_code();
2796 uves_load_raw_imagelist(
const cpl_frameset *frames,
2798 const char *blue_tag,
const char *red_tag, cpl_type type,
2799 cpl_imagelist *images[2],
2803 const char *tag = NULL;
2804 const cpl_frame *frame = NULL;
2805 cpl_image *temp_image[2] = {NULL, NULL};
2807 cpl_size number_of_frames = 0;
2808 int frameset_size = 0;
2812 raw_headers[0] = NULL;
2813 raw_headers[1] = NULL;
2815 check( frameset_size = cpl_frameset_get_size(frames),
2816 "Error reading frameset size");
2818 check( tag = identify_arm(frames, blue_tag, red_tag, blue),
2819 "Could not identify chip type");
2821 nchips = (*blue) ? 1 : 2;
2822 for(chip = 0; chip < nchips; chip++)
2824 images[chip] = NULL;
2825 rotated_header[chip] = NULL;
2827 images[chip] = cpl_imagelist_new();
2835 number_of_frames = 0;
2838 nfrm=cpl_frameset_get_size(frames);
2841 frame=cpl_frameset_get_frame_const(frames,i);
2843 if ( strcmp(cpl_frame_get_tag(frame), tag) == 0)
2845 const char *filename = cpl_frame_get_filename(frame);
2848 uves_free_propertylist(&rotated_header[0]);
2849 uves_free_propertylist(&rotated_header[1]);
2851 check( load_raw_image(filename,
2858 "Could not load image from file '%s'", filename);
2861 for(chip = 0; chip < nchips; chip++)
2863 raw_headers[chip][number_of_frames] = temp_header[chip];
2864 temp_header[chip] = NULL;
2866 check( cpl_imagelist_set(images[chip],
2869 cpl_imagelist_get_size(images[chip])
2871 "Could not insert image into image list");
2874 temp_image[chip] = NULL;
2877 number_of_frames += 1;
2883 for(chip = 0; chip < nchips; chip++)
2886 assure (cpl_imagelist_is_uniform(images[chip]) == 0,
2887 CPL_ERROR_INCOMPATIBLE_INPUT,
2888 "Input images are not of same size and type");
2890 passure( cpl_imagelist_get_size(images[chip]) == number_of_frames,
2891 "%" CPL_SIZE_FORMAT
" %" CPL_SIZE_FORMAT
"", cpl_imagelist_get_size(images[0]), number_of_frames);
2897 if ( strcmp(UVES_BIAS (*blue), tag) != 0 &&
2898 strcmp(UVES_DARK (*blue), tag) != 0 &&
2899 strcmp(UVES_PDARK(*blue), tag) != 0) {
2900 enum uves_chip chip_id;
2905 chip_id != UVES_CHIP_INVALID;
2907 for (i = 0; i < number_of_frames; i++) {
2911 "Error reading central wavelength of input frame number %d", i+1);
2918 "Error reading central wavelength of input frame number %d", i+1);
2920 assure( fabs((w-wlen)/wlen) < 0.01, CPL_ERROR_INCOMPATIBLE_INPUT,
2921 "Mis-matching input frame central wavelengths: "
2922 "%e (frame 1) != %e (frame %d)", wlen, w, i+1);
2929 uves_free_image(&temp_image[0]);
2930 uves_free_image(&temp_image[1]);
2931 uves_free_propertylist(&temp_header[0]);
2932 uves_free_propertylist(&temp_header[1]);
2934 if (cpl_error_get_code() != CPL_ERROR_NONE) {
2935 if (raw_headers[0] != NULL) {
2937 for (i = 0; i < frameset_size; i++) {
2938 if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
2939 if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
2942 cpl_free(raw_headers[0]); raw_headers[0] = NULL;
2943 cpl_free(raw_headers[1]); raw_headers[1] = NULL;
2945 uves_free_imagelist(&images[0]);
2946 uves_free_imagelist(&images[1]);
2948 uves_free_propertylist(&rotated_header[0]);
2949 uves_free_propertylist(&rotated_header[1]);
2952 return cpl_error_get_code();
2975 uves_load_orderpos(
const cpl_frameset *frames,
2977 const char **raw_filename,
2978 cpl_image *raw_image[2],
2982 const char *tags[4];
2984 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
2989 tags[0] = UVES_ORDER_FLAT(flames,
false);
2990 tags[1] = UVES_ORDER_FLAT(flames,
true);
2991 tags[2] = UVES_STD_STAR(
false);
2992 tags[3] = UVES_STD_STAR(
true);
2999 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
3001 "Could not find raw frame (%s) in SOF",
3007 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
3009 "Could not find raw frame (%s, %s, %s, or %s) in SOF",
3010 tags[0], tags[1], tags[2], tags[3]);
3012 *blue = (indx == 1) || (indx == 3);
3016 check( load_raw_image(*raw_filename,
3023 "Error loading image from file '%s'", *raw_filename);
3025 passure( !flames || !(*blue),
"%d %d",
3029 if (cpl_error_get_code() != CPL_ERROR_NONE)
3031 *raw_filename = NULL;
3034 return cpl_error_get_code();
3055 uves_load_formatcheck(
const cpl_frameset *frames,
3057 const char **raw_filename,
3058 cpl_image *raw_image[2],
3062 const char *tags[2];
3063 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3066 tags[0] = UVES_FORMATCHECK(flames,
false);
3067 tags[1] = UVES_FORMATCHECK(flames,
true);
3073 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3074 "Could not find raw frame (%s) in SOF",
3079 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3080 "Could not find raw frame (%s or %s) in SOF",
3083 *blue = (indx == 1);
3087 check( load_raw_image(*raw_filename,
3094 "Error loading image from file '%s'", *raw_filename);
3097 if (cpl_error_get_code() != CPL_ERROR_NONE)
3099 *raw_filename = NULL;
3101 return cpl_error_get_code();
3124 void uves_load_cd_align(
const cpl_frameset *frames,
3125 const char **raw_filename1,
3126 const char **raw_filename2,
3127 cpl_image *raw_image1[2],
3128 cpl_image *raw_image2[2],
3135 const char *tags[2];
3136 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3138 bool flames =
false;
3139 const cpl_frame *frame;
3141 tags[0] = UVES_CD_ALIGN(
false);
3142 tags[1] = UVES_CD_ALIGN(
true);
3144 check( *raw_filename1 = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3145 "Could not find raw frame (%s or %s) in SOF",
3148 *blue = (indx == 1);
3150 assure( cpl_frameset_count_tags(frames, tags[indx]) == 2,
3151 CPL_ERROR_ILLEGAL_INPUT,
3152 "%d %s frames found. Exactly 2 required",
3153 cpl_frameset_count_tags(frames, tags[indx]), tags[indx] );
3159 int nfrm=cpl_frameset_get_size(frames);
3160 for (i = 0;i < nfrm;i++)
3162 frame=cpl_frameset_get_frame_const(frames,i);
3163 if (strcmp(cpl_frame_get_tag(frame), tags[indx]) == 0)
3167 *raw_filename1 = cpl_frame_get_filename(frame);
3171 *raw_filename2 = cpl_frame_get_filename(frame);
3174 check( load_raw_image(n == 1 ?
3189 "Error loading image from file '%s'",
3190 n == 1 ? *raw_filename1 : *raw_filename2);
3198 if (cpl_error_get_code() != CPL_ERROR_NONE)
3200 *raw_filename1 = NULL;
3201 *raw_filename2 = NULL;
3231 uves_load_arclamp(
const cpl_frameset *frames,
3233 const char **raw_filename,
3238 const char *tags[4];
3240 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3248 tags[0] = UVES_ARC_LAMP(flames,
true);
3249 tags[1] = FLAMES_FIB_SCI_SIM;
3254 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3255 "Could not find raw frame (%s or %s) in SOF",
3258 *sim_cal = (indx == 1);
3262 tags[0] = UVES_ARC_LAMP(flames,
true);
3263 tags[1] = UVES_ARC_LAMP(flames,
false);
3264 tags[2] = UVES_ECH_ARC_LAMP(
true);
3265 tags[3] = UVES_ECH_ARC_LAMP(
false);
3267 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3268 "Could not find raw frame (%s, %s, %s or %s) in SOF",
3269 tags[0], tags[1], tags[2], tags[3]);
3271 *blue = (indx == 0 || indx == 2);
3275 check( load_raw_image(*raw_filename,
3282 "Error loading image from file '%s'", *raw_filename);
3285 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3286 *raw_filename = NULL;
3287 uves_free_image (raw_image);
3288 uves_free_propertylist(raw_header);
3310 uves_load_science(
const cpl_frameset *frames,
const char **raw_filename,
3311 cpl_image *raw_image[2],
3315 const char **sci_type)
3318 const char *tags[] =
3320 UVES_SCIENCE(
true), UVES_SCIENCE(
false),
3321 UVES_SCI_EXTND(
true), UVES_SCI_EXTND(
false),
3322 UVES_SCI_POINT(
true), UVES_SCI_POINT(
false),
3323 UVES_SCI_SLICER(
true), UVES_SCI_SLICER(
false),
3324 UVES_TFLAT(
true), UVES_TFLAT(
false)
3327 const char *type[] =
3329 "SCIENCE",
"SCIENCE",
3330 "SCI_EXTND",
"SCI_EXTND",
3331 "SCI_POINT",
"SCI_POINT",
3332 "SCI_SLICER",
"SCI_SLICER",
3336 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3338 bool flames =
false;
3340 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3341 "No science frame (%s, %s, %s, %s, %s, %s, %s, %s, %s or %s) in SOF",
3342 tags[0], tags[1], tags[2], tags[3],
3343 tags[4], tags[5], tags[6], tags[7], tags[7], tags[8]);
3345 *blue = (indx % 2 == 0);
3346 *sci_type = type[indx];
3349 check( load_raw_image(*raw_filename,
3356 "Error loading image from file '%s'", *raw_filename);
3358 if (cpl_error_get_code() != CPL_ERROR_NONE)
3360 *raw_filename = NULL;
3361 uves_free_image (raw_image);
3362 uves_free_propertylist(raw_header);
3364 return cpl_error_get_code();
3386 uves_load_standard(
const cpl_frameset *frames,
const char **raw_filename,
3387 cpl_image *raw_image[2],
3391 const char *tags[] = { UVES_STD_STAR(
true), UVES_STD_STAR(
false) };
3392 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3394 bool flames =
false;
3396 check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3397 "Could not identify raw frame (%s or %s) in SOF", tags[0], tags[1]);
3399 *blue = (indx == 0);
3402 check( load_raw_image(*raw_filename,
3409 "Error loading image from file '%s'", *raw_filename);
3412 if (cpl_error_get_code() != CPL_ERROR_NONE)
3414 *raw_filename = NULL;
3415 uves_free_image (raw_image);
3416 uves_free_propertylist(raw_header);
3418 return cpl_error_get_code();
3440 uves_load_drs(
const cpl_frameset *frames,
3442 const char *chip_name,
3443 const char **drs_filename,
3445 enum uves_chip chip)
3447 const char *tags[1];
3448 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3453 tags[0] = UVES_DRS_SETUP(flames, chip);
3454 extension = UVES_DRS_SETUP_EXTENSION(chip);
3456 check( *drs_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3457 "Could not find DRS table (%s) in SOF", tags[0]);
3462 "Could not load header from extension %d of file '%s'", extension, *drs_filename);
3464 check_nomsg( uves_warn_if_chip_names_dont_match(*drs_header, chip_name, chip) );
3467 if (cpl_error_get_code() != CPL_ERROR_NONE) {
3468 *drs_filename = NULL;
3469 uves_free_propertylist(drs_header);
3471 return cpl_error_get_code();
3484 uves_load_weights(
const cpl_frameset *frames,
const char **weights_filename,
3485 enum uves_chip chip)
3487 cpl_image *weights = NULL;
3488 const char *tags[1];
3489 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3493 assure( weights_filename != NULL, CPL_ERROR_NULL_INPUT,
"Null filename");
3495 tags[0] = UVES_WEIGHTS(chip);
3497 check( *weights_filename = uves_find_frame(frames,
3498 tags, number_of_tags, &indx, NULL),
3499 "Could not find '%s' in frame set", tags[0]);
3501 check( weights = cpl_image_load(*weights_filename,
3506 "Could not load master bias from extension %d of file '%s'",
3507 extension, *weights_filename);
3533 uves_load_mbias(
const cpl_frameset *frames,
const char *chip_name,
3534 const char **mbias_filename,
3537 const char *tags[1];
3538 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3543 *mbias_header = NULL;
3545 tags[0] = UVES_MASTER_BIAS (chip);
3546 extension = UVES_MASTER_BIAS_EXTENSION(chip);
3548 check( *mbias_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3549 "Could not find '%s' in frame set", tags[0]);
3552 check( *mbias = cpl_image_load(*mbias_filename,
3557 "Could not load master bias from extension %d of file '%s'",
3558 extension, *mbias_filename);
3563 "Could not load header from extension %d of file '%s'",
3564 extension, *mbias_filename);
3566 check_nomsg( uves_warn_if_chip_names_dont_match(*mbias_header, chip_name, chip) );
3569 if (cpl_error_get_code() != CPL_ERROR_NONE)
3571 *mbias_filename = NULL;
3572 uves_free_image(mbias);
3573 uves_free_propertylist(mbias_header);
3575 return cpl_error_get_code();
3598 uves_load_master_formatcheck(
const cpl_frameset *frames,
const char *chip_name,
3599 const char **mform_filename,
3602 const char *tags[1];
3603 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3608 *mform_header = NULL;
3610 tags[0] = UVES_MASTER_ARC_FORM (chip);
3611 extension = UVES_MASTER_ARC_FORM_EXTENSION(chip);
3613 check( *mform_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3614 "Could not find '%s' in frame set", tags[0]);
3617 check( *mform = cpl_image_load(*mform_filename,
3622 "Could not load master formatcheck from extension %d of file '%s'",
3623 extension, *mform_filename);
3629 "Could not load header from extension %d of file '%s'",
3630 extension, *mform_filename);
3632 check_nomsg( uves_warn_if_chip_names_dont_match(*mform_header, chip_name, chip) );
3635 if (cpl_error_get_code() != CPL_ERROR_NONE)
3637 *mform_filename = NULL;
3638 uves_free_image(mform);
3639 uves_free_propertylist(mform_header);
3641 return cpl_error_get_code();
3663 uves_load_mdark(
const cpl_frameset *frames,
const char *chip_name,
3664 const char **mdark_filename, cpl_image **mdark,
3667 const char *tags[2];
3668 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3673 *mdark_header = NULL;
3675 tags[0] = UVES_MASTER_DARK (chip);
3676 tags[1] = UVES_MASTER_PDARK (chip);
3677 extension = UVES_MASTER_DARK_EXTENSION(chip);
3679 check( *mdark_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3680 "Could not find %s or %s in frame set", tags[0], tags[1]);
3683 check( *mdark = cpl_image_load(*mdark_filename,
3688 "Could not load master dark from extension %d of file '%s'",
3689 extension, *mdark_filename);
3694 "Could not load header from extension %d of file '%s'",
3695 extension, *mdark_filename);
3697 check_nomsg( uves_warn_if_chip_names_dont_match(*mdark_header, chip_name, chip) );
3700 if (cpl_error_get_code() != CPL_ERROR_NONE)
3702 *mdark_filename = NULL;
3703 uves_free_image(mdark);
3704 uves_free_propertylist(mdark_header);
3706 return cpl_error_get_code();
3726 uves_load_ref_flat(
const cpl_frameset *frames,
const char *chip_name,
3727 const char **filename, cpl_image **rflat,
3730 const char *tags[1];
3731 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3736 *rflat_header = NULL;
3738 tags[0] = UVES_REF_TFLAT(chip);
3739 extension = UVES_MASTER_FLAT_EXTENSION(chip);
3741 check( *filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3742 "Could not find %s in frame set", tags[0]);
3744 check( *rflat = cpl_image_load(*filename,
3749 "Could not load reference dark from extension %d of file '%s'",
3750 extension, *filename);
3754 "Could not load header from extension %d of file '%s'",
3755 extension, *filename);
3757 check_nomsg( uves_warn_if_chip_names_dont_match(*rflat_header, chip_name, chip) );
3760 if (cpl_error_get_code() != CPL_ERROR_NONE)
3763 uves_free_image(rflat);
3764 uves_free_propertylist(rflat_header);
3789 uves_load_mflat_const(
const cpl_frameset *frames,
const char *chip_name,
3790 const char **mflat_filename,
3792 enum uves_chip chip,
3793 const cpl_frame **mflat_frame)
3795 const char *tags[6];
3796 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3801 *mflat_header = NULL;
3803 tags[0] = UVES_REF_TFLAT (chip);
3804 tags[1] = UVES_MASTER_FLAT (chip);
3805 tags[2] = UVES_MASTER_DFLAT (chip);
3806 tags[3] = UVES_MASTER_IFLAT (chip);
3807 tags[4] = UVES_MASTER_TFLAT (chip);
3808 tags[5] = UVES_MASTER_SCREEN_FLAT (chip);
3809 extension = UVES_MASTER_FLAT_EXTENSION(chip);
3811 check( *mflat_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
3813 "Could not find '%s', '%s', '%s', '%s' or '%s' in frame set",
3814 tags[0], tags[1], tags[2], tags[3], tags[4]);
3817 check( *mflat = cpl_image_load(*mflat_filename,
3822 "Could not load master flat from extension %d of file '%s'",
3823 extension, *mflat_filename);
3828 "Could not load header from extension %d of file '%s'",
3829 extension, *mflat_filename);
3831 check_nomsg( uves_warn_if_chip_names_dont_match(*mflat_header, chip_name, chip) );
3834 if (cpl_error_get_code() != CPL_ERROR_NONE)
3836 *mflat_filename = NULL;
3837 uves_free_image(mflat);
3838 uves_free_propertylist(mflat_header);
3840 return cpl_error_get_code();
3860 uves_load_mflat(cpl_frameset *frames,
const char *chip_name,
3861 const char **mflat_filename,
3863 cpl_frame **mflat_frame)
3865 return uves_load_mflat_const((
const cpl_frameset *)frames,
3868 mflat, mflat_header, chip,
3869 (
const cpl_frame **) mflat_frame);
3906 uves_load_ordertable(
const cpl_frameset *frames,
3908 const char *chip_name,
3909 const char **ordertable_filename,
3910 cpl_table **ordertable,
3915 int *tab_in_out_oshift,
3916 double *tab_in_out_yshift,
3919 enum uves_chip chip,
3924 const char *tags[1];
3925 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
3926 bool format_is_midas;
3928 double *tioy = NULL;
3931 double *fibre_pos = NULL;
3932 int *fibre_mask = NULL;
3936 tags[0] = UVES_GUESS_ORDER_TABLE(flames, chip);
3940 tags[0] = UVES_ORDER_TABLE(flames, chip);
3943 check( *ordertable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
3944 "No order table (%s) found in SOF", tags[0]);
3946 check( *ordertable = cpl_table_load(*ordertable_filename,
3947 UVES_ORDER_TABLE_EXTENSION,
3950 "Error loading order table from extension %d of file '%s'",
3951 UVES_ORDER_TABLE_EXTENSION, *ordertable_filename);
3953 assure(ordertable_header != NULL,CPL_ERROR_NULL_INPUT,
3954 "NULL primary header uves_propertylist variable header");
3956 "Could not load header from extension 0 of '%s'", *ordertable_filename);
3958 if(ordertable_xheader != NULL) {
3961 "Could not load header from extension 1 of '%s'", *ordertable_filename);
3966 check_nomsg( uves_warn_if_chip_names_dont_match(*ordertable_header, chip_name, chip) );
3968 check(uves_check_if_format_is_midas(*ordertable_header,&format_is_midas),
3969 "Error getting FITS format");
3972 if (!format_is_midas && !flames)
3983 if (cpl_table_has_column(*ordertable,
"ORDER"))
3985 cpl_table_name_column(*ordertable,
"ORDER",
"Order");
3987 if (cpl_table_has_column(*ordertable,
"YFIT"))
3989 cpl_table_name_column(*ordertable,
"YFIT",
"Yfit");
3992 if (order_locations != NULL)
3994 check( *order_locations =
3995 load_polynomial(*ordertable_filename, UVES_ORDER_TABLE_EXTENSION_POLY),
3996 "Could not read polynomial from extension %d of file '%s'",
3997 UVES_ORDER_TABLE_EXTENSION_POLY, *ordertable_filename);
4002 check( *traces = cpl_table_load(*ordertable_filename,
4003 UVES_ORDER_TABLE_EXTENSION_FIBRE,
4006 "Error loading fibre table from extension %d of file '%s'",
4007 UVES_ORDER_TABLE_EXTENSION_FIBRE, *ordertable_filename);
4014 check(( cpl_table_cast_column (*ordertable,
"ORDER",
"Order", CPL_TYPE_INT),
4015 cpl_table_erase_column(*ordertable,
"ORDER")),
4016 "Error casting and renaming column 'ORDER'");
4018 check( cpl_table_name_column(*ordertable,
"YFIT",
"Yfit"),
4019 "Error renaming column 'YFIT'");
4025 "Could not load header from extension 1 of '%s'",
4026 *ordertable_filename);
4030 "Could not load header from extension 0 of '%s'",
4031 *ordertable_filename);
4036 if (order_locations != NULL)
4038 check( *order_locations =
4039 uves_polynomial_convert_from_plist_midas(midas_header,
"COEFF",-1),
4040 "Error reading polynomial from %s", *ordertable_filename);
4044 if (flames && tab_in_out_oshift != NULL )
4050 check( tioo = uves_read_midas_array(
4051 midas_header,
"TAB_IN_OUT_OSHIFT", &tioo_length,
4053 "Error reading TAB_IN_OUT_OSHIFT from MIDAS header");
4055 assure( tioo_type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4056 "Type of TAB_IN_OUT_OSHIFT is %s, double expected",
4059 if (tioo_length != 1)
4062 "%d expected", tioo_length, 1);
4065 *tab_in_out_oshift = tioo[0];
4071 if (flames && tab_in_out_yshift != NULL)
4077 check( tioy = uves_read_midas_array(
4078 midas_header,
"TAB_IN_OUT_YSHIFT", &tioy_length,
4080 "Error reading TAB_IN_OUT_YSHIFT from MIDAS header");
4082 assure( tioy_type == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
4083 "Type of TAB_IN_OUT_YSHIFT is %s, double expected",
4086 if (tioy_length != 1)
4089 "%d expected", tioy_length, 1);
4092 *tab_in_out_yshift = tioy[0];
4105 double fibre_offset = 0.0;
4116 int fibre_pos_length;
4117 int fibre_mask_length;
4118 cpl_type fibre_pos_type;
4119 cpl_type fibre_mask_type;
4122 check( fibre_pos = uves_read_midas_array(
4123 midas_header,
"FIBREPOS", &fibre_pos_length,
4124 &fibre_pos_type, NULL),
4125 "Error reading FIBREPOS from MIDAS header");
4127 assure( fibre_pos_type == CPL_TYPE_DOUBLE, CPL_ERROR_TYPE_MISMATCH,
4128 "Type of FIBREPOS is %s, double expected",
4131 check( fibre_mask = uves_read_midas_array(
4132 midas_header,
"FIBREMASK", &fibre_mask_length,
4133 &fibre_mask_type, NULL),
4134 "Error reading FIBREMASK from MIDAS header");
4136 assure( fibre_mask_type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4137 "Type of FIBREMASK is %s, double expected",
4140 assure( fibre_pos_length == fibre_mask_length,
4141 CPL_ERROR_INCOMPATIBLE_INPUT,
4142 "FIBREMASK has length %d, but "
4143 "FIBREPOS has length %d",
4144 fibre_mask_length, fibre_pos_length );
4146 *fib_pos= cpl_malloc(
sizeof(
double) * fibre_pos_length);
4147 *fib_msk= cpl_malloc(
sizeof(
int) * fibre_mask_length);
4149 for (fibre_ID = 0; fibre_ID < fibre_mask_length; fibre_ID++)
4152 fibre_ID, fibre_pos[fibre_ID],
4153 fibre_mask[fibre_ID] ?
4154 "enabled" :
"disabled");
4157 fibre_pos[fibre_ID],
4158 fibre_mask[fibre_ID]);
4159 (*fib_pos)[fibre_ID]=fibre_pos[fibre_ID];
4160 (*fib_msk)[fibre_ID]=fibre_mask[fibre_ID];
4167 uves_free_propertylist(&midas_header);
4168 uves_free_double(&fibre_pos);
4169 uves_free_int(&fibre_mask);
4170 uves_free_int(&tioo);
4171 uves_free_double(&tioy);
4172 uves_free_propertylist(&prime_header);
4174 if (cpl_error_get_code() != CPL_ERROR_NONE)
4176 *ordertable_filename = NULL;
4177 uves_free_table (ordertable);
4178 uves_free_propertylist(ordertable_header);
4180 if (traces != NULL) uves_free_table (traces);
4182 return cpl_error_get_code();
4207 const char* drs_id=NULL;
4210 if (strstr(drs_id,
"CPL") != NULL ||
4211 strstr(drs_id,
"cpl") != NULL) {
4212 *format_is_midas =
false;
4214 }
else if (strstr(drs_id,
"MIDAS") != NULL ||
4215 strstr(drs_id,
"midas") != NULL) {
4216 *format_is_midas =
true;
4219 assure (
false, CPL_ERROR_ILLEGAL_INPUT,
4220 "Unrecognized order table format, DRS_ID = '%s'", drs_id);
4224 *format_is_midas =
true;
4225 uves_msg_debug(
"No '%s' keyword found. Assuming MIDAS format", UVES_DRS_ID);
4229 return cpl_error_get_code();
4245 static cpl_error_code
4246 create_column_pixelsize(cpl_table *linetable)
4249 cpl_table *t = NULL;
4255 check( t = uves_extract_table_rows(linetable,
"Ident", CPL_GREATER_THAN, 0.1),
4256 "Error deleting rows with Ident=0");
4259 check(( cpl_table_duplicate_column(t,
"Aux", t,
"Ident"),
4260 cpl_table_multiply_columns(t,
"Aux",
"Order")),
4261 "Error creating 'Aux' column");
4269 "Regression failed");
4272 "Error reading polynomial coefficient");
4275 "Error reading polynomial coefficient");
4277 cpl_table_new_column(linetable, LINETAB_PIXELSIZE, CPL_TYPE_DOUBLE);
4279 for (i = 0; i < cpl_table_get_nrow(linetable); i++)
4286 check(( x = cpl_table_get_double(linetable,
"X", i, NULL),
4287 order = cpl_table_get_int (linetable,
"Order", i, NULL),
4288 ident = cpl_table_get_double(linetable,
"Ident", i, NULL)),
4289 "Error reading line table");
4291 assure( order != 0, CPL_ERROR_ILLEGAL_INPUT,
"Illegal order number: %d", order);
4299 pixelsize = (d1 + 2*d2* x) / order;
4304 cpl_table_set_double(linetable, LINETAB_PIXELSIZE, i, pixelsize);
4308 cpl_table_set_invalid(linetable, LINETAB_PIXELSIZE, i);
4313 uves_free_table(&t);
4315 return cpl_error_get_code();
4349 align_order_line_table(cpl_table *linetable,
const polynomial *absolute_order,
4351 const polynomial *order_locations,
int minorder,
int maxorder)
4355 assure ( order_locations != NULL, CPL_ERROR_NULL_INPUT,
4356 "Null order locations polynomial!");
4358 assure ( absolute_order != NULL, CPL_ERROR_NULL_INPUT,
4359 "Null absolute order pllynomial!");
4360 assure( cpl_table_has_column(linetable,
"X" ), CPL_ERROR_DATA_NOT_FOUND,
4361 "Missing line table column 'X'");
4362 assure( cpl_table_has_column(linetable,
"Ynew"), CPL_ERROR_DATA_NOT_FOUND,
4363 "Missing line table column 'Ynew'");
4364 assure( cpl_table_has_column(linetable,
"Order"), CPL_ERROR_DATA_NOT_FOUND,
4365 "Missing line table column 'Order'");
4367 assure( cpl_table_get_column_type(linetable,
"X") == CPL_TYPE_DOUBLE,
4368 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'X' has type %s (double expected))",
4371 assure( cpl_table_get_column_type(linetable,
"Ynew") == CPL_TYPE_DOUBLE,
4372 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'Ynew' has type %s (double expected))",
4375 assure( cpl_table_get_column_type(linetable,
"Y") == CPL_TYPE_INT,
4376 CPL_ERROR_TYPE_MISMATCH,
"Line table column 'Y' has type %s (integer expected))",
4380 if (linetable_header != NULL)
4383 int line_first, line_last;
4384 int ord_first, ord_last;
4389 int x, y, order, absorder;
4393 maxx = uves_round_double(cpl_table_get_column_max(linetable,
"X"));
4395 minx = uves_round_double(cpl_table_get_column_min(linetable,
"X"));
4397 assure( 1 <= minx && minx <= maxx, CPL_ERROR_ILLEGAL_INPUT,
4398 "Illegal min/max line x positions: %d/%d, must be > 1",
4402 x = (minx + maxx) / 2;
4403 order = (minorder + maxorder) / 2;
4413 assure ( order_locations != NULL, CPL_ERROR_NULL_INPUT,
4414 "Null order locations polynomial!");
4421 "rounding to %d", x, y,
4424 ord_first = absorder + (minorder - order) * coeff;
4425 ord_last = absorder + (maxorder - order) * coeff;
4430 "Could not read order number from line table header");
4434 "Could not read order number from line table header");
4436 uves_msg_debug(
"Order table range: %d - %d. Line table range: %d - %d",
4437 ord_first, ord_last, line_first, line_last);
4439 if (line_first != ord_first ||
4440 line_last != ord_last)
4443 "Line table contains orders %d - %d. "
4444 "Order table contains orders %d - %d. "
4445 "Correcting on the fly",
4446 line_first, line_last, ord_first, ord_last);
4450 "Could not write corrected first absolute order number");
4453 "Could not write corrected first absolute order number");
4456 ord_first, ord_last);
4465 double epsilon = 0.01;
4470 if (fabs(cpl_table_get_column_median(linetable,
"Y") -
4471 cpl_table_get_column_median(linetable,
"Order")) > epsilon)
4483 cpl_table_erase_column(linetable,
"Y");
4533 uves_load_linetable(
const cpl_frameset *frames,
4535 const char *chip_name,
4536 const polynomial *order_locations,
int minorder,
int maxorder,
4537 const char **linetable_filename,
4538 cpl_table **linetable,
4542 enum uves_chip chip,
int trace_id,
int window)
4547 int *absorders = NULL;
4548 cpl_table *temp = NULL;
4550 const char *tags[3];
4551 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
4553 bool format_is_midas;
4560 tags[0] = UVES_GUESS_LINE_TABLE(flames, chip);
4561 tags[1] = UVES_LINE_TABLE(flames, chip);
4562 tags[2] = UVES_LINE_TABLE(flames, chip);
4565 check( *linetable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
4566 "No line table (%s, %s or %s) found in SOF", tags[0], tags[1], tags[2]);
4570 tags[0] = UVES_LINE_TABLE(flames, chip);
4571 tags[1] = UVES_LINE_TABLE(flames, chip);
4572 tags[2] = UVES_GUESS_LINE_TABLE(flames, chip);
4576 if (cpl_frameset_find_const(frames, tags[0]) == NULL &&
4577 cpl_frameset_find_const(frames, tags[1]) == NULL &&
4578 cpl_frameset_find_const(frames, tags[2]) == NULL)
4586 tags[0] = UVES_LINE_TABLE_MIDAS(chip, window);
4587 tags[1] = UVES_LINE_TABLE_MIDAS(chip, window);
4588 tags[2] = UVES_LINE_TABLE_MIDAS(chip, window);
4595 tags[0] = UVES_LINE_TABLE_MIDAS(chip, 1);
4596 tags[1] = UVES_LINE_TABLE_MIDAS(chip, 2);
4597 tags[2] = UVES_LINE_TABLE_MIDAS(chip, 3);
4599 uves_msg_debug(
"Trying %s, %s or %s", tags[0], tags[1], tags[2]);
4603 check( *linetable_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
4604 "No line table (%s, %s or %s) found in SOF", tags[0], tags[1], tags[2]);
4609 "Could not load primary header of '%s'", *linetable_filename);
4611 check_nomsg( uves_warn_if_chip_names_dont_match(primary_header, chip_name, chip) );
4617 if (strstr(drs_id,
"CPL") != NULL || strstr(drs_id,
"cpl") != NULL)
4619 format_is_midas =
false;
4622 else if (strstr(drs_id,
"MIDAS") != NULL || strstr(drs_id,
"midas") != NULL)
4624 format_is_midas =
true;
4630 CPL_ERROR_ILLEGAL_INPUT,
4631 "Unrecognized line table format, DRS_ID = '%s'", drs_id);
4636 format_is_midas =
true;
4637 uves_msg_debug(
"No '%s' keyword found. Assuming MIDAS format", UVES_DRS_ID);
4640 if (format_is_midas || flames)
4644 assure( trace_id == 0 && (window == -1 || (1 <= window && window <= 3)),
4645 CPL_ERROR_UNSUPPORTED_MODE,
4646 "Cannot read (fibre, window) = (%d, %d) from MIDAS line table",
4656 assure( ((1<= trace_id && trace_id <= 9) && (window == -1)),
4657 CPL_ERROR_UNSUPPORTED_MODE,
4658 "Cannot read (fibre, window) = (%d, %d) from MIDAS line table",
4677 check( nextensions = uves_get_nextensions(*linetable_filename),
4678 "Error reading number of extensions of file '%s'", *linetable_filename);
4684 for (base_extension = 1; base_extension < nextensions && !found; base_extension++)
4690 check(( uves_free_propertylist(&header),
4692 "Could not header of extension %d of '%s'",
4693 base_extension, *linetable_filename);
4696 "Error reading trace ID from header of extension %d of '%s'",
4697 base_extension, *linetable_filename);
4700 "Error reading window number from header of extension %d of '%s'",
4701 base_extension, *linetable_filename);
4703 uves_msg_debug(
"Found (trace, window) = (%d, %d), need (%d, %d)",
4704 header_trace, header_window,
4707 found = ( (trace_id == header_trace) &&
4708 (window == -1 || window == header_window) );
4712 CPL_ERROR_ILLEGAL_INPUT,
4713 "Line table (trace, window) = (%d, %d) is not present in file '%s'",
4714 trace_id, window, *linetable_filename);
4718 base_extension -= 2;
4722 check( *linetable = cpl_table_load(*linetable_filename,
4723 base_extension + UVES_LINE_TABLE_EXTENSION,
4726 "Error loading line table from extension %d of file '%s'",
4727 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4730 if (linetable_header != NULL)
4732 check( *linetable_header =
4734 base_extension + UVES_LINE_TABLE_EXTENSION),
4735 "Could not load header of extension %d of '%s'",
4736 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4738 if (format_is_midas)
4742 absorders = uves_read_midas_array(*linetable_header,
"ORDER", &size,
4745 assure( type == CPL_TYPE_INT, CPL_ERROR_TYPE_MISMATCH,
4746 "Type of ORDER is %s, int expected",
4750 CPL_ERROR_ILLEGAL_INPUT,
4751 "'ORDER' array has size %d. Size 2 expected.", size);
4754 "Error updating table header");
4759 if (format_is_midas)
4762 check(( cpl_table_cast_column(*linetable,
"X",
"xxxx", CPL_TYPE_DOUBLE),
4763 cpl_table_erase_column(*linetable,
"X"),
4764 cpl_table_name_column(*linetable,
"xxxx",
"X")),
4765 "Error casting and renaming column 'X'");
4767 check(( cpl_table_cast_column(*linetable,
"YNEW",
"xxxx", CPL_TYPE_DOUBLE),
4768 cpl_table_erase_column(*linetable,
"YNEW"),
4769 cpl_table_name_column(*linetable,
"xxxx",
"Ynew")),
4770 "Error casting and renaming column 'YNEW'");
4772 check(( cpl_table_cast_column(*linetable,
"Y",
"xxxx", CPL_TYPE_INT),
4773 cpl_table_erase_column(*linetable,
"Y"),
4774 cpl_table_name_column(*linetable,
"xxxx",
"Y")),
4775 "Error casting and renaming column 'Y'");
4777 check(( cpl_table_cast_column(*linetable,
"ORDER",
"Order", CPL_TYPE_INT),
4778 cpl_table_erase_column(*linetable,
"ORDER")),
4779 "Error casting and renaming column 'ORDER'");
4781 check( cpl_table_name_column(*linetable,
"IDENT",
"Ident"),
4782 "Error renaming column 'IDENT'");
4785 *linetable_filename,
4786 base_extension + UVES_LINE_TABLE_EXTENSION),
4787 "Could not load header of extension %d of '%s'",
4788 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4790 if (dispersion_relation != NULL) {
4792 check( *dispersion_relation =
4793 uves_polynomial_convert_from_plist_midas(midas_header,
4795 "Error reading polynomial 'REGR%d' from '%s'",
4797 *linetable_filename);
4800 check( *dispersion_relation =
4801 uves_polynomial_convert_from_plist_midas(midas_header,
4803 "Error reading polynomial 'REGR' from '%s'",
4804 *linetable_filename);
4809 check( absolute_order_local =
4810 uves_polynomial_convert_from_plist_midas(midas_header,
"RORD",-1),
4811 "Error reading polynomial 'RORD' from '%s'", *linetable_filename);
4826 if (cpl_table_has_column(*linetable,
"YNEW"))
4828 cpl_table_name_column(*linetable,
"YNEW",
"Ynew");
4831 if (dispersion_relation != NULL)
4833 check( *dispersion_relation = load_polynomial(
4834 *linetable_filename,
4835 base_extension + UVES_LINE_TABLE_EXTENSION_DISPERSION),
4836 "Could not read polynomial from extension %d of file '%s'",
4837 base_extension + UVES_LINE_TABLE_EXTENSION_DISPERSION,
4838 *linetable_filename);
4841 check( absolute_order_local =
4842 load_polynomial(*linetable_filename,
4843 base_extension + UVES_LINE_TABLE_EXTENSION_ABSORDER),
4844 "Could not read polynomial from extension %d of file '%s'",
4845 base_extension + UVES_LINE_TABLE_EXTENSION_ABSORDER, *linetable_filename);
4848 if (absolute_order != NULL)
4854 check( align_order_line_table(
4855 *linetable, absolute_order_local, linetable_header,
4856 order_locations, minorder, maxorder),
4857 "Error while aligning line/order tables");
4862 const char *colname;
4870 uves_free_table(&temp);
4871 check(( temp = cpl_table_new(0),
4872 cpl_table_copy_structure(temp, *linetable)),
4873 "Error duplicating line table column structure");
4875 colname = cpl_table_get_column_name(temp);
4876 while (colname != NULL)
4878 if (!(strcmp(colname,
"X" ) == 0 ||
4879 strcmp(colname,
"Order" ) == 0 ||
4880 strcmp(colname,
"Ident" ) == 0 ||
4881 strcmp(colname,
"FIBRE" ) == 0 ||
4882 strcmp(colname,
"Fibre" ) == 0 ||
4883 strcmp(colname, LINETAB_PIXELSIZE) == 0))
4885 cpl_table_erase_column(*linetable, colname);
4890 colname = cpl_table_get_column_name(NULL);
4897 if ( !cpl_table_has_column(*linetable, LINETAB_PIXELSIZE) )
4899 check( create_column_pixelsize(*linetable),
4900 "Error adding 'Pixelsize' column");
4904 check( uves_erase_invalid_table_rows(*linetable,
"Ident"),
4905 "Error deleting rows with illegal 'Ident' value");
4907 check( uves_erase_table_rows(*linetable,
"Ident", CPL_LESS_THAN, 0.01),
4908 "Error deleting rows with illegal 'Ident' value");
4911 assure( uves_erase_invalid_table_rows(*linetable, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
4912 "After deleting rows with invalid 'Ident' values, "
4913 "the table in extension %d of file '%s' still contains invalid rows",
4914 base_extension + UVES_LINE_TABLE_EXTENSION, *linetable_filename);
4917 check( uves_sort_table_2(*linetable,
"Order",
"X",
false,
false),
"Error sorting line table");
4920 uves_free_propertylist(&primary_header);
4921 uves_free_propertylist(&header);
4922 uves_free_propertylist(&midas_header);
4923 uves_free_table(&temp);
4925 cpl_free(absorders);
4926 if (cpl_error_get_code() != CPL_ERROR_NONE) {
4927 *linetable_filename = NULL;
4928 uves_free_table(linetable);
4941 uves_load_linetable_const(
const cpl_frameset *frames,
4943 const char *chip_name,
4944 const polynomial *order_locations,
int minorder,
int maxorder,
4945 const char **linetable_filename,
4946 const cpl_table **linetable,
4950 enum uves_chip chip,
int trace_id,
int window)
4952 uves_load_linetable(frames, flames, chip_name, order_locations,
4955 (cpl_table **)linetable,
4959 chip, trace_id, window);
4982 uves_load_response_curve(
const cpl_frameset *frames,
const char *chip_name,
4983 const char **response_filename,
4984 cpl_image **response_curve,
4985 cpl_table **master_response,
4988 const char *tags[2];
4989 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
4993 *response_curve = NULL;
4994 *response_header = NULL;
4995 *master_response = NULL;
4997 tags[0] = UVES_INSTR_RESPONSE (chip);
4998 tags[1] = UVES_MASTER_RESPONSE(chip);
5000 check( *response_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
5002 "Could not find '%s' in frame set", tags[0]);
5007 extension = UVES_INSTR_RESPONSE_EXTENSION(chip);
5016 check( *response_curve = uves_load_image_file(*response_filename,
5023 "Could not load response curve from extension %d of file '%s'",
5024 extension, *response_filename);
5033 check_nomsg( uves_warn_if_chip_names_dont_match(*response_header, chip_name, chip) );
5038 extension = UVES_MASTER_RESPONSE_EXTENSION(chip);
5040 check( *master_response = cpl_table_load(*response_filename,
5041 UVES_LINE_INTMON_TABLE_EXTENSION,
5044 "Error master response curve from extension %d of file '%s'",
5045 extension, *response_filename);
5048 check(( cpl_table_cast_column(*master_response,
"LAMBDA",
"LAMBDA_double",
5050 cpl_table_erase_column(*master_response,
"LAMBDA"),
5051 cpl_table_name_column(*master_response,
"LAMBDA_double",
"LAMBDA")),
5052 "Could not cast column 'LAMBDA'");
5054 check(( cpl_table_cast_column(*master_response,
"FLUX_CONV",
"FLUX_CONV_double",
5056 cpl_table_erase_column(*master_response,
"FLUX_CONV"),
5057 cpl_table_name_column(*master_response,
"FLUX_CONV_double",
"FLUX_CONV")),
5058 "Could not cast column 'FLUX_CONV'");
5065 if (cpl_error_get_code() != CPL_ERROR_NONE)
5067 *response_filename = NULL;
5068 uves_free_image(response_curve);
5069 uves_free_propertylist(response_header);
5071 return cpl_error_get_code();
5086 cpl_error_code uves_load_lineintmon(
const cpl_frameset *frames,
5087 const char **line_intmon_filename,
5088 cpl_table **line_intmon)
5090 const char *tags[1] = {UVES_LINE_INTMON_TABLE};
5092 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5096 check( *line_intmon_filename = uves_find_frame(frames, tags, number_of_tags,
5098 "No line intensity table (%s) found in SOF", tags[0]);
5101 check( *line_intmon = cpl_table_load(*line_intmon_filename,
5102 UVES_LINE_INTMON_TABLE_EXTENSION,
5105 "Error loading line reference table from extension %d of file '%s'",
5106 UVES_LINE_INTMON_TABLE_EXTENSION, *line_intmon_filename);
5108 check(( cpl_table_cast_column(*line_intmon,
"WAVE",
"Wave", CPL_TYPE_DOUBLE),
5109 cpl_table_erase_column(*line_intmon,
"WAVE")),
5110 "Could not cast and rename column");
5113 check( uves_sort_table_1(*line_intmon,
"Wave",
false),
"Error sorting table");
5116 if (cpl_error_get_code() != CPL_ERROR_NONE)
5118 *line_intmon_filename = NULL;
5119 uves_free_table(line_intmon);
5121 return cpl_error_get_code();
5139 uves_load_corvel(
const cpl_frameset *frames,
5142 const char **corvel_filename)
5144 const char *tags[1];
5145 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5149 tags[0] = FLAMES_CORVEL_MASK;
5152 assure_nomsg( corvel_filename != NULL, CPL_ERROR_NULL_INPUT );
5155 check( *corvel_filename = uves_find_frame(frames, tags, number_of_tags,
5157 "No velocity correction table (%s) found in SOF", tags[0]);
5161 check( *corvel = cpl_table_load(*corvel_filename,
5165 "Error loading line reference table from extension %d of file '%s'",
5166 extension, *corvel_filename);
5169 if (corvel_header != NULL)
5174 "Could not load header from extension %d of file %s",
5175 extension, *corvel_filename);
5180 if (cpl_error_get_code() != CPL_ERROR_NONE)
5182 *corvel_filename = NULL;
5183 uves_free_table(corvel);
5206 uves_load_linerefertable(
const cpl_frameset *frames,
5207 const char **line_refer_filename,
5210 const char *tags[1] = {UVES_LINE_REFER_TABLE};
5212 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5216 check( *line_refer_filename = uves_find_frame(frames, tags, number_of_tags,
5218 "No line reference table (%s) found in SOF", tags[0]);
5221 check( *line_refer = cpl_table_load(*line_refer_filename,
5222 UVES_LINE_REFER_TABLE_EXTENSION,
5225 "Error loading line reference table from extension %d of file '%s'",
5226 UVES_LINE_REFER_TABLE_EXTENSION, *line_refer_filename);
5229 if (line_refer_header != NULL)
5232 "Could not load header of line_refer table in '%s'", *line_refer_filename);
5235 assure( uves_erase_invalid_table_rows(*line_refer, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
5236 "Table in extension %d of file '%s' contains invalid rows",
5237 UVES_LINE_REFER_TABLE_EXTENSION, *line_refer_filename);
5239 check(( cpl_table_cast_column(*line_refer,
"WAVE",
"Wave", CPL_TYPE_DOUBLE),
5240 cpl_table_erase_column(*line_refer,
"WAVE")),
5241 "Could not cast and rename column");
5254 check(( cpl_table_duplicate_column(*line_refer,
"dWave", *line_refer,
"Wave"),
5255 cpl_table_divide_scalar (*line_refer,
"dWave", 300000*10)),
5256 "Error writing wavelength uncertainties");
5259 check(( cpl_table_new_column(*line_refer,
"dWave", CPL_TYPE_DOUBLE),
5260 cpl_table_fill_column_window(*line_refer,
5263 cpl_table_get_nrow(*line_refer), 0.002)),
5264 "Error writing wavelength uncertainties");
5268 check( uves_sort_table_1(*line_refer,
"Wave",
false),
"Error sorting table");
5271 if (cpl_error_get_code() != CPL_ERROR_NONE) {
5272 *line_refer_filename = NULL;
5273 uves_free_table (line_refer);
5274 if (line_refer_header != NULL) uves_free_propertylist(line_refer_header);
5276 return cpl_error_get_code();
5295 uves_load_flux_table(
const cpl_frameset *frames,
const char **flux_table_filename,
5296 cpl_table **flux_table)
5298 const char *tags[1] = {UVES_FLUX_STD_TABLE};
5300 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5304 check( *flux_table_filename = uves_find_frame(frames, tags, number_of_tags,
5306 "No standard star flux table (%s) in SOF", tags[0]);
5309 check( *flux_table = cpl_table_load(*flux_table_filename,
5310 UVES_FLUX_STD_TABLE_EXTENSION,
5313 "Error loading flux table from extension %d of file '%s'",
5314 UVES_FLUX_STD_TABLE_EXTENSION, *flux_table_filename);
5321 if (uves_erase_invalid_table_rows(*flux_table, NULL) != 0)
5323 uves_msg_warning(
"Table in extension %d of file '%s' contains null values",
5324 UVES_FLUX_STD_TABLE_EXTENSION, *flux_table_filename);
5330 for (i = 0; i < cpl_table_get_nrow(*flux_table); i++)
5332 if (cpl_table_get_string(*flux_table,
"TYPE", i) == NULL)
5334 cpl_table_set_string(*flux_table,
"TYPE", i,
"NULL");
5341 if (cpl_error_get_code() != CPL_ERROR_NONE)
5343 *flux_table_filename = NULL;
5344 uves_free_table(flux_table);
5346 return cpl_error_get_code();
5366 uves_load_atmo_ext(
const cpl_frameset *frames,
const char **atmext_table_filename,
5367 cpl_table **atmext_table)
5369 const char *tags[1] = {UVES_EXTCOEFF_TABLE};
5371 int number_of_tags =
sizeof(tags) /
sizeof(
char *);
5375 check( *atmext_table_filename = uves_find_frame(frames, tags, number_of_tags,
5377 "No atmospheric extinction table (%s) found in SOF", tags[0]);
5380 check( *atmext_table = cpl_table_load(*atmext_table_filename,
5381 UVES_EXTCOEFF_TABLE_EXTENSION,
5384 "Error loading atmospheric extinction table from extension %d of file '%s'",
5385 UVES_EXTCOEFF_TABLE_EXTENSION, *atmext_table_filename);
5387 assure( uves_erase_invalid_table_rows(*atmext_table, NULL) == 0, CPL_ERROR_ILLEGAL_INPUT,
5388 "Table in extension %d of file '%s' contains invalid rows",
5389 UVES_EXTCOEFF_TABLE_EXTENSION, *atmext_table_filename);
5391 check( uves_sort_table_1(*atmext_table,
"LAMBDA",
false),
5392 "Error sorting table");
5395 check(( cpl_table_cast_column(*atmext_table,
"LAMBDA",
"LAMBDA_double", CPL_TYPE_DOUBLE),
5396 cpl_table_erase_column(*atmext_table,
"LAMBDA"),
5397 cpl_table_name_column(*atmext_table,
"LAMBDA_double",
"LAMBDA")),
5398 "Could not cast column 'LAMBDA'");
5400 check(( cpl_table_cast_column(*atmext_table,
"LA_SILLA",
"LA_SILLA_double", CPL_TYPE_DOUBLE),
5401 cpl_table_erase_column(*atmext_table,
"LA_SILLA"),
5402 cpl_table_name_column(*atmext_table,
"LA_SILLA_double",
"LA_SILLA")),
5403 "Could not cast column 'LA_SILLA'");
5406 if (cpl_error_get_code() != CPL_ERROR_NONE)
5408 *atmext_table_filename = NULL;
5409 uves_free_table(atmext_table);
5411 return cpl_error_get_code();
5423 uves_guess_order_table_filename(
enum uves_chip chip)
5425 return uves_local_filename(
"orderguesstable", chip, -1, -1);
5438 uves_order_table_filename(
enum uves_chip chip)
5440 return uves_local_filename(
"ordertable", chip, -1, -1);
5451 char *uves_ordef_filename(
enum uves_chip chip)
5453 return uves_local_filename(
"order_def", chip, -1, -1);
5466 uves_masterdark_filename(
enum uves_chip chip)
5468 return uves_local_filename(
"masterdark", chip, -1, -1);
5480 uves_flat_ratio_filename(
enum uves_chip chip)
5482 return uves_local_filename(
"ratio", chip, -1, -1);
5493 char *uves_cd_align_filename(
enum uves_chip chip)
5495 return uves_local_filename(
"cd_align", chip, -1, -1);
5508 uves_masterflat_filename(
enum uves_chip chip)
5510 return uves_local_filename(
"masterflat", chip, -1, -1);
5522 uves_masterflat_bkg_filename(
enum uves_chip chip)
5524 return uves_local_filename(
"masterflat_bkg", chip, -1, -1);
5537 uves_masterbias_filename(
enum uves_chip chip)
5539 return uves_local_filename(
"masterbias", chip, -1, -1);
5552 uves_guess_line_table_filename(
enum uves_chip chip)
5554 return uves_local_filename(
"lineguesstable", chip, -1, -1);
5566 uves_line_table_filename(
enum uves_chip chip)
5568 return uves_local_filename(
"linetable", chip, -1, -1);
5581 uves_line_table_filename_paf(
enum uves_chip chip)
5583 return uves_local_filename(
"linetable_paf", chip, -1, -1);
5596 uves_response_curve_filename(
enum uves_chip chip)
5598 return uves_local_filename(
"response", chip, -1, -1);
5611 uves_response_curve_2d_filename(
enum uves_chip chip)
5613 return uves_local_filename(
"response_2d", chip, -1, -1);
5626 uves_response_red_standard_filename(
enum uves_chip chip)
5628 return uves_local_filename(
"red_std", chip, -1, -1);
5642 uves_response_red_noappend_standard_filename(
enum uves_chip chip)
5644 return uves_local_filename(
"red_nonmerged", chip, -1, -1);
5657 uves_response_bkg_standard_filename(
enum uves_chip chip)
5659 return uves_local_filename(
"bkg_std", chip, -1, -1);
5673 uves_order_extract_qc_standard_filename(
enum uves_chip chip)
5675 return uves_local_filename(
"order_extract_qc", chip, -1, -1);
5688 uves_response_efficiency_filename(
enum uves_chip chip)
5690 return uves_local_filename(
"efficiency", chip, -1, -1);
5704 uves_scired_red_2d_science_filename(
enum uves_chip chip)
5706 return uves_local_filename(
"red_2d_science", chip, -1, -1);
5721 uves_scired_red_science_filename(
enum uves_chip chip)
5723 return uves_local_filename(
"red_science", chip, -1, -1);
5737 uves_scired_red_noappend_science_filename(
enum uves_chip chip)
5739 return uves_local_filename(
"red_nonmerged_science", chip, -1, -1);
5751 uves_scired_red_error_filename(
enum uves_chip chip)
5753 return uves_local_filename(
"error_red_science", chip, -1, -1);
5766 uves_scired_red_noappend_error_filename(
enum uves_chip chip)
5768 return uves_local_filename(
"error_red_nonmerged_science", chip, -1, -1);
5781 uves_scired_red_2d_error_filename(
enum uves_chip chip)
5783 return uves_local_filename(
"error_2d_science", chip, -1, -1);
5797 uves_scired_fluxcal_science_filename(
enum uves_chip chip)
5799 return uves_local_filename(
"fluxcal_science", chip, -1, -1);
5813 uves_scired_fluxcal_science_noappend_filename(
enum uves_chip chip)
5815 return uves_local_filename(
"fluxcal_nonmerged_science", chip, -1, -1);
5827 uves_scired_fluxcal_error_filename(
enum uves_chip chip)
5829 return uves_local_filename(
"fluxcal_error_science", chip, -1, -1);
5843 uves_scired_fluxcal_error_noappend_filename(
enum uves_chip chip)
5845 return uves_local_filename(
"fluxcal_error_nonmerged_science", chip, -1, -1);
5859 uves_scired_fluxcal_science_2d_filename(
enum uves_chip chip)
5861 return uves_local_filename(
"fluxcal_2d_science", chip, -1, -1);
5873 uves_scired_fluxcal_error_2d_filename(
enum uves_chip chip)
5875 return uves_local_filename(
"fluxcal_error_2d_science", chip, -1, -1);
5887 uves_scired_ff_variance_filename(
enum uves_chip chip)
5889 return uves_local_filename(
"variance_ff_science", chip, -1, -1);
5902 uves_scired_ff_variance_2d_filename(
enum uves_chip chip)
5904 return uves_local_filename(
"variance_ff_2d_science", chip, -1, -1);
5916 uves_scired_merged_2d_science_filename(
enum uves_chip chip)
5918 return uves_local_filename(
"merged_2d_science", chip, -1, -1);
5930 uves_scired_merged_science_filename(
enum uves_chip chip)
5932 return uves_local_filename(
"merged_science", chip, -1, -1);
5944 uves_scired_merged_sky_filename(
enum uves_chip chip)
5946 return uves_local_filename(
"merged_sky", chip, -1, -1);
5959 uves_scired_background_filename(
enum uves_chip chip)
5961 return uves_local_filename(
"background", chip, -1, -1);
5974 uves_scired_resampled_filename(
enum uves_chip chip)
5976 return uves_local_filename(
"resampled_science", chip, -1, -1);
5991 uves_scired_resampled_2d_filename(
enum uves_chip chip)
5993 return uves_local_filename(
"resampled_2d_science", chip, -1, -1);
6007 uves_scired_resampledmf_filename(
enum uves_chip chip)
6009 return uves_local_filename(
"resampled_mflat", chip, -1, -1);
6023 uves_scired_rebinned_filename(
enum uves_chip chip)
6025 return uves_local_filename(
"resampled_ff_science", chip, -1, -1);
6039 uves_scired_rebinned_error_filename(
enum uves_chip chip)
6041 return uves_local_filename(
"resampled_error_ff_science", chip, -1, -1);
6055 uves_scired_rebinned_2d_filename(
enum uves_chip chip)
6057 return uves_local_filename(
"resampled_ff_2d_science", chip, -1, -1);
6070 uves_scired_rebinned_2d_error_filename(
enum uves_chip chip)
6072 return uves_local_filename(
"resampled_error_ff_2d_science", chip, -1, -1);
6084 uves_scired_ordertrace_filename(
enum uves_chip chip)
6086 return uves_local_filename(
"ordertrace", chip, -1, -1);
6099 uves_scired_crmask_filename(
enum uves_chip chip)
6101 return uves_local_filename(
"cr_mask", chip, -1, -1);
6114 uves_scired_wmap_filename(
enum uves_chip chip)
6116 return uves_local_filename(
"wave_map", chip, -1, -1);
6128 char *uves_scired_ext2d_filename(
enum uves_chip chip)
6130 return uves_local_filename(
"ext_2d_science", chip, -1, -1);
6142 char *uves_scired_ff2d_filename(
enum uves_chip chip)
6144 return uves_local_filename(
"ff_2d_science", chip, -1, -1);
6170 uves_local_filename(
const char *prefix,
enum uves_chip chip,
int trace,
int window)
6172 char *result = NULL;
6173 const char *chip_string;
6174 const char *suffix =
".fits";
6178 assure( (trace < 0 && window < 0) ||
6179 (trace < 0 && window > 0) ||
6180 (trace >= 0 && window > 0),
6181 CPL_ERROR_ILLEGAL_INPUT,
"Illegal trace and window numbers: (%d, %d)",
6188 check(( t = int_to_string(trace),
6189 w = int_to_string(window)),
6190 "Error creating substrings");
6206 result = uves_sprintf(
"%s_%s%s%s%s", prefix, chip_string, t, w, suffix);
6212 if (cpl_error_get_code() != CPL_ERROR_NONE)
6214 cpl_free(result); result = NULL;
6232 int_to_string(
int i)
6234 char *result = NULL;
6236 assure( -1 <= i, CPL_ERROR_ILLEGAL_INPUT,
"Illegal number (%d)", i);
6241 result = cpl_calloc(1,
sizeof(
char));
6246 result = uves_sprintf(
"_%d", i);
6250 if (cpl_error_get_code() != CPL_ERROR_NONE){
6251 cpl_free(result); result = NULL;
6270 uves_vector_to_image(
const cpl_vector* vector,cpl_type type)
6273 cpl_image* image=NULL;
6275 const double* pv=NULL;
6281 size=cpl_vector_get_size(vector);
6282 image=cpl_image_new(size,1,type);
6283 pv=cpl_vector_get_data_const(vector);
6284 if(type == CPL_TYPE_INT) {
6285 pi=cpl_image_get_data_int(image);
6286 for(i=0;i<size;i++) {
6289 }
else if (type == CPL_TYPE_FLOAT) {
6290 pf=cpl_image_get_data_float(image);
6291 for(i=0;i<size;i++) {
6294 }
else if (type == CPL_TYPE_DOUBLE) {
6295 pd=cpl_image_get_data_double(image);
6296 for(i=0;i<size;i++) {
6300 assure(
false, CPL_ERROR_INVALID_TYPE,
6301 "No CPL type to represent BITPIX = %d", type);
6305 if (cpl_error_get_code() != CPL_ERROR_NONE){
6306 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.