36#include "irplib_sdp_spectrum.h"
38#ifdef IRPLIB_USE_FITS_UPDATE_CHECKSUM
47#ifndef cpl_error_set_regex
49cpl_error_code cpl_error_set_regex_macro(
const char *,
56 ...) CPL_ATTR_PRINTF(7, 8);
58#define cpl_error_set_regex(code, regcode, preg, ...) \
59 cpl_error_set_regex_macro(cpl_func, code, regcode, preg, __FILE__, \
60 __LINE__, __VA_ARGS__)
65#define KEY_ARCFILE "ARCFILE"
66#define KEY_ORIGFILE "ORIGFILE"
68#define KEY_RA_COMMENT "[deg] Spectroscopic target position (J2000)"
70#define KEY_DEC_COMMENT "[deg] Spectroscopic target position (J2000)"
71#define KEY_EXPTIME "EXPTIME"
72#define KEY_EXPTIME_COMMENT "[s] Total integration time per pixel"
73#define KEY_TEXPTIME "TEXPTIME"
74#define KEY_TEXPTIME_COMMENT "[s] Total integration time of all exposures"
75#define KEY_TIMESYS "TIMESYS"
76#define KEY_TIMESYS_COMMENT "Time system used"
77#define KEY_MJDOBS "MJD-OBS"
78#define KEY_MJDOBS_COMMENT "[d] Start of observations (days)"
79#define KEY_MJDEND "MJD-END"
80#define KEY_MJDEND_COMMENT "[d] End of observations (days)"
81#define KEY_PRODLVL "PRODLVL"
82#define KEY_PRODLVL_VALUE 2
83#define KEY_PRODLVL_COMMENT \
84 "Phase 3 product level: 1-raw, 2-science grade, 3-advanced"
85#define KEY_PROCSOFT "PROCSOFT"
86#define KEY_PROCSOFT_COMMENT "ESO pipeline version"
87#define KEY_PRODCATG "PRODCATG"
88#define KEY_PRODCATG_COMMENT "Data product category"
89#define KEY_ORIGIN "ORIGIN"
90#define KEY_ORIGIN_VALUE "ESO"
91#define KEY_ORIGIN_COMMENT "European Southern Observatory"
92#define KEY_EXT_OBJ "EXT_OBJ"
93#define KEY_EXT_OBJ_COMMENT "TRUE if extended"
94#define KEY_DISPELEM "DISPELEM"
95#define KEY_DISPELEM_COMMENT "Dispersive element name"
96#define KEY_SPECSYS "SPECSYS"
97#define KEY_SPECSYS_VALUE "TOPOCENT"
98#define KEY_SPECSYS_COMMENT "Reference frame for spectral coordinates"
99#define KEY_PROG_ID "PROG_ID"
100#define KEY_PROG_ID_COMMENT "ESO programme identification"
101#define KEY_OBID "OBID"
102#define KEY_OBID_COMMENT "Observation block ID"
103#define KEY_M_EPOCH "M_EPOCH"
104#define KEY_M_EPOCH_COMMENT "TRUE if resulting from multiple epochs"
105#define KEY_OBSTECH "OBSTECH"
106#define KEY_OBSTECH_COMMENT "Technique for observation"
107#define KEY_FLUXCAL "FLUXCAL"
108#define KEY_FLUXCAL_COMMENT \
109 "Type of flux calibration (ABSOLUTE or UNCALIBRATED)"
110#define KEY_CONTNORM "CONTNORM"
111#define KEY_CONTNORM_COMMENT "TRUE if normalised to the continuum"
112#define KEY_WAVELMIN "WAVELMIN"
113#define KEY_WAVELMIN_COMMENT "[nm] Minimum wavelength"
114#define KEY_WAVELMAX "WAVELMAX"
115#define KEY_WAVELMAX_COMMENT "[nm] Maximum wavelength"
116#define KEY_SPEC_BIN "SPEC_BIN"
117#define KEY_SPEC_BIN_COMMENT "[nm] Wavelength bin size"
118#define KEY_TOT_FLUX "TOT_FLUX"
119#define KEY_TOT_FLUX_COMMENT \
120 "TRUE if photometric conditions and all source flux is captured"
121#define KEY_FLUXERR "FLUXERR"
122#define KEY_FLUXERR_VALUE -2
123#define KEY_FLUXERR_COMMENT "Uncertainty in flux scale (%)"
124#define KEY_REFERENC "REFERENC"
125#define KEY_REFERENC_VALUE " "
126#define KEY_REFERENC_COMMENT "Reference publication"
127#define KEY_SPEC_RES "SPEC_RES"
128#define KEY_SPEC_RES_COMMENT "Reference spectral resolving power"
129#define KEY_SPEC_ERR "SPEC_ERR"
130#define KEY_SPEC_ERR_COMMENT "[nm] Statistical error in spectral coordinate"
131#define KEY_SPEC_SYE "SPEC_SYE"
132#define KEY_SPEC_SYE_COMMENT "[nm] Systematic error in spectral coordinate"
133#define KEY_LAMNLIN "LAMNLIN"
134#define KEY_LAMNLIN_COMMENT "Number of arc lines used for the wavel. solution"
135#define KEY_LAMRMS "LAMRMS"
136#define KEY_LAMRMS_COMMENT "[nm] RMS of the residuals of the wavel. solution"
137#define KEY_GAIN "GAIN"
138#define KEY_GAIN_COMMENT "Conversion factor (e-/ADU) electrons per data unit"
139#define KEY_DETRON "DETRON"
140#define KEY_DETRON_COMMENT "Readout noise per output (e-)"
141#define KEY_EFFRON "EFFRON"
142#define KEY_EFFRON_COMMENT "Median effective readout noise (e-)"
144#define KEY_SNR_COMMENT "Median signal to noise ratio per order"
145#define KEY_NCOMBINE "NCOMBINE"
146#define KEY_NCOMBINE_COMMENT "No. of combined raw science data files"
147#define KEY_PROV "PROV"
148#define KEY_PROV_COMMENT "Originating raw science file"
149#define KEY_ASSON "ASSON"
150#define KEY_ASSON_COMMENT "Associated file name"
151#define KEY_ASSOC "ASSOC"
152#define KEY_ASSOC_COMMENT "Associated file category"
153#define KEY_ASSOM "ASSOM"
154#define KEY_ASSOM_COMMENT "Associated file md5sum"
155#define KEY_VOCLASS "VOCLASS"
156#define KEY_VOCLASS_VALUE "SPECTRUM V2.0"
157#define KEY_VOCLASS_COMMENT "VO Data Model"
158#define KEY_VOPUB "VOPUB"
159#define KEY_VOPUB_VALUE "ESO/SAF"
160#define KEY_VOPUB_COMMENT "VO Publishing Authority"
161#define KEY_TITLE "TITLE"
162#define KEY_TITLE_COMMENT "Dataset title"
163#define KEY_OBJECT "OBJECT"
164#define KEY_OBJECT_COMMENT "Target designation"
165#define KEY_OBJECT_PHDU_COMMENT "Original target."
166#define KEY_APERTURE "APERTURE"
167#define KEY_APERTURE_COMMENT "[deg] Aperture diameter"
168#define KEY_TELAPSE "TELAPSE"
169#define KEY_TELAPSE_COMMENT "[s] Total elapsed time"
170#define KEY_TMID "TMID"
171#define KEY_TMID_COMMENT "[d] MJD mid exposure"
172#define KEY_SPEC_VAL "SPEC_VAL"
173#define KEY_SPEC_VAL_COMMENT "[nm] Mean wavelength"
174#define KEY_SPEC_BW "SPEC_BW"
175#define KEY_SPEC_BW_COMMENT "[nm] Bandpass width = Wmax - Wmin"
176#define KEY_TDMIN(n) "TDMIN" #n
177#define KEY_TDMIN1_COMMENT "Start in spectral coordinate"
178#define KEY_TDMAX(n) "TDMAX" #n
179#define KEY_TDMAX1_COMMENT "Stop in spectral coordinate"
180#define KEY_TUTYP "TUTYP"
181#define KEY_TUTYP_COMMENT "IVOA data model element for field "
182#define KEY_TUCD "TUCD"
183#define KEY_TUCD_COMMENT "UCD for field "
184#define KEY_TCOMM "TCOMM"
185#define KEY_TCOMM_COMMENT "Description for field "
186#define KEY_NELEM "NELEM"
187#define KEY_NELEM_COMMENT "Length of the data arrays"
188#define KEY_EXTNAME "EXTNAME"
189#define KEY_EXTNAME_VALUE "SPECTRUM"
190#define KEY_EXTNAME_COMMENT "Extension name"
191#define KEY_INHERIT "INHERIT"
192#define KEY_INHERIT_VALUE CPL_TRUE
193#define KEY_INHERIT_COMMENT "Primary header keywords are inherited"
196#define ALL_KEYS_REGEXP \
197 "^(" KEY_RA "|" KEY_DEC "|" KEY_EXPTIME "|" KEY_TEXPTIME "|" KEY_TIMESYS \
198 "|" KEY_MJDOBS "|" KEY_MJDEND "|" KEY_PRODLVL "|" KEY_PROCSOFT \
199 "|" KEY_PRODCATG "|" KEY_ORIGIN "|" KEY_EXT_OBJ "|" KEY_DISPELEM \
200 "|" KEY_SPECSYS "|" KEY_PROG_ID "|" KEY_OBID "[0-9]+|" KEY_M_EPOCH \
201 "|" KEY_OBSTECH "|" KEY_FLUXCAL "|" KEY_CONTNORM "|" KEY_WAVELMIN \
202 "|" KEY_WAVELMAX "|" KEY_SPEC_BIN "|" KEY_TOT_FLUX "|" KEY_FLUXERR \
203 "|" KEY_REFERENC "|" KEY_SPEC_RES "|" KEY_SPEC_ERR "|" KEY_SPEC_SYE \
204 "|" KEY_LAMNLIN "|" KEY_LAMRMS "|" KEY_GAIN "|" KEY_DETRON "|" KEY_EFFRON \
205 "|" KEY_SNR "|" KEY_NCOMBINE "|" KEY_PROV "[0-9]+|" KEY_ASSON \
206 "[0-9]+|" KEY_ASSOC "[0-9]+|" KEY_ASSOM "[0-9]+|" KEY_VOCLASS \
207 "|" KEY_VOPUB "|" KEY_TITLE "|" KEY_OBJECT "|" KEY_APERTURE \
208 "|" KEY_TELAPSE "|" KEY_TMID "|" KEY_SPEC_VAL "|" KEY_SPEC_BW \
209 "|" KEY_TDMIN(1) "|" KEY_TDMAX(1) "|" KEY_TUTYP "[0-9]+|" KEY_TUCD \
210 "[0-9]+|" KEY_TCOMM "[0-9]+|" KEY_NELEM \
211 "|" KEY_EXTNAME "|" KEY_INHERIT ")$"
215#define PRIMARY_HDU_KEYS_REGEXP \
216 "^(" KEY_RA "|" KEY_DEC "|" KEY_EXPTIME "|" KEY_TEXPTIME "|" KEY_TIMESYS \
217 "|" KEY_MJDOBS "|" KEY_MJDEND "|" KEY_PRODLVL "|" KEY_PROCSOFT \
218 "|" KEY_PRODCATG "|" KEY_ORIGIN "|" KEY_EXT_OBJ "|" KEY_DISPELEM \
219 "|" KEY_SPECSYS "|" KEY_PROG_ID "|" KEY_OBID "[0-9]+|" KEY_M_EPOCH \
220 "|" KEY_OBSTECH "|" KEY_FLUXCAL "|" KEY_CONTNORM "|" KEY_WAVELMIN \
221 "|" KEY_WAVELMAX "|" KEY_SPEC_BIN "|" KEY_TOT_FLUX "|" KEY_FLUXERR \
222 "|" KEY_REFERENC "|" KEY_SPEC_RES "|" KEY_SPEC_ERR "|" KEY_SPEC_SYE \
223 "|" KEY_LAMNLIN "|" KEY_LAMRMS "|" KEY_GAIN "|" KEY_DETRON "|" KEY_EFFRON \
224 "|" KEY_SNR "|" KEY_NCOMBINE "|" KEY_PROV "[0-9]+|" KEY_ASSON \
225 "[0-9]+|" KEY_ASSOC "[0-9]+|" KEY_ASSOM "[0-9]+|" KEY_OBJECT ")$"
229#define EXTENSION_HDU_KEYS_REGEXP \
230 "^(" KEY_RA "|" KEY_DEC "|" KEY_VOCLASS "|" KEY_VOPUB "|" KEY_TITLE \
231 "|" KEY_OBJECT "|" KEY_APERTURE "|" KEY_TELAPSE "|" KEY_TMID \
232 "|" KEY_SPEC_VAL "|" KEY_SPEC_BW \
233 "|" KEY_TDMIN(1) "|" KEY_TDMAX(1) "|" KEY_TUTYP "[0-9]+|" KEY_TUCD \
234 "[0-9]+|" KEY_TCOMM "[0-9]+|" KEY_NELEM \
235 "|" KEY_EXTNAME "|" KEY_INHERIT ")$"
238#define GET_SET_METHODS(param, keyname, type, rettype, defaultval, comment) \
239 rettype irplib_sdp_spectrum_get_##param(const irplib_sdp_spectrum *self) \
241 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, defaultval); \
242 assert(self->proplist != NULL); \
243 if (cpl_propertylist_has(self->proplist, keyname)) { \
244 return cpl_propertylist_get_##type(self->proplist, keyname); \
250 cpl_error_code irplib_sdp_spectrum_reset_##param( \
251 irplib_sdp_spectrum *self) \
253 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT); \
254 assert(self->proplist != NULL); \
255 (void)cpl_propertylist_erase(self->proplist, keyname); \
256 return CPL_ERROR_NONE; \
258 cpl_error_code irplib_sdp_spectrum_set_##param(irplib_sdp_spectrum *self, \
261 cpl_error_code error; \
262 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT); \
263 assert(self->proplist != NULL); \
264 if (cpl_propertylist_has(self->proplist, keyname)) { \
266 cpl_propertylist_set_##type(self->proplist, keyname, value); \
269 error = cpl_propertylist_append_##type(self->proplist, keyname, \
272 error = cpl_propertylist_set_comment(self->proplist, keyname, \
277 cpl_errorstate prestate = cpl_errorstate_get(); \
278 (void)cpl_propertylist_erase(self->proplist, keyname); \
279 cpl_errorstate_set(prestate); \
285 cpl_error_code irplib_sdp_spectrum_copy_##param( \
286 irplib_sdp_spectrum *self, const cpl_propertylist *plist, \
290 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT); \
291 assert(self->proplist != NULL); \
292 if (cpl_propertylist_has(plist, name)) { \
293 cpl_errorstate prestate = cpl_errorstate_get(); \
294 rettype value = cpl_propertylist_get_##type(plist, name); \
295 if (cpl_errorstate_is_equal(prestate)) { \
296 return irplib_sdp_spectrum_set_##param(self, value); \
299 return cpl_error_set_message( \
300 cpl_func, cpl_error_get_code(), \
301 "Could not set '%s'. Likely the source '%s' keyword has a" \
302 " different format or type.", \
307 return cpl_error_set_message( \
308 cpl_func, CPL_ERROR_DATA_NOT_FOUND, \
309 "Could not set '%s' since the '%s' keyword was not found.", \
314#define GET_SET_ARRAY_METHODS(param, keyname, type, rettype, defaultval, \
316 rettype irplib_sdp_spectrum_get_##param(const irplib_sdp_spectrum *self, \
320 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, defaultval); \
321 assert(self->proplist != NULL); \
322 name = cpl_sprintf("%s%" CPL_SIZE_FORMAT, keyname, index); \
323 rettype result = defaultval; \
324 if (cpl_propertylist_has(self->proplist, name)) { \
325 result = cpl_propertylist_get_##type(self->proplist, name); \
330 cpl_error_code irplib_sdp_spectrum_reset_##param( \
331 irplib_sdp_spectrum *self, cpl_size index) \
334 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT); \
335 assert(self->proplist != NULL); \
336 name = cpl_sprintf("%s%" CPL_SIZE_FORMAT, keyname, index); \
337 (void)cpl_propertylist_erase(self->proplist, name); \
339 return CPL_ERROR_NONE; \
341 cpl_error_code irplib_sdp_spectrum_set_##param(irplib_sdp_spectrum *self, \
345 cpl_error_code error; \
347 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT); \
348 assert(self->proplist != NULL); \
349 name = cpl_sprintf("%s%" CPL_SIZE_FORMAT, keyname, index); \
350 if (cpl_propertylist_has(self->proplist, name)) { \
351 error = cpl_propertylist_set_##type(self->proplist, name, value); \
355 cpl_propertylist_append_##type(self->proplist, name, value); \
357 error = cpl_propertylist_set_comment(self->proplist, name, \
362 cpl_errorstate prestate = cpl_errorstate_get(); \
363 (void)cpl_propertylist_erase(self->proplist, name); \
364 cpl_errorstate_set(prestate); \
371 cpl_error_code irplib_sdp_spectrum_copy_##param( \
372 irplib_sdp_spectrum *self, cpl_size index, \
373 const cpl_propertylist *plist, const char *name) \
376 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT); \
377 assert(self->proplist != NULL); \
378 if (cpl_propertylist_has(plist, name)) { \
379 cpl_errorstate prestate = cpl_errorstate_get(); \
380 rettype value = cpl_propertylist_get_##type(plist, name); \
381 if (cpl_errorstate_is_equal(prestate)) { \
382 return irplib_sdp_spectrum_set_##param(self, index, value); \
385 return cpl_error_set_message( \
386 cpl_func, cpl_error_get_code(), \
387 "Could not set '%s%" CPL_SIZE_FORMAT \
388 "'. Likely the source" \
389 " '%s' keyword has a different format or type.", \
390 keyname, index, name); \
394 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND, \
395 "Could not set '%s%" CPL_SIZE_FORMAT \
397 " keyword was not found.", \
398 keyname, index, name); \
403#define GET_SET_METHODS_TYPE_BOOL(param, keyname, comment) \
404 GET_SET_METHODS(param, keyname, bool, cpl_boolean, CPL_FALSE, comment)
406#define GET_SET_METHODS_TYPE_DOUBLE(param, keyname, comment) \
407 GET_SET_METHODS(param, keyname, double, double, NAN, comment)
409#define GET_SET_METHODS_TYPE_INT(param, keyname, comment) \
410 GET_SET_METHODS(param, keyname, int, int, -1, comment)
412#define GET_SET_METHODS_TYPE_STRING(param, keyname, comment) \
413 GET_SET_METHODS(param, keyname, string, const char *, NULL, comment)
415#define GET_SET_ARRAY_METHODS_TYPE_INT(param, keyname, comment) \
416 GET_SET_ARRAY_METHODS(param, keyname, int, int, -1, comment)
418#define GET_SET_ARRAY_METHODS_TYPE_STRING(param, keyname, comment) \
419 GET_SET_ARRAY_METHODS(param, keyname, string, const char *, NULL, comment)
422#define IRPLIB_TYPE_NELEM CPL_TYPE_LONG_LONG | CPL_TYPE_UNSPECIFIED
443struct _irplib_sdp_spectrum_
449 cpl_propertylist *proplist;
460typedef struct _irplib_keyword_record_
472 cpl_boolean is_array_key;
474} irplib_keyword_record;
481_irplib_property_equal(
const cpl_property *a,
const cpl_property *b);
484_irplib_array_equal(
const cpl_array *a,
const cpl_array *b, cpl_size n);
486static cpl_boolean _irplib_table_column_equal(
const cpl_table *a,
489 cpl_boolean only_intersect);
494 const cpl_table *table,
495 const char *from_name);
508 const char *keyword);
515 const char *comment);
522_irplib_make_regexp(
const cpl_propertylist *plist,
const char *extra);
526_irplib_keyword_table_is_sorted(
const irplib_keyword_record *table,
530static const irplib_keyword_record *
531_irplib_sdp_spectrum_get_keyword_record(
const char *name);
543irplib_sdp_spectrum_new(
void)
547 obj->proplist = cpl_propertylist_new();
548 obj->table = cpl_table_new(1);
562 cpl_ensure(other != NULL, CPL_ERROR_NULL_INPUT, NULL);
564 assert(other->proplist != NULL);
565 assert(other->table != NULL);
568 obj->nelem = other->nelem;
569 obj->proplist = cpl_propertylist_duplicate(other->proplist);
570 obj->table = cpl_table_duplicate(other->table);
583 assert(self->proplist != NULL);
584 assert(self->table != NULL);
585 cpl_propertylist_delete(self->proplist);
586 cpl_table_delete(self->table);
598_irplib_property_equal(
const cpl_property *a,
const cpl_property *b)
608 type = cpl_property_get_type(a);
609 if (cpl_property_get_type(b) != type)
616 cpl_property_get_char(a) != cpl_property_get_char(b);
620 cpl_property_get_bool(a) != cpl_property_get_bool(b);
624 cpl_property_get_int(a) != cpl_property_get_int(b);
628 cpl_property_get_long(a) != cpl_property_get_long(b);
630 case CPL_TYPE_LONG_LONG:
632 cpl_property_get_long_long(a) != cpl_property_get_long_long(b);
636 cpl_property_get_float(a) != cpl_property_get_float(b);
638 case CPL_TYPE_DOUBLE:
640 cpl_property_get_double(a) != cpl_property_get_double(b);
642 case CPL_TYPE_STRING:
643 sa = cpl_property_get_string(a);
644 sb = cpl_property_get_string(b);
645 if (sa == NULL && sb == NULL) {
648 else if (sa != NULL && sb != NULL) {
649 value_not_equal = strcmp(sa, sb) != 0;
656 case CPL_TYPE_FLOAT_COMPLEX:
657 value_not_equal = cpl_property_get_float_complex(a) !=
658 cpl_property_get_float_complex(b);
660 case CPL_TYPE_DOUBLE_COMPLEX:
661 value_not_equal = cpl_property_get_double_complex(a) !=
662 cpl_property_get_double_complex(b);
666 cpl_error_set_message(
667 cpl_func, CPL_ERROR_INVALID_TYPE,
668 "Unsupported data type found in property '%s'.",
669 cpl_property_get_name(a));
693_irplib_array_equal(
const cpl_array *a,
const cpl_array *b, cpl_size n)
699 assert(n <= cpl_array_get_size(a));
700 assert(n <= cpl_array_get_size(b));
702 type = cpl_array_get_type(a);
703 if (type != cpl_array_get_type(b))
706 if (type == CPL_TYPE_STRING) {
709 const char **stra = cpl_array_get_data_string_const(a);
710 const char **strb = cpl_array_get_data_string_const(b);
711 cpl_error_ensure(stra != NULL && strb != NULL, cpl_error_get_code(),
712 return CPL_FALSE,
"Failed to get %s data for array.",
713 cpl_type_get_name(type));
714 for (i = 0; i < n; ++i) {
715 if (stra[i] == NULL && strb[i] == NULL)
717 if (stra[i] == NULL || strb[i] == NULL)
719 if (strcmp(stra[i], strb[i]) != 0)
731 va = cpl_array_get_data_int_const(a);
732 vb = cpl_array_get_data_int_const(b);
734 case CPL_TYPE_LONG_LONG:
735 size =
sizeof(
long long);
736 va = cpl_array_get_data_long_long_const(a);
737 vb = cpl_array_get_data_long_long_const(b);
740 size =
sizeof(float);
741 va = cpl_array_get_data_float_const(a);
742 vb = cpl_array_get_data_float_const(b);
744 case CPL_TYPE_DOUBLE:
745 size =
sizeof(double);
746 va = cpl_array_get_data_double_const(a);
747 vb = cpl_array_get_data_double_const(b);
750 case CPL_TYPE_FLOAT_COMPLEX:
751 size =
sizeof(_Complex float);
752 va = cpl_array_get_data_float_complex_const(a);
753 vb = cpl_array_get_data_float_complex_const(b);
755 case CPL_TYPE_DOUBLE_COMPLEX:
756 size =
sizeof(_Complex double);
757 va = cpl_array_get_data_double_complex_const(a);
758 vb = cpl_array_get_data_double_complex_const(b);
762 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
763 "Unsupported data type.");
766 cpl_error_ensure(va != NULL && vb != NULL, cpl_error_get_code(),
767 return CPL_FALSE,
"Failed to get %s data for array.",
768 cpl_type_get_name(type));
769 for (i = 0; i < n; ++i) {
770 int valid_a = cpl_array_is_valid(a, i);
771 int valid_b = cpl_array_is_valid(b, i);
772 if (!valid_a && !valid_b)
774 if (!valid_a || !valid_b)
776 const void *vai = (
const char *)va + (size * i);
777 const void *vbi = (
const char *)vb + (size * i);
778 if (memcmp(vai, vbi, size) != 0)
802_irplib_table_column_equal(
const cpl_table *a,
805 cpl_boolean only_intersect)
808 cpl_size nrows, na, nb, i;
814 nrows = cpl_table_get_nrow(a);
815 if (only_intersect) {
816 cpl_size nrows2 = cpl_table_get_nrow(b);
821 if (cpl_table_get_nrow(b) != nrows)
826 type = cpl_table_get_column_type(a, name);
827 if (cpl_table_get_column_type(b, name) != type)
831 na = cpl_table_get_column_dimensions(a, name);
832 nb = cpl_table_get_column_dimensions(b, name);
837 sa = cpl_table_get_column_unit(a, name);
838 sb = cpl_table_get_column_unit(b, name);
839 cpl_error_ensure(sa != NULL && sb != NULL, cpl_error_get_code(),
841 "Failed to get unit strings for column '%s'.", name);
842 if (strcmp(sa, sb) != 0)
847 if (type & CPL_TYPE_POINTER) {
848 cpl_errorstate prestate;
850 const cpl_array **va = cpl_table_get_data_array_const(a, name);
851 const cpl_array **vb = cpl_table_get_data_array_const(b, name);
852 cpl_error_ensure(va != NULL && vb != NULL, cpl_error_get_code(),
854 "Failed to get %s data for column '%s'.",
855 cpl_type_get_name(type), name);
856 if (only_intersect) {
857 for (i = 0; i < nrows; ++i) {
860 if (va[i] == NULL && vb[i] == NULL)
862 if (va[i] == NULL || vb[i] == NULL)
864 prestate = cpl_errorstate_get();
865 cpl_size n1 = cpl_array_get_size(va[i]);
866 cpl_size n2 = cpl_array_get_size(vb[i]);
867 cpl_size n = n1 < n2 ? n1 : n2;
868 if (!_irplib_array_equal(va[i], vb[i], n))
871 cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
873 "Failed when trying to match %s data for column '%s'.",
874 cpl_type_get_name(type), name);
878 for (i = 0; i < nrows; ++i) {
881 if (va[i] == NULL && vb[i] == NULL)
883 if (va[i] == NULL || vb[i] == NULL)
885 prestate = cpl_errorstate_get();
886 cpl_size n = cpl_array_get_size(va[i]);
887 if (n != cpl_array_get_size(vb[i]))
889 if (!_irplib_array_equal(va[i], vb[i], n))
892 cpl_errorstate_is_equal(prestate), cpl_error_get_code(),
894 "Failed when trying to match %s data for column '%s'.",
895 cpl_type_get_name(type), name);
899 else if (type == CPL_TYPE_STRING) {
901 const char **va = cpl_table_get_data_string_const(a, name);
902 const char **vb = cpl_table_get_data_string_const(b, name);
903 cpl_error_ensure(va != NULL && vb != NULL, cpl_error_get_code(),
905 "Failed to get %s data for column '%s'.",
906 cpl_type_get_name(type), name);
907 if (only_intersect) {
908 for (i = 0; i < nrows; ++i) {
909 if (va[i] == NULL && vb[i] == NULL)
911 if (va[i] == NULL || vb[i] == NULL)
913 size_t n1 = strlen(va[i]);
914 size_t n2 = strlen(vb[i]);
915 size_t n = n1 < n2 ? n1 : n2;
916 if (strncmp(va[i], vb[i], (cpl_size)n) != 0)
921 for (i = 0; i < nrows; ++i) {
922 if (va[i] == NULL && vb[i] == NULL)
924 if (va[i] == NULL || vb[i] == NULL)
926 if (strcmp(va[i], vb[i]) != 0)
939 va = cpl_table_get_data_int_const(a, name);
940 vb = cpl_table_get_data_int_const(b, name);
942 case CPL_TYPE_LONG_LONG:
943 size =
sizeof(
long long);
944 va = cpl_table_get_data_long_long_const(a, name);
945 vb = cpl_table_get_data_long_long_const(b, name);
948 size =
sizeof(float);
949 va = cpl_table_get_data_float_const(a, name);
950 vb = cpl_table_get_data_float_const(b, name);
952 case CPL_TYPE_DOUBLE:
953 size =
sizeof(double);
954 va = cpl_table_get_data_double_const(a, name);
955 vb = cpl_table_get_data_double_const(b, name);
958 case CPL_TYPE_FLOAT_COMPLEX:
959 size =
sizeof(_Complex float);
960 va = cpl_table_get_data_float_complex_const(a, name);
961 vb = cpl_table_get_data_float_complex_const(b, name);
963 case CPL_TYPE_DOUBLE_COMPLEX:
964 size =
sizeof(_Complex double);
965 va = cpl_table_get_data_double_complex_const(a, name);
966 vb = cpl_table_get_data_double_complex_const(b, name);
970 cpl_error_set_message(
971 cpl_func, CPL_ERROR_INVALID_TYPE,
972 "Unsupported data type found in column '%s'.", name);
975 cpl_error_ensure(va != NULL && vb != NULL, cpl_error_get_code(),
977 "Failed to get %s data for column '%s'.",
978 cpl_type_get_name(type), name);
979 for (i = 0; i < nrows; ++i) {
980 int valid_a = cpl_table_is_valid(a, name, i);
981 int valid_b = cpl_table_is_valid(b, name, i);
982 if (!valid_a && !valid_b)
984 if (!valid_a || !valid_b)
986 const void *vai = (
const char *)va + (size * i);
987 const void *vbi = (
const char *)vb + (size * i);
988 if (memcmp(vai, vbi, size) != 0)
1017 cpl_boolean only_intersect)
1019 cpl_errorstate prestate;
1021 cpl_boolean no_match = CPL_FALSE;
1024 const cpl_property *pa, *pb;
1026 cpl_ensure(a != NULL && b != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
1028 assert(a->proplist != NULL);
1029 assert(a->table != NULL);
1030 assert(b->proplist != NULL);
1031 assert(b->table != NULL);
1033 na = cpl_propertylist_get_size(a->proplist);
1035 if (only_intersect) {
1038 for (i = 0; i < na; ++i) {
1039 pa = cpl_propertylist_get_const(a->proplist, i);
1041 pa != NULL, cpl_error_get_code(),
return CPL_FALSE,
1042 "Failed to get property structure %" CPL_SIZE_FORMAT
".", i);
1043 name = cpl_property_get_name(pa);
1045 name != NULL, cpl_error_get_code(),
return CPL_FALSE,
1046 "Failed to get the name for property %" CPL_SIZE_FORMAT
".", i);
1047 pb = cpl_propertylist_get_property_const(b->proplist, name);
1049 prestate = cpl_errorstate_get();
1050 if (!_irplib_property_equal(pa, pb))
1052 if (!cpl_errorstate_is_equal(prestate))
1059 prestate = cpl_errorstate_get();
1060 na = cpl_table_get_ncol(a->table);
1061 names = cpl_table_get_column_names(a->table);
1062 for (i = 0; i < na; ++i) {
1063 name = cpl_array_get_string(names, i);
1065 name != NULL, cpl_error_get_code(),
break,
1066 "Failed to get the name for column %" CPL_SIZE_FORMAT
".", i);
1067 if (cpl_table_has_column(b->table, name)) {
1068 if (!_irplib_table_column_equal(a->table, b->table, name,
1070 no_match = CPL_TRUE;
1075 cpl_array_delete(names);
1077 if (no_match || !cpl_errorstate_is_equal(prestate))
1082 if (a->nelem != b->nelem)
1086 nb = cpl_propertylist_get_size(b->proplist);
1089 for (i = 0; i < na; ++i) {
1090 pa = cpl_propertylist_get_const(a->proplist, i);
1092 pa != NULL, cpl_error_get_code(),
return CPL_FALSE,
1093 "Failed to get property structure %" CPL_SIZE_FORMAT
".", i);
1094 name = cpl_property_get_name(pa);
1096 name != NULL, cpl_error_get_code(),
return CPL_FALSE,
1097 "Failed to get the name for property %" CPL_SIZE_FORMAT
".", i);
1098 pb = cpl_propertylist_get_property_const(b->proplist, name);
1101 prestate = cpl_errorstate_get();
1102 if (!_irplib_property_equal(pa, pb))
1104 if (!cpl_errorstate_is_equal(prestate))
1109 prestate = cpl_errorstate_get();
1110 na = cpl_table_get_ncol(a->table);
1111 nb = cpl_table_get_ncol(b->table);
1114 names = cpl_table_get_column_names(a->table);
1115 for (i = 0; i < na; ++i) {
1116 name = cpl_array_get_string(names, i);
1118 name != NULL, cpl_error_get_code(),
break,
1119 "Failed to get the name for column %" CPL_SIZE_FORMAT
".", i);
1120 if (!cpl_table_has_column(b->table, name) ||
1121 !_irplib_table_column_equal(a->table, b->table, name,
1123 no_match = CPL_TRUE;
1127 cpl_array_delete(names);
1129 if (no_match || !cpl_errorstate_is_equal(prestate))
1147 cpl_error_code error;
1148 cpl_size result = 0;
1149 cpl_propertylist *list = cpl_propertylist_new();
1151 assert(self != NULL);
1152 assert(self->proplist != NULL);
1154 error = cpl_propertylist_copy_property_regexp(list, self->proplist, regexp,
1157 result = cpl_propertylist_get_size(list);
1159 cpl_propertylist_delete(list);
1167 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
1168 return _irplib_sdp_spectrum_count_keywords(self,
"^OBID[0-9]+$");
1174 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
1175 return _irplib_sdp_spectrum_count_keywords(self,
"^PROV[0-9]+$");
1181 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
1182 return _irplib_sdp_spectrum_count_keywords(self,
"^ASSON[0-9]+$");
1188 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
1189 return _irplib_sdp_spectrum_count_keywords(self,
"^ASSOC[0-9]+$");
1195 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
1196 return _irplib_sdp_spectrum_count_keywords(self,
"^ASSOM[0-9]+$");
1207_irplib_keyword_table_is_sorted(
const irplib_keyword_record *table,
1213 for (i = 0; i < entries - 1; ++i) {
1214 if (strcmp(table[i].name, table[i + 1].name) >= 0) {
1224static const irplib_keyword_record *
1225_irplib_sdp_spectrum_get_keyword_record(
const char *name)
1230 static const irplib_keyword_record keyword_table[] = {
1232 {KEY_APERTURE, KEY_APERTURE_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1233 {KEY_ASSOC, KEY_ASSOC_COMMENT, CPL_TYPE_STRING, CPL_TRUE},
1234 {KEY_ASSOM, KEY_ASSOM_COMMENT, CPL_TYPE_STRING, CPL_TRUE},
1235 {KEY_ASSON, KEY_ASSON_COMMENT, CPL_TYPE_STRING, CPL_TRUE},
1236 {KEY_CONTNORM, KEY_CONTNORM_COMMENT, CPL_TYPE_BOOL, CPL_FALSE},
1237 {KEY_DEC, KEY_DEC_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1238 {KEY_DETRON, KEY_DETRON_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1239 {KEY_DISPELEM, KEY_DISPELEM_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1240 {KEY_EFFRON, KEY_EFFRON_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1241 {KEY_EXPTIME, KEY_EXPTIME_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1242 {KEY_EXTNAME, KEY_EXTNAME_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1243 {KEY_EXT_OBJ, KEY_EXT_OBJ_COMMENT, CPL_TYPE_BOOL, CPL_FALSE},
1244 {KEY_FLUXCAL, KEY_FLUXCAL_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1245 {KEY_FLUXERR, KEY_FLUXERR_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1246 {KEY_GAIN, KEY_GAIN_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1247 {KEY_INHERIT, KEY_INHERIT_COMMENT, CPL_TYPE_BOOL, CPL_FALSE},
1248 {KEY_LAMNLIN, KEY_LAMNLIN_COMMENT, CPL_TYPE_INT, CPL_FALSE},
1249 {KEY_LAMRMS, KEY_LAMRMS_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1250 {KEY_MJDEND, KEY_MJDEND_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1251 {KEY_MJDOBS, KEY_MJDOBS_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1252 {KEY_M_EPOCH, KEY_M_EPOCH_COMMENT, CPL_TYPE_BOOL, CPL_FALSE},
1253 {KEY_NCOMBINE, KEY_NCOMBINE_COMMENT, CPL_TYPE_INT, CPL_FALSE},
1254 {KEY_NELEM, KEY_NELEM_COMMENT, IRPLIB_TYPE_NELEM, CPL_FALSE},
1255 {KEY_OBID, KEY_OBID_COMMENT, CPL_TYPE_INT, CPL_TRUE},
1256 {KEY_OBJECT, KEY_OBJECT_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1257 {KEY_OBSTECH, KEY_OBSTECH_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1258 {KEY_ORIGIN, KEY_ORIGIN_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1259 {KEY_PROCSOFT, KEY_PROCSOFT_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1260 {KEY_PRODCATG, KEY_PRODCATG_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1261 {KEY_PRODLVL, KEY_PRODLVL_COMMENT, CPL_TYPE_INT, CPL_FALSE},
1262 {KEY_PROG_ID, KEY_PROG_ID_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1263 {KEY_PROV, KEY_PROV_COMMENT, CPL_TYPE_STRING, CPL_TRUE},
1264 {KEY_RA, KEY_RA_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1265 {KEY_REFERENC, KEY_REFERENC_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1266 {KEY_SNR, KEY_SNR_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1267 {KEY_SPECSYS, KEY_SPECSYS_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1268 {KEY_SPEC_BIN, KEY_SPEC_BIN_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1269 {KEY_SPEC_BW, KEY_SPEC_BW_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1270 {KEY_SPEC_ERR, KEY_SPEC_ERR_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1271 {KEY_SPEC_RES, KEY_SPEC_RES_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1272 {KEY_SPEC_SYE, KEY_SPEC_SYE_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1273 {KEY_SPEC_VAL, KEY_SPEC_VAL_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1274 {KEY_TCOMM, KEY_TCOMM_COMMENT, CPL_TYPE_STRING, CPL_TRUE},
1275 {KEY_TDMAX(1), KEY_TDMAX1_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1276 {KEY_TDMIN(1), KEY_TDMIN1_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1277 {KEY_TELAPSE, KEY_TELAPSE_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1278 {KEY_TEXPTIME, KEY_TEXPTIME_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1279 {KEY_TIMESYS, KEY_TIMESYS_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1280 {KEY_TITLE, KEY_TITLE_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1281 {KEY_TMID, KEY_TMID_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1282 {KEY_TOT_FLUX, KEY_TOT_FLUX_COMMENT, CPL_TYPE_BOOL, CPL_FALSE},
1283 {KEY_TUCD, KEY_TUCD_COMMENT, CPL_TYPE_STRING, CPL_TRUE},
1284 {KEY_TUTYP, KEY_TUTYP_COMMENT, CPL_TYPE_STRING, CPL_TRUE},
1285 {KEY_VOCLASS, KEY_VOCLASS_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1286 {KEY_VOPUB, KEY_VOPUB_COMMENT, CPL_TYPE_STRING, CPL_FALSE},
1287 {KEY_WAVELMAX, KEY_WAVELMAX_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE},
1288 {KEY_WAVELMIN, KEY_WAVELMIN_COMMENT, CPL_TYPE_DOUBLE, CPL_FALSE}};
1290 static const size_t tablesize =
1291 sizeof(keyword_table) /
sizeof(irplib_keyword_record);
1293 size_t high = tablesize - 1;
1294 const irplib_keyword_record *record = NULL;
1296 assert(_irplib_keyword_table_is_sorted(keyword_table, tablesize));
1297 assert(name != NULL);
1307 size_t keylen = strlen(keyword_table[mid].name);
1308 int result = strncmp(name, keyword_table[mid].name, keylen);
1310 record = &keyword_table[mid];
1313 else if (result < 0) {
1328 assert(record != NULL);
1330 if (strlen(record->name) != strlen(name)) {
1331 if (!record->is_array_key)
1335 const char *c = name + strlen(record->name);
1336 while (*c !=
'\0') {
1349 const cpl_propertylist *plist,
1352 const irplib_keyword_record *key;
1353 cpl_errorstate prestate = cpl_errorstate_get();
1354 cpl_boolean spectrum_has_keyword;
1356 cpl_ensure_code(self != NULL && plist != NULL && name != NULL,
1357 CPL_ERROR_NULL_INPUT);
1359 assert(self->proplist != NULL);
1361 if (!cpl_propertylist_has(plist, name)) {
1362 return cpl_error_set_message(
1363 cpl_func, cpl_error_get_code(),
1364 "Could not set '%s' since the keyword was not found in the"
1369 key = _irplib_sdp_spectrum_get_keyword_record(name);
1371 return cpl_error_set_message(
1372 cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1373 "The keyword name '%s' is not valid for an SPD spectrum.", name);
1376 spectrum_has_keyword = cpl_propertylist_has(self->proplist, name);
1378 switch ((
int)key->type) {
1379 case CPL_TYPE_BOOL: {
1383 cpl_boolean value = cpl_propertylist_get_bool(plist, name);
1384 cpl_propertylist_update_bool(self->proplist, name, value);
1386 case CPL_TYPE_INT: {
1387 int value = cpl_propertylist_get_int(plist, name);
1388 cpl_propertylist_update_int(self->proplist, name, value);
1390 case CPL_TYPE_DOUBLE: {
1391 double value = cpl_propertylist_get_double(plist, name);
1392 cpl_propertylist_update_double(self->proplist, name, value);
1394 case CPL_TYPE_STRING: {
1395 const char *value = cpl_propertylist_get_string(plist, name);
1396 cpl_propertylist_update_string(self->proplist, name, value);
1398 case IRPLIB_TYPE_NELEM: {
1400 spectrum_has_keyword = CPL_TRUE;
1402 (cpl_size)cpl_propertylist_get_long_long(plist, name);
1403 if (cpl_errorstate_is_equal(prestate)) {
1404 irplib_sdp_spectrum_set_nelem(self, value);
1408 return cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
1409 "Cannot handle type '%s'.",
1410 cpl_type_get_name(key->type));
1413 if (!spectrum_has_keyword) {
1414 cpl_propertylist_set_comment(self->proplist, name, key->comment);
1417 if (!cpl_errorstate_is_equal(prestate)) {
1418 if (!spectrum_has_keyword) {
1421 prestate = cpl_errorstate_get();
1422 (void)cpl_propertylist_erase(self->proplist, name);
1423 cpl_errorstate_set(prestate);
1425 return cpl_error_set_message(
1426 cpl_func, cpl_error_get_code(),
1427 "Could not set '%s'. Likely the keyword from the source list"
1428 " has a different format or type.",
1432 return CPL_ERROR_NONE;
1438 const cpl_property *prop)
1441 const irplib_keyword_record *key;
1442 cpl_errorstate prestate = cpl_errorstate_get();
1443 cpl_boolean spectrum_has_keyword;
1445 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
1447 assert(self->proplist != NULL);
1449 name = cpl_property_get_name(prop);
1451 return cpl_error_get_code();
1453 key = _irplib_sdp_spectrum_get_keyword_record(name);
1455 return cpl_error_set_message(
1456 cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1457 "The keyword name '%s' is not valid for an SPD spectrum.", name);
1460 spectrum_has_keyword = cpl_propertylist_has(self->proplist, name);
1462 switch ((
int)key->type) {
1463 case CPL_TYPE_BOOL: {
1464 cpl_boolean value = cpl_property_get_bool(prop);
1465 cpl_propertylist_update_bool(self->proplist, name, value);
1467 case CPL_TYPE_INT: {
1468 int value = cpl_property_get_int(prop);
1469 cpl_propertylist_update_int(self->proplist, name, value);
1471 case CPL_TYPE_DOUBLE: {
1472 double value = cpl_property_get_double(prop);
1473 cpl_propertylist_update_double(self->proplist, name, value);
1475 case CPL_TYPE_STRING: {
1476 const char *value = cpl_property_get_string(prop);
1477 cpl_propertylist_update_string(self->proplist, name, value);
1479 case IRPLIB_TYPE_NELEM: {
1481 spectrum_has_keyword = CPL_TRUE;
1482 cpl_size value = (cpl_size)cpl_property_get_long_long(prop);
1483 if (cpl_errorstate_is_equal(prestate)) {
1484 irplib_sdp_spectrum_set_nelem(self, value);
1488 return cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
1489 "Cannot handle type '%s'.",
1490 cpl_type_get_name(key->type));
1493 if (!spectrum_has_keyword) {
1494 cpl_propertylist_set_comment(self->proplist, name, key->comment);
1497 if (!cpl_errorstate_is_equal(prestate)) {
1498 if (!spectrum_has_keyword) {
1501 prestate = cpl_errorstate_get();
1502 (void)cpl_propertylist_erase(self->proplist, name);
1503 cpl_errorstate_set(prestate);
1505 return cpl_error_set_message(
1506 cpl_func, cpl_error_get_code(),
1507 "Could not set '%s'. Likely the source property has a"
1508 " different format or type.",
1512 return CPL_ERROR_NONE;
1518 const cpl_propertylist *plist,
1522 cpl_propertylist *sublist = NULL;
1523 cpl_propertylist *origlist = NULL;
1524 cpl_errorstate prestate = cpl_errorstate_get();
1527 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
1529 assert(self->proplist != NULL);
1531 sublist = cpl_propertylist_new();
1532 origlist = cpl_propertylist_new();
1533 cpl_propertylist_copy_property_regexp(origlist, self->proplist, regexp,
1535 cpl_propertylist_copy_property_regexp(sublist, plist, regexp, invert);
1536 if (cpl_propertylist_has(sublist, KEY_NELEM)) {
1539 cpl_propertylist_erase(sublist, KEY_NELEM);
1540 cpl_propertylist_copy_property(sublist, plist, KEY_NELEM);
1542 if (!cpl_errorstate_is_equal(prestate))
1545 for (i = 0; i < cpl_propertylist_get_size(sublist); ++i) {
1546 const cpl_property *p = cpl_propertylist_get_const(sublist, i);
1547 const char *name = cpl_property_get_name(p);
1548 irplib_sdp_spectrum_copy_keyword(self, sublist, name);
1549 if (!cpl_errorstate_is_equal(prestate))
1553 cpl_propertylist_delete(sublist);
1554 cpl_propertylist_delete(origlist);
1555 return CPL_ERROR_NONE;
1560 prestate = cpl_errorstate_get();
1561 (void)cpl_propertylist_copy_property_regexp(self->proplist, origlist,
".*",
1563 cpl_errorstate_set(prestate);
1564 cpl_propertylist_delete(sublist);
1565 cpl_propertylist_delete(origlist);
1566 return cpl_error_get_code();
1570GET_SET_METHODS_TYPE_DOUBLE(ra, KEY_RA, KEY_RA_COMMENT)
1571GET_SET_METHODS_TYPE_DOUBLE(dec, KEY_DEC, KEY_DEC_COMMENT)
1572GET_SET_METHODS_TYPE_DOUBLE(exptime, KEY_EXPTIME, KEY_EXPTIME_COMMENT)
1573GET_SET_METHODS_TYPE_DOUBLE(texptime, KEY_TEXPTIME, KEY_TEXPTIME_COMMENT)
1574GET_SET_METHODS_TYPE_STRING(timesys, KEY_TIMESYS, KEY_TIMESYS_COMMENT)
1575GET_SET_METHODS_TYPE_DOUBLE(mjdobs, KEY_MJDOBS, KEY_MJDOBS_COMMENT)
1576GET_SET_METHODS_TYPE_DOUBLE(mjdend, KEY_MJDEND, KEY_MJDEND_COMMENT)
1577GET_SET_METHODS_TYPE_INT(prodlvl, KEY_PRODLVL, KEY_PRODLVL_COMMENT)
1578GET_SET_METHODS_TYPE_STRING(procsoft, KEY_PROCSOFT, KEY_PROCSOFT_COMMENT)
1579GET_SET_METHODS_TYPE_STRING(prodcatg, KEY_PRODCATG, KEY_PRODCATG_COMMENT)
1580GET_SET_METHODS_TYPE_STRING(origin, KEY_ORIGIN, KEY_ORIGIN_COMMENT)
1581GET_SET_METHODS_TYPE_BOOL(extobj, KEY_EXT_OBJ, KEY_EXT_OBJ_COMMENT)
1582GET_SET_METHODS_TYPE_STRING(dispelem, KEY_DISPELEM, KEY_DISPELEM_COMMENT)
1583GET_SET_METHODS_TYPE_STRING(specsys, KEY_SPECSYS, KEY_SPECSYS_COMMENT)
1584GET_SET_METHODS_TYPE_STRING(progid, KEY_PROG_ID, KEY_PROG_ID_COMMENT)
1585GET_SET_ARRAY_METHODS_TYPE_INT(obid, KEY_OBID, KEY_OBID_COMMENT)
1586GET_SET_METHODS_TYPE_BOOL(mepoch, KEY_M_EPOCH, KEY_M_EPOCH_COMMENT)
1587GET_SET_METHODS_TYPE_STRING(obstech, KEY_OBSTECH, KEY_OBSTECH_COMMENT)
1588GET_SET_METHODS_TYPE_STRING(fluxcal, KEY_FLUXCAL, KEY_FLUXCAL_COMMENT)
1589GET_SET_METHODS_TYPE_BOOL(contnorm, KEY_CONTNORM, KEY_CONTNORM_COMMENT)
1590GET_SET_METHODS_TYPE_DOUBLE(wavelmin, KEY_WAVELMIN, KEY_WAVELMIN_COMMENT)
1591GET_SET_METHODS_TYPE_DOUBLE(wavelmax, KEY_WAVELMAX, KEY_WAVELMAX_COMMENT)
1592GET_SET_METHODS_TYPE_DOUBLE(specbin, KEY_SPEC_BIN, KEY_SPEC_BIN_COMMENT)
1593GET_SET_METHODS_TYPE_BOOL(totflux, KEY_TOT_FLUX, KEY_TOT_FLUX_COMMENT)
1594GET_SET_METHODS_TYPE_DOUBLE(fluxerr, KEY_FLUXERR, KEY_FLUXERR_COMMENT)
1595GET_SET_METHODS_TYPE_STRING(referenc, KEY_REFERENC, KEY_REFERENC_COMMENT)
1596GET_SET_METHODS_TYPE_DOUBLE(specres, KEY_SPEC_RES, KEY_SPEC_RES_COMMENT)
1597GET_SET_METHODS_TYPE_DOUBLE(specerr, KEY_SPEC_ERR, KEY_SPEC_ERR_COMMENT)
1598GET_SET_METHODS_TYPE_DOUBLE(specsye, KEY_SPEC_SYE, KEY_SPEC_SYE_COMMENT)
1599GET_SET_METHODS_TYPE_INT(lamnlin, KEY_LAMNLIN, KEY_LAMNLIN_COMMENT)
1600GET_SET_METHODS_TYPE_DOUBLE(lamrms, KEY_LAMRMS, KEY_LAMRMS_COMMENT)
1601GET_SET_METHODS_TYPE_DOUBLE(gain, KEY_GAIN, KEY_GAIN_COMMENT)
1602GET_SET_METHODS_TYPE_DOUBLE(detron, KEY_DETRON, KEY_DETRON_COMMENT)
1603GET_SET_METHODS_TYPE_DOUBLE(effron, KEY_EFFRON, KEY_EFFRON_COMMENT)
1604GET_SET_METHODS_TYPE_DOUBLE(snr, KEY_SNR, KEY_SNR_COMMENT)
1605GET_SET_METHODS_TYPE_INT(ncombine, KEY_NCOMBINE, KEY_NCOMBINE_COMMENT)
1606GET_SET_ARRAY_METHODS_TYPE_STRING(prov, KEY_PROV, KEY_PROV_COMMENT)
1607GET_SET_ARRAY_METHODS_TYPE_STRING(asson, KEY_ASSON, KEY_ASSON_COMMENT)
1608GET_SET_ARRAY_METHODS_TYPE_STRING(assoc, KEY_ASSOC, KEY_ASSOC_COMMENT)
1609GET_SET_ARRAY_METHODS_TYPE_STRING(assom, KEY_ASSOM, KEY_ASSOM_COMMENT)
1610GET_SET_METHODS_TYPE_STRING(voclass, KEY_VOCLASS, KEY_VOCLASS_COMMENT)
1611GET_SET_METHODS_TYPE_STRING(vopub, KEY_VOPUB, KEY_VOPUB_COMMENT)
1612GET_SET_METHODS_TYPE_STRING(title, KEY_TITLE, KEY_TITLE_COMMENT)
1613GET_SET_METHODS_TYPE_STRING(
object, KEY_OBJECT, KEY_OBJECT_COMMENT)
1614GET_SET_METHODS_TYPE_DOUBLE(aperture, KEY_APERTURE, KEY_APERTURE_COMMENT)
1615GET_SET_METHODS_TYPE_DOUBLE(telapse, KEY_TELAPSE, KEY_TELAPSE_COMMENT)
1616GET_SET_METHODS_TYPE_DOUBLE(tmid, KEY_TMID, KEY_TMID_COMMENT)
1617GET_SET_METHODS_TYPE_DOUBLE(specval, KEY_SPEC_VAL, KEY_SPEC_VAL_COMMENT)
1618GET_SET_METHODS_TYPE_DOUBLE(specbw, KEY_SPEC_BW, KEY_SPEC_BW_COMMENT)
1619GET_SET_METHODS_TYPE_STRING(extname, KEY_EXTNAME, KEY_EXTNAME_COMMENT)
1620GET_SET_METHODS_TYPE_BOOL(inherit, KEY_INHERIT, KEY_INHERIT_COMMENT)
1621GET_SET_METHODS_TYPE_DOUBLE(tdmin, KEY_TDMIN(1), KEY_TDMIN1_COMMENT)
1622GET_SET_METHODS_TYPE_DOUBLE(tdmax, KEY_TDMAX(1), KEY_TDMAX1_COMMENT)
1645 const char *keyword,
1646 const char *comment)
1648 cpl_ensure_code((self != NULL) && (keyword != NULL) && (comment != NULL),
1649 CPL_ERROR_NULL_INPUT);
1650 cpl_ensure_code((self->proplist != NULL), CPL_ERROR_ILLEGAL_INPUT);
1652 if (!cpl_propertylist_has(self->proplist, keyword)) {
1653 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1654 "Could not find '%s' keyword.", keyword);
1657 cpl_propertylist_set_comment(self->proplist, keyword, comment);
1658 return CPL_ERROR_NONE;
1665 cpl_size firstindex,
1666 const cpl_frameset *frames)
1668 cpl_frameset_iterator *iter = NULL;
1669 cpl_propertylist *keywords = NULL;
1670 const cpl_frame *frame;
1671 cpl_size index = firstindex;
1674 assert(self != NULL);
1675 assert(self->proplist != NULL);
1677 iter = cpl_frameset_iterator_new(frames);
1678 frame = cpl_frameset_iterator_get_const(iter);
1679 while (frame != NULL) {
1680 cpl_error_code error;
1681 const char *value = NULL;
1684 const char *filename = cpl_frame_get_filename(frame);
1685 cpl_error_ensure(filename != NULL, cpl_error_get_code(),
goto cleanup,
1686 "%s", cpl_error_get_message());
1687 keywords = cpl_propertylist_load(filename, 0);
1688 cpl_error_ensure(filename != NULL, cpl_error_get_code(),
goto cleanup,
1689 "Could not load keywords from primary HDU in '%s'.",
1694 if (cpl_propertylist_has(keywords, KEY_ARCFILE)) {
1695 value = cpl_propertylist_get_string(keywords, KEY_ARCFILE);
1697 value != NULL, cpl_error_get_code(),
goto cleanup,
1698 "Could not extract the '%s' keyword value from '%s'.",
1699 KEY_ARCFILE, filename);
1701 else if (cpl_propertylist_has(keywords, KEY_ORIGFILE)) {
1702 value = cpl_propertylist_get_string(keywords, KEY_ORIGFILE);
1704 value != NULL, cpl_error_get_code(),
goto cleanup,
1705 "Could not extract the '%s' keyword value from '%s'.",
1706 KEY_ORIGFILE, filename);
1713 error = irplib_sdp_spectrum_set_prov(self, index, value);
1714 cpl_error_ensure(!error, error,
goto cleanup,
"%s",
1715 cpl_error_get_message());
1716 cpl_propertylist_delete(keywords);
1720 cpl_errorstate status = cpl_errorstate_get();
1721 cpl_frameset_iterator_advance(iter, 1);
1722 if (cpl_error_get_code() == CPL_ERROR_ACCESS_OUT_OF_RANGE) {
1723 cpl_errorstate_set(status);
1725 frame = cpl_frameset_iterator_get_const(iter);
1729 cpl_frameset_iterator_delete(iter);
1730 return CPL_ERROR_NONE;
1734 cpl_frameset_iterator_delete(iter);
1735 cpl_propertylist_delete(keywords);
1736 return cpl_error_get_code();
1743 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
1751 return irplib_sdp_spectrum_set_nelem(self, 0);
1759 cpl_error_code error = CPL_ERROR_NONE;
1761 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
1763 assert(self->table != NULL);
1765 ncol = cpl_table_get_ncol(self->table);
1769 cpl_array *names = cpl_table_get_column_names(self->table);
1770 for (i = 0; i < ncol; ++i) {
1771 const char *name = cpl_array_get_string(names, i);
1772 error = cpl_table_set_column_depth(self->table, name, value);
1777 cpl_errorstate prestate = cpl_errorstate_get();
1778 for (j = 0; j < i; ++j) {
1779 (void)cpl_table_set_column_depth(self->table, name,
1782 cpl_errorstate_set(prestate);
1786 cpl_array_delete(names);
1789 self->nelem = value;
1797 const cpl_propertylist *plist,
1801 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
1803 assert(self->proplist != NULL);
1805 if (cpl_propertylist_has(plist, name)) {
1806 cpl_errorstate prestate = cpl_errorstate_get();
1807 cpl_size value = (cpl_size)cpl_propertylist_get_long_long(plist, name);
1808 if (cpl_errorstate_is_equal(prestate)) {
1809 return irplib_sdp_spectrum_set_nelem(self, value);
1812 return cpl_error_set_message(
1813 cpl_func, cpl_error_get_code(),
1814 "Could not set '%s'. Likely the source '%s' keyword has a"
1815 " different format or type.",
1820 return cpl_error_set_message(
1821 cpl_func, CPL_ERROR_DATA_NOT_FOUND,
1822 "Could not set '%s' since the '%s' keyword was not found.",
1831 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
1832 assert(self->table != NULL);
1833 return cpl_table_get_ncol(self->table);
1841 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
1842 assert(self->table != NULL);
1843 return cpl_table_has_column(self->table, name);
1850 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
1851 assert(self->table != NULL);
1852 return cpl_table_get_column_names(self->table);
1861 cpl_error_code error;
1862 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
1863 assert(self->table != NULL);
1864 error = cpl_table_new_column_array(self->table, name, type, self->nelem);
1866 cpl_error_set_message(cpl_func, cpl_error_get_code(),
1867 "Failed to create a new column called '%s'.",
1882 const cpl_array *data)
1884 cpl_error_code error;
1887 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
1889 assert(self->table != NULL);
1893 error = cpl_table_new_column_array(self->table, name, type, self->nelem);
1894 if (unit != NULL && *unit !=
'\0') {
1895 error |= cpl_table_set_column_unit(self->table, name, unit);
1898 error |= cpl_table_set_column_unit(self->table, name,
" ");
1900 if (format != NULL) {
1901 error |= cpl_table_set_column_format(self->table, name, format);
1903 if (tutyp != NULL) {
1904 error |= irplib_sdp_spectrum_set_column_tutyp(self, name, tutyp);
1907 error |= irplib_sdp_spectrum_set_column_tutyp(self, name,
"");
1910 error |= irplib_sdp_spectrum_set_column_tucd(self, name, tucd);
1913 error |= irplib_sdp_spectrum_set_column_tucd(self, name,
"");
1920 error = cpl_table_set_array(self->table, name, 0, data);
1923 cpl_array *array = cpl_array_new(self->nelem, type);
1924 if (array != NULL) {
1925 error = cpl_table_set_array(self->table, name, 0, array);
1926 cpl_array_delete(array);
1929 error = cpl_error_get_code();
1939 cpl_errorstate prestate = cpl_errorstate_get();
1940 _irplib_sdp_spectrum_erase_column_keywords(self, name);
1941 (void)cpl_table_erase_column(self->table, name);
1942 cpl_errorstate_set(prestate);
1944 cpl_error_set_message(cpl_func, cpl_error_get_code(),
1945 "Failed to create a new column called '%s'.",
1956 cpl_errorstate prestate = cpl_errorstate_get();
1957 cpl_error_code error = CPL_ERROR_NONE;
1959 cpl_ensure_code(self != NULL && name != NULL, CPL_ERROR_NULL_INPUT);
1961 assert(self->table != NULL);
1963 _irplib_sdp_spectrum_erase_column_keywords(self, name);
1964 if (!cpl_errorstate_is_equal(prestate)) {
1965 error |= cpl_error_get_code();
1967 error |= cpl_table_erase_column(self->table, name);
1969 return cpl_error_get_code();
1972 return CPL_ERROR_NONE;
1977static cpl_error_code
1979 const char *to_name,
1980 const cpl_table *table,
1981 const char *from_name)
1983 cpl_error_code error;
1985 assert(self != NULL);
1986 assert(self->table != NULL);
1988 error = cpl_table_duplicate_column(self->table, to_name, table, from_name);
1991 error |= irplib_sdp_spectrum_set_column_tutyp(self, to_name,
"");
1992 error |= irplib_sdp_spectrum_set_column_tucd(self, to_name,
"");
1995 cpl_errorstate prestate = cpl_errorstate_get();
1996 _irplib_sdp_spectrum_erase_column_keywords(self, to_name);
1997 (void)cpl_table_erase_column(self->table, to_name);
1998 cpl_errorstate_set(prestate);
1999 return cpl_error_get_code();
2001 return CPL_ERROR_NONE;
2007 const cpl_table *table,
2012 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
2013 return _irplib_sdp_spectrum_copy_column(self, name, table, name);
2019 const cpl_table *table,
2024 cpl_array *names = NULL;
2029 cpl_ensure_code(self != NULL && regexp != NULL, CPL_ERROR_NULL_INPUT);
2031 assert(self->table != NULL);
2033 reg_error_code = regcomp(&re, regexp, REG_EXTENDED | REG_NOSUB);
2034 if (reg_error_code != 0) {
2035 return cpl_error_set_regex(CPL_ERROR_ILLEGAL_INPUT, reg_error_code, &re,
2036 "regexp='%s', invert=%d", regexp, invert);
2041 names = cpl_table_get_column_names(table);
2042 n = cpl_array_get_size(names);
2043 for (i = 0; i < n; ++i) {
2045 const char *namei = cpl_array_get_string(names, i);
2046 cpl_error_ensure(!cpl_table_has_column(self->table, namei),
2047 CPL_ERROR_ILLEGAL_OUTPUT,
goto cleanup,
2048 "The column '%s' already exists in the spectrum.",
2050 match = (regexec(&re, namei, 0, NULL, 0) == 0);
2051 if ((!match && !invert) || (match && invert)) {
2052 cpl_array_set_invalid(names, i);
2056 for (i = 0; i < n; ++i) {
2057 if (cpl_array_is_valid(names, i)) {
2058 const char *namei = cpl_array_get_string(names, i);
2059 cpl_error_code error =
2060 _irplib_sdp_spectrum_copy_column(self, namei, table, namei);
2062 cpl_errorstate prestate;
2064 cpl_error_set_message(cpl_func, error,
2065 "Could not copy column '%s'.", namei);
2068 prestate = cpl_errorstate_get();
2069 for (j = 0; j < i; ++j) {
2070 namei = cpl_array_get_string(names, i);
2071 _irplib_sdp_spectrum_erase_column_keywords(self, namei);
2072 (void)cpl_table_erase_column(self->table, namei);
2074 cpl_errorstate_set(prestate);
2079 cpl_array_delete(names);
2081 return CPL_ERROR_NONE;
2085 cpl_array_delete(names);
2087 return cpl_error_get_code();
2094 const cpl_table *table,
2095 const char *colname,
2098 char *orig_unit = NULL;
2099 char *orig_format = NULL;
2100 cpl_errorstate prestate = cpl_errorstate_get();
2104 cpl_ensure_code(self != NULL && table != NULL, CPL_ERROR_NULL_INPUT);
2106 assert(self->table != NULL);
2108 if (!cpl_table_has_column(self->table, name)) {
2110 return _irplib_sdp_spectrum_copy_column(self, name, table, colname);
2114 if (!cpl_table_has_column(table, colname)) {
2115 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
2116 "Column '%s' not found in table.",
2122 if (flags & IRPLIB_COLUMN_UNIT) {
2123 const char *unit = cpl_table_get_column_unit(table, colname);
2126 if (unit != NULL && *unit ==
'\0') {
2129 orig_unit = cpl_strdup(cpl_table_get_column_unit(self->table, name));
2130 cpl_table_set_column_unit(self->table, name, unit);
2131 if (!cpl_errorstate_is_equal(prestate))
2134 if (flags & IRPLIB_COLUMN_FORMAT) {
2136 cpl_strdup(cpl_table_get_column_format(self->table, name));
2137 cpl_table_set_column_format(self->table, name,
2138 cpl_table_get_column_format(table,
2140 if (!cpl_errorstate_is_equal(prestate))
2147 if (flags & IRPLIB_COLUMN_DATA) {
2148 if (cpl_table_get_column_type(self->table, name) !=
2149 cpl_table_get_column_type(table, colname)) {
2150 cpl_error_set_message(
2151 cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
2152 "The table column '%s' and spectrum column '%s' do not"
2153 " have the same types.",
2157 if (cpl_table_get_column_depth(self->table, name) !=
2158 cpl_table_get_column_depth(table, colname)) {
2159 cpl_error_set_message(
2160 cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
2161 "The table column '%s' and spectrum column '%s' do not"
2162 " have the same dimensions.",
2166 const cpl_array *data = cpl_table_get_array(table, colname, 0);
2169 cpl_table_set_array(self->table, name, 0, data);
2170 if (!cpl_errorstate_is_equal(prestate))
2174 cpl_free(orig_unit);
2175 cpl_free(orig_format);
2176 return CPL_ERROR_NONE;
2180 prestate = cpl_errorstate_get();
2181 if (orig_unit != NULL) {
2182 (void)cpl_table_set_column_unit(self->table, name, orig_unit);
2183 cpl_free(orig_unit);
2185 if (orig_format != NULL) {
2186 (void)cpl_table_set_column_format(self->table, name, orig_format);
2187 cpl_free(orig_format);
2189 cpl_errorstate_set(prestate);
2190 return cpl_error_get_code();
2198 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, CPL_TYPE_INVALID);
2199 assert(self->table != NULL);
2200 return cpl_table_get_column_type(self->table, name);
2208 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
2209 assert(self->table != NULL);
2210 return cpl_table_get_column_unit(self->table, name);
2234 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
2235 assert(self->table != NULL);
2238 if (unit != NULL && *unit ==
'\0') {
2241 return cpl_table_set_column_unit(self->table, name, unit);
2248 const cpl_propertylist *plist,
2251 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
2253 assert(self->table != NULL);
2255 if (cpl_propertylist_has(plist, key)) {
2256 cpl_errorstate prestate = cpl_errorstate_get();
2257 const char *value = cpl_propertylist_get_string(plist, key);
2258 if (cpl_errorstate_is_equal(prestate)) {
2261 if (value != NULL && *value ==
'\0') {
2264 return cpl_table_set_column_unit(self->table, name, value);
2267 return cpl_error_set_message(
2268 cpl_func, cpl_error_get_code(),
2269 "Could not set the unit for column '%s'. Likely the source '%s'"
2270 " keyword is not a string.",
2275 return cpl_error_set_message(
2276 cpl_func, CPL_ERROR_DATA_NOT_FOUND,
2277 "Could not set the unit for column '%s' since the '%s' keyword"
2288 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
2289 assert(self->table != NULL);
2290 return cpl_table_get_column_format(self->table, name);
2299 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
2300 assert(self->table != NULL);
2301 return cpl_table_set_column_format(self->table, name, format);
2312 assert(self != NULL);
2313 assert(self->table != NULL);
2314 assert(name != NULL);
2317 names = cpl_table_get_column_names(self->table);
2318 n = cpl_array_get_size(names);
2319 for (i = 0; i < n; ++i) {
2320 const char *namei = cpl_array_get_string(names, i);
2321 if (strcmp(namei, name) == 0) {
2322 cpl_array_delete(names);
2326 cpl_array_delete(names);
2327 return (cpl_size)-1;
2334 const char *keyword)
2337 const char *result = NULL;
2339 assert(self != NULL);
2340 assert(self->proplist != NULL);
2341 assert(name != NULL);
2342 assert(keyword != NULL);
2344 index = _irplib_sdp_spectrum_get_column_index(self, name);
2345 if (index != (cpl_size)-1) {
2347 char *propname = cpl_sprintf(
"%s%" CPL_SIZE_FORMAT, keyword, index + 1);
2348 if (cpl_propertylist_has(self->proplist, propname)) {
2349 result = cpl_propertylist_get_string(self->proplist, propname);
2354 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
2355 "Could not find '%s' keyword for column '%s'.",
2362static cpl_error_code
2366 const char *keyword,
2367 const char *comment)
2370 char *propname, *pcomment;
2372 assert(self != NULL);
2373 assert(self->proplist != NULL);
2374 assert(name != NULL);
2375 assert(keyword != NULL);
2376 assert(comment != NULL);
2378 index = _irplib_sdp_spectrum_get_column_index(self, name);
2380 if (index == (cpl_size)-1) {
2381 return cpl_error_set_message(
2382 cpl_func, CPL_ERROR_DATA_NOT_FOUND,
2383 "Could not find '%s' keyword for column '%s'.", keyword, name);
2386 cpl_error_code error = CPL_ERROR_NONE;
2387 propname = cpl_sprintf(
"%s%" CPL_SIZE_FORMAT, keyword, index + 1);
2388 pcomment = cpl_sprintf(
"%s%" CPL_SIZE_FORMAT, comment, index + 1);
2389 if (cpl_propertylist_has(self->proplist, propname)) {
2390 if (value != NULL) {
2392 cpl_propertylist_set_string(self->proplist, propname, value);
2395 (void)cpl_propertylist_erase(self->proplist, propname);
2398 else if (value != NULL) {
2399 error = cpl_propertylist_append_string(self->proplist, propname, value);
2401 error = cpl_propertylist_set_comment(self->proplist, propname,
2406 cpl_errorstate prestate = cpl_errorstate_get();
2407 (void)cpl_propertylist_erase(self->proplist, propname);
2408 cpl_errorstate_set(prestate);
2424 assert(self != NULL);
2425 assert(self->proplist != NULL);
2426 assert(name != NULL);
2428 index = _irplib_sdp_spectrum_get_column_index(self, name);
2429 if (index != (cpl_size)-1) {
2431 cpl_sprintf(
"%s%" CPL_SIZE_FORMAT, KEY_TUTYP, index + 1);
2432 cpl_propertylist_erase(self->proplist, propname);
2434 propname = cpl_sprintf(
"%s%" CPL_SIZE_FORMAT, KEY_TUCD, index + 1);
2435 cpl_propertylist_erase(self->proplist, propname);
2437 propname = cpl_sprintf(
"%s%" CPL_SIZE_FORMAT, KEY_TCOMM, index + 1);
2438 cpl_propertylist_erase(self->proplist, propname);
2448 cpl_errorstate prestate = cpl_errorstate_get();
2450 cpl_ensure(self != NULL && name != NULL, CPL_ERROR_NULL_INPUT, NULL);
2451 result = _irplib_sdp_spectrum_get_column_keyword(self, name, KEY_TUTYP);
2452 if (!cpl_errorstate_is_equal(prestate)) {
2453 cpl_error_set_where(cpl_func);
2464 cpl_error_code error;
2465 cpl_ensure_code(self != NULL && name != NULL, CPL_ERROR_NULL_INPUT);
2467 _irplib_sdp_spectrum_set_column_keyword(self, name, tutyp, KEY_TUTYP,
2470 cpl_error_set_where(cpl_func);
2479 const cpl_propertylist *plist,
2482 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
2484 assert(self->table != NULL);
2486 if (cpl_propertylist_has(plist, key)) {
2487 cpl_errorstate prestate = cpl_errorstate_get();
2488 const char *value = cpl_propertylist_get_string(plist, key);
2489 if (cpl_errorstate_is_equal(prestate)) {
2490 return irplib_sdp_spectrum_set_column_tutyp(self, name, value);
2494 _irplib_sdp_spectrum_get_column_index(self, name) + 1;
2495 return cpl_error_set_message(
2496 cpl_func, cpl_error_get_code(),
2497 "Could not set '%s%" CPL_SIZE_FORMAT
"' for column '%s'. Likely"
2498 " the source '%s' keyword is not a string.",
2499 KEY_TUTYP, index, name, key);
2503 cpl_size index = _irplib_sdp_spectrum_get_column_index(self, name) + 1;
2504 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
2505 "Could not set '%s%" CPL_SIZE_FORMAT
2506 "' for column '%s' since the"
2507 " '%s' keyword was not found.",
2508 KEY_TUTYP, index, name, key);
2517 cpl_errorstate prestate = cpl_errorstate_get();
2519 cpl_ensure(self != NULL && name != NULL, CPL_ERROR_NULL_INPUT, NULL);
2520 result = _irplib_sdp_spectrum_get_column_keyword(self, name, KEY_TUCD);
2521 if (!cpl_errorstate_is_equal(prestate)) {
2522 cpl_error_set_where(cpl_func);
2533 cpl_error_code error;
2534 cpl_ensure_code(self != NULL && name != NULL, CPL_ERROR_NULL_INPUT);
2535 error = _irplib_sdp_spectrum_set_column_keyword(self, name, tucd, KEY_TUCD,
2538 cpl_error_set_where(cpl_func);
2547 const cpl_propertylist *plist,
2550 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
2552 assert(self->table != NULL);
2554 if (cpl_propertylist_has(plist, key)) {
2555 cpl_errorstate prestate = cpl_errorstate_get();
2556 const char *value = cpl_propertylist_get_string(plist, key);
2557 if (cpl_errorstate_is_equal(prestate)) {
2558 return irplib_sdp_spectrum_set_column_tucd(self, name, value);
2562 _irplib_sdp_spectrum_get_column_index(self, name) + 1;
2563 return cpl_error_set_message(
2564 cpl_func, cpl_error_get_code(),
2565 "Could not set '%s%" CPL_SIZE_FORMAT
"' for column '%s'. Likely"
2566 " the source '%s' keyword is not a string.",
2567 KEY_TUCD, index, name, key);
2571 cpl_size index = _irplib_sdp_spectrum_get_column_index(self, name) + 1;
2572 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
2573 "Could not set '%s%" CPL_SIZE_FORMAT
2574 "' for column '%s' since the"
2575 " '%s' keyword was not found.",
2576 KEY_TUCD, index, name, key);
2585 cpl_errorstate prestate = cpl_errorstate_get();
2587 cpl_ensure(self != NULL && name != NULL, CPL_ERROR_NULL_INPUT, NULL);
2588 result = _irplib_sdp_spectrum_get_column_keyword(self, name, KEY_TCOMM);
2589 if (!cpl_errorstate_is_equal(prestate)) {
2590 cpl_error_set_where(cpl_func);
2601 cpl_error_code error;
2602 cpl_ensure_code(self != NULL && name != NULL, CPL_ERROR_NULL_INPUT);
2604 _irplib_sdp_spectrum_set_column_keyword(self, name, tcomm, KEY_TCOMM,
2607 cpl_error_set_where(cpl_func);
2616 const cpl_propertylist *plist,
2619 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
2621 assert(self->table != NULL);
2623 if (cpl_propertylist_has(plist, key)) {
2624 cpl_errorstate prestate = cpl_errorstate_get();
2625 const char *value = cpl_propertylist_get_string(plist, key);
2626 if (cpl_errorstate_is_equal(prestate)) {
2627 return irplib_sdp_spectrum_set_column_tcomm(self, name, value);
2631 _irplib_sdp_spectrum_get_column_index(self, name) + 1;
2632 return cpl_error_set_message(
2633 cpl_func, cpl_error_get_code(),
2634 "Could not set '%s%" CPL_SIZE_FORMAT
"' for column '%s'. Likely"
2635 " the source '%s' keyword is not a string.",
2636 KEY_TCOMM, index, name, key);
2640 cpl_size index = _irplib_sdp_spectrum_get_column_index(self, name) + 1;
2641 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
2642 "Could not set '%s%" CPL_SIZE_FORMAT
2643 "' for column '%s' since the"
2644 " '%s' keyword was not found.",
2645 KEY_TCOMM, index, name, key);
2672 const char *keyword,
2673 const char *comment)
2675 cpl_ensure_code((self != NULL), CPL_ERROR_NULL_INPUT);
2676 cpl_ensure_code((self->proplist != NULL), CPL_ERROR_ILLEGAL_INPUT);
2677 cpl_ensure_code((name != NULL) && (keyword != NULL) && (comment != NULL),
2678 CPL_ERROR_NULL_INPUT);
2680 cpl_size index = _irplib_sdp_spectrum_get_column_index(self, name);
2681 if (index == (cpl_size)-1) {
2682 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
2683 "Could not find column '%s'.", name);
2686 char *propname = cpl_sprintf(
"%s%" CPL_SIZE_FORMAT, keyword, index + 1);
2687 if (!cpl_propertylist_has(self->proplist, propname)) {
2689 return cpl_error_set_message(
2690 cpl_func, CPL_ERROR_DATA_NOT_FOUND,
2691 "Could not find '%s' keyword for column '%s'.", keyword, name);
2694 cpl_propertylist_set_comment(self->proplist, propname, comment);
2697 return CPL_ERROR_NONE;
2705 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
2706 assert(self->table != NULL);
2707 return cpl_table_get_array(self->table, name, 0);
2714 const cpl_array *array)
2716 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
2717 assert(self->table != NULL);
2718 return cpl_table_set_array(self->table, name, 0, array);
2723_irplib_make_regexp(
const cpl_propertylist *plist,
const char *extra)
2727 static const cpl_size min_chars_required = 6;
2731 static const char *start_fragment =
"^(";
2732 static const char *end_fragment =
")$";
2733 static const char *join_fragment =
"|";
2735 cpl_size extra_length = (extra != NULL ? (cpl_size)strlen(extra) : 0);
2736 cpl_size regexp_size, bytesleft, nkeys, i;
2738 char *regexp = NULL;
2740 assert(plist != NULL);
2742 nkeys = cpl_propertylist_get_size(plist);
2745 if (extra != NULL) {
2746 return cpl_sprintf(
"%s%s%s", start_fragment, extra, end_fragment);
2749 return cpl_strdup(
"");
2754 regexp_size = nkeys * 80 + min_chars_required + extra_length;
2755 regexp = cpl_malloc(regexp_size);
2757 bytesleft = regexp_size;
2759 for (i = 0; i < nkeys; ++i) {
2760 cpl_size name_length, fragment_length;
2761 const char *name, *fragment;
2764 const cpl_property *p = cpl_propertylist_get_const(plist, i);
2766 p != NULL, cpl_error_get_code(),
goto cleanup,
2767 "Unexpected error accessing property structure %" CPL_SIZE_FORMAT
2770 name = cpl_property_get_name(p);
2772 name != NULL, cpl_error_get_code(),
goto cleanup,
2773 "Unexpected error accessing the name of property %" CPL_SIZE_FORMAT
2776 name_length = (cpl_size)strlen(name);
2779 fragment = (i == 0) ? start_fragment : join_fragment;
2780 fragment_length = (cpl_size)strlen(fragment);
2782 while (bytesleft < fragment_length + name_length + extra_length +
2783 min_chars_required) {
2786 bytesleft += regexp_size;
2787 regexp_size += regexp_size;
2788 regexp = cpl_realloc(regexp, regexp_size);
2789 writepos = regexp + (regexp_size - bytesleft);
2793 strncpy(writepos, fragment, bytesleft);
2794 bytesleft -= fragment_length;
2795 writepos += fragment_length;
2796 strncpy(writepos, name, bytesleft);
2797 bytesleft -= name_length;
2798 writepos += name_length;
2802 if (extra != NULL) {
2803 strncpy(writepos, join_fragment, bytesleft);
2804 bytesleft -= (cpl_size)strlen(join_fragment);
2805 writepos += (cpl_size)strlen(join_fragment);
2806 strncpy(writepos, extra, bytesleft);
2807 bytesleft -= extra_length;
2808 writepos += extra_length;
2810 strncpy(writepos, end_fragment, bytesleft);
2812 regexp[regexp_size - 1] =
'\0';
2824irplib_sdp_spectrum_load(
const char *filename)
2826 cpl_error_code error;
2828 cpl_propertylist *plist = NULL;
2829 cpl_propertylist *tmpplist = NULL;
2830 cpl_table *table = NULL;
2831 cpl_array *names = NULL;
2832 cpl_array *emptyarray = NULL;
2833 cpl_size nelem, ext, i;
2834 char *regexp = NULL;
2836 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
2841 plist = cpl_propertylist_load_regexp(filename, 0, ALL_KEYS_REGEXP, 0);
2843 plist != NULL, cpl_error_get_code(),
goto cleanup,
2844 "Could not load property list from primary HDU when loading file '%s'.",
2849 regexp = _irplib_make_regexp(plist, NULL);
2850 cpl_error_ensure(regexp != NULL, cpl_error_get_code(),
goto cleanup,
2851 "Could not create regular expression to filter keywords.");
2855 ext = cpl_fits_find_extension(filename, KEY_EXTNAME_VALUE);
2856 cpl_error_ensure(ext != (cpl_size)-1, cpl_error_get_code(),
goto cleanup,
2857 "Failed to get the extension '%s' from file '%s'.",
2858 KEY_EXTNAME_VALUE, filename);
2863 tmpplist = cpl_propertylist_load_regexp(filename, ext, ALL_KEYS_REGEXP, 0);
2865 tmpplist != NULL, cpl_error_get_code(),
goto cleanup,
2866 "Could not load property list from extension %" CPL_SIZE_FORMAT
2867 " when loading file '%s'.",
2871 error = cpl_propertylist_copy_property_regexp(plist, tmpplist, regexp, 1);
2873 !error, error,
goto cleanup,
2874 "Failed to append keywords from file '%s' extension %" CPL_SIZE_FORMAT
2879 cpl_propertylist_delete(tmpplist);
2884 table = cpl_table_load(filename, (
int)ext, CPL_TRUE);
2886 table != NULL, cpl_error_get_code(),
goto cleanup,
2887 "Could not load the spectrum table from extension %" CPL_SIZE_FORMAT
2888 " when loading file '%s'.",
2892 if (cpl_propertylist_has(plist, KEY_NELEM)) {
2893 cpl_errorstate prestate = cpl_errorstate_get();
2894 nelem = (cpl_size)cpl_propertylist_get_long_long(plist, KEY_NELEM);
2896 cpl_propertylist_erase(plist, KEY_NELEM);
2897 cpl_error_ensure(cpl_errorstate_is_equal(prestate),
2898 cpl_error_get_code(),
goto cleanup,
2899 "Could not process the temporary '%s' keyword.",
2905 "Keyword '%s' not found in file '%s'. Possibly corrupted."
2906 " Will try find correct value from the table and continue.",
2907 KEY_NELEM, filename);
2909 if (cpl_table_get_nrow(table) > 0) {
2910 names = cpl_table_get_column_names(table);
2911 if (names != NULL) {
2912 if (cpl_array_get_size(names) > 0) {
2913 const char *name = cpl_array_get_string(names, 0);
2914 nelem = cpl_table_get_column_depth(table, name);
2916 cpl_array_delete(names);
2922 names = cpl_table_get_column_names(table);
2923 cpl_error_ensure(names != NULL, cpl_error_get_code(),
goto cleanup,
2924 "Could not get table column names when loading file '%s'.",
2926 for (i = 0; i < cpl_array_get_size(names); ++i) {
2928 const char *name = cpl_array_get_string(names, 0);
2929 cpl_type type = cpl_table_get_column_type(table, name);
2930 if ((type & CPL_TYPE_POINTER) == 0)
2932 for (j = 0; j < cpl_table_get_nrow(table); ++j) {
2933 if (cpl_table_get_array(table, name, j) != NULL)
2935 emptyarray = cpl_array_new(nelem, type & (~CPL_TYPE_POINTER));
2936 cpl_error_ensure(emptyarray != NULL, cpl_error_get_code(),
2938 "Could not create empty array when spectrum table "
2941 error = cpl_table_set_array(table, name, j, emptyarray);
2942 cpl_array_delete(emptyarray);
2946 cpl_array_delete(names);
2951 obj->proplist = plist;
2959 cpl_propertylist_delete(plist);
2960 cpl_propertylist_delete(tmpplist);
2961 cpl_table_delete(table);
2962 cpl_array_delete(names);
2963 cpl_array_delete(emptyarray);
2971 const char *filename,
2972 const cpl_propertylist *extra_pheader,
2973 const cpl_propertylist *extra_theader)
2975 cpl_error_code error;
2976 cpl_propertylist *primarykeys = NULL;
2977 cpl_propertylist *tablekeys = NULL;
2978 char *regexp = NULL;
2980 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
2982 assert(self->proplist != NULL);
2983 assert(self->table != NULL);
2987 regexp = _irplib_make_regexp(self->proplist, KEY_NELEM);
2988 cpl_error_ensure(regexp != NULL, cpl_error_get_code(),
goto cleanup,
2989 "Could not create regular expression to filter keywords.");
2993 primarykeys = cpl_propertylist_new();
2994 error = cpl_propertylist_copy_property_regexp(primarykeys, self->proplist,
2995 PRIMARY_HDU_KEYS_REGEXP, 0);
2996 cpl_error_ensure(!error, error,
goto cleanup,
2997 "Failed to extract keywords for primary HDU.");
3001 if (cpl_propertylist_has(primarykeys, KEY_OBJECT)) {
3002 error = cpl_propertylist_set_comment(primarykeys, KEY_OBJECT,
3003 KEY_OBJECT_PHDU_COMMENT);
3004 cpl_error_ensure(!error, error,
goto cleanup,
3005 "Could not update comment for '%s' in primary HDU.",
3010 if (extra_pheader != NULL) {
3011 error = cpl_propertylist_copy_property_regexp(primarykeys,
3012 extra_pheader, regexp, 1);
3013 cpl_error_ensure(!error, error,
goto cleanup,
3014 "Could not add extra keywords for primary HDU.");
3018 tablekeys = cpl_propertylist_new();
3019 error = cpl_propertylist_copy_property_regexp(tablekeys, self->proplist,
3020 EXTENSION_HDU_KEYS_REGEXP, 0);
3021 cpl_error_ensure(!error, error,
goto cleanup,
3022 "Failed to extract keywords for extension HDU.");
3025 cpl_error_ensure(self->nelem <= INT_MAX, CPL_ERROR_INCOMPATIBLE_INPUT,
3027 "The value for the keyword '%s' is too big (> %d).",
3028 KEY_NELEM, INT_MAX);
3029 error = cpl_propertylist_append_int(tablekeys, KEY_NELEM, (
int)self->nelem);
3031 cpl_propertylist_set_comment(tablekeys, KEY_NELEM, KEY_NELEM_COMMENT);
3033 !error, error,
goto cleanup,
3034 "Could not add keyword '%s' to primary HDU or set the comment.",
3038 if (extra_theader != NULL) {
3039 error = cpl_propertylist_copy_property_regexp(tablekeys, extra_theader,
3041 cpl_error_ensure(!error, error,
goto cleanup,
3042 "Could not add extra keywords for extension HDU.");
3051 error = CPL_ERROR_NONE;
3052 if (!cpl_propertylist_has(primarykeys, KEY_ORIGIN)) {
3053 error |= cpl_propertylist_append_string(primarykeys, KEY_ORIGIN,
3055 error |= cpl_propertylist_set_comment(primarykeys, KEY_ORIGIN,
3056 KEY_ORIGIN_COMMENT);
3058 if (!cpl_propertylist_has(primarykeys, KEY_PRODLVL)) {
3059 error |= cpl_propertylist_append_int(primarykeys, KEY_PRODLVL,
3061 error |= cpl_propertylist_set_comment(primarykeys, KEY_PRODLVL,
3062 KEY_PRODLVL_COMMENT);
3064 if (!cpl_propertylist_has(primarykeys, KEY_SPECSYS)) {
3065 error |= cpl_propertylist_append_string(primarykeys, KEY_SPECSYS,
3067 error |= cpl_propertylist_set_comment(primarykeys, KEY_SPECSYS,
3068 KEY_SPECSYS_COMMENT);
3070 if (!cpl_propertylist_has(primarykeys, KEY_FLUXERR)) {
3071 error |= cpl_propertylist_append_int(primarykeys, KEY_FLUXERR,
3073 error |= cpl_propertylist_set_comment(primarykeys, KEY_FLUXERR,
3074 KEY_FLUXERR_COMMENT);
3076 if (!cpl_propertylist_has(tablekeys, KEY_VOCLASS)) {
3077 error |= cpl_propertylist_append_string(tablekeys, KEY_VOCLASS,
3079 error |= cpl_propertylist_set_comment(tablekeys, KEY_VOCLASS,
3080 KEY_VOCLASS_COMMENT);
3082 if (!cpl_propertylist_has(tablekeys, KEY_VOPUB)) {
3083 error |= cpl_propertylist_append_string(tablekeys, KEY_VOPUB,
3085 error |= cpl_propertylist_set_comment(tablekeys, KEY_VOPUB,
3088 if (!cpl_propertylist_has(tablekeys, KEY_EXTNAME)) {
3089 error |= cpl_propertylist_append_string(tablekeys, KEY_EXTNAME,
3091 error |= cpl_propertylist_set_comment(tablekeys, KEY_EXTNAME,
3092 KEY_EXTNAME_COMMENT);
3094 if (!cpl_propertylist_has(tablekeys, KEY_INHERIT)) {
3095 error |= cpl_propertylist_append_bool(tablekeys, KEY_INHERIT,
3097 error |= cpl_propertylist_set_comment(tablekeys, KEY_INHERIT,
3098 KEY_INHERIT_COMMENT);
3100 cpl_error_ensure(!error, cpl_error_get_code(),
goto cleanup,
3101 "Could not set default header keywords for file '%s'.",
3104 error = cpl_table_save(self->table, primarykeys, tablekeys, filename,
3106 cpl_error_ensure(!error, error,
goto cleanup,
3107 "Could not save the spectrum table to file '%s'.",
3110 cpl_propertylist_delete(primarykeys);
3111 cpl_propertylist_delete(tablekeys);
3113 return CPL_ERROR_NONE;
3118 cpl_propertylist_delete(primarykeys);
3119 cpl_propertylist_delete(tablekeys);
3121 return cpl_error_get_code();
3126irplib_dfs_save_spectrum(cpl_frameset *allframes,
3127 cpl_propertylist *header,
3128 const cpl_parameterlist *parlist,
3129 const cpl_frameset *usedframes,
3130 const cpl_frame *inherit,
3133 const cpl_propertylist *applist,
3134 const cpl_propertylist *tablelist,
3135 const char *remregexp,
3136 const char *pipe_id,
3137 const char *dict_id,
3138 const char *filename)
3141 cpl_propertylist *plist = NULL;
3142 cpl_frame *product_frame = NULL;
3143 cpl_error_code error;
3145 cpl_ensure_code(allframes != NULL, CPL_ERROR_NULL_INPUT);
3146 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
3147 cpl_ensure_code(usedframes != NULL, CPL_ERROR_NULL_INPUT);
3148 cpl_ensure_code(spectrum != NULL, CPL_ERROR_NULL_INPUT);
3149 cpl_ensure_code(recipe != NULL, CPL_ERROR_NULL_INPUT);
3150 cpl_ensure_code(applist != NULL, CPL_ERROR_NULL_INPUT);
3151 cpl_ensure_code(pipe_id != NULL, CPL_ERROR_NULL_INPUT);
3152 cpl_ensure_code(dict_id != NULL, CPL_ERROR_NULL_INPUT);
3153 cpl_ensure_code(filename != NULL, CPL_ERROR_NULL_INPUT);
3155 procat = cpl_propertylist_get_string(applist, CPL_DFS_PRO_CATG);
3156 cpl_error_ensure(procat != NULL, cpl_error_get_code(),
goto cleanup,
3157 "Could not find keyword '%s' in 'applist'.",
3161 product_frame = cpl_frame_new();
3162 error = cpl_frame_set_filename(product_frame, filename);
3163 error |= cpl_frame_set_tag(product_frame, procat);
3164 error |= cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE);
3165 error |= cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
3166 error |= cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
3167 cpl_error_ensure(!error, cpl_error_get_code(),
goto cleanup,
3168 "Failed to setup the product frame.");
3172 if (header != NULL) {
3173 cpl_propertylist_empty(header);
3177 plist = cpl_propertylist_new();
3181 error = cpl_propertylist_append(plist, applist);
3182 cpl_error_ensure(!error, error,
goto cleanup,
3183 "Could not append extra keywords when writing file '%s'.",
3188 cpl_dfs_setup_product_header(plist, product_frame, usedframes, parlist,
3189 recipe, pipe_id, dict_id, inherit);
3190 cpl_error_ensure(!error, error,
goto cleanup,
3191 "Failed to setup DFS keywords when writing file '%s'.",
3200 error = cpl_propertylist_copy_property_regexp(plist, applist,
".*", 0);
3201 cpl_error_ensure(!error, error,
goto cleanup,
3202 "Could not update extra keywords when writing file '%s'.",
3205 if (remregexp != NULL) {
3206 cpl_errorstate prestate = cpl_errorstate_get();
3207 (void)cpl_propertylist_erase_regexp(plist, remregexp, 0);
3208 cpl_error_ensure(cpl_errorstate_is_equal(prestate),
3209 cpl_error_get_code(),
goto cleanup,
3210 "Failed to filter keywords when writing file '%s'.",
3214 error = irplib_sdp_spectrum_save(spectrum, filename, plist, tablelist);
3215 cpl_error_ensure(!error, error,
goto cleanup,
3216 "Failed to save SPD spectrum to file '%s'.", filename);
3219 if (header != NULL) {
3221 cpl_propertylist_copy_property_regexp(header, spectrum->proplist,
3223 cpl_error_ensure(!error, error,
goto cleanup,
3224 "Could not return SDP keywords in header output.");
3228 error = cpl_frameset_insert(allframes, product_frame);
3230 !error, error,
goto cleanup,
3231 "Failed to insert new product frame when writing file '%s'.", filename);
3234 if (plist != header)
3235 cpl_propertylist_delete(plist);
3237 return CPL_ERROR_NONE;
3242 if (header != NULL) {
3243 cpl_errorstate prestate = cpl_errorstate_get();
3244 (void)cpl_propertylist_empty(header);
3245 cpl_errorstate_set(prestate);
3248 cpl_propertylist_delete(plist);
3250 cpl_frame_delete(product_frame);
3251 return cpl_error_get_code();
3258 if (stream == NULL) {
3262 fprintf(stream,
"NULL SDP spectrum\n\n");
3266 assert(self->proplist != NULL);
3267 assert(self->table != NULL);
3269 fprintf(stream,
"SDP spectrum at address %p\n", (
void *)self);
3270 fprintf(stream,
"NELEM = %" CPL_SIZE_FORMAT
"\n", self->nelem);
3271 cpl_propertylist_dump(self->proplist, stream);
3272 cpl_table_dump_structure(self->table, stream);
3273 cpl_table_dump(self->table, 0, cpl_table_get_nrow(self->table), stream);
3277#ifdef IRPLIB_USE_FITS_UPDATE_CHECKSUM
3291irplib_fits_update_checksums(
const char *filename)
3293 fitsfile *filehandle;
3296 if (fits_open_diskfile(&filehandle, filename, READWRITE, &error)) {
3297 return cpl_error_set_message(
3298 cpl_func, CPL_ERROR_FILE_IO,
3299 "Could not open file '%s' to update CHECKSUM keywords"
3305 while (!fits_movabs_hdu(filehandle, ++i, NULL, &error)) {
3306 if (fits_write_chksum(filehandle, &error)) {
3307 return cpl_error_set_message(
3308 cpl_func, CPL_ERROR_FILE_IO,
3309 "Could not update the CHECKSUM keywords in '%s' HDU %d"
3311 filename, i, error);
3315 if (error == END_OF_FILE)
3318 if (fits_close_file(filehandle, &error)) {
3319 return cpl_error_set_message(
3320 cpl_func, CPL_ERROR_FILE_IO,
3321 "There was a problem trying to close the file '%s'"
3325 return CPL_ERROR_NONE;
struct _irplib_sdp_spectrum_ irplib_sdp_spectrum
Data type for a Science Data Product 1D spectrum.