58 #include <flames_midas_def.h>
60 #include <uves_utils_cpl.h>
62 #include <uves_dump.h>
63 #include <uves_utils_wrappers.h>
64 #include <uves_error.h>
67 #include <uves_pfits.h>
68 #include <uves_globals.h>
128 #define COLNAME "ColName"
159 struct frame frames[MAX_OPEN];
160 const char *current_caller = NULL;
182 struct frame *frm = &frames[id];
187 (!frm->need_to_save || frm->data.image.image != NULL) &&
188 (frm->data.image.image == NULL
189 || cpl_image_get_type(frm->data.image.image) == frm->data.image.type);
193 return (!frm->need_to_save || frm->data.table.table != NULL)
195 ((frm->data.table.table == NULL) == (frm->data.table.colnames == NULL))
197 (frm->data.table.table == NULL ||
198 cpl_table_get_ncol(frm->data.table.table) - 1 ==
199 cpl_table_get_nrow(frm->data.table.colnames))
201 (frm->data.table.table == NULL ||
202 cpl_table_has_column(frm->data.table.table,
"Select"));
215 return frames[id].filename != NULL;
233 if (strstr(filename,
".fits") == filename + strlen(filename) - 5) {
234 frames[id].filename = uves_sprintf(
"%s", filename);
236 else if (strstr(filename,
".bdf") == filename + strlen(filename) - 4) {
238 frames[id].filename = uves_sprintf(
"%sX", filename);
239 ((
char *)frames[
id].filename)[strlen(filename) - 3] =
'f';
240 ((
char *)frames[
id].filename)[strlen(filename) - 2] =
'i';
241 ((
char *)frames[
id].filename)[strlen(filename) - 1] =
't';
242 ((
char *)frames[
id].filename)[strlen(filename) - 0] =
's';
245 frames[id].filename = uves_sprintf(
"%s.fits", filename);
247 frames[id].is_image =
true;
248 frames[id].data.image.image = image;
249 frames[id].data.image.type = type;
250 frames[id].data.image.dtype = dtype;
251 frames[id].header = header;
252 frames[id].need_to_save = need_to_save;
271 if (strstr(filename,
".fits") == filename + strlen(filename) - 5) {
272 frames[id].filename = uves_sprintf(
"%s", filename);
274 else if (strstr(filename,
".tfits") == filename + strlen(filename) - 6) {
275 frames[id].filename = uves_sprintf(
"%s", filename);
277 else if (strstr(filename,
".tbl") == filename + strlen(filename) - 4) {
279 frames[id].filename = uves_sprintf(
"%sX", filename);
280 ((
char *)frames[
id].filename)[strlen(filename) - 3] =
'f';
281 ((
char *)frames[
id].filename)[strlen(filename) - 2] =
'i';
282 ((
char *)frames[
id].filename)[strlen(filename) - 1] =
't';
283 ((
char *)frames[
id].filename)[strlen(filename) - 0] =
's';
285 frames[id].filename = uves_sprintf(
"%s.fits", filename);
287 frames[id].is_image =
false;
288 frames[id].data.table.table = table;
290 cpl_table_new_column(table,
"Select", CPL_TYPE_INT);
291 cpl_table_fill_column_window_int(table,
"Select",
292 0, cpl_table_get_nrow(table),
298 frames[id].data.table.maxrow = maxrow;
299 frames[id].data.table.colnames = colnames;
300 frames[id].header = header;
301 frames[id].need_to_save = need_to_save;
317 uves_free_string_const(&frames[
id].filename);
318 if (frames[
id].is_image)
320 uves_free_image(&frames[
id].data.image.image);
324 uves_free_table(&frames[
id].data.table.table);
325 uves_free_table(&frames[
id].data.table.colnames);
327 uves_free_propertylist(&frames[
id].header);
342 strcmp(descr,
"NPIX") == 0 ||
343 strcmp(descr,
"REFPIX") == 0 ||
344 strcmp(descr,
"START") == 0 ||
345 strcmp(descr,
"STEP") == 0 ||
346 strcmp(descr,
"ROTA") == 0 ||
347 strcmp(descr,
"CUNIT") == 0 ||
348 strcmp(descr,
"IDENT") == 0 ||
349 strcmp(descr,
"O_TIME") == 0 ||
350 strcmp(descr,
"LHCUTS") == 0 ||
351 strcmp(descr,
"O_POS") == 0;
387 const char *fits_descr = NULL;
388 if (strcmp(descr,
"NPIX" ) == 0) fits_descr = uves_sprintf(
"NAXIS%d", indx);
389 else if (strcmp(descr,
"REFPIX") == 0) assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"%s", descr);
390 else if (strcmp(descr,
"START" ) == 0) fits_descr = uves_sprintf(
"CRVAL%d", indx);
391 else if (strcmp(descr,
"STEP" ) == 0) fits_descr = uves_sprintf(
"CDELT%d", indx);
392 else if (strcmp(descr,
"ROTA" ) == 0) assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"%s", descr);
393 else if (strcmp(descr,
"CUNIT" ) == 0)
394 fits_descr = (indx == 1) ? uves_sprintf(
"BUNIT") : uves_sprintf(
"CTYPE%d", indx);
395 else if (strcmp(descr,
"IDENT" ) == 0) fits_descr = uves_sprintf(
"OBJECT");
396 else if (strcmp(descr,
"O_TIME") == 0) assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"%s", descr);
397 else if (strcmp(descr,
"LHCUTS") == 0)
400 (indx == 1) ? uves_sprintf(
"LHCUTS1") :
401 (indx == 2) ? uves_sprintf(
"LHCUTS2") :
402 (indx == 3) ? uves_sprintf(
"DATAMIN") : uves_sprintf(
"DATAMAX");
404 else if (strcmp(descr,
"O_POS" ) == 0) assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"%s", descr);
406 if (fits_descr == NULL)
408 fits_descr = uves_sprintf(
"%s", descr);
445 const char *new_name = NULL;
447 cpl_table *new_values = NULL;
448 cpl_property *new_prop = NULL;
454 for (i = 0; i < plist_size; i++) {
455 cpl_property *p = uves_propertylist_get(*header, i);
456 const char *name = cpl_property_get_name(p);
459 if (cpl_property_get_comment(p) != NULL &&
460 strcmp(cpl_property_get_comment(p),
"PROCESSED") == 0) {
463 else if ((strlen(name) > 8 && strncmp(name,
"ESO ", 4) != 0)
465 (cpl_property_get_comment(p) != NULL &&
466 strcmp(cpl_property_get_comment(p),
"MIDAS_DESC") == 0)) {
470 uves_free_string_const(&new_name);
471 switch (cpl_property_get_type(p)) {
472 case CPL_TYPE_STRING:
473 new_name = uves_sprintf(
"'%s','C'", name);
476 new_name = uves_sprintf(
"'%s','I'", name);
479 new_name = uves_sprintf(
"'%s','R*4'", name);
481 case CPL_TYPE_DOUBLE:
482 new_name = uves_sprintf(
"'%s','R*8'", name);
485 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me %s %s", name,
490 uves_free_table(&new_values);
491 new_values = cpl_table_new(2);
492 cpl_table_new_column(new_values,
"Val", CPL_TYPE_STRING);
496 for (j = i; j < plist_size; j++) {
499 check_nomsg( p2 = uves_propertylist_get(*header, j) );
500 if (strcmp(cpl_property_get_name(p2), name) == 0) {
506 cpl_property_set_comment(p2,
"PROCESSED");
508 assure( cpl_property_get_type(p2) == cpl_property_get_type(p),
509 CPL_ERROR_TYPE_MISMATCH,
510 "Found property %s with type %s and with type %s",
515 uves_free_string(&v);
516 switch (cpl_property_get_type(p2)) {
517 case CPL_TYPE_STRING:
518 if(strlen(cpl_property_get_string(p2)) > 0) {
519 v = uves_sprintf(
"%s", cpl_property_get_string(p2));
527 v = uves_sprintf(
"%d", cpl_property_get_int(p2));
530 v = uves_sprintf(
"%g", cpl_property_get_float(p2));
532 case CPL_TYPE_DOUBLE:
533 v = uves_sprintf(
"%g", cpl_property_get_double(p2));
536 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
537 "Implement me %s %s", name,
543 assure(v != NULL, CPL_ERROR_UNSPECIFIED,
"Allocation failure");
544 assure( strlen(v) <= 80 - strlen(
"HISTORY "),
545 CPL_ERROR_UNSUPPORTED_MODE,
546 "Value (%s) too long string",
552 if (n_prop >= cpl_table_get_nrow(new_values))
554 cpl_table_set_size(new_values,
555 2*cpl_table_get_nrow(new_values));
558 check_nomsg( cpl_table_set_string(new_values,
"Val", n_prop, v) );
562 cpl_table_set_size(new_values, n_prop);
570 uves_propertylist_append_string(result,
"HISTORY", new_name);
571 for (j = 0; j < cpl_table_get_nrow(new_values); j++)
573 uves_propertylist_append_string(result,
"HISTORY",
574 cpl_table_get_string(new_values,
"Val", j));
576 uves_propertylist_append_string(result,
"HISTORY",
"");
581 uves_free_property(&new_prop);
582 new_prop = cpl_property_duplicate(p);
583 uves_propertylist_append_property(result, new_prop);
588 uves_free_propertylist(header);
592 uves_free_string_const(&new_name);
593 uves_free_string(&v);
594 uves_free_table(&new_values);
595 uves_free_property(&new_prop);
596 uves_free_propertylist(&result);
628 const char *new_name = NULL;
629 const char *new_value = NULL;
631 const char *desc_name = NULL;
632 const void *values = NULL;
635 "Could not load extension %d header from file %s", extension, filename);
639 for (i = 0; i < plist_size; i++) {
640 cpl_property *p = uves_propertylist_get(fitsheader, i);
641 const char *name = cpl_property_get_name(p);
642 bool convert_from_midas =
false;
644 if (strcmp(name,
"HISTORY") == 0)
648 if (cpl_property_get_type(p) == CPL_TYPE_STRING)
650 value = cpl_property_get_string(p);
655 if ((
int)strlen(value) >= 3 &&
657 strstr(value+2,
"'") != NULL)
660 uves_free_string_const(&desc_name);
661 desc_name = cpl_strdup(value+1);
662 *(strstr(desc_name,
"'")) =
'\0';
679 if (convert_from_midas) {
684 uves_free(values); values = NULL;
686 uves_read_midas_array(fitsheader, desc_name, &length, &type,
688 "Could not get values of HISTORY descriptor '%s'", desc_name);
693 for (j = 0; j < ((type == CPL_TYPE_STRING) ? 1 : length); j++) {
695 uves_free_string_const(&new_name);
696 new_name = uves_sprintf(
"%s", desc_name);
705 case CPL_TYPE_DOUBLE:
708 case CPL_TYPE_STRING:
712 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
718 uves_propertylist_append_property(result, p);
723 uves_free_string_const(&new_name);
724 uves_free_string_const(&new_value);
725 uves_free_string_const(&desc_name);
726 uves_free_propertylist(&fitsheader);
727 uves_free(values); values = NULL;
728 if (cpl_error_get_code() != CPL_ERROR_NONE)
730 uves_free_propertylist(&result);
744 cpl_type type = CPL_TYPE_INVALID;
747 case D_OLD_FORMAT: type = CPL_TYPE_FLOAT;
break;
748 case D_R4_FORMAT: type = CPL_TYPE_FLOAT;
break;
749 case D_R8_FORMAT: type = CPL_TYPE_DOUBLE;
break;
750 case D_I1_FORMAT: type = CPL_TYPE_INT;
break;
751 case D_I2_FORMAT: type = CPL_TYPE_INT;
break;
752 case D_I4_FORMAT: type = CPL_TYPE_INT;
break;
754 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me %d",
781 if (frames[
id].header == NULL)
786 "Error loading header from %s", frames[
id].filename);
814 char *key_type = NULL;
815 char *key_form = NULL;
816 char *key_unit = NULL;
817 char *key_null = NULL;
818 char *val_type = NULL;
819 char *val_form = NULL;
820 char *val_unit = NULL;
825 for(colname = cpl_table_get_column_name(t);
827 colname = cpl_table_get_column_name(NULL)) {
830 for (i = 1; i <= tfield && !found; i++) {
831 uves_free_string(&key_type);
832 uves_free_string(&key_form);
833 uves_free_string(&key_unit);
834 uves_free_string(&key_null);
835 uves_free_string(&val_type);
836 uves_free_string(&val_form);
837 uves_free_string(&val_unit);
838 key_type = uves_sprintf(
"TTYPE%d", i);
839 key_form = uves_sprintf(
"TFORM%d", i);
840 key_unit = uves_sprintf(
"TUNIT%d", i);
841 key_null = uves_sprintf(
"TNULL%d", i);
845 if (strlen(val_type) > 0) {
846 while (val_type[strlen(val_type)-1] ==
' ') {
847 val_type[strlen(val_type)-1] =
'\0';
851 if (strcmp(val_type, colname) == 0) {
855 if (strlen(val_form) > 0) {
856 while (val_form[strlen(val_form)-1] ==
' ') {
857 val_form[strlen(val_form)-1] =
'\0';
861 cpl_table_set_column_format(t, colname, val_form);
865 if (strlen(val_unit) > 0) {
866 while (val_unit[strlen(val_unit)-1] ==
' ') {
867 val_unit[strlen(val_unit)-1] =
'\0';
871 cpl_table_set_column_unit(t, colname, val_unit);
882 cpl_table_set_column_unit(t, colname,
" ");
885 if (cpl_table_get_column_type(t, colname) == CPL_TYPE_INT &&
889 cpl_table_fill_invalid_int(t, colname, val_null);
896 uves_free_string(&key_type);
897 uves_free_string(&key_form);
898 uves_free_string(&key_unit);
899 uves_free_string(&key_null);
900 uves_free_string(&val_type);
901 uves_free_string(&val_form);
902 uves_free_string(&val_unit);
917 cpl_imagelist *ilist = NULL;
918 fitsfile *fptr = NULL;
923 if (frames[
id].is_image) {
924 if (frames[
id].data.image.image == NULL) {
926 long firstpixel[4] = {1, 1, 1, 1};
936 fits_open_file(&fptr, frames[
id].filename, READONLY, &fio_status);
938 assure( fio_status == 0, CPL_ERROR_FILE_IO,
939 "Failed to open %s for reading", frames[
id].filename );
942 fits_get_img_dim(fptr, &naxis, &fio_status);
943 assure( naxis == 1 || naxis == 2 || naxis == 3 || naxis == 4,
944 CPL_ERROR_ILLEGAL_INPUT,
"Illegal dimension: %d", naxis);
951 fits_get_img_size(fptr, naxis, naxes, &fio_status);
952 assure( fio_status == 0, CPL_ERROR_FILE_IO,
953 "Failed to get %s image size", frames[
id].filename);
956 frames[id].data.image.image = cpl_image_new(naxes[0] * naxes[1] * naxes[2] * naxes[3], 1,
957 frames[
id].data.image.type);
959 switch(frames[
id].data.image.type) {
960 case CPL_TYPE_DOUBLE:
961 fits_read_pix(fptr, TDOUBLE, firstpixel, naxes[0] * naxes[1] * naxes[2] * naxes[3],
962 NULL, cpl_image_get_data_double(frames[
id].data.image.image),
966 fits_read_pix(fptr, TFLOAT, firstpixel, naxes[0] * naxes[1] * naxes[2] * naxes[3],
967 NULL, cpl_image_get_data_float(frames[
id].data.image.image),
971 fits_read_pix(fptr, TINT, firstpixel, naxes[0] * naxes[1] * naxes[2] * naxes[3],
972 NULL, cpl_image_get_data_int(frames[
id].data.image.image),
976 assure(
false, CPL_ERROR_INVALID_TYPE,
981 fits_close_file(fptr, &fio_status) ;
982 assure( fio_status == 0, CPL_ERROR_FILE_IO,
983 "Failed to load image %s", frames[
id].filename);
989 if (frames[
id].data.table.table == NULL)
992 int mark_invalid_values = 1;
996 uves_msg_debug(
"Loading table %s to memory", frames[
id].filename);
998 check( frames[
id].data.table.table =
999 cpl_table_load(frames[
id].filename,
1001 mark_invalid_values),
1002 "Error loading table from %s", frames[
id].filename);
1004 if (!cpl_table_has_column(frames[
id].data.table.table,
"Select")) {
1005 cpl_table_new_column(frames[
id].data.table.table,
"Select",
1007 cpl_table_fill_column_window_int(
1008 frames[
id].data.table.table,
"Select",
1009 0, cpl_table_get_nrow(frames[
id].data.table.table),
1013 frames[id].data.table.maxrow = cpl_table_get_nrow(frames[
id].data.table.table);
1016 "Error loading table header from %s", frames[
id].filename);
1019 frames[id].data.table.colnames =
1020 cpl_table_new(cpl_table_get_ncol(frames[
id].data.table.table) - 1);
1021 cpl_table_new_column(frames[
id].data.table.colnames, COLNAME, CPL_TYPE_STRING);
1023 for(name = cpl_table_get_column_name(frames[
id].data.table.table), row = 0;
1025 name = cpl_table_get_column_name(NULL)) {
1026 if (strcmp(name,
"Select") != 0) {
1027 cpl_table_set_string(frames[
id].data.table.colnames, COLNAME, row, name);
1035 "Error loading table %s format/units", frames[
id].filename);
1042 uves_free_imagelist(&ilist);
1043 uves_free_propertylist(&theader);
1059 static cpl_property **
1062 char type,
int length,
1065 const char *fits_descr = NULL;
1066 const char *previous_descr = NULL;
1067 cpl_property **cards = NULL;
1069 cpl_property *new_prop = NULL;
1071 passure( header != NULL,
" ");
1072 assure( length >= 1, CPL_ERROR_ILLEGAL_INPUT,
"Length = %d", length);
1074 cards = cpl_malloc((length+1) *
sizeof(cpl_property *));
1080 "Could not convert %s to FITS", descr);
1084 previous_descr = uves_sprintf(
"----");
1087 for (i = 1; i <= length; i++)
1089 const char *comment;
1091 uves_free_string_const(&fits_descr);
1093 "Could not convert %s to FITS", descr);
1095 uves_msg_debug(
"Creating property %s (%d of %d, type = '%c')", fits_descr,
1096 nexist + i, nexist + length, type);
1098 if (strcmp(descr, fits_descr) == 0 &&
1099 strncmp(descr,
"CTYPE", 5) != 0 &&
1100 strncmp(descr,
"CDELT", 5) != 0 &&
1101 strncmp(descr,
"CRVAL", 5) != 0 &&
1102 strncmp(descr,
"CRPIX", 5) != 0 &&
1103 strncmp(descr,
"ESO QC", 6) != 0 &&
1104 strcmp(descr,
"BUNIT") != 0 &&
1105 strcmp(descr,
"COLS") != 0 &&
1106 strcmp(descr,
"ROWS") != 0 &&
1107 strcmp(descr,
"PIXMAX") != 0 &&
1108 strcmp(descr,
"STARTX") != 0 &&
1109 strcmp(descr,
"STARTY") != 0 &&
1110 strcmp(descr,
"STEPX") != 0 &&
1111 strcmp(descr,
"STEPY") != 0 &&
1112 strcmp(descr,
"YSHIFT") != 0 &&
1113 strcmp(descr,
"DATAMIN") != 0 &&
1114 strcmp(descr,
"DATAMAX") != 0 &&
1115 strcmp(descr,
"NFLATS") != 0 &&
1116 strcmp(descr,
"RON") != 0 &&
1117 strcmp(descr,
"GAIN") != 0 &&
1118 strcmp(descr,
"FIBRESON") != 0)
1122 comment =
"MIDAS_DESC";
1138 default: assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"%c", type);
break;
1143 cards[i-1] = uves_find_property(header,
1145 strcmp(fits_descr, previous_descr) != 0 ?
1148 passure( cards[i-1] != NULL,
"%s %d %d", fits_descr, i-1, nexist);
1150 uves_free_string_const(&previous_descr);
1151 previous_descr = uves_sprintf(
"%s", fits_descr);
1154 cards[length] = NULL;
1157 uves_free_property(&new_prop);
1158 uves_free_string_const(&fits_descr);
1159 uves_free_string_const(&previous_descr);
1183 static cpl_property **
1185 char *type,
int *length,
int *bytelem)
1191 cpl_property **cards = NULL;
1193 const char *fits_descr = NULL;
1194 const char *previous_fits_descr = NULL;
1200 "Frame no. %d is not open",
id);
1203 "Could not load header of file %s", frames[
id].filename);
1205 cards = cpl_calloc(1,
sizeof(cpl_property *));
1211 cards = cpl_realloc(cards, (*length)*
sizeof(cpl_property *));
1213 uves_free_string_const(&previous_fits_descr);
1214 previous_fits_descr = uves_sprintf(
"%s", fits_descr != NULL ? fits_descr :
"----");
1216 uves_free_string_const(&fits_descr);
1220 strcmp(fits_descr, previous_fits_descr) == 0 ?
1225 uves_find_property(frames[
id].header,
1227 strcmp(fits_descr, previous_fits_descr) == 0 ?
1230 while (cards[*length-1] != NULL);
1234 if (cards[0] != NULL)
1236 t = cpl_property_get_type(cards[0]);
1240 case CPL_TYPE_INT : *type =
'I';
break;
1241 case CPL_TYPE_FLOAT : *type =
'R';
break;
1242 case CPL_TYPE_STRING: *type =
'C';
break;
1243 case CPL_TYPE_DOUBLE: *type =
'D';
break;
1244 default: *type =
' ';
break;
1249 assure( *type !=
'C' || *length <= 3, CPL_ERROR_UNSUPPORTED_MODE,
1250 "Cannot handle string array descriptor %s %s of length %d",
1251 descr, cpl_property_get_string(cards[0]),*length );
1256 passure( *length <= 3,
"%d", *length );
1259 *length = strlen(cpl_property_get_string(cards[0]));
1265 cpl_free(cards); cards = NULL;
1272 uves_free_string_const(&fits_descr);
1273 uves_free_string_const(&previous_fits_descr);
1274 if (cpl_error_get_code() != CPL_ERROR_NONE)
1276 cpl_free(cards); cards = NULL;
1297 assure( current_caller == NULL, CPL_ERROR_ILLEGAL_INPUT,
1298 "MIDAS mode already running");
1301 current_caller = uves_sprintf(
"%s", name);
1303 assure( strcmp(name,
"-1") != 0, CPL_ERROR_UNSUPPORTED_MODE,
1304 "Running outside MIDAS mode not supported");
1307 for (i = 0; i < MAX_OPEN; i++)
1309 frames[i].filename = NULL;
1313 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1325 if (current_caller == NULL)
1334 for (i = 0; i < MAX_OPEN; i++)
1340 frames[i].is_image ?
"Image" :
"Table",
1341 i, frames[i].filename);
1348 uves_free_string_const(¤t_caller);
1351 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1375 current_caller != NULL ? current_caller :
"???",
1376 function, file,
function, line);
1392 const char *f = cpl_strdup(current_caller != NULL ? current_caller :
"???");
1399 assure(
false, CPL_ERROR_UNSPECIFIED,
"%s failed", f);
1402 uves_free_string_const(&f);
1418 int felem,
int maxvals,
int *unit)
1425 uves_msg_debug(
"Writing %d elements to integer keyword", maxvals);
1427 for (i = 0; i < maxvals; i++) {
1428 key[(felem-1) + i] = values[i];
1432 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1447 int felem,
int maxvals,
int *unit)
1454 uves_msg_debug(
"Writing %d elements to double keyword", maxvals);
1456 for (i = 0; i < maxvals; i++) {
1457 key[(felem-1) + i] = values[i];
1463 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1480 const char *values,
int felem,
int maxvals,
int *unit)
1483 assure_nomsg( noelem == 1, CPL_ERROR_UNSUPPORTED_MODE);
1486 uves_msg_debug(
"Writing %d elements to character keyword", maxvals);
1488 strncpy(key+(felem-1), values, maxvals);
1492 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1508 int felem,
int maxvals,
1509 int *actvals,
char *values)
1516 strncpy(values, key + (felem - 1), maxvals);
1517 values[strlen(key)+1] =
'\0';
1518 *actvals = strlen(values);
1527 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1542 int felem,
int maxvals,
1543 int *actvals,
const cpl_frameset **values)
1547 assure( felem == 1, CPL_ERROR_ILLEGAL_INPUT,
1548 "felem = %d", felem );
1555 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1570 int felem,
int maxvals,
1571 int *actvals, cpl_frameset ***values)
1575 assure( felem == 1, CPL_ERROR_ILLEGAL_INPUT,
1576 "felem = %d", felem );
1583 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1600 int *actvals,
double *values,
int *unit,
int *null)
1609 for (i = 0; i < maxvals; i++)
1611 values[i] = key[(felem-1)+i];
1618 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1635 int *actvals,
float *values,
int *unit,
int *null)
1644 for (i = 0; i < maxvals; i++)
1646 values[i] = key[(felem-1)+i];
1653 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1670 int *actvals,
int *values,
int *unit,
int *null)
1677 for (i = 0; i < maxvals; i++)
1679 values[i] = key[(felem-1)+i];
1686 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1704 int *actvals,
char *values,
int *unit,
int *null)
1714 assure( felem == 1, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me" );
1715 assure( maxvals == 1, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me" );
1717 strncpy(values, key + (felem - 1), noelm);
1718 values[noelm] =
'\0';
1719 *actvals = strlen(values);
1724 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1740 sckfnd(
const char the_type,
const void *key,
char *type,
int *noelem,
int *bytelem)
1756 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1766 return sckfnd(
'D', key, type, noelem, bytelem);
1775 return sckfnd(
'R', key, type, noelem, bytelem);
1785 return sckfnd(
'I', key, type, noelem, bytelem);
1795 return sckfnd(
'C', key, type, noelem, bytelem);
1807 const char *
function,
const char *file,
int line)
1809 if (strncmp(msg,
"Error", 5) == 0)
1813 else if (strncmp(msg,
"Warning", 7) == 0)
1824 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1863 if (name == NULL)
return 1;
1866 file = fopen(name,
"r");
1878 if (uves_get_nextensions(name) > 0) {
1879 ibuf[0] = F_TBL_TYPE;
1882 ibuf[0] = F_IMA_TYPE;
1892 "Could not load %s primary header", name);
1895 "Could not get BITPIX from %s", name);
1900 case 16: ibuf[1] = D_I2_FORMAT;
break;
1901 case 32: ibuf[1] = D_I4_FORMAT;
break;
1902 case -32: ibuf[1] = D_R4_FORMAT;
break;
1903 case -64: ibuf[1] = D_R8_FORMAT;
break;
1905 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1906 "Cannot convert BITPIX = %d to DATTYPE",
1917 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
1918 "fno = %d is not needed by FLAMES code", fno);
1922 uves_free_propertylist(&header);
1923 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
1942 if (filtype == F_IMA_TYPE) {
1949 for (i = 0; !found && i < MAX_OPEN; i++)
1961 NULL, type, dattype);
1969 assure( found, CPL_ERROR_UNSUPPORTED_MODE,
1970 "Cannot open more than %d image files",
1976 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me");
1981 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me");
1985 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
2004 int size,
int *imno)
2006 if (filtype == F_IMA_TYPE)
2008 if (iomode == F_O_MODE)
2016 for (i = 0; !found && i < MAX_OPEN; i++)
2029 assure( found, CPL_ERROR_UNSUPPORTED_MODE,
2030 "Cannot open more than %d image files",
2037 cpl_image_new(size, 1, type), type, dattype);
2043 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me");
2048 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me");
2055 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
2068 cpl_property **cards = NULL;
2069 fitsfile *fptr = NULL;
2077 frames[
id].is_image ?
"Image" :
"Table",
2082 if (frames[
id].need_to_save) {
2090 "Could not load frame %s", frames[
id].filename);
2093 "Could not load %s header", frames[
id].filename);
2096 &type, &naxis, &bytelem),
2097 "Could not get info on descriptor NPIX");
2100 "Could not convert header");
2103 if (frames[
id].is_image)
2107 bool save_as_bitpix16 =
2108 (frames[id].data.image.dtype == D_I1_FORMAT ||
2109 frames[id].data.image.dtype == D_I2_FORMAT);
2113 bool save_as_1d = (naxis == 1);
2116 assure( naxis == 1 || naxis == 2 || naxis == 3 || naxis == 4,
2117 CPL_ERROR_UNSUPPORTED_MODE,
2118 "Cannot save image with NAXIS = %d", naxis);
2122 check( uves_save_image(frames[
id].data.image.image,
2123 frames[
id].filename,
2127 "Error saving image %s", frames[
id].filename);
2129 if (naxis == 2 || naxis == 3 || naxis == 4) {
2134 char err_message[81];
2142 CPL_ERROR_ILLEGAL_INPUT,
2143 "Failed to read NPIX");
2145 assure( actvals == naxis, CPL_ERROR_ILLEGAL_INPUT,
2146 "naxis = %d but actvals = %d", naxis, actvals);
2152 fits_open_file(&fptr, frames[
id].filename, READWRITE, &fio_status);
2154 assure( fio_status == 0, CPL_ERROR_ILLEGAL_OUTPUT,
2155 "Failed to open file %s", frames[
id].filename);
2158 fits_read_record(fptr, 0, dummy, &fio_status);
2160 fits_read_key(fptr, TINT, (
char*)
"NAXIS", ¤t_naxis, NULL, &fio_status);
2162 fits_update_key(fptr, TINT, (
char*)
"NAXIS", &naxis, (
char*)
"Empty unit", &fio_status);
2163 fits_update_key(fptr, TINT, (
char*)
"NAXIS1", &NAXIS[0], (
char*)
"Empty unit", &fio_status);
2165 if (current_naxis < 2) {
2166 fits_insert_card(fptr, (
char*)
"NAXIS2", &fio_status);
2168 fits_update_key(fptr, TINT, (
char*)
"NAXIS2", &NAXIS[1], (
char*)
"Empty unit", &fio_status);
2171 fits_insert_card(fptr, (
char*)
"NAXIS3", &fio_status);
2172 fits_update_key(fptr, TINT, (
char*)
"NAXIS3", &NAXIS[2], (
char*)
"Empty unit", &fio_status);
2176 fits_insert_card(fptr, (
char*)
"NAXIS4", &fio_status);
2177 fits_update_key(fptr, TINT, (
char*)
"NAXIS4", &NAXIS[3], (
char*)
"Empty unit", &fio_status);
2180 fits_close_file(fptr, &fio_status);
2182 if (fio_status != 0) fits_read_errmsg(err_message);
2183 assure( fio_status == 0, CPL_ERROR_ILLEGAL_OUTPUT,
2184 "Error '%s' code %d while updating %s FITS header",
2185 err_message, fio_status, frames[
id].filename);
2191 cpl_table_set_size(frames[
id].data.table.table,
2192 frames[
id].data.table.maxrow);
2194 check( uves_table_save(frames[
id].data.table.table,
2197 frames[
id].filename,
2199 "Error saving table %s", frames[
id].filename);
2202 frames[id].need_to_save =
false;
2208 frames[
id].is_image ?
"image" :
"table",
2209 frames[
id].filename);
2220 cpl_free(cards); cards = NULL;
2221 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
2256 assure(
frame_is_open(imno) && frames[imno].is_image, CPL_ERROR_ILLEGAL_INPUT,
2257 "Image no. %d is not open", imno);
2260 "Could not load image %s", frames[imno].filename);
2262 assure( (felem-1) + size <=
2263 cpl_image_get_size_x(frames[imno].data.image.image)*
2264 cpl_image_get_size_y(frames[imno].data.image.image),
2265 CPL_ERROR_ACCESS_OUT_OF_RANGE,
2266 "Cannot read %d bytes of CPL image of size %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
" position %d",
2268 cpl_image_get_size_x(frames[imno].data.image.image),
2269 cpl_image_get_size_y(frames[imno].data.image.image),
2272 switch(frames[imno].data.image.type) {
2277 buffer = cpl_image_get_data_int(frames[imno].data.image.image);
2278 buffer += (felem-1);
2280 switch(frames[imno].data.image.dtype) {
2282 for (i = 0; i < size; i++)
2284 ((
char *)bufadr)[i] = buffer[i];
2289 for (i = 0; i < size; i++)
2291 ((int32_t *)bufadr)[i] = buffer[i];
2301 case CPL_TYPE_FLOAT:
2305 buffer = cpl_image_get_data_float(frames[imno].data.image.image);
2306 buffer += (felem-1);
2308 for (i = 0; i < size; i++)
2310 ((
float *)bufadr)[i] = buffer[i];
2316 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Type is %s",
2324 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
2345 assure(
frame_is_open(imno) && frames[imno].is_image, CPL_ERROR_ILLEGAL_INPUT,
2346 "Image no. %d is not open", imno);
2350 "Could not load image %s", frames[imno].filename);
2352 assure( (felem-1) + size <=
2353 cpl_image_get_size_x(frames[imno].data.image.image)*
2354 cpl_image_get_size_y(frames[imno].data.image.image),
2355 CPL_ERROR_ACCESS_OUT_OF_RANGE,
2356 "Cannot write %d pixels to CPL image of size %" CPL_SIZE_FORMAT
"x%" CPL_SIZE_FORMAT
" position %d",
2358 cpl_image_get_size_x(frames[imno].data.image.image),
2359 cpl_image_get_size_y(frames[imno].data.image.image),
2363 size, frames[imno].filename);
2365 switch(frames[imno].data.image.type) {
2369 buffer = cpl_image_get_data_int(frames[imno].data.image.image);
2370 buffer += (felem-1);
2372 switch(frames[imno].data.image.dtype) {
2374 for (i = 0; i < size; i++)
2376 buffer[i] = ((
char *)bufadr)[i];
2381 for (i = 0; i < size; i++)
2383 buffer[i] = ((
int *)bufadr)[i];
2392 case CPL_TYPE_FLOAT:
2395 buffer = cpl_image_get_data_float(frames[imno].data.image.image);
2396 buffer += (felem-1);
2398 for (i = 0; i < size; i++)
2400 buffer[i] = ((
float *)bufadr)[i];
2404 case CPL_TYPE_DOUBLE:
2407 buffer = cpl_image_get_data_double(frames[imno].data.image.image);
2408 buffer += (felem-1);
2410 for (i = 0; i < size; i++)
2412 buffer[i] = ((
double *)bufadr)[i];
2417 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Type is %s",
2422 frames[imno].need_to_save =
true;
2427 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
2446 char *type,
int *noelem,
int *bytelem)
2448 cpl_property **cards = NULL;
2453 if (strcmp(descr,
"LHCUTS") == 0)
2455 int datamin_noelem, datamax_noelem;
2456 char datamin_type, datamax_type;
2464 assure( datamin_noelem <= 1, CPL_ERROR_ILLEGAL_INPUT,
2465 "Multiple (%d) DATAMIN keywords found", datamin_noelem);
2467 assure( datamax_noelem <= 1, CPL_ERROR_ILLEGAL_INPUT,
2468 "Multiple (%d) DATAMIN keywords found", datamax_noelem);
2470 if (datamin_noelem > 0)
2473 assure( datamin_type ==
'D', CPL_ERROR_TYPE_MISMATCH,
2474 "DATAMIN has type %c, %c expected", datamin_type,
'D');
2476 if (datamax_noelem > 0)
2479 assure( datamax_type ==
'D', CPL_ERROR_TYPE_MISMATCH,
2480 "DATAMAX has type %c, %c expected", datamax_type,
'D');
2488 type, noelem, bytelem),
2489 "Could not get info on descriptor %s", descr);
2499 descr, *type, *noelem);
2506 cpl_free(cards); cards = NULL;
2508 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
2529 char *type,
int *noelem,
int *bytelem)
2531 cpl_property **cards = NULL;
2536 if (strcmp(descr,
"LHCUTS") == 0)
2538 int datamin_noelem, datamax_noelem;
2539 char datamin_type, datamax_type;
2547 assure( datamin_noelem <= 1, CPL_ERROR_ILLEGAL_INPUT,
2548 "Multiple (%d) DATAMIN keywords found", datamin_noelem);
2550 assure( datamax_noelem <= 1, CPL_ERROR_ILLEGAL_INPUT,
2551 "Multiple (%d) DATAMIN keywords found", datamax_noelem);
2553 if (datamin_noelem > 0)
2556 assure( datamin_type ==
'D', CPL_ERROR_TYPE_MISMATCH,
2557 "DATAMIN has type %c, %c expected", datamin_type,
'D');
2559 if (datamax_noelem > 0)
2562 assure( datamax_type ==
'D', CPL_ERROR_TYPE_MISMATCH,
2563 "DATAMAX has type %c, %c expected", datamax_type,
'D');
2571 type, noelem, bytelem),
2572 "Could not get info on descriptor %s", descr);
2578 cpl_free(cards); cards = NULL;
2584 descr, *type, *noelem);
2591 cpl_free(cards); cards = NULL;
2593 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
2613 scdrd(
char expected_type,
int id,
const char *descr,
2614 int felem,
int maxvals,
2615 int *actvals,
void *values,
2616 int *unit,
int *null)
2621 cpl_property **cards = NULL;
2630 "Could not get info on descriptor %s", descr);
2632 assure( cards != NULL, CPL_ERROR_DATA_NOT_FOUND,
2633 "Descriptor %s not found in file %s", descr, frames[
id].filename);
2636 assure( (expected_type ==
'D' && type ==
'R')
2638 type == expected_type, CPL_ERROR_TYPE_MISMATCH,
2639 "Descriptor %s has type %c, %c expected",
2640 descr, type, expected_type);
2642 passure( type !=
'C' || felem == 1,
"'%c' %d", type, felem);
2646 for (i = felem-1; i < length; i++)
2648 if (*actvals < maxvals)
2650 uves_msg_debug(
"Getting %d of %d (max %d) values of descriptor %s",
2651 *actvals + 1, length - (felem-1), maxvals, descr);
2655 ((
int *)values)[i-(felem-1)] = cpl_property_get_int(cards[i]);
2659 ((
double *)values)[i-(felem-1)] = cpl_property_get_double(cards[i]);
2663 switch(expected_type) {
2665 ((
float *)values)[i-(felem-1)] = cpl_property_get_float(cards[i]);
2669 ((
double *)values)[i-(felem-1)] = cpl_property_get_float(cards[i]);
2678 ((
char *)values)[i-(felem-1)] = cpl_property_get_string(cards[0])[i];
2682 assure(
false, CPL_ERROR_INVALID_TYPE,
"Type is %c", type);
2689 if (type ==
'C' && *actvals < maxvals)
2696 ((
char *)values)[length-(felem-1)] =
'\0';
2704 cpl_free(cards); cards = NULL;
2706 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
2717 int felem,
int maxvals,
2718 int *actvals,
int *values,
2719 int *unit,
int *null)
2721 char *char_values = NULL;
2724 if (strcmp(descr,
"MAXFIBRES") == 0)
2726 char_values = cpl_malloc(maxvals + 1);
2728 char_values[0] =
'\0';
2730 assure( felem == 1, CPL_ERROR_UNSUPPORTED_MODE,
2731 "first element no. (%d) is not 1", felem);
2733 check(
scdrd(
'C', imno, descr, felem, maxvals, actvals, char_values, unit, null),
2734 "Reading %s as string failed", descr);
2736 assure( strlen(char_values) == 1, CPL_ERROR_ILLEGAL_INPUT,
2737 "MAXFIBRES value (%s) has length different from 1",
2742 values[0] = atoi(char_values);
2743 assure( errno == 0, CPL_ERROR_ILLEGAL_OUTPUT,
2744 "Conversion of %s to integer failed", char_values);
2746 cpl_msg_debug(
"Got value %s (%d)", char_values, values[0]);
2752 return scdrd(
'I',
id, descr, felem, maxvals, actvals, values, unit, null);
2756 uves_free_string(&char_values);
2757 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
2768 int felem,
int maxvals,
2769 int *actvals,
double *values,
2770 int *unit,
int *null)
2772 return scdrd(
'D',
id, descr, felem, maxvals, actvals, values, unit, null);
2783 int felem,
int maxvals,
2784 int *actvals,
float *values,
2785 int *unit,
int *null)
2787 if (strcmp(
"LHCUTS", descr) == 0 && felem < 3)
2790 bool success =
true;
2792 for (i = felem; i < felem+maxvals; i++)
2795 if (1 <= i && i <= 2)
2801 success = success &&
2807 values[i-felem] = (float) val;
2811 success = success &&
2816 values[i-felem] = (float) val;
2823 return success ? 0 : 1;
2827 return scdrd(
'R',
id, descr, felem, maxvals, actvals, values, unit, null);
2844 int felem,
int maxvals,
2845 int *actvals,
char *values,
2846 int *unit,
int *null)
2848 int returnvalue = 1;
2850 assure( noelem ==
sizeof(
char), CPL_ERROR_UNSUPPORTED_MODE,
"Implement me" );
2851 assure( felem == 1, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me" );
2853 returnvalue =
scdrd(
'C',
id, descr, felem, maxvals, actvals, values, unit, null);
2881 scdwr(
char type_to_write,
int id,
const char *descr,
const void *values,
2882 int felem,
int nval,
const int *unit)
2885 int length, bytelem, i;
2886 cpl_property **cards = NULL;
2887 cpl_property **cards_extra = NULL;
2893 "Frame no. %d is not open",
id);
2896 &type, &length, &bytelem),
2897 "Could not get info on descriptor %s", descr);
2899 if (cards == NULL) {
2900 int number_of_cards = (type_to_write ==
'C') ? 1 : (felem-1)+nval;
2903 type = type_to_write;
2905 number_of_cards, nexisting),
2906 "Could not create %d %s descriptors",
2907 number_of_cards, descr);
2910 assure( type == type_to_write ||
2911 (type ==
'D' && type_to_write ==
'R'),
2912 CPL_ERROR_TYPE_MISMATCH,
2913 "Cannot write type %c data to type %c descriptor %s",
2914 type_to_write, type, descr);
2916 if (type_to_write !=
'C' &&
2917 (felem-1) + nval > length)
2920 int number_of_extra = (felem-1) + nval - length;
2925 while(cards[ncards] != NULL) ncards++;
2927 uves_msg_debug(
"Only %d existing %s descriptor(s), add another %d",
2928 ncards, descr, number_of_extra);
2932 number_of_extra, ncards),
2933 "Could not create %d %s descriptors",
2934 number_of_extra, descr);
2937 cards = cpl_realloc(cards, (ncards + number_of_extra + 1)*
sizeof(cpl_property *));
2939 for (i = ncards; i < ncards + number_of_extra; i++)
2941 cards[i] = cards_extra[i-ncards];
2944 cards[ncards+number_of_extra] = NULL;
2952 for (i = 0; i < ((type_to_write ==
'C') ? 1 : nval); i++) {
2953 if (type_to_write ==
'I') {
2956 ((type_to_write ==
'C') ? 1 : nval),
2957 ((
const int *)values)[i],
2963 ((type_to_write ==
'C') ? 1 : nval),
2968 switch(type_to_write) {
2969 case 'I': cpl_property_set_int (cards[(felem-1) + i], ((
const int *)values)[i]);
break;
2973 cpl_property_set_float (cards[(felem-1) + i], ((
const float *)values)[i]);
break;
2975 cpl_property_set_double(cards[(felem-1) + i], ((
const float *)values)[i]);
break;
2977 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
2978 "Cannot write type '%c' values to type '%c' descriptor",
2979 type_to_write, type);
2983 case 'C': cpl_property_set_string(cards[(felem-1) + i], (
const char *)values);
break;
2984 case 'D': cpl_property_set_double(cards[(felem-1) + i], ((
const double *)values)[i]);
break;
2986 assure(
false,CPL_ERROR_UNSUPPORTED_MODE,
"Implement me");
break;
2993 frames[id].need_to_save =
true;
2997 "Could not load frame %s", frames[
id].filename );
3002 cpl_free(cards); cards = NULL;
3003 cpl_free(cards_extra); cards_extra = NULL;
3005 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3016 int felem,
int nval,
const int *unit)
3018 if (strcmp(descr,
"NPIX") == 0)
3022 int *buffer_int=NULL;
3023 float *buffer_float=NULL;
3024 double *buffer_double=NULL;
3027 assure( nval == 1 || nval == 2 || nval == 3 || nval == 4,
3028 CPL_ERROR_UNSUPPORTED_MODE,
3029 "Only 2d, 3d and 4d (not %dd) images supported",
3032 assure( frames[
id].is_image, CPL_ERROR_ILLEGAL_INPUT,
3033 "Cannot write NPIX to table %s", frames[
id].filename);
3036 case 1: size = values[0];
break;
3037 case 2: size = values[0] * values[1];
break;
3038 case 3: size = values[0] * values[1] * values[2];
break;
3039 case 4: size = values[0] * values[1] * values[2] * values[3];
break;
3041 passure(
false,
"Impossible");
3045 if (frames[
id].data.image.image == NULL) {
3046 frames[id].data.image.image =
3047 cpl_image_new(size, 1,
3048 frames[
id].data.image.type);
3052 cpl_image_get_size_x(frames[
id].data.image.image) *
3053 cpl_image_get_size_y(frames[
id].data.image.image),
3054 CPL_ERROR_INCOMPATIBLE_INPUT,
3055 "Cannot set image %s NAXIS to %d because the "
3056 "image memory buffer size is %" CPL_SIZE_FORMAT
"",
3057 frames[
id].filename,
3059 cpl_image_get_size_x(frames[
id].data.image.image) *
3060 cpl_image_get_size_y(frames[
id].data.image.image));
3064 type = cpl_image_get_type(frames[
id].data.image.image);
3068 frames[
id].filename,
3070 values[0], values[1]);
3072 case CPL_TYPE_INT : buffer_int = cpl_image_get_data_int(frames[
id].data.image.image);
break;
3073 case CPL_TYPE_FLOAT : buffer_float = cpl_image_get_data_float(frames[
id].data.image.image);
break;
3074 case CPL_TYPE_DOUBLE: buffer_double = cpl_image_get_data_double(frames[
id].data.image.image);
break;
3076 assure(
false, CPL_ERROR_INVALID_TYPE,
"Type is %s",
3083 cpl_image_unwrap(frames[
id].data.image.image);
3086 case CPL_TYPE_INT : frames[id].data.image.image = cpl_image_wrap_int (values[0], values[1], buffer_int);
break;
3087 case CPL_TYPE_FLOAT : frames[id].data.image.image = cpl_image_wrap_float (values[0], values[1], buffer_float);
break;
3088 case CPL_TYPE_DOUBLE: frames[id].data.image.image = cpl_image_wrap_double(values[0], values[1], buffer_double);
break;
3090 assure(
false, CPL_ERROR_INVALID_TYPE,
"Type is %s",
3102 scdwr(
'I',
id, descr, values, felem, nval, unit);
3105 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3116 int felem,
int nval,
const int *unit)
3118 if (strcmp(
"CRPIX", descr) == 0 ||
3119 strcmp(
"CRVAL", descr) == 0 ||
3120 strcmp(
"CDELT", descr) == 0) {
3122 bool success =
true;
3124 for (i = felem; i < felem+nval; i++) {
3127 sprintf(descr_i,
"%s%i", descr, i);
3128 success = success &&
3130 felem, 1, unit) == 0);
3133 return success ? 0 : 1;
3136 return scdwr(
'D',
id, descr, values, felem, nval, unit);
3147 int felem,
int nval,
const int *unit)
3149 if (strcmp(
"LHCUTS", descr) == 0 && felem < 3) {
3151 bool success =
true;
3153 for (i = felem; i < felem+nval; i++)
3155 if (1 <= i && i <= 2)
3161 double val = (double) values[i-felem-1];
3162 success = success &&
3168 double val = (double) values[i-felem-1];
3169 success = success &&
3178 return success ? 0 : 1;
3182 return scdwr(
'R',
id, descr, values, felem, nval, unit);
3198 int felem,
int nval,
const int *unit)
3200 int returnvalue = 1;
3201 char* tmp_string=NULL;
3203 if (strcmp(descr,
"CUNIT") == 0) {
3208 assure( nval % 16 == 0, CPL_ERROR_UNSUPPORTED_MODE,
3209 "nval = %d", nval );
3213 strncpy(val, values, 16);
3217 for (i = 1; i < nval/16; i++) {
3218 const char *ctype_i;
3221 case 1: ctype_i =
"CTYPE1";
break;
3222 case 2: ctype_i =
"CTYPE2";
break;
3223 case 3: ctype_i =
"CTYPE3";
break;
3224 case 4: ctype_i =
"CTYPE4";
break;
3230 strncpy(val, values+i*16, 16);
3233 if (returnvalue == 0) {
3242 assure( nval == 3, CPL_ERROR_UNSUPPORTED_MODE,
3243 "noelm = %d, nval = %d", noelm, nval);
3247 if (returnvalue == 0) {
3250 if (returnvalue == 0) {
3256 assure( noelm ==
sizeof(
char), CPL_ERROR_UNSUPPORTED_MODE,
"Implement me" );
3257 assure( felem == 1, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me" );
3260 tmp_string=cpl_calloc((nval+1),
sizeof(
char));
3261 strncpy(tmp_string,values,nval);
3262 returnvalue =
scdwr(
'C',
id, descr, tmp_string, felem, nval, unit);
3267 cpl_free(tmp_string);
3281 cpl_property **cards = NULL;
3283 int length, bytelem;
3284 const char *fits_descr = NULL;
3285 const char *name_regexp = NULL;
3290 "Frame no. %d is not open",
id );
3295 "Could not get info on descriptor %s", descr);
3301 frames[id].need_to_save =
true;
3303 for (i = 1; i <= length; i++)
3306 uves_free_string_const(&fits_descr);
3308 "Could not convert %s to FITS", descr);
3314 uves_free_string_const(&fits_descr);
3315 name_regexp = uves_sprintf(
"^%s$", fits_descr);
3323 uves_free_string_const(&fits_descr);
3324 uves_free_string_const(&name_regexp);
3325 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3351 static int scdcop(
int from,
int to,
int mask,
bool copy_naxis)
3353 const char *key = NULL;
3358 "Image no. %d is not open", from);
3361 "Image no. %d is not open", to);
3364 "Could not load header of file %s", frames[from].filename);
3367 "Could not load header of file %s", frames[to].filename);
3382 frames[from].header,
3387 frames[from].header,
3396 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me");
3400 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me");
3404 if (frames[from].is_image && copy_naxis) {
3412 assure( naxis == 2, CPL_ERROR_UNSUPPORTED_MODE,
3413 "NAXIS = %d", naxis );
3415 for (axis = 1; axis <= naxis; axis++) {
3416 uves_free_string_const(&key);
3417 key = uves_sprintf(
"NAXIS%d", axis);
3425 frames[to].need_to_save =
true;
3427 "Could not load image %s", frames[to].filename);
3433 uves_free_string_const(&key);
3434 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3447 return scdcop(from, to, mask,
true);
3460 return scdcop(from, to, mask,
false);
3483 *noent = cpl_frameset_get_size(catfile);
3488 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3511 check( f = cpl_frameset_get_frame_const(catfile, frmno-1),
3512 "Could not get frame no. %d from catalog", frmno);
3514 strcpy(frame, cpl_frame_get_filename(f));
3519 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3541 char *name,
char *ident,
3544 const char *fits_descr = NULL;
3550 assure_nomsg( flag == 0 || ident != NULL, CPL_ERROR_NULL_INPUT );
3552 if (*no == cpl_frameset_get_size(catfile))
3562 check( f = cpl_frameset_get_frame_const(catfile, *no),
3563 "Could not get frame no. %d from catalog", *no);
3566 strcpy(name, cpl_frame_get_filename(f));
3569 const char *ident_value;
3572 "Failed to load %s header", name);
3578 fits_descr = uves_sprintf(
"%s",
"OBJECT");
3591 strcpy(ident, ident_value);
3616 uves_free_propertylist(&header);
3617 uves_free_string_const(&fits_descr);
3619 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3643 assure( type == F_IMA_TYPE, CPL_ERROR_UNSUPPORTED_MODE,
3645 assure(flag == 0, CPL_ERROR_UNSUPPORTED_MODE,
3648 *catfile = cpl_frameset_new();
3651 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3673 cpl_frame*
frame = NULL;
3680 frame = cpl_frame_new();
3681 cpl_frame_set_filename(frame, name);
3682 cpl_frame_set_tag(frame,
"dummy");
3683 cpl_frameset_insert(catfile, frame);
3692 while(ident[i] !=
'\0') {
3693 assure( ident[i] ==
' ', CPL_ERROR_UNSUPPORTED_MODE,
3694 "Blank ident string expected. Received '%s'",
3705 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3718 cpl_type type = CPL_TYPE_INVALID;
3721 assure( dtype == D_I4_FORMAT ||
3722 dtype == D_R8_FORMAT ||
3723 dtype == D_R4_FORMAT ||
3724 dtype == D_C_FORMAT, CPL_ERROR_UNSUPPORTED_MODE,
3725 "dtype = %d", dtype);
3727 assure( dtype == D_C_FORMAT || alen == 1, CPL_ERROR_UNSUPPORTED_MODE,
3728 "dtype = %d, alen = %d", dtype, alen);
3731 case D_I4_FORMAT: type = CPL_TYPE_INT;
break;
3732 case D_R4_FORMAT: type = CPL_TYPE_FLOAT;
break;
3733 case D_R8_FORMAT: type = CPL_TYPE_DOUBLE;
break;
3734 case D_C_FORMAT: type = CPL_TYPE_STRING;
break;
3757 case CPL_TYPE_INT:
return sizeof(int);
break;
3758 case CPL_TYPE_FLOAT:
return sizeof(float);
break;
3759 case CPL_TYPE_DOUBLE:
return sizeof(double);
break;
3760 case CPL_TYPE_STRING:
return sizeof(char);
break;
3762 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
3784 case CPL_TYPE_INT: dtype = D_I4_FORMAT;
break;
3785 case CPL_TYPE_FLOAT: dtype = D_R4_FORMAT;
break;
3786 case CPL_TYPE_DOUBLE: dtype = D_R8_FORMAT;
break;
3787 case CPL_TYPE_STRING: dtype = D_C_FORMAT;
break;
3789 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
3809 const char *name = NULL;
3813 assure(
frame_is_open(tid) && !frames[tid].is_image, CPL_ERROR_ILLEGAL_INPUT,
3814 "Table %d is not open", tid);
3816 check(
load_frame(tid),
"Could not load table %s", frames[tid].filename);
3818 assure( 1 <= column && column <= cpl_table_get_nrow(frames[tid].data.table.colnames),
3819 CPL_ERROR_ACCESS_OUT_OF_RANGE,
3820 "Illegal column number %d. Table has %" CPL_SIZE_FORMAT
" row(s)",
3821 column, cpl_table_get_nrow(frames[tid].data.table.colnames));
3823 name = cpl_table_get_string(frames[tid].data.table.colnames, COLNAME,
3847 assure( allrow == -1 || mode == F_O_MODE, CPL_ERROR_INCOMPATIBLE_INPUT,
3848 "allrow = %d, mode = %d", allrow, mode);
3850 if (mode == F_I_MODE || mode == F_IO_MODE || mode == F_O_MODE)
3855 for (i = 0; !found && i < MAX_OPEN; i++)
3862 if (mode == F_I_MODE || mode == F_IO_MODE)
3867 else if (mode == F_O_MODE)
3869 cpl_table *colnames = cpl_table_new(0);
3870 cpl_table_new_column(colnames,
3871 COLNAME, CPL_TYPE_STRING);
3874 cpl_table_new(allrow), 0, colnames);
3883 assure( found, CPL_ERROR_UNSUPPORTED_MODE,
3884 "Cannot open more than %d table files",
3889 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me");
3895 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3935 assure( colref[0] !=
':' &&
3936 colref[0] !=
'#', CPL_ERROR_UNSUPPORTED_MODE,
"Illegal column name: %s",
3939 assure(
frame_is_open(tid) && !frames[tid].is_image, CPL_ERROR_ILLEGAL_INPUT,
3940 "Table %d is not open", tid);
3942 check(
load_frame(tid),
"Could not load table %s", frames[tid].filename);
3946 for (i = 0; i < cpl_table_get_nrow(frames[tid].data.table.colnames) && !found; i++)
3948 const char *name = cpl_table_get_string(frames[tid].data.table.colnames, COLNAME, i);
3949 if (strcmp(name, colref) == 0)
3959 frames[tid].filename, colref);
3965 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
3984 assure(
frame_is_open(tid) && !frames[tid].is_image, CPL_ERROR_ILLEGAL_INPUT,
3985 "Table %d is not open", tid);
3987 check(
load_frame(tid),
"Could not load table %s", frames[tid].filename);
3989 *column = cpl_table_get_ncol(frames[tid].data.table.table) - 1;
3992 *row = frames[tid].data.table.maxrow;
3997 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4016 const char *colname;
4021 cpl_table_get_column_type(frames[tid].data.table.table,
4031 if (*dtype == D_C_FORMAT)
4043 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4058 assure(
false, CPL_ERROR_UNSUPPORTED_MODE,
"Implement me");
4074 const char *colname;
4080 cpl_table_get_column_type(frames[tid].data.table.table, colname)));
4082 check_nomsg( format = cpl_table_get_column_format(frames[tid].data.table.table, colname));
4084 strcpy(form, format);
4087 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4101 const char *colname;
4107 strcpy(label, colname);
4110 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4124 const char *colname;
4130 check_nomsg( u = cpl_table_get_column_unit(frames[tid].data.table.table, colname));
4131 assure( u != NULL, CPL_ERROR_ILLEGAL_INPUT,
"Column %s unit not set", colname);
4136 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4180 const char *form,
const char *unit,
const char *label,
4187 assure(
frame_is_open(tid) && !frames[tid].is_image, CPL_ERROR_ILLEGAL_INPUT,
4188 "Table %d is not open", tid);
4190 check(
load_frame(tid),
"Could not load table %s", frames[tid].filename);
4198 assure( !cpl_table_has_column(frames[tid].data.table.table, label),
4199 CPL_ERROR_ILLEGAL_OUTPUT,
4200 "Column %s already exists", label);
4201 cpl_table_new_column (frames[tid].data.table.table, label, type);
4202 cpl_table_set_column_format(frames[tid].data.table.table, label, form);
4203 cpl_table_set_column_unit (frames[tid].data.table.table, label, unit);
4207 case CPL_TYPE_INT : cpl_table_fill_invalid_int (frames[tid].data.table.table, label, -1);
break;
4208 case CPL_TYPE_FLOAT : cpl_table_fill_invalid_float (frames[tid].data.table.table, label, -1);
break;
4209 case CPL_TYPE_DOUBLE: cpl_table_fill_invalid_double(frames[tid].data.table.table, label, -1);
break;
4210 case CPL_TYPE_STRING:
break;
4217 *column = cpl_table_get_ncol(frames[tid].data.table.table) - 1;
4218 cpl_table_set_size (frames[tid].data.table.colnames, *column);
4222 cpl_table_set_string(frames[tid].data.table.colnames, COLNAME, *column-1, label);
4227 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4247 tcerd(cpl_type type,
int tid,
int row,
int column,
4248 void *value,
int *null)
4250 const char *colname;
4251 const char *val_str;
4262 assure( 1 <= row && row <= cpl_table_get_nrow(frames[tid].data.table.table),
4263 CPL_ERROR_ACCESS_OUT_OF_RANGE,
4264 "Cannot read row %d of %" CPL_SIZE_FORMAT
" row table",
4265 row, cpl_table_get_nrow(frames[tid].data.table.table));
4269 ((
int *)value)[0] = cpl_table_get(frames[tid].data.table.table,
4273 case CPL_TYPE_FLOAT:
4274 ((
float *)value)[0] = cpl_table_get(frames[tid].data.table.table,
4278 case CPL_TYPE_DOUBLE:
4279 ((
double *)value)[0] = cpl_table_get(frames[tid].data.table.table,
4283 case CPL_TYPE_STRING:
4284 val_str = cpl_table_get_string(frames[tid].data.table.table,
4286 if (val_str == NULL)
4288 if (null != NULL) *null = 1;
4289 ((
char *)value)[0] =
'\0';
4293 if (null != NULL) *null = 0;
4294 strcpy((
char *)value, val_str);
4303 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4316 return tcerd(CPL_TYPE_STRING, tid, row, column, values, null);
4326 return tcerd(CPL_TYPE_INT, tid, row, column, value, null);
4336 return tcerd(CPL_TYPE_FLOAT, tid, row, column, value, null);
4345 double *value,
int *null)
4347 return tcerd(CPL_TYPE_DOUBLE, tid, row, column, value, null);
4363 static int tcewr(cpl_type type,
int tid,
int row,
int column,
const void *value)
4365 const char *colname;
4376 assure( row <= cpl_table_get_nrow(frames[tid].data.table.table),
4377 CPL_ERROR_ACCESS_OUT_OF_RANGE,
4378 "Cannot write row %d from %" CPL_SIZE_FORMAT
" row table",
4379 row, cpl_table_get_nrow(frames[tid].data.table.table));
4381 if (row > frames[tid].data.table.maxrow) {
4382 frames[tid].data.table.maxrow = row;
4387 cpl_table_set(frames[tid].data.table.table, colname, row - 1, ((
const int *)value)[0]);
4389 case CPL_TYPE_FLOAT:
4390 cpl_table_set(frames[tid].data.table.table, colname, row - 1, ((
const float *)value)[0]);
4392 case CPL_TYPE_DOUBLE:
4393 cpl_table_set(frames[tid].data.table.table, colname, row - 1, ((
const double *)value)[0]);
4395 case CPL_TYPE_STRING:
4396 cpl_table_set_string(frames[tid].data.table.table, colname, row - 1, (
const char *)value);
4404 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4418 return tcewr(CPL_TYPE_STRING, tid, row, column, value);
4428 return tcewr(CPL_TYPE_DOUBLE, tid, row, column, value);
4438 return tcewr(CPL_TYPE_INT, tid, row, column, value);
4447 return tcewr(CPL_TYPE_FLOAT, tid, row, column, value);
4456 tcard(cpl_type type,
int tid,
int row,
int column,
4457 int index,
int items,
void *value)
4459 assure( index == 1 && (
4460 type == CPL_TYPE_STRING ||
4462 CPL_ERROR_UNSUPPORTED_MODE,
4463 "index, items = %d, %d", index, items);
4465 return tcerd(type, tid, row, column, value, NULL);
4468 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4486 return tcard(CPL_TYPE_STRING, tid, row, col, index, items, value);
4503 return tcard(CPL_TYPE_DOUBLE, tid, row, col, index, items, value);
4513 return tcard(CPL_TYPE_INT, tid, row, col, index, items, value);
4523 return tcard(CPL_TYPE_FLOAT, tid, row, col, index, items, value);
4533 tcawr(cpl_type type,
int tid,
int row,
int col,
int index,
int items,
const void *value)
4535 assure( index == 1 && (
4536 type == CPL_TYPE_STRING ||
4538 CPL_ERROR_UNSUPPORTED_MODE,
4539 "index, items = %d, %d", index, items);
4541 return tcewr(type, tid, row, col, value);
4544 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4561 return tcawr(CPL_TYPE_STRING, tid, row, col, index, items, value);
4577 return tcawr(CPL_TYPE_DOUBLE, tid, row, col, index, items, value);
4586 return tcawr(CPL_TYPE_INT, tid, row, col, index, items, value);
4596 return tcawr(CPL_TYPE_FLOAT, tid, row, col, index, items, value);
4612 assure(
frame_is_open(tid) && !frames[tid].is_image, CPL_ERROR_ILLEGAL_INPUT,
4613 "Table %d is not open", tid);
4615 check(
load_frame(tid),
"Could not load table %s", frames[tid].filename);
4617 assure( 1 <= row && row <= cpl_table_get_nrow(frames[tid].data.table.table),
4618 CPL_ERROR_ACCESS_OUT_OF_RANGE,
4619 "Cannot read row %d of %" CPL_SIZE_FORMAT
" row table %s", row,
4620 cpl_table_get_nrow(frames[tid].data.table.table),
4621 frames[tid].filename);
4623 *value = cpl_table_get_int(frames[tid].data.table.table,
4628 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;
4644 assure(
frame_is_open(tid) && !frames[tid].is_image, CPL_ERROR_ILLEGAL_INPUT,
4645 "Table %d is not open", tid);
4647 check(
load_frame(tid),
"Could not load table %s", frames[tid].filename);
4649 assure( 1 <= row && row <= cpl_table_get_nrow(frames[tid].data.table.table),
4650 CPL_ERROR_ACCESS_OUT_OF_RANGE,
4651 "Cannot write to row %d of %" CPL_SIZE_FORMAT
" row table %s", row,
4652 cpl_table_get_nrow(frames[tid].data.table.table),
4653 frames[tid].filename);
4655 cpl_table_set_int(frames[tid].data.table.table,
"Select", row - 1, *value);
4658 if (row > frames[tid].data.table.maxrow) {
4659 frames[tid].data.table.maxrow = row;
4662 return (cpl_error_get_code() == CPL_ERROR_NONE) ? 0 : 1;