85 cpl_boolean fl_data = CPL_FALSE;
86 int npar0 = 0, i = 0, nparx = 0;
94 sprintf(errtxt,
"%s: npar < 1 (parameters at line)",
96 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP,
"%s", errtxt);
101 for (i = 0; i < npar0; i++) {
109 while (fgets(line,
SC_MAXLEN, stream) != NULL) {
110 if (line[0] !=
'#' && strspn(line,
"\n\t ") != strlen(line)) {
116 if (fl_data == CPL_FALSE) {
117 sprintf(errtxt,
"%s: FILE *stream (no parameters)",
119 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
"%s", errtxt);
124 str = (
char **) calloc(npar0+1,
sizeof(
char *));
126 sprintf(errtxt,
"%s: char **str", SC_ERROR_ISM_TXT);
127 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_ISM,
"%s", errtxt);
132 str[0] = strtok(line,
"\n\t ");
134 for (i = 1; i < npar0; i++) {
135 str[i] = strtok(NULL,
"\n\t ");
136 if (str[i] == NULL) {
143 if (nparx == npar0) {
144 str[npar0] = strtok(NULL,
"\n\t ");
145 if (str[npar0] != NULL) {
148 sprintf(errtxt,
"%s: FILE *stream (more parameters than "
149 "expected)", SC_ERROR_UFS_TXT);
150 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UFS,
"%s",
157 for (i = 0; i < nparx; i++) {
158 strcpy(par[i].c, str[i]);
159 strtok(par[i].c,
"\r");
160 par[i].i = strtol(str[i], NULL, 10);
162 par[i].d = strtod(str[i], NULL);
175 return CPL_ERROR_NONE;
185 double refjd = 0., ncycle = 0., rest = 0.;
186 double frac = 0., nyears = 0., date = 0.;
188 refjd = mjd - 51544.;
189 ncycle = floor(refjd / 1461.);
190 rest = refjd - ncycle * 1461.;
196 frac = modf((rest - 366.) / 365., &nyears);
197 nyears += 4 * ncycle + 1;
200 date = 2000. + nyears + frac;
207 int *hh,
int *mm,
double *ss,
208 const double *fracyear)
238 int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
239 int days_in_year = 365;
246 *year = floor(*fracyear);
249 rest = *fracyear - *year;
252 rest = floor(rest/acc + 1) * acc;
254 if (*year % 4 == 0 && *year % 100 != 0) {
261 rest *= days_in_year;
264 for (i = 0; i < 12 && (rest-days_in_month[i]) >= 0; i++) {
265 rest -= days_in_month[i];
272 *day = floor(rest) + 1;
297 const char *outflux,
const cpl_table *inspec,
298 const char *inlam,
const char *influx)
322 int n_in = 0, n_out = 0, i = 0, jo = -1, j = 0;
323 const double *inlamv = NULL, *influxv = NULL;
324 double *outlamv = NULL, *outfluxv = NULL;
325 double olmin = 0., olmax = 0., dol = 0., ilmin = 0., ilmax = 0., dil = 0.;
329 n_in = cpl_table_get_nrow(inspec);
330 n_out = cpl_table_get_nrow(outspec);
333 if (cpl_table_has_column(outspec, outflux) == 0) {
334 cpl_table_new_column(outspec, outflux, CPL_TYPE_DOUBLE);
338 cpl_table_fill_column_window(outspec, outflux, 0, n_out, 0.);
341 if (n_in <= 0 || n_out <= 0) {
342 return CPL_ERROR_NONE;
346 inlamv = cpl_table_get_data_double_const(inspec, inlam);
347 influxv = cpl_table_get_data_double_const(inspec, influx);
348 outlamv = cpl_table_get_data_double(outspec, outlam);
349 outfluxv = cpl_table_get_data_double(outspec, outflux);
353 cpl_table_fill_column_window(outspec, outflux, 0, n_out, influxv[0]);
354 return CPL_ERROR_NONE;
357 for (i = 0; i < n_out; i++) {
364 olmin = 1.5 * inlamv[0] - 0.5 * inlamv[1];
365 olmax = 1.5 * inlamv[n_in-1] - 0.5 * inlamv[n_in-2];
370 olmin = 1.5 * outlamv[i] - 0.5 * outlamv[i+1];
375 if (i == n_out - 1) {
376 olmax = 1.5 * outlamv[i] - 0.5 * outlamv[i-1];
378 olmax = 0.5 * (outlamv[i] + outlamv[i+1]);
392 ilmin = 1.5 * inlamv[j] - 0.5 * inlamv[j+1];
398 ilmax = 1.5 * inlamv[j] - 0.5 * inlamv[j-1];
400 ilmax = 0.5 * (inlamv[j] + inlamv[j+1]);
407 if (ilmin < olmin && ilmax <= olmax) {
409 }
else if (ilmin >= olmin && ilmax > olmax) {
411 }
else if (ilmin < olmin && ilmax > olmax) {
425 outfluxv[i] += influxv[j] * rdl;
430 }
while (ilmax <= olmax && j < n_in);
437 return CPL_ERROR_NONE;
442 const cpl_array *kernel)
465 int n = 0, nkpix = 0, k = 0, kmin = 0, kmax = 0, i = 0, j = 0;
466 const double *kern = NULL;
467 double *flux = NULL, *tflux = NULL;
468 double sum = 0., out = 0.;
472 n = cpl_table_get_nrow(spec);
474 return CPL_ERROR_NONE;
479 nkpix = cpl_array_get_size(kernel);
481 return CPL_ERROR_NONE;
486 kern = cpl_array_get_data_double_const(kernel);
490 for (k = 0; k < nkpix; k++) {
491 if (kern[k] < 0 || kern[k] > 1) {
492 sprintf(errtxt,
"%s: cpl_array *kernel "
493 "(kernel element(s) < 0 or > 1)", SC_ERROR_IOV_TXT);
494 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s",
500 if (sum < 1 - SC_TOL || sum > 1 +
SC_TOL) {
501 sprintf(errtxt,
"%s: cpl_array *kernel (sum of kernel elements != 1)",
503 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
509 return CPL_ERROR_NONE;
514 tempspec = cpl_table_duplicate(spec);
515 cpl_table_fill_column_window(spec, colname, 0, n, 0.);
519 flux = cpl_table_get_data_double(spec, colname);
520 tflux = cpl_table_get_data_double(tempspec, colname);
525 if (nkpix % 2 == 0) {
528 kmin = - (nkpix - 1) / 2;
530 kmax = kmin + nkpix - 1;
534 for (i = 0; i < n; i++) {
536 for (out = 0., k = kmin; k <= kmax; k++) {
548 out += tflux[j] * kern[k - kmin];
558 cpl_table_delete(tempspec);
560 return CPL_ERROR_NONE;
565 const cpl_array *flux,
567 const cpl_array *kernel)
596 int n = 0, i = 0, nkpix = 0, k = 0, kmin = 0, kmax = 0, jmin = 0;
598 const double *influx = NULL, *kern = NULL;
599 double *outflux = NULL;
600 double sum = 0., in0 = 0., innm1 = 0., in = 0.;
604 n = cpl_array_get_size(flux);
606 return CPL_ERROR_NONE;
611 if (cpl_array_get_size(convflux) != n) {
612 sprintf(errtxt,
"%s: cpl_array *convflux != cpl_array *flux (size)",
614 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOS,
"%s", errtxt);
619 if (range[0] > range[1]) {
620 sprintf(errtxt,
"%s: range[2] (min. > max.)", SC_ERROR_IIP_TXT);
621 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP,
"%s", errtxt);
623 if (range[0] < 0 || range[1] >= n) {
624 sprintf(errtxt,
"%s: range[2] (invalid pixel)", SC_ERROR_IIP_TXT);
625 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IIP,
"%s", errtxt);
630 if (cpl_array_has_invalid(convflux) == 1) {
631 cpl_array_fill_window_double(convflux, 0, n, 0.);
636 influx = cpl_array_get_data_double_const(flux);
637 outflux = cpl_array_get_data_double(convflux);
641 nkpix = cpl_array_get_size(kernel);
643 for (i = range[0]; i <= range[1]; i++) {
644 outflux[i] += influx[i];
646 return CPL_ERROR_NONE;
651 kern = cpl_array_get_data_double_const(kernel);
655 for (k = 0; k < nkpix; k++) {
656 if (kern[k] < 0 || kern[k] > 1) {
657 for (i = range[0]; i <= range[1]; i++) {
658 outflux[i] += influx[i];
660 sprintf(errtxt,
"%s: cpl_array *kernel "
661 "(kernel element(s) < 0 or > 1)", SC_ERROR_IOV_TXT);
662 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s",
668 if (sum < 1 - SC_TOL || sum > 1 +
SC_TOL) {
669 for (i = range[0]; i <= range[1]; i++) {
670 outflux[i] += influx[i];
672 sprintf(errtxt,
"%s: cpl_array *kernel (sum of kernel elements != 1)",
674 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
680 for (i = range[0]; i <= range[1]; i++) {
681 outflux[i] += influx[i];
683 return CPL_ERROR_NONE;
689 if (nkpix % 2 == 0) {
692 kmin = - (nkpix - 1) / 2;
694 kmax = kmin + nkpix - 1;
703 if (range[1] == n-1) {
716 for (j = jmin; j <= jmax; j++) {
731 for (k =
SC_MAX(kmin, -j); k <=
SC_MIN(kmax, n-1 - j); k++) {
735 outflux[i] += in * kern[k - kmin];
741 return CPL_ERROR_NONE;
766 int n = 0, urad = 0, j = 0, i = 0;
767 double *flux = NULL, *tflux = NULL, *mflux = NULL;
770 n = cpl_table_get_nrow(spec);
772 return CPL_ERROR_NONE;
777 return CPL_ERROR_NONE;
781 tempspec = cpl_table_duplicate(spec);
782 cpl_table_fill_column_window(spec, colname, 0, n, 0.);
785 flux = cpl_table_get_data_double(spec, colname);
786 tflux = cpl_table_get_data_double(tempspec, colname);
790 medpix = cpl_array_new(npix, CPL_TYPE_DOUBLE);
791 cpl_array_fill_window(medpix, 0, n, tflux[0]);
794 mflux = cpl_array_get_data_double(medpix);
797 urad = (int) floor(0.5 * npix);
801 for (j = 0, i = -urad; i < n; i++) {
805 mflux[j] = tflux[n-1];
807 mflux[j] = tflux[i+urad];
812 flux[i] = cpl_array_get_median(medpix);
824 cpl_table_delete(tempspec);
825 cpl_array_delete(medpix);
827 return CPL_ERROR_NONE;
853 if (fseek(fp, 0, 0) != 0) {
854 return cpl_error_set_message(cpl_func, (cpl_error_code)CPL_ERROR_FILE_NOT_FOUND,
855 "Problem with file pointer");
865 return CPL_ERROR_NONE;
898 double t, logt, log_ew, log_ei, log_e, log_dbl_max = log(DBL_MAX);
903 cpl_msg_warning(cpl_func,
"Temperatures must be larger than 0K");
911 log_ew = 54.842763 - 6763.22 / t - 4.210 * logt + 0.000367 * t +
912 tanh(0.0415 * (t - 218.8)) * (53.878 - 1331.22 / t - 9.44523 *
913 logt + 0.014025 * t);
916 log_ei = 9.550426 - 5723.265 / t + 3.53068 * logt - 0.00728332 * t;
919 log_e =
SC_MIN(log_ew, log_ei);
922 if (log_e < log_dbl_max) {
923 p_sat = exp(log_e) / 100.;
925 p_sat = DBL_MAX / 100.;
926 cpl_msg_warning(cpl_func,
"Temperature value results in overflow");
929 p_h2o =
SC_MIN(*hum, 100) / 100. * p_sat;
931 *ppmv =
SC_MAX(p_h2o / *p * 1e6, 0);
936 const cpl_array *pres,
937 const cpl_array *rhum,
962 cpl_error_code err_code;
972 double aw[] = { 6.11583699e+00, 4.44606896e-01, 1.43177157e-02,
973 2.64224321e-04, 2.99291081e-06, 2.03154182e-08,
974 7.02620698e-11, 3.79534310e-14,-3.21582393e-16 },
975 ai[] = { 6.09868993e+00, 4.99320233e-01, 1.84672631e-02,
976 4.02737184e-04, 5.65392987e-06, 5.21693933e-08,
977 3.07839583e-10, 1.05785160e-12, 1.61444444e-15 },
982 int sz = cpl_array_get_size(temp);
984 ewater = cpl_array_new(sz, CPL_TYPE_DOUBLE);
985 eice = cpl_array_new(sz, CPL_TYPE_DOUBLE);
986 e = cpl_array_new(sz, CPL_TYPE_DOUBLE);
987 tc = cpl_array_new(sz, CPL_TYPE_DOUBLE);
989 a = cpl_array_new(9, CPL_TYPE_DOUBLE);
991 for (i = 0; i < sz; i++) {
992 cpl_array_set_double(tc, i,
993 cpl_array_get_double(temp, i, NULL) - 273.15);
994 cpl_array_set_double(ewater, i, 0);
995 cpl_array_set_double(eice, i, 0);
996 cpl_array_set_double(e, i, 0);
999 for (i = 0; i < 9; i++) {
1000 cpl_array_set_double(a, i, aw[i]);
1003 for (i = 8; i >= 0; i--) {
1004 for (j = 0; j < sz; j++) {
1005 val = cpl_array_get_double(a, i, NULL) +
1006 cpl_array_get_double(ewater, j, NULL) *
1007 cpl_array_get_double(tc, j, NULL);
1008 cpl_array_set_double(ewater, j, val);
1012 for (i = 0; i < 9; i++) {
1013 cpl_array_set_double(a, i, ai[i]);
1016 for (i = 8; i >= 0; i--) {
1017 for (j = 0; j < sz; j++) {
1018 val = cpl_array_get_double(a, i, NULL) +
1019 cpl_array_get_double(eice, j, NULL) *
1020 cpl_array_get_double(tc, j, NULL);
1021 cpl_array_set_double(eice, j, val);
1025 for (i = 0; i < sz; i++) {
1026 val =
SC_MIN(cpl_array_get_double(ewater, i, NULL),
1027 cpl_array_get_double(eice, i, NULL));
1028 cpl_array_set_double(e, i, val);
1031 err_code = CPL_ERROR_NONE;
1032 for (i = 0; i < sz; i++) {
1033 p = cpl_array_get_double(pres, i, NULL);
1036 cpl_msg_warning(cpl_func,
"sc_rhum2ppmv() encountered a problem:"
1037 " Division by zero");
1038 err_code = CPL_ERROR_DIVISION_BY_ZERO;
1040 val = cpl_array_get_double(rhum, i, NULL) / 100. *
1041 cpl_array_get_double(e, i, NULL) / p * 1e6;
1045 cpl_array_set_double(ppmv, i, val);
1048 cpl_array_delete(ewater);
1049 cpl_array_delete(eice);
1050 cpl_array_delete(e);
1051 cpl_array_delete(tc);
1052 cpl_array_delete(a);
1077 double c1 = 3.7417749e-5,
1079 log_dbl_max = log(DBL_MAX);
1084 if ((sz = cpl_array_get_size(wavelength)) != cpl_array_get_size(bb)) {
1085 cpl_array_set_size(bb, sz);
1088 for (i = 0; i < sz; i++) {
1090 if ((wave = cpl_array_get_double(wavelength, i, NULL)) == 0) {
1091 return cpl_error_set_message(cpl_func, (cpl_error_code)CPL_ERROR_DIVISION_BY_ZERO,
1092 "Zero wavelength value");
1095 val = c2 / wave / temp;
1097 if (val < log_dbl_max) {
1098 cpl_array_set_double(bb, i, c1 / (pow(wave, 5) * (expm1(val)))
1101 cpl_array_set_double(bb, i, 0);
1105 return CPL_ERROR_NONE;
1126 if ((
unsigned) (strrchr(dir,
'/')-dir) != (
unsigned) strlen(dir)-1) {
1154 char *ptr, *p_out, tmp_dir[
SC_MAXLEN] =
"";
1160 strcpy(tmp_dir, dir);
1162 if (strncmp(tmp_dir,
"/", 1) == 0) {
1172 ptr = strtok(tmp_dir,
"/");
1173 while (ptr != NULL) {
1174 if (strcmp(ptr,
"..") == 0) {
1176 if ((p_out = strrchr(out,
'/')) != NULL) {
1177 sprintf(p_out,
"%c",
'\0');
1178 if ((p_out = strrchr(out,
'/')) != NULL) {
1179 sprintf(p_out,
"%c",
'\0');
1185 if (strcmp(ptr,
".") != 0) {
1192 ptr = strtok(NULL,
"/");
1238 err = access(pathname, mode);
1242 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_ROFS,
1243 "%s: %s", SC_ERROR_ROFS_TXT, pathname);
1246 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_INVAL,
1247 "%s: %s", SC_ERROR_INVAL_TXT, pathname);
1250 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_TXTBSY,
1251 "%s: %s", SC_ERROR_TXTBSY_TXT, pathname);
1254 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_ACCES,
1255 "%s: %s", SC_ERROR_ACCES_TXT, pathname);
1258 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_LOOP,
1259 "%s: %s", SC_ERROR_LOOP_TXT, pathname);
1262 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NAMETOOLONG,
1263 "%s: %s", SC_ERROR_NAMETOOLONG_TXT,
1267 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NOENT,
1268 "%s: %s", SC_ERROR_NOENT_TXT, pathname);
1271 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NOTDIR,
1272 "%s: %s", SC_ERROR_NOTDIR_TXT, pathname);
1275 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_FAULT,
1276 "%s: %s", SC_ERROR_FAULT_TXT, pathname);
1279 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IO,
1280 "%s: %s", SC_ERROR_IO_TXT, pathname);
1283 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NOMEM,
1284 "%s: %s", SC_ERROR_NOMEM_TXT, pathname);
1289 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_UNDEF,
1290 "%s: %s", SC_ERROR_UNDEF_TXT, pathname);
1293 return CPL_ERROR_NONE;
1320 if ((year < 1) || (month < 1) || (month > 12) || (day < 1) || (day > 31)) {
1321 return (cpl_error_code)SC_ERROR_BADUSERINPUT;
1324 jd1 = day-32075+1461*(year+4800+(month-14)/12)/4;
1325 jd2 = 367*(month-2-(month-14)/12*12)/12;
1326 jd3 = 3*((year+4900+(month-14)/12)/100)/4;
1330 return CPL_ERROR_NONE;
1359 l -= (146097*n + 3)/4;
1360 y = 4000*(l + 1)/1461001;
1361 l += -1461*y/4 + 31;
1363 *day = (int)(l - 2447*j/80);
1366 *year = (int)(100*(n - 49) + y + l);
1369 if ((*year < 1) || (*month < 1) || (*month > 12) || (*day < 1) || (*day > 31)) {
1370 return (cpl_error_code)SC_ERROR_BADUSERINPUT;
1373 return CPL_ERROR_NONE;
1377cpl_error_code sc_basic_absfile(
char *absfilename,
const char *filename)
1381 if (filename[0] !=
'/') {
1383 if ((d = getcwd(cwd,
sizeof(cwd))) == NULL) {
1384 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_GETCWD,
1385 "%s: %s", SC_ERROR_GETCWD_TXT, cwd);
1390 if ((d = strrchr(filename,
'/')) != NULL) {
1392 strncpy(basedir, filename, (d-filename));
1394 strcat(absfilename, (d+1));
1397 strcpy(absfilename, cwd);
1398 strcat(absfilename, filename);
1401 return CPL_ERROR_NONE;
1405cpl_boolean sc_basic_parameterlists_same(cpl_parameterlist *list1,
1406 cpl_parameterlist *list2)
1408 cpl_boolean
bool = CPL_TRUE;
1410 cpl_parameter *p1, *p2;
1412 p1 = cpl_parameterlist_get_first(list1);
1413 p2 = cpl_parameterlist_get_first(list2);
1414 type = cpl_parameter_get_type(p1);
1417 if (cpl_parameter_get_int(p1) != cpl_parameter_get_int(p2)) {
1421 case CPL_TYPE_DOUBLE:
1422 if (cpl_parameter_get_double(p1) != cpl_parameter_get_double(p2)) {
1426 case CPL_TYPE_STRING:
1427 if (cpl_parameter_get_string(p1) != cpl_parameter_get_string(p2)) {
1435 if (
bool == CPL_TRUE) {
1436 while ((p1 = cpl_parameterlist_get_next(list1)) != NULL &&
1437 (p2 = cpl_parameterlist_get_next(list2)) != NULL) {
1438 type = cpl_parameter_get_type(p1);
1441 if (cpl_parameter_get_int(p1) !=
1442 cpl_parameter_get_int(p2)) {
1446 case CPL_TYPE_DOUBLE:
1447 if (cpl_parameter_get_double(p1) !=
1448 cpl_parameter_get_double(p2)) {
1452 case CPL_TYPE_STRING:
1453 if (cpl_parameter_get_string(p1) !=
1454 cpl_parameter_get_string(p2)) {
1461 if (
bool == CPL_FALSE) {
1469cpl_error_code sc_basic_status2txt(
char *msg,
const int status)
1475 sprintf(msg,
"Convergence in chi-square value");
1478 sprintf(msg,
"Convergence in parameter value");
1481 sprintf(msg,
"Both MP_OK_PAR and MP_OK_CHI hold");
1484 sprintf(msg,
"Convergence in orthogonality");
1487 sprintf(msg,
"Maximum number of iterations reached");
1490 sprintf(msg,
"ftol is too small; no further improvement");
1493 sprintf(msg,
"xtol is too small; no further improvement");
1497 sprintf(msg,
"gtol is too small; no further improvement");
1501 sprintf(msg,
"General input parameter error");
1504 sprintf(msg,
"User function produced non-finite values");
1507 sprintf(msg,
"No user function was supplied");
1509 case MP_ERR_NPOINTS:
1510 sprintf(msg,
"No user data points were supplied");
1513 sprintf(msg,
"No free parameters");
1516 sprintf(msg,
"Memory allocation error");
1518 case MP_ERR_INITBOUNDS:
1519 sprintf(msg,
"Initial values inconsistent w constraints");
1522 sprintf(msg,
"Initial constraints inconsistent");
1525 sprintf(msg,
"General input parameter error");
1528 sprintf(msg,
"Not enough degrees of freedom");
1531 sprintf(msg,
"Exception: unknown exit value");
1532 return (cpl_error_code)SC_ERROR_INVAL;
1535 return CPL_ERROR_NONE;
1540 cpl_array *arr,
const cpl_boolean clip) {
1574 double val_p, val_m, val = 0., mean_old, eps = 1e-4;
1578 nclip = 0, nclip_old = 0;
1581 n = cpl_array_get_size(arr);
1584 mad_arr = cpl_array_new(n, CPL_TYPE_DOUBLE);
1587 *mean = cpl_array_get_median(arr);
1588 for (i = 0; i < n; i++) {
1590 cpl_array_set_double(mad_arr, i,
1591 fabs(cpl_array_get_double(arr, i, NULL) - *mean));
1592 if (cpl_array_is_valid(arr, i) == 0) {
1594 cpl_array_set_invalid(mad_arr, i);
1597 *rms = cpl_array_get_median(mad_arr) * 1.5;
1602 for (i = 0; i < n; i++) {
1603 cpl_array_set_double(mad_arr, i, cpl_array_get_double(arr, i, NULL));
1604 if (cpl_array_is_valid(arr, i) == 0) {
1606 cpl_array_set_invalid(mad_arr, i);
1611 for (j = 0, mean_old = *mean, nclip = 0, nclip_old = 0; j < 100; j++) {
1613 val_p = *mean + (1.5 * *rms);
1614 val_m = *mean - (1.5 * *rms);
1615 for (i = 0, *mean = 0; i < n; i++) {
1616 if (cpl_array_is_valid(mad_arr, i) == 1) {
1617 val = cpl_array_get_double(mad_arr, i, NULL);
1620 cpl_array_set_invalid(arr, i);
1621 cpl_array_set_double(mad_arr, i, val_p);
1623 }
else if (val < val_m) {
1625 cpl_array_set_invalid(arr, i);
1626 cpl_array_set_double(mad_arr, i, val_m);
1631 *mean = cpl_array_get_mean(mad_arr);
1632 *rms = cpl_array_get_stdev(mad_arr)*1.134;
1634 if (fabs(mean_old / *mean - 1) < eps || nclip == nclip_old) {
1641 if (clip == CPL_TRUE) {
1642 *mean = cpl_array_get_mean(arr);
1643 *rms = cpl_array_get_stdev(arr);
1646 cpl_array_delete(mad_arr);
1648 return CPL_ERROR_NONE;
1653 const char *colname)
1681 cpl_type tabcoltype;
1682 cpl_type arrcoltype;
1687 nrow_tab = cpl_table_get_nrow(tab);
1688 nrow_arr = cpl_array_get_size(arr);
1689 tabcoltype = cpl_table_get_column_type(tab, colname);
1690 arrcoltype = cpl_array_get_type(arr);
1693 if (nrow_tab == 0) {
1694 sprintf(err_msg,
"CPL input table has size = 0!\n"
1695 "Cannot continue. Emergency stop.");
1696 return cpl_error_set_message(cpl_func, (cpl_error_code)CPL_ERROR_ILLEGAL_INPUT,
1701 if (cpl_table_has_column(tab, colname) == 0) {
1702 sprintf(err_msg,
"CPL input table does not contain a column '%s'!\n"
1703 "Cannot continue. Emergency stop.",colname);
1704 return cpl_error_set_message(cpl_func, (cpl_error_code)CPL_ERROR_ILLEGAL_INPUT,
1710 cpl_msg_warning(cpl_func,
"Input CPL array size > 0, existing data "
1711 "will be overwritten!");
1715 if (tabcoltype != arrcoltype) {
1716 sprintf(err_msg,
"CPL types of table column and array do not match!\n"
1717 "Cannot continue. Emergency stop.");
1718 return cpl_error_set_message(cpl_func, (cpl_error_code)CPL_ERROR_ILLEGAL_INPUT,
1723 cpl_array_set_size(arr, nrow_tab);
1725 for (loop = 0; loop < nrow_tab; loop++)
1727 switch (tabcoltype) {
1729 cpl_array_set_int(arr, loop,
1730 cpl_table_get_int(tab,colname,loop,NULL));
1732 case CPL_TYPE_FLOAT:
1733 cpl_array_set_float(arr, loop,
1734 cpl_table_get_float(tab,colname,loop,NULL));
1736 case CPL_TYPE_DOUBLE:
1737 cpl_array_set_double(arr, loop,
1738 cpl_table_get_double(tab,colname,loop,NULL));
1740 case CPL_TYPE_STRING:
1741 cpl_array_set_string(arr, loop,
1742 cpl_table_get_string(tab,colname,loop));
1745 sprintf(err_msg,
"CPL type not supported!\n"
1746 "Cannot continue. Emergency stop.");
1747 return cpl_error_set_message(cpl_func, (cpl_error_code)CPL_ERROR_ILLEGAL_INPUT,
1753 return cpl_error_get_code();
1758 const cpl_array *arr)
1792 cpl_type arrcoltype;
1797 arrcoltype = cpl_array_get_type(arr);
1798 nrow_arr = cpl_array_get_size(arr);
1799 nrow_tab = cpl_table_get_nrow(tab);
1802 if (nrow_arr == 0) {
1803 sprintf(err_msg,
"CPL array size = 0\n"
1804 "Cannot continue. Emergency stop.");
1805 return cpl_error_set_message(cpl_func, (cpl_error_code)CPL_ERROR_ILLEGAL_INPUT,
1810 if ((nrow_arr != nrow_tab) && (nrow_tab > 0)) {
1811 sprintf(err_msg,
"CPL array and table size do not coincide!\n"
1812 "Cannot continue. Emergency stop.");
1813 return cpl_error_set_message(cpl_func, (cpl_error_code)CPL_ERROR_ILLEGAL_INPUT,
1819 if (cpl_table_has_column(tab, colname) == 0) {
1820 arrcoltype = cpl_array_get_type(arr);
1821 cpl_table_new_column(tab, colname, arrcoltype);
1822 if (nrow_tab == 0) {
1823 cpl_table_set_size(tab,nrow_arr);
1828 for (loop = 0; loop<nrow_arr; loop++) {
1829 switch (arrcoltype) {
1831 cpl_table_set_int(tab, colname, loop,
1832 cpl_array_get_int(arr, loop, NULL));
1834 case CPL_TYPE_FLOAT:
1835 cpl_table_set_float(tab, colname, loop,
1836 cpl_array_get_float(arr, loop, NULL));
1838 case CPL_TYPE_DOUBLE:
1839 cpl_table_set_double(tab, colname, loop,
1840 cpl_array_get_double(arr, loop, NULL));
1842 case CPL_TYPE_STRING:
1843 cpl_table_set_string(tab, colname, loop,
1844 cpl_array_get_string(arr, loop));
1847 sprintf(err_msg,
"CPL type not supported!\n"
1848 "Cannot continue. Emergency stop.");
1849 return cpl_error_set_message(cpl_func, (cpl_error_code)CPL_ERROR_ILLEGAL_INPUT,
1855 return cpl_error_get_code();
1859int sc_basic_sortarr_double(
const void *p1,
const void *p2)
1861 if (*(
double *)p1 > *(
double *)p2) {
1863 }
else if (*(
double *)p1 < *(
double *)p2) {
1898 if (cpl_array_get_size(arr) == 0) {
1899 return CPL_ERROR_DATA_NOT_FOUND;
1904 case CPL_TYPE_DOUBLE:
1906 p = cpl_array_get_data_double(arr);
1909 qsort(p, cpl_array_get_size(arr),
1910 cpl_type_get_sizeof(CPL_TYPE_DOUBLE),
1911 sc_basic_sortarr_double);
1913 return CPL_ERROR_NONE;
1917 return CPL_ERROR_INVALID_TYPE;
1924 const cpl_table *intab)
1942 cpl_array *colnames;
1944 char* colname = NULL;
1946 int ncol = 0, i = 0;
1951 if (cpl_table_compare_structure(outtab, intab) != 0) {
1952 sprintf(errtxt,
"%s: structure of cpl_table *outtab != *intab",
1954 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s", errtxt);
1958 colnames = cpl_table_get_column_names(intab);
1959 ncol = cpl_array_get_size(colnames);
1963 for (i = 0; i < ncol; i++) {
1965 colname = cpl_sprintf(
"%s", cpl_array_get_string(colnames, i));
1966 type = cpl_table_get_column_type(intab, colname);
1968 if (type == CPL_TYPE_STRING) {
1969 sp = cpl_table_get_data_string_const(intab, colname);
1970 cpl_table_copy_data_string(outtab, colname, sp);
1971 }
else if (type == CPL_TYPE_INT) {
1972 ip = cpl_table_get_data_int_const(intab, colname);
1973 cpl_table_copy_data_int(outtab, colname, ip);
1974 }
else if (type == CPL_TYPE_FLOAT) {
1975 fp = cpl_table_get_data_float_const(intab, colname);
1976 cpl_table_copy_data_float(outtab, colname, fp);
1977 }
else if (type == CPL_TYPE_DOUBLE) {
1978 dp = cpl_table_get_data_double_const(intab, colname);
1979 cpl_table_copy_data_double(outtab, colname, dp);
1985 cpl_array_delete(colnames);
1987 return CPL_ERROR_NONE;
1992 const cpl_table *intab)
2011 cpl_array *colnames;
2012 char* colname = NULL;
2014 int nrow = 0, ncol = 0, i = 0;
2020 nrow = cpl_table_get_nrow(intab);
2021 cpl_table_set_size(outtab, nrow);
2024 colnames = cpl_table_get_column_names(intab);
2025 ncol = cpl_array_get_size(colnames);
2029 for (i = 0; i < ncol; i++) {
2031 colname = cpl_sprintf(
"%s", cpl_array_get_string(colnames, i));
2032 type = cpl_table_get_column_type(intab, colname);
2033 if (cpl_table_has_column(outtab, colname) != 1) {
2034 cpl_table_new_column(outtab, colname, type);
2037 if (type == CPL_TYPE_STRING) {
2038 sp = cpl_table_get_data_string_const(intab, colname);
2039 cpl_table_copy_data_string(outtab, colname, sp);
2040 }
else if (type == CPL_TYPE_INT) {
2041 ip = cpl_table_get_data_int_const(intab, colname);
2042 cpl_table_copy_data_int(outtab, colname, ip);
2043 }
else if (type == CPL_TYPE_FLOAT) {
2044 fp = cpl_table_get_data_float_const(intab, colname);
2045 cpl_table_copy_data_float(outtab, colname, fp);
2046 }
else if (type == CPL_TYPE_DOUBLE) {
2047 dp = cpl_table_get_data_double_const(intab, colname);
2048 cpl_table_copy_data_double(outtab, colname, dp);
2054 cpl_array_delete(colnames);
2056 return CPL_ERROR_NONE;
2080 for (i = 0; i < n; i++) {
2107 p = str + strlen(str);
2131 if (str != NULL && *str != 0) {
2137 *out = (
char *)malloc(len + 1);
2144 while (isspace(*q)) {
2152 while (isspace(*q)) {
2198 if (str != NULL && *str != 0) {
2206 while (isspace(*q)) {
2214 while (isspace(*q)) {
2258 cpl_boolean issign = CPL_FALSE, ispoint = CPL_FALSE, isexp = CPL_FALSE;
2259 int length = 0, iexp = 0, i = 0;
2263 length = strlen(str);
2265 for (iexp = 0, i = 0; i < length; i++) {
2267 if (isdigit(str[i]) != 0) {
2270 }
else if (str[i] ==
'+' || str[i] ==
'-') {
2275 if (i == length-1) {
2277 }
else if (i > 1 && iexp != 1) {
2280 }
else if (str[i] ==
'.') {
2282 if (i == 0 && length == 1) {
2284 }
else if (i > 0 && ispoint == CPL_TRUE) {
2286 }
else if (i > 1 && iexp > 0) {
2290 }
else if (str[i] ==
'e' || str[i] ==
'E') {
2292 if (i == length-1) {
2294 }
else if (i == 1 &&
2295 (issign == CPL_TRUE || ispoint == CPL_TRUE)) {
2297 }
else if (i > 0 && isexp == CPL_TRUE) {
2306 if (isexp == CPL_TRUE) {
2347 if (str[0] ==
'+' || str[0] ==
'-') {
2353 for (;(unsigned)i < strlen(str); i++) {
2354 if (isdigit(str[i]) == 0) {
2360 if (falseflag == 1) {
2370 const long n_out,
const double *x_ref,
2371 const double *y_ref,
const long n_ref,
2372 const int extrapolate)
2411 int divide_by_zero = 0;
2419 for (ref = 0, out = 0; out < n_out; out++, xo++, yo++) {
2424 while (*xr < *xo && ref < n_ref) {
2438 if (ref == n_ref - 1) {
2439 if (extrapolate == 1) {
2440 xr = x_ref + n_ref - 2;
2441 yr = y_ref + n_ref - 2;
2443 xr = x_ref + n_ref - 1;
2444 yr = y_ref + n_ref - 1;
2449 if (*(xr + 1) - *xr == 0) {
2454 if (extrapolate != 1 && *xo < *xr) {
2458 *yo = (*xo - *xr) * (*(yr + 1) - *yr) /
2459 (*(xr + 1) - *xr) + *yr;
2464 if (divide_by_zero == 1) {
2465 return cpl_error_set_message(cpl_func, (cpl_error_code)CPL_ERROR_DIVISION_BY_ZERO,
2466 "Duplicate x-values");
2468 return CPL_ERROR_NONE;
2474 const int n_data,
const double *par)
2498 for (i = 0; i < n_data; i++) {
2499 fgauss[i] = par[0] * pow(CPL_MATH_E, -pow(xgauss[i] - par[1], 2) /
2500 (2 * pow(par[2], 2)));
2527 cpl_boolean hasdot = CPL_FALSE, hasslash = CPL_FALSE;
2532 if (path == NULL || path[0] ==
'\0') {
2536 return CPL_ERROR_NONE;
2546 for (i = len-1; i >= 0; i--) {
2547 if (str[i] ==
'.' && hasdot == CPL_FALSE && hasslash == CPL_FALSE) {
2550 }
else if (str[i] ==
'/' && hasslash == CPL_FALSE) {
2552 hasslash = CPL_TRUE;
2557 if (hasdot == CPL_FALSE && hasslash == CPL_FALSE) {
2561 if (filename != NULL) {
2562 strcpy(filename, str);
2565 return CPL_ERROR_NONE;
2571 ptr = strtok(str,
" ");
2572 if (hasslash == CPL_FALSE) {
2576 if (filename != NULL) {
2577 sprintf(filename,
"%s", ptr);
2581 sprintf(dir,
"%s", ptr);
2583 ptr = strtok(NULL,
" ");
2584 if (filename != NULL) {
2585 sprintf(filename,
"%s", ptr);
2590 if (hasdot == CPL_TRUE && suffix != NULL) {
2591 ptr = strtok(NULL,
" ");
2592 sprintf(suffix,
"%s", ptr);
2597 return CPL_ERROR_NONE;
2602 const cpl_vector *vector)
2624 double maskmin = 0., maskmax = 0.;
2627 n = cpl_vector_get_size(vector);
2629 sprintf(errtxt,
"%s: cpl_vector *vector", SC_ERROR_NDA_TXT);
2630 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
2634 maskmin = cpl_vector_get_min(vector);
2635 maskmax = cpl_vector_get_max(vector);
2638 if ((maskmin == 0. || maskmin == 1.) && maskmax == 1.) {
2640 maskval[0] = maskmin;
2641 maskval[1] = maskmax;
2642 }
else if (maskmin == 0. && maskmax != 1.) {
2644 maskval[0] = maskmax;
2645 maskval[1] = maskmin;
2647 sprintf(errtxt,
"%s: cpl_vector *vector (minimum != 0 and != 1)",
2649 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s",
2653 return CPL_ERROR_NONE;
2658 const cpl_image *image)
2680 double maskmin = 0., maskmax = 0.;
2683 nx = cpl_image_get_size_x(image);
2684 ny = cpl_image_get_size_y(image);
2685 if (nx <= 0 || ny <= 0) {
2686 sprintf(errtxt,
"%s: cpl_image *image", SC_ERROR_NDA_TXT);
2687 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_NDA,
"%s", errtxt);
2691 maskmin = cpl_image_get_min(image);
2692 maskmax = cpl_image_get_max(image);
2695 if ((maskmin == 0. || maskmin == 1.) && maskmax == 1.) {
2699 }
else if (maskmin == 0. && maskmax != 1.) {
2701 maskval[0] = maskmax;
2704 sprintf(errtxt,
"%s: cpl_image *image (minimum != 0 and != 1)",
2706 return cpl_error_set_message(cpl_func, (cpl_error_code)SC_ERROR_IOV,
"%s",
2710 return CPL_ERROR_NONE;
2736 int nsinc, nsinch, k;
2742 cpl_error_ensure((nsinc % 2 == 1), CPL_ERROR_ILLEGAL_INPUT,
2743 return CPL_ERROR_ILLEGAL_INPUT,
2744 "sc_basic_calcsinc requires odd number of points");
2747 cpl_vector_set_size(sinc, nsinc);
2748 cpl_vector_fill(sinc, 0.);
2757 kern = cpl_vector_get_data(sinc);
2760 for (k = 0; k < nsinch; k++) {
2761 double x = (k - nsinch) * dx;
2766 * sin(CPL_MATH_PI * x) / (CPL_MATH_PI * x);
2773 for (k = nsinch + 1; k < nsinc; k++) {
2774 kern[k] = kern[nsinc - k - 1];
2777 return CPL_ERROR_NONE;
cpl_error_code sc_basic_rebin(cpl_table *outspec, const char *outlam, const char *outflux, const cpl_table *inspec, const char *inlam, const char *influx)
double sc_basic_mjd2fracyear(double mjd)
cpl_error_code sc_basic_linecount(long *n_lines, FILE *fp)
int sc_basic_gaussfunc(double *fgauss, const double *xgauss, const int n_data, const double *par)
cpl_error_code sc_basic_access(const char *pathname, const int mode)
cpl_error_code sc_basic_col2arr(cpl_array *arr, const cpl_table *tab, const char *colname)
cpl_boolean sc_basic_isnumber(char *str)
cpl_error_code sc_basic_clipmean(double *mean, double *rms, cpl_array *arr, const cpl_boolean clip)
cpl_error_code sc_basic_copytable_content(cpl_table *outtab, const cpl_table *intab)
cpl_error_code sc_basic_greg2jd(long *jd, const int year, const int month, const int day)
#define SC_SINCRAD_PRECOMP
cpl_error_code sc_basic_filtermedian(cpl_table *spec, const char *colname, const int npix)
cpl_error_code sc_basic_copytable_full(cpl_table *outtab, const cpl_table *intab)
cpl_error_code sc_basic_interpollin(const double *x_out, double *y_out, const long n_out, const double *x_ref, const double *y_ref, const long n_ref, const int extrapolate)
cpl_error_code sc_basic_convolve(cpl_table *spec, const char *colname, const cpl_array *kernel)
char * sc_basic_strtrim(char *str)
cpl_error_code sc_basic_getmaskval_image(double maskval[2], const cpl_image *image)
cpl_error_code sc_basic_arr2col(cpl_table *tab, const char *colname, const cpl_array *arr)
double sc_basic_fracyear2date(int *year, int *month, int *day, int *hh, int *mm, double *ss, const double *fracyear)
void sc_basic_abspath(char *out, const char *dir, const char *cwd)
void sc_basic_dirslash(char *dir)
void sc_basic_rhum2ppmv(double *ppmv, const double *tem, const double *p, const double *hum)
void sc_basic_initstring(char *str, const long n)
cpl_error_code sc_basic_getfilename(char *dir, char *filename, char *suffix, const char *path)
cpl_error_code sc_basic_convolvewindow(cpl_array *convflux, const cpl_array *flux, const int range[2], const cpl_array *kernel)
cpl_error_code sc_basic_rhum2ppmv_old(const cpl_array *temp, const cpl_array *pres, const cpl_array *rhum, cpl_array *ppmv)
void sc_basic_strtrim_inplace(char *str)
cpl_error_code sc_basic_readline(FILE *stream, scpar par[], int *npar)
cpl_error_code sc_basic_getmaskval_vector(double maskval[2], const cpl_vector *vector)
cpl_error_code sc_basic_calcsinc(cpl_vector *sinc)
cpl_error_code sc_basic_planck(cpl_array *bb, const cpl_array *wavelength, const double temp)
cpl_error_code sc_basic_jd2greg(int *year, int *month, int *day, const long jd)
cpl_error_code sc_basic_sortarr(cpl_array *arr, cpl_type type)
void sc_basic_terminatestring(char *str)
cpl_boolean sc_basic_isinteger(char *str)