70 cpl_propertylist *header = NULL;
75 if ((stream = fopen(filename,
"r")) == NULL) {
77 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_FOF,
"%s: %s",
78 SC_ERROR_FOF_TXT, filename);
83 next = cpl_fits_count_extensions(filename);
89 cpl_errorstate_set(CPL_ERROR_NONE);
91 }
else if (next == 0) {
93 header = cpl_propertylist_load(filename, 0);
98 header = cpl_propertylist_load(filename, 1);
100 prop = cpl_propertylist_get_property(header,
"XTENSION");
103 cpl_propertylist_delete(header);
104 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
105 "%s: %s (keyword XTENSION not found)",
106 SC_ERROR_UFS_TXT, filename);
108 if (strcmp(cpl_property_get_string(prop),
"IMAGE") == 0) {
111 }
else if (strcmp(cpl_property_get_string(prop),
"BINTABLE")
117 cpl_propertylist_delete(header);
118 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
119 "%s: %s (keyword XTENSION != IMAGE or BINTABLE)",
120 SC_ERROR_UFS_TXT, filename);
127 if (*fitsformat == 2) {
128 prop = cpl_propertylist_get_property(header,
"NAXIS");
131 cpl_propertylist_delete(header);
132 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
133 "%s: %s (keyword NAXIS not found)",
134 SC_ERROR_UFS_TXT, filename);
136 if (cpl_property_get_int(prop) == 1) {
139 }
else if (cpl_property_get_int(prop) == 2) {
142 }
else if (cpl_property_get_int(prop) == 3) {
147 cpl_propertylist_delete(header);
148 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
149 "%s: %s (keyword NAXIS != 1, 2, or 3)",
150 SC_ERROR_UFS_TXT, filename);
157 cpl_propertylist_delete(header);
160 return cpl_error_get_code();
165 const cpl_parameterlist *parlist)
184 const cpl_parameter *p;
185 char* col_lam = NULL;
186 char* col_flux = NULL;
187 char* col_dflux = NULL;
188 char* col_mask = NULL;
192 cpl_array_set_size(colnames, 4);
195 p = cpl_parameterlist_find_const(parlist,
"col_lam");
196 col_lam = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
197 cpl_array_set_string(colnames, j, col_lam);
199 p = cpl_parameterlist_find_const(parlist,
"col_flux");
200 col_flux = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
201 cpl_array_set_string(colnames, j, col_flux);
205 p = cpl_parameterlist_find_const(parlist,
"col_dflux");
206 col_dflux = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
207 if (strcmp(col_dflux,
"NONE") != 0) {
208 cpl_array_set_string(colnames, j, col_dflux);
211 p = cpl_parameterlist_find_const(parlist,
"col_mask");
212 col_mask = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
213 if (strcmp(col_mask,
"NONE") != 0) {
214 cpl_array_set_string(colnames, j, col_mask);
219 cpl_array_set_size(colnames, j);
224 return CPL_ERROR_NONE;
230 const cpl_parameterlist *parlist)
251 const cpl_parameter *p;
253 cpl_boolean none_lam = CPL_FALSE, none_flux = CPL_FALSE;
256 char* extname = NULL;
258 int ncol0 = 4, ncol = 0, next = 0, check = 0, j = 0, h = 0;
262 cpl_table_set_size(extnames, ncol0);
265 if (cpl_table_has_column(extnames,
"col") != 1) {
266 cpl_table_new_column(extnames,
"col", CPL_TYPE_STRING);
268 if (cpl_table_has_column(extnames,
"extn") != 1) {
269 cpl_table_new_column(extnames,
"extn", CPL_TYPE_INT);
271 cpl_table_fill_column_window_int(extnames,
"extn", 0, ncol0, -1);
277 p = cpl_parameterlist_find_const(parlist,
"col_lam");
278 sprintf(colname,
"%s", cpl_parameter_get_string(p));
279 cpl_table_set_string(extnames,
"col", 0, colname);
280 if (strcmp(colname,
"NONE") == 0) {
286 p = cpl_parameterlist_find_const(parlist,
"col_flux");
287 sprintf(colname,
"%s", cpl_parameter_get_string(p));
288 cpl_table_set_string(extnames,
"col", 1, colname);
289 if (strcmp(colname,
"NONE") == 0) {
291 none_flux = CPL_TRUE;
294 p = cpl_parameterlist_find_const(parlist,
"col_dflux");
295 sprintf(colname,
"%s", cpl_parameter_get_string(p));
296 cpl_table_set_string(extnames,
"col", 2, colname);
297 if (strcmp(colname,
"NONE") == 0) {
301 p = cpl_parameterlist_find_const(parlist,
"col_mask");
302 sprintf(colname,
"%s", cpl_parameter_get_string(p));
303 cpl_table_set_string(extnames,
"col", 3, colname);
304 if (strcmp(colname,
"NONE") == 0) {
309 col = cpl_table_get_data_string(extnames,
"col");
310 extn = cpl_table_get_data_int(extnames,
"extn");
318 for (check = 0, j = 0; j <= next; j++) {
319 prop = cpl_propertylist_get_property(vecdat->head[j],
"EXTNAME");
320 if (prop == NULL && j > 0) {
322 }
else if (prop == NULL && j == 0) {
325 if (none_flux == CPL_TRUE) {
330 extname = cpl_sprintf(
"%s", cpl_property_get_string(prop));
331 for (h = 0; h < ncol0; h++) {
332 if (strncmp(extname, col[h],
SC_LENLINE+1) == 0) {
342 if (extn[0] < 0 && none_lam == CPL_FALSE) {
343 sprintf(errtxt,
"%s: scvarr *vecdat (wavelength extension '%s' "
344 "not found)", SC_ERROR_IOS_TXT, col[0]);
345 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
348 if (extn[1] < 0 && none_flux == CPL_FALSE) {
349 sprintf(errtxt,
"%s: scvarr *vecdat (flux extension '%s' not "
350 "found)", SC_ERROR_IOS_TXT, col[1]);
351 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
354 if (extn[2] < 0 && strcmp(col[2],
"NONE") != 0) {
355 sprintf(errtxt,
"%s: scvarr *vecdat (flux error extension '%s' "
356 "not found)", SC_ERROR_IOS_TXT, col[2]);
357 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
360 if (extn[3] < 0 && strcmp(col[3],
"NONE") != 0) {
361 sprintf(errtxt,
"%s: scvarr *vecdat (mask extension '%s' not "
362 "found)", SC_ERROR_IOS_TXT, col[3]);
363 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
368 return CPL_ERROR_NONE;
374 const cpl_parameterlist *parlist)
395 const cpl_parameter *p;
397 cpl_boolean none_lam = CPL_FALSE, none_flux = CPL_FALSE;
400 char* extname = NULL;
402 int ncol0 = 4, ncol = 0, next = 0, check = 0, j = 0, h = 0;
406 cpl_table_set_size(extnames, ncol0);
409 if (cpl_table_has_column(extnames,
"col") != 1) {
410 cpl_table_new_column(extnames,
"col", CPL_TYPE_STRING);
412 if (cpl_table_has_column(extnames,
"extn") != 1) {
413 cpl_table_new_column(extnames,
"extn", CPL_TYPE_INT);
415 cpl_table_fill_column_window_int(extnames,
"extn", 0, ncol0, -1);
421 p = cpl_parameterlist_find_const(parlist,
"col_lam");
422 sprintf(colname,
"%s", cpl_parameter_get_string(p));
423 cpl_table_set_string(extnames,
"col", 0, colname);
424 if (strcmp(colname,
"NONE") == 0) {
430 p = cpl_parameterlist_find_const(parlist,
"col_flux");
431 sprintf(colname,
"%s", cpl_parameter_get_string(p));
432 cpl_table_set_string(extnames,
"col", 1, colname);
433 if (strcmp(colname,
"NONE") == 0) {
435 none_flux = CPL_TRUE;
438 p = cpl_parameterlist_find_const(parlist,
"col_dflux");
439 sprintf(colname,
"%s", cpl_parameter_get_string(p));
440 cpl_table_set_string(extnames,
"col", 2, colname);
441 if (strcmp(colname,
"NONE") == 0) {
445 p = cpl_parameterlist_find_const(parlist,
"col_mask");
446 sprintf(colname,
"%s", cpl_parameter_get_string(p));
447 cpl_table_set_string(extnames,
"col", 3, colname);
448 if (strcmp(colname,
"NONE") == 0) {
453 col = cpl_table_get_data_string(extnames,
"col");
454 extn = cpl_table_get_data_int(extnames,
"extn");
462 for (check = 0, j = 0; j <= next; j++) {
463 prop = cpl_propertylist_get_property(imadat->head[j],
"EXTNAME");
464 if (prop == NULL && j > 0) {
466 }
else if (prop == NULL && j == 0) {
469 if (none_flux == CPL_TRUE) {
474 extname = cpl_sprintf(
"%s", cpl_property_get_string(prop));
475 for (h = 0; h < ncol0; h++) {
476 if (strncmp(extname, col[h],
SC_LENLINE+1) == 0) {
486 if (extn[0] < 0 && none_lam == CPL_FALSE) {
487 sprintf(errtxt,
"%s: sciarr *imadat (wavelength extension '%s' "
488 "not found)", SC_ERROR_IOS_TXT, col[0]);
489 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
492 if (extn[1] < 0 && none_flux == CPL_FALSE) {
493 sprintf(errtxt,
"%s: sciarr *imadat (flux extension '%s' not "
494 "found)", SC_ERROR_IOS_TXT, col[1]);
495 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
498 if (extn[2] < 0 && strcmp(col[2],
"NONE") != 0) {
499 sprintf(errtxt,
"%s: sciarr *imadat (flux error extension '%s' "
500 "not found)", SC_ERROR_IOS_TXT, col[2]);
501 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
504 if (extn[3] < 0 && strcmp(col[3],
"NONE") != 0) {
505 sprintf(errtxt,
"%s: sciarr *imadat (mask extension '%s' not "
506 "found)", SC_ERROR_IOS_TXT, col[3]);
507 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
512 return CPL_ERROR_NONE;
517 const cpl_table *extnames)
542 int ncol = 0, next = 0, h = 0, nrow = 0, j = 0, i = 0;
544 double crpix = 0., crval = 0., cdelt = 0.;
549 tabdat->tab[0] = cpl_table_new(0);
550 tabdat->tab[1] = cpl_table_new(0);
553 tabdat->head[0] = cpl_propertylist_duplicate(vecdat->head[0]);
554 tabdat->head[1] = cpl_propertylist_new();
557 ncol = cpl_table_get_nrow(extnames);
560 sprintf(errtxt,
"%s: cpl_table *extnames", SC_ERROR_NDA_TXT);
561 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
565 if (cpl_table_has_column(extnames,
"col") != 1 ||
566 cpl_table_has_column(extnames,
"extn") != 1) {
568 sprintf(errtxt,
"%s: cpl_table *extnames (column names)",
570 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
574 if (cpl_table_get_column_max(extnames,
"extn") == -1) {
576 sprintf(errtxt,
"%s: cpl_table *extnames (no extension with valid "
577 "data)", SC_ERROR_IOV_TXT);
578 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
585 if (cpl_table_get_column_max(extnames,
"extn") > next) {
587 sprintf(errtxt,
"%s: cpl_table *extnames (incorrect extension "
588 "number)", SC_ERROR_IOV_TXT);
589 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
593 col = cpl_table_get_data_string_const(extnames,
"col");
594 extn = cpl_table_get_data_int_const(extnames,
"extn");
597 cpl_table_new_column(tabdat->tab[1], col[0], CPL_TYPE_DOUBLE);
598 for (h = 1; h < ncol; h++) {
600 cpl_table_new_column(tabdat->tab[1], col[h], CPL_TYPE_DOUBLE);
605 nrow = cpl_vector_get_size(vecdat->vec[0]);
608 sprintf(errtxt,
"%s: scvarr *vecdat", SC_ERROR_NDA_TXT);
609 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
610 }
else if (nrow > 0 && next > 0) {
611 for (j = 1; j <= next; j++) {
612 if (cpl_vector_get_size(vecdat->vec[j]) != nrow) {
614 sprintf(errtxt,
"%s: scvarr *vecdat (vector size differs for "
615 "different extensions", SC_ERROR_IOS_TXT);
616 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
621 cpl_table_set_size(tabdat->tab[1], nrow);
627 if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
629 sprintf(errtxt,
"%s: scvarr *vecdat (keyword CRPIX1 not found)",
631 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
635 if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
637 sprintf(errtxt,
"%s: scvarr *vecdat (keyword CRVAL1 not found)",
639 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
643 if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
645 sprintf(errtxt,
"%s: scvarr *vecdat (keyword CDELT1 not found)",
647 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
653 for (h = 0; h < ncol; h++) {
654 for (i = 0; i < nrow; i++) {
656 cpl_table_set(tabdat->tab[1], col[h], i,
657 cpl_vector_get(vecdat->vec[extn[h]], i));
658 }
else if (extn[0] < 0 && h == 0) {
659 cpl_table_set(tabdat->tab[1], col[h], i,
660 crval + (i - crpix + 1) * cdelt);
665 return CPL_ERROR_NONE;
686 const cpl_errorstate cleanstate = cpl_errorstate_get();
688 d = cpl_propertylist_get_double(plist, key);
691 if (cpl_error_get_code() == CPL_ERROR_TYPE_MISMATCH) {
692 cpl_errorstate_set(cleanstate);
693 d = (double) cpl_propertylist_get_int(plist, key);
700static cpl_table * sc_convert_sdp_table(
const cpl_table * inptable,
701 const char * col_lam,
const char * col_flux,
702 const char * col_dflux,
const char * col_mask)
722 const cpl_array * alam = cpl_table_get_array(inptable, col_lam, 0);
724 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"Expected %s "
725 "array column in input", col_lam);;
728 const cpl_array * aflux = cpl_table_get_array(inptable, col_flux, 0);
730 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"Expected %s "
731 "array column in input", col_flux);
734 const cpl_array * adflux = NULL, * amask = NULL;
736 adflux = cpl_table_get_array(inptable, col_dflux, 0);
737 if (adflux == NULL) {
738 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"Expected %s "
739 "array column in input", col_dflux);
744 amask = cpl_table_get_array(inptable, col_mask, 0);
745 if (adflux == NULL) {
746 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"Expected %s "
747 "array column in input", col_mask);
751 if (cpl_array_get_size(alam) != cpl_array_get_size(aflux) ||
752 (adflux && cpl_array_get_size(alam) != cpl_array_get_size(adflux)) ||
753 (amask && cpl_array_get_size(alam) != cpl_array_get_size(amask))) {
754 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
755 "Table columns do not all have the same size");
759 cpl_table * ntab = cpl_table_new(cpl_array_get_size(alam));
762 cpl_array * dalam = cpl_array_cast((cpl_array*)alam, CPL_TYPE_DOUBLE);
763 cpl_table_new_column(ntab, col_lam, CPL_TYPE_DOUBLE);
764 cpl_table_copy_data_double(ntab, col_lam, cpl_array_get_data_double_const(dalam));
765 cpl_array_delete(dalam);
767 cpl_array * daflux = cpl_array_cast((cpl_array*)aflux, CPL_TYPE_DOUBLE);
768 cpl_table_new_column(ntab, col_flux, CPL_TYPE_DOUBLE);
769 cpl_table_copy_data_double(ntab, col_flux, cpl_array_get_data_double_const(daflux));
770 cpl_array_delete(daflux);
773 cpl_array * dadflux = cpl_array_cast((cpl_array*)adflux, CPL_TYPE_DOUBLE);
774 cpl_table_new_column(ntab, col_dflux, CPL_TYPE_DOUBLE);
775 cpl_table_copy_data_double(ntab, col_dflux, cpl_array_get_data_double_const(dadflux));
776 cpl_array_delete(dadflux);
779 cpl_table_new_column(ntab, col_mask, CPL_TYPE_INT);
780 cpl_table_copy_data_int(ntab, col_mask, cpl_array_get_data_int_const(amask));
788 const cpl_parameterlist *parlist)
813 const cpl_parameter *p;
814 char* col_lam = NULL;
815 char* col_flux = NULL;
816 char* col_dflux = NULL;
817 char* col_mask = NULL;
821 cpl_boolean exerr = CPL_TRUE, exmask = CPL_TRUE, isnanflux = CPL_FALSE;
822 cpl_boolean isnanum = CPL_FALSE, isedgeflux = CPL_FALSE;
823 cpl_boolean isedge = CPL_FALSE, isnandflux = CPL_FALSE;
824 cpl_boolean isoutrange = CPL_FALSE, is0 = CPL_FALSE, isnomask = CPL_FALSE;
825 cpl_boolean ismask0 = CPL_FALSE;
826 int next = 0, nrow = 0, edgepix = 0, i = 0, nmask0 = 0, imask = 0;
827 double minflux = 0., maxflux = 0., flux = 0., dflux = 0., mask = 0.;
830 p = cpl_parameterlist_find_const(parlist,
"col_lam");
831 col_lam = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
832 if (strcmp(col_lam,
"NONE") == 0) {
835 p = cpl_parameterlist_find_const(parlist,
"col_flux");
836 col_flux = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
837 if (strcmp(col_flux,
"NONE") == 0) {
842 p = cpl_parameterlist_find_const(parlist,
"col_dflux");
843 col_dflux = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
844 if (strcmp(col_dflux,
"NONE") == 0) {
847 p = cpl_parameterlist_find_const(parlist,
"col_mask");
848 col_mask = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
849 if (strcmp(col_mask,
"NONE") == 0) {
856 sprintf(errtxt,
"%s: sctarr *tabdat", SC_ERROR_NDA_TXT);
857 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
858 }
else if (next > 1) {
859 sprintf(errtxt,
"%s: sctarr *tabdat (number of extensions != 1)",
861 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
865 if (cpl_table_has_column(tabdat->tab[1], col_lam) != 1) {
866 sprintf(errtxt,
"%s: sctarr *tabdat (wavelength column '%s' not "
867 "found)", SC_ERROR_IOS_TXT, col_lam);
868 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
870 if (cpl_table_has_column(tabdat->tab[1], col_flux) != 1) {
871 sprintf(errtxt,
"%s: sctarr *tabdat (flux column '%s' not found)",
872 SC_ERROR_IOS_TXT, col_flux);
873 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
876 if (cpl_table_has_column(tabdat->tab[1], col_dflux) != 1) {
877 sprintf(errtxt,
"%s: sctarr *tabdat (flux error column '%s' not "
878 "found)", SC_ERROR_IOS_TXT, col_dflux);
879 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
883 if (exmask == TRUE) {
884 if (cpl_table_has_column(tabdat->tab[1], col_mask) != 1) {
885 sprintf(errtxt,
"%s: sctarr *tabdat (mask column '%s' not found)",
886 SC_ERROR_IOS_TXT, col_mask);
887 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
893 if (cpl_propertylist_has(tabdat->head[1],
"VOCLASS") &&
894 strncmp(cpl_propertylist_get_string(tabdat->head[1],
896 "SPECTRUM", strlen(
"SPECTRUM")) == 0) {
897 cpl_table * ntab = sc_convert_sdp_table(tabdat->tab[1], col_lam, col_flux,
898 exerr ? col_dflux : NULL,
899 exmask ? col_mask : NULL);
900 if (cpl_error_get_code()) {
901 return cpl_error_get_code();
903 cpl_table_delete(tabdat->tab[1]);
904 tabdat->tab[1] = ntab;
909 if (exmask == CPL_TRUE) {
910 sprintf(col_imask,
"%s_I", col_mask);
914 if (cpl_table_has_column(tabdat->tab[1], col_imask) == 1) {
915 sprintf(col_renamed,
"%s_orig", col_imask);
916 if (cpl_table_has_column(tabdat->tab[1], col_renamed) != 1) {
917 cpl_msg_debug(cpl_func,
"Name of internal integer mask already "
918 "used: Rename %s in %s", col_imask, col_renamed);
919 cpl_table_name_column(tabdat->tab[1], col_imask, col_renamed);
921 cpl_msg_debug(cpl_func,
"Use of reserved mask column names: "
922 "Erase %s, keep %s", col_imask, col_renamed);
923 cpl_table_erase_column(tabdat->tab[1], col_imask);
926 cpl_table_new_column(tabdat->tab[1], col_imask, CPL_TYPE_INT);
929 nrow = cpl_table_get_nrow(tabdat->tab[1]);
935 for (minflux = 0., maxflux = 0., i = edgepix; i < nrow - edgepix; i++) {
936 flux = cpl_table_get(tabdat->tab[1], col_flux, i, NULL);
937 if (flux < minflux) {
939 }
else if (flux > maxflux) {
946 for (nmask0 = 0, i = 0; i < nrow; i++) {
948 flux = cpl_table_get(tabdat->tab[1], col_flux, i, NULL);
949 if (isnan(flux) != 0) {
950 cpl_table_set(tabdat->tab[1], col_flux, i, 0.);
951 isnanflux = CPL_TRUE;
953 isedgeflux = CPL_FALSE;
956 isnanflux = CPL_FALSE;
957 isedgeflux = CPL_TRUE;
960 isnanflux = CPL_FALSE;
961 isedgeflux = CPL_FALSE;
964 if (exerr == CPL_TRUE) {
965 dflux = cpl_table_get(tabdat->tab[1], col_dflux, i, NULL);
966 if (dflux <= 0 || isnan(dflux) != 0) {
967 cpl_table_set(tabdat->tab[1], col_dflux, i, 0.);
968 isnandflux = CPL_TRUE;
969 isoutrange = CPL_TRUE;
971 isnandflux = CPL_FALSE;
975 if (exmask == CPL_TRUE) {
976 mask = cpl_table_get(tabdat->tab[1], col_mask, i, NULL);
980 if (isnanflux == CPL_TRUE || isnandflux == CPL_TRUE ||
981 isedgeflux == CPL_TRUE) {
982 cpl_table_set(tabdat->tab[1], col_imask, i, -1);
983 if (mask != 0 && mask != 1) {
985 }
else if (mask == 0) {
991 cpl_table_set(tabdat->tab[1], col_imask, i, 0);
995 cpl_table_set(tabdat->tab[1], col_imask, i, 1);
1005 if (isnomask == CPL_TRUE && is0 == CPL_FALSE) {
1006 sprintf(errtxt,
"%s: sctarr *tabdat (all mask value(s) != 0 or 1)",
1008 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1013 for (i = 0; i < nrow; i++) {
1014 imask = cpl_table_get(tabdat->tab[1], col_imask, i, NULL);
1015 if (isnomask == CPL_TRUE) {
1017 cpl_table_set(tabdat->tab[1], col_imask, i, 1);
1019 cpl_table_set(tabdat->tab[1], col_imask, i, 0);
1023 cpl_table_set(tabdat->tab[1], col_imask, i, 0);
1024 }
else if (nmask0 == nrow) {
1025 cpl_table_set(tabdat->tab[1], col_imask, i, 1);
1034 if (isnanum == CPL_TRUE) {
1035 cpl_msg_debug(cpl_func,
"Input data: flux(es) = 'nan' "
1039 if (isedge == CPL_TRUE) {
1040 cpl_msg_debug(cpl_func,
"Input data: unreliable edge flux(es) "
1044 if (isoutrange == CPL_TRUE) {
1045 cpl_msg_debug(cpl_func,
"Input data: error(s) <= 0 or 'nan' "
1049 if (isnomask == CPL_TRUE) {
1050 cpl_msg_debug(cpl_func,
"Input data: mask value(s) != 0 or 1 "
1051 "-> reverse definition (0 -> 1; != 0 -> 0)");
1054 if (ismask0 == CPL_TRUE) {
1055 cpl_msg_debug(cpl_func,
"Input data: all mask values = 0 "
1056 "-> reverse definition (0 -> 1)");
1061 cpl_free(col_dflux);
1064 return CPL_ERROR_NONE;
1068 const cpl_parameterlist *parlist)
1085 const cpl_parameter *p;
1086 char* basedir = NULL;
1088 char* outname = NULL;
1089 char* spectype = NULL;
1094 p = cpl_parameterlist_find_const(parlist,
"inst_dir");
1095 basedir = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
1096 p = cpl_parameterlist_find_const(parlist,
"output_dir");
1098 p = cpl_parameterlist_find_const(parlist,
"output_name");
1099 outname = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
1100 p = cpl_parameterlist_find_const(parlist,
"spectype");
1101 spectype = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
1102 if (strncmp(spectype,
"SCI", 3) == 0) {
1103 sprintf(filename,
"%s%s_sci.fits", outdir, outname);
1104 }
else if (strncmp(spectype,
"SKY", 3) == 0) {
1105 sprintf(filename,
"%s%s_sky.fits", outdir, outname);
1107 sprintf(errtxt,
"%s: cpl_parameterlist *parlist (spectype neither "
1108 "'SCI' nor 'SKY')", SC_ERROR_IOV_TXT);
1109 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
1118 return cpl_error_get_code();
1147 int next = 0, nrow = 0;
1150 if (cpl_table_has_column(results,
"scflux") != 1) {
1151 sprintf(errtxt,
"%s: cpl_table *results (required column 'scflux' "
1152 "not present)", SC_ERROR_IOS_TXT);
1153 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1157 next = tabdat->next;
1159 sprintf(errtxt,
"%s: sctarr *tabdat", SC_ERROR_NDA_TXT);
1160 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
1161 }
else if (next > 1) {
1162 sprintf(errtxt,
"%s: sctarr *tabdat (number of extensions != 1)",
1164 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1168 if (cpl_table_has_column(tabdat->tab[1],
"scflux") != 0 ||
1169 cpl_table_has_column(tabdat->tab[1],
"scdflux") != 0 ||
1170 cpl_table_has_column(tabdat->tab[1],
"scmask") != 0) {
1171 sprintf(errtxt,
"%s: sctarr *tabdat ('sc' results column(s) already "
1172 "exist(s))", SC_ERROR_IOS_TXT);
1173 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1177 nrow = cpl_table_get_nrow(results);
1179 sprintf(errtxt,
"%s: cpl_table *results", SC_ERROR_NDA_TXT);
1180 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
1181 }
else if (nrow != cpl_table_get_nrow(tabdat->tab[1])) {
1182 sprintf(errtxt,
"%s: cpl_table *results != sctarr tabdat->tab[1]",
1184 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IDG,
"%s", errtxt);
1188 cpl_table_duplicate_column(tabdat->tab[1],
"scflux", results,
"scflux");
1191 if (cpl_table_has_column(results,
"scdflux") == 1) {
1192 cpl_table_duplicate_column(tabdat->tab[1],
"scdflux",
1193 results,
"scdflux");
1197 if (cpl_table_has_column(results,
"scmask") == 1) {
1198 cpl_table_duplicate_column(tabdat->tab[1],
"scmask",
1202 return CPL_ERROR_NONE;
1207 const cpl_parameterlist *parlist)
1225 const cpl_parameter *p;
1226 char* col_mask = NULL;
1233 p = cpl_parameterlist_find_const(parlist,
"col_mask");
1234 col_mask = cpl_sprintf(
"%s", cpl_parameter_get_string(p));
1235 if (strcmp(col_mask,
"NONE") == 0) {
1238 sprintf(col_imask,
"%s_I", col_mask);
1242 next = tabdat->next;
1244 sprintf(errtxt,
"%s: sctarr *tabdat", SC_ERROR_NDA_TXT);
1245 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
1246 }
else if (next > 1) {
1247 sprintf(errtxt,
"%s: sctarr *tabdat (number of extensions != 1)",
1249 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1253 if (cpl_table_has_column(tabdat->tab[1], col_imask) == 1) {
1254 cpl_table_erase_column(tabdat->tab[1], col_imask);
1259 sprintf(col_renamed,
"%s_orig", col_imask);
1260 if (cpl_table_has_column(tabdat->tab[1], col_renamed) == 1) {
1261 cpl_table_name_column(tabdat->tab[1], col_renamed, col_imask);
1264 return CPL_ERROR_NONE;
1269 const cpl_table *extnames,
1293 cpl_boolean hascol = CPL_TRUE;
1294 int next = 0, extn_flux = 0, extn_dflux = 0, extn_mask = 0, nrow = 0;
1297 double maskval[2] = {0., 1.};
1298 double *flux = NULL, *dflux = NULL, *mask = NULL, *scflux = NULL;
1299 double *scdflux = NULL;
1302 if (cpl_table_get_nrow(extnames) != 4) {
1303 sprintf(errtxt,
"%s: cpl_table *extnames (number of rows != 4)",
1305 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1309 if (cpl_table_has_column(extnames,
"col") != 1 ||
1310 cpl_table_has_column(extnames,
"extn") != 1) {
1311 sprintf(errtxt,
"%s: cpl_table *extnames (column names)",
1313 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1317 if (cpl_table_get_column_max(extnames,
"extn") == -1) {
1318 sprintf(errtxt,
"%s: cpl_table *extnames (no extension with valid "
1319 "data)", SC_ERROR_IOV_TXT);
1320 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
1324 next = vecdat->next;
1327 if (cpl_table_get_column_max(extnames,
"extn") > next) {
1328 sprintf(errtxt,
"%s: cpl_table *extnames (incorrect extension "
1329 "number)", SC_ERROR_IOV_TXT);
1330 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
1334 extn_flux = cpl_table_get(extnames,
"extn", 1, NULL);
1335 extn_dflux = cpl_table_get(extnames,
"extn", 2, NULL);
1336 extn_mask = cpl_table_get(extnames,
"extn", 3, NULL);
1339 if (tabdat->next != 1) {
1340 sprintf(errtxt,
"%s: sctarr *tabdat (number of extensions != 1)",
1342 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1346 nrow = cpl_table_get_nrow(tabdat->tab[1]);
1348 sprintf(errtxt,
"%s: sctarr *tabdat", SC_ERROR_NDA_TXT);
1349 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
1353 for (j = 0; j <= next; j++) {
1354 if (cpl_vector_get_size(vecdat->vec[j]) != nrow) {
1355 sprintf(errtxt,
"%s: scvarr vecdat->vec[%d] != "
1356 "sctarr tabdat->tab[1]", SC_ERROR_IDG_TXT, j);
1357 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IDG,
"%s",
1364 return cpl_error_get_code();
1368 flux = cpl_vector_get_data(vecdat->vec[extn_flux]);
1369 if (extn_dflux >= 0) {
1370 dflux = cpl_vector_get_data(vecdat->vec[extn_dflux]);
1372 if (extn_mask >= 0) {
1373 mask = cpl_vector_get_data(vecdat->vec[extn_mask]);
1377 scflux = cpl_table_get_data_double(tabdat->tab[1],
"scflux");
1378 if (scflux == NULL) hascol = CPL_FALSE;
1379 if (extn_dflux >= 0) {
1380 scdflux = cpl_table_get_data_double(tabdat->tab[1],
"scdflux");
1381 if (scdflux == NULL) hascol = CPL_FALSE;
1383 if (extn_mask >= 0) {
1384 scmask = cpl_table_get_data_int(tabdat->tab[1],
"scmask");
1385 if (scmask == NULL) hascol = CPL_FALSE;
1387 if (hascol == CPL_FALSE) {
1388 cpl_table_dump_structure(tabdat->tab[1], NULL);
1389 sprintf(errtxt,
"%s: sctarr *tabdat (required 'sc' column(s) not "
1390 "present)", SC_ERROR_IOS_TXT);
1391 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1395 for (i = 0; i < nrow; i++) {
1396 flux[i] = scflux[i];
1397 if (extn_dflux >= 0) {
1398 dflux[i] = scdflux[i];
1400 if (extn_mask >= 0) {
1402 if (mask[i] == maskval[1] && scmask[i] == 0) {
1403 mask[i] = maskval[0];
1408 return CPL_ERROR_NONE;
1413 const cpl_table *extnames)
1434 cpl_boolean hascol = CPL_TRUE;
1435 cpl_size imaskmaxpos = 0;
1437 double maskmin = 0., maskmax = 0.;
1444 if (cpl_table_get_nrow(extnames) != 4) {
1445 sprintf(errtxt,
"%s: cpl_table *extnames (number of rows != 4)",
1447 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1451 if (cpl_table_has_column(extnames,
"col") != 1 ||
1452 cpl_table_has_column(extnames,
"extn") != 1) {
1453 sprintf(errtxt,
"%s: cpl_table *extnames (column names)",
1455 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1459 sprintf(col_mask,
"%s", cpl_table_get_string(extnames,
"col", 3));
1460 extn_mask = cpl_table_get_int(extnames,
"extn", 3, NULL);
1463 if (extn_mask >= 0) {
1464 sprintf(col_imask,
"%s_I", col_mask);
1470 if (tabdat->next != 1) {
1471 sprintf(errtxt,
"%s: sctarr *tabdat (number of extensions != 1)",
1473 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1477 if (cpl_table_get_nrow(tabdat->tab[1]) == 0) {
1478 sprintf(errtxt,
"%s: sctarr *tabdat", SC_ERROR_NDA_TXT);
1479 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
1483 if (extn_mask >= 0) {
1484 if (cpl_table_has_column(tabdat->tab[1], col_mask) != 1) {
1488 if (cpl_table_has_column(tabdat->tab[1], col_imask) != 1) {
1491 if (hascol == CPL_FALSE) {
1492 sprintf(errtxt,
"%s: sctarr *tabdat (required input column(s) not "
1493 "present)", SC_ERROR_IOS_TXT);
1494 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1498 if (extn_mask >= 0) {
1499 maskmin = cpl_table_get_column_min(tabdat->tab[1], col_mask);
1500 maskmax = cpl_table_get_column_max(tabdat->tab[1], col_mask);
1501 cpl_table_get_column_maxpos(tabdat->tab[1], col_imask, &imaskmaxpos);
1502 if (cpl_table_get_int(tabdat->tab[1], col_imask, imaskmaxpos, NULL)
1504 maskval[0] = maskmax;
1505 maskval[1] = maskmin;
1507 maskval[0] = maskmin;
1508 maskval[1] = maskmax;
1512 return CPL_ERROR_NONE;
1517 const cpl_table *extnames,
1518 const double minrelprofflux)
1560 cpl_vector *row, *prof, *relsum;
1562 int next = 0, h = 0, nx = 0, ny = 0, j = 0, i = 0, qual = 0, spix = 0;
1564 double maxerr = 0., maskval[2] = {0., 0.}, profmin = 0., profmax = 0.;
1565 double proflim = 0., profval = 0., sum = 0., scale = 0., mask = 0.;
1569 if (minrelprofflux < 0. || minrelprofflux > 1.) {
1570 sprintf(errtxt,
"%s: double minrelprofflux (value outside [0,1])",
1572 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP,
"%s", errtxt);
1576 next = imadat->next;
1581 for (h = 0; h <= next; h++) {
1582 vecdat->vec[h] = cpl_vector_new(1);
1586 for (h = 0; h <= next; h++) {
1587 vecdat->head[h] = cpl_propertylist_duplicate(imadat->head[h]);
1588 cpl_propertylist_erase(vecdat->head[h],
"CRPIX2");
1589 cpl_propertylist_erase(vecdat->head[h],
"CRVAL2");
1590 cpl_propertylist_erase(vecdat->head[h],
"CDELT2");
1591 cpl_propertylist_erase(vecdat->head[h],
"CTYPE2");
1595 if (cpl_table_get_nrow(extnames) != 4) {
1597 sprintf(errtxt,
"%s: cpl_table *extnames (number of rows != 4)",
1599 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1603 if (cpl_table_has_column(extnames,
"col") != 1 ||
1604 cpl_table_has_column(extnames,
"extn") != 1) {
1606 sprintf(errtxt,
"%s: cpl_table *extnames (column names)",
1608 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1612 if (cpl_table_get_column_max(extnames,
"extn") == -1) {
1614 sprintf(errtxt,
"%s: cpl_table *extnames (no extension with valid "
1615 "data)", SC_ERROR_IOV_TXT);
1616 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
1620 if (cpl_table_get_column_max(extnames,
"extn") > next) {
1622 sprintf(errtxt,
"%s: cpl_table *extnames (incorrect extension "
1623 "number)", SC_ERROR_IOV_TXT);
1624 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
1628 extn = cpl_table_get_data_int_const(extnames,
"extn");
1631 nx = cpl_image_get_size_x(imadat->ima[0]);
1632 ny = cpl_image_get_size_y(imadat->ima[0]);
1633 if (nx <= 0 || ny <= 0) {
1635 sprintf(errtxt,
"%s: sciarr *imadat", SC_ERROR_NDA_TXT);
1636 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
1637 }
else if (nx > 0 && ny > 0 && next > 0) {
1638 for (h = 1; h <= next; h++) {
1639 if (cpl_image_get_size_x(imadat->ima[h]) != nx ||
1640 cpl_image_get_size_y(imadat->ima[h]) != ny) {
1642 sprintf(errtxt,
"%s: sciarr *imadat (image size differs for "
1643 "different extensions", SC_ERROR_IOS_TXT);
1644 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
1652 maxerr = cpl_image_get_max(imadat->ima[extn[2]]);
1659 return (cpl_error_code)SC_ERROR_IOV;
1664 row = cpl_vector_new(nx);
1665 prof = cpl_vector_new(ny);
1668 for (j = 0; j < ny; j++) {
1669 for (i = 0; i < nx; i++) {
1670 cpl_vector_set(row, i,
1671 cpl_image_get(imadat->ima[extn[1]], i+1, j+1,
1674 cpl_vector_set(prof, j, cpl_vector_get_median(row));
1678 cpl_vector_delete(row);
1681 profmin = cpl_vector_get_min(prof);
1682 cpl_vector_subtract_scalar(prof, profmin);
1685 profmax = cpl_vector_get_max(prof);
1686 proflim = minrelprofflux * profmax;
1687 for (j = 0; j < ny; j++) {
1688 profval = cpl_vector_get(prof, j);
1689 if (profval < proflim) {
1690 cpl_vector_set(prof, j, 0.);
1695 for (sum = 0., j = 0; j < ny; j++) {
1696 sum += cpl_vector_get(prof, j);
1700 cpl_vector_delete(prof);
1701 sprintf(errtxt,
"%s: sciarr *imadat (flux sum = 0)",
1703 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
1705 cpl_vector_divide_scalar(prof, sum);
1708 relsum = cpl_vector_new(nx);
1710 for (i = 0; i < nx; i++) {
1711 for (sum = 0., j = 0; j < ny; j++) {
1712 if (cpl_image_get(imadat->ima[extn[3]], i+1, j+1, &qual) <
1714 sum += cpl_vector_get(prof, j);
1717 cpl_vector_set(relsum, i, sum);
1720 cpl_vector_fill(relsum, 1.);
1724 for (h = 0; h <= next; h++) {
1725 cpl_vector_set_size(vecdat->vec[h], nx);
1729 cpl_vector_set_size(selpix, nx);
1730 cpl_vector_fill(selpix, 0.);
1735 for (h = 0; h <= next; h++) {
1736 for (i = 0; i < nx; i++) {
1739 scale = cpl_vector_get(relsum, i);
1745 if (scale < 2 *
SC_TOL) {
1746 cpl_vector_set(vecdat->vec[extn[3]], i, maskval[0]);
1748 cpl_vector_set(vecdat->vec[extn[3]], i, maskval[1]);
1755 for (sum = 0., spix = 0, j = 0; j < ny; j++) {
1758 profval = cpl_vector_get(prof, j);
1759 if (profval == 0.) {
1765 mask = cpl_image_get(imadat->ima[extn[3]], i+1, j+1,
1772 val = cpl_image_get(imadat->ima[h], i+1, j+1, &qual);
1773 if (val <= 0. && h == extn[0]) {
1775 cpl_vector_delete(prof);
1776 cpl_vector_delete(relsum);
1777 sprintf(errtxt,
"%s: sciarr *imadat "
1778 "(wavelength <= 0)", SC_ERROR_IOV_TXT);
1779 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
1784 if (mask == maskval[1] || h == extn[0]) {
1803 cpl_vector_set(selpix, i, (
double) spix / scale);
1805 cpl_vector_set(selpix, i, (
double) spix);
1813 }
else if (h == extn[2]) {
1815 if (sum > 0. && scale > 0.) {
1816 sum = sqrt(sum) / scale;
1828 cpl_vector_set(vecdat->vec[h], i, sum);
1836 cpl_vector_delete(prof);
1837 cpl_vector_delete(relsum);
1839 return CPL_ERROR_NONE;
1844 const cpl_table *extnames,
1845 const double lowpixfrac,
1846 const cpl_vector *selpix)
1884 cpl_vector *yvec, *ysel;
1886 int next = 0, h = 0, nx = 0, ny = 0, j = 0, i = 0, ngood = 0, qual = 0;
1889 double maskval[2] = {0., 0.};
1890 double mask = 0., val = 0., med = 0., nmed = 0.;
1893 if (lowpixfrac < 0. || lowpixfrac > 1.) {
1894 sprintf(errtxt,
"%s: double lowfracpix (value outside [0,1])",
1896 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP,
"%s", errtxt);
1900 if (cpl_vector_get_max(selpix) > 0.) {
1902 sprintf(errtxt,
"%s: cpl_vector *selpix (invalid elements or all 0)",
1904 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
1908 next = imadat->next;
1913 for (h = 0; h <= next; h++) {
1914 vecdat->vec[h] = cpl_vector_new(1);
1918 for (h = 0; h <= next; h++) {
1919 vecdat->head[h] = cpl_propertylist_duplicate(imadat->head[h]);
1920 cpl_propertylist_erase(vecdat->head[h],
"CRPIX2");
1921 cpl_propertylist_erase(vecdat->head[h],
"CRVAL2");
1922 cpl_propertylist_erase(vecdat->head[h],
"CDELT2");
1923 cpl_propertylist_erase(vecdat->head[h],
"CTYPE2");
1927 if (cpl_table_get_nrow(extnames) != 4) {
1929 sprintf(errtxt,
"%s: cpl_table *extnames (number of rows != 4)",
1931 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1935 if (cpl_table_has_column(extnames,
"col") != 1 ||
1936 cpl_table_has_column(extnames,
"extn") != 1) {
1938 sprintf(errtxt,
"%s: cpl_table *extnames (column names)",
1940 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1944 if (cpl_table_get_column_max(extnames,
"extn") == -1) {
1946 sprintf(errtxt,
"%s: cpl_table *extnames (no extension with valid "
1947 "data)", SC_ERROR_IOV_TXT);
1948 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
1952 if (cpl_table_get_column_max(extnames,
"extn") > next) {
1954 sprintf(errtxt,
"%s: cpl_table *extnames (incorrect extension "
1955 "number)", SC_ERROR_IOV_TXT);
1956 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
1960 extn = cpl_table_get_data_int_const(extnames,
"extn");
1963 nx = cpl_image_get_size_x(imadat->ima[0]);
1964 ny = cpl_image_get_size_y(imadat->ima[0]);
1965 if (nx <= 0 || ny <= 0) {
1967 sprintf(errtxt,
"%s: sciarr *imadat", SC_ERROR_NDA_TXT);
1968 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
1969 }
else if (nx > 0 && ny > 0 && next > 0) {
1970 for (h = 1; h <= next; h++) {
1971 if (cpl_image_get_size_x(imadat->ima[h]) != nx ||
1972 cpl_image_get_size_y(imadat->ima[h]) != ny) {
1974 sprintf(errtxt,
"%s: sciarr *imadat (image size differs for "
1975 "different extensions", SC_ERROR_IOS_TXT);
1976 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
1983 if (cpl_vector_get_size(selpix) != nx) {
1985 sprintf(errtxt,
"%s: cpl_vector *selpix (size) != sciarr *imadat "
1986 "(x size)", SC_ERROR_IOS_TXT);
1987 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
1995 return (cpl_error_code)SC_ERROR_IOV;
2000 yvec = cpl_vector_new(ny);
2001 ysel = cpl_vector_new(ny);
2004 for (h = 0; h <= next; h++) {
2005 cpl_vector_set_size(vecdat->vec[h], nx);
2011 for (h = 0; h <= next; h++) {
2012 for (i = 0; i < nx; i++) {
2014 for (ngood = 0, j = 0; j < ny; j++) {
2018 mask = cpl_image_get(imadat->ima[extn[3]], i+1, j+1,
2025 if (mask == maskval[1]) {
2035 val = cpl_image_get(imadat->ima[h], i+1, j+1, &qual);
2036 if (val <= 0. && h == extn[0]) {
2038 cpl_vector_delete(yvec);
2039 cpl_vector_delete(ysel);
2040 sprintf(errtxt,
"%s: sciarr *imadat (wavelength <= 0)",
2042 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s",
2047 cpl_vector_set(yvec, j, val);
2048 if (mask == maskval[1]) {
2050 cpl_vector_set(ysel, j, val);
2053 cpl_vector_set(ysel, j, HUGE_VAL);
2062 cpl_vector_set(vecdat->vec[extn[3]], i, maskval[0]);
2064 cpl_vector_set(vecdat->vec[extn[3]], i, maskval[1]);
2071 cpl_vector_sort(ysel, CPL_SORT_ASCENDING);
2072 jmed = ceil(lowpixfrac * ngood) - 1;
2076 med = cpl_vector_get(ysel, jmed);
2078 cpl_vector_sort(yvec, CPL_SORT_ASCENDING);
2079 jmed = ceil(lowpixfrac * ny) - 1;
2083 med = cpl_vector_get(yvec, jmed);
2088 nmed = 2. * ngood *
SC_MIN(lowpixfrac, 1. - lowpixfrac);
2095 if (h == extn[1] || h == extn[2]) {
2096 med *= cpl_vector_get(selpix, i);
2101 cpl_vector_set(vecdat->vec[h], i, med);
2107 cpl_vector_delete(yvec);
2108 cpl_vector_delete(ysel);
2110 return CPL_ERROR_NONE;
2115 const cpl_array *colnames)
2140 int ncolmin = 0, nrec = 0, j = 0, i = 0;
2144 ncolmin = cpl_array_get_size(colnames);
2145 if (ncolmin == 0 || colnames == NULL) {
2146 sprintf(errtxt,
"%s: cpl_array *colnames", SC_ERROR_NDA_TXT);
2147 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
2151 col = cpl_array_get_data_string_const(colnames);
2154 if ((stream = fopen(filename,
"r")) == NULL) {
2155 sprintf(errtxt,
"%s: %s", SC_ERROR_FOF_TXT, filename);
2156 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_FOF,
"%s", errtxt);
2160 while (fgets(str,
SC_LENLINE+2, stream) != NULL) {
2161 if (str[0] ==
'#' || str[0] ==
'\n') {
2162 }
else if (isdigit(str[0]) || isspace(str[0]) || str[0] ==
'-') {
2166 sprintf(errtxt,
"%s: %s (unexpected first character at line)",
2167 SC_ERROR_UFS_TXT, filename);
2168 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
"%s",
2177 sprintf(errtxt,
"%s: %s (no data)",
2178 SC_ERROR_UFS_TXT, filename);
2179 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
"%s", errtxt);
2184 if (ncol0 < ncolmin) {
2186 sprintf(errtxt,
"%s: %s (too low number of columns)",
2187 SC_ERROR_UFS_TXT, filename);
2188 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
"%s", errtxt);
2197 tabdat->head[0] = cpl_propertylist_new();
2198 tabdat->head[1] = cpl_propertylist_new();
2202 tabdat->tab[0] = cpl_table_new(0);
2203 tabdat->tab[1] = cpl_table_new(nrec);
2206 for (j = 0; j < ncolmin; j++) {
2207 cpl_table_new_column(tabdat->tab[1], col[j], CPL_TYPE_DOUBLE);
2212 for (i = 0; i < nrec; i++) {
2216 if (ncol != ncol0) {
2219 sprintf(errtxt,
"%s: %s (unexpected number of values at line)",
2220 SC_ERROR_UFS_TXT, filename);
2221 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
"%s",
2225 for (j = 0; j < ncolmin; j++) {
2226 cpl_table_set(tabdat->tab[1], col[j], i, val[j].d);
2234 return CPL_ERROR_NONE;
2259 cpl_array *colnames = NULL;
2264 char* str_val = NULL;
2267 int nrow = 0, ncol = 0, i = 0, j = 0;
2270 if (tabdat->next != 1) {
2271 sprintf(errtxt,
"%s: sctarr *tabdat (number of extensions != 1)",
2273 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
2277 nrow = cpl_table_get_nrow(tabdat->tab[1]);
2279 sprintf(errtxt,
"%s: sctarr *tabdat", SC_ERROR_NDA_TXT);
2280 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
2284 colnames = cpl_table_get_column_names(tabdat->tab[1]);
2285 ncol = cpl_array_get_size(colnames);
2288 col = cpl_array_get_data_string(colnames);
2291 if ((stream = fopen(filename,
"w+")) == NULL) {
2292 cpl_array_delete(colnames);
2293 sprintf(errtxt,
"%s: %s", SC_ERROR_FOF_TXT, filename);
2294 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_FOF,
"%s", errtxt);
2299 for (j = 0; j < ncol; j++) {
2300 sprintf(strcomp,
" %s", col[j]);
2301 str = cpl_sprintf(
"%s%s",str1, strcomp);
2306 fprintf(stream,
"%s\n", str);
2310 for (i = 0; i < nrow; i++) {
2314 for (j = 0; j < ncol; j++) {
2315 coltype = cpl_table_get_column_type(tabdat->tab[1], col[j]);
2316 if (coltype == CPL_TYPE_STRING) {
2317 sprintf(strcomp,
"%s", cpl_table_get_string(tabdat->tab[1],
2319 }
else if (coltype == CPL_TYPE_INT) {
2320 sprintf(strcomp,
"%d", cpl_table_get_int(tabdat->tab[1],
2322 }
else if (coltype == CPL_TYPE_FLOAT) {
2323 sprintf(strcomp,
"%f", cpl_table_get_float(tabdat->tab[1],
2325 }
else if (coltype == CPL_TYPE_DOUBLE) {
2326 sprintf(strcomp,
"%e", cpl_table_get_double(tabdat->tab[1],
2330 cpl_array_delete(colnames);
2331 sprintf(errtxt,
"%s: sctarr tabdat->tab[1] (unsupported "
2332 "column type)", SC_ERROR_IOS_TXT);
2333 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s",
2337 str = cpl_sprintf(
"%s%s",str1, strcomp);
2339 sprintf(strcomp,
" ");
2341 str_val = cpl_sprintf(
"%s%s",str,strcomp);
2346 fprintf(stream,
"%s\n", str);
2355 cpl_array_delete(colnames);
2357 return CPL_ERROR_NONE;
2377 tabdat->next = next;
2378 tabdat->head = cpl_calloc(next+1,
sizeof(cpl_propertylist *));
2379 tabdat->tab = cpl_calloc(next+1,
sizeof(cpl_table *));
2381 return CPL_ERROR_NONE;
2404 int next = 0, fitsformat = 0, i = 0;
2407 next = cpl_fits_count_extensions(filename);
2409 sprintf(errtxt,
"%s: %s", SC_ERROR_FOF_TXT, filename);
2410 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_FOF,
"%s", errtxt);
2411 }
else if (next == 0) {
2412 sprintf(errtxt,
"%s: %s (number of FITS extensions = 0)",
2413 SC_ERROR_UFS_TXT, filename);
2414 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
"%s", errtxt);
2419 if (fitsformat != 1) {
2420 sprintf(errtxt,
"%s: %s (no FITS table)", SC_ERROR_UFS_TXT, filename);
2421 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
"%s", errtxt);
2429 for (i = 0; i <= next; i++) {
2430 tabdat->head[i] = cpl_propertylist_load(filename, i);
2432 tabdat->tab[i] = cpl_table_new(0);
2434 tabdat->tab[i] = cpl_table_load(filename, i, 0);
2438 return CPL_ERROR_NONE;
2456 int next = 0, i = 0;
2459 next = tabdat->next;
2462 for (i = 0; i < next; i++) {
2464 cpl_table_save(tabdat->tab[i+1], tabdat->head[0],
2465 tabdat->head[i+1], filename, CPL_IO_CREATE);
2467 cpl_table_save(tabdat->tab[i+1], NULL,
2468 tabdat->head[i+1], filename, CPL_IO_EXTEND);
2472 return CPL_ERROR_NONE;
2489 int next = 0, i = 0;
2491 if (tabdat == NULL) {
2492 return CPL_ERROR_NONE;
2495 next = tabdat->next;
2497 for (i = 0; i <= next; i++) {
2498 cpl_propertylist_delete(tabdat->head[i]);
2499 cpl_table_delete(tabdat->tab[i]);
2502 cpl_free(tabdat->head);
2503 cpl_free(tabdat->tab);
2505 return CPL_ERROR_NONE;
2525 vecdat->next = next;
2526 vecdat->head = cpl_calloc(next+1,
sizeof(cpl_propertylist *));
2527 vecdat->vec = cpl_calloc(next+1,
sizeof(cpl_vector *));
2529 return CPL_ERROR_NONE;
2552 int next = 0, fitsformat = 0, i = 0;
2555 next = cpl_fits_count_extensions(filename);
2557 sprintf(errtxt,
"%s: %s", SC_ERROR_FOF_TXT, filename);
2558 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_FOF,
"%s", errtxt);
2563 if (fitsformat != 2) {
2564 sprintf(errtxt,
"%s: %s (no 1D FITS image)", SC_ERROR_UFS_TXT,
2566 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
"%s", errtxt);
2574 for (i = 0; i <= next; i++) {
2575 vecdat->head[i] = cpl_propertylist_load(filename, i);
2576 vecdat->vec[i] = cpl_vector_load(filename, i);
2579 return CPL_ERROR_NONE;
2597 int next = 0, i = 0;
2600 next = vecdat->next;
2603 for (i = 0; i <= next; i++) {
2605 cpl_vector_save(vecdat->vec[i], filename, CPL_TYPE_FLOAT,
2606 vecdat->head[i], CPL_IO_CREATE);
2608 cpl_vector_save(vecdat->vec[i], filename, CPL_TYPE_FLOAT,
2609 vecdat->head[i], CPL_IO_EXTEND);
2613 return CPL_ERROR_NONE;
2630 int next = 0, i = 0;
2632 if (vecdat == NULL) {
2633 return CPL_ERROR_NONE;
2636 next = vecdat->next;
2638 for (i = 0; i <= next; i++) {
2639 cpl_propertylist_delete(vecdat->head[i]);
2640 cpl_vector_delete(vecdat->vec[i]);
2643 cpl_free(vecdat->head);
2644 cpl_free(vecdat->vec);
2646 return CPL_ERROR_NONE;
2666 imadat->next = next;
2667 imadat->head = cpl_calloc(next+1,
sizeof(cpl_propertylist *));
2668 imadat->ima = cpl_calloc(next+1,
sizeof(cpl_image *));
2670 return CPL_ERROR_NONE;
2693 int next = 0, fitsformat = 0, i = 0;
2696 next = cpl_fits_count_extensions(filename);
2698 sprintf(errtxt,
"%s: %s", SC_ERROR_FOF_TXT, filename);
2699 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_FOF,
"%s", errtxt);
2704 if (fitsformat != 3) {
2705 sprintf(errtxt,
"%s: %s (no 2D FITS image)",SC_ERROR_UFS_TXT,
2707 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
"%s", errtxt);
2715 for (i = 0; i <= next; i++) {
2716 imadat->head[i] = cpl_propertylist_load(filename, i);
2717 imadat->ima[i] = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED, 0, i);
2720 return CPL_ERROR_NONE;
2738 int next = 0, i = 0;
2741 next = imadat->next;
2744 for (i = 0; i <= next; i++) {
2746 cpl_image_save(imadat->ima[i], filename, CPL_TYPE_UNSPECIFIED,
2747 imadat->head[i], CPL_IO_CREATE);
2749 cpl_image_save(imadat->ima[i], filename, CPL_TYPE_UNSPECIFIED,
2750 imadat->head[i], CPL_IO_EXTEND);
2754 return CPL_ERROR_NONE;
2771 int next = 0, i = 0;
2773 if (imadat == NULL) {
2774 return CPL_ERROR_NONE;
2777 next = imadat->next;
2779 for (i = 0; i <= next; i++) {
2780 cpl_propertylist_delete(imadat->head[i]);
2781 cpl_image_delete(imadat->ima[i]);
2784 cpl_free(imadat->head);
2785 cpl_free(imadat->ima);
2787 return CPL_ERROR_NONE;
cpl_error_code sc_basic_getmaskval_image(double maskval[2], const cpl_image *image)
void sc_basic_abspath(char *out, const char *dir, const char *cwd)
cpl_error_code sc_basic_readline(FILE *stream, scpar par[], int *npar)
cpl_error_code sc_conv_tarr_read(sctarr *tabdat, const char *filename)
cpl_error_code sc_conv_varr2tarr(sctarr *tabdat, const scvarr *vecdat, const cpl_table *extnames)
cpl_error_code sc_conv_ascii_read(sctarr *tabdat, const char *filename, const cpl_array *colnames)
cpl_error_code sc_conv_setextnames_iarr(cpl_table *extnames, const sciarr *imadat, const cpl_parameterlist *parlist)
cpl_error_code sc_conv_varr_delete(scvarr *vecdat)
cpl_error_code sc_conv_getmaskval(double maskval[2], const sctarr *tabdat, const cpl_table *extnames)
cpl_error_code sc_conv_varr_read(scvarr *vecdat, const char *filename)
cpl_error_code sc_conv_results2tarr(sctarr *tabdat, cpl_table *results)
cpl_error_code sc_conv_tarr_write(const char *filename, const sctarr *tabdat)
cpl_error_code sc_conv_tarr_delete(sctarr *tabdat)
cpl_error_code sc_conv_checkfitsformat(int *fitsformat, const char *filename)
cpl_error_code sc_conv_iarr2varr_sci(scvarr *vecdat, cpl_vector *selpix, const sciarr *imadat, const cpl_table *extnames, const double minrelprofflux)
cpl_error_code sc_conv_iarr_write(const char *filename, const sciarr *imadat)
cpl_error_code sc_conv_readprepfits(sctarr *tabdat, const cpl_parameterlist *parlist)
cpl_error_code sc_conv_iarr_init(sciarr *imadat, const int next)
cpl_error_code sc_conv_varr_write(const char *filename, const scvarr *vecdat)
cpl_error_code sc_conv_iarr_delete(sciarr *imadat)
cpl_error_code sc_conv_tarr_init(sctarr *tabdat, const int next)
cpl_error_code sc_conv_setextnames_varr(cpl_table *extnames, const scvarr *vecdat, const cpl_parameterlist *parlist)
cpl_error_code sc_conv_iarr2varr_sky(scvarr *vecdat, const sciarr *imadat, const cpl_table *extnames, const double lowpixfrac, const cpl_vector *selpix)
cpl_error_code sc_conv_modtable(sctarr *tabdat, const cpl_parameterlist *parlist)
cpl_error_code sc_conv_erasemaskcol(sctarr *tabdat, const cpl_parameterlist *parlist)
cpl_error_code sc_conv_resultstarr2varr(scvarr *vecdat, const cpl_table *extnames, const sctarr *tabdat)
cpl_error_code sc_conv_ascii_write(const char *filename, const sctarr *tabdat)
cpl_error_code sc_conv_setcolnames(cpl_array *colnames, const cpl_parameterlist *parlist)
cpl_error_code sc_conv_varr_init(scvarr *vecdat, const int next)
double sc_conv_getwcskey(const cpl_propertylist *plist, const char *key)
cpl_error_code sc_conv_iarr_read(sciarr *imadat, const char *filename)