ERIS Pipeline Reference Manual 1.9.2
sc_conv.c
Go to the documentation of this file.
1/*
2 * This file is part of the SKYCORR software package.
3 * Copyright (C) 2009-2013 European Southern Observatory
4 *
5 * This programme is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This programme is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this programme. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
37/*****************************************************************************
38 * INCLUDES *
39 ****************************************************************************/
40
41#include <sc_conv.h>
42
43
44/*****************************************************************************
45 * CODE *
46 ****************************************************************************/
47cpl_error_code sc_conv_checkfitsformat(int *fitsformat, const char *filename)
48{
69 FILE *stream;
70 cpl_propertylist *header = NULL;
71 cpl_property *prop;
72 int next = 0;
73
74 /* Check file existence */
75 if ((stream = fopen(filename, "r")) == NULL) {
76 *fitsformat = -1;
77 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_FOF, "%s: %s",
78 SC_ERROR_FOF_TXT, filename);
79 }
80 fclose(stream);
81
82 /* Get number of extensions */
83 next = cpl_fits_count_extensions(filename);
84
85 /* FITS table or FITS image? */
86
87 if (next == -1) {
88 /* Not a FITS file */
89 cpl_errorstate_set(CPL_ERROR_NONE);
90 *fitsformat = 0;
91 } else if (next == 0) {
92 /* File header from zeroth extension */
93 header = cpl_propertylist_load(filename, 0);
94 /* FITS tables require at least one extension */
95 *fitsformat = 2;
96 } else {
97 /* File header from first extension */
98 header = cpl_propertylist_load(filename, 1);
99 /* Read FITS keyword XTENSION */
100 prop = cpl_propertylist_get_property(header, "XTENSION");
101 if (prop == NULL) {
102 *fitsformat = -1;
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);
107 } else {
108 if (strcmp(cpl_property_get_string(prop), "IMAGE") == 0) {
109 /* FITS image */
110 *fitsformat = 2;
111 } else if (strcmp(cpl_property_get_string(prop), "BINTABLE")
112 == 0) {
113 /* FITS table */
114 *fitsformat = 1;
115 } else {
116 *fitsformat = -1;
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);
121 }
122 }
123 }
124
125 /* 1D or 2D FITS image? */
126
127 if (*fitsformat == 2) {
128 prop = cpl_propertylist_get_property(header, "NAXIS");
129 if (prop == NULL) {
130 *fitsformat = -1;
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);
135 } else {
136 if (cpl_property_get_int(prop) == 1) {
137 /* 1D FITS image */
138 *fitsformat = 2;
139 } else if (cpl_property_get_int(prop) == 2) {
140 /* 2D FITS image */
141 *fitsformat = 3;
142 } else if (cpl_property_get_int(prop) == 3) {
143 /* 3D FITS image */
144 *fitsformat = 4;
145 } else {
146 *fitsformat = -1;
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);
151 }
152 }
153
154 }
155
156 /* Free allocated memory */
157 cpl_propertylist_delete(header);
158
159 /* Return error code of last error */
160 return cpl_error_get_code();
161}
162
163
164cpl_error_code sc_conv_setcolnames(cpl_array *colnames,
165 const cpl_parameterlist *parlist)
166{
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;
189 int j = 0;
190
191 /* Set default size of array for column names */
192 cpl_array_set_size(colnames, 4);
193
194 /* Get names of wavelength and flux column */
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);
198 j++;
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);
202 j++;
203
204 /* Get information on existence and names of error and mask column */
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);
209 j++;
210 }
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);
215 j++;
216 }
217
218 /* Resize array */
219 cpl_array_set_size(colnames, j);
220 cpl_free(col_lam);
221 cpl_free(col_flux);
222 cpl_free(col_dflux);
223 cpl_free(col_mask);
224 return CPL_ERROR_NONE;
225}
226
227
228cpl_error_code sc_conv_setextnames_varr(cpl_table *extnames,
229 const scvarr *vecdat,
230 const cpl_parameterlist *parlist)
231{
251 const cpl_parameter *p;
252 cpl_property *prop;
253 cpl_boolean none_lam = CPL_FALSE, none_flux = CPL_FALSE;
254 char errtxt[SC_MAXLEN] = "";
255 char colname[SC_LENLINE+1] = "";
256 char* extname = NULL;
257 char **col;
258 int ncol0 = 4, ncol = 0, next = 0, check = 0, j = 0, h = 0;
259 int *extn;
260
261 /* Set default size of output table */
262 cpl_table_set_size(extnames, ncol0);
263
264 /* Create columns for extension numbers and names */
265 if (cpl_table_has_column(extnames, "col") != 1) {
266 cpl_table_new_column(extnames, "col", CPL_TYPE_STRING);
267 }
268 if (cpl_table_has_column(extnames, "extn") != 1) {
269 cpl_table_new_column(extnames, "extn", CPL_TYPE_INT);
270 }
271 cpl_table_fill_column_window_int(extnames, "extn", 0, ncol0, -1);
272
273 /* Get column/extension names from parameter list */
274
275 ncol = ncol0;
276
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) {
281 cpl_table_set_string(extnames, "col", 0, SC_DEFLAMCOL);
282 ncol--;
283 none_lam = CPL_TRUE;
284 }
285
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) {
290 cpl_table_set_string(extnames, "col", 1, SC_DEFFLUXCOL);
291 none_flux = CPL_TRUE;
292 }
293
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) {
298 ncol--;
299 }
300
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) {
305 ncol--;
306 }
307
308 /* Get pointers to table columns */
309 col = cpl_table_get_data_string(extnames, "col");
310 extn = cpl_table_get_data_int(extnames, "extn");
311
312 /* Get number of extensions */
313 next = vecdat->next;
314
315 /* Check existence of extensions with expected names and set extension
316 numbers */
317
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) {
321 continue;
322 } else if (prop == NULL && j == 0) {
323 /* Make sure that flux vector is counted independent of existence
324 of keyword EXTNAME (requires 'NONE' as extension name) */
325 if (none_flux == CPL_TRUE) {
326 extn[1] = 0;
327 check++;
328 }
329 } else {
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) {
333 extn[h] = j;
334 check++;
335 }
336 }
337 cpl_free(extname);
338 }
339 }
340
341 if (check != ncol) {
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",
346 errtxt);
347 }
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",
352 errtxt);
353 }
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",
358 errtxt);
359 }
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",
364 errtxt);
365 }
366 }
367
368 return CPL_ERROR_NONE;
369}
370
371
372cpl_error_code sc_conv_setextnames_iarr(cpl_table *extnames,
373 const sciarr *imadat,
374 const cpl_parameterlist *parlist)
375{
395 const cpl_parameter *p;
396 cpl_property *prop;
397 cpl_boolean none_lam = CPL_FALSE, none_flux = CPL_FALSE;
398 char errtxt[SC_MAXLEN] = "";
399 char colname[SC_LENLINE+1] = "";
400 char* extname = NULL;
401 char **col;
402 int ncol0 = 4, ncol = 0, next = 0, check = 0, j = 0, h = 0;
403 int *extn;
404
405 /* Set default size of output table */
406 cpl_table_set_size(extnames, ncol0);
407
408 /* Create columns for extension numbers and names */
409 if (cpl_table_has_column(extnames, "col") != 1) {
410 cpl_table_new_column(extnames, "col", CPL_TYPE_STRING);
411 }
412 if (cpl_table_has_column(extnames, "extn") != 1) {
413 cpl_table_new_column(extnames, "extn", CPL_TYPE_INT);
414 }
415 cpl_table_fill_column_window_int(extnames, "extn", 0, ncol0, -1);
416
417 /* Get column/extension names from parameter list */
418
419 ncol = ncol0;
420
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) {
425 cpl_table_set_string(extnames, "col", 0, SC_DEFLAMCOL);
426 ncol--;
427 none_lam = CPL_TRUE;
428 }
429
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) {
434 cpl_table_set_string(extnames, "col", 1, SC_DEFFLUXCOL);
435 none_flux = CPL_TRUE;
436 }
437
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) {
442 ncol--;
443 }
444
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) {
449 ncol--;
450 }
451
452 /* Get pointers to table columns */
453 col = cpl_table_get_data_string(extnames, "col");
454 extn = cpl_table_get_data_int(extnames, "extn");
455
456 /* Get number of extensions */
457 next = imadat->next;
458
459 /* Check existence of extensions with expected names and set extension
460 numbers */
461
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) {
465 continue;
466 } else if (prop == NULL && j == 0) {
467 /* Make sure that flux vector is counted independent of existence
468 of keyword EXTNAME (requires 'NONE' as extension name) */
469 if (none_flux == CPL_TRUE) {
470 extn[1] = 0;
471 check++;
472 }
473 } else {
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) {
477 extn[h] = j;
478 check++;
479 }
480 }
481 cpl_free(extname);
482 }
483 }
484
485 if (check != ncol) {
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",
490 errtxt);
491 }
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",
496 errtxt);
497 }
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",
502 errtxt);
503 }
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",
508 errtxt);
509 }
510 }
511
512 return CPL_ERROR_NONE;
513}
514
515
516cpl_error_code sc_conv_varr2tarr(sctarr *tabdat, const scvarr *vecdat,
517 const cpl_table *extnames)
518{
540 char errtxt[SC_MAXLEN] = "";
541 const char **col;
542 int ncol = 0, next = 0, h = 0, nrow = 0, j = 0, i = 0;
543 const int *extn;
544 double crpix = 0., crval = 0., cdelt = 0.;
545
546 /* Initialise CPL tables and CPL property lists for content of scvarr
547 structure (memory allocation) */
548 sc_conv_tarr_init(tabdat, 1);
549 tabdat->tab[0] = cpl_table_new(0);
550 tabdat->tab[1] = cpl_table_new(0);
551
552 /* Transfer FITS header data */
553 tabdat->head[0] = cpl_propertylist_duplicate(vecdat->head[0]);
554 tabdat->head[1] = cpl_propertylist_new();
555
556 /* Check table for extension numbers and names */
557 ncol = cpl_table_get_nrow(extnames);
558 if (ncol <= 0) {
559 sc_conv_tarr_delete(tabdat);
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);
562 }
563
564 /* Check column names */
565 if (cpl_table_has_column(extnames, "col") != 1 ||
566 cpl_table_has_column(extnames, "extn") != 1) {
567 sc_conv_tarr_delete(tabdat);
568 sprintf(errtxt, "%s: cpl_table *extnames (column names)",
569 SC_ERROR_IOS_TXT);
570 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
571 }
572
573 /* Check existence of data */
574 if (cpl_table_get_column_max(extnames, "extn") == -1) {
575 sc_conv_tarr_delete(tabdat);
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);
579 }
580
581 /* Get number of extensions */
582 next = vecdat->next;
583
584 /* Check extension numbers */
585 if (cpl_table_get_column_max(extnames, "extn") > next) {
586 sc_conv_tarr_delete(tabdat);
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);
590 }
591
592 /* Get pointers to table columns */
593 col = cpl_table_get_data_string_const(extnames, "col");
594 extn = cpl_table_get_data_int_const(extnames, "extn");
595
596 /* Create table columns */
597 cpl_table_new_column(tabdat->tab[1], col[0], CPL_TYPE_DOUBLE);
598 for (h = 1; h < ncol; h++) {
599 if (extn[h] >= 0) {
600 cpl_table_new_column(tabdat->tab[1], col[h], CPL_TYPE_DOUBLE);
601 }
602 }
603
604 /* Set size of data table */
605 nrow = cpl_vector_get_size(vecdat->vec[0]);
606 if (nrow <= 0) {
607 sc_conv_tarr_delete(tabdat);
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) {
613 sc_conv_tarr_delete(tabdat);
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",
617 errtxt);
618 }
619 }
620 }
621 cpl_table_set_size(tabdat->tab[1], nrow);
622
623 /* Get wavelength grid from FITS header if a wavelength vector is not
624 provided */
625 if (extn[0] < 0) {
626 crpix = sc_conv_getwcskey(vecdat->head[0], "CRPIX1");
627 if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
628 sc_conv_tarr_delete(tabdat);
629 sprintf(errtxt, "%s: scvarr *vecdat (keyword CRPIX1 not found)",
630 SC_ERROR_IOS_TXT);
631 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s",
632 errtxt);
633 }
634 crval = sc_conv_getwcskey(vecdat->head[0], "CRVAL1");
635 if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
636 sc_conv_tarr_delete(tabdat);
637 sprintf(errtxt, "%s: scvarr *vecdat (keyword CRVAL1 not found)",
638 SC_ERROR_IOS_TXT);
639 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s",
640 errtxt);
641 }
642 cdelt = sc_conv_getwcskey(vecdat->head[0], "CDELT1");
643 if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
644 sc_conv_tarr_delete(tabdat);
645 sprintf(errtxt, "%s: scvarr *vecdat (keyword CDELT1 not found)",
646 SC_ERROR_IOS_TXT);
647 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s",
648 errtxt);
649 }
650 }
651
652 /* Convert vectors to table columns */
653 for (h = 0; h < ncol; h++) {
654 for (i = 0; i < nrow; i++) {
655 if (extn[h] >= 0) {
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);
661 }
662 }
663 }
664
665 return CPL_ERROR_NONE;
666}
667
668
669double sc_conv_getwcskey(const cpl_propertylist *plist, const char *key)
670{
685 double d;
686 const cpl_errorstate cleanstate = cpl_errorstate_get();
687
688 d = cpl_propertylist_get_double(plist, key);
689
690 /* WCS keys can be written as ints but should be interpreted as doubles */
691 if (cpl_error_get_code() == CPL_ERROR_TYPE_MISMATCH) {
692 cpl_errorstate_set(cleanstate);
693 d = (double) cpl_propertylist_get_int(plist, key);
694 }
695
696 return d;
697}
698
699
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)
703{
722 const cpl_array * alam = cpl_table_get_array(inptable, col_lam, 0);
723 if (alam == NULL) {
724 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT, "Expected %s "
725 "array column in input", col_lam);;
726 return NULL;
727 }
728 const cpl_array * aflux = cpl_table_get_array(inptable, col_flux, 0);
729 if (aflux == NULL) {
730 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT, "Expected %s "
731 "array column in input", col_flux);
732 return NULL;
733 }
734 const cpl_array * adflux = NULL, * amask = NULL;
735 if (col_dflux) {
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);
740 return NULL;
741 }
742 }
743 if (col_mask) {
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);
748 return NULL;
749 }
750 }
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");
756 return NULL;
757 }
758
759 cpl_table * ntab = cpl_table_new(cpl_array_get_size(alam));
760
761 /* const casts due to PIPE-6075 */
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);
766
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);
771
772 if(adflux) {
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);
777 }
778 if (amask) {
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));
781 }
782
783 return ntab;
784}
785
786
787cpl_error_code sc_conv_modtable(sctarr *tabdat,
788 const cpl_parameterlist *parlist)
789{
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;
818 char errtxt[SC_MAXLEN] = "";
819 char col_imask[SC_LENLINE+5] = "";
820 char col_renamed[SC_LENLINE+10] = "";
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.;
828
829 /* Get names of wavelength and flux column */
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) {
833 sprintf(col_lam, "%s", SC_DEFLAMCOL);
834 }
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) {
838 sprintf(col_flux, "%s", SC_DEFFLUXCOL);
839 }
840
841 /* Get information on existence and names of error and mask column */
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) {
845 exerr = CPL_FALSE;
846 }
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) {
850 exmask = CPL_FALSE;
851 }
852
853 /* Check number of extensions */
854 next = tabdat->next;
855 if (next <= 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)",
860 SC_ERROR_IOS_TXT);
861 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
862 }
863
864 /* Check existence of expected columns */
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);
869 }
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);
874 }
875 if (exerr == TRUE) {
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",
880 errtxt);
881 }
882 }
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",
888 errtxt);
889 }
890 }
891
892 /* check for and convert sdp data format */
893 if (cpl_propertylist_has(tabdat->head[1], "VOCLASS") &&
894 strncmp(cpl_propertylist_get_string(tabdat->head[1],
895 "VOCLASS"),
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();
902 }
903 cpl_table_delete(tabdat->tab[1]);
904 tabdat->tab[1] = ntab;
905 }
906
907 /* Create integer mask column (and rename already existing column of the
908 same name) */
909 if (exmask == CPL_TRUE) {
910 sprintf(col_imask, "%s_I", col_mask);
911 } else {
912 sprintf(col_imask, "%s_I", SC_DEFMASKCOL);
913 }
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);
920 } else {
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);
924 }
925 }
926 cpl_table_new_column(tabdat->tab[1], col_imask, CPL_TYPE_INT);
927
928 /* Get number of rows */
929 nrow = cpl_table_get_nrow(tabdat->tab[1]);
930
931 /* Get number of edge pixels */
932 edgepix = (int) ceil(nrow * SC_EDGEFRAC);
933
934 /* Get minimum and maximum flux excluding the edge pixels */
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) {
938 minflux = flux;
939 } else if (flux > maxflux) {
940 maxflux = flux;
941 }
942 }
943
944 /* Check for nan values and negative errors and correct them */
945
946 for (nmask0 = 0, i = 0; i < nrow; i++) {
947
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;
952 isnanum = CPL_TRUE;
953 isedgeflux = CPL_FALSE;
954 } else if (flux < SC_MAXFLUXFAC * minflux ||
955 flux > SC_MAXFLUXFAC * maxflux) {
956 isnanflux = CPL_FALSE;
957 isedgeflux = CPL_TRUE;
958 isedge = CPL_TRUE;
959 } else {
960 isnanflux = CPL_FALSE;
961 isedgeflux = CPL_FALSE;
962 }
963
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;
970 } else {
971 isnandflux = CPL_FALSE;
972 }
973 }
974
975 if (exmask == CPL_TRUE) {
976 mask = cpl_table_get(tabdat->tab[1], col_mask, i, NULL);
977 } else {
978 mask = 1;
979 }
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) {
984 isnomask = CPL_TRUE;
985 } else if (mask == 0) {
986 is0 = CPL_TRUE;
987 nmask0++;
988 }
989 } else {
990 if (mask == 0) {
991 cpl_table_set(tabdat->tab[1], col_imask, i, 0);
992 is0 = CPL_TRUE;
993 nmask0++;
994 } else {
995 cpl_table_set(tabdat->tab[1], col_imask, i, 1);
996 if (mask != 1) {
997 isnomask = CPL_TRUE;
998 }
999 }
1000 }
1001
1002 }
1003
1004 /* Return if mask cannot be interpreted */
1005 if (isnomask == CPL_TRUE && is0 == CPL_FALSE) {
1006 sprintf(errtxt, "%s: sctarr *tabdat (all mask value(s) != 0 or 1)",
1007 SC_ERROR_IOS_TXT);
1008 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1009 }
1010
1011 /* Correct integer mask values if required (e.g. reverse definition) */
1012
1013 for (i = 0; i < nrow; i++) {
1014 imask = cpl_table_get(tabdat->tab[1], col_imask, i, NULL);
1015 if (isnomask == CPL_TRUE) {
1016 if (imask == 0) {
1017 cpl_table_set(tabdat->tab[1], col_imask, i, 1);
1018 } else {
1019 cpl_table_set(tabdat->tab[1], col_imask, i, 0);
1020 }
1021 } else {
1022 if (imask == -1) {
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);
1026 ismask0 = CPL_TRUE;
1027 }
1028 }
1029 }
1030
1031 /* Print info message in the case of bad fluxes, errors, or mask
1032 values */
1033
1034 if (isnanum == CPL_TRUE) {
1035 cpl_msg_debug(cpl_func, "Input data: flux(es) = 'nan' "
1036 "-> set mask = 0");
1037 }
1038
1039 if (isedge == CPL_TRUE) {
1040 cpl_msg_debug(cpl_func, "Input data: unreliable edge flux(es) "
1041 "-> set mask = 0");
1042 }
1043
1044 if (isoutrange == CPL_TRUE) {
1045 cpl_msg_debug(cpl_func, "Input data: error(s) <= 0 or 'nan' "
1046 "-> set mask = 0");
1047 }
1048
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)");
1052 }
1053
1054 if (ismask0 == CPL_TRUE) {
1055 cpl_msg_debug(cpl_func, "Input data: all mask values = 0 "
1056 "-> reverse definition (0 -> 1)");
1057 }
1058
1059 cpl_free(col_lam);
1060 cpl_free(col_flux);
1061 cpl_free(col_dflux);
1062 cpl_free(col_mask);
1063
1064 return CPL_ERROR_NONE;
1065}
1066
1067cpl_error_code sc_conv_readprepfits(sctarr *tabdat,
1068 const cpl_parameterlist *parlist)
1069{
1085 const cpl_parameter *p;
1086 char* basedir = NULL;
1087 char outdir[SC_MAXLEN] = "";
1088 char* outname = NULL;
1089 char* spectype = NULL;
1090 char filename[2*SC_MAXLEN+10] = "";
1091 char errtxt[SC_MAXLEN] = "";
1092
1093 /* Get path and name of results file */
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");
1097 sc_basic_abspath(outdir, cpl_parameter_get_string(p), basedir);
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);
1106 } else {
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);
1110 }
1111
1112 /* Read FITS table */
1113 sc_conv_tarr_read(tabdat, filename);
1114 cpl_free(basedir);
1115 cpl_free(outname);
1116 cpl_free(spectype);
1117 /* Return error code of last error */
1118 return cpl_error_get_code();
1119}
1120
1121
1122cpl_error_code sc_conv_results2tarr(sctarr *tabdat, cpl_table *results)
1123{
1146 char errtxt[SC_MAXLEN] = "";
1147 int next = 0, nrow = 0;
1148
1149 /* Check existence of required column in results data table */
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);
1154 }
1155
1156 /* Check number of extensions */
1157 next = tabdat->next;
1158 if (next <= 0) {
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)",
1163 SC_ERROR_IOS_TXT);
1164 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1165 }
1166
1167 /* Check non-existence of 'sc' results columns in input data table */
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);
1174 }
1175
1176 /* Get and check number of data points */
1177 nrow = cpl_table_get_nrow(results);
1178 if (nrow == 0) {
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]",
1183 SC_ERROR_IDG_TXT);
1184 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IDG, "%s", errtxt);
1185 }
1186
1187 /* Copy scflux column with SKYCORR results into table with input data */
1188 cpl_table_duplicate_column(tabdat->tab[1], "scflux", results, "scflux");
1189
1190 /* Copy scdflux column if present */
1191 if (cpl_table_has_column(results, "scdflux") == 1) {
1192 cpl_table_duplicate_column(tabdat->tab[1], "scdflux",
1193 results, "scdflux");
1194 }
1195
1196 /* Copy scmask column if present */
1197 if (cpl_table_has_column(results, "scmask") == 1) {
1198 cpl_table_duplicate_column(tabdat->tab[1], "scmask",
1199 results, "scmask");
1200 }
1201
1202 return CPL_ERROR_NONE;
1203}
1204
1205
1206cpl_error_code sc_conv_erasemaskcol(sctarr *tabdat,
1207 const cpl_parameterlist *parlist)
1208{
1225 const cpl_parameter *p;
1226 char* col_mask = NULL;
1227 char col_imask[SC_LENLINE+3] = "";
1228 char errtxt[SC_MAXLEN] = "";
1229 char col_renamed[SC_LENLINE+10] = "";
1230 int next = 0;
1231
1232 /* Get name of integer mask column */
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) {
1236 sprintf(col_imask, "%s_I", SC_DEFMASKCOL);
1237 } else {
1238 sprintf(col_imask, "%s_I", col_mask);
1239 }
1240
1241 /* Check number of extensions */
1242 next = tabdat->next;
1243 if (next <= 0) {
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)",
1248 SC_ERROR_IOS_TXT);
1249 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1250 }
1251
1252 /* Erase integer mask column if it exists */
1253 if (cpl_table_has_column(tabdat->tab[1], col_imask) == 1) {
1254 cpl_table_erase_column(tabdat->tab[1], col_imask);
1255 }
1256
1257 /* Rename a column with the same original name as the internal integer
1258 mask column (temporary suffix: "_orig") */
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);
1262 }
1263 cpl_free(col_mask);
1264 return CPL_ERROR_NONE;
1265}
1266
1267
1268cpl_error_code sc_conv_resultstarr2varr(scvarr *vecdat,
1269 const cpl_table *extnames,
1270 const sctarr *tabdat)
1271{
1292 char errtxt[SC_MAXLEN] = "";
1293 cpl_boolean hascol = CPL_TRUE;
1294 int next = 0, extn_flux = 0, extn_dflux = 0, extn_mask = 0, nrow = 0;
1295 int j = 0, i = 0;
1296 int *scmask = NULL;
1297 double maskval[2] = {0., 1.};
1298 double *flux = NULL, *dflux = NULL, *mask = NULL, *scflux = NULL;
1299 double *scdflux = NULL;
1300
1301 /* Check table for extension numbers and names */
1302 if (cpl_table_get_nrow(extnames) != 4) {
1303 sprintf(errtxt, "%s: cpl_table *extnames (number of rows != 4)",
1304 SC_ERROR_IOS_TXT);
1305 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1306 }
1307
1308 /* Check column names */
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)",
1312 SC_ERROR_IOS_TXT);
1313 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1314 }
1315
1316 /* Check existence of data */
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);
1321 }
1322
1323 /* Get number of extensions */
1324 next = vecdat->next;
1325
1326 /* Check extension numbers */
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);
1331 }
1332
1333 /* Get extension numbers */
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);
1337
1338 /* Check number of extensions in results table */
1339 if (tabdat->next != 1) {
1340 sprintf(errtxt, "%s: sctarr *tabdat (number of extensions != 1)",
1341 SC_ERROR_IOS_TXT);
1342 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1343 }
1344
1345 /* Get number of data points */
1346 nrow = cpl_table_get_nrow(tabdat->tab[1]);
1347 if (nrow == 0) {
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);
1350 }
1351
1352 /* Check number of data points */
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",
1358 errtxt);
1359 }
1360 }
1361
1362 /* Get the two (extreme) original mask values */
1363 if (sc_conv_getmaskval(maskval, tabdat, extnames) != CPL_ERROR_NONE) {
1364 return cpl_error_get_code();
1365 }
1366
1367 /* Get pointers to image data */
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]);
1371 }
1372 if (extn_mask >= 0) {
1373 mask = cpl_vector_get_data(vecdat->vec[extn_mask]);
1374 }
1375
1376 /* Get pointers to columns of results data table */
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;
1382 }
1383 if (extn_mask >= 0) {
1384 scmask = cpl_table_get_data_int(tabdat->tab[1], "scmask");
1385 if (scmask == NULL) hascol = CPL_FALSE;
1386 }
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);
1392 }
1393
1394 /* Write sky-corrected flux, errors, and mask into flux image vectors */
1395 for (i = 0; i < nrow; i++) {
1396 flux[i] = scflux[i];
1397 if (extn_dflux >= 0) {
1398 dflux[i] = scdflux[i];
1399 }
1400 if (extn_mask >= 0) {
1401 /* Correct mask value if necessary (good -> bad) */
1402 if (mask[i] == maskval[1] && scmask[i] == 0) {
1403 mask[i] = maskval[0];
1404 }
1405 }
1406 }
1407
1408 return CPL_ERROR_NONE;
1409}
1410
1411
1412cpl_error_code sc_conv_getmaskval(double maskval[2], const sctarr *tabdat,
1413 const cpl_table *extnames)
1414{
1432 char errtxt[SC_MAXLEN] = "", col_mask[SC_LENLINE+1] = "";
1433 char col_imask[SC_LENLINE+5] = "";
1434 cpl_boolean hascol = CPL_TRUE;
1435 cpl_size imaskmaxpos = 0;
1436 int extn_mask = 0;
1437 double maskmin = 0., maskmax = 0.;
1438
1439 /* Default mask values */
1440 maskval[0] = 0;
1441 maskval[1] = 1;
1442
1443 /* Check table for extension numbers and names */
1444 if (cpl_table_get_nrow(extnames) != 4) {
1445 sprintf(errtxt, "%s: cpl_table *extnames (number of rows != 4)",
1446 SC_ERROR_IOS_TXT);
1447 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1448 }
1449
1450 /* Check column names */
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)",
1454 SC_ERROR_IOS_TXT);
1455 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1456 }
1457
1458 /* Get name and extension of input mask column */
1459 sprintf(col_mask, "%s", cpl_table_get_string(extnames, "col", 3));
1460 extn_mask = cpl_table_get_int(extnames, "extn", 3, NULL);
1461
1462 /* Get name of integer mask column */
1463 if (extn_mask >= 0) {
1464 sprintf(col_imask, "%s_I", col_mask);
1465 } else {
1466 sprintf(col_imask, "%s_I", SC_DEFMASKCOL);
1467 }
1468
1469 /* Check number of extensions in results table */
1470 if (tabdat->next != 1) {
1471 sprintf(errtxt, "%s: sctarr *tabdat (number of extensions != 1)",
1472 SC_ERROR_IOS_TXT);
1473 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1474 }
1475
1476 /* Check number of data points */
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);
1480 }
1481
1482 /* Check existence of mask columns in input data table */
1483 if (extn_mask >= 0) {
1484 if (cpl_table_has_column(tabdat->tab[1], col_mask) != 1) {
1485 hascol = CPL_FALSE;
1486 }
1487 }
1488 if (cpl_table_has_column(tabdat->tab[1], col_imask) != 1) {
1489 hascol = CPL_FALSE;
1490 }
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);
1495 }
1496
1497 /* Get the two (extreme) original mask values */
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)
1503 == 1) {
1504 maskval[0] = maskmax;
1505 maskval[1] = maskmin;
1506 } else {
1507 maskval[0] = maskmin;
1508 maskval[1] = maskmax;
1509 }
1510 }
1511
1512 return CPL_ERROR_NONE;
1513}
1514
1515cpl_error_code sc_conv_iarr2varr_sci(scvarr *vecdat, cpl_vector *selpix,
1516 const sciarr *imadat,
1517 const cpl_table *extnames,
1518 const double minrelprofflux)
1519{
1560 cpl_vector *row, *prof, *relsum;
1561 char errtxt[SC_MAXLEN] = "";
1562 int next = 0, h = 0, nx = 0, ny = 0, j = 0, i = 0, qual = 0, spix = 0;
1563 const int *extn;
1564 double maxerr = 0., maskval[2] = {0., 0.}, profmin = 0., profmax = 0.;
1565 double proflim = 0., profval = 0., sum = 0., scale = 0., mask = 0.;
1566 double val = 0.;
1567
1568 /* Check input parameter */
1569 if (minrelprofflux < 0. || minrelprofflux > 1.) {
1570 sprintf(errtxt, "%s: double minrelprofflux (value outside [0,1])",
1571 SC_ERROR_IIP_TXT);
1572 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP, "%s", errtxt);
1573 }
1574
1575 /* Get number of extensions */
1576 next = imadat->next;
1577
1578 /* Initialise CPL vectors and CPL property lists for content of sciarr
1579 structure (memory allocation) */
1580 sc_conv_varr_init(vecdat, next);
1581 for (h = 0; h <= next; h++) {
1582 vecdat->vec[h] = cpl_vector_new(1);
1583 }
1584
1585 /* Transfer FITS header data and delete keywords specific for 2D images */
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");
1592 }
1593
1594 /* Check table for extension numbers and names */
1595 if (cpl_table_get_nrow(extnames) != 4) {
1596 sc_conv_varr_delete(vecdat);
1597 sprintf(errtxt, "%s: cpl_table *extnames (number of rows != 4)",
1598 SC_ERROR_IOS_TXT);
1599 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1600 }
1601
1602 /* Check column names */
1603 if (cpl_table_has_column(extnames, "col") != 1 ||
1604 cpl_table_has_column(extnames, "extn") != 1) {
1605 sc_conv_varr_delete(vecdat);
1606 sprintf(errtxt, "%s: cpl_table *extnames (column names)",
1607 SC_ERROR_IOS_TXT);
1608 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1609 }
1610
1611 /* Check existence of data */
1612 if (cpl_table_get_column_max(extnames, "extn") == -1) {
1613 sc_conv_varr_delete(vecdat);
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);
1617 }
1618
1619 /* Check extension numbers */
1620 if (cpl_table_get_column_max(extnames, "extn") > next) {
1621 sc_conv_varr_delete(vecdat);
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);
1625 }
1626
1627 /* Get pointer to extension numbers */
1628 extn = cpl_table_get_data_int_const(extnames, "extn");
1629
1630 /* Check image size */
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) {
1634 sc_conv_varr_delete(vecdat);
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) {
1641 sc_conv_varr_delete(vecdat);
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",
1645 errtxt);
1646 }
1647 }
1648 }
1649
1650 /* Get maximum error if available */
1651 if (extn[2] >= 0) {
1652 maxerr = cpl_image_get_max(imadat->ima[extn[2]]);
1653 }
1654
1655 /* Get meaning of mask values (identification of good and bad pixels) */
1656 if (extn[3] >= 0) {
1657 if (sc_basic_getmaskval_image(maskval, imadat->ima[extn[3]]) !=
1658 CPL_ERROR_NONE) {
1659 return (cpl_error_code)SC_ERROR_IOV;
1660 }
1661 }
1662
1663 /* Create row (x) and profile (y) vector */
1664 row = cpl_vector_new(nx);
1665 prof = cpl_vector_new(ny);
1666
1667 /* Get profile along slit */
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,
1672 &qual));
1673 }
1674 cpl_vector_set(prof, j, cpl_vector_get_median(row));
1675 }
1676
1677 /* Delete temporary row vector */
1678 cpl_vector_delete(row);
1679
1680 /* Subtract minimum flux from profile function */
1681 profmin = cpl_vector_get_min(prof);
1682 cpl_vector_subtract_scalar(prof, profmin);
1683
1684 /* Avoid pixels with negligible flux contribution */
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.);
1691 }
1692 }
1693
1694 /* Normalise profile function to get weights */
1695 for (sum = 0., j = 0; j < ny; j++) {
1696 sum += cpl_vector_get(prof, j);
1697 }
1698 if (sum == 0.) {
1699 sc_conv_varr_delete(vecdat);
1700 cpl_vector_delete(prof);
1701 sprintf(errtxt, "%s: sciarr *imadat (flux sum = 0)",
1702 SC_ERROR_IOV_TXT);
1703 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV, "%s", errtxt);
1704 }
1705 cpl_vector_divide_scalar(prof, sum);
1706
1707 /* Calculate correction function for columns with bad pixels */
1708 relsum = cpl_vector_new(nx);
1709 if (extn[3] >= 0) {
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) <
1713 maskval[1] + SC_TOL) {
1714 sum += cpl_vector_get(prof, j);
1715 }
1716 }
1717 cpl_vector_set(relsum, i, sum);
1718 }
1719 } else {
1720 cpl_vector_fill(relsum, 1.);
1721 }
1722
1723 /* Set size of vecdat vectors */
1724 for (h = 0; h <= next; h++) {
1725 cpl_vector_set_size(vecdat->vec[h], nx);
1726 }
1727
1728 /* Set size of selpix vector and initialise all elements */
1729 cpl_vector_set_size(selpix, nx);
1730 cpl_vector_fill(selpix, 0.);
1731
1732 /* Convert sciarr to scvarr structure (unidentified extensions are
1733 handled like flux) */
1734
1735 for (h = 0; h <= next; h++) {
1736 for (i = 0; i < nx; i++) {
1737
1738 /* Get scaling factor for pixel flux */
1739 scale = cpl_vector_get(relsum, i);
1740
1741 if (h == extn[3]) {
1742
1743 /* Set mask values (bad value only if no good pixel in image
1744 column) */
1745 if (scale < 2 * SC_TOL) {
1746 cpl_vector_set(vecdat->vec[extn[3]], i, maskval[0]);
1747 } else {
1748 cpl_vector_set(vecdat->vec[extn[3]], i, maskval[1]);
1749 }
1750
1751 } else {
1752
1753 /* Add values of good pixels */
1754
1755 for (sum = 0., spix = 0, j = 0; j < ny; j++) {
1756
1757 /* Skip pixels with negligible flux contribution */
1758 profval = cpl_vector_get(prof, j);
1759 if (profval == 0.) {
1760 continue;
1761 }
1762
1763 /* Get mask value for pixel */
1764 if (extn[3] >= 0) {
1765 mask = cpl_image_get(imadat->ima[extn[3]], i+1, j+1,
1766 &qual);
1767 } else {
1768 mask = maskval[1];
1769 }
1770
1771 /* Get pixel value */
1772 val = cpl_image_get(imadat->ima[h], i+1, j+1, &qual);
1773 if (val <= 0. && h == extn[0]) {
1774 sc_conv_varr_delete(vecdat);
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,
1780 "%s", errtxt);
1781 }
1782
1783 /* Consider good pixels only if not wavelength */
1784 if (mask == maskval[1] || h == extn[0]) {
1785 /* Add pixel values */
1786 if (h == extn[2]) {
1787 /* Squared summation of error pixels */
1788 sum += val * val;
1789 } else {
1790 sum += val;
1791 }
1792 /* Number of good pixels */
1793 if (h == extn[1]) {
1794 spix++;
1795 }
1796 }
1797
1798 }
1799
1800 /* Save effective pixel number */
1801 if (h == extn[1]) {
1802 if (scale > 0.) {
1803 cpl_vector_set(selpix, i, (double) spix / scale);
1804 } else {
1805 cpl_vector_set(selpix, i, (double) spix);
1806 }
1807 }
1808
1809 /* Get resulting value depending on kind of data */
1810 if (h == extn[0]) {
1811 /* Wavelength */
1812 sum /= (double) ny;
1813 } else if (h == extn[2]) {
1814 /* Error */
1815 if (sum > 0. && scale > 0.) {
1816 sum = sqrt(sum) / scale;
1817 } else {
1818 sum = maxerr;
1819 }
1820 } else {
1821 /* Flux */
1822 if (scale > 0.) {
1823 sum /= scale;
1824 }
1825 }
1826
1827 /* Write resulting value into output vector */
1828 cpl_vector_set(vecdat->vec[h], i, sum);
1829
1830 }
1831
1832 }
1833 }
1834
1835 /* Free allocated memory */
1836 cpl_vector_delete(prof);
1837 cpl_vector_delete(relsum);
1838
1839 return CPL_ERROR_NONE;
1840}
1841
1842
1843cpl_error_code sc_conv_iarr2varr_sky(scvarr *vecdat, const sciarr *imadat,
1844 const cpl_table *extnames,
1845 const double lowpixfrac,
1846 const cpl_vector *selpix)
1847{
1884 cpl_vector *yvec, *ysel;
1885 char errtxt[SC_MAXLEN] = "";
1886 int next = 0, h = 0, nx = 0, ny = 0, j = 0, i = 0, ngood = 0, qual = 0;
1887 int jmed = 0;
1888 const int *extn;
1889 double maskval[2] = {0., 0.};
1890 double mask = 0., val = 0., med = 0., nmed = 0.;
1891
1892 /* Check input parameter */
1893 if (lowpixfrac < 0. || lowpixfrac > 1.) {
1894 sprintf(errtxt, "%s: double lowfracpix (value outside [0,1])",
1895 SC_ERROR_IIP_TXT);
1896 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP, "%s", errtxt);
1897 }
1898
1899 /* Check validity of CPL vector of effective pixel numbers */
1900 if (cpl_vector_get_max(selpix) > 0.) {
1901 } else {
1902 sprintf(errtxt, "%s: cpl_vector *selpix (invalid elements or all 0)",
1903 SC_ERROR_IOV_TXT);
1904 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV, "%s", errtxt);
1905 }
1906
1907 /* Get number of extensions */
1908 next = imadat->next;
1909
1910 /* Initialise CPL vectors and CPL property lists for content of sciarr
1911 structure (memory allocation) */
1912 sc_conv_varr_init(vecdat, next);
1913 for (h = 0; h <= next; h++) {
1914 vecdat->vec[h] = cpl_vector_new(1);
1915 }
1916
1917 /* Transfer FITS header data and delete keywords specific for 2D images */
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");
1924 }
1925
1926 /* Check table for extension numbers and names */
1927 if (cpl_table_get_nrow(extnames) != 4) {
1928 sc_conv_varr_delete(vecdat);
1929 sprintf(errtxt, "%s: cpl_table *extnames (number of rows != 4)",
1930 SC_ERROR_IOS_TXT);
1931 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1932 }
1933
1934 /* Check column names */
1935 if (cpl_table_has_column(extnames, "col") != 1 ||
1936 cpl_table_has_column(extnames, "extn") != 1) {
1937 sc_conv_varr_delete(vecdat);
1938 sprintf(errtxt, "%s: cpl_table *extnames (column names)",
1939 SC_ERROR_IOS_TXT);
1940 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
1941 }
1942
1943 /* Check existence of data */
1944 if (cpl_table_get_column_max(extnames, "extn") == -1) {
1945 sc_conv_varr_delete(vecdat);
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);
1949 }
1950
1951 /* Check extension numbers */
1952 if (cpl_table_get_column_max(extnames, "extn") > next) {
1953 sc_conv_varr_delete(vecdat);
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);
1957 }
1958
1959 /* Get pointer to extension numbers */
1960 extn = cpl_table_get_data_int_const(extnames, "extn");
1961
1962 /* Check image size */
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) {
1966 sc_conv_varr_delete(vecdat);
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) {
1973 sc_conv_varr_delete(vecdat);
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",
1977 errtxt);
1978 }
1979 }
1980 }
1981
1982 /* Compare size of image in x direction and size of selpix vector */
1983 if (cpl_vector_get_size(selpix) != nx) {
1984 sc_conv_varr_delete(vecdat);
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);
1988 }
1989
1990 /* Get meaning of mask values (identification of good and bad pixels) */
1991 if (extn[3] >= 0) {
1992 if (sc_basic_getmaskval_image(maskval, imadat->ima[extn[3]]) !=
1993 CPL_ERROR_NONE) {
1994 sc_conv_varr_delete(vecdat);
1995 return (cpl_error_code)SC_ERROR_IOV;
1996 }
1997 }
1998
1999 /* Create temporary y column vectors */
2000 yvec = cpl_vector_new(ny);
2001 ysel = cpl_vector_new(ny);
2002
2003 /* Set size of output vectors */
2004 for (h = 0; h <= next; h++) {
2005 cpl_vector_set_size(vecdat->vec[h], nx);
2006 }
2007
2008 /* Convert sciarr to scvarr structure (unidentified extensions are
2009 handled like flux) */
2010
2011 for (h = 0; h <= next; h++) {
2012 for (i = 0; i < nx; i++) {
2013
2014 for (ngood = 0, j = 0; j < ny; j++) {
2015
2016 /* Get mask value for pixel */
2017 if (extn[3] >= 0) {
2018 mask = cpl_image_get(imadat->ima[extn[3]], i+1, j+1,
2019 &qual);
2020 } else {
2021 mask = maskval[1];
2022 }
2023
2024 /* Count good pixels */
2025 if (mask == maskval[1]) {
2026 ngood++;
2027 }
2028
2029 /* Skip rest of loop in the case of mask extension */
2030 if (h == extn[3]) {
2031 continue;
2032 }
2033
2034 /* Get pixel value */
2035 val = cpl_image_get(imadat->ima[h], i+1, j+1, &qual);
2036 if (val <= 0. && h == extn[0]) {
2037 sc_conv_varr_delete(vecdat);
2038 cpl_vector_delete(yvec);
2039 cpl_vector_delete(ysel);
2040 sprintf(errtxt, "%s: sciarr *imadat (wavelength <= 0)",
2041 SC_ERROR_IOV_TXT);
2042 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV, "%s",
2043 errtxt);
2044 }
2045
2046 /* Set value for good and bad pixels */
2047 cpl_vector_set(yvec, j, val);
2048 if (mask == maskval[1]) {
2049 /* good pixel */
2050 cpl_vector_set(ysel, j, val);
2051 } else {
2052 /* bad pixel */
2053 cpl_vector_set(ysel, j, HUGE_VAL);
2054 }
2055
2056 }
2057
2058 /* Set mask values (bad value only if no good pixel in image
2059 column) */
2060 if (h == extn[3]) {
2061 if (ngood == 0) {
2062 cpl_vector_set(vecdat->vec[extn[3]], i, maskval[0]);
2063 } else {
2064 cpl_vector_set(vecdat->vec[extn[3]], i, maskval[1]);
2065 }
2066 continue;
2067 }
2068
2069 /* Get resulting value depending on kind of data */
2070 if (ngood > 0) {
2071 cpl_vector_sort(ysel, CPL_SORT_ASCENDING);
2072 jmed = ceil(lowpixfrac * ngood) - 1;
2073 if (jmed < 0) {
2074 jmed = 0;
2075 }
2076 med = cpl_vector_get(ysel, jmed);
2077 } else {
2078 cpl_vector_sort(yvec, CPL_SORT_ASCENDING);
2079 jmed = ceil(lowpixfrac * ny) - 1;
2080 if (jmed < 0) {
2081 jmed = 0;
2082 }
2083 med = cpl_vector_get(yvec, jmed);
2084 }
2085
2086 /* Rough error estimate */
2087 if (h == extn[2]) {
2088 nmed = 2. * ngood * SC_MIN(lowpixfrac, 1. - lowpixfrac);
2089 if (nmed > 0.) {
2090 med /= sqrt(nmed);
2091 }
2092 }
2093
2094 /* Provide fluxes and errors for given effective pixel number */
2095 if (h == extn[1] || h == extn[2]) {
2096 med *= cpl_vector_get(selpix, i);
2097 }
2098
2099
2100 /* Write resulting value into output vector */
2101 cpl_vector_set(vecdat->vec[h], i, med);
2102
2103 }
2104 }
2105
2106 /* Free allocated memory */
2107 cpl_vector_delete(yvec);
2108 cpl_vector_delete(ysel);
2109
2110 return CPL_ERROR_NONE;
2111}
2112
2113
2114cpl_error_code sc_conv_ascii_read(sctarr *tabdat, const char *filename,
2115 const cpl_array *colnames)
2116{
2136 FILE *stream;
2137 scpar val[SC_MAXPAR];
2138 char errtxt[SC_MAXLEN] = "", str[SC_LENLINE+2] = "";
2139 const char **col;
2140 int ncolmin = 0, nrec = 0, j = 0, i = 0;
2141 int ncol0 = SC_MAXPAR, ncol = SC_MAXPAR;
2142
2143 /* Get size of column name array */
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);
2148 }
2149
2150 /* Get pointer for column name array */
2151 col = cpl_array_get_data_string_const(colnames);
2152
2153 /* Check file existence and open ASCII file */
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);
2157 }
2158
2159 /* Find number of data lines */
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] == '-') {
2163 nrec++;
2164 } else {
2165 fclose(stream);
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",
2169 errtxt);
2170 }
2171 }
2172 rewind(stream);
2173
2174 /* No data points */
2175 if (nrec == 0) {
2176 fclose(stream);
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);
2180 }
2181
2182 /* Number of values per line */
2183 sc_basic_readline(stream, val, &ncol0);
2184 if (ncol0 < ncolmin) {
2185 fclose(stream);
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);
2189 }
2190 rewind(stream);
2191
2192 /* Initialise CPL table and CPL property list for content of ASCII file
2193 (memory allocation) */
2194 sc_conv_tarr_init(tabdat, 1);
2195
2196 /* Create empty CPL property lists */
2197 tabdat->head[0] = cpl_propertylist_new();
2198 tabdat->head[1] = cpl_propertylist_new();
2199
2200 /* Create CPL tables of required size (put data in first extension of
2201 output file) */
2202 tabdat->tab[0] = cpl_table_new(0);
2203 tabdat->tab[1] = cpl_table_new(nrec);
2204
2205 /* Create required table columns */
2206 for (j = 0; j < ncolmin; j++) {
2207 cpl_table_new_column(tabdat->tab[1], col[j], CPL_TYPE_DOUBLE);
2208 }
2209
2210 /* Read spectral data from file and write it to CPL table */
2211
2212 for (i = 0; i < nrec; i++) {
2213
2214 sc_basic_readline(stream, val, &ncol);
2215
2216 if (ncol != ncol0) {
2217 sc_conv_tarr_delete(tabdat);
2218 fclose(stream);
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",
2222 errtxt);
2223 }
2224
2225 for (j = 0; j < ncolmin; j++) {
2226 cpl_table_set(tabdat->tab[1], col[j], i, val[j].d);
2227 }
2228
2229 }
2230
2231 /* Close ASCII file */
2232 fclose(stream);
2233
2234 return CPL_ERROR_NONE;
2235}
2236
2237
2238cpl_error_code sc_conv_ascii_write(const char *filename, const sctarr *tabdat)
2239{
2257 FILE *stream;
2258 cpl_type coltype;
2259 cpl_array *colnames = NULL;
2260 char errtxt[SC_MAXLEN] = "";
2261 //char str[SC_MAXLEN] = "";
2262 char str1[SC_MAXLEN] = "";
2263 char* str = NULL;
2264 char* str_val = NULL;
2265 char strcomp[SC_LENLINE+2] = "";
2266 char **col;
2267 int nrow = 0, ncol = 0, i = 0, j = 0;
2268
2269 /* Check number of extensions */
2270 if (tabdat->next != 1) {
2271 sprintf(errtxt, "%s: sctarr *tabdat (number of extensions != 1)",
2272 SC_ERROR_IOS_TXT);
2273 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS, "%s", errtxt);
2274 }
2275
2276 /* Get and check number of data points */
2277 nrow = cpl_table_get_nrow(tabdat->tab[1]);
2278 if (nrow == 0) {
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);
2281 }
2282
2283 /* Get column names in input table */
2284 colnames = cpl_table_get_column_names(tabdat->tab[1]);
2285 ncol = cpl_array_get_size(colnames);
2286
2287 /* Get pointer to array */
2288 col = cpl_array_get_data_string(colnames);
2289
2290 /* Open output ASCII file */
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);
2295 }
2296
2297 /* Build string of column names */
2298 sprintf(str1, "#");
2299 for (j = 0; j < ncol; j++) {
2300 sprintf(strcomp, " %s", col[j]);
2301 str = cpl_sprintf("%s%s",str1, strcomp);
2302 //strncat(str, strcomp, strlen(strcomp));
2303 }
2304
2305 /* Write column names into ASCII file */
2306 fprintf(stream, "%s\n", str);
2307
2308 /* Write data into ASCII file */
2309 cpl_free(str);
2310 for (i = 0; i < nrow; i++) {
2311
2312 /* Build string of data values */
2313 str1[0] = '\0';
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],
2318 col[j], i));
2319 } else if (coltype == CPL_TYPE_INT) {
2320 sprintf(strcomp, "%d", cpl_table_get_int(tabdat->tab[1],
2321 col[j], i, NULL));
2322 } else if (coltype == CPL_TYPE_FLOAT) {
2323 sprintf(strcomp, "%f", cpl_table_get_float(tabdat->tab[1],
2324 col[j], i, NULL));
2325 } else if (coltype == CPL_TYPE_DOUBLE) {
2326 sprintf(strcomp, "%e", cpl_table_get_double(tabdat->tab[1],
2327 col[j], i, NULL));
2328 } else {
2329 fclose(stream);
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",
2334 errtxt);
2335 }
2336 //strncat(str, strcomp, strlen(strcomp));
2337 str = cpl_sprintf("%s%s",str1, strcomp);
2338 if (j != ncol-1) {
2339 sprintf(strcomp, " ");
2340 //strncat(str, strcomp, strlen(strcomp));
2341 str_val = cpl_sprintf("%s%s",str,strcomp);
2342 }
2343 }
2344
2345 /* Write data string into ASCII file */
2346 fprintf(stream, "%s\n", str);
2347
2348 }
2349
2350 /* Close ASCII file */
2351 fclose(stream);
2352 cpl_free(str);
2353 cpl_free(str_val);
2354 /* Free allocated memory */
2355 cpl_array_delete(colnames);
2356
2357 return CPL_ERROR_NONE;
2358}
2359
2360
2361cpl_error_code sc_conv_tarr_init(sctarr *tabdat, const int next)
2362{
2377 tabdat->next = next;
2378 tabdat->head = cpl_calloc(next+1, sizeof(cpl_propertylist *));
2379 tabdat->tab = cpl_calloc(next+1, sizeof(cpl_table *));
2380
2381 return CPL_ERROR_NONE;
2382}
2383
2384
2385cpl_error_code sc_conv_tarr_read(sctarr *tabdat, const char *filename)
2386{
2403 char errtxt[SC_MAXLEN] = "";
2404 int next = 0, fitsformat = 0, i = 0;
2405
2406 /* Get number of extensions and check file */
2407 next = cpl_fits_count_extensions(filename);
2408 if (next < 0) {
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);
2415 }
2416
2417 /* Check FITS format */
2418 sc_conv_checkfitsformat(&fitsformat, filename);
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);
2422 }
2423
2424 /* Initialise CPL tables and CPL property lists for content of FITS
2425 file (memory allocation) */
2426 sc_conv_tarr_init(tabdat, next);
2427
2428 /* Read FITS file */
2429 for (i = 0; i <= next; i++) {
2430 tabdat->head[i] = cpl_propertylist_load(filename, i);
2431 if (i == 0) {
2432 tabdat->tab[i] = cpl_table_new(0);
2433 } else {
2434 tabdat->tab[i] = cpl_table_load(filename, i, 0);
2435 }
2436 }
2437
2438 return CPL_ERROR_NONE;
2439}
2440
2441
2442cpl_error_code sc_conv_tarr_write(const char *filename, const sctarr *tabdat)
2443{
2456 int next = 0, i = 0;
2457
2458 /* Get number of FITS extensions */
2459 next = tabdat->next;
2460
2461 /* Write FITS table with next extensions */
2462 for (i = 0; i < next; i++) {
2463 if (i == 0) {
2464 cpl_table_save(tabdat->tab[i+1], tabdat->head[0],
2465 tabdat->head[i+1], filename, CPL_IO_CREATE);
2466 } else {
2467 cpl_table_save(tabdat->tab[i+1], NULL,
2468 tabdat->head[i+1], filename, CPL_IO_EXTEND);
2469 }
2470 }
2471
2472 return CPL_ERROR_NONE;
2473}
2474
2475
2476cpl_error_code sc_conv_tarr_delete(sctarr *tabdat)
2477{
2489 int next = 0, i = 0;
2490
2491 if (tabdat == NULL) {
2492 return CPL_ERROR_NONE;
2493 }
2494
2495 next = tabdat->next;
2496
2497 for (i = 0; i <= next; i++) {
2498 cpl_propertylist_delete(tabdat->head[i]);
2499 cpl_table_delete(tabdat->tab[i]);
2500 }
2501
2502 cpl_free(tabdat->head);
2503 cpl_free(tabdat->tab);
2504
2505 return CPL_ERROR_NONE;
2506}
2507
2508
2509cpl_error_code sc_conv_varr_init(scvarr *vecdat, const int next)
2510{
2525 vecdat->next = next;
2526 vecdat->head = cpl_calloc(next+1, sizeof(cpl_propertylist *));
2527 vecdat->vec = cpl_calloc(next+1, sizeof(cpl_vector *));
2528
2529 return CPL_ERROR_NONE;
2530}
2531
2532
2533cpl_error_code sc_conv_varr_read(scvarr *vecdat, const char *filename)
2534{
2551 char errtxt[SC_MAXLEN] = "";
2552 int next = 0, fitsformat = 0, i = 0;
2553
2554 /* Get number of extensions and check file */
2555 next = cpl_fits_count_extensions(filename);
2556 if (next < 0) {
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);
2559 }
2560
2561 /* Check FITS format */
2562 sc_conv_checkfitsformat(&fitsformat, filename);
2563 if (fitsformat != 2) {
2564 sprintf(errtxt, "%s: %s (no 1D FITS image)", SC_ERROR_UFS_TXT,
2565 filename);
2566 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS, "%s", errtxt);
2567 }
2568
2569 /* Initialise CPL tables and CPL property lists for content of FITS
2570 file (memory allocation) */
2571 sc_conv_varr_init(vecdat, next);
2572
2573 /* Read FITS file */
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);
2577 }
2578
2579 return CPL_ERROR_NONE;
2580}
2581
2582
2583cpl_error_code sc_conv_varr_write(const char *filename, const scvarr *vecdat)
2584{
2597 int next = 0, i = 0;
2598
2599 /* Get number of FITS extensions */
2600 next = vecdat->next;
2601
2602 /* Write 1D FITS image with next extensions */
2603 for (i = 0; i <= next; i++) {
2604 if (i == 0) {
2605 cpl_vector_save(vecdat->vec[i], filename, CPL_TYPE_FLOAT,
2606 vecdat->head[i], CPL_IO_CREATE);
2607 } else {
2608 cpl_vector_save(vecdat->vec[i], filename, CPL_TYPE_FLOAT,
2609 vecdat->head[i], CPL_IO_EXTEND);
2610 }
2611 }
2612
2613 return CPL_ERROR_NONE;
2614}
2615
2616
2617cpl_error_code sc_conv_varr_delete(scvarr *vecdat)
2618{
2630 int next = 0, i = 0;
2631
2632 if (vecdat == NULL) {
2633 return CPL_ERROR_NONE;
2634 }
2635
2636 next = vecdat->next;
2637
2638 for (i = 0; i <= next; i++) {
2639 cpl_propertylist_delete(vecdat->head[i]);
2640 cpl_vector_delete(vecdat->vec[i]);
2641 }
2642
2643 cpl_free(vecdat->head);
2644 cpl_free(vecdat->vec);
2645
2646 return CPL_ERROR_NONE;
2647}
2648
2649
2650cpl_error_code sc_conv_iarr_init(sciarr *imadat, const int next)
2651{
2666 imadat->next = next;
2667 imadat->head = cpl_calloc(next+1, sizeof(cpl_propertylist *));
2668 imadat->ima = cpl_calloc(next+1, sizeof(cpl_image *));
2669
2670 return CPL_ERROR_NONE;
2671}
2672
2673
2674cpl_error_code sc_conv_iarr_read(sciarr *imadat, const char *filename)
2675{
2692 char errtxt[SC_MAXLEN] = "";
2693 int next = 0, fitsformat = 0, i = 0;
2694
2695 /* Get number of extensions and check file */
2696 next = cpl_fits_count_extensions(filename);
2697 if (next < 0) {
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);
2700 }
2701
2702 /* Check FITS format */
2703 sc_conv_checkfitsformat(&fitsformat, filename);
2704 if (fitsformat != 3) {
2705 sprintf(errtxt, "%s: %s (no 2D FITS image)",SC_ERROR_UFS_TXT,
2706 filename);
2707 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS, "%s", errtxt);
2708 }
2709
2710 /* Initialise CPL tables and CPL property lists for content of FITS
2711 file (memory allocation) */
2712 sc_conv_iarr_init(imadat, next);
2713
2714 /* Read FITS file */
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);
2718 }
2719
2720 return CPL_ERROR_NONE;
2721}
2722
2723
2724cpl_error_code sc_conv_iarr_write(const char *filename, const sciarr *imadat)
2725{
2738 int next = 0, i = 0;
2739
2740 /* Get number of FITS extensions */
2741 next = imadat->next;
2742
2743 /* Write 2D FITS image with next extensions */
2744 for (i = 0; i <= next; i++) {
2745 if (i == 0) {
2746 cpl_image_save(imadat->ima[i], filename, CPL_TYPE_UNSPECIFIED,
2747 imadat->head[i], CPL_IO_CREATE);
2748 } else {
2749 cpl_image_save(imadat->ima[i], filename, CPL_TYPE_UNSPECIFIED,
2750 imadat->head[i], CPL_IO_EXTEND);
2751 }
2752 }
2753
2754 return CPL_ERROR_NONE;
2755}
2756
2757
2758cpl_error_code sc_conv_iarr_delete(sciarr *imadat)
2759{
2771 int next = 0, i = 0;
2772
2773 if (imadat == NULL) {
2774 return CPL_ERROR_NONE;
2775 }
2776
2777 next = imadat->next;
2778
2779 for (i = 0; i <= next; i++) {
2780 cpl_propertylist_delete(imadat->head[i]);
2781 cpl_image_delete(imadat->ima[i]);
2782 }
2783
2784 cpl_free(imadat->head);
2785 cpl_free(imadat->ima);
2786
2787 return CPL_ERROR_NONE;
2788}
2789
#define SC_MAXLEN
Definition: sc_basic.h:94
#define SC_MIN(a, b)
Definition: sc_basic.h:85
#define SC_TOL
Definition: sc_basic.h:96
#define SC_DEFFLUXCOL
Definition: sc_basic.h:109
#define SC_MAXPAR
Definition: sc_basic.h:99
cpl_error_code sc_basic_getmaskval_image(double maskval[2], const cpl_image *image)
Definition: sc_basic.c:2657
#define SC_DEFMASKCOL
Definition: sc_basic.h:113
void sc_basic_abspath(char *out, const char *dir, const char *cwd)
Definition: sc_basic.c:1133
#define SC_LENLINE
Definition: sc_basic.h:92
cpl_error_code sc_basic_readline(FILE *stream, scpar par[], int *npar)
Definition: sc_basic.c:52
#define SC_DEFLAMCOL
Definition: sc_basic.h:107
cpl_error_code sc_conv_tarr_read(sctarr *tabdat, const char *filename)
Definition: sc_conv.c:2385
cpl_error_code sc_conv_varr2tarr(sctarr *tabdat, const scvarr *vecdat, const cpl_table *extnames)
Definition: sc_conv.c:516
cpl_error_code sc_conv_ascii_read(sctarr *tabdat, const char *filename, const cpl_array *colnames)
Definition: sc_conv.c:2114
cpl_error_code sc_conv_setextnames_iarr(cpl_table *extnames, const sciarr *imadat, const cpl_parameterlist *parlist)
Definition: sc_conv.c:372
cpl_error_code sc_conv_varr_delete(scvarr *vecdat)
Definition: sc_conv.c:2617
cpl_error_code sc_conv_getmaskval(double maskval[2], const sctarr *tabdat, const cpl_table *extnames)
Definition: sc_conv.c:1412
cpl_error_code sc_conv_varr_read(scvarr *vecdat, const char *filename)
Definition: sc_conv.c:2533
cpl_error_code sc_conv_results2tarr(sctarr *tabdat, cpl_table *results)
Definition: sc_conv.c:1122
cpl_error_code sc_conv_tarr_write(const char *filename, const sctarr *tabdat)
Definition: sc_conv.c:2442
cpl_error_code sc_conv_tarr_delete(sctarr *tabdat)
Definition: sc_conv.c:2476
cpl_error_code sc_conv_checkfitsformat(int *fitsformat, const char *filename)
Definition: sc_conv.c:47
cpl_error_code sc_conv_iarr2varr_sci(scvarr *vecdat, cpl_vector *selpix, const sciarr *imadat, const cpl_table *extnames, const double minrelprofflux)
Definition: sc_conv.c:1515
cpl_error_code sc_conv_iarr_write(const char *filename, const sciarr *imadat)
Definition: sc_conv.c:2724
cpl_error_code sc_conv_readprepfits(sctarr *tabdat, const cpl_parameterlist *parlist)
Definition: sc_conv.c:1067
cpl_error_code sc_conv_iarr_init(sciarr *imadat, const int next)
Definition: sc_conv.c:2650
cpl_error_code sc_conv_varr_write(const char *filename, const scvarr *vecdat)
Definition: sc_conv.c:2583
cpl_error_code sc_conv_iarr_delete(sciarr *imadat)
Definition: sc_conv.c:2758
cpl_error_code sc_conv_tarr_init(sctarr *tabdat, const int next)
Definition: sc_conv.c:2361
cpl_error_code sc_conv_setextnames_varr(cpl_table *extnames, const scvarr *vecdat, const cpl_parameterlist *parlist)
Definition: sc_conv.c:228
cpl_error_code sc_conv_iarr2varr_sky(scvarr *vecdat, const sciarr *imadat, const cpl_table *extnames, const double lowpixfrac, const cpl_vector *selpix)
Definition: sc_conv.c:1843
cpl_error_code sc_conv_modtable(sctarr *tabdat, const cpl_parameterlist *parlist)
Definition: sc_conv.c:787
cpl_error_code sc_conv_erasemaskcol(sctarr *tabdat, const cpl_parameterlist *parlist)
Definition: sc_conv.c:1206
cpl_error_code sc_conv_resultstarr2varr(scvarr *vecdat, const cpl_table *extnames, const sctarr *tabdat)
Definition: sc_conv.c:1268
cpl_error_code sc_conv_ascii_write(const char *filename, const sctarr *tabdat)
Definition: sc_conv.c:2238
cpl_error_code sc_conv_setcolnames(cpl_array *colnames, const cpl_parameterlist *parlist)
Definition: sc_conv.c:164
cpl_error_code sc_conv_varr_init(scvarr *vecdat, const int next)
Definition: sc_conv.c:2509
double sc_conv_getwcskey(const cpl_propertylist *plist, const char *key)
Definition: sc_conv.c:669
cpl_error_code sc_conv_iarr_read(sciarr *imadat, const char *filename)
Definition: sc_conv.c:2674
#define SC_MAXFLUXFAC
Definition: sc_conv.h:67
#define SC_EDGEFRAC
Definition: sc_conv.h:64