39 #include <fors_utils.h>
43 static cpl_polynomial *read_global_distortion(cpl_table *global, cpl_size row);
52 float cpl_tools_get_median_float(
float *, cpl_size);
54 #define MAX_COLNAME (80)
55 #define STRETCH_FACTOR (1.20)
59 static int mos_multiplex = -1;
60 static int mos_region_size = 800;
62 static double default_lines_hi[] = {
122 static double default_lines_lo[] = {
201 static void mos_seed(
void)
203 srand((
unsigned int)time((time_t *)0));
206 static double mos_randg(
int seme)
209 static int gotit = 1;
210 double x1, x2, w, y1;
221 x1 = 2.0 * (double)rand() / RAND_MAX - 1.0;
222 x2 = 2.0 * (double)rand() / RAND_MAX - 1.0;
223 w = x1 * x1 + x2 * x2;
224 }
while (w >= 1.0 || w == 0.0);
226 w = sqrt( (-2.0 * log(w)) / w);
245 static cpl_image *mos_image_vertical_median_filter(cpl_image *ima_in,
246 int filtsizey,
int refrow,
247 int above,
int below,
int step)
250 const char *func =
"mos_image_general_median_filter";
252 cpl_image *filt_img = NULL;
257 int upright_y, loleft_y;
259 int yIsEven = !(filtsizey - (filtsizey/2)*2);
261 int nx = cpl_image_get_size_x(ima_in);
262 int ny = cpl_image_get_size_y(ima_in);
266 if (yIsEven) filtsizey++;
268 if (ny <= filtsizey) {
270 "Median filter size: %d, image size: %d", filtsizey, ny);
276 filt_img = cpl_image_duplicate(ima_in);
277 buf = cpl_malloc(filtsizey *
sizeof(
float));
278 data = cpl_image_get_data(ima_in);
279 fdata = cpl_image_get_data(filt_img);
281 firstRow = refrow - step * (below / step);
285 for (col = 0; col < nx; col++) {
286 for (row = firstRow; row < refrow + above; row += step) {
289 loleft_y = row - f2y;
290 upright_y = row + f2y + 1;
291 for (j = loleft_y; j < upright_y; j++)
292 buf[j - loleft_y] = data[col + j * nx];
294 fdata[col + row * nx] = cpl_tools_get_median_float(buf, filtsizey);
321 static int peakPosition(
const float *data,
int size,
float *position,
327 float max, median, level, pos, variance, uniformVariance;
342 copy = (
float *) cpl_malloc(size*
sizeof(
float));
343 for (i = 0; i < size; i++)
345 median = cpl_tools_get_median_float(copy, size);
354 for (i = 1; i < size; i++)
364 if (max-median < 0.00001)
373 level = (max + median) / 2;
384 for (i = 0, sum = 0., weights = 0.; i < size; i++) {
385 if (data[i] > level) {
387 weights += (data[i] - median);
388 sum += i * (data[i] - median);
398 if (count < minPoints)
402 for (i = 0, sum = 0., weights = 0.; i < size; i++) {
403 if (data[i] > level) {
405 sum += (i - pos) * (i - pos);
408 variance = sqrt(sum / weights);
418 uniformVariance = sqrt(size*size/3 - pos*size + pos*pos);
420 if (variance > 0.8 * uniformVariance)
423 *position = pos + 0.5;
475 static double values_to_dx(
double v1,
double v2,
double v3)
478 static double epsilon = 0.00000001;
482 if (v1 > v2 || v3 > v2)
485 if (2 * v2 - v1 - v3 < epsilon)
488 r = 0.5 * (v3 - v1) / (2 * v2 - v3 - v1);
500 static float *min_filter(
float *buffer,
int length,
int size)
502 float *minf = cpl_calloc(length,
sizeof(
float));
504 int start = size / 2;
505 int end = length - size / 2;
509 for (i = start; i < end; i++) {
510 min = buffer[i-start];
511 for (j = i - start + 1; j <= i + start; j++)
517 for (i = 0; i < start; i++)
518 minf[i] = minf[start];
520 for (i = end; i < length; i++)
521 minf[i] = minf[end-1];
532 static float *max_filter(
float *buffer,
int length,
int size)
534 float *maxf = cpl_calloc(length,
sizeof(
float));
536 int start = size / 2;
537 int end = length - size / 2;
541 for (i = start; i < end; i++) {
542 max = buffer[i-start];
543 for (j = i - start + 1; j <= i + start; j++)
549 for (i = 0; i < start; i++)
550 maxf[i] = maxf[start];
552 for (i = end; i < length; i++)
553 maxf[i] = maxf[end-1];
564 static float *smo_filter(
float *buffer,
int length,
int size)
566 float *smof = cpl_calloc(length,
sizeof(
float));
568 int start = size / 2;
569 int end = length - size / 2;
573 for (i = start; i < end; i++) {
575 for (j = i - start; j <= i + start; j++)
577 smof[i] = sum / size;
580 for (i = 0; i < start; i++)
581 smof[i] = smof[start];
583 for (i = end; i < length; i++)
584 smof[i] = smof[end-1];
608 static cpl_polynomial *read_global_distortion(cpl_table *global, cpl_size row)
610 cpl_polynomial *poly = NULL;
616 char name[MAX_COLNAME];
619 for (p[0] = 0; p[0] <= degree; p[0]++) {
620 for (p[1] = 0; p[1] <= degree - p[0]; p[1]++) {
621 snprintf(name, MAX_COLNAME,
"a%"CPL_SIZE_FORMAT
"%"CPL_SIZE_FORMAT
"", p[0], p[1]);
622 coeff = cpl_table_get_double(global, name, row, &null);
626 poly = cpl_polynomial_new(2);
627 cpl_polynomial_set_coeff(poly, p, coeff);
634 static cpl_table *write_global_distortion(cpl_table *global,
int row,
635 cpl_polynomial *poly)
642 char name[MAX_COLNAME];
649 table = cpl_table_new(nrow);
650 for (p[0] = 0; p[0] <= degree; p[0]++) {
651 for (p[1] = 0; p[1] <= degree - p[0]; p[1]++) {
652 snprintf(name, MAX_COLNAME,
"a%"CPL_SIZE_FORMAT
"%"CPL_SIZE_FORMAT
"", p[0], p[1]);
653 cpl_table_new_column(table, name, CPL_TYPE_DOUBLE);
659 for (p[0] = 0; p[0] <= degree; p[0]++) {
660 for (p[1] = 0; p[1] <= degree - p[0]; p[1]++) {
661 snprintf(name, MAX_COLNAME,
"a%"CPL_SIZE_FORMAT
"%"CPL_SIZE_FORMAT
"", p[0], p[1]);
662 cpl_table_set_double(table, name, row,
663 cpl_polynomial_get_coeff(poly, p));
681 #define SEGNO(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
682 static int robustLinearFit(cpl_bivector *
list,
double *a,
double *b,
689 double aa, bb, bcomp, b1, b2, del, abdevt, f, f1, f2, sigb, temp, d, sum;
690 double sx, sy, sxy, sxx, chisq;
697 int max_iterate = 30;
701 np = cpl_bivector_get_size(list);
702 vx = cpl_bivector_get_x(list);
703 vy = cpl_bivector_get_y(list);
704 x = cpl_vector_get_data(vx);
705 y = cpl_vector_get_data(vy);
707 sx = sy = sxx = sxy = 0.00;
708 for (i = 0; i < np; i++) {
715 del = np * sxx - sx * sx;
716 aa_ls = aa = (sxx * sy - sx * sxy) / del;
717 bb_ls = bb = (np * sxy - sx * sy) / del;
720 for (i = 0; i < np; i++) {
721 temp = y[i] - (aa+bb*x[i]);
726 va = cpl_vector_new(np);
727 arr = cpl_vector_get_data(va);
728 sigb = sqrt(chisq/del);
733 for (i = 0; i < np; i++) {
734 arr[i] = y[i] - bcomp * x[i];
736 aa = cpl_vector_get_median_const(va);
738 for (i = 0; i < np; i++) {
739 d = y[i] - (bcomp * x[i] + aa);
744 sum += (d >= 0.0 ? x[i] : -x[i]);
748 b2 = bb + SEGNO(3.0 * sigb, f1);
752 for (i = 0; i < np; i++) {
753 arr[i] = y[i] - bcomp * x[i];
755 aa = cpl_vector_get_median_const(va);
757 for (i = 0; i < np; i++) {
758 d = y[i] - (bcomp * x[i] + aa);
763 sum += (d >= 0.0 ? x[i] : -x[i]);
767 if (fabs(b2-b1)<1e-7) {
770 *abdev = abdevt / (double)np;
771 cpl_vector_delete(va);
776 while (f1*f2 > 0.0) {
784 for (i = 0; i < np; i++) {
785 arr[i] = y[i] - bcomp * x[i];
787 aa = cpl_vector_get_median_const(va);
789 for (i = 0; i < np; i++) {
790 d = y[i] - (bcomp * x[i] + aa);
795 sum += (d >= 0.0 ? x[i] : -x[i]);
799 if (iter >= max_iterate)
802 if (iter >= max_iterate) {
806 cpl_vector_delete(va);
811 while (fabs(b2-b1) > sigb) {
812 bb = 0.5 * (b1 + b2);
813 if ((fabs(bb-b1) < 1e-7) || (fabs(bb-b2) < 1e-7))
817 for (i = 0; i < np; i++) {
818 arr[i] = y[i] - bcomp * x[i];
820 aa = cpl_vector_get_median_const(va);
822 for (i = 0; i < np; i++) {
823 d = y[i] - (bcomp * x[i] + aa);
828 sum += (d >= 0.0 ? x[i] : -x[i]);
841 cpl_vector_delete(va);
844 *abdev = abdevt / np;
859 cpl_table *mos_hough_table(cpl_table *table,
const char *x,
const char *y)
871 npoints = cpl_table_get_nrow(table);
872 opoints = npoints*(npoints-1)/2;
874 output = cpl_table_new(opoints);
875 cpl_table_new_column(output,
"m", CPL_TYPE_DOUBLE);
876 cpl_table_new_column(output,
"q", CPL_TYPE_DOUBLE);
877 cpl_table_fill_column_window_double(output,
"m", 0, opoints, 0.0);
878 cpl_table_fill_column_window_double(output,
"q", 0, opoints, 0.0);
880 xodata = cpl_table_get_data_double(output,
"m");
881 yodata = cpl_table_get_data_double(output,
"q");
883 cpl_table_cast_column(table, x,
"x", CPL_TYPE_DOUBLE);
884 cpl_table_cast_column(table, y,
"y", CPL_TYPE_DOUBLE);
886 xdata = cpl_table_get_data_double(table,
"x");
887 ydata = cpl_table_get_data_double(table,
"y");
890 for (i = 0; i < npoints; i++) {
891 for (j = i+1; j < npoints; j++) {
892 xodata[k] = (ydata[i]-ydata[j])/(xdata[i]-xdata[j]);
893 yodata[k] = ydata[i] - xodata[k] * xdata[i];
899 printf(
"Assert k = %d, expected %d\n", k, opoints);
901 cpl_table_erase_column(table,
"x");
902 cpl_table_erase_column(table,
"y");
913 static void mos_extraction(cpl_image *sciwin, cpl_image *sci_var_win,
915 cpl_image *extracted, cpl_image *sky,
916 cpl_image *error,
int nobjects,
int extraction,
917 double ron,
double conad,
int ncomb)
920 cpl_vector *vprofile;
932 double sumWeight, sum, sumSky, sumProf, sumVar, variance, weight;
946 specLen = cpl_image_get_size_x(sciwin);
947 numRows = cpl_image_get_size_y(sciwin);
949 edata = cpl_image_get_data(extracted);
950 edata += nobjects * specLen;
952 ekdata = cpl_image_get_data(sky);
953 ekdata += nobjects * specLen;
955 endata = cpl_image_get_data(error);
956 endata += nobjects * specLen;
958 sdata = cpl_image_get_data(sciwin);
959 kdata = cpl_image_get_data(skywin);
960 if(sci_var_win != NULL)
961 vardata = cpl_image_get_data(sci_var_win);
968 if (extraction && numRows > 5) {
970 fdata = cpl_image_get_data(smowin);
971 for (i = 0; i < specLen; i++)
972 for (j = 0, edata[i] = 0.0; j < numRows; j++)
973 edata[i] += fdata[i + j * specLen];
974 cpl_image_delete(smowin);
977 for (i = 0; i < specLen; i++)
978 for (j = 0, edata[i] = 0.0; j < numRows; j++)
979 edata[i] += sdata[i + j * specLen];
984 profile = cpl_calloc(specLen * numRows,
sizeof(
double));
985 buffer = cpl_calloc(specLen,
sizeof(
double));
987 for (iter = 0; iter < maxIter; iter++) {
993 for (i = 0; i < specLen; i++) {
994 for (j = 0; j < numRows; j++) {
995 index = i + j * specLen;
997 if (fabs(edata[i]) > 0.00001)
998 profile[index] = sdata[index] / edata[i];
1000 profile[index] = 0.0;
1004 for (j = 0; j < numRows; j++) {
1010 for (i = 0; i < specLen - smoothBox; i++) {
1011 vprofile = cpl_vector_wrap(smoothBox, profile + i + j*specLen);
1012 value = cpl_vector_get_median_const(vprofile);
1013 cpl_vector_unwrap(vprofile);
1016 buffer[i + smoothBox / 2] = value;
1023 vprofile = cpl_vector_wrap(smoothBox / 2, profile + j*specLen);
1024 value = cpl_vector_get_mean(vprofile);
1025 cpl_vector_unwrap(vprofile);
1030 for (i = 0; i < smoothBox / 2; i++)
1033 vprofile = cpl_vector_wrap(smoothBox / 2,
1034 profile + specLen - smoothBox/2 + j*specLen);
1035 value = cpl_vector_get_mean(vprofile);
1036 cpl_vector_unwrap(vprofile);
1041 for (i = 0; i < smoothBox / 2; i++)
1042 buffer[i + specLen - smoothBox / 2] = value;
1044 for (i = 0; i < specLen; i++)
1045 profile[i + j * specLen] = buffer[i];
1053 for (i = 0; i < specLen; i++) {
1054 for (j = 0, value = 0.0; j < numRows; j++)
1055 value += profile[i + j * specLen];
1056 if (value > 0.00001)
1057 for (j = 0; j < numRows; j++)
1058 profile[i + j * specLen] /= value;
1060 for (j = 0; j < numRows; j++)
1061 profile[i + j * specLen] = 0.0;
1069 for (i = 0; i < specLen; i++) {
1075 for (j = 0; j < numRows; j++) {
1076 index = i + j * specLen;
1083 variance = ron*ron + fabs(edata[i] * profile[index] + kdata[index])
1086 value = sdata[index] - edata[i] * profile[index];
1087 if (fabs(value) / sqrt(variance) < nsigma) {
1088 weight = 1000000 * profile[index] / variance;
1089 sum += weight * sdata[index];
1090 sumSky += weight * kdata[index];
1091 sumWeight += weight * profile[index];
1092 sumProf += profile[index];
1096 if(sci_var_win != NULL)
1097 sumVar += weight * weight * vardata[index];
1101 if (sumWeight > 0.00001) {
1102 edata[i] = sum / sumWeight;
1103 ekdata[i] = sumSky / sumWeight;
1104 if(sci_var_win != NULL)
1105 endata[i] = sqrt(sumVar / sumWeight / sumWeight);
1107 endata[i] = 1000 * sqrt(sumProf / sumWeight);
1129 for (i = 0; i < specLen; i++)
1130 for (j = 0, ekdata[i] = 0.0; j < numRows; j++)
1131 ekdata[i] += kdata[i + j * specLen];
1136 for (i = 0; i < specLen; i++)
1138 if(sci_var_win != NULL)
1141 for (j = 0, endata[i] = 0.0; j < numRows; j++)
1142 endata[i] += vardata[i + j * specLen];
1143 endata[i] = sqrt(endata[i]);
1146 endata[i] = sqrt(ron*ron + fabs(edata[i] + ekdata[i]) / conad);
1201 cpl_table *ids, cpl_table *crv,
1204 const char *func =
"mos_global_distortion";
1206 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
1208 cpl_table *global = NULL;
1218 cpl_polynomial *poly;
1231 int nslits, nmaskslits, npoints;
1237 if (slits == NULL || maskslits == NULL || ids == NULL || crv == NULL) {
1238 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1242 nslits = cpl_table_get_nrow(slits);
1244 if (nslits < minslit) {
1245 cpl_msg_warning(func,
"Too few slits (%d < %d) for global "
1246 "distortion model determination", nslits, minslit);
1250 nmaskslits = cpl_table_get_nrow(maskslits);
1252 length = cpl_table_get_data_int(slits,
"length");
1253 position = cpl_table_get_data_int(slits,
"position");
1254 slit_id = cpl_table_get_data_int(slits,
"slit_id");
1255 mslit_id = cpl_table_get_data_int(maskslits,
"slit_id");
1256 xtop = cpl_table_get_data_double(slits,
"xtop");
1257 ytop = cpl_table_get_data_double(slits,
"ytop");
1258 xbottom = cpl_table_get_data_double(slits,
"xbottom");
1259 ybottom = cpl_table_get_data_double(slits,
"ybottom");
1260 mxtop = cpl_table_get_data_double(maskslits,
"xtop");
1261 mytop = cpl_table_get_data_double(maskslits,
"ytop");
1262 mxbottom = cpl_table_get_data_double(maskslits,
"xbottom");
1263 mybottom = cpl_table_get_data_double(maskslits,
"ybottom");
1270 coeff = cpl_table_new(nslits);
1271 cpl_table_copy_structure(coeff, ids);
1272 cpl_table_new_column(coeff,
"xccd", CPL_TYPE_DOUBLE);
1273 cpl_table_new_column(coeff,
"yccd", CPL_TYPE_DOUBLE);
1274 cpl_table_new_column(coeff,
"xmask", CPL_TYPE_DOUBLE);
1275 cpl_table_new_column(coeff,
"ymask", CPL_TYPE_DOUBLE);
1277 for (i = 0; i < nslits; i++) {
1278 for (j = 0; j < nmaskslits; j++) {
1279 if (slit_id[i] == mslit_id[j]) {
1280 cpl_table_set_double(coeff,
"xmask", i,
1281 (mxtop[j] + mxbottom[j]) / 2);
1282 cpl_table_set_double(coeff,
"ymask", i,
1283 (mytop[j] + mybottom[j]) / 2);
1288 if (cpl_table_has_invalid(coeff,
"xmask")) {
1289 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
1290 cpl_table_delete(coeff);
1294 for (i = 0; i < nslits; i++) {
1295 cpl_table_set_double(coeff,
"xccd", i, (xtop[i] + xbottom[i]) / 2);
1296 cpl_table_set_double(coeff,
"yccd", i, (ytop[i] + ybottom[i]) / 2);
1299 for (i = 0; i < nslits; i++) {
1304 cpl_table_and_selected_window(ids, position[i], length[i]);
1305 dummy = cpl_table_extract_selected(ids);
1306 for (j = 0; j < 6; j++) {
1307 if (cpl_table_has_column(dummy, clab[j])) {
1308 if (length[i] - cpl_table_count_invalid(dummy, clab[j]) > 10) {
1309 cpl_table_set_double(coeff, clab[j], i,
1310 cpl_table_get_column_median(dummy, clab[j]));
1315 cpl_table_delete(dummy);
1316 cpl_table_select_all(ids);
1320 for (j = 0; j < 6; j++) {
1321 if (cpl_table_has_column(coeff, clab[j])) {
1322 cpl_table_and_selected_invalid(coeff, clab[j]);
1324 if (cpl_table_not_selected(coeff))
1325 dummy = cpl_table_extract_selected(coeff);
1329 npoints = cpl_table_get_nrow(dummy);
1338 ci = cpl_vector_wrap(npoints,
1339 cpl_table_get_data_double(dummy, clab[j]));
1341 xccd = cpl_vector_wrap(npoints,
1342 cpl_table_get_data_double(dummy,
"xccd"));
1343 yccd = cpl_vector_wrap(npoints,
1344 cpl_table_get_data_double(dummy,
"yccd"));
1345 ccd = cpl_bivector_wrap_vectors(xccd, yccd);
1348 poly = cpl_polynomial_fit_2d_create(ccd, ci, order, NULL);
1350 cpl_bivector_unwrap_vectors(ccd);
1351 cpl_vector_unwrap(xccd);
1352 cpl_vector_unwrap(yccd);
1353 cpl_vector_unwrap(ci);
1356 xmask = cpl_vector_wrap(npoints,
1357 cpl_table_get_data_double(dummy,
"xmask"));
1358 ymask = cpl_vector_wrap(npoints,
1359 cpl_table_get_data_double(dummy,
"ymask"));
1360 mask = cpl_bivector_wrap_vectors(xmask, ymask);
1363 poly = cpl_polynomial_fit_2d_create(mask, ci, order, NULL);
1365 cpl_bivector_unwrap_vectors(mask);
1366 cpl_vector_unwrap(xmask);
1367 cpl_vector_unwrap(ymask);
1368 cpl_vector_unwrap(ci);
1372 cpl_size p[2] = {0, 0};
1373 poly = cpl_polynomial_new(2);
1374 cpl_polynomial_set_coeff(poly, p,
1375 cpl_table_get_column_median(dummy, clab[j]));
1378 cpl_table_delete(dummy);
1380 global = write_global_distortion(global, j, poly);
1382 cpl_polynomial_delete(poly);
1384 cpl_table_select_all(coeff);
1388 cpl_table_delete(coeff);
1395 cpl_table_set_double(global,
"a00", 6, reference);
1402 coeff = cpl_table_duplicate(crv);
1403 cpl_table_new_column(coeff,
"xmask", CPL_TYPE_DOUBLE);
1404 cpl_table_new_column(coeff,
"ymask", CPL_TYPE_DOUBLE);
1405 cpl_table_new_column(coeff,
"xccd", CPL_TYPE_DOUBLE);
1406 cpl_table_new_column(coeff,
"yccd", CPL_TYPE_DOUBLE);
1407 slit_id = cpl_table_get_data_int(coeff,
"slit_id");
1408 npoints = cpl_table_get_nrow(coeff);
1410 for (i = 0; i < npoints; i++) {
1411 for (j = 0; j < nmaskslits; j++) {
1412 if (slit_id[i] == mslit_id[j]) {
1414 cpl_table_set_double(coeff,
"xmask", i, mxbottom[j]);
1415 cpl_table_set_double(coeff,
"ymask", i, mybottom[j]);
1418 cpl_table_set_double(coeff,
"xmask", i, mxtop[j]);
1419 cpl_table_set_double(coeff,
"ymask", i, mytop[j]);
1425 cpl_table_set_double(coeff,
"xccd", i, xtop[(i-1)/2]);
1426 cpl_table_set_double(coeff,
"yccd", i, ytop[(i-1)/2]);
1430 cpl_table_set_double(coeff,
"xccd", i, xbottom[i/2]);
1431 cpl_table_set_double(coeff,
"yccd", i, ybottom[i/2]);
1435 if (cpl_table_has_invalid(coeff,
"xmask")) {
1436 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
1437 cpl_table_delete(coeff);
1441 for (j = 0; j < 3; j++) {
1442 cpl_polynomial * poly_ccd;
1443 if (cpl_table_has_column(coeff, clab[j])) {
1444 cpl_table_and_selected_invalid(coeff, clab[j]);
1446 if (cpl_table_not_selected(coeff))
1447 dummy = cpl_table_extract_selected(coeff);
1451 npoints = cpl_table_get_nrow(dummy);
1460 ci = cpl_vector_wrap(npoints,
1461 cpl_table_get_data_double(dummy, clab[j]));
1462 xmask = cpl_vector_wrap(npoints,
1463 cpl_table_get_data_double(dummy,
"xmask"));
1464 ymask = cpl_vector_wrap(npoints,
1465 cpl_table_get_data_double(dummy,
"ymask"));
1466 mask = cpl_bivector_wrap_vectors(xmask, ymask);
1468 poly = cpl_polynomial_fit_2d_create(mask, ci, order, NULL);
1470 xccd = cpl_vector_wrap(npoints,
1471 cpl_table_get_data_double(dummy,
"xccd"));
1472 yccd = cpl_vector_wrap(npoints,
1473 cpl_table_get_data_double(dummy,
"yccd"));
1474 ccd = cpl_bivector_wrap_vectors(xccd, yccd);
1476 poly_ccd = cpl_polynomial_fit_2d_create(ccd, ci, order, NULL);
1479 cpl_bivector_unwrap_vectors(mask);
1480 cpl_vector_unwrap(ci);
1481 cpl_vector_unwrap(xmask);
1482 cpl_vector_unwrap(ymask);
1485 cpl_size p[2] = {0, 0};
1486 poly = cpl_polynomial_new(2);
1487 cpl_polynomial_set_coeff(poly, p,
1488 cpl_table_get_column_median(dummy, clab[j]));
1491 cpl_table_delete(dummy);
1493 global = write_global_distortion(global, j + 7, poly);
1494 global = write_global_distortion(global, j + 10, poly_ccd);
1496 cpl_polynomial_delete(poly);
1497 cpl_polynomial_delete(poly_ccd);
1498 cpl_table_select_all(coeff);
1502 cpl_table_delete(coeff);
1549 const char *func =
"mos_build_slit_location";
1551 cpl_propertylist *sort_col;
1552 cpl_polynomial *ids0;
1553 cpl_polynomial *crv[3];
1554 cpl_polynomial *loc_crv;
1572 if (global == NULL || maskslits == NULL) {
1573 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1577 nslits = cpl_table_get_nrow(maskslits);
1578 slit_id = cpl_table_get_data_int(maskslits,
"slit_id");
1579 mxtop = cpl_table_get_data_double(maskslits,
"xtop");
1580 mytop = cpl_table_get_data_double(maskslits,
"ytop");
1581 mxbottom = cpl_table_get_data_double(maskslits,
"xbottom");
1582 mybottom = cpl_table_get_data_double(maskslits,
"ybottom");
1584 slits = cpl_table_duplicate(maskslits);
1586 xtop = cpl_table_get_data_double(slits,
"xtop");
1587 ytop = cpl_table_get_data_double(slits,
"ytop");
1588 xbottom = cpl_table_get_data_double(slits,
"xbottom");
1589 ybottom = cpl_table_get_data_double(slits,
"ybottom");
1591 ids0 = read_global_distortion(global, 0);
1592 crv[0] = read_global_distortion(global, 7);
1593 crv[1] = read_global_distortion(global, 8);
1594 crv[2] = read_global_distortion(global, 9);
1596 loc_crv = cpl_polynomial_new(1);
1598 point = cpl_vector_new(2);
1599 dpoint = cpl_vector_get_data(point);
1601 for (i = 0; i < nslits; i++) {
1602 dpoint[0] = mxtop[i];
1603 dpoint[1] = mytop[i];
1605 xtop[i] = cpl_polynomial_eval(ids0, point);
1607 for (j = 0; j < 3; j++)
1609 cpl_polynomial_set_coeff(loc_crv, &j,
1610 cpl_polynomial_eval(crv[j], point));
1612 ytop[i] = cpl_polynomial_eval_1d(loc_crv, xtop[i], NULL);
1614 dpoint[0] = mxbottom[i];
1615 dpoint[1] = mybottom[i];
1616 xbottom[i] = cpl_polynomial_eval(ids0, point);
1618 for (j = 0; j < 3; j++)
1620 cpl_polynomial_set_coeff(loc_crv, &j,
1621 cpl_polynomial_eval(crv[j], point));
1623 ybottom[i] = cpl_polynomial_eval_1d(loc_crv, xbottom[i], NULL);
1626 cpl_vector_delete(point);
1627 cpl_polynomial_delete(ids0);
1628 cpl_polynomial_delete(loc_crv);
1629 for (j = 0; j < 3; j++)
1630 cpl_polynomial_delete(crv[j]);
1632 sort_col = cpl_propertylist_new();
1633 cpl_propertylist_append_bool(sort_col,
"ytop", 1);
1634 cpl_table_sort(slits, sort_col);
1635 cpl_table_sort(maskslits, sort_col);
1636 cpl_propertylist_delete(sort_col);
1642 cpl_table_and_selected_double(slits,
"ybottom", CPL_GREATER_THAN, ysize-1);
1643 cpl_table_or_selected_double(slits,
"ytop", CPL_LESS_THAN, 0);
1644 cpl_table_erase_selected(slits);
1646 nslits = cpl_table_get_nrow(slits);
1649 cpl_msg_warning(func,
"No slits found on the CCD");
1650 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
1651 cpl_table_delete(slits);
1656 cpl_msg_info(func,
"Slit location: %"CPL_SIZE_FORMAT
" slits are entirely or partially "
1657 "contained in CCD", nslits);
1659 cpl_msg_info(func,
"Slit location: %"CPL_SIZE_FORMAT
" slit is entirely or partially "
1660 "contained in CCD", nslits);
1696 const char *func =
"mos_build_curv_coeff";
1698 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
1701 cpl_polynomial *crv[3];
1703 cpl_table *polytraces;
1716 if (global == NULL || slits == NULL || maskslits == NULL) {
1717 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1721 nslits = cpl_table_get_nrow(maskslits);
1722 slit_id = cpl_table_get_data_int(maskslits,
"slit_id");
1723 xtop = cpl_table_get_data_double(maskslits,
"xtop");
1724 ytop = cpl_table_get_data_double(maskslits,
"ytop");
1725 xbottom = cpl_table_get_data_double(maskslits,
"xbottom");
1726 ybottom = cpl_table_get_data_double(maskslits,
"ybottom");
1728 polytraces = cpl_table_new(2*nslits);
1729 cpl_table_new_column(polytraces,
"slit_id", CPL_TYPE_INT);
1730 for (i = 0; i < 3; i++)
1731 cpl_table_new_column(polytraces, clab[i], CPL_TYPE_DOUBLE);
1733 crv[0] = read_global_distortion(global, 7);
1734 crv[1] = read_global_distortion(global, 8);
1735 crv[2] = read_global_distortion(global, 9);
1737 point = cpl_vector_new(2);
1738 dpoint = cpl_vector_get_data(point);
1740 for (i = 0; i < nslits; i++) {
1741 for (j = 0; j < 2; j++) {
1743 cpl_table_set_int(polytraces,
"slit_id", 2*i+j, slit_id[i]);
1746 dpoint[0] = xbottom[i];
1747 dpoint[1] = ybottom[i];
1750 dpoint[0] = xtop[i];
1751 dpoint[1] = ytop[i];
1754 for (k = 0; k < 3; k++)
1756 cpl_table_set_double(polytraces, clab[k], 2*i+j,
1757 cpl_polynomial_eval(crv[k], point));
1761 cpl_vector_delete(point);
1762 for (j = 0; j < 3; j++)
1763 cpl_polynomial_delete(crv[j]);
1769 nvalid = cpl_table_get_nrow(slits);
1770 valid_id = cpl_table_get_data_int(slits,
"slit_id");
1771 cpl_table_unselect_all(polytraces);
1772 for (i = 0; i < nslits; i++) {
1774 for (j = 0; j < nvalid; j++) {
1775 if (slit_id[i] == valid_id[j]) {
1781 cpl_table_select_row(polytraces, 2*i);
1782 cpl_table_select_row(polytraces, 2*i + 1);
1785 cpl_table_erase_selected(polytraces);
1787 nslits = cpl_table_get_nrow(polytraces);
1790 cpl_msg_warning(func,
"No slits found on the CCD");
1791 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
1792 cpl_table_delete(polytraces);
1797 cpl_msg_info(func,
"Curvature model: %d slits are entirely or "
1798 "partially contained in CCD", nslits / 2);
1800 cpl_msg_info(func,
"Curvature model: %d slit is entirely or "
1801 "partially contained in CCD", nslits / 2);
1850 const char *func =
"mos_build_disp_coeff";
1852 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
1854 cpl_polynomial *ids[6];
1856 cpl_table *idscoeff;
1871 if (global == NULL || slits == NULL) {
1872 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
1876 nslits = cpl_table_get_nrow(slits);
1877 position = cpl_table_get_data_int(slits,
"position");
1878 length = cpl_table_get_data_int(slits,
"length");
1879 xtop = cpl_table_get_data_double(slits,
"xtop");
1880 ytop = cpl_table_get_data_double(slits,
"ytop");
1881 xbottom = cpl_table_get_data_double(slits,
"xbottom");
1882 ybottom = cpl_table_get_data_double(slits,
"ybottom");
1884 for (i = 0; i < 6; i++)
1885 ids[i] = read_global_distortion(global, i);
1887 for (i = 0; i < 6; i++)
1894 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
1899 for (i = 0; i < nslits; i++)
1902 idscoeff = cpl_table_new(nrows);
1904 for (j = 0; j <= order; j++)
1905 cpl_table_new_column(idscoeff, clab[j], CPL_TYPE_DOUBLE);
1907 cpl_table_new_column(idscoeff,
"error", CPL_TYPE_DOUBLE);
1908 cpl_table_fill_column_window_double(idscoeff,
"error", 0, nrows, 0.0);
1909 cpl_table_new_column(idscoeff,
"nlines", CPL_TYPE_INT);
1910 cpl_table_fill_column_window_int(idscoeff,
"nlines", 0, nrows, 0);
1912 point = cpl_vector_new(2);
1913 dpoint = cpl_vector_get_data(point);
1915 for (i = 0; i < nslits; i++) {
1921 yhig = ylow + length[i];
1923 for (j = 0; j <= order; j++) {
1925 for (k = 0; k < length[i]; k++) {
1926 dpoint[0] = xbottom[i] + k*(xtop[i]-xbottom[i])/length[i];
1927 dpoint[1] = ybottom[i] + k*(ytop[i]-ybottom[i])/length[i];
1928 cpl_table_set_double(idscoeff, clab[j], ylow + k,
1929 cpl_polynomial_eval(ids[j], point));
1933 for (k = 0; k < length[i]; k++) {
1934 cpl_table_set_double(idscoeff, clab[0], ylow + k,
1935 xbottom[i] + k*(xtop[i]-xbottom[i])/length[i]);
1941 cpl_vector_delete(point);
1942 for (j = 0; j < 6; j++)
1943 cpl_polynomial_delete(ids[j]);
1973 cpl_table *polytraces,
double reference,
1974 double blue,
double red,
double dispersion)
1976 const char *func =
"mos_subtract_sky";
1978 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
1985 cpl_polynomial *polytop;
1986 cpl_polynomial *polybot;
1987 cpl_polynomial *trend;
2001 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
2005 int missing_top, missing_bot;
2014 if (science == NULL || slits == NULL || polytraces == NULL) {
2015 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
2019 if (dispersion <= 0.0) {
2020 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
2024 if (red - blue < dispersion) {
2025 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
2029 nx = cpl_image_get_size_x(science);
2030 ny = cpl_image_get_size_y(science);
2032 sky = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
2034 sdata = cpl_image_get_data(science);
2035 kdata = cpl_image_get_data(sky);
2037 nslits = cpl_table_get_nrow(slits);
2038 order = cpl_table_get_ncol(polytraces) - 2;
2039 length = cpl_table_get_data_int(slits,
"length");
2040 slit_id = cpl_table_get_data_int(slits,
"slit_id");
2047 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
2048 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
2050 for (i = 0; i < nslits; i++) {
2061 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
2063 start_pixel = refpixel - pixel_below;
2064 if (start_pixel < 0)
2067 end_pixel = refpixel + pixel_above;
2072 polytop = cpl_polynomial_new(1);
2073 for (k = 0; k <= order; k++) {
2074 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
2076 cpl_polynomial_delete(polytop);
2080 cpl_polynomial_set_coeff(polytop, &k, coeff);
2084 polybot = cpl_polynomial_new(1);
2085 for (k = 0; k <= order; k++) {
2086 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
2088 cpl_polynomial_delete(polybot);
2092 cpl_polynomial_set_coeff(polybot, &k, coeff);
2095 if (missing_top && missing_bot) {
2096 cpl_msg_debug(func,
"Slit %d was not traced: no extraction!",
2108 cpl_msg_debug(func,
"Upper edge of slit %d was not traced: "
2109 "the spectral curvature of the lower edge "
2110 "is used instead.", slit_id[i]);
2111 polytop = cpl_polynomial_duplicate(polybot);
2112 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
2113 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
2115 coeff = cpl_polynomial_get_coeff(polybot, &k);
2116 coeff += ytop - ybot;
2117 cpl_polynomial_set_coeff(polytop, &k, coeff);
2121 cpl_msg_debug(func,
"Lower edge of slit %d was not traced: "
2122 "the spectral curvature of the upper edge "
2123 "is used instead.", slit_id[i]);
2124 polybot = cpl_polynomial_duplicate(polytop);
2125 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
2126 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
2128 coeff = cpl_polynomial_get_coeff(polytop, &k);
2129 coeff -= ytop - ybot;
2130 cpl_polynomial_set_coeff(polybot, &k, coeff);
2138 for (j = start_pixel; j < end_pixel; j++) {
2139 top = cpl_polynomial_eval_1d(polytop, j, NULL);
2140 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
2141 itop = floor(top + 0.5) + 1;
2142 ibot = floor(bot + 0.5);
2151 list = cpl_bivector_new(npix);
2152 listx = cpl_bivector_get_x(list);
2153 listy = cpl_bivector_get_y(list);
2154 dlistx = cpl_vector_get_data(listx);
2155 dlisty = cpl_vector_get_data(listy);
2157 for (k = 0; k < npix; k++) {
2159 dlisty[k] = sdata[j + (ibot + k)*nx];
2162 if (robustLinearFit(list, &q, &m, &err)) {
2163 cpl_bivector_delete(list);
2167 cpl_bivector_delete(list);
2169 for (k = 0; k < npix; k++) {
2170 kdata[j + (ibot + k)*nx] = m*k + q;
2173 if (npix > window) {
2182 for (k = 0; k < npix; k++)
2183 if (fabs(sdata[j + (ibot + k)*nx] - m*k - q) < err)
2189 list = cpl_bivector_new(count);
2190 listx = cpl_bivector_get_x(list);
2191 listy = cpl_bivector_get_y(list);
2192 dlistx = cpl_vector_get_data(listx);
2193 dlisty = cpl_vector_get_data(listy);
2196 for (k = 0; k < npix; k++) {
2197 if (fabs(sdata[j + (ibot + k)*nx] - m*k - q) < err) {
2199 dlisty[count] = sdata[j + (ibot + k)*nx];
2204 trend = cpl_polynomial_fit_1d_create(listx, listy, 2, &err);
2206 cpl_bivector_delete(list);
2211 for (k = 0; k < npix; k++)
2212 if (fabs(sdata[j + (ibot + k)*nx]
2213 - cpl_polynomial_eval_1d(trend, k, NULL)) < err)
2217 cpl_polynomial_delete(trend);
2221 list = cpl_bivector_new(count);
2222 listx = cpl_bivector_get_x(list);
2223 listy = cpl_bivector_get_y(list);
2224 dlistx = cpl_vector_get_data(listx);
2225 dlisty = cpl_vector_get_data(listy);
2228 for (k = 0; k < npix; k++) {
2229 if (fabs(sdata[j + (ibot + k)*nx]
2230 - cpl_polynomial_eval_1d(trend, k, NULL)) < err) {
2232 dlisty[count] = sdata[j + (ibot + k)*nx];
2237 cpl_polynomial_delete(trend);
2239 trend = cpl_polynomial_fit_1d_create(listx, listy, 3, &err);
2241 cpl_bivector_delete(list);
2243 for (k = 0; k < npix; k++) {
2244 kdata[j + (ibot + k)*nx] = cpl_polynomial_eval_1d(trend,
2248 cpl_polynomial_delete(trend);
2251 cpl_polynomial_delete(polytop);
2252 cpl_polynomial_delete(polybot);
2255 cpl_image_subtract(science, sky);
2294 cpl_table *slits, cpl_table *polytraces,
2295 double reference,
double blue,
double red,
2296 double dispersion,
int sradius,
int polyorder)
2298 const char *func =
"mos_normalise_flat";
2300 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
2303 cpl_image *rectified;
2304 cpl_image *smo_flat;
2306 cpl_vector *positions;
2308 cpl_vector *smo_flux;
2309 cpl_polynomial *trend;
2310 cpl_polynomial *polytop;
2311 cpl_polynomial *polybot;
2321 double vtop, vbot, value;
2331 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
2332 int nx, ny, nsubx, nsuby;
2333 int xlow, ylow, xhig, yhig;
2337 int missing_top, missing_bot;
2350 if (flat == NULL || slits == NULL || polytraces == NULL) {
2351 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
2355 if (dispersion <= 0.0) {
2356 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
2360 if (red - blue < dispersion) {
2361 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
2366 blue, red, dispersion, 0, NULL);
2368 nx = cpl_image_get_size_x(rectified);
2369 ny = cpl_image_get_size_y(rectified);
2371 smo_flat = cpl_image_new(cpl_image_get_size_x(spatial),
2372 cpl_image_get_size_y(spatial), CPL_TYPE_FLOAT);
2373 wdata = cpl_image_get_data(smo_flat);
2375 nslits = cpl_table_get_nrow(slits);
2376 order = cpl_table_get_ncol(polytraces) - 2;
2377 position = cpl_table_get_data_int(slits,
"position");
2378 length = cpl_table_get_data_int(slits,
"length");
2379 slit_id = cpl_table_get_data_int(slits,
"slit_id");
2386 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
2387 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
2391 for (i = 0; i < nslits; i++) {
2406 ylow = position[i] + 1;
2407 yhig = ylow + length[i] - 1;
2409 exslit = cpl_image_extract(rectified, xlow, ylow, xhig, yhig);
2411 if (polyorder < 0) {
2413 cpl_image_turn(exslit, -1);
2415 nsubx = cpl_image_get_size_x(exslit);
2416 nsuby = cpl_image_get_size_y(exslit);
2417 data = cpl_image_get_data(exslit);
2418 flux = cpl_vector_new(nsubx);
2420 uradius = nsubx / 2;
2421 if (uradius > sradius)
2424 for (j = 0; j < nsuby; j++) {
2425 fdata = cpl_vector_get_data(flux);
2427 for (k = 0; k < nsubx; k++)
2429 smo_flux = cpl_vector_filter_median_create(flux, uradius);
2430 fdata = cpl_vector_get_data(smo_flux);
2432 for (k = 0; k < nsubx; k++)
2434 cpl_vector_delete(smo_flux);
2438 cpl_vector_delete(flux);
2474 cpl_image_turn(exslit, 1);
2475 nsubx = cpl_image_get_size_x(exslit);
2476 nsuby = cpl_image_get_size_y(exslit);
2477 data = cpl_image_get_data(exslit);
2479 for (j = 0; j < nsuby; j++) {
2480 flux = cpl_vector_new(nsubx);
2481 fdata = cpl_vector_get_data(flux);
2483 for (k = 0; k < nsubx; k++)
2485 smo_flux = cpl_vector_filter_median_create(flux, sradius);
2486 cpl_vector_delete(flux);
2487 fdata = cpl_vector_get_data(smo_flux);
2489 for (k = 0; k < nsubx; k++)
2491 cpl_vector_delete(smo_flux);
2501 nsubx = cpl_image_get_size_x(exslit);
2502 nsuby = cpl_image_get_size_y(exslit);
2503 data = cpl_image_get_data(exslit);
2505 for (j = 0; j < nsuby; j++) {
2513 for (k = 0; k < nsubx; k++)
2517 if (npoints > polyorder + 1) {
2523 flux = cpl_vector_new(npoints);
2524 fdata = cpl_vector_get_data(flux);
2525 positions = cpl_vector_new(npoints);
2526 pdata = cpl_vector_get_data(positions);
2530 for (k = 0; k < nsubx; k++) {
2532 fdata[npoints] = p[k];
2538 trend = cpl_polynomial_fit_1d_create(positions, flux,
2541 cpl_vector_delete(flux);
2542 cpl_vector_delete(positions);
2546 for (k = 0; k < nsubx; k++)
2548 p[k] = cpl_polynomial_eval_1d(trend, k, NULL);
2549 cpl_polynomial_delete(trend);
2552 cpl_msg_warning(func,
"Invalid flat field flux fit "
2565 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
2567 start_pixel = refpixel - pixel_below;
2568 if (start_pixel < 0)
2571 end_pixel = refpixel + pixel_above;
2576 polytop = cpl_polynomial_new(1);
2577 for (k = 0; k <= order; k++) {
2578 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
2580 cpl_polynomial_delete(polytop);
2584 cpl_polynomial_set_coeff(polytop, &k, coeff);
2588 polybot = cpl_polynomial_new(1);
2589 for (k = 0; k <= order; k++) {
2590 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
2592 cpl_polynomial_delete(polybot);
2596 cpl_polynomial_set_coeff(polybot, &k, coeff);
2599 if (missing_top && missing_bot) {
2600 cpl_msg_debug(func,
"Slit %d was not traced: no extraction!",
2612 cpl_msg_debug(func,
"Upper edge of slit %d was not traced: "
2613 "the spectral curvature of the lower edge "
2614 "is used instead.", slit_id[i]);
2615 polytop = cpl_polynomial_duplicate(polybot);
2616 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
2617 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
2619 coeff = cpl_polynomial_get_coeff(polybot, &k);
2620 coeff += ytop - ybot;
2621 cpl_polynomial_set_coeff(polytop, &k, coeff);
2625 cpl_msg_debug(func,
"Lower edge of slit %d was not traced: "
2626 "the spectral curvature of the upper edge "
2627 "is used instead.", slit_id[i]);
2628 polybot = cpl_polynomial_duplicate(polytop);
2629 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
2630 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
2632 coeff = cpl_polynomial_get_coeff(polytop, &k);
2633 coeff -= ytop - ybot;
2634 cpl_polynomial_set_coeff(polybot, &k, coeff);
2645 nx = cpl_image_get_size_x(flat);
2646 ny = cpl_image_get_size_y(flat);
2648 sdata = cpl_image_get_data(spatial);
2649 xdata = cpl_image_get_data(exslit);
2650 npseudo = cpl_image_get_size_y(exslit) - 1;
2656 for (j = start_pixel; j < end_pixel; j++) {
2657 top = cpl_polynomial_eval_1d(polytop, j, NULL);
2658 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
2659 for (k = 0; k <= npseudo; k++) {
2660 ypos = top - k*(top-bot)/npseudo;
2670 if (yint < 0 || yint >= ny-1) {
2675 value = sdata[j + nx*yint];
2677 fvalue = value - ivalue;
2678 if (ivalue < npseudo && ivalue >= 0) {
2679 vtop = xdata[j + nx*(npseudo-ivalue)];
2680 vbot = xdata[j + nx*(npseudo-ivalue-1)];
2681 wdata[j + nx*yint] = vtop*(1-fvalue) + vbot*fvalue;
2691 if (yprev - yint > 1) {
2692 value = sdata[j + nx*(yint+1)];
2694 fvalue = value - ivalue;
2695 if (ivalue < npseudo && ivalue >= 0) {
2696 vtop = xdata[j + nx*(npseudo-ivalue)];
2697 vbot = xdata[j + nx*(npseudo-ivalue-1)];
2698 wdata[j + nx*(yint+1)] = vtop*(1-fvalue)
2707 cpl_polynomial_delete(polytop);
2708 cpl_polynomial_delete(polybot);
2709 cpl_image_delete(exslit);
2712 cpl_image_delete(rectified);
2714 cpl_image_divide(flat, smo_flat);
2747 const char *func =
"mos_normalise_longflat";
2749 cpl_image *smo_flat;
2752 cpl_vector *smo_flux;
2753 cpl_vector *positions;
2754 cpl_polynomial *trend;
2768 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
2772 if (sradius < 1 || dradius < 1) {
2773 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
2777 smo_flat = cpl_image_duplicate(flat);
2779 if (polyorder < 0) {
2785 cpl_image_turn(smo_flat, -1);
2787 nx = cpl_image_get_size_x(smo_flat);
2788 ny = cpl_image_get_size_y(smo_flat);
2789 data = cpl_image_get_data(smo_flat);
2791 for (i = 0; i < ny; i++) {
2792 flux = cpl_vector_new(nx);
2793 fdata = cpl_vector_get_data(flux);
2795 for (j = 0; j < nx; j++)
2797 smo_flux = cpl_vector_filter_median_create(flux, sradius);
2798 cpl_vector_delete(flux);
2799 fdata = cpl_vector_get_data(smo_flux);
2801 for (j = 0; j < nx; j++)
2803 cpl_vector_delete(smo_flux);
2811 cpl_image_turn(smo_flat, 1);
2813 nx = cpl_image_get_size_x(smo_flat);
2814 ny = cpl_image_get_size_y(smo_flat);
2815 data = cpl_image_get_data(smo_flat);
2817 for (i = 0; i < ny; i++) {
2818 flux = cpl_vector_new(nx);
2819 fdata = cpl_vector_get_data(flux);
2821 for (j = 0; j < nx; j++)
2823 smo_flux = cpl_vector_filter_median_create(flux, sradius);
2824 cpl_vector_delete(flux);
2825 fdata = cpl_vector_get_data(smo_flux);
2827 for (j = 0; j < nx; j++)
2829 cpl_vector_delete(smo_flux);
2839 cpl_image_turn(smo_flat, -1);
2841 nx = cpl_image_get_size_x(smo_flat);
2842 ny = cpl_image_get_size_y(smo_flat);
2843 data = cpl_image_get_data(smo_flat);
2845 profile = cpl_image_collapse_median_create(smo_flat, 1, 0, 0);
2846 level = cpl_image_get_data(profile);
2848 for (i = 0; i < ny; i++) {
2858 for (j = 0; j < nx; j++)
2859 if (fabs(p[j]/level[i] - 1) < 0.20)
2862 if (npoints > polyorder + 1) {
2868 flux = cpl_vector_new(npoints);
2869 fdata = cpl_vector_get_data(flux);
2870 positions = cpl_vector_new(npoints);
2871 pdata = cpl_vector_get_data(positions);
2875 for (j = 0; j < nx; j++) {
2876 if (fabs(p[j]/level[i] - 1) < 0.20) {
2877 fdata[npoints] = p[j];
2883 trend = cpl_polynomial_fit_1d_create(positions, flux,
2886 cpl_vector_delete(flux);
2887 cpl_vector_delete(positions);
2891 for (j = 0; j < nx; j++)
2892 p[j] = cpl_polynomial_eval_1d(trend, j, NULL);
2893 cpl_polynomial_delete(trend);
2896 cpl_msg_warning(func,
2897 "Invalid flat field flux fit (ignored)");
2902 cpl_image_delete(profile);
2903 cpl_image_turn(smo_flat, 1);
2907 cpl_image_divide(flat, smo_flat);
2937 int order,
int global)
2939 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
2941 int nrow = cpl_table_get_nrow(slits);
2946 return CPL_ERROR_NONE;
2948 cpl_table_new_column(idscoeff,
"x", CPL_TYPE_DOUBLE);
2949 cpl_table_new_column(idscoeff,
"y", CPL_TYPE_DOUBLE);
2951 for (i = 0; i < nrow; i++) {
2952 int position = cpl_table_get_int (slits,
"position", i, NULL);
2953 int length = cpl_table_get_int (slits,
"length", i, NULL);
2954 double xtop = cpl_table_get_double(slits,
"xtop", i, NULL);
2955 double xbot = cpl_table_get_double(slits,
"xbottom", i, NULL);
2956 double ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
2957 double ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
2958 double dx = xtop - xbot;
2959 double dy = ytop - ybot;
2960 cpl_table *table = cpl_table_extract(idscoeff, position, length);
2965 cpl_table_erase_window(idscoeff, position, length);
2966 cpl_table_insert(idscoeff, table, position);
2968 cpl_table_delete(table);
2970 for (j = 0; j < length; j++) {
2971 cpl_table_set_double(idscoeff,
"x", j + position,
2972 xbot + j*(dx/length));
2973 cpl_table_set_double(idscoeff,
"y", j + position,
2974 ybot + j*(dy/length));
2984 nrow = cpl_table_get_nrow(idscoeff);
2986 for (i = 0; i < 6; i++) {
2997 if (!cpl_table_has_column(idscoeff, clab[i]))
3000 npoints = nrow - cpl_table_count_invalid(idscoeff, clab[i]);
3004 dummy = cpl_table_new(nrow);
3005 cpl_table_duplicate_column(dummy,
"x", idscoeff,
"x");
3006 cpl_table_duplicate_column(dummy,
"y", idscoeff,
"y");
3007 cpl_table_duplicate_column(dummy, clab[i], idscoeff, clab[i]);
3008 cpl_table_erase_invalid(dummy);
3010 x = cpl_vector_wrap(npoints, cpl_table_get_data_double(dummy,
"x"));
3011 y = cpl_vector_wrap(npoints, cpl_table_get_data_double(dummy,
"y"));
3012 z = cpl_bivector_wrap_vectors(x, y);
3013 c = cpl_vector_wrap(npoints, cpl_table_get_data_double(dummy,
3015 p = cpl_polynomial_fit_2d_create(z, c, 2, NULL);
3016 cpl_bivector_unwrap_vectors(z);
3017 cpl_vector_unwrap(x);
3018 cpl_vector_unwrap(y);
3019 cpl_vector_unwrap(c);
3020 cpl_table_delete(dummy);
3022 point = cpl_vector_new(2);
3023 dpoint = cpl_vector_get_data(point);
3024 for (j = 0; j < nrow; j++) {
3025 dpoint[0] = cpl_table_get_double(idscoeff,
"x", j, NULL);
3026 dpoint[1] = cpl_table_get_double(idscoeff,
"y", j, NULL);
3027 cpl_table_set_double(idscoeff, clab[i], j,
3028 cpl_polynomial_eval(p, point));
3030 cpl_vector_delete(point);
3031 cpl_polynomial_delete(p);
3035 return CPL_ERROR_NONE;
3065 cpl_image *wavemap,
int mode,
3068 const char *func =
"mos_interpolate_wavecalib";
3070 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
3074 cpl_vector *positions;
3075 cpl_polynomial *trend;
3086 int nrows, first_row, last_row;
3088 int npoints, rpoints;
3095 if (idscoeff == NULL)
3096 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
3098 if (mode < 0 || mode > 2)
3099 return cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
3101 if (mode == 0 || degree < 0)
3102 return CPL_ERROR_NONE;
3110 cpl_image_turn(wavemap, -1);
3112 nx = cpl_image_get_size_x(wavemap);
3113 ny = cpl_image_get_size_y(wavemap);
3114 data = cpl_image_get_data(wavemap);
3116 for (i = 0; i < ny; i++) {
3125 for (j = 0; j < nx; j++)
3129 if (npoints > polyorder + 1) {
3135 wave = cpl_vector_new(npoints);
3136 wdata = cpl_vector_get_data(wave);
3137 positions = cpl_vector_new(npoints);
3138 pdata = cpl_vector_get_data(positions);
3142 for (j = 0; j < nx; j++) {
3144 wdata[npoints] = p[j];
3150 trend = cpl_polynomial_fit_1d_create(positions, wave,
3153 ksigma = 3*sqrt(mse);
3155 cpl_vector_delete(wave);
3156 cpl_vector_delete(positions);
3166 for (j = 0; j < nx; j++)
3168 if (fabs(cpl_polynomial_eval_1d(trend, j, NULL)
3172 if (rpoints < npoints && rpoints > polyorder + 1) {
3174 wave = cpl_vector_new(rpoints);
3175 wdata = cpl_vector_get_data(wave);
3176 positions = cpl_vector_new(rpoints);
3177 pdata = cpl_vector_get_data(positions);
3181 for (j = 0; j < nx; j++) {
3183 if (fabs(cpl_polynomial_eval_1d(trend,
3186 wdata[npoints] = p[j];
3193 cpl_polynomial_delete(trend);
3194 trend = cpl_polynomial_fit_1d_create(positions, wave,
3197 cpl_vector_delete(wave);
3198 cpl_vector_delete(positions);
3205 for (j = 0; j < nx; j++)
3207 p[j] = cpl_polynomial_eval_1d(trend, j, NULL);
3209 else if (mode == 2) {
3210 for (j = 0; j < nx; j++)
3211 p[j] = cpl_polynomial_eval_1d(trend, j, NULL);
3213 cpl_polynomial_delete(trend);
3216 cpl_msg_warning(func,
3217 "Invalid wavelength field fit (ignored)");
3223 cpl_image_turn(wavemap, 1);
3232 nrows = cpl_table_get_nrow(idscoeff);
3235 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
3240 for (k = 0; k <= order; k++) {
3242 if (cpl_table_has_column(idscoeff, clab[k])) {
3243 m = cpl_table_get_column_median(idscoeff, clab[k]);
3244 cpl_table_fill_column_window_double(idscoeff, clab[k],
3249 return CPL_ERROR_NONE;
3253 while (!cpl_table_is_valid(idscoeff, clab[0], first_row))
3256 last_row = nrows - 1;
3257 while (!cpl_table_is_valid(idscoeff, clab[0], last_row))
3260 for (k = 0; k <= order; k++) {
3262 npoints = nrows - cpl_table_count_invalid(idscoeff, clab[k]);
3263 wave = cpl_vector_new(npoints);
3264 wdata = cpl_vector_get_data(wave);
3265 positions = cpl_vector_new(npoints);
3266 pdata = cpl_vector_get_data(positions);
3269 for (i = first_row; i <= last_row; i++) {
3270 c = cpl_table_get_double(idscoeff, clab[k], i, &null);
3288 p = cpl_vector_extract(positions, 2, npoints - 2, 1);
3289 w = cpl_vector_extract(wave, 2, npoints - 2, 1);
3296 list = cpl_bivector_wrap_vectors(p, w);
3298 robustLinearFit(list, &q, &m, &mse);
3299 cpl_bivector_unwrap_vectors(list);
3300 for (i = first_row; i <= last_row; i++)
3301 cpl_table_set_double(idscoeff, clab[k], i, q + m*i);
3304 cpl_vector_delete(p);
3305 cpl_vector_delete(w);
3308 cpl_vector_delete(wave);
3309 cpl_vector_delete(positions);
3316 trend = cpl_polynomial_fit_1d_create(positions, wave, degree, &mse);
3318 ksigma = 3*sqrt(mse);
3320 cpl_vector_delete(wave);
3321 cpl_vector_delete(positions);
3329 for (i = first_row; i <= last_row; i++) {
3330 c = cpl_table_get_double(idscoeff, clab[k], i, &null);
3332 if (fabs(cpl_polynomial_eval_1d(trend, i, NULL) - c)
3339 if (rpoints > 0 && rpoints < npoints) {
3340 cpl_msg_debug(func,
"%d points rejected from "
3341 "wavelength calibration fit",
3344 wave = cpl_vector_new(rpoints);
3345 wdata = cpl_vector_get_data(wave);
3346 positions = cpl_vector_new(rpoints);
3347 pdata = cpl_vector_get_data(positions);
3350 for (i = first_row; i <= last_row; i++) {
3351 c = cpl_table_get_double(idscoeff, clab[k], i, &null);
3353 if (fabs(cpl_polynomial_eval_1d(trend, i, NULL) - c)
3363 cpl_polynomial_delete(trend);
3364 trend = cpl_polynomial_fit_1d_create(positions,
3365 wave, degree, NULL);
3368 cpl_vector_delete(wave);
3369 cpl_vector_delete(positions);
3375 for (i = first_row; i <= last_row; i++) {
3377 if (!cpl_table_is_valid(idscoeff, clab[k], i)) {
3378 cpl_table_set_double(idscoeff, clab[k], i,
3379 cpl_polynomial_eval_1d(trend, i,
3383 else if (mode == 2) {
3384 cpl_table_set_double(idscoeff, clab[k], i,
3385 cpl_polynomial_eval_1d(trend, i, NULL));
3388 cpl_polynomial_delete(trend);
3391 cpl_msg_warning(func,
"Invalid IDS coefficient fit (ignored)");
3396 return CPL_ERROR_NONE;
3420 const char *func =
"mos_interpolate_wavecalib";
3422 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
3426 cpl_vector *positions;
3427 cpl_polynomial *trend;
3436 int nrows, first_row, last_row;
3437 int npoints, rpoints;
3442 if (idscoeff == NULL)
3443 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
3445 if (mode < 0 || mode > 2)
3446 return cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
3448 if (mode == 0 || degree < 0)
3449 return CPL_ERROR_NONE;
3455 nrows = cpl_table_get_nrow(idscoeff);
3458 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
3467 for (
int k = kinit; k <= order; k++) {
3469 if (cpl_table_has_column(idscoeff, clab[k])) {
3470 m = cpl_table_get_column_median(idscoeff, clab[k]);
3471 cpl_table_fill_column_window_double(idscoeff, clab[k],
3479 while (!cpl_table_is_valid(idscoeff, clab[0], first_row))
3482 last_row = nrows - 1;
3483 while (!cpl_table_is_valid(idscoeff, clab[0], last_row))
3488 npoints = nrows - cpl_table_count_invalid(idscoeff, clab[korder]);
3489 wave = cpl_vector_new(npoints);
3490 wdata = cpl_vector_get_data(wave);
3491 positions = cpl_vector_new(npoints);
3492 pdata = cpl_vector_get_data(positions);
3495 for (i = first_row; i <= last_row; i++) {
3496 c = cpl_table_get_double(idscoeff, clab[korder], i, &null);
3514 p = cpl_vector_extract(positions, 2, npoints - 2, 1);
3515 w = cpl_vector_extract(wave, 2, npoints - 2, 1);
3522 list = cpl_bivector_wrap_vectors(p, w);
3524 robustLinearFit(list, &q, &m, &mse);
3525 cpl_bivector_unwrap_vectors(list);
3526 for (i = first_row; i <= last_row; i++)
3527 cpl_table_set_double(idscoeff, clab[korder], i, q + m*i);
3530 cpl_vector_delete(p);
3531 cpl_vector_delete(w);
3534 cpl_vector_delete(wave);
3535 cpl_vector_delete(positions);
3543 trend = cpl_polynomial_fit_1d_create(positions, wave, degree, &mse);
3545 ksigma = 3*sqrt(mse);
3547 cpl_vector_delete(wave);
3548 cpl_vector_delete(positions);
3556 for (i = first_row; i <= last_row; i++) {
3557 c = cpl_table_get_double(idscoeff, clab[korder], i, &null);
3559 if (fabs(cpl_polynomial_eval_1d(trend, i, NULL) - c)
3566 if (rpoints > 0 && rpoints < npoints) {
3567 cpl_msg_debug(func,
"%d points rejected from "
3568 "wavelength calibration fit",
3571 wave = cpl_vector_new(rpoints);
3572 wdata = cpl_vector_get_data(wave);
3573 positions = cpl_vector_new(rpoints);
3574 pdata = cpl_vector_get_data(positions);
3577 for (i = first_row; i <= last_row; i++) {
3578 c = cpl_table_get_double(idscoeff, clab[korder], i, &null);
3580 if (fabs(cpl_polynomial_eval_1d(trend, i, NULL) - c)
3590 cpl_polynomial_delete(trend);
3591 trend = cpl_polynomial_fit_1d_create(positions,
3592 wave, degree, NULL);
3595 cpl_vector_delete(wave);
3596 cpl_vector_delete(positions);
3602 for (i = first_row; i <= last_row; i++) {
3604 if (!cpl_table_is_valid(idscoeff, clab[korder], i)) {
3605 cpl_table_set_double(idscoeff, clab[korder], i,
3606 cpl_polynomial_eval_1d(trend, i,
3610 else if (mode == 2) {
3611 cpl_table_set_double(idscoeff, clab[korder], i,
3612 cpl_polynomial_eval_1d(trend, i, NULL));
3615 cpl_polynomial_delete(trend);
3618 cpl_msg_warning(func,
"Invalid IDS coefficient fit (ignored)");
3623 return CPL_ERROR_NONE;
3655 cpl_table *overscans)
3657 const char *func =
"mos_remove_bias";
3659 cpl_image *unbiased;
3660 cpl_image *overscan;
3661 double mean_bias_level;
3662 double mean_overscans_level;
3665 int xlow, ylow, xhig, yhig;
3669 if (image == NULL || overscans == NULL) {
3670 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
3674 nrows = cpl_table_get_nrow(overscans);
3677 cpl_msg_error(func,
"Empty overscan table");
3678 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
3684 unbiased = cpl_image_subtract_create(image, bias);
3685 if (unbiased == NULL) {
3686 cpl_msg_error(func,
"Incompatible master bias");
3687 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
3691 mean_bias_level = cpl_image_get_mean(bias);
3695 cpl_msg_error(func,
"No master bias in input, and no overscan "
3696 "regions in input image: bias subtraction "
3697 "cannot be performed!");
3698 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
3701 mean_bias_level = 0.0;
3704 mean_overscans_level = 0.0;
3706 for (i = 0; i < nrows; i++) {
3707 xlow = cpl_table_get_int(overscans,
"xlow", i, NULL);
3708 ylow = cpl_table_get_int(overscans,
"ylow", i, NULL);
3709 xhig = cpl_table_get_int(overscans,
"xhig", i, NULL);
3710 yhig = cpl_table_get_int(overscans,
"yhig", i, NULL);
3713 unbiased = cpl_image_extract(image, xlow+1, ylow+1, xhig, yhig);
3714 if (unbiased == NULL) {
3715 cpl_msg_error(func,
"Incompatible overscan table");
3716 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
3720 if (cpl_image_subtract(unbiased, bias)) {
3721 cpl_msg_error(func,
"Incompatible master bias");
3722 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
3723 cpl_image_delete(unbiased);
3729 overscan = cpl_image_extract(image, xlow+1, ylow+1, xhig, yhig);
3730 if (overscan == NULL) {
3731 cpl_msg_error(func,
"Incompatible overscan table");
3732 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
3733 cpl_image_delete(unbiased);
3737 mean_overscans_level += cpl_image_get_median(overscan);
3747 cpl_image_delete(overscan);
3755 mean_overscans_level /= count;
3757 cpl_image_subtract_scalar(unbiased, mean_overscans_level - mean_bias_level);
3759 cpl_msg_info(cpl_func,
3760 "Difference between mean overscans level "
3761 "and mean bias level: %.2f",
3762 mean_overscans_level - mean_bias_level);
3828 int length,
int msize,
int fsize)
3830 const char *func =
"mos_arc_background_1D";
3838 if (spectrum == NULL || back == NULL)
3839 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
3847 if (msize < 3 || fsize < msize || length < 2*fsize)
3848 return cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
3851 minf = min_filter(spectrum, length, msize);
3852 smof = smo_filter(minf, length, fsize);
3854 maxf = max_filter(smof, length, 2*msize+1);
3856 smof = smo_filter(maxf, length, 2*fsize+1);
3858 minf = min_filter(smof, length, 2*msize+1);
3860 smof = smo_filter(minf, length, 2*fsize+1);
3863 for (i = 0; i < length; i++)
3868 return CPL_ERROR_NONE;
3931 const char *func =
"mos_arc_background";
3943 if (image == NULL) {
3944 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
3954 nx = cpl_image_get_size_x(image);
3955 ny = cpl_image_get_size_y(image);
3957 bimage = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
3961 data = cpl_image_get_data_float(fimage);
3962 bdata = cpl_image_get_data_float(bimage);
3964 for (i = 0; i < ny; i++) {
3965 row = data + i * nx;
3966 brow = bdata + i * nx;
3968 cpl_error_set_where(func);
3969 cpl_image_delete(fimage);
3970 cpl_image_delete(bimage);
3975 cpl_image_delete(fimage);
4004 const char *func =
"mos_lines_width";
4006 double *profile1 = cpl_calloc(length - 1,
sizeof(
double));
4007 double *profile2 = cpl_calloc(length - 1,
sizeof(
double));
4009 double norm, value, max;
4011 int short_length = length - 2*radius - 1;
4020 for (j = 0, i = 1; i < length; j++, i++) {
4021 profile1[j] = profile2[j] = spectrum[i] - spectrum[j];
4022 if (profile1[j] < 0)
4024 if (profile2[j] > 0)
4027 profile2[j] = -profile2[j];
4038 for (i = 0; i < length; i++)
4039 if (norm < profile1[i])
4042 for (i = 0; i < length; i++) {
4043 profile1[i] /= norm;
4044 profile2[i] /= norm;
4053 for (i = 0; i <= radius; i++) {
4055 for (j = 0; j < short_length; j++) {
4057 value += profile1[k] * profile2[k+i];
4069 cpl_msg_debug(func,
"Cannot estimate line width");
4105 int length,
float level,
4109 const char *func =
"mos_peak_candidates";
4112 int nint = length - 1;
4114 int width = 2 * ceil(exp_width / 2) + 1;
4115 int start = width / 2;
4116 int end = length - width / 2;
4119 double *data = cpl_calloc(length/2,
sizeof(
double));
4122 if (spectrum == NULL) {
4123 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
4134 smo = cpl_calloc(length,
sizeof(
float));
4136 end = length - width / 2;
4137 for (i = 0; i < start; i++)
4138 smo[i] = spectrum[i];
4139 for (i = start; i < end; i++) {
4140 for (j = i - start; j <= i + start; j++)
4141 smo[i] += spectrum[j];
4144 for (i = end; i < length; i++)
4145 smo[i] = spectrum[i];
4148 smo = (
float *)spectrum;
4161 for (i = step; i < nint - step + 1; i += step) {
4162 if (smo[i] > level) {
4163 if (smo[i] >= smo[i-step] && smo[i] > smo[i+step]) {
4164 if (smo[i-step] != 0.0 && smo[i+step] != 0.0) {
4165 data[n] = i + step * values_to_dx(smo[i-step], smo[i], smo[i+step]);
4181 return cpl_vector_wrap(n, data);
4208 cpl_vector *peaks,
int sradius)
4211 const char *func =
"mos_refine_peaks";
4216 int startPos, endPos;
4217 int window = 2*sradius+1;
4221 if (peaks == NULL || spectrum == NULL) {
4222 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
4226 npeaks = cpl_vector_get_size(peaks);
4227 data = cpl_vector_unwrap(peaks);
4229 for (i = 0; i < npeaks; i++) {
4230 startPos = data[i] - window/2;
4231 endPos = startPos + window;
4232 if (startPos < 0 || endPos >= length)
4235 if (0 == peakPosition(spectrum + startPos, window, &pos, 1)) {
4241 for (i = 1; i < npeaks; i++)
4242 if (data[i] - data[i-1] < 0.5)
4245 for (i = 0, j = 0; i < npeaks; i++) {
4246 if (data[i] > 0.0) {
4253 return cpl_vector_wrap(j, data);
4258 void mos_set_multiplex(
int multiplex)
4260 mos_multiplex = multiplex;
4317 double min_disp,
double max_disp,
4325 double lratio, pratio;
4326 double lo_start, lo_end, hi_start, hi_end, denom;
4327 double disp, variation, prev_variation;
4328 int max, maxpos, minl, mink;
4330 int npeaks_lo, npeaks_hi;
4355 peak = cpl_vector_get_data(peaks);
4356 npeaks = cpl_vector_get_size(peaks);
4357 line = cpl_vector_get_data(lines);
4358 nlines = cpl_vector_get_size(lines);
4363 peak_lo = cpl_malloc(npeaks *
sizeof(
int));
4364 peak_hi = cpl_malloc(npeaks *
sizeof(
int));
4365 nident = cpl_calloc(npeaks,
sizeof(
int));
4366 lident = cpl_calloc(nlines,
sizeof(
int));
4367 xpos = cpl_calloc(npeaks,
sizeof(
double));
4368 lambda = cpl_calloc(npeaks,
sizeof(
double));
4369 ilambda = cpl_calloc(npeaks,
sizeof(
int));
4370 tmp_xpos = cpl_calloc(npeaks,
sizeof(
double));
4371 tmp_lambda = cpl_calloc(npeaks,
sizeof(
double));
4372 tmp_ilambda = cpl_calloc(npeaks,
sizeof(
int));
4373 flag = cpl_calloc(npeaks,
sizeof(
int));
4374 seq_length = cpl_calloc(npeaks,
sizeof(
int));
4375 ident = cpl_malloc(npeaks *
sizeof(
int *));
4376 for (i = 0; i < npeaks; i++)
4377 ident[i] = cpl_malloc(3 * npeaks *
sizeof(
int));
4392 for (i = 1; i < nlint; i++) {
4402 lratio = (line[i+1] - line[i]) / (line[i] - line[i-1]);
4409 for (j = 1; j < npint; j++) {
4422 lo_start = peak[j] - (line[i] - line[i-1]) / min_disp;
4423 lo_end = peak[j] - (line[i] - line[i-1]) / max_disp;
4424 hi_start = peak[j] + (line[i+1] - line[i]) / max_disp;
4425 hi_end = peak[j] + (line[i+1] - line[i]) / min_disp;
4427 for (npeaks_lo = 0, k = 0; k < npeaks; k++) {
4428 if (peak[k] > lo_end)
4430 if (peak[k] > lo_start) {
4431 peak_lo[npeaks_lo] = k;
4439 for (npeaks_hi = 0, k = 0; k < npeaks; k++) {
4440 if (peak[k] > hi_end)
4442 if (peak[k] > hi_start) {
4443 peak_hi[npeaks_hi] = k;
4460 prev_variation = 1000.0;
4463 for (k = 0; k < npeaks_lo; k++) {
4464 denom = peak[j] - peak[peak_lo[k]];
4465 for (l = 0; l < npeaks_hi; l++) {
4473 pratio = (peak[peak_hi[l]] - peak[j]) / denom;
4486 variation = fabs(lratio-pratio) / pratio;
4488 if (variation < tolerance) {
4489 if (variation < prev_variation) {
4490 prev_variation = variation;
4497 if (prev_variation < tolerance) {
4498 ident[j][nident[j]] = i;
4499 ident[peak_hi[minl]][nident[peak_hi[minl]]] = i + 1;
4500 ident[peak_lo[mink]][nident[peak_lo[mink]]] = i - 1;
4502 ++nident[peak_hi[minl]];
4503 ++nident[peak_lo[mink]];
4515 for (i = 0; i < npeaks; i++) {
4524 if (nident[i] > 1) {
4531 for (j = 0; j < nlines; j++)
4540 for (j = 0; j < nident[i]; j++)
4541 ++lident[ident[i][j]];
4550 for (j = 0; j < nlines; j++) {
4551 if (max < lident[j]) {
4566 for (k = maxpos + 1; k < nlines; k++) {
4567 if (lident[k] == max) {
4582 tmp_xpos[n] = peak[i];
4583 tmp_lambda[n] = line[maxpos];
4584 tmp_ilambda[n] = maxpos;
4608 for (k = 0; k < n; k++) {
4611 xpos[nn] = tmp_xpos[k];
4612 lambda[nn] = tmp_lambda[k];
4613 ilambda[nn] = tmp_ilambda[k];
4626 for (j = i + 1; j < n; j++) {
4628 disp = (tmp_lambda[j] - tmp_lambda[i])
4629 / (tmp_xpos[j] - tmp_xpos[i]);
4630 if (disp >= min_disp && disp <= max_disp) {
4632 xpos[nn] = tmp_xpos[j];
4633 lambda[nn] = tmp_lambda[j];
4634 ilambda[nn] = tmp_ilambda[j];
4664 if (mos_multiplex < 0) {
4665 for (i = 0; i < nseq; i++) {
4666 if (seq_length[i] > max) {
4667 max = seq_length[i];
4683 for (i = 0; i < nseq; i++) {
4686 cpl_array *regions = cpl_array_new(n, CPL_TYPE_INT);
4689 for (j = 0; j < n; j++)
4690 cpl_array_set_int(regions, j,
4691 ((
int)floor(xpos[nn + j])) / mos_region_size);
4693 region = (int)cpl_array_get_median(regions);
4694 cpl_array_delete(regions);
4696 if (mos_multiplex == region) {
4698 cpl_msg_debug(cpl_func,
"More than one spectrum found in "
4699 "region %d (only the first one is extracted)",
4704 max = seq_length[i];
4708 nn += seq_length[i];
4718 for (i = 0; i < maxpos; i++)
4719 nn += seq_length[i];
4726 for (i = 0; i < n; i++, nn++) {
4728 lambda[i] = lambda[nn];
4729 ilambda[i] = ilambda[nn];
4737 for (i = 1; i < n; i++) {
4738 gap = ilambda[i] - ilambda[i-1];
4739 for (j = 1; j < gap; j++) {
4747 disp = (lambda[i] - lambda[i-1]) / (xpos[i] - xpos[i-1]);
4755 hi_start = xpos[i-1] + (line[ilambda[i-1] + j] - lambda[i-1]) / disp;
4770 for (k = 0; k < npeaks; k++) {
4771 if (fabs(peak[k] - hi_start) < 2) {
4772 for (l = n; l > i; l--) {
4773 xpos[l] = xpos[l-1];
4774 lambda[l] = lambda[l-1];
4775 ilambda[l] = ilambda[l-1];
4778 lambda[i] = line[ilambda[i-1] + j];
4779 ilambda[i] = ilambda[i-1] + j;
4796 while (ilambda[n-1] < nlines - 1 && found) {
4804 disp = (lambda[n-1] - lambda[n-2]) / (xpos[n-1] - xpos[n-2]);
4808 if (disp > max_disp || disp < min_disp)
4817 hi_start = xpos[n-1] + (line[ilambda[n-1] + 1] - lambda[n-1]) / disp;
4828 min = fabs(peak[0] - hi_start);
4830 for (k = 1; k < npeaks; k++) {
4831 if (min > fabs(peak[k] - hi_start)) {
4832 min = fabs(peak[k] - hi_start);
4836 if (min < 6 && fabs(peak[minpos] - xpos[n-1]) > 1.0) {
4837 xpos[n] = peak[minpos];
4838 lambda[n] = line[ilambda[n-1] + 1];
4839 ilambda[n] = ilambda[n-1] + 1;
4851 while (ilambda[0] > 0 && found) {
4858 disp = (lambda[1] - lambda[0]) / (xpos[1] - xpos[0]);
4860 if (disp > max_disp || disp < min_disp)
4869 hi_start = xpos[0] - (lambda[0] - line[ilambda[0] - 1]) / disp;
4881 min = fabs(peak[0] - hi_start);
4883 for (k = 1; k < npeaks; k++) {
4884 if (min > fabs(peak[k] - hi_start)) {
4885 min = fabs(peak[k] - hi_start);
4889 if (min < 6 && fabs(peak[minpos] - xpos[0]) > 1.0) {
4890 for (j = n; j > 0; j--) {
4891 xpos[j] = xpos[j-1];
4892 lambda[j] = lambda[j-1];
4893 ilambda[j] = ilambda[j-1];
4895 xpos[0] = peak[minpos];
4896 lambda[0] = line[ilambda[0] - 1];
4897 ilambda[0] = ilambda[0] - 1;
4923 for (i = 0; i < npeaks; i++)
4930 cpl_free(tmp_lambda);
4931 cpl_free(tmp_ilambda);
4934 cpl_free(seq_length);
4943 return cpl_bivector_wrap_vectors(cpl_vector_wrap(n, xpos),
4944 cpl_vector_wrap(n, lambda));
5006 double refwave,
double pixel)
5012 if (cpl_polynomial_eval_1d(ids, blue-refwave, NULL) > pixel)
5015 if (cpl_polynomial_eval_1d(ids, red-refwave, NULL) < pixel)
5018 yellow = (blue + red) / 2 - refwave;
5020 coeff = cpl_polynomial_get_coeff(ids, &zero);
5021 cpl_polynomial_set_coeff(ids, &zero, coeff - pixel);
5023 cpl_polynomial_solve_1d(ids, yellow, &yellow, 1);
5024 if (cpl_error_get_code() != CPL_ERROR_NONE) {
5029 cpl_polynomial_set_coeff(ids, &zero, coeff);
5031 return yellow + refwave;
5061 double reject,
int minlines,
5062 int *nlines,
double *err)
5064 const char *func =
"mos_poly_wav2pix";
5066 cpl_bivector *pixwav2;
5076 cpl_polynomial *ids;
5082 if (pixwav == NULL) {
5083 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
5087 fitlines = cpl_bivector_get_size(pixwav);
5089 if (fitlines < minlines) {
5090 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
5104 pixwav2 = cpl_bivector_duplicate(pixwav);
5114 pixel = cpl_bivector_get_x(pixwav2);
5115 wavel = cpl_bivector_get_y(pixwav2);
5123 cpl_bivector_unwrap_vectors(pixwav2);
5130 while (fitlines >= minlines) {
5132 ids = cpl_polynomial_fit_1d_create(wavel, pixel, order, err);
5136 cpl_msg_debug(cpl_error_get_where(),
"%s", cpl_error_get_message());
5137 cpl_msg_debug(func,
"Fitting IDS");
5138 cpl_error_set_where(func);
5140 cpl_vector_delete(wavel);
5141 cpl_vector_delete(pixel);
5153 d_pixel = cpl_vector_unwrap(pixel);
5154 d_wavel = cpl_vector_unwrap(wavel);
5156 for (i = 0, j = 0; i < fitlines; i++) {
5157 pixpos = cpl_polynomial_eval_1d(ids, d_wavel[i], NULL);
5158 if (fabs(pixpos - d_pixel[i]) < reject) {
5159 d_pixel[j] = d_pixel[i];
5160 d_wavel[j] = d_wavel[i];
5165 if (j == fitlines) {
5173 cpl_polynomial_delete(ids);
5174 if (fitlines >= minlines) {
5175 pixel = cpl_vector_wrap(fitlines, d_pixel);
5176 wavel = cpl_vector_wrap(fitlines, d_wavel);
5181 cpl_error_set(func, CPL_ERROR_CONTINUE);
5221 double reject,
int minlines,
5222 int *nlines,
double *err)
5225 cpl_bivector *wavpix;
5229 cpl_polynomial *dds;
5236 pixel = cpl_bivector_get_x(pixwav);
5237 wavel = cpl_bivector_get_y(pixwav);
5239 wavpix = cpl_bivector_wrap_vectors(wavel, pixel);
5243 cpl_bivector_unwrap_vectors(wavpix);
5273 cpl_vector *lines, cpl_polynomial *ids,
5274 double refwave,
int sradius)
5276 const char *func =
"mos_find_peaks";
5287 if (spectrum == NULL || lines == NULL || ids == NULL) {
5288 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
5292 nlines = cpl_vector_get_size(lines);
5294 if (sradius < 1 || length < 2*sradius+1 || nlines < 1) {
5295 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
5299 d_wavel = cpl_malloc(nlines *
sizeof(
double));
5300 d_pixel = cpl_malloc(nlines *
sizeof(
double));
5302 data = cpl_vector_get_data(lines);
5304 for (i = 0, j = 0; i < nlines; i++) {
5305 pixel = cpl_polynomial_eval_1d(ids, data[i]-refwave, NULL) + 0.5;
5306 if (pixel < 0 || pixel - sradius < 0 || pixel + sradius >= length)
5308 if (peakPosition(spectrum+pixel-sradius, 2*sradius+1, &pos, 1) == 0) {
5309 pos += pixel - sradius;
5311 d_wavel[j] = data[i];
5317 return cpl_bivector_wrap_vectors(cpl_vector_wrap(j, d_pixel),
5318 cpl_vector_wrap(j, d_wavel));
5323 cpl_error_set(func, CPL_ERROR_ILLEGAL_OUTPUT);
5454 double dispersion,
float level,
5455 int sradius,
int order,
5456 double reject,
double refwave,
5457 double *wavestart,
double *waveend,
5458 int *nlines,
double *error,
5459 cpl_table *idscoeff,
5460 cpl_image *calibration,
5461 cpl_image *residuals,
5462 cpl_table *restable,
5464 cpl_table *detected_lines)
5467 const char *func =
"mos_wavelength_calibration_raw";
5469 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
5472 double tolerance = 20.0;
5475 char name[MAX_COLNAME];
5476 cpl_image *resampled;
5477 cpl_bivector *output;
5478 cpl_bivector *new_output;
5481 cpl_polynomial *ids;
5482 cpl_polynomial *lin;
5485 double max_disp, min_disp;
5487 double firstLambda, lastLambda, lambda;
5488 double value, wave, pixe;
5497 int pixstart, pixend;
5500 int nl, nx, ny, pixel;
5501 int countLines, usedLines;
5503 int in, first, last;
5510 if (dispersion == 0.0) {
5511 cpl_msg_error(func,
"The expected dispersion (A/pixel) must be given");
5512 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
5516 if (dispersion < 0.0) {
5517 cpl_msg_error(func,
"The expected dispersion must be positive");
5518 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
5522 max_disp = dispersion + dispersion * tolerance / 100;
5523 min_disp = dispersion - dispersion * tolerance / 100;
5526 cpl_msg_error(func,
"The order of the fitting polynomial "
5527 "must be at least 1");
5528 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
5532 if (image == NULL || lines == NULL) {
5533 cpl_msg_error(func,
"Both spectral exposure and reference line "
5534 "catalog are required in input");
5535 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
5539 nx = cpl_image_get_size_x(image);
5540 ny = cpl_image_get_size_y(image);
5541 sdata = cpl_image_get_data_float_const(image);
5543 nref = cpl_vector_get_size(lines);
5544 line = cpl_vector_get_data(lines);
5546 if (*wavestart < 1.0 && *waveend < 1.0) {
5547 firstLambda = line[0];
5548 lastLambda = line[nref-1];
5549 extrapolation = (lastLambda - firstLambda) / 10;
5550 firstLambda -= extrapolation;
5551 lastLambda += extrapolation;
5552 *wavestart = firstLambda;
5553 *waveend = lastLambda;
5556 firstLambda = *wavestart;
5557 lastLambda = *waveend;
5560 nl = (lastLambda - firstLambda) / dispersion;
5561 resampled = cpl_image_new(nl, ny, CPL_TYPE_FLOAT);
5562 rdata = cpl_image_get_data_float(resampled);
5565 idata = cpl_image_get_data_float(calibration);
5568 ddata = cpl_image_get_data_float(residuals);
5571 for (j = 0; j <= order; j++)
5572 cpl_table_new_column(idscoeff, clab[j], CPL_TYPE_DOUBLE);
5575 cpl_table_set_size(restable, nref);
5576 cpl_table_new_column(restable,
"wavelength", CPL_TYPE_DOUBLE);
5577 cpl_table_copy_data_double(restable,
"wavelength", line);
5578 for (i = 0; i < ny; i += step) {
5579 snprintf(name, MAX_COLNAME,
"r%d", i);
5580 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
5581 snprintf(name, MAX_COLNAME,
"d%d", i);
5582 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
5583 snprintf(name, MAX_COLNAME,
"p%d", i);
5584 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
5588 if (detected_lines) {
5589 cpl_table_set_size(detected_lines, 0);
5590 cpl_table_new_column(detected_lines,
"xpos", CPL_TYPE_DOUBLE);
5591 cpl_table_new_column(detected_lines,
"ypos", CPL_TYPE_DOUBLE);
5592 cpl_table_new_column(detected_lines,
"xpos_iter", CPL_TYPE_DOUBLE);
5593 cpl_table_new_column(detected_lines,
"ypos_iter", CPL_TYPE_DOUBLE);
5594 cpl_table_new_column(detected_lines,
"peak_flux", CPL_TYPE_DOUBLE);
5595 cpl_table_new_column(detected_lines,
"wave_ident", CPL_TYPE_DOUBLE);
5596 cpl_table_new_column(detected_lines,
"wave_ident_iter", CPL_TYPE_DOUBLE);
5597 cpl_table_new_column(detected_lines,
"xpos_fit_rect_wavecal", CPL_TYPE_DOUBLE);
5598 cpl_table_new_column(detected_lines,
"res_xpos", CPL_TYPE_DOUBLE);
5607 for (i = 0; i < ny; i++) {
5610 if (width > sradius) {
5626 countLines = cpl_bivector_get_size(output);
5627 if (countLines < 4) {
5628 cpl_bivector_delete(output);
5629 cpl_vector_delete(peaks);
5641 wavel = cpl_bivector_get_y(output);
5642 cpl_vector_subtract_scalar(wavel, refwave);
5644 uorder = countLines / 2 - 1;
5658 2 * (uorder + 1), &usedLines,
5662 cpl_bivector_delete(output);
5663 cpl_vector_delete(peaks);
5680 for (k = 0; k <= order; k++) {
5682 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
5685 cpl_table_set_double(idscoeff, clab[k], i,
5686 cpl_polynomial_get_coeff(ids, &k));
5693 cpl_size newlines = cpl_vector_get_size(peaks);
5694 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
5695 cpl_table_set_size(detected_lines, oldsize + newlines);
5696 for(cpl_size iline = 0; iline < newlines; ++iline)
5698 cpl_table_set_double(detected_lines,
"xpos",
5699 oldsize + iline, cpl_vector_get(peaks, iline) + 1);
5700 cpl_table_set_double(detected_lines,
"ypos",
5701 oldsize + iline, (
double)i + 1);
5702 cpl_table_set_double(detected_lines,
"peak_flux",
5704 sdata[i*nx+(
int)(cpl_vector_get(peaks, iline)+0.5)]);
5712 cpl_size nidentlines = cpl_bivector_get_size(output);
5713 cpl_size ndetectlines = cpl_vector_get_size(peaks);
5714 cpl_size totalsize = cpl_table_get_nrow(detected_lines);
5715 for(cpl_size idline = 0; idline < nidentlines; ++idline)
5717 for(cpl_size detline = 0; detline < ndetectlines; ++detline)
5719 if(cpl_vector_get(peaks, detline) ==
5720 cpl_bivector_get_x_data(output)[idline])
5722 cpl_size table_pos = totalsize - ndetectlines + detline;
5723 double wave_ident = cpl_bivector_get_y_data(output)[idline] + refwave;
5724 double xpix_fit = cpl_polynomial_eval_1d(ids,
5725 wave_ident - refwave, NULL);
5726 double xpos_det = cpl_table_get_double(detected_lines,
5729 cpl_table_set_double(detected_lines,
5733 cpl_table_set_double(detected_lines,
5734 "xpos_fit_rect_wavecal",
5737 cpl_table_set_double(detected_lines,
5740 xpos_det - xpix_fit - 1);
5754 ids, refwave, uradius);
5757 cpl_bivector_delete(output);
5758 output = new_output;
5764 cpl_polynomial_delete(ids);
5766 countLines = cpl_bivector_get_size(output);
5768 if (countLines < 4) {
5769 cpl_bivector_delete(output);
5770 cpl_vector_delete(peaks);
5783 for (k = 0; k <= order; k++)
5784 cpl_table_set_invalid(idscoeff, clab[k], i);
5788 wavel = cpl_bivector_get_y(output);
5789 cpl_vector_subtract_scalar(wavel, refwave);
5791 uorder = countLines / 2 - 1;
5796 2 * (uorder + 1), &usedLines,
5800 cpl_bivector_delete(output);
5801 cpl_vector_delete(peaks);
5814 for (k = 0; k <= order; k++)
5815 cpl_table_set_invalid(idscoeff, clab[k], i);
5821 for (k = 0; k <= order; k++) {
5823 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
5826 cpl_table_set_double(idscoeff, clab[k], i,
5827 cpl_polynomial_get_coeff(ids, &k));
5835 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
5836 cpl_size nidentlines = cpl_bivector_get_size(output);
5837 cpl_table_set_size(detected_lines, oldsize + nidentlines);
5838 for(cpl_size idline = 0; idline < nidentlines ; ++idline)
5840 double wave_ident = cpl_bivector_get_y_data(output)[idline] + refwave;
5841 double xpix_fit = cpl_polynomial_eval_1d(ids,
5842 wave_ident - refwave, NULL);
5843 cpl_table_set_double(detected_lines,
"xpos_iter",
5844 oldsize + idline, cpl_bivector_get_x_data(output)[idline] + 1);
5845 cpl_table_set_double(detected_lines,
"ypos_iter",
5846 oldsize + idline, (
double)i + 1);
5847 cpl_table_set_double(detected_lines,
"peak_flux",
5849 sdata[i*nx+(
int)(cpl_bivector_get_x_data(output)[idline]+0.5)]);
5850 cpl_table_set_double(detected_lines,
"wave_ident_iter",
5851 oldsize + idline, wave_ident);
5852 cpl_table_set_double(detected_lines,
"xpos_fit_rect_wavecal",
5853 oldsize + idline, xpix_fit + 1);
5860 nlines[i] = usedLines;
5862 error[i] = ids_err / sqrt(usedLines/(uorder + 1));
5864 pixstart = cpl_polynomial_eval_1d(ids,
5865 cpl_bivector_get_y_data(output)[0], NULL);
5866 pixend = cpl_polynomial_eval_1d(ids,
5867 cpl_bivector_get_y_data(output)[countLines-1], NULL);
5868 extrapolation = (pixend - pixstart) / 5;
5869 pixstart -= extrapolation;
5870 pixend += extrapolation;
5881 for (j = pixstart; j < pixend; j++) {
5883 lastLambda, refwave,
5892 for (j = 0; j < nl; j++) {
5893 lambda = firstLambda + j * dispersion;
5894 fpixel = cpl_polynomial_eval_1d(ids, lambda - refwave,
5897 if (pixel >= 0 && pixel < nx-1) {
5898 v1 = (sdata + i*nx)[pixel];
5899 v2 = (sdata + i*nx)[pixel+1];
5900 vi = v1 + (v2-v1)*(fpixel-pixel);
5901 (rdata + i*nl)[j] = vi;
5909 if (residuals || (restable && !(i%step))) {
5910 if (restable && !(i%step)) {
5911 lin = cpl_polynomial_new(1);
5912 for (k = 0; k < 2; k++)
5913 cpl_polynomial_set_coeff(lin, &k,
5914 cpl_polynomial_get_coeff(ids, &k));
5916 for (j = 0; j < countLines; j++) {
5917 pixe = cpl_bivector_get_x_data(output)[j];
5918 wave = cpl_bivector_get_y_data(output)[j];
5919 value = pixe - cpl_polynomial_eval_1d(ids, wave, NULL);
5922 (ddata + i*nx)[pixel] = value;
5924 if (restable && !(i%step)) {
5925 for (k = 0; k < nref; k++) {
5926 if (fabs(line[k] - refwave - wave) < 0.1) {
5927 snprintf(name, MAX_COLNAME,
"r%d", i);
5928 cpl_table_set_double(restable, name,
5931 - cpl_polynomial_eval_1d(lin, wave,
5933 snprintf(name, MAX_COLNAME,
"d%d", i);
5934 cpl_table_set_double(restable, name,
5936 snprintf(name, MAX_COLNAME,
"p%d", i);
5937 cpl_table_set_double(restable, name,
5944 if (restable && !(i%step)) {
5945 cpl_polynomial_delete(lin);
5954 mdata = cpl_mask_get_data(refmask);
5955 pixel = cpl_polynomial_eval_1d(ids, 0.0, NULL) + 0.5;
5956 if (pixel - 1 >= 0 && pixel + 1 < nx) {
5957 mdata[pixel-1 + i*nx] = CPL_BINARY_1;
5958 mdata[pixel + i*nx] = CPL_BINARY_1;
5959 mdata[pixel+1 + i*nx] = CPL_BINARY_1;
5963 cpl_polynomial_delete(ids);
5964 cpl_bivector_delete(output);
5966 cpl_vector_delete(peaks);
5971 kernel = cpl_matrix_new(3, 3);
5972 cpl_matrix_set(kernel, 0, 1, 1.0);
5973 cpl_matrix_set(kernel, 1, 1, 1.0);
5974 cpl_matrix_set(kernel, 2, 1, 1.0);
5976 cpl_mask_dilation(refmask, kernel);
5977 cpl_mask_erosion(refmask, kernel);
5978 cpl_mask_erosion(refmask, kernel);
5979 cpl_mask_dilation(refmask, kernel);
5981 cpl_matrix_delete(kernel);
5987 mdata = cpl_mask_get_data(refmask);
5988 have_it = cpl_calloc(ny,
sizeof(
int));
5990 for (i = 0; i < ny; i++, mdata += nx) {
5991 for (j = 0; j < nx; j++) {
5992 if (mdata[j] == CPL_BINARY_1) {
5999 mdata = cpl_mask_get_data(refmask);
6003 for (i = 0; i < ny; i++) {
6009 if (abs(have_it[first] - have_it[last]) < 3) {
6010 for (j = first; j < last; j++) {
6011 mdata[have_it[first] + nx*j + 0] = CPL_BINARY_1;
6012 mdata[have_it[first] + nx*j + 1] = CPL_BINARY_1;
6013 mdata[have_it[first] + nx*j + 2] = CPL_BINARY_1;
6155 const char *func =
"mos_locate_spectra";
6157 cpl_apertures *slits;
6158 cpl_image *labimage;
6159 cpl_image *refimage;
6161 cpl_propertylist *sort_col;
6167 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
6171 labimage = cpl_image_labelise_mask_create(mask, &nslits);
6174 cpl_image_delete(labimage);
6175 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6179 refimage = cpl_image_new_from_mask(mask);
6181 slits = cpl_apertures_new_from_image(refimage, labimage);
6183 cpl_image_delete(labimage);
6184 cpl_image_delete(refimage);
6186 nslits = cpl_apertures_get_size(slits);
6188 cpl_apertures_delete(slits);
6189 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6193 slitpos = cpl_table_new(nslits);
6194 cpl_table_new_column(slitpos,
"xtop", CPL_TYPE_DOUBLE);
6195 cpl_table_new_column(slitpos,
"ytop", CPL_TYPE_DOUBLE);
6196 cpl_table_new_column(slitpos,
"xbottom", CPL_TYPE_DOUBLE);
6197 cpl_table_new_column(slitpos,
"ybottom", CPL_TYPE_DOUBLE);
6198 cpl_table_set_column_unit(slitpos,
"xtop",
"pixel");
6199 cpl_table_set_column_unit(slitpos,
"ytop",
"pixel");
6200 cpl_table_set_column_unit(slitpos,
"xbottom",
"pixel");
6201 cpl_table_set_column_unit(slitpos,
"ybottom",
"pixel");
6203 for (i = 0; i < nslits; i++) {
6204 cpl_table_set_double(slitpos,
"xtop", i,
6205 cpl_apertures_get_top_x(slits, i+1) - 1);
6206 cpl_table_set_double(slitpos,
"ytop", i,
6207 cpl_apertures_get_top(slits, i+1));
6208 cpl_table_set_double(slitpos,
"xbottom", i,
6209 cpl_apertures_get_bottom_x(slits, i+1) - 1);
6210 cpl_table_set_double(slitpos,
"ybottom", i,
6211 cpl_apertures_get_bottom(slits, i+1));
6214 cpl_apertures_delete(slits);
6216 sort_col = cpl_propertylist_new();
6217 cpl_propertylist_append_bool(sort_col,
"ytop", 1);
6218 cpl_table_sort(slitpos, sort_col);
6219 cpl_propertylist_delete(sort_col);
6243 const char *func =
"mos_validate_slits";
6247 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
6249 if (1 != cpl_table_has_column(slits,
"xtop"))
6250 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6252 if (1 != cpl_table_has_column(slits,
"ytop"))
6253 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6255 if (1 != cpl_table_has_column(slits,
"xbottom"))
6256 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6258 if (1 != cpl_table_has_column(slits,
"ybottom"))
6259 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6261 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"xtop"))
6262 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6264 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"ytop"))
6265 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6267 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"xbottom"))
6268 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6270 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"ybottom"))
6271 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6273 return CPL_ERROR_NONE;
6307 const char *func =
"mos_rotate_slits";
6309 cpl_error_code error;
6310 char aux_name[] =
"_0";
6319 return CPL_ERROR_NONE;
6323 return cpl_error_set(func, error);
6325 if (rotation == 1 || rotation == 3) {
6331 for (i = 0; i < 77; i++)
6332 if (1 == cpl_table_has_column(slits, aux_name))
6334 if (1 == cpl_table_has_column(slits, aux_name))
6335 return cpl_error_set(func, CPL_ERROR_CONTINUE);
6336 cpl_table_name_column(slits,
"xtop", aux_name);
6337 cpl_table_name_column(slits,
"ytop",
"xtop");
6338 cpl_table_name_column(slits, aux_name,
"ytop");
6339 cpl_table_name_column(slits,
"xbottom", aux_name);
6340 cpl_table_name_column(slits,
"ybottom",
"xbottom");
6341 cpl_table_name_column(slits, aux_name,
"ybottom");
6344 if (rotation == 1 || rotation == 2) {
6345 cpl_table_multiply_scalar(slits,
"xtop", -1.0);
6346 cpl_table_multiply_scalar(slits,
"xbottom", -1.0);
6347 cpl_table_add_scalar(slits,
"xtop", nx);
6348 cpl_table_add_scalar(slits,
"xbottom", nx);
6351 if (rotation == 3 || rotation == 2) {
6352 cpl_table_multiply_scalar(slits,
"ytop", -1.0);
6353 cpl_table_multiply_scalar(slits,
"ybottom", -1.0);
6354 cpl_table_add_scalar(slits,
"ytop", ny);
6355 cpl_table_add_scalar(slits,
"ybottom", ny);
6358 return CPL_ERROR_NONE;
6422 cpl_array *top_ident = NULL;;
6423 cpl_array *bot_ident = NULL;;
6425 cpl_matrix *mpattern;
6426 cpl_matrix *top_data;
6427 cpl_matrix *top_pattern;
6428 cpl_matrix *top_mdata;
6429 cpl_matrix *top_mpattern;
6430 cpl_matrix *bot_data;
6431 cpl_matrix *bot_pattern;
6432 cpl_matrix *bot_mdata;
6433 cpl_matrix *bot_mpattern;
6434 cpl_propertylist *sort_col;
6443 double top_scale, bot_scale;
6444 double angle, top_angle, bot_angle;
6446 double xrms, top_xrms, bot_xrms;
6447 double yrms, top_yrms, bot_yrms;
6449 int nmaskslits, use_pattern;
6450 int found_slits, found_slits_top, found_slits_bot;
6452 cpl_table *positions;
6453 cpl_error_code error;
6462 cpl_polynomial *xpoly = NULL;
6463 cpl_polynomial *ypoly = NULL;
6464 cpl_polynomial *top_xpoly = NULL;
6465 cpl_polynomial *top_ypoly = NULL;
6466 cpl_polynomial *bot_xpoly = NULL;
6467 cpl_polynomial *bot_ypoly = NULL;
6469 char *msg_multiplex =
" ";
6474 cpl_msg_error(cpl_func,
"CCD slits table validation: %s",
6475 cpl_error_get_message());
6476 cpl_error_set(cpl_func, error);
6482 cpl_msg_error(cpl_func,
"Mask slits table validation: %s",
6483 cpl_error_get_message());
6484 cpl_error_set(cpl_func, error);
6488 if (1 != cpl_table_has_column(maskslits,
"slit_id")) {
6489 cpl_msg_error(cpl_func,
"Missing slits identifiers");
6490 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
6494 if (CPL_TYPE_INT != cpl_table_get_column_type(maskslits,
"slit_id")) {
6495 cpl_msg_error(cpl_func,
"Wrong type used for slits identifiers");
6496 cpl_error_set(cpl_func, CPL_ERROR_INVALID_TYPE);
6500 nslits = cpl_table_get_nrow(slits);
6501 nmaskslits = cpl_table_get_nrow(maskslits);
6503 if (nslits == 0 || nmaskslits == 0) {
6504 cpl_msg_error(cpl_func,
"Empty slits table");
6505 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
6509 if (nslits > 100 && mos_multiplex < 0) {
6510 cpl_msg_info(cpl_func,
"Many slits: using 'fast' pattern matching...");
6511 positions = mos_identify_slits_fast(slits, maskslits, global);
6512 if (positions == NULL)
6513 cpl_error_set_where(cpl_func);
6521 sort_col = cpl_propertylist_new();
6522 cpl_propertylist_append_bool(sort_col,
"ytop", 1);
6523 cpl_table_sort(slits, sort_col);
6524 cpl_table_sort(maskslits, sort_col);
6525 cpl_propertylist_delete(sort_col);
6531 if (nslits < 3 && nmaskslits > nslits) {
6541 cpl_msg_warning(cpl_func,
"Cannot match the %d found CCD slits "
6542 "with the %d mask slits: process will continue "
6543 "using the detected CCD slits positions", nslits,
6546 cpl_msg_warning(cpl_func,
"Cannot match the found CCD slit with "
6547 "the %d mask slits: process will continue using "
6548 "the detected CCD slit position", nmaskslits);
6552 if (nmaskslits < 3 && nslits > nmaskslits) {
6560 cpl_msg_warning(cpl_func,
"Cannot match the %d found CCD slits with "
6561 "the %d mask slits: process will continue using "
6562 "the detected CCD slits positions", nslits,
6574 xtop = cpl_table_get_data_double(slits,
"xtop");
6575 ytop = cpl_table_get_data_double(slits,
"ytop");
6576 xmtop = cpl_table_get_data_double(maskslits,
"xtop");
6577 ymtop = cpl_table_get_data_double(maskslits,
"ytop");
6579 xbot = cpl_table_get_data_double(slits,
"xbottom");
6580 ybot = cpl_table_get_data_double(slits,
"ybottom");
6581 xmbot = cpl_table_get_data_double(maskslits,
"xbottom");
6582 ymbot = cpl_table_get_data_double(maskslits,
"ybottom");
6584 top_data = cpl_matrix_new(2, nslits);
6585 top_pattern = cpl_matrix_new(2, nmaskslits);
6586 bot_data = cpl_matrix_new(2, nslits);
6587 bot_pattern = cpl_matrix_new(2, nmaskslits);
6589 for (i = 0; i < nslits; i++)
6590 cpl_matrix_set(top_data, 0, i, xtop[i]);
6592 for (i = 0; i < nslits; i++)
6593 cpl_matrix_set(top_data, 1, i, ytop[i]);
6595 for (i = 0; i < nmaskslits; i++)
6596 cpl_matrix_set(top_pattern, 0, i, xmtop[i]);
6598 for (i = 0; i < nmaskslits; i++)
6599 cpl_matrix_set(top_pattern, 1, i, ymtop[i]);
6601 for (i = 0; i < nslits; i++)
6602 cpl_matrix_set(bot_data, 0, i, xbot[i]);
6604 for (i = 0; i < nslits; i++)
6605 cpl_matrix_set(bot_data, 1, i, ybot[i]);
6607 for (i = 0; i < nmaskslits; i++)
6608 cpl_matrix_set(bot_pattern, 0, i, xmbot[i]);
6610 for (i = 0; i < nmaskslits; i++)
6611 cpl_matrix_set(bot_pattern, 1, i, ymbot[i]);
6613 if (nmaskslits > nslits)
6614 use_pattern = nslits;
6616 use_pattern = nmaskslits;
6618 top_ident = cpl_ppm_match_points(top_data, nslits, 1.0, top_pattern,
6619 use_pattern, 0.0, 0.1, 5, &top_mdata,
6620 &top_mpattern, &top_scale, &top_angle);
6622 bot_ident = cpl_ppm_match_points(bot_data, nslits, 1.0, bot_pattern,
6623 use_pattern, 0.0, 0.1, 5, &bot_mdata,
6624 &bot_mpattern, &bot_scale, &bot_angle);
6625 cpl_matrix_delete(top_data);
6626 cpl_matrix_delete(top_pattern);
6627 cpl_matrix_delete(bot_data);
6628 cpl_matrix_delete(bot_pattern);
6630 if (top_ident == NULL && bot_ident == NULL) {
6631 cpl_msg_warning(cpl_func,
"Pattern matching failure: cannot match "
6632 "the %d found CCD slits with the %d mask slits: "
6633 "process will continue using the detected CCD "
6634 "slits positions", nslits, nmaskslits);
6638 found_slits_top = 0;
6639 found_slits_bot = 0;
6640 if (top_ident && bot_ident) {
6641 cpl_msg_info(cpl_func,
"Median platescale: %f +/- %f pixel/mm",
6642 (top_scale + bot_scale) / 2, fabs(top_scale - bot_scale));
6643 cpl_msg_info(cpl_func,
"Median rotation: %f +/- %f degrees",
6644 (top_angle + bot_angle) / 2, fabs(top_angle - bot_angle));
6645 if (fabs(top_angle) < fabs(bot_angle))
6646 angle = fabs(top_angle);
6648 angle = fabs(bot_angle);
6649 found_slits_top = cpl_matrix_get_ncol(top_mdata);
6650 found_slits_bot = cpl_matrix_get_ncol(bot_mdata);
6652 else if (top_ident) {
6653 cpl_msg_info(cpl_func,
"Median platescale: %f pixel/mm", top_scale);
6654 cpl_msg_info(cpl_func,
"Median rotation: %f degrees", top_angle);
6655 angle = fabs(top_angle);
6656 found_slits_top = cpl_matrix_get_ncol(top_mdata);
6659 cpl_msg_info(cpl_func,
"Median platescale: %f pixel/mm", bot_scale);
6660 cpl_msg_info(cpl_func,
"Median rotation: %f degrees", bot_angle);
6661 angle = fabs(bot_angle);
6662 found_slits_bot = cpl_matrix_get_ncol(bot_mdata);
6665 cpl_array_delete(top_ident);
6666 cpl_array_delete(bot_ident);
6669 cpl_msg_warning(cpl_func,
"Uncertain pattern matching: the rotation "
6670 "angle is expected to be around zero. This match is "
6671 "rejected: the process will continue using the %d "
6672 "detected CCD slits positions", nslits);
6676 found_slits = found_slits_top;
6677 if (found_slits < found_slits_bot)
6678 found_slits = found_slits_bot;
6680 if (found_slits < 4) {
6681 cpl_msg_warning(cpl_func,
6682 "Too few safely identified slits: %d out of %d "
6683 "candidates (%d expected). Process will continue "
6684 "using the detected CCD slits positions", found_slits,
6685 nslits, nmaskslits);
6689 cpl_msg_info(cpl_func,
"Preliminary identified slits: %d out of %d "
6690 "candidates\n(%d expected)", found_slits, nslits,
6693 if (found_slits_top < 4)
6694 found_slits_top = 0;
6696 if (found_slits_bot < 4)
6697 found_slits_bot = 0;
6705 for (i = 0; i < 2; i++) {
6706 cpl_size mindeg2d[] = {0, 0};
6707 cpl_size maxdeg2d[2];
6708 cpl_vector * fitresidual;
6710 found_slits = found_slits_top;
6712 mpattern = top_mpattern;
6715 found_slits = found_slits_bot;
6717 mpattern = bot_mpattern;
6720 if (found_slits == 0)
6722 else if (found_slits < 10)
6723 maxdeg2d[0] = maxdeg2d[1] = 1;
6725 maxdeg2d[0] = maxdeg2d[1] = 2;
6727 xpos = cpl_vector_wrap(found_slits,
6728 cpl_matrix_get_data(mdata) );
6729 ypos = cpl_vector_wrap(found_slits,
6730 cpl_matrix_get_data(mdata) + found_slits);
6731 xmpos = cpl_vector_wrap(found_slits,
6732 cpl_matrix_get_data(mpattern) );
6733 ympos = cpl_vector_wrap(found_slits,
6734 cpl_matrix_get_data(mpattern) + found_slits);
6735 mpos = cpl_bivector_wrap_vectors(xmpos, ympos);
6736 fitresidual = cpl_vector_new(cpl_vector_get_size(xpos));
6737 xpoly = cpl_polynomial_new(2);
6738 cpl_polynomial_fit(xpoly, mpattern, NULL, xpos, NULL, CPL_FALSE, mindeg2d, maxdeg2d);
6739 cpl_vector_fill_polynomial_fit_residual(fitresidual, xpos, NULL, xpoly, mpattern, NULL);
6740 xmse = cpl_vector_product(fitresidual, fitresidual)
6741 / cpl_vector_get_size(fitresidual);
6742 ypoly = cpl_polynomial_new(2);
6743 cpl_polynomial_fit(ypoly, mpattern, NULL, ypos, NULL, CPL_FALSE, mindeg2d, maxdeg2d);
6744 cpl_vector_fill_polynomial_fit_residual(fitresidual, ypos, NULL, ypoly, mpattern, NULL);
6745 ymse = cpl_vector_product(fitresidual, fitresidual)
6746 / cpl_vector_get_size(fitresidual);
6748 cpl_bivector_unwrap_vectors(mpos);
6749 cpl_vector_unwrap(xpos);
6750 cpl_vector_unwrap(ypos);
6751 cpl_vector_unwrap(xmpos);
6752 cpl_vector_unwrap(ympos);
6753 cpl_matrix_delete(mdata);
6754 cpl_matrix_delete(mpattern);
6755 cpl_vector_delete(fitresidual);
6760 top_xrms = sqrt(xmse*2*maxdeg2d[0]/(found_slits - 1));
6761 top_yrms = sqrt(ymse*2*maxdeg2d[0]/(found_slits - 1));
6766 bot_xrms = sqrt(xmse*2*maxdeg2d[0]/(found_slits - 1));
6767 bot_yrms = sqrt(ymse*2*maxdeg2d[0]/(found_slits - 1));
6771 if (top_xpoly && bot_xpoly) {
6772 if (top_xrms < bot_xrms) {
6775 cpl_polynomial_delete(bot_xpoly);
6780 cpl_polynomial_delete(top_xpoly);
6783 else if (top_xpoly) {
6792 if (top_ypoly && bot_ypoly) {
6793 if (top_yrms < bot_yrms) {
6796 cpl_polynomial_delete(bot_ypoly);
6801 cpl_polynomial_delete(top_ypoly);
6804 else if (top_ypoly) {
6813 if (xpoly == NULL || ypoly == NULL) {
6814 cpl_msg_warning(cpl_func,
"Fit failure: the accuracy of the "
6815 "identified slits positions cannot be improved.");
6816 cpl_polynomial_delete(xpoly);
6817 cpl_polynomial_delete(ypoly);
6822 cpl_msg_info(cpl_func,
6823 "Fit successful: X rms = %.3g, Y rms = %.3g (pixel)",
6827 write_global_distortion(global, 0, xpoly);
6828 write_global_distortion(global, 7, ypoly);
6836 positions = cpl_table_duplicate(maskslits);
6837 cpl_table_duplicate_column(positions,
"xmtop", positions,
"xtop");
6838 cpl_table_duplicate_column(positions,
"ymtop", positions,
"ytop");
6839 cpl_table_duplicate_column(positions,
"xmbottom", positions,
"xbottom");
6840 cpl_table_duplicate_column(positions,
"ymbottom", positions,
"ybottom");
6842 point = cpl_vector_new(2);
6843 dpoint = cpl_vector_get_data(point);
6845 for (i = 0; i < nmaskslits; i++) {
6849 dpoint[0] = cpl_table_get_double(positions,
"xmtop", i, NULL);
6850 dpoint[1] = cpl_table_get_double(positions,
"ymtop", i, NULL);
6851 position_x = cpl_polynomial_eval(xpoly, point);
6858 cpl_table_set_double(positions,
"xtop", i, position_x);
6859 position_y = cpl_polynomial_eval(ypoly, point);
6860 cpl_table_set_double(positions,
"ytop", i, position_y);
6861 dpoint[0] = cpl_table_get_double(positions,
"xmbottom", i, NULL);
6862 dpoint[1] = cpl_table_get_double(positions,
"ymbottom", i, NULL);
6863 position_x = cpl_polynomial_eval(xpoly, point);
6864 cpl_table_set_double(positions,
"xbottom", i, position_x);
6865 position_y = cpl_polynomial_eval(ypoly, point);
6866 cpl_table_set_double(positions,
"ybottom", i, position_y);
6875 cpl_vector_delete(point);
6876 cpl_polynomial_delete(xpoly);
6877 cpl_polynomial_delete(ypoly);
6879 cpl_table_erase_column(positions,
"xmtop");
6880 cpl_table_erase_column(positions,
"ymtop");
6881 cpl_table_erase_column(positions,
"xmbottom");
6882 cpl_table_erase_column(positions,
"ymbottom");
6884 if (mos_multiplex >= 0) {
6886 cpl_sprintf(
"in the CCD section between %d and %d pixel",
6887 mos_multiplex * mos_region_size,
6888 (mos_multiplex + 1) * mos_region_size);
6891 if (nmaskslits > nslits)
6892 cpl_msg_info(cpl_func,
6893 "Finally identified slits: %d out of %d expected %s\n"
6894 "(%d recovered)", nmaskslits, nmaskslits, msg_multiplex,
6895 nmaskslits - nslits);
6896 else if (nmaskslits < nslits)
6897 cpl_msg_info(cpl_func,
6898 "Finally identified slits: %d out of %d expected %s\n"
6899 "(%d rejected)", nmaskslits, nmaskslits, msg_multiplex,
6900 nslits - nmaskslits);
6902 cpl_msg_info(cpl_func,
6903 "Finally identified slits: %d out of %d expected %s",
6904 nmaskslits, nmaskslits, msg_multiplex);
6906 if (mos_multiplex >= 0) {
6907 cpl_free(msg_multiplex);
6915 cpl_table *mos_identify_slits_fast(cpl_table *slits, cpl_table *maskslits,
6918 const char *func =
"mos_identify_slits_fast";
6920 cpl_propertylist *sort_col;
6921 cpl_table *positions;
6930 cpl_polynomial *xpoly = NULL;
6931 cpl_polynomial *ypoly = NULL;
6932 cpl_error_code error;
6938 double dist1, dist2, dist3, dist, mindist;
6939 double scale, minscale, maxscale;
6940 double angle, minangle, maxangle;
6967 double sradius = 0.01;
6970 double pi = 3.14159265358979323846;
6975 cpl_msg_error(func,
"CCD slits table validation: %s",
6976 cpl_error_get_message());
6977 cpl_error_set(func, error);
6983 cpl_msg_error(func,
"Mask slits table validation: %s",
6984 cpl_error_get_message());
6985 cpl_error_set(func, error);
6989 if (1 != cpl_table_has_column(maskslits,
"slit_id")) {
6990 cpl_msg_error(func,
"Missing slits identifiers");
6991 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6995 if (CPL_TYPE_INT != cpl_table_get_column_type(maskslits,
"slit_id")) {
6996 cpl_msg_error(func,
"Wrong type used for slits identifiers");
6997 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
7001 nslits = cpl_table_get_nrow(slits);
7002 nmaskslits = cpl_table_get_nrow(maskslits);
7004 if (nslits == 0 || nmaskslits == 0) {
7005 cpl_msg_error(func,
"Empty slits table");
7006 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
7016 if (cpl_table_has_column(slits,
"xcenter"))
7017 cpl_table_erase_column(slits,
"xcenter");
7019 if (cpl_table_has_column(slits,
"ycenter"))
7020 cpl_table_erase_column(slits,
"ycenter");
7022 if (cpl_table_has_column(maskslits,
"xcenter"))
7023 cpl_table_erase_column(maskslits,
"xcenter");
7025 if (cpl_table_has_column(maskslits,
"ycenter"))
7026 cpl_table_erase_column(maskslits,
"ycenter");
7028 cpl_table_duplicate_column(slits,
"xcenter", slits,
"xtop");
7029 cpl_table_add_columns(slits,
"xcenter",
"xbottom");
7030 cpl_table_divide_scalar(slits,
"xcenter", 2.0);
7031 cpl_table_duplicate_column(slits,
"ycenter", slits,
"ytop");
7032 cpl_table_add_columns(slits,
"ycenter",
"ybottom");
7033 cpl_table_divide_scalar(slits,
"ycenter", 2.0);
7035 cpl_table_duplicate_column(maskslits,
"xcenter", maskslits,
"xtop");
7036 cpl_table_add_columns(maskslits,
"xcenter",
"xbottom");
7037 cpl_table_divide_scalar(maskslits,
"xcenter", 2.0);
7038 cpl_table_duplicate_column(maskslits,
"ycenter", maskslits,
"ytop");
7039 cpl_table_add_columns(maskslits,
"ycenter",
"ybottom");
7040 cpl_table_divide_scalar(maskslits,
"ycenter", 2.0);
7047 sort_col = cpl_propertylist_new();
7048 cpl_propertylist_append_bool(sort_col,
"ycenter", 1);
7049 cpl_table_sort(slits, sort_col);
7050 cpl_table_sort(maskslits, sort_col);
7051 cpl_propertylist_delete(sort_col);
7058 if (nslits < 3 && nmaskslits > nslits) {
7068 cpl_msg_warning(func,
"Cannot match the found CCD slit with the "
7069 "%d mask slits: process will continue using the "
7070 "detected CCD slit position", nmaskslits);
7072 cpl_msg_warning(func,
"Cannot match the %d found CCD slits with "
7073 "the %d mask slits: process will continue using "
7074 "the detected CCD slits positions", nslits,
7079 if (nslits <= 3 && nslits == nmaskslits) {
7081 cpl_msg_warning(func,
"Too few slits (%d) on mask and CCD", nslits);
7082 cpl_msg_warning(func,
"Their detected positions are left unchanged");
7093 positions = cpl_table_duplicate(slits);
7094 cpl_table_erase_column(slits,
"xcenter");
7095 cpl_table_erase_column(slits,
"ycenter");
7096 cpl_table_duplicate_column(positions,
"xmtop", maskslits,
"xtop");
7097 cpl_table_duplicate_column(positions,
"ymtop", maskslits,
"ytop");
7098 cpl_table_duplicate_column(positions,
"xmbottom", maskslits,
"xbottom");
7099 cpl_table_duplicate_column(positions,
"ymbottom", maskslits,
"ybottom");
7100 cpl_table_duplicate_column(positions,
"xmcenter", maskslits,
"xcenter");
7101 cpl_table_duplicate_column(positions,
"ymcenter", maskslits,
"ycenter");
7102 cpl_table_duplicate_column(positions,
"slit_id", maskslits,
"slit_id");
7103 cpl_table_erase_column(maskslits,
"xcenter");
7104 cpl_table_erase_column(maskslits,
"ycenter");
7107 xcenter = cpl_table_get_data_double(positions,
"xcenter");
7108 ycenter = cpl_table_get_data_double(positions,
"ycenter");
7109 xmcenter = cpl_table_get_data_double(positions,
"xmcenter");
7110 ymcenter = cpl_table_get_data_double(positions,
"ymcenter");
7112 dist1 = (xcenter[0] - xcenter[1])*(xcenter[0] - xcenter[1])
7113 + (ycenter[0] - ycenter[1])*(ycenter[0] - ycenter[1]);
7114 dist2 = (xmcenter[0] - xmcenter[1])*(xmcenter[0] - xmcenter[1])
7115 + (ymcenter[0] - ymcenter[1])*(ymcenter[0] - ymcenter[1]);
7116 scale = sqrt(dist1/dist2);
7119 dist1 = (xcenter[1] - xcenter[2])*(xcenter[1] - xcenter[2])
7120 + (ycenter[1] - ycenter[2])*(ycenter[1] - ycenter[2]);
7121 dist2 = (xmcenter[1] - xmcenter[2])*(xmcenter[1] - xmcenter[2])
7122 + (ymcenter[1] - ymcenter[2])*(ymcenter[1] - ymcenter[2]);
7123 scale += sqrt(dist1/dist2);
7127 cpl_msg_info(func,
"Platescale: %f pixel/mm", scale);
7133 if (nmaskslits < 3 && nslits > nmaskslits) {
7141 cpl_msg_warning(func,
"Cannot match the %d found CCD slits with "
7142 "the %d mask slits: process will continue using "
7143 "the detected CCD slits positions", nslits,
7175 if (cpl_table_has_column(slits,
"xpseudo"))
7176 cpl_table_erase_column(slits,
"xpseudo");
7178 if (cpl_table_has_column(slits,
"ypseudo"))
7179 cpl_table_erase_column(slits,
"ypseudo");
7181 if (cpl_table_has_column(maskslits,
"xpseudo"))
7182 cpl_table_erase_column(maskslits,
"xpseudo");
7184 if (cpl_table_has_column(maskslits,
"ypseudo"))
7185 cpl_table_erase_column(maskslits,
"ypseudo");
7187 cpl_table_duplicate_column(slits,
"xpseudo", slits,
"xcenter");
7188 cpl_table_duplicate_column(slits,
"ypseudo", slits,
"ycenter");
7190 xcenter = cpl_table_get_data_double(slits,
"xcenter");
7191 ycenter = cpl_table_get_data_double(slits,
"ycenter");
7192 xpseudo = cpl_table_get_data_double(slits,
"xpseudo");
7193 ypseudo = cpl_table_get_data_double(slits,
"ypseudo");
7195 for (i = 1; i < nslits - 1; i++) {
7196 dist1 = (xcenter[i-1] - xcenter[i]) * (xcenter[i-1] - xcenter[i])
7197 + (ycenter[i-1] - ycenter[i]) * (ycenter[i-1] - ycenter[i]);
7198 dist2 = (xcenter[i-1] - xcenter[i+1]) * (xcenter[i-1] - xcenter[i+1])
7199 + (ycenter[i-1] - ycenter[i+1]) * (ycenter[i-1] - ycenter[i+1]);
7200 dist3 = (xcenter[i] - xcenter[i+1]) * (xcenter[i] - xcenter[i+1])
7201 + (ycenter[i] - ycenter[i+1]) * (ycenter[i] - ycenter[i+1]);
7202 xpseudo[i] = sqrt(dist1/dist2);
7203 ypseudo[i] = sqrt(dist3/dist2);
7206 cpl_table_set_invalid(slits,
"xpseudo", 0);
7207 cpl_table_set_invalid(slits,
"xpseudo", nslits-1);
7208 cpl_table_set_invalid(slits,
"ypseudo", 0);
7209 cpl_table_set_invalid(slits,
"ypseudo", nslits-1);
7211 cpl_table_duplicate_column(maskslits,
"xpseudo", maskslits,
"xcenter");
7212 cpl_table_duplicate_column(maskslits,
"ypseudo", maskslits,
"ycenter");
7214 xcenter = cpl_table_get_data_double(maskslits,
"xcenter");
7215 ycenter = cpl_table_get_data_double(maskslits,
"ycenter");
7216 xmpseudo = cpl_table_get_data_double(maskslits,
"xpseudo");
7217 ympseudo = cpl_table_get_data_double(maskslits,
"ypseudo");
7219 for (i = 1; i < nmaskslits - 1; i++) {
7220 dist1 = (xcenter[i-1] - xcenter[i])*(xcenter[i-1] - xcenter[i])
7221 + (ycenter[i-1] - ycenter[i])*(ycenter[i-1] - ycenter[i]);
7222 dist2 = (xcenter[i-1] - xcenter[i+1])*(xcenter[i-1] - xcenter[i+1])
7223 + (ycenter[i-1] - ycenter[i+1])*(ycenter[i-1] - ycenter[i+1]);
7224 dist3 = (xcenter[i] - xcenter[i+1])*(xcenter[i] - xcenter[i+1])
7225 + (ycenter[i] - ycenter[i+1])*(ycenter[i] - ycenter[i+1]);
7226 xmpseudo[i] = sqrt(dist1/dist2);
7227 ympseudo[i] = sqrt(dist3/dist2);
7230 cpl_table_set_invalid(maskslits,
"xpseudo", 0);
7231 cpl_table_set_invalid(maskslits,
"xpseudo", nmaskslits-1);
7232 cpl_table_set_invalid(maskslits,
"ypseudo", 0);
7233 cpl_table_set_invalid(maskslits,
"ypseudo", nmaskslits-1);
7245 if (cpl_table_has_column(slits,
"slit_id"))
7246 cpl_table_erase_column(slits,
"slit_id");
7247 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
7248 slit_id = cpl_table_get_data_int(maskslits,
"slit_id");
7250 for (i = 1; i < nmaskslits - 1; i++) {
7252 mindist = (xmpseudo[i] - xpseudo[1]) * (xmpseudo[i] - xpseudo[1])
7253 + (ympseudo[i] - ypseudo[1]) * (ympseudo[i] - ypseudo[1]);
7255 if (mindist < sradius*sradius)
7257 for (j = 2; j < nslits - 1; j++) {
7258 dist = (xmpseudo[i] - xpseudo[j]) * (xmpseudo[i] - xpseudo[j])
7259 + (ympseudo[i] - ypseudo[j]) * (ympseudo[i] - ypseudo[j]);
7260 if (dist < sradius*sradius)
7264 if (mindist > dist) {
7270 mindist = sqrt(mindist);
7272 if (mindist < sradius && in_sradius == 1) {
7273 cpl_table_set_int(slits,
"slit_id", minpos-1, slit_id[i-1]);
7274 cpl_table_set_int(slits,
"slit_id", minpos, slit_id[i]);
7275 cpl_table_set_int(slits,
"slit_id", minpos+1, slit_id[i+1]);
7285 found_slits = nslits - cpl_table_count_invalid(slits,
"slit_id");
7287 if (found_slits < 3) {
7288 cpl_msg_warning(func,
"Too few preliminarily identified slits: "
7289 "%d out of %d", found_slits, nslits);
7290 if (nslits == nmaskslits) {
7291 cpl_msg_warning(func,
"(this is not an error, it could be caused "
7292 "by a mask with regularly located slits)");
7293 cpl_msg_warning(func,
"The detected slits positions are left "
7303 cpl_table_erase_column(slits,
"slit_id");
7304 cpl_table_erase_column(slits,
"xpseudo");
7305 cpl_table_erase_column(slits,
"ypseudo");
7306 positions = cpl_table_duplicate(slits);
7307 cpl_table_erase_column(slits,
"xcenter");
7308 cpl_table_erase_column(slits,
"ycenter");
7310 cpl_table_erase_column(maskslits,
"xpseudo");
7311 cpl_table_erase_column(maskslits,
"ypseudo");
7312 cpl_table_duplicate_column(positions,
"xmtop",
7314 cpl_table_duplicate_column(positions,
"ymtop",
7316 cpl_table_duplicate_column(positions,
"xmbottom",
7317 maskslits,
"xbottom");
7318 cpl_table_duplicate_column(positions,
"ymbottom",
7319 maskslits,
"ybottom");
7320 cpl_table_duplicate_column(positions,
"xmcenter",
7321 maskslits,
"xcenter");
7322 cpl_table_duplicate_column(positions,
"ymcenter",
7323 maskslits,
"ycenter");
7324 cpl_table_duplicate_column(positions,
"slit_id",
7325 maskslits,
"slit_id");
7326 cpl_table_erase_column(maskslits,
"xcenter");
7327 cpl_table_erase_column(maskslits,
"ycenter");
7331 cpl_table_erase_column(slits,
"slit_id");
7332 cpl_table_erase_column(slits,
"xpseudo");
7333 cpl_table_erase_column(slits,
"ypseudo");
7334 positions = cpl_table_duplicate(slits);
7335 cpl_table_erase_column(slits,
"xcenter");
7336 cpl_table_erase_column(slits,
"ycenter");
7337 cpl_msg_warning(func,
"(the failure could be caused "
7338 "by a mask with regularly located slits)");
7343 cpl_msg_info(func,
"Preliminarily identified slits: %d out of %d "
7344 "candidates (%d expected)", found_slits, nslits,
7355 positions = cpl_table_new(found_slits);
7356 cpl_table_new_column(positions,
"slit_id", CPL_TYPE_INT);
7357 cpl_table_new_column(positions,
"xtop", CPL_TYPE_DOUBLE);
7358 cpl_table_new_column(positions,
"ytop", CPL_TYPE_DOUBLE);
7359 cpl_table_new_column(positions,
"xbottom", CPL_TYPE_DOUBLE);
7360 cpl_table_new_column(positions,
"ybottom", CPL_TYPE_DOUBLE);
7361 cpl_table_new_column(positions,
"xcenter", CPL_TYPE_DOUBLE);
7362 cpl_table_new_column(positions,
"ycenter", CPL_TYPE_DOUBLE);
7363 cpl_table_new_column(positions,
"xmtop", CPL_TYPE_DOUBLE);
7364 cpl_table_new_column(positions,
"ymtop", CPL_TYPE_DOUBLE);
7365 cpl_table_new_column(positions,
"xmbottom", CPL_TYPE_DOUBLE);
7366 cpl_table_new_column(positions,
"ymbottom", CPL_TYPE_DOUBLE);
7367 cpl_table_new_column(positions,
"xmcenter", CPL_TYPE_DOUBLE);
7368 cpl_table_new_column(positions,
"ymcenter", CPL_TYPE_DOUBLE);
7369 cpl_table_new_column(positions,
"good", CPL_TYPE_INT);
7370 cpl_table_fill_column_window_int(positions,
"good", 0, found_slits, 0);
7372 slit_id = cpl_table_get_data_int (slits,
"slit_id");
7373 xtop = cpl_table_get_data_double(slits,
"xtop");
7374 ytop = cpl_table_get_data_double(slits,
"ytop");
7375 xbottom = cpl_table_get_data_double(slits,
"xbottom");
7376 ybottom = cpl_table_get_data_double(slits,
"ybottom");
7377 xcenter = cpl_table_get_data_double(slits,
"xcenter");
7378 ycenter = cpl_table_get_data_double(slits,
"ycenter");
7380 mslit_id = cpl_table_get_data_int (maskslits,
"slit_id");
7381 xmtop = cpl_table_get_data_double(maskslits,
"xtop");
7382 ymtop = cpl_table_get_data_double(maskslits,
"ytop");
7383 xmbottom = cpl_table_get_data_double(maskslits,
"xbottom");
7384 ymbottom = cpl_table_get_data_double(maskslits,
"ybottom");
7385 xmcenter = cpl_table_get_data_double(maskslits,
"xcenter");
7386 ymcenter = cpl_table_get_data_double(maskslits,
"ycenter");
7396 cpl_table_fill_invalid_int(slits,
"slit_id", 0);
7397 for (i = 0; i < nmaskslits; i++) {
7398 for (j = 0; j < nslits; j++) {
7399 if (slit_id[j] == 0)
7401 if (mslit_id[i] == slit_id[j]) {
7402 cpl_table_set_int (positions,
"slit_id", k, slit_id[j]);
7404 cpl_table_set_double(positions,
"xtop", k, xtop[j]);
7405 cpl_table_set_double(positions,
"ytop", k, ytop[j]);
7406 cpl_table_set_double(positions,
"xbottom", k, xbottom[j]);
7407 cpl_table_set_double(positions,
"ybottom", k, ybottom[j]);
7408 cpl_table_set_double(positions,
"xcenter", k, xcenter[j]);
7409 cpl_table_set_double(positions,
"ycenter", k, ycenter[j]);
7411 cpl_table_set_double(positions,
"xmtop", k, xmtop[i]);
7412 cpl_table_set_double(positions,
"ymtop", k, ymtop[i]);
7413 cpl_table_set_double(positions,
"xmbottom", k, xmbottom[i]);
7414 cpl_table_set_double(positions,
"ymbottom", k, ymbottom[i]);
7415 cpl_table_set_double(positions,
"xmcenter", k, xmcenter[i]);
7416 cpl_table_set_double(positions,
"ymcenter", k, ymcenter[i]);
7427 cpl_table_erase_column(slits,
"slit_id");
7428 cpl_table_erase_column(slits,
"xpseudo");
7429 cpl_table_erase_column(slits,
"ypseudo");
7430 cpl_table_erase_column(slits,
"xcenter");
7431 cpl_table_erase_column(slits,
"ycenter");
7432 cpl_table_erase_column(maskslits,
"xpseudo");
7433 cpl_table_erase_column(maskslits,
"ypseudo");
7434 cpl_table_erase_column(maskslits,
"xcenter");
7435 cpl_table_erase_column(maskslits,
"ycenter");
7445 ytop = cpl_table_get_data_double(positions,
"ytop");
7446 ybottom = cpl_table_get_data_double(positions,
"ybottom");
7447 xcenter = cpl_table_get_data_double(positions,
"xcenter");
7448 ycenter = cpl_table_get_data_double(positions,
"ycenter");
7449 xmcenter = cpl_table_get_data_double(positions,
"xmcenter");
7450 ymcenter = cpl_table_get_data_double(positions,
"ymcenter");
7452 scales = cpl_vector_new(found_slits - 1);
7453 dscale = cpl_vector_get_data(scales);
7454 angles = cpl_vector_new(found_slits - 1);
7455 dangle = cpl_vector_get_data(angles);
7457 for (i = 1; i < found_slits; i++) {
7458 dist1 = (xcenter[i-1] - xcenter[i]) * (xcenter[i-1] - xcenter[i])
7459 + (ycenter[i-1] - ycenter[i]) * (ycenter[i-1] - ycenter[i]);
7460 dist2 = (xmcenter[i-1] - xmcenter[i]) * (xmcenter[i-1] - xmcenter[i])
7461 + (ymcenter[i-1] - ymcenter[i]) * (ymcenter[i-1] - ymcenter[i]);
7462 dscale[i-1] = sqrt(dist1/dist2);
7463 dangle[i-1] = atan2(ycenter[i-1] - ycenter[i],
7464 xcenter[i-1] - xcenter[i])
7465 - atan2(ymcenter[i-1] - ymcenter[i],
7466 xmcenter[i-1] - xmcenter[i]);
7471 minscale = cpl_vector_get_min(scales);
7472 scale = cpl_vector_get_median_const(scales);
7473 maxscale = cpl_vector_get_max(scales);
7475 minangle = cpl_vector_get_min(angles);
7476 angle = cpl_vector_get_median_const(angles);
7477 maxangle = cpl_vector_get_max(angles);
7479 cpl_msg_info(func,
"Median platescale: %f pixel/mm", scale);
7480 cpl_msg_info(func,
"Minmax platescale: %f, %f pixel/mm",
7481 minscale, maxscale);
7483 cpl_msg_info(func,
"Median rotation: %f degrees", angle);
7484 cpl_msg_info(func,
"Minmax rotation: %f, %f degrees",
7485 minangle, maxangle);
7487 good = cpl_table_get_data_int(positions,
"good");
7489 good[0] = good[found_slits - 1] = 1;
7490 for (i = 1; i < found_slits; i++) {
7491 if (fabs((dscale[i-1] - scale)/scale) < 0.10
7492 && fabs(dangle[i-1] - angle) < 2) {
7498 for (i = 0; i < found_slits; i++) {
7538 cpl_vector_delete(scales);
7539 cpl_vector_delete(angles);
7541 cpl_table_and_selected_int(positions,
"good", CPL_EQUAL_TO, 0);
7542 cpl_table_erase_selected(positions);
7543 cpl_table_erase_column(positions,
"good");
7544 found_slits = cpl_table_get_nrow(positions);
7546 if (found_slits < 4) {
7554 cpl_msg_warning(func,
"Too few safely identified slits: %d out of %d "
7555 "candidates (%d expected). Process will continue "
7556 "using the detected CCD slits positions", found_slits,
7557 nslits, nmaskslits);
7558 cpl_table_delete(positions);
7562 cpl_msg_info(func,
"Safely identified slits: %d out of %d "
7563 "candidates\n(%d expected)", found_slits, nslits,
7574 xpos = cpl_vector_wrap(found_slits,
7575 cpl_table_get_data_double(positions,
"xcenter"));
7576 ypos = cpl_vector_wrap(found_slits,
7577 cpl_table_get_data_double(positions,
"ycenter"));
7578 xmpos = cpl_vector_wrap(found_slits,
7579 cpl_table_get_data_double(positions,
"xmcenter"));
7580 ympos = cpl_vector_wrap(found_slits,
7581 cpl_table_get_data_double(positions,
"ymcenter"));
7582 mpos = cpl_bivector_wrap_vectors(xmpos, ympos);
7584 if (found_slits < 10)
7589 xpoly = cpl_polynomial_fit_2d_create(mpos, xpos, degree, &xmse);
7591 ypoly = cpl_polynomial_fit_2d_create(mpos, ypos, degree, &ymse);
7592 cpl_bivector_unwrap_vectors(mpos);
7593 cpl_vector_unwrap(xpos);
7594 cpl_vector_unwrap(ypos);
7595 cpl_vector_unwrap(xmpos);
7596 cpl_vector_unwrap(ympos);
7597 if (ypoly == NULL) {
7598 if (found_slits == nmaskslits) {
7599 cpl_msg_warning(func,
"Fit failure: the accuracy of the "
7600 "identified slits positions is not improved.");
7611 cpl_msg_info(func,
"Fit failure: not all slits have been "
7612 "identified. Process will continue using "
7613 "the detected CCD slits positions");
7616 cpl_polynomial_delete(xpoly);
7620 cpl_msg_info(func,
"Fit successful: X rms = %.3g, Y rms = %.3g (pixel)",
7621 sqrt(xmse), sqrt(ymse));
7624 write_global_distortion(global, 0, xpoly);
7625 write_global_distortion(global, 7, ypoly);
7633 cpl_table_delete(positions);
7635 positions = cpl_table_duplicate(maskslits);
7636 cpl_table_duplicate_column(positions,
"xmtop", positions,
"xtop");
7637 cpl_table_duplicate_column(positions,
"ymtop", positions,
"ytop");
7638 cpl_table_duplicate_column(positions,
"xmbottom", positions,
"xbottom");
7639 cpl_table_duplicate_column(positions,
"ymbottom", positions,
"ybottom");
7641 point = cpl_vector_new(2);
7642 dpoint = cpl_vector_get_data(point);
7644 for (i = 0; i < nmaskslits; i++) {
7645 dpoint[0] = cpl_table_get_double(positions,
"xmtop", i, NULL);
7646 dpoint[1] = cpl_table_get_double(positions,
"ymtop", i, NULL);
7647 cpl_table_set_double(positions,
"xtop", i,
7648 cpl_polynomial_eval(xpoly, point));
7649 cpl_table_set_double(positions,
"ytop", i,
7650 cpl_polynomial_eval(ypoly, point));
7651 dpoint[0] = cpl_table_get_double(positions,
"xmbottom", i, NULL);
7652 dpoint[1] = cpl_table_get_double(positions,
"ymbottom", i, NULL);
7653 cpl_table_set_double(positions,
"xbottom", i,
7654 cpl_polynomial_eval(xpoly, point));
7655 cpl_table_set_double(positions,
"ybottom", i,
7656 cpl_polynomial_eval(ypoly, point));
7659 cpl_vector_delete(point);
7660 cpl_polynomial_delete(xpoly);
7661 cpl_polynomial_delete(ypoly);
7663 cpl_table_erase_column(positions,
"xmtop");
7664 cpl_table_erase_column(positions,
"ymtop");
7665 cpl_table_erase_column(positions,
"xmbottom");
7666 cpl_table_erase_column(positions,
"ymbottom");
7668 if (nmaskslits > nslits)
7669 cpl_msg_info(func,
"Finally identified slits: %d out of %d expected\n"
7670 "(%d recovered)", nmaskslits, nmaskslits, nmaskslits - nslits);
7671 else if (nmaskslits < nslits)
7672 cpl_msg_info(func,
"Finally identified slits: %d out of %d expected\n"
7673 "(%d rejected)", nmaskslits, nmaskslits, nslits - nmaskslits);
7675 cpl_msg_info(func,
"Finally identified slits: %d out of %d expected",
7676 nmaskslits, nmaskslits);
7724 double blue,
double red,
double dispersion)
7727 const char *func =
"mos_trace_flat";
7729 cpl_image *gradient;
7730 cpl_image *sgradient;
7747 double start_y, prev_y;
7756 int pixel_above, pixel_below;
7758 char trace_id[MAX_COLNAME];
7763 if (flat == NULL || slits == NULL) {
7764 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
7768 if (dispersion <= 0.0) {
7769 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
7773 if (red - blue < dispersion) {
7774 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
7783 nslits = cpl_table_get_nrow(slits);
7784 if (1 != cpl_table_has_column(slits,
"slit_id")) {
7785 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
7786 for (i = 0; i < nslits; i++)
7787 cpl_table_set_int(slits,
"slit_id", i, -(i+1));
7790 slit_id = cpl_table_get_data_int(slits,
"slit_id");
7792 nx = cpl_image_get_size_x(flat);
7793 ny = cpl_image_get_size_y(flat);
7796 gradient = cpl_image_duplicate(flat);
7797 dgradient = cpl_image_get_data_float(gradient);
7799 for (i = 0; i < ny - 1; i++) {
7801 for (j = 0; j < nx; j++) {
7803 dgradient[l] = fabs(dgradient[l] - dgradient[l + nx]);
7808 for (j = 0; j < nx; j++)
7809 dgradient[npix - j] = 0.0;
7811 cpl_image_turn(gradient, -1);
7812 nx = cpl_image_get_size_x(gradient);
7813 ny = cpl_image_get_size_y(gradient);
7814 sgradient = mos_image_vertical_median_filter(gradient,
7815 filtbox, 0, ny, 0, step);
7816 cpl_image_delete(gradient);
7823 dgradient = cpl_image_get_data_float(sgradient);
7825 for (i = 1; i <= ny; i += step) {
7826 row = cpl_vector_new_from_image_row(sgradient, i);
7827 srow = cpl_vector_filter_median_create(row, filtbox);
7828 cpl_vector_subtract(row, srow);
7829 cpl_vector_delete(srow);
7830 g = dgradient + (i-1)*nx;
7831 r = cpl_vector_get_data(row);
7832 for (j = 0; j < nx; j++)
7834 cpl_vector_delete(row);
7844 xtop = cpl_table_get_data_double(slits,
"xtop");
7845 ytop = cpl_table_get_data_double(slits,
"ytop");
7846 xbottom = cpl_table_get_data_double(slits,
"xbottom");
7847 ybottom = cpl_table_get_data_double(slits,
"ybottom");
7855 peaks = cpl_calloc(ny,
sizeof(cpl_vector *));
7857 for (i = 0; i < ny; i += step) {
7858 g = dgradient + i*nx;
7864 cpl_vector_subtract_scalar(peaks[i], 0.5);
7868 cpl_image_delete(sgradient);
7894 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
7895 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
7902 nrows = (ny-1)/step + 1;
7903 traces = cpl_table_new(nrows);
7904 cpl_table_new_column(traces,
"x", CPL_TYPE_DOUBLE);
7905 cpl_table_set_column_unit(traces,
"x",
"pixel");
7906 for (i = 0, j = 0; i < ny; i += step, j++)
7907 cpl_table_set(traces,
"x", j, i);
7909 for (i = 0; i < nslits; i++) {
7930 peak = cpl_vector_get_data(peaks[pos]);
7931 npeaks = cpl_vector_get_size(peaks[pos]);
7933 min = fabs(peak[0] - xtop[i]);
7935 for (j = 1; j < npeaks; j++) {
7936 dist = fabs(peak[j] - xtop[i]);
7947 snprintf(trace_id, MAX_COLNAME,
"t%d", slit_id[i]);
7948 cpl_table_new_column(traces, trace_id, CPL_TYPE_DOUBLE);
7950 if (min > sradius || npeaks == 0) {
7951 cpl_msg_warning(func,
"Cannot find spectrum edge for "
7952 "top (or left) end of slit %d", slit_id[i]);
7964 cpl_table_set(traces, trace_id, pos/step, nx - peak[minpos]);
7965 start_y = peak[minpos];
7973 for (j = pos + step; j < ny; j += step) {
7974 if (j - pos > pixel_above)
7977 peak = cpl_vector_get_data(peaks[j]);
7978 npeaks = cpl_vector_get_size(peaks[j]);
7979 min = fabs(peak[0] - prev_y);
7981 for (k = 1; k < npeaks; k++) {
7982 dist = fabs(peak[k] - prev_y);
7988 if (min < tolerance) {
7989 cpl_table_set(traces, trace_id, j/step,
7991 prev_y = peak[minpos];
8002 for (j = pos - step; j >= 0; j -= step) {
8003 if (pos - j > pixel_below)
8006 peak = cpl_vector_get_data(peaks[j]);
8007 npeaks = cpl_vector_get_size(peaks[j]);
8008 min = fabs(peak[0] - prev_y);
8010 for (k = 1; k < npeaks; k++) {
8011 dist = fabs(peak[k] - prev_y);
8017 if (min < tolerance) {
8018 cpl_table_set(traces, trace_id, j/step,
8020 prev_y = peak[minpos];
8032 peak = cpl_vector_get_data(peaks[pos]);
8033 npeaks = cpl_vector_get_size(peaks[pos]);
8035 min = fabs(peak[0] - xbottom[i]);
8037 for (j = 1; j < npeaks; j++) {
8038 dist = fabs(peak[j] - xbottom[i]);
8049 snprintf(trace_id, MAX_COLNAME,
"b%d", slit_id[i]);
8050 cpl_table_new_column(traces, trace_id, CPL_TYPE_DOUBLE);
8052 if (min > sradius || npeaks == 0) {
8053 cpl_msg_warning(func,
"Cannot find spectrum edge for "
8054 "bottom (or right) end of slit %d", slit_id[i]);
8058 cpl_table_set(traces, trace_id, pos/step, nx - peak[minpos]);
8059 start_y = peak[minpos];
8067 for (j = pos + step; j < ny; j += step) {
8068 if (j - pos > pixel_above)
8071 peak = cpl_vector_get_data(peaks[j]);
8072 npeaks = cpl_vector_get_size(peaks[j]);
8073 min = fabs(peak[0] - prev_y);
8075 for (k = 1; k < npeaks; k++) {
8076 dist = fabs(peak[k] - prev_y);
8082 if (min < tolerance) {
8083 cpl_table_set(traces, trace_id, j/step,
8085 prev_y = peak[minpos];
8096 for (j = pos - step; j >= 0; j -= step) {
8097 if (pos - j > pixel_below)
8100 peak = cpl_vector_get_data(peaks[j]);
8101 npeaks = cpl_vector_get_size(peaks[j]);
8102 min = fabs(peak[0] - prev_y);
8104 for (k = 1; k < npeaks; k++) {
8105 dist = fabs(peak[k] - prev_y);
8111 if (min < tolerance) {
8112 cpl_table_set(traces, trace_id, j/step,
8114 prev_y = peak[minpos];
8122 for (i = 0; i < ny; i += step)
8123 cpl_vector_delete(peaks[i]);
8159 const char *func =
"mos_poly_trace";
8161 cpl_table *polytraces;
8165 cpl_polynomial *polytrace;
8166 char trace_id[MAX_COLNAME];
8167 char trace_res[MAX_COLNAME];
8168 char trace_mod[MAX_COLNAME];
8169 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8180 if (traces == NULL || slits == NULL) {
8181 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8186 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8190 nrows = cpl_table_get_nrow(traces);
8191 xdata = cpl_table_get_data_double(traces,
"x");
8192 nslits = cpl_table_get_nrow(slits);
8193 slit_id = cpl_table_get_data_int(slits,
"slit_id");
8195 polytraces = cpl_table_new(2*nslits);
8196 cpl_table_new_column(polytraces,
"slit_id", CPL_TYPE_INT);
8197 for (i = 0; i <= order; i++)
8198 cpl_table_new_column(polytraces, clab[i], CPL_TYPE_DOUBLE);
8200 for (i = 0; i < nslits; i++) {
8201 for (j = 0; j < 2; j++) {
8204 snprintf(trace_id, MAX_COLNAME,
"b%d", slit_id[i]);
8205 snprintf(trace_res, MAX_COLNAME,
"b%d_res", slit_id[i]);
8206 snprintf(trace_mod, MAX_COLNAME,
"b%d_mod", slit_id[i]);
8209 snprintf(trace_id, MAX_COLNAME,
"t%d", slit_id[i]);
8210 snprintf(trace_res, MAX_COLNAME,
"t%d_res", slit_id[i]);
8211 snprintf(trace_mod, MAX_COLNAME,
"t%d_mod", slit_id[i]);
8214 cpl_table_set_int(polytraces,
"slit_id", 2*i+j, slit_id[i]);
8221 dummy = cpl_table_new(nrows);
8222 cpl_table_duplicate_column(dummy,
"x", traces,
"x");
8223 cpl_table_duplicate_column(dummy, trace_id, traces, trace_id);
8224 npoints = nrows - cpl_table_count_invalid(dummy, trace_id);
8225 if (npoints < 2 * order) {
8226 cpl_table_delete(dummy);
8229 cpl_table_erase_invalid(dummy);
8230 x = cpl_vector_wrap(npoints,
8231 cpl_table_get_data_double(dummy,
"x"));
8232 trace = cpl_vector_wrap(npoints,
8233 cpl_table_get_data_double(dummy, trace_id));
8234 polytrace = cpl_polynomial_fit_1d_create(x, trace, order, NULL);
8235 cpl_vector_unwrap(x);
8236 cpl_vector_unwrap(trace);
8237 cpl_table_delete(dummy);
8246 if (fabs(cpl_polynomial_get_coeff(polytrace, &k)) > 1.E-4) {
8247 cpl_polynomial_delete(polytrace);
8248 cpl_table_new_column(traces, trace_mod, CPL_TYPE_DOUBLE);
8249 cpl_table_duplicate_column(traces, trace_res, traces,
8252 cpl_msg_warning(func,
"Exclude bad curvature solution "
8253 "for bottom (right) edge of slit %d", slit_id[i]);
8255 cpl_msg_warning(func,
"Exclude bad curvature solution "
8256 "for top (left) edge of slit %d", slit_id[i]);
8265 for (k = 0; k <= order; k++)
8266 cpl_table_set_double(polytraces, clab[k], 2*i+j,
8267 cpl_polynomial_get_coeff(polytrace, &k));
8273 cpl_table_new_column(traces, trace_mod, CPL_TYPE_DOUBLE);
8274 cpl_table_set_column_unit(traces, trace_mod,
"pixel");
8276 for (k = 0; k < nrows; k++) {
8277 cpl_table_set_double(traces, trace_mod, k,
8278 cpl_polynomial_eval_1d(polytrace, xdata[k], NULL));
8281 cpl_polynomial_delete(polytrace);
8283 cpl_table_duplicate_column(traces, trace_res, traces, trace_mod);
8284 cpl_table_subtract_columns(traces, trace_res, trace_id);
8285 cpl_table_multiply_scalar(traces, trace_res, -1.0);
8321 const char *func =
"mos_global_trace";
8323 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8336 int order, nrows, nslits;
8340 if (polytraces == NULL) {
8341 cpl_msg_error(func,
"Missing spectral curvature table");
8342 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8345 if (slits == NULL) {
8346 cpl_msg_error(func,
"Missing slits positions table");
8347 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8350 nslits = cpl_table_get_nrow(slits);
8352 table = cpl_table_duplicate(polytraces);
8353 cpl_table_erase_invalid(table);
8355 nrows = cpl_table_get_nrow(table);
8358 cpl_msg_warning(func,
"Too few successful spectral curvature tracings "
8359 "(%d): the determination of a global curvature model "
8361 return CPL_ERROR_NONE;
8364 order = cpl_table_get_ncol(polytraces) - 2;
8366 for (i = 0; i <= order; i++) {
8367 if (!cpl_table_has_column(table, clab[i])) {
8368 cpl_msg_error(func,
"Wrong spectral curvature table");
8369 return cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8378 for (i = 0; i < nslits; i++) {
8379 if (!cpl_table_is_valid(polytraces, clab[0], 2*i)) {
8380 cpl_table_set_double(polytraces, clab[0], 2*i,
8381 cpl_table_get_double(slits,
"ytop", i, NULL));
8383 if (!cpl_table_is_valid(polytraces, clab[0], 2*i+1)) {
8384 cpl_table_set_double(polytraces, clab[0], 2*i+1,
8385 cpl_table_get_double(slits,
"ybottom", i, NULL));
8389 offset = cpl_table_get_data_double(polytraces, clab[0]);
8396 c0 = cpl_vector_wrap(nrows, cpl_table_get_data_double(table, clab[0]));
8398 for (i = 1; i <= order; i++) {
8399 cn = cpl_vector_wrap(nrows, cpl_table_get_data_double(table, clab[i]));
8400 list = cpl_bivector_wrap_vectors(c0, cn);
8401 robustLinearFit(list, &q, &m, &rms);
8405 for (j = 0; j < 2*nslits; j++) {
8407 if (cpl_table_is_valid(polytraces, clab[i], j))
8409 cpl_table_set_double(polytraces, clab[i], j, offset[j]*m + q);
8415 cpl_bivector_unwrap_vectors(list);
8419 cpl_vector_unwrap(cn);
8422 cpl_vector_unwrap(c0);
8423 cpl_table_delete(table);
8425 return CPL_ERROR_NONE;
8500 cpl_table *polytraces,
double reference,
8501 double blue,
double red,
double dispersion,
8502 int flux, cpl_image *calibration)
8504 const char *func =
"mos_spatial_calibration";
8506 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8508 cpl_polynomial *polytop;
8509 cpl_polynomial *polybot;
8511 cpl_image *resampled;
8515 double vtop, vbot, value;
8521 int yint, ysize, yprev;
8527 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
8528 int missing_top, missing_bot;
8534 int create_position = 1;
8537 if (spectra == NULL || slits == NULL || polytraces == NULL) {
8538 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8542 if (dispersion <= 0.0) {
8543 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8547 if (red - blue < dispersion) {
8548 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8552 nx = cpl_image_get_size_x(spectra);
8553 ny = cpl_image_get_size_y(spectra);
8554 sdata = cpl_image_get_data(spectra);
8556 data = cpl_image_get_data(calibration);
8558 if (cpl_table_has_column(slits,
"position"))
8559 create_position = 0;
8561 if (create_position) {
8562 cpl_table_new_column(slits,
"position", CPL_TYPE_INT);
8563 cpl_table_new_column(slits,
"length", CPL_TYPE_INT);
8564 cpl_table_set_column_unit(slits,
"position",
"pixel");
8565 cpl_table_set_column_unit(slits,
"length",
"pixel");
8568 length = cpl_table_get_data_int(slits,
"length");
8570 nslits = cpl_table_get_nrow(slits);
8571 slit_id = cpl_table_get_data_int(slits,
"slit_id");
8572 order = cpl_table_get_ncol(polytraces) - 2;
8579 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
8580 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
8582 exslit = cpl_calloc(nslits,
sizeof(cpl_image *));
8584 for (i = 0; i < nslits; i++) {
8586 if (create_position == 0)
8601 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
8603 start_pixel = refpixel - pixel_below;
8604 if (start_pixel < 0)
8607 end_pixel = refpixel + pixel_above;
8617 polytop = cpl_polynomial_new(1);
8618 for (k = 0; k <= order; k++) {
8619 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
8621 cpl_polynomial_delete(polytop);
8625 cpl_polynomial_set_coeff(polytop, &k, coeff);
8629 polybot = cpl_polynomial_new(1);
8630 for (k = 0; k <= order; k++) {
8631 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
8633 cpl_polynomial_delete(polybot);
8637 cpl_polynomial_set_coeff(polybot, &k, coeff);
8640 if (missing_top && missing_bot) {
8641 cpl_msg_warning(func,
"Spatial calibration, slit %d was not "
8642 "traced: no extraction!",
8654 cpl_msg_warning(func,
"Upper edge of slit %d was not traced: "
8655 "the spectral curvature of the lower edge "
8656 "is used instead.", slit_id[i]);
8657 polytop = cpl_polynomial_duplicate(polybot);
8658 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
8659 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
8661 coeff = cpl_polynomial_get_coeff(polybot, &k);
8662 coeff += ytop - ybot;
8663 cpl_polynomial_set_coeff(polytop, &k, coeff);
8667 cpl_msg_warning(func,
"Lower edge of slit %d was not traced: "
8668 "the spectral curvature of the upper edge "
8669 "is used instead.", slit_id[i]);
8670 polybot = cpl_polynomial_duplicate(polytop);
8671 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
8672 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
8674 coeff = cpl_polynomial_get_coeff(polytop, &k);
8675 coeff -= ytop - ybot;
8676 cpl_polynomial_set_coeff(polybot, &k, coeff);
8683 top = cpl_polynomial_eval_1d(polytop, refpixel, NULL);
8684 bot = cpl_polynomial_eval_1d(polybot, refpixel, NULL);
8685 npseudo = ceil(top-bot) + 1;
8688 cpl_polynomial_delete(polytop);
8689 cpl_polynomial_delete(polybot);
8690 cpl_msg_warning(func,
"Slit %d was badly traced: no extraction!",
8695 exslit[i] = cpl_image_new(nx, npseudo+1, CPL_TYPE_FLOAT);
8696 xdata = cpl_image_get_data(exslit[i]);
8702 for (j = start_pixel; j < end_pixel; j++) {
8703 top = cpl_polynomial_eval_1d(polytop, j, NULL);
8704 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
8705 factor = (top-bot)/npseudo;
8706 for (k = 0; k <= npseudo; k++) {
8707 ypos = top - k*factor;
8710 if (yint >= 0 && yint < ny-1) {
8711 vtop = sdata[j + nx*yint];
8712 vbot = sdata[j + nx*(yint+1)];
8718 else if(vtop == FLT_MAX || vbot == FLT_MAX)
8722 value = vtop*(1-yfra) + vbot*yfra;
8726 xdata[j + nx*(npseudo-k)] = value;
8728 data[j + nx*yint] = (top-yint)/factor;
8737 if (yprev - yint > 1) {
8738 data[j + nx*(yint+1)] = (top-yint-1)/factor;
8746 cpl_polynomial_delete(polytop);
8747 cpl_polynomial_delete(polybot);
8755 for (i = 0; i < nslits; i++)
8757 ysize += cpl_image_get_size_y(exslit[i]);
8762 resampled = cpl_image_new(nx, ysize, CPL_TYPE_FLOAT);
8765 for (i = 0; i < nslits; i++) {
8767 yint += cpl_image_get_size_y(exslit[i]);
8768 cpl_image_copy(resampled, exslit[i], 1, ysize - yint);
8769 if (create_position) {
8770 cpl_table_set_int(slits,
"position", i, ysize - yint - 1);
8771 cpl_table_set_int(slits,
"length", i,
8772 cpl_image_get_size_y(exslit[i]));
8774 cpl_image_delete(exslit[i]);
8776 else if (create_position) {
8777 cpl_table_set_int(slits,
"position", i, -1);
8778 cpl_table_set_int(slits,
"length", i, 0);
8919 double dispersion,
float level,
8920 int sradius,
int order,
8921 double reject,
double refwave,
8922 double *wavestart,
double *waveend,
8923 int *nlines,
double *error,
8924 cpl_table *idscoeff,
8925 cpl_image *calibration,
8926 cpl_image *residuals,
8927 cpl_table *restable,
8928 cpl_table *detected_lines)
8931 const char *func =
"mos_wavelength_calibration_final";
8933 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8936 double tolerance = 20.0;
8939 char name[MAX_COLNAME];
8941 cpl_image *resampled;
8942 cpl_bivector *peaks_ident;
8945 cpl_polynomial *ids;
8946 cpl_polynomial *lin;
8947 cpl_polynomial *fguess;
8950 double max_disp, min_disp;
8952 double firstLambda, lastLambda, lambda;
8953 double wave, pixe, value;
8962 int pixstart, pixend;
8963 int row_top, row_bot;
8968 int nl, nx, ny, pixel;
8969 int countLines, usedLines;
8978 if (dispersion == 0.0) {
8979 cpl_msg_error(func,
"The expected dispersion (A/pixel) must be given");
8980 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8984 if (dispersion < 0.0) {
8985 cpl_msg_error(func,
"The expected dispersion must be positive");
8986 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8990 if (idscoeff == NULL) {
8991 cpl_msg_error(func,
"A preallocated IDS coeff table must be given");
8992 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8996 max_disp = dispersion + dispersion * tolerance / 100;
8997 min_disp = dispersion - dispersion * tolerance / 100;
9000 cpl_msg_error(func,
"The order of the fitting polynomial "
9001 "must be at least 1");
9002 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9006 if (image == NULL || lines == NULL) {
9007 cpl_msg_error(func,
"Both spectral exposure and reference line "
9008 "catalog are required in input");
9009 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9013 nx = cpl_image_get_size_x(image);
9014 ny = cpl_image_get_size_y(image);
9015 sdata = cpl_image_get_data_float(image);
9017 nref = cpl_vector_get_size(lines);
9018 line = cpl_vector_get_data(lines);
9020 if (*wavestart < 1.0 && *waveend < 1.0) {
9021 firstLambda = line[0];
9022 lastLambda = line[nref-1];
9023 extrapolation = (lastLambda - firstLambda) / 10;
9024 firstLambda -= extrapolation;
9025 lastLambda += extrapolation;
9026 *wavestart = firstLambda;
9027 *waveend = lastLambda;
9030 firstLambda = *wavestart;
9031 lastLambda = *waveend;
9034 nl = (lastLambda - firstLambda) / dispersion;
9035 resampled = cpl_image_new(nl, ny, CPL_TYPE_FLOAT);
9036 rdata = cpl_image_get_data_float(resampled);
9042 for (j = 0; j <= order; j++)
9043 cpl_table_new_column(idscoeff, clab[j], CPL_TYPE_DOUBLE);
9046 idata = cpl_image_get_data_float(calibration);
9049 ddata = cpl_image_get_data_float(residuals);
9052 cpl_table_set_size(restable, nref);
9053 cpl_table_new_column(restable,
"wavelength", CPL_TYPE_DOUBLE);
9054 cpl_table_copy_data_double(restable,
"wavelength", line);
9055 for (i = 0; i < ny; i += step) {
9056 snprintf(name, MAX_COLNAME,
"r%d", i);
9057 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
9058 snprintf(name, MAX_COLNAME,
"d%d", i);
9059 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
9060 snprintf(name, MAX_COLNAME,
"p%d", i);
9061 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
9065 if (detected_lines) {
9066 cpl_table_set_size(detected_lines, 0);
9067 cpl_table_new_column(detected_lines,
"slit_id", CPL_TYPE_INT);
9068 cpl_table_new_column(detected_lines,
"xpos_rectified", CPL_TYPE_DOUBLE);
9069 cpl_table_new_column(detected_lines,
"ypos_rectified", CPL_TYPE_DOUBLE);
9070 cpl_table_new_column(detected_lines,
"xpos_rectified_iter", CPL_TYPE_DOUBLE);
9071 cpl_table_new_column(detected_lines,
"ypos_rectified_iter", CPL_TYPE_DOUBLE);
9072 cpl_table_new_column(detected_lines,
"peak_flux", CPL_TYPE_DOUBLE);
9073 cpl_table_new_column(detected_lines,
"wave_ident", CPL_TYPE_DOUBLE);
9074 cpl_table_new_column(detected_lines,
"wave_ident_iter", CPL_TYPE_DOUBLE);
9075 cpl_table_new_column(detected_lines,
"xpos_fit_rect_wavecal", CPL_TYPE_DOUBLE);
9076 cpl_table_new_column(detected_lines,
"res_xpos", CPL_TYPE_DOUBLE);
9084 nslits = cpl_table_get_nrow(slits);
9085 length = cpl_table_get_data_int(slits,
"length");
9088 for (s = 0; s < nslits; s++) {
9091 slit_id = cpl_table_get_int(slits,
"slit_id", s, NULL);
9101 row_bot = cpl_table_get_int(slits,
"position", s, NULL);
9113 coeff = cpl_table_new(row_top - row_bot);
9114 for (j = 0; j <= order; j++)
9115 cpl_table_new_column(coeff, clab[j], CPL_TYPE_DOUBLE);
9123 for (i = row_bot; i < row_top; i++) {
9132 int keep_multiplex = mos_multiplex;
9136 cpl_size newlines = cpl_vector_get_size(peaks);
9137 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
9138 cpl_table_set_size(detected_lines, oldsize + newlines);
9139 for(cpl_size iline = 0; iline < newlines; ++iline)
9141 cpl_table_set_int(detected_lines,
"slit_id",
9142 oldsize + iline, slit_id);
9143 cpl_table_set_double(detected_lines,
"xpos_rectified",
9144 oldsize + iline, cpl_vector_get(peaks, iline) + 1);
9145 cpl_table_set_double(detected_lines,
"ypos_rectified",
9146 oldsize + iline, (
double)i + 1);
9147 cpl_table_set_double(detected_lines,
"peak_flux",
9149 sdata[i*nx+(
int)(cpl_vector_get(peaks, iline)+0.5)]);
9153 min_disp, max_disp, 0.05);
9154 mos_multiplex = keep_multiplex;
9156 countLines = cpl_bivector_get_size(peaks_ident);
9157 if (countLines < 4) {
9158 cpl_bivector_delete(peaks_ident);
9159 cpl_vector_delete(peaks);
9171 wavel = cpl_bivector_get_y(peaks_ident);
9172 cpl_vector_subtract_scalar(wavel, refwave);
9174 uorder = countLines / 2 - 1;
9179 2 * (uorder + 1), &usedLines,
9183 cpl_bivector_delete(peaks_ident);
9184 cpl_vector_delete(peaks);
9194 for (k = 0; k <= order; k++) {
9196 cpl_table_set_double(coeff, clab[k],
9200 cpl_table_set_double(coeff, clab[k],
9201 i - row_bot, cpl_polynomial_get_coeff(ids, &k));
9207 pixstart = cpl_polynomial_eval_1d(ids,
9208 cpl_bivector_get_y_data(peaks_ident)[0],
9210 pixend = cpl_polynomial_eval_1d(ids,
9211 cpl_bivector_get_y_data(peaks_ident)[countLines-1],
9213 extrapolation = (pixend - pixstart) / 5;
9214 pixstart -= extrapolation;
9215 pixend += extrapolation;
9221 for (j = pixstart; j < pixend; j++) {
9223 firstLambda, lastLambda, refwave, j);
9231 if (residuals || (restable && !(i%step))) {
9232 if (restable && !(i%step)) {
9233 lin = cpl_polynomial_new(1);
9234 for (k = 0; k < 2; k++)
9235 cpl_polynomial_set_coeff(lin, &k,
9236 cpl_polynomial_get_coeff(ids, &k));
9238 for (j = 0; j < countLines; j++) {
9239 pixe = cpl_bivector_get_x_data(peaks_ident)[j];
9240 wave = cpl_bivector_get_y_data(peaks_ident)[j];
9242 - cpl_polynomial_eval_1d(ids, wave, NULL);
9245 (ddata + i*nx)[pixel] = value;
9247 if (restable && !(i%step)) {
9248 for (k = 0; k < nref; k++) {
9249 if (fabs(line[k]-refwave-wave) < 0.1) {
9250 snprintf(name, MAX_COLNAME,
9252 cpl_table_set_double(restable, name,
9255 - cpl_polynomial_eval_1d(lin,
9257 snprintf(name, MAX_COLNAME,
9259 cpl_table_set_double(restable, name,
9261 snprintf(name, MAX_COLNAME,
9263 cpl_table_set_double(restable, name,
9270 if (restable && !(i%step)) {
9271 cpl_polynomial_delete(lin);
9288 cpl_size nidentlines = cpl_bivector_get_size(peaks_ident);
9289 cpl_size ndetectlines = cpl_vector_get_size(peaks);
9290 cpl_size totalsize = cpl_table_get_nrow(detected_lines);
9291 for(cpl_size idline = 0; idline < nidentlines; ++idline)
9293 for(cpl_size detline = 0; detline < ndetectlines; ++detline)
9295 if(cpl_vector_get(peaks, detline) ==
9296 cpl_bivector_get_x_data(peaks_ident)[idline])
9298 cpl_size table_pos = totalsize - ndetectlines + detline;
9299 double wave_ident = cpl_bivector_get_y_data(peaks_ident)[idline] + refwave;
9300 double xpix_fit = cpl_polynomial_eval_1d(ids,
9301 wave_ident - refwave, NULL);
9302 double xpos_det = cpl_table_get_double(detected_lines,
9305 cpl_table_set_double(detected_lines,
9309 cpl_table_set_double(detected_lines,
9310 "xpos_fit_rect_wavecal",
9313 cpl_table_set_double(detected_lines,
9316 xpos_det - xpix_fit - 1);
9332 nlines[i] = usedLines;
9334 error[i] = ids_err / sqrt(usedLines/(uorder + 1));
9336 for (k = 0; k <= order; k++) {
9338 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
9341 cpl_table_set_double(idscoeff, clab[k], i,
9342 cpl_polynomial_get_coeff(ids, &k));
9346 cpl_polynomial_delete(ids);
9347 cpl_bivector_delete(peaks_ident);
9349 cpl_vector_delete(peaks);
9360 nfits = row_top - row_bot - cpl_table_count_invalid(coeff, clab[0]);
9365 fguess = cpl_polynomial_new(1);
9375 for (k = 0; k <= order; k++) {
9376 c = cpl_table_get_column_median(coeff, clab[k]);
9377 cpl_polynomial_set_coeff(fguess, &k, c);
9384 for (i = row_bot; i < row_top; i++) {
9391 if (width > sradius) {
9399 for (k = 0; k <= order; k++) {
9400 c = cpl_table_get_double(coeff, clab[k],
9402 cpl_polynomial_set_coeff(fguess, &k, c);
9407 fguess, refwave, uradius);
9409 if (peaks_ident == NULL) {
9414 countLines = cpl_bivector_get_size(peaks_ident);
9416 if (countLines < 4) {
9417 cpl_bivector_delete(peaks_ident);
9425 wavel = cpl_bivector_get_y(peaks_ident);
9426 cpl_vector_subtract_scalar(wavel, refwave);
9428 uorder = countLines / 2 - 1;
9433 2 * (uorder + 1), &usedLines,
9438 cpl_bivector_delete(peaks_ident);
9443 nlines[i] = usedLines;
9445 error[i] = ids_err / sqrt(usedLines/(uorder + 1));
9448 pixstart = cpl_polynomial_eval_1d(ids,
9449 cpl_bivector_get_y_data(peaks_ident)[0],
9451 pixend = cpl_polynomial_eval_1d(ids,
9452 cpl_bivector_get_y_data(peaks_ident)[countLines-1],
9454 extrapolation = (pixend - pixstart) / 5;
9455 pixstart -= extrapolation;
9456 pixend += extrapolation;
9462 for (j = pixstart; j < pixend; j++) {
9464 firstLambda, lastLambda, refwave, j);
9472 if (residuals || (restable && !(i%step))) {
9473 if (restable && !(i%step)) {
9474 lin = cpl_polynomial_new(1);
9475 for (k = 0; k < 2; k++)
9476 cpl_polynomial_set_coeff(lin, &k,
9477 cpl_polynomial_get_coeff(ids, &k));
9479 for (j = 0; j < countLines; j++) {
9480 pixe = cpl_bivector_get_x_data(peaks_ident)[j];
9481 wave = cpl_bivector_get_y_data(peaks_ident)[j];
9483 - cpl_polynomial_eval_1d(ids, wave, NULL);
9486 (ddata + i*nx)[pixel] = value;
9488 if (restable && !(i%step)) {
9489 for (k = 0; k < nref; k++) {
9490 if (fabs(line[k]-refwave-wave) < 0.1) {
9491 snprintf(name, MAX_COLNAME,
9493 cpl_table_set_double(restable, name,
9496 - cpl_polynomial_eval_1d(lin,
9498 snprintf(name, MAX_COLNAME,
9500 cpl_table_set_double(restable, name,
9502 snprintf(name, MAX_COLNAME,
9504 cpl_table_set_double(restable, name,
9511 if (restable && !(i%step)) {
9512 cpl_polynomial_delete(lin);
9529 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
9530 cpl_size nidentlines = cpl_bivector_get_size(peaks_ident);
9531 cpl_table_set_size(detected_lines, oldsize + nidentlines);
9532 for(cpl_size idline = 0; idline < nidentlines ; ++idline)
9534 double wave_ident = cpl_bivector_get_y_data(peaks_ident)[idline] + refwave;
9535 double xpix_fit = cpl_polynomial_eval_1d(ids,
9536 wave_ident - refwave, NULL);
9537 cpl_table_set_int(detected_lines,
"slit_id",
9538 oldsize + idline, slit_id);
9539 cpl_table_set_double(detected_lines,
"xpos_rectified_iter",
9540 oldsize + idline, cpl_bivector_get_x_data(peaks_ident)[idline] + 1);
9541 cpl_table_set_double(detected_lines,
"ypos_rectified_iter",
9542 oldsize + idline, (
double)i + 1);
9543 cpl_table_set_double(detected_lines,
"peak_flux",
9545 sdata[i*nx+(
int)(cpl_bivector_get_x_data(peaks_ident)[idline]+0.5)]);
9546 cpl_table_set_double(detected_lines,
"wave_ident_iter",
9547 oldsize + idline, wave_ident);
9548 cpl_table_set_double(detected_lines,
"xpos_fit_rect_wavecal",
9549 oldsize + idline, xpix_fit + 1);
9554 for (k = 0; k <= order; k++) {
9556 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
9559 cpl_table_set_double(idscoeff, clab[k], i,
9560 cpl_polynomial_get_coeff(ids, &k));
9564 cpl_bivector_delete(peaks_ident);
9565 cpl_polynomial_delete(ids);
9569 cpl_polynomial_delete(fguess);
9572 cpl_table_delete(coeff);
9588 for (i = 0; i < ny; i++) {
9591 ids = cpl_polynomial_new(1);
9592 for (k = 0; k <= order; k++) {
9593 c = cpl_table_get_double(idscoeff, clab[k], i, &null);
9595 cpl_polynomial_delete(ids);
9599 cpl_polynomial_set_coeff(ids, &k, c);
9604 pixstart = cpl_polynomial_eval_1d(ids, firstLambda - refwave, NULL);
9605 pixend = cpl_polynomial_eval_1d(ids, lastLambda - refwave, NULL);
9615 for (j = 0; j < nl; j++) {
9616 lambda = firstLambda + j * dispersion;
9617 fpixel = cpl_polynomial_eval_1d(ids, lambda - refwave, NULL);
9619 if (pixel >= 0 && pixel < nx-1) {
9620 v1 = (sdata + i*nx)[pixel];
9621 v2 = (sdata + i*nx)[pixel+1];
9622 vi = v1 + (v2-v1)*(fpixel-pixel);
9623 (rdata + i*nl)[j] = vi;
9627 cpl_polynomial_delete(ids);
9661 double firstLambda,
double lastLambda,
9662 double dispersion, cpl_table *idscoeff,
9666 const char *func =
"mos_wavelength_calibration";
9668 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
9671 cpl_image *resampled;
9672 cpl_polynomial *ids;
9673 double pixel_per_lambda;
9678 float v0, v1, v2, v3, vi;
9681 int pixstart, pixend;
9682 int nl, nx, ny, pixel;
9689 if (dispersion <= 0.0) {
9690 cpl_msg_error(func,
"The resampling step must be positive");
9691 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9695 if (lastLambda - firstLambda < dispersion) {
9696 cpl_msg_error(func,
"Invalid spectral range: %.2f to %.2f",
9697 firstLambda, lastLambda);
9698 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9702 if (idscoeff == NULL) {
9703 cpl_msg_error(func,
"An IDS coeff table must be given");
9704 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9708 if (image == NULL) {
9709 cpl_msg_error(func,
"A scientific spectral image must be given");
9710 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9714 nx = cpl_image_get_size_x(image);
9715 ny = cpl_image_get_size_y(image);
9716 sdata = cpl_image_get_data_float(image);
9718 nl = (lastLambda - firstLambda) / dispersion;
9719 resampled = cpl_image_new(nl, ny, CPL_TYPE_FLOAT);
9720 rdata = cpl_image_get_data_float(resampled);
9723 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
9727 for (i = 0; i < ny; i++) {
9730 ids = cpl_polynomial_new(1);
9731 for (k = 0; k <= order; k++) {
9732 c = cpl_table_get_double(idscoeff, clab[k], i, &null);
9734 cpl_polynomial_delete(ids);
9738 cpl_polynomial_set_coeff(ids, &k, c);
9743 pixstart = cpl_polynomial_eval_1d(ids, firstLambda - refwave, NULL);
9744 pixend = cpl_polynomial_eval_1d(ids, lastLambda - refwave, NULL);
9754 for (j = 0; j < nl; j++) {
9755 lambda = firstLambda + j * dispersion;
9756 fpixel = cpl_polynomial_eval_1d(ids, lambda - refwave,
9778 if(pixel_per_lambda <= 0)
9780 else if (fpixel < 0)
9782 else if (pixel >= 1 && pixel < nx-2) {
9783 v0 = (sdata + i*nx)[pixel-1];
9784 v1 = (sdata + i*nx)[pixel];
9785 v2 = (sdata + i*nx)[pixel+1];
9786 v3 = (sdata + i*nx)[pixel+2];
9787 vi = (fpixel-pixel)*(fpixel-pixel)*(v3 - v2 - v1 + v0)
9788 + (fpixel-pixel)*(3*v2 - v3 - v1 - v0)
9808 vi *= dispersion * pixel_per_lambda;
9810 else if (pixel >= 0 && pixel < nx-1) {
9811 v1 = (sdata + i*nx)[pixel];
9812 v2 = (sdata + i*nx)[pixel+1];
9813 vi = v1 + (v2-v1)*(fpixel-pixel);
9815 vi *= dispersion * pixel_per_lambda;
9819 (rdata + i*nl)[j] = vi;
9832 double spos = fpixel - dispersion * pixel_per_lambda / 2;
9833 double epos = fpixel + dispersion * pixel_per_lambda / 2;
9840 int epix = epos + 1;
9849 for (k = spix; k < epix; k++) {
9850 if (pixel >= 0 && pixel < nx) {
9851 vi += (sdata + i*nx)[k];
9862 vi *= dispersion * pixel_per_lambda / (epix - spix);
9870 vi *= dispersion * pixel_per_lambda;
9872 (rdata + i*nl)[j] = vi;
9876 cpl_polynomial_delete(ids);
9950 double refwave,
double firstLambda,
9951 double lastLambda, cpl_table *idscoeff,
9952 cpl_vector *skylines,
int highres,
int order,
9953 cpl_image *calibration,
int sradius)
9955 const char *func =
"mos_wavelength_align";
9957 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
9961 double expPos, offset;
9963 double lambda1, lambda2;
9969 int startPos, endPos;
9970 int window = 2*sradius + 1;
9976 int xlow, ylow, xhig, yhig;
9977 int idsorder, uorder;
9986 char offname[MAX_COLNAME];
9987 char name[MAX_COLNAME];
9989 cpl_polynomial *ids;
9990 cpl_polynomial *polycorr;
9999 if (idscoeff == NULL) {
10000 cpl_msg_error(func,
"An IDS coeff table must be given");
10001 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10005 if (image == NULL) {
10006 cpl_msg_error(func,
"A scientific spectral image must be given");
10007 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10011 if (slits == NULL) {
10012 cpl_msg_error(func,
"A slit position table must be given");
10013 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10018 line = cpl_vector_get_data(skylines);
10019 nlines = cpl_vector_get_size(skylines);
10022 cpl_msg_warning(func,
"A catalog of sky lines wavelengths was not "
10023 "given: using internal list of reference sky lines");
10025 line = default_lines_hi;
10026 nlines =
sizeof(default_lines_hi) /
sizeof(
double);
10029 line = default_lines_lo;
10030 nlines =
sizeof(default_lines_lo) /
sizeof(
double);
10035 cdata = cpl_image_get_data(calibration);
10037 nx = cpl_image_get_size_x(image);
10038 ny = cpl_image_get_size_y(image);
10040 nslits = cpl_table_get_nrow(slits);
10041 slit_id = cpl_table_get_data_int(slits,
"slit_id");
10042 position = cpl_table_get_data_int(slits,
"position");
10043 length = cpl_table_get_data_int(slits,
"length");
10051 for (i = 0; i < nlines; i++)
10052 if (line[i] > firstLambda && line[i] < lastLambda)
10055 offsets = cpl_table_new(nrows);
10056 cpl_table_new_column(offsets,
"wave", CPL_TYPE_DOUBLE);
10057 cpl_table_set_column_unit(offsets,
"wave",
"Angstrom");
10060 for (i = 0; i < nlines; i++) {
10061 if (line[i] > firstLambda && line[i] < lastLambda) {
10062 cpl_table_set_double(offsets,
"wave", nrows, line[i]);
10071 line = cpl_table_get_data_double(offsets,
"wave");
10075 while (idsorder < 6 && cpl_table_has_column(idscoeff, clab[idsorder]))
10081 for (i = 0; i < nslits; i++) {
10083 if (length[i] == 0)
10086 snprintf(offname, MAX_COLNAME,
"offset%d", slit_id[i]);
10087 cpl_table_new_column(offsets, offname, CPL_TYPE_DOUBLE);
10099 ylow = position[i] + 1;
10100 yhig = ylow + length[i] - 1;
10102 exslit = cpl_image_extract(image, xlow, ylow, xhig, yhig);
10103 sky = cpl_image_collapse_median_create(exslit, 0, 0, 1);
10104 sdata = cpl_image_get_data(sky);
10106 cpl_image_delete(exslit);
10121 dummy = cpl_table_new(yhig - ylow);
10122 for (j = 0; j < nlines; j++) {
10123 snprintf(name, MAX_COLNAME,
"%"CPL_SIZE_FORMAT, j);
10124 cpl_table_new_column(dummy, name, CPL_TYPE_DOUBLE);
10127 for (j = ylow; j < yhig; j++) {
10134 ids = cpl_polynomial_new(1);
10135 for (k = 0; k <= idsorder; k++) {
10136 c = cpl_table_get_double(idscoeff, clab[k], j, &null);
10138 cpl_polynomial_delete(ids);
10142 cpl_polynomial_set_coeff(ids, &k, c);
10147 for (k = 0; k < nlines; k++) {
10148 expPos = cpl_polynomial_eval_1d(ids, line[k] - refwave, NULL);
10149 startPos = expPos - sradius;
10150 endPos = startPos + window;
10151 if (startPos < 0 || endPos >= nx)
10154 if (0 == peakPosition(sdata + startPos, window, &pos, 1)) {
10156 offset = pos - expPos;
10157 snprintf(name, MAX_COLNAME,
"%"CPL_SIZE_FORMAT, k);
10158 cpl_table_set_double(dummy, name, j - ylow, offset);
10162 cpl_polynomial_delete(ids);
10165 cpl_image_delete(sky);
10167 for (j = 0; j < nlines; j++) {
10168 snprintf(name, MAX_COLNAME,
"%"CPL_SIZE_FORMAT, j);
10169 if (cpl_table_has_valid(dummy, name)) {
10170 offset = cpl_table_get_column_median(dummy, name);
10171 cpl_table_set_double(offsets, offname, j, offset);
10175 cpl_table_delete(dummy);
10186 for (i = 0; i < nslits; i++) {
10188 if (length[i] == 0)
10191 snprintf(offname, MAX_COLNAME,
"offset%d", slit_id[i]);
10198 dummy = cpl_table_new(nlines);
10199 cpl_table_duplicate_column(dummy,
"wave", offsets,
"wave");
10200 cpl_table_duplicate_column(dummy,
"offset", offsets, offname);
10202 npoints = nlines - cpl_table_count_invalid(dummy,
"offset");
10203 if (npoints == 0) {
10204 cpl_msg_warning(func,
"No sky lines alignment was possible "
10205 "for slit ID=%d: no sky line found", slit_id[i]);
10206 cpl_table_delete(dummy);
10211 if (npoints <= uorder) {
10212 uorder = npoints - 1;
10214 cpl_msg_warning(func,
"Just %d sky lines detected for slit "
10215 "ID=%d, while a polynomial order %d was "
10216 "requested. Using polynomial order %d for "
10217 "this slit!", npoints, slit_id[i], order,
10221 cpl_msg_warning(func,
"Just %d sky lines detected for slit "
10222 "ID=%d, while a polynomial order %d was "
10223 "requested. Computing a median offset for "
10224 "this slit!", npoints, slit_id[i], order);
10228 cpl_table_erase_invalid(dummy);
10236 wave = cpl_vector_wrap(npoints,
10237 cpl_table_get_data_double(dummy,
"wave"));
10238 offs = cpl_vector_wrap(npoints,
10239 cpl_table_get_data_double(dummy,
"offset"));
10245 cpl_vector_subtract_scalar(wave, refwave);
10247 polycorr = cpl_polynomial_fit_1d_create(wave, offs, uorder, &rms);
10249 rms = sqrt(rms * (uorder + 1) / npoints);
10251 cpl_vector_unwrap(wave);
10252 cpl_vector_unwrap(offs);
10253 cpl_table_delete(dummy);
10260 ylow = position[i];
10261 yhig = ylow + length[i];
10263 for (j = 0; j <= uorder; j++) {
10264 data = cpl_table_get_data_double(idscoeff, clab[j]);
10265 c = cpl_polynomial_get_coeff(polycorr, &j);
10266 for (k = ylow; k < yhig; k++)
10270 data = cpl_table_get_data_double(idscoeff,
"error");
10271 for (k = ylow; k < yhig; k++)
10272 data[k] = sqrt(data[k]*data[k] + rms*rms);
10274 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10275 for (k = ylow; k < yhig; k++)
10276 idata[k] = npoints;
10284 for (j = ylow; j < yhig; j++) {
10285 for (k = 1; k < nx; k++) {
10286 lambda1 = cdata[k - 1 + j*nx];
10287 lambda2 = cdata[k + j*nx];
10288 if (lambda1 < 1.0 || lambda2 < 1.0)
10290 offset = cpl_polynomial_eval_1d(polycorr,
10291 lambda1-refwave, NULL);
10292 cdata[k - 1 + j*nx] -= offset * (lambda2-lambda1);
10297 cpl_polynomial_delete(polycorr);
10299 else if (uorder == 1) {
10306 cpl_bivector *list;
10309 wave = cpl_vector_wrap(npoints,
10310 cpl_table_get_data_double(dummy,
"wave"));
10311 offs = cpl_vector_wrap(npoints,
10312 cpl_table_get_data_double(dummy,
"offset"));
10314 list = cpl_bivector_wrap_vectors(wave, offs);
10320 cpl_vector_subtract_scalar(wave, refwave);
10322 robustLinearFit(list, &q, &m, &rms);
10324 rms = sqrt(rms * (uorder + 1) / npoints);
10326 cpl_bivector_unwrap_vectors(list);
10327 cpl_vector_unwrap(wave);
10328 cpl_vector_unwrap(offs);
10329 cpl_table_delete(dummy);
10336 ylow = position[i];
10337 yhig = ylow + length[i];
10339 for (j = 0; j <= uorder; j++) {
10340 data = cpl_table_get_data_double(idscoeff, clab[j]);
10345 for (k = ylow; k < yhig; k++)
10349 data = cpl_table_get_data_double(idscoeff,
"error");
10350 for (k = ylow; k < yhig; k++)
10351 data[k] = sqrt(data[k]*data[k] + rms*rms);
10353 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10354 for (k = ylow; k < yhig; k++)
10355 idata[k] = npoints;
10363 for (j = ylow; j < yhig; j++) {
10364 for (k = 1; k < nx; k++) {
10365 lambda1 = cdata[k - 1 + j*nx];
10366 lambda2 = cdata[k + j*nx];
10367 if (lambda1 < 1.0 || lambda2 < 1.0)
10369 offset = q + m*(lambda1-refwave);
10370 cdata[k - 1 + j*nx] -= offset * (lambda2-lambda1);
10381 offs = cpl_vector_wrap(npoints,
10382 cpl_table_get_data_double(dummy,
"offset"));
10384 offset = cpl_vector_get_median_const(offs);
10387 rms = cpl_table_get_column_stdev(dummy,
"offset");
10391 rms /= sqrt(npoints);
10393 cpl_vector_unwrap(offs);
10394 cpl_table_delete(dummy);
10401 ylow = position[i];
10402 yhig = ylow + length[i];
10404 data = cpl_table_get_data_double(idscoeff, clab[0]);
10405 for (k = ylow; k < yhig; k++)
10408 data = cpl_table_get_data_double(idscoeff,
"error");
10409 for (k = ylow; k < yhig; k++)
10410 data[k] = sqrt(data[k]*data[k] + rms*rms);
10412 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10413 for (k = ylow; k < yhig; k++)
10414 idata[k] = npoints;
10423 for (j = ylow; j < yhig; j++) {
10424 for (k = 1; k < nx; k++) {
10425 lambda1 = cdata[k - 1 + j*nx];
10426 lambda2 = cdata[k + j*nx];
10427 if (lambda1 < 1.0 || lambda2 < 1.0)
10429 cdata[k - 1 + j*nx] -= offset * (lambda2-lambda1);
10503 double firstLambda,
double lastLambda,
10504 cpl_table *idscoeff, cpl_vector *skylines,
10505 int highres,
int order,
10506 cpl_image *calibration,
int sradius)
10508 const char *func =
"mos_wavelength_align_lss";
10510 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
10516 double expPos, offset;
10518 double lambda1, lambda2;
10524 int startPos, endPos;
10525 int window = 2*sradius + 1;
10530 int idsorder, uorder;
10535 char name[MAX_COLNAME];
10536 char fname[MAX_COLNAME];
10538 cpl_polynomial *ids;
10539 cpl_polynomial *polycorr;
10540 cpl_table *offsets;
10541 cpl_table *fittable;
10548 if (idscoeff == NULL) {
10549 cpl_msg_error(func,
"An IDS coeff table must be given");
10550 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10554 if (image == NULL) {
10555 cpl_msg_error(func,
"A scientific spectral image must be given");
10556 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10561 line = cpl_vector_get_data(skylines);
10562 nlines = cpl_vector_get_size(skylines);
10565 cpl_msg_warning(func,
"A catalog of sky lines wavelengths was not "
10566 "given: using internal list of reference sky lines");
10568 line = default_lines_hi;
10569 nlines =
sizeof(default_lines_hi) /
sizeof(
double);
10572 line = default_lines_lo;
10573 nlines =
sizeof(default_lines_lo) /
sizeof(
double);
10578 cdata = cpl_image_get_data(calibration);
10580 nx = cpl_image_get_size_x(image);
10581 ny = cpl_image_get_size_y(image);
10583 sdata = cpl_image_get_data(image);
10585 if (ny != cpl_table_get_nrow(idscoeff)) {
10586 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
10600 for (i = 0; i < nlines; i++)
10601 if (line[i] > firstLambda && line[i] < lastLambda)
10604 offsets = cpl_table_new(nrows);
10605 cpl_table_new_column(offsets,
"wave", CPL_TYPE_DOUBLE);
10606 cpl_table_set_column_unit(offsets,
"wave",
"Angstrom");
10609 for (i = 0; i < nlines; i++) {
10610 if (line[i] > firstLambda && line[i] < lastLambda) {
10611 cpl_table_set_double(offsets,
"wave", nrows, line[i]);
10620 line = cpl_table_get_data_double(offsets,
"wave");
10624 while (idsorder < 6 && cpl_table_has_column(idscoeff, clab[idsorder]))
10634 dummy = cpl_table_new(ny);
10635 for (j = 0; j < nlines; j++) {
10636 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[j]);
10637 snprintf(fname, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10638 cpl_table_new_column(dummy, name, CPL_TYPE_DOUBLE);
10639 cpl_table_new_column(dummy, fname, CPL_TYPE_DOUBLE);
10642 for (j = 0; j < ny; j++, sdata += nx) {
10649 ids = cpl_polynomial_new(1);
10650 for (k = 0; k <= idsorder; k++) {
10651 c = cpl_table_get_double(idscoeff, clab[k], j, &missing);
10653 cpl_polynomial_delete(ids);
10656 cpl_polynomial_set_coeff(ids, &k, c);
10661 for (k = 0; k < nlines; k++) {
10662 expPos = cpl_polynomial_eval_1d(ids, line[k] - refwave, NULL);
10663 startPos = expPos - sradius;
10664 endPos = startPos + window;
10665 if (startPos < 0 || endPos >= nx)
10668 if (0 == peakPosition(sdata + startPos, window, &pos, 1)) {
10670 offset = pos - expPos;
10671 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[k]);
10672 cpl_table_set_double(dummy, name, j, offset);
10676 cpl_polynomial_delete(ids);
10685 for (j = 0; j < nlines; j++) {
10686 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[j]);
10687 snprintf(fname, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10688 if (cpl_table_has_valid(dummy, name)) {
10696 cpl_bivector *list;
10698 fittable = cpl_table_new(ny);
10699 cpl_table_new_column(fittable,
"row", CPL_TYPE_DOUBLE);
10700 cpl_table_set_column_unit(fittable,
"row",
"pixel");
10701 for (k = 0; k < ny; k++)
10702 cpl_table_set_double(fittable,
"row", k, k);
10703 cpl_table_duplicate_column(fittable,
"offset", dummy, name);
10704 npoints = ny - cpl_table_count_invalid(fittable,
"offset");
10705 cpl_table_erase_invalid(fittable);
10706 row = cpl_vector_wrap(npoints,
10707 cpl_table_get_data_double(fittable,
"row"));
10708 offs = cpl_vector_wrap(npoints,
10709 cpl_table_get_data_double(fittable,
"offset"));
10710 list = cpl_bivector_wrap_vectors(row, offs);
10711 robustLinearFit(list, &q, &m, &rms);
10712 cpl_bivector_unwrap_vectors(list);
10713 cpl_vector_unwrap(row);
10714 cpl_vector_unwrap(offs);
10715 cpl_table_delete(fittable);
10716 for (k = 0; k < ny; k++)
10717 cpl_table_set_double(dummy, fname, k, q + m*k);
10730 for (i = 0; i < ny; i++) {
10732 if (!cpl_table_is_valid(idscoeff, clab[0], i))
10736 for (j = 0; j < nlines; j++) {
10737 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10738 if (cpl_table_is_valid(dummy, name, i))
10746 if (npoints <= uorder)
10747 uorder = npoints - 1;
10755 wave = cpl_vector_new(npoints);
10756 wdata = cpl_vector_get_data(wave);
10757 offs = cpl_vector_new(npoints);
10758 odata = cpl_vector_get_data(offs);
10761 for (j = 0; j < nlines; j++) {
10762 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10763 if (cpl_table_is_valid(dummy, name, i)) {
10764 wdata[npoints] = line[j] - refwave;
10765 odata[npoints] = cpl_table_get_double(dummy, name, i, NULL);
10770 polycorr = cpl_polynomial_fit_1d_create(wave, offs, uorder, &rms);
10772 rms = sqrt(rms * (uorder + 1) / npoints);
10774 cpl_vector_delete(wave);
10775 cpl_vector_delete(offs);
10782 for (j = 0; j <= uorder; j++) {
10783 data = cpl_table_get_data_double(idscoeff, clab[j]);
10784 c = cpl_polynomial_get_coeff(polycorr, &j);
10788 data = cpl_table_get_data_double(idscoeff,
"error");
10789 data[i] = sqrt(data[i]*data[i] + rms*rms);
10791 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10792 idata[i] = npoints;
10800 for (k = 1; k < nx; k++) {
10801 lambda1 = cdata[k - 1 + i*nx];
10802 lambda2 = cdata[k + i*nx];
10803 if (lambda1 < 1.0 || lambda2 < 1.0)
10805 offset = cpl_polynomial_eval_1d(polycorr,
10806 lambda1-refwave, NULL);
10807 cdata[k - 1 + i*nx] -= offset * (lambda2-lambda1);
10811 cpl_polynomial_delete(polycorr);
10814 else if (uorder == 1) {
10820 cpl_bivector *list;
10823 wave = cpl_vector_new(npoints);
10824 wdata = cpl_vector_get_data(wave);
10825 offs = cpl_vector_new(npoints);
10826 odata = cpl_vector_get_data(offs);
10829 for (j = 0; j < nlines; j++) {
10830 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10831 if (cpl_table_is_valid(dummy, name, i)) {
10832 wdata[npoints] = line[j] - refwave;
10833 odata[npoints] = cpl_table_get_double(dummy, name, i, NULL);
10838 list = cpl_bivector_wrap_vectors(wave, offs);
10839 robustLinearFit(list, &q, &m, &rms);
10841 rms = sqrt(rms * (uorder + 1) / npoints);
10843 cpl_bivector_unwrap_vectors(list);
10844 cpl_vector_delete(wave);
10845 cpl_vector_delete(offs);
10852 for (j = 0; j <= uorder; j++) {
10853 data = cpl_table_get_data_double(idscoeff, clab[j]);
10861 data = cpl_table_get_data_double(idscoeff,
"error");
10862 data[i] = sqrt(data[i]*data[i] + rms*rms);
10864 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10865 idata[i] = npoints;
10873 for (k = 1; k < nx; k++) {
10874 lambda1 = cdata[k - 1 + i*nx];
10875 lambda2 = cdata[k + i*nx];
10876 if (lambda1 < 1.0 || lambda2 < 1.0)
10878 offset = q + m*(lambda1-refwave);
10879 cdata[k - 1 + i*nx] -= offset * (lambda2-lambda1);
10889 offs = cpl_vector_new(npoints);
10890 odata = cpl_vector_get_data(offs);
10893 for (j = 0; j < nlines; j++) {
10894 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10895 if (cpl_table_is_valid(dummy, name, i)) {
10896 odata[npoints] = cpl_table_get_double(dummy, name, i, NULL);
10901 offset = cpl_vector_get_median_const(offs);
10904 rms = cpl_vector_get_stdev(offs);
10906 else if (npoints == 1) {
10907 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[0]);
10908 if (cpl_table_has_valid(dummy, name)) {
10909 rms = cpl_table_get_column_stdev(dummy, name);
10910 rms /= sqrt(ny - cpl_table_count_invalid(dummy, name));
10920 rms /= sqrt(npoints);
10922 cpl_vector_delete(offs);
10929 data = cpl_table_get_data_double(idscoeff, clab[0]);
10932 data = cpl_table_get_data_double(idscoeff,
"error");
10933 data[i] = sqrt(data[i]*data[i] + rms*rms);
10935 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10936 idata[i] = npoints;
10945 for (k = 1; k < nx; k++) {
10946 lambda1 = cdata[k - 1 + i*nx];
10947 lambda2 = cdata[k + i*nx];
10948 if (lambda1 < 1.0 || lambda2 < 1.0)
10950 cdata[k - 1 + i*nx] -= offset * (lambda2-lambda1);
10957 for (j = 0; j < nlines; j++) {
10958 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[j]);
10959 if (cpl_table_has_valid(dummy, name)) {
10961 offset = cpl_table_get_column_median(dummy, name);
10962 cpl_msg_info(func,
"Median offset for %.3f: %.3f pixel",
10967 "Median offset for %.2f: not available", line[j]);
10971 cpl_table_delete(offsets);
10974 cpl_table_delete(dummy);
11011 double wavestart,
double dispersion,
int radius,
11015 const char *func =
"mos_distortions_rms";
11020 int cpix, npix, nzero;
11023 int npeaks, allPeaks;
11026 float peak, expectPeak, offset;
11030 double rms, oneRms;
11036 xlen = cpl_image_get_size_x(rectified);
11037 ylen = cpl_image_get_size_y(rectified);
11038 sdata = cpl_image_get_data(rectified);
11041 wdata = cpl_vector_get_data(lines);
11042 numLines = cpl_vector_get_size(lines);
11045 cpl_msg_warning(func,
"A catalog of sky lines wavelengths was not "
11046 "given: using internal list of reference sky lines");
11048 wdata = default_lines_hi;
11049 numLines =
sizeof(default_lines_hi) /
sizeof(
double);
11052 wdata = default_lines_lo;
11053 numLines =
sizeof(default_lines_lo) /
sizeof(
double);
11057 npix = 2 * radius + 1;
11058 profile = cpl_calloc(npix,
sizeof(
float));
11063 for (i = 0; i < numLines; i++) {
11070 expectPeak = (lambda - wavestart) / dispersion;
11071 cpix = floor(expectPeak + 0.5);
11077 sp = cpix - radius;
11078 ep = cpix + radius;
11080 if (sp < 0 || ep > xlen)
11087 for (j = 0; j < ylen; j++) {
11089 for (k = 0; k < npix; k++) {
11090 profile[k] = sdata[sp + k + j * xlen];
11091 if (fabs(profile[k]) < 0.0001)
11097 if (peakPosition(profile, npix, &peak, 1) == 0) {
11098 offset = (sp + peak) - expectPeak;
11100 rms += fabs(offset);
11101 oneRms += fabs(offset);
11108 cpl_msg_info(func,
"RMS for %.2f: %.3f pixel (%d points)",
11109 lambda, oneRms / npeaks * 1.25, npeaks);
11111 cpl_msg_info(func,
"RMS for %.2f: line not available", lambda);
11148 double blue,
double red,
double dispersion,
int trend)
11150 const char *func =
"mos_map_pixel";
11152 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11155 cpl_polynomial *ids;
11167 if (idscoeff == NULL) {
11168 cpl_msg_error(func,
"An IDS coeff table must be given");
11169 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11173 xsize = (red - blue) / dispersion;
11174 ysize = cpl_table_get_nrow(idscoeff);
11175 map = cpl_image_new(xsize, ysize, CPL_TYPE_FLOAT);
11176 mdata = cpl_image_get_data(map);
11179 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
11183 for (i = 0; i < ysize; i++, mdata += xsize) {
11186 ids = cpl_polynomial_new(1);
11187 for (k = trend; k <= order; k++) {
11188 c = cpl_table_get_double(idscoeff, clab[k], i, &missing);
11190 cpl_polynomial_delete(ids);
11193 cpl_polynomial_set_coeff(ids, &k, c);
11198 for (j = 0; j < xsize; j++) {
11199 lambda = blue + j*dispersion;
11200 mdata[j] = cpl_polynomial_eval_1d(ids, lambda-reference, NULL);
11203 cpl_polynomial_delete(ids);
11233 double blue,
double red)
11235 const char *func =
"mos_map_idscoeff";
11237 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11240 cpl_polynomial *ids;
11252 if (idscoeff == NULL) {
11253 cpl_msg_error(func,
"An IDS coeff table must be given");
11254 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11259 cpl_msg_error(func,
"Invalid image size");
11260 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11264 if (xsize < 20 || xsize > 5000) {
11265 cpl_msg_warning(func,
"Do you really have a detector %d pixels long?",
11269 ysize = cpl_table_get_nrow(idscoeff);
11270 map = cpl_image_new(xsize, ysize, CPL_TYPE_FLOAT);
11271 mdata = cpl_image_get_data(map);
11274 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
11278 for (i = 0; i < ysize; i++, mdata += xsize) {
11281 ids = cpl_polynomial_new(1);
11282 for (k = 0; k <= order; k++) {
11283 c = cpl_table_get_double(idscoeff, clab[k], i, &missing);
11285 cpl_polynomial_delete(ids);
11288 cpl_polynomial_set_coeff(ids, &k, c);
11293 for (j = 0; j < xsize; j++) {
11296 if (lambda >= blue && lambda <= red) {
11301 cpl_polynomial_delete(ids);
11344 cpl_table *slits, cpl_table *polytraces,
11345 double reference,
double blue,
double red,
11348 const char *func =
"mos_map_wavelengths";
11350 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11352 cpl_polynomial *polytop;
11353 cpl_polynomial *polybot;
11354 cpl_image *remapped;
11359 double vtop, vbot, value;
11366 int yint, ysize, yprev;
11373 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
11374 int missing_top, missing_bot;
11381 if (spatial == NULL || calibration == NULL ||
11382 slits == NULL || polytraces == NULL) {
11383 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11387 if (dispersion <= 0.0) {
11388 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11392 if (red - blue < dispersion) {
11393 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11397 nx = cpl_image_get_size_x(spatial);
11398 ny = cpl_image_get_size_y(spatial);
11399 ysize = cpl_image_get_size_y(calibration);
11400 remapped = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
11401 data = cpl_image_get_data(remapped);
11402 sdata = cpl_image_get_data(spatial);
11403 wdata = cpl_image_get_data(calibration);
11405 nslits = cpl_table_get_nrow(slits);
11406 slit_id = cpl_table_get_data_int(slits,
"slit_id");
11407 order = cpl_table_get_ncol(polytraces) - 2;
11408 position = cpl_table_get_data_int(slits,
"position");
11409 length = cpl_table_get_data_int(slits,
"length");
11416 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
11417 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
11419 for (i = 0; i < nslits; i++) {
11421 if (length[i] == 0)
11435 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
11437 start_pixel = refpixel - pixel_below;
11438 if (start_pixel < 0)
11441 end_pixel = refpixel + pixel_above;
11442 if (end_pixel > nx)
11451 polytop = cpl_polynomial_new(1);
11452 for (k = 0; k <= order; k++) {
11453 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
11455 cpl_polynomial_delete(polytop);
11459 cpl_polynomial_set_coeff(polytop, &k, coeff);
11463 polybot = cpl_polynomial_new(1);
11464 for (k = 0; k <= order; k++) {
11465 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
11467 cpl_polynomial_delete(polybot);
11471 cpl_polynomial_set_coeff(polybot, &k, coeff);
11474 if (missing_top && missing_bot) {
11475 cpl_msg_debug(func,
"Slit %d was not traced: no extraction!",
11487 cpl_msg_debug(func,
"Upper edge of slit %d was not traced: "
11488 "the spectral curvature of the lower edge "
11489 "is used instead.", slit_id[i]);
11490 polytop = cpl_polynomial_duplicate(polybot);
11491 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
11492 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
11494 coeff = cpl_polynomial_get_coeff(polybot, &k);
11495 coeff += ytop - ybot;
11496 cpl_polynomial_set_coeff(polytop, &k, coeff);
11500 cpl_msg_debug(func,
"Lower edge of slit %d was not traced: "
11501 "the spectral curvature of the upper edge "
11502 "is used instead.", slit_id[i]);
11503 polybot = cpl_polynomial_duplicate(polytop);
11504 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
11505 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
11507 coeff = cpl_polynomial_get_coeff(polytop, &k);
11508 coeff -= ytop - ybot;
11509 cpl_polynomial_set_coeff(polybot, &k, coeff);
11519 xdata = wdata + nx*position[i];
11520 npseudo = length[i] - 1;
11526 for (j = start_pixel; j < end_pixel; j++) {
11527 top = cpl_polynomial_eval_1d(polytop, j, NULL);
11528 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
11529 for (k = 0; k <= npseudo; k++) {
11530 ypos = top - k*(top-bot)/npseudo;
11540 if (yint < 0 || yint >= ny-1) {
11545 value = sdata[j + nx*yint];
11547 fvalue = value - ivalue;
11548 if (ivalue < npseudo && ivalue >= 0) {
11549 vtop = xdata[j + nx*(npseudo-ivalue)];
11550 vbot = xdata[j + nx*(npseudo-ivalue-1)];
11559 else if (vbot < 1.0) {
11565 else if (fabs(vbot-vtop) > 10*dispersion) {
11569 value = vtop*(1-fvalue) + vbot*fvalue;
11571 data[j + nx*yint] = value;
11581 if (yprev - yint > 1) {
11582 value = sdata[j + nx*(yint+1)];
11584 fvalue = value - ivalue;
11585 if (ivalue < npseudo && ivalue >= 0) {
11586 vtop = xdata[j + nx*(npseudo-ivalue)];
11587 vbot = xdata[j + nx*(npseudo-ivalue-1)];
11590 value = data[j + nx*(yint+1)];
11596 else if (vbot < 1.0) {
11599 else if (fabs(vbot-vtop) > 2*dispersion) {
11603 value = vtop*(1-fvalue) + vbot*fvalue;
11605 data[j + nx*(yint+1)] = value;
11613 cpl_polynomial_delete(polytop);
11614 cpl_polynomial_delete(polybot);
11694 cpl_image *spatial, cpl_table *slits,
11695 cpl_table *polytraces,
double reference,
11696 double blue,
double red,
double dispersion,
11699 const char *func =
"mos_map_spectrum";
11701 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11703 cpl_polynomial *polytop;
11704 cpl_polynomial *polybot;
11705 cpl_image *remapped;
11706 cpl_image **exslit;
11711 double lambda00, lambda01, lambda10, lambda11, lambda;
11712 double space00, space01, space10, space11, space;
11713 double value00, value01, value10, value11, value0, value1, value;
11718 double xfrac, yfrac;
11731 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
11732 int missing_top, missing_bot;
11741 if (spectra == NULL || spatial == NULL || wavecalib == NULL ||
11742 slits == NULL || polytraces == NULL) {
11743 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11747 if (dispersion <= 0.0) {
11748 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11752 if (red - blue < dispersion) {
11753 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11757 nx = cpl_image_get_size_x(spectra);
11758 ny = cpl_image_get_size_y(spectra);
11760 if (nx != cpl_image_get_size_x(spatial) ||
11761 ny != cpl_image_get_size_y(spatial) ||
11762 nx != cpl_image_get_size_x(wavecalib) ||
11763 ny != cpl_image_get_size_y(wavecalib)) {
11764 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
11768 nlambda = STRETCH_FACTOR * (red - blue) / dispersion;
11769 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
11770 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
11772 data = cpl_image_get_data(spectra);
11773 sdata = cpl_image_get_data(spatial);
11774 wdata = cpl_image_get_data(wavecalib);
11776 nslits = cpl_table_get_nrow(slits);
11777 slit_id = cpl_table_get_data_int(slits,
"slit_id");
11778 order = cpl_table_get_ncol(polytraces) - 2;
11779 position = cpl_table_get_data_int(slits,
"position");
11780 length = cpl_table_get_data_int(slits,
"length");
11782 exslit = cpl_calloc(nslits,
sizeof(cpl_image *));
11784 for (i = 0; i < nslits; i++) {
11800 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
11802 start_pixel = refpixel - pixel_below;
11803 if (start_pixel < 1)
11806 end_pixel = refpixel + pixel_above;
11807 if (end_pixel > nx)
11816 polytop = cpl_polynomial_new(1);
11817 for (k = 0; k <= order; k++) {
11818 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
11820 cpl_polynomial_delete(polytop);
11824 cpl_polynomial_set_coeff(polytop, &k, coeff);
11828 polybot = cpl_polynomial_new(1);
11829 for (k = 0; k <= order; k++) {
11830 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
11832 cpl_polynomial_delete(polybot);
11836 cpl_polynomial_set_coeff(polybot, &k, coeff);
11839 if (missing_top && missing_bot) {
11840 cpl_msg_debug(func,
"Slit %d was not traced: no extraction!",
11852 cpl_msg_debug(func,
"Upper edge of slit %d was not traced: "
11853 "the spectral curvature of the lower edge "
11854 "is used instead.", slit_id[i]);
11855 polytop = cpl_polynomial_duplicate(polybot);
11856 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
11857 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
11859 coeff = cpl_polynomial_get_coeff(polybot, &k);
11860 coeff += ytop - ybot;
11861 cpl_polynomial_set_coeff(polytop, &k, coeff);
11865 cpl_msg_debug(func,
"Lower edge of slit %d was not traced: "
11866 "the spectral curvature of the upper edge "
11867 "is used instead.", slit_id[i]);
11868 polybot = cpl_polynomial_duplicate(polytop);
11869 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
11870 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
11872 coeff = cpl_polynomial_get_coeff(polytop, &k);
11873 coeff -= ytop - ybot;
11874 cpl_polynomial_set_coeff(polybot, &k, coeff);
11881 top = cpl_polynomial_eval_1d(polytop, refpixel, NULL);
11882 bot = cpl_polynomial_eval_1d(polybot, refpixel, NULL);
11883 npseudo = ceil(top-bot) + 1;
11886 cpl_polynomial_delete(polytop);
11887 cpl_polynomial_delete(polybot);
11888 cpl_msg_debug(func,
"Slit %d was badly traced: no extraction!",
11893 exslit[i] = cpl_image_new(nlambda, npseudo+1, CPL_TYPE_FLOAT);
11894 xdata = cpl_image_get_data(exslit[i]);
11900 for (x = start_pixel; x < end_pixel; x++) {
11901 top = cpl_polynomial_eval_1d(polytop, x, NULL);
11902 bot = cpl_polynomial_eval_1d(polybot, x, NULL);
11913 for (y = ibot; y < itop; y++) {
11914 lambda11 = wdata[x + y*nx];
11915 if (lambda11 < 1.0)
11917 space11 = sdata[x + y*nx];
11920 lambda01 = wdata[x - 1 + y*nx];
11921 if (lambda01 < 1.0)
11923 space01 = sdata[x - 1 + y*nx];
11956 lambda10 = wdata[x + shift + (y+1)*nx];
11957 if (lambda10 < 1.0)
11959 space10 = sdata[x + shift + (y+1)*nx];
11962 lambda00 = wdata[x - 1 + shift + (y+1)*nx];
11963 if (lambda00 < 1.0)
11965 space00 = sdata[x - 1 + shift + (y+1)*nx];
11975 dL = lambda11 - lambda01;
11976 dS = space11 - space10;
11983 L = (lambda11 - blue)/dispersion + 0.5;
11986 if (L < 0 || L >= nlambda)
11988 if (S < 0 || S > npseudo)
11995 lambda = blue + L*dispersion;
12007 xfrac = (lambda11-lambda)/dL;
12008 yfrac = (space11-space)/dS;
12019 value11 = data[x + y*nx];
12020 value01 = data[x - 1 + y*nx];
12021 value10 = data[x + shift + (y+1)*nx];
12022 value00 = data[x + shift - 1 + (y+1)*nx];
12028 value1 = (1-xfrac)*value11 + xfrac*value01;
12029 value0 = (1-xfrac)*value10 + xfrac*value00;
12030 value = (1-yfrac)*value1 + yfrac*value0;
12037 xdata[L + nlambda*(npseudo-S)] = value;
12041 cpl_polynomial_delete(polytop);
12042 cpl_polynomial_delete(polybot);
12050 for (i = 0; i < nslits; i++)
12052 ysize += cpl_image_get_size_y(exslit[i]);
12054 remapped = cpl_image_new(nlambda, ysize, CPL_TYPE_FLOAT);
12057 for (i = 0; i < nslits; i++) {
12059 yint += cpl_image_get_size_y(exslit[i]);
12060 cpl_image_copy(remapped, exslit[i], 1, ysize - yint);
12061 cpl_image_delete(exslit[i]);
12062 cpl_table_set_int(slits,
"position", i, ysize - yint - 1);
12106 double dispersion,
double factor,
int minpoints,
12109 const char *func =
"mos_sky_map_super";
12111 cpl_vector **vector;
12112 cpl_vector **wvector;
12113 double firstLambda, lastLambda;
12114 double lambda, lambda1, lambda2;
12115 double value, value1, value2;
12121 int first_valid, valid_bins;
12125 double *sky_spectrum;
12132 if (spectra == NULL || wavemap == NULL || skymap == NULL) {
12133 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12137 if (dispersion <= 0.0) {
12138 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12139 cpl_msg_error(func,
"Negative dispersion: %s", cpl_error_get_message());
12143 nx = cpl_image_get_size_x(spectra);
12144 ny = cpl_image_get_size_y(spectra);
12147 if (nx != cpl_image_get_size_x(wavemap) ||
12148 ny != cpl_image_get_size_y(wavemap) ||
12149 nx != cpl_image_get_size_x(skymap) ||
12150 ny != cpl_image_get_size_y(skymap)) {
12151 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
12152 cpl_msg_error(func,
"Image sizes: %s", cpl_error_get_message());
12156 if (factor < 1.0) {
12157 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12158 cpl_msg_error(func,
"Undersampling (%f): %s", factor,
12159 cpl_error_get_message());
12163 if (minpoints < 0) {
12164 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12165 cpl_msg_error(func,
"Negative threshold: %s", cpl_error_get_message());
12169 dispersion /= factor;
12176 data = cpl_image_get_data(wavemap);
12178 for (i = 0; i < npix; i++) {
12179 if (data[i] > 1.0) {
12180 min = max = data[i];
12186 for (i = j; i < npix; i++) {
12203 nbin = (lastLambda - firstLambda) / dispersion;
12212 count = cpl_calloc(nbin,
sizeof(
int));
12214 data = cpl_image_get_data(wavemap);
12216 for (i = 0; i < npix; i++) {
12219 bin = (data[i] - firstLambda) / dispersion;
12225 for (i = 0; i < nbin; i++)
12226 if (count[i] >= minpoints)
12229 if (valid_bins < nbin/3) {
12230 cpl_msg_warning(func,
"Cannot determine a good global sky "
12231 "spectrum from input data");
12243 vector = cpl_calloc(nbin,
sizeof(cpl_vector *));
12244 wvector = cpl_calloc(nbin,
sizeof(cpl_vector *));
12245 for (i = 0; i < nbin; i++) {
12246 if (count[i] >= minpoints) {
12247 vector[i] = cpl_vector_new(count[i]);
12248 wvector[i] = cpl_vector_new(count[i]);
12259 data = cpl_image_get_data(wavemap);
12260 sdata = cpl_image_get_data(spectra);
12262 for (i = 0; i < npix; i++) {
12265 bin = (data[i] - firstLambda) / dispersion;
12268 cpl_vector_set(vector[bin], count[bin], sdata[i]);
12269 cpl_vector_set(wvector[bin], count[bin], data[i]);
12281 sky_spectrum = cpl_calloc(nbin,
sizeof(
double));
12282 sky_wave = cpl_calloc(nbin,
sizeof(
double));
12283 for (i = 0; i < nbin; i++) {
12285 sky_spectrum[i] = cpl_vector_get_median_const(vector[i]);
12286 sky_wave[i] = cpl_vector_get_median_const(wvector[i]);
12287 cpl_vector_delete(vector[i]);
12288 cpl_vector_delete(wvector[i]);
12300 for (i = 0; i < nbin; i++) {
12301 if (count[i] >= minpoints) {
12307 for (i = first_valid; i < nbin; i++) {
12308 if (count[i] < minpoints) {
12309 sky_wave[i] = firstLambda + (i+0.5)*dispersion;
12310 for (j = i+1; j < nbin; j++) {
12311 if (count[j] >= minpoints) {
12312 if (sky_wave[j] - sky_wave[i-1] < 0.1) {
12313 sky_spectrum[i] = (sky_spectrum[j] + sky_spectrum[i-1])
12317 frac = (sky_wave[i] - sky_wave[i-1])
12318 / (sky_wave[j] - sky_wave[i-1]);
12319 sky_spectrum[i] = frac * sky_spectrum[j]
12320 + (1 - frac) * sky_spectrum[i-1];
12332 sky = cpl_table_new(nbin);
12333 cpl_table_wrap_double(sky, sky_wave,
"wavelength");
12334 cpl_table_wrap_double(sky, sky_spectrum,
"sky");
12335 cpl_table_wrap_int(sky, count,
"npoints");
12342 data = cpl_image_get_data(wavemap);
12343 sdata = cpl_image_get_data(spectra);
12344 kdata = cpl_image_get_data(skymap);
12346 for (i = 0; i < npix; i++) {
12355 bin = (lambda - firstLambda) / dispersion;
12356 lambda1 = sky_wave[bin];
12357 value1 = sky_spectrum[bin];
12358 if (lambda1 < lambda) {
12361 lambda2 = sky_wave[bin];
12362 value2 = sky_spectrum[bin];
12363 if (lambda2 - lambda1 < 0.1) {
12364 value = (value1 + value2) / 2;
12367 frac = (lambda - lambda1) / (lambda2 - lambda1);
12368 value = frac * value2 + (1 - frac) * value1;
12380 lambda1 = sky_wave[bin];
12381 value1 = sky_spectrum[bin];
12382 if (lambda2 - lambda1 < 0.1) {
12383 value = (value1 + value2) / 2;
12386 frac = (lambda - lambda1) / (lambda2 - lambda1);
12387 value = frac * value2 + (1 - frac) * value1;
12398 cpl_table_erase_window(sky, 0, first_valid);
12439 double dispersion, cpl_image *skymap)
12441 const char *func =
"mos_sky_map";
12443 cpl_vector **vector;
12444 double firstLambda, lastLambda;
12445 double lambda, lambda1, lambda2;
12446 double value, value1, value2;
12454 double *sky_spectrum;
12461 if (spectra == NULL || wavemap == NULL || skymap == NULL) {
12462 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12466 if (dispersion <= 0.0) {
12467 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12471 nx = cpl_image_get_size_x(spectra);
12472 ny = cpl_image_get_size_y(spectra);
12475 if (nx != cpl_image_get_size_x(wavemap) ||
12476 ny != cpl_image_get_size_y(wavemap) ||
12477 nx != cpl_image_get_size_x(skymap) ||
12478 ny != cpl_image_get_size_y(skymap)) {
12479 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
12488 data = cpl_image_get_data(wavemap);
12490 for (i = 0; i < npix; i++) {
12491 if (data[i] > 1.0) {
12492 min = max = data[i];
12498 for (i = j; i < npix; i++) {
12515 nbin = (lastLambda - firstLambda) / dispersion;
12524 count = cpl_calloc(nbin,
sizeof(
int));
12526 data = cpl_image_get_data(wavemap);
12528 for (i = 0; i < npix; i++) {
12531 bin = (data[i] - firstLambda) / dispersion;
12544 vector = cpl_calloc(nbin,
sizeof(cpl_vector *));
12545 for (i = 0; i < nbin; i++) {
12547 vector[i] = cpl_vector_new(count[i]);
12559 data = cpl_image_get_data(wavemap);
12560 sdata = cpl_image_get_data(spectra);
12562 for (i = 0; i < npix; i++) {
12565 bin = (data[i] - firstLambda) / dispersion;
12567 cpl_vector_set(vector[bin], count[bin], sdata[i]);
12578 sky_spectrum = cpl_calloc(nbin,
sizeof(
double));
12579 for (i = 0; i < nbin; i++) {
12581 sky_spectrum[i] = cpl_vector_get_median_const(vector[i]);
12582 cpl_vector_delete(vector[i]);
12600 sky = cpl_table_new(nbin);
12601 cpl_table_new_column(sky,
"wavelength", CPL_TYPE_DOUBLE);
12602 cpl_table_set_column_unit(sky,
"wavelength",
"pixel");
12603 cpl_table_wrap_double(sky, sky_spectrum,
"sky");
12604 cpl_table_wrap_int(sky, count,
"npoints");
12605 for (i = 0; i < nbin; i++)
12606 cpl_table_set_double(sky,
"wavelength", i,
12607 firstLambda + (i+0.5)*dispersion);
12614 data = cpl_image_get_data(wavemap);
12615 sdata = cpl_image_get_data(spectra);
12616 kdata = cpl_image_get_data(skymap);
12617 wdata = cpl_table_get_data_double(sky,
"wavelength");
12619 for (i = 0; i < npix; i++) {
12628 bin = (lambda - firstLambda) / dispersion;
12629 lambda1 = wdata[bin];
12630 value1 = sky_spectrum[bin];
12631 if (lambda1 < lambda) {
12634 lambda2 = wdata[bin];
12635 value2 = sky_spectrum[bin];
12636 value = ((lambda2 - lambda)*value1
12637 + (lambda - lambda1)*value2) / dispersion;
12648 lambda1 = wdata[bin];
12649 value1 = sky_spectrum[bin];
12650 value = ((lambda2 - lambda)*value1
12651 + (lambda - lambda1)*value2)/dispersion;
12682 const char *func =
"mos_sky_local_old";
12690 int xlow, ylow, xhig, yhig;
12698 if (spectra == NULL) {
12699 cpl_msg_error(func,
12700 "A scientific rectified spectral image must be given");
12701 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12705 if (slits == NULL) {
12706 cpl_msg_error(func,
"A slits position table must be given");
12707 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12711 nslits = cpl_table_get_nrow(slits);
12712 slit_id = cpl_table_get_data_int(slits,
"slit_id");
12713 position = cpl_table_get_data_int(slits,
"position");
12714 length = cpl_table_get_data_int(slits,
"length");
12716 nx = cpl_image_get_size_x(spectra);
12717 ny = cpl_image_get_size_y(spectra);
12719 skymap = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
12723 for (i = 0; i < nslits; i++) {
12725 if (length[i] == 0)
12738 ylow = position[i] + 1;
12739 yhig = ylow + length[i] - 1;
12741 exslit = cpl_image_extract(spectra, xlow, ylow, xhig, yhig);
12742 sky = cpl_image_collapse_median_create(exslit, 0, 0, 1);
12743 cpl_image_delete(exslit);
12745 data = cpl_image_get_data(skymap);
12746 data += nx * position[i];
12748 for (j = 0; j < length[i]; j++) {
12749 sdata = cpl_image_get_data(sky);
12750 for (k = 0; k < nx; k++) {
12751 *data++ = *sdata++;
12755 cpl_image_delete(sky);
12784 const char *func =
"mos_sky_local";
12786 char name[MAX_COLNAME];
12788 cpl_polynomial *fit;
12789 cpl_vector *points;
12790 cpl_vector *values;
12791 cpl_vector *keep_points;
12792 cpl_vector *keep_values;
12795 cpl_image *subtracted;
12796 cpl_image *profile;
12798 cpl_table *objects;
12806 int xlow, ylow, xhig, yhig;
12819 if (spectra == NULL) {
12820 cpl_msg_error(func,
12821 "A scientific rectified spectral image must be given");
12822 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12826 if (slits == NULL) {
12827 cpl_msg_error(func,
"A slits position table must be given");
12828 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12833 cpl_msg_error(func,
"Invalid fit order");
12834 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12838 nslits = cpl_table_get_nrow(slits);
12839 slit_id = cpl_table_get_data_int(slits,
"slit_id");
12840 position = cpl_table_get_data_int(slits,
"position");
12841 length = cpl_table_get_data_int(slits,
"length");
12843 nx = cpl_image_get_size_x(spectra);
12844 ny = cpl_image_get_size_y(spectra);
12846 skymap = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
12850 for (i = 0; i < nslits; i++) {
12852 if (length[i] == 0)
12865 ylow = position[i] + 1;
12866 yhig = ylow + length[i] - 1;
12868 exslit = cpl_image_extract(spectra, xlow, ylow, xhig, yhig);
12869 sky = cpl_image_collapse_median_create(exslit, 0, 0, 1);
12870 cpl_image_delete(exslit);
12872 data = cpl_image_get_data(skymap);
12873 data += nx * position[i];
12875 for (j = 0; j < length[i]; j++) {
12876 sdata = cpl_image_get_data(sky);
12877 for (k = 0; k < nx; k++) {
12878 *data++ = *sdata++;
12882 cpl_image_delete(sky);
12890 subtracted = cpl_image_duplicate(spectra);
12891 cpl_image_subtract(subtracted, skymap);
12892 cpl_image_delete(skymap);
12899 objects = cpl_table_duplicate(slits);
12901 cpl_image_delete(profile);
12902 cpl_image_delete(subtracted);
12911 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
12912 while (cpl_table_has_column(objects, name)) {
12914 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
12917 is_sky = cpl_calloc(ny,
sizeof(
int));
12919 for (i = 0; i < nslits; i++) {
12921 if (length[i] == 0)
12924 ylow = position[i] + margin;
12925 yhig = position[i] + length[i] - margin;
12927 for (j = ylow; j < yhig; j++)
12930 for (j = 1; j < maxobjects; j++) {
12931 snprintf(name, MAX_COLNAME,
"object_%d", j);
12932 if (cpl_table_is_valid(objects, name, i)) {
12933 snprintf(name, MAX_COLNAME,
"start_%d", j);
12934 ylow = cpl_table_get_int(objects, name, i, NULL);
12935 snprintf(name, MAX_COLNAME,
"end_%d", j);
12936 yhig = cpl_table_get_int(objects, name, i, NULL);
12937 for (k = ylow; k <= yhig; k++)
12947 ylow = position[i] + margin + 1;
12948 yhig = position[i] + length[i] - margin - 1;
12950 for (j = ylow; j < yhig; j++)
12952 if (is_sky[j-1] == 0 && is_sky[j+1] == 0)
12962 skymap = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
12964 for (i = 0; i < nslits; i++) {
12966 if (length[i] == 0)
12969 ylow = position[i];
12970 yhig = ylow + length[i];
12973 for (j = ylow; j < yhig; j++)
12977 if (nsky > order + 1) {
12979 points = cpl_vector_new(nsky);
12981 for (j = ylow; j < yhig; j++) {
12983 cpl_vector_set(points, nsky, j);
12988 exslit = cpl_image_extract(spectra, 1, ylow+1, nx, yhig);
12989 xdata = cpl_image_get_data(exslit);
12990 values = cpl_vector_new(nsky);
12992 for (j = 0; j < nx; j++) {
12994 for (k = ylow; k < yhig; k++) {
12996 cpl_vector_set(values, nsky, xdata[j+(k-ylow)*nx]);
13005 median = cpl_vector_get_median_const(values);
13006 vdata = cpl_vector_get_data(values);
13007 pdata = cpl_vector_get_data(points);
13009 for (k = 0; k < nsky; k++) {
13010 if (fabs(vdata[k] - median) < 100) {
13012 vdata[k-nbad] = vdata[k];
13013 pdata[k-nbad] = pdata[k];
13023 if (nbad && nsky - nbad > order + 1) {
13024 keep_values = values;
13025 keep_points = points;
13026 values = cpl_vector_wrap(nsky-nbad, vdata);
13027 points = cpl_vector_wrap(nsky-nbad, pdata);
13030 if (nsky - nbad > order + 1) {
13032 fit = cpl_polynomial_fit_1d_create(points, values,
13036 for (k = ylow; k < yhig; k++) {
13037 xdata[j+(k-ylow)*nx] =
13038 cpl_polynomial_eval_1d(fit, k, NULL);
13041 cpl_polynomial_delete(fit);
13047 for (k = 0; k < nsky; k++) {
13048 xdata[j+k*nx] = median;
13052 if (nbad && nsky - nbad > order + 1) {
13053 cpl_vector_unwrap(values);
13054 cpl_vector_unwrap(points);
13055 values = keep_values;
13056 points = keep_points;
13061 for (k = ylow; k < yhig; k++) {
13063 cpl_vector_set(points, nsky, k);
13071 cpl_vector_delete(values);
13072 cpl_vector_delete(points);
13074 cpl_image_copy(skymap, exslit, 1, ylow+1);
13075 cpl_image_delete(exslit);
13079 exslit = cpl_image_extract(spectra, 1, ylow+1, nx, yhig);
13080 xdata = cpl_image_get_data(exslit);
13081 values = cpl_vector_new(nsky);
13083 for (j = 0; j < nx; j++) {
13085 for (k = ylow; k < yhig; k++) {
13087 cpl_vector_set(values, nsky, xdata[j+(k-ylow)*nx]);
13092 median = cpl_vector_get_median_const(values);
13094 for (k = ylow; k < yhig; k++)
13095 xdata[j+(k-ylow)*nx] = median;
13099 cpl_vector_delete(values);
13101 cpl_image_copy(skymap, exslit, 1, ylow+1);
13102 cpl_image_delete(exslit);
13106 cpl_msg_warning(func,
"Too few sky points in slit %d", i + 1);
13138 float threshold,
float ratio)
13140 const char *func =
"mos_clean_cosmics";
13142 cpl_image *smoothImage;
13144 cpl_matrix *kernel;
13149 float sigma, sum, value, smoothValue;
13153 int iMin, iMax, jMin, jMax, iPosMax, jPosMax;
13159 int pos, i, j, k, l, ii, jj, iii = 0, jjj = 0;
13161 int found, foundContiguousCandidate;
13166 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13179 xLen = cpl_image_get_size_x(image);
13180 yLen = cpl_image_get_size_y(image);
13182 if (xLen < 4 || yLen < 4)
13183 return CPL_ERROR_NONE;
13185 nPix = xLen * yLen;
13208 idata = cpl_image_get_data(image);
13212 for (i = 0; i < nPix; i++) {
13213 if (idata[i] < -0.00001) {
13222 cosmic = cpl_calloc(nPix,
sizeof(
int));
13224 if (threshold < 0.)
13229 kernel = cpl_matrix_new(3, 3);
13230 cpl_matrix_fill(kernel, 1.0);
13231 cpl_matrix_set(kernel, 1, 1, 0.0);
13232 smoothImage = cpl_image_filter_median(image, kernel);
13233 cpl_matrix_delete(kernel);
13244 sdata = cpl_image_get_data(smoothImage);
13246 for (j = 1; j < yLen - 1; j++) {
13247 for (i = 1; i < xLen - 1; i++) {
13248 value = idata[i + j * xLen];
13249 smoothValue = sdata[i + j * xLen];
13250 if (smoothValue < 1.0)
13252 sigma = sqrt(noise * noise + smoothValue / gain);
13253 if (value - smoothValue >= threshold * sigma)
13254 cosmic[i + j * xLen] = -1;
13258 cpl_image_delete(smoothImage);
13267 for (pos = first; pos < nPix; pos++) {
13268 if (cosmic[pos] == -1) {
13288 iMin = iMax = iPosMax = i;
13289 jMin = jMax = jPosMax = j;
13290 fMax = idata[i + j * xLen];
13293 foundContiguousCandidate = 0;
13294 for (l = 0; l <= 1; l++) {
13295 for (k = 0; k <= 1; k++) {
13302 jj = j + k + l - 1;
13303 if (cosmic[ii + jj * xLen] == -1) {
13304 foundContiguousCandidate = 1;
13305 cosmic[ii + jj * xLen] = 2;
13323 if (idata[ii + jj * xLen] > fMax) {
13324 fMax = idata[ii + jj * xLen];
13337 cosmic[i + j * xLen] = 3;
13339 if (foundContiguousCandidate) {
13361 for (l = jMin; l <= jMax; l++) {
13362 for (k = iMin; k <= iMax; k++) {
13363 if (cosmic[k + l * xLen] == 2) {
13366 foundContiguousCandidate = 1;
13370 if (foundContiguousCandidate)
13373 }
while (foundContiguousCandidate);
13382 for (l = -1; l <= 1; l++) {
13383 for (k = -1; k <= 1; k++) {
13384 if (l != 0 || k != 0) {
13385 sum += idata[iPosMax + k + (jPosMax + l) * xLen];
13391 if (fMax > ratio * sum) {
13392 for (l = jMin - 1; l <= jMax + 1; l++) {
13393 for (k = iMin - 1; k <= iMax + 1; k++) {
13394 if (cosmic[k + l * xLen] == 3) {
13395 cosmic[k + l * xLen] = 1;
13402 for (l = jMin - 1; l <= jMax + 1; l++) {
13403 for (k = iMin - 1; k <= iMax + 1; k++) {
13404 if (cosmic[k + l * xLen] != -1) {
13405 if (cosmic[k + l * xLen] == 1)
13407 cosmic[k + l * xLen] = 0;
13420 table = cpl_table_new(numCosmic);
13421 cpl_table_new_column(table,
"x", CPL_TYPE_INT);
13422 cpl_table_new_column(table,
"y", CPL_TYPE_INT);
13423 cpl_table_set_column_unit(table,
"x",
"pixel");
13424 cpl_table_set_column_unit(table,
"y",
"pixel");
13425 xdata = cpl_table_get_data_int(table,
"x");
13426 ydata = cpl_table_get_data_int(table,
"y");
13428 for (pos = 0, i = 0; pos < nPix; pos++) {
13429 if (cosmic[pos] == 1) {
13430 xdata[i] = (pos % xLen);
13431 ydata[i] = (pos / xLen);
13436 mos_clean_bad_pixels(image, table, 1);
13439 cpl_table_delete(table);
13441 return CPL_ERROR_NONE;
13446 cpl_error_code mos_clean_bad_pixels(cpl_image *image, cpl_table *table,
13449 const char *func =
"mos_clean_cosmics";
13454 int xlen, ylen, totPix;
13455 int nBadPixels = 0;
13456 int sign, foundFirst;
13457 int *xValue = NULL;
13458 int *yValue = NULL;
13464 int sx[] = {0, 1, 1, 1};
13465 int sy[] = {1,-1, 0, 1};
13466 int searchHorizon = 100;
13470 if (image == NULL || table == NULL)
13471 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13473 if (1 != cpl_table_has_column(table,
"x"))
13474 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
13476 if (1 != cpl_table_has_column(table,
"y"))
13477 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
13479 if (CPL_TYPE_INT != cpl_table_get_column_type(table,
"x"))
13480 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
13482 if (CPL_TYPE_INT != cpl_table_get_column_type(table,
"y"))
13483 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
13485 nBadPixels = cpl_table_get_nrow(table);
13488 xlen = cpl_image_get_size_x(image);
13489 ylen = cpl_image_get_size_y(image);
13490 idata = cpl_image_get_data(image);
13491 totPix = xlen * ylen;
13492 if (((
float) nBadPixels) / ((
float) totPix) < percent/100.) {
13493 isBadPix = cpl_calloc(totPix,
sizeof(
int));
13496 cpl_msg_warning(func,
"Too many bad pixels (> %d%%): "
13497 "skip bad pixel correction", percent);
13498 return cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13502 cpl_msg_debug(func,
"No pixel values to interpolate");
13503 return CPL_ERROR_NONE;
13506 xValue = cpl_table_get_data_int(table,
"x");
13507 yValue = cpl_table_get_data_int(table,
"y");
13509 for (i = 0; i < nBadPixels; i++)
13510 isBadPix[xValue[i] + yValue[i] * xlen] = 1;
13512 for (i = 0; i < nBadPixels; i++) {
13527 for (j = 0; j < 4; j++) {
13533 estimate[nPairs] = 0.;
13536 for (k = 0; k < 2; k++) {
13542 cx += sign * sx[j];
13543 cy += sign * sy[j];
13544 if (cx < 0 || cx >= xlen || cy < 0 || cy >= ylen)
13547 }
while (isBadPix[cx + cy * xlen] && d < searchHorizon);
13549 if (cx >= 0 && cx < xlen &&
13550 cy >= 0 && cy < ylen && d < searchHorizon) {
13556 save = idata[cx + cy * xlen];
13557 estimate[nPairs] += save / d;
13558 sumd += 1. / (double) d;
13560 estimate[nPairs] /= sumd;
13575 estimate[nPairs] = save;
13590 idata[xValue[i] + yValue[i] * xlen] =
13591 cpl_tools_get_median_float(estimate, nPairs);
13593 else if (nPairs == 2) {
13594 idata[xValue[i] + yValue[i] * xlen] =
13595 (estimate[0] + estimate[1]) / 2.;
13597 else if (nPairs == 1) {
13598 idata[xValue[i] + yValue[i] * xlen] = estimate[0];
13601 cpl_msg_debug(func,
"Cannot correct bad pixel %d,%d\n",
13602 xValue[i], yValue[i]);
13606 cpl_free(isBadPix);
13608 return CPL_ERROR_NONE;
13642 cpl_table *polytraces,
double reference,
13643 double blue,
double red,
double dispersion)
13645 const char *func =
"mos_spatial_map";
13647 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
13649 cpl_polynomial *polytop;
13650 cpl_polynomial *polybot;
13651 cpl_image *calibration;
13664 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
13665 int missing_top, missing_bot;
13672 if (spectra == NULL || slits == NULL || polytraces == NULL) {
13673 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13677 if (dispersion <= 0.0) {
13678 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13682 if (red - blue < dispersion) {
13683 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13687 nx = cpl_image_get_size_x(spectra);
13688 ny = cpl_image_get_size_y(spectra);
13690 calibration = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
13691 data = cpl_image_get_data(calibration);
13693 length = cpl_table_get_data_int(slits,
"length");
13694 nslits = cpl_table_get_nrow(slits);
13695 slit_id = cpl_table_get_data_int(slits,
"slit_id");
13696 order = cpl_table_get_ncol(polytraces) - 2;
13703 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
13704 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
13706 for (i = 0; i < nslits; i++) {
13708 if (length[i] == 0)
13722 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
13724 start_pixel = refpixel - pixel_below;
13725 if (start_pixel < 0)
13728 end_pixel = refpixel + pixel_above;
13729 if (end_pixel > nx)
13738 polytop = cpl_polynomial_new(1);
13739 for (k = 0; k <= order; k++) {
13740 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
13742 cpl_polynomial_delete(polytop);
13746 cpl_polynomial_set_coeff(polytop, &k, coeff);
13750 polybot = cpl_polynomial_new(1);
13751 for (k = 0; k <= order; k++) {
13752 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
13754 cpl_polynomial_delete(polybot);
13758 cpl_polynomial_set_coeff(polybot, &k, coeff);
13761 if (missing_top && missing_bot) {
13762 cpl_msg_warning(func,
"Spatial map, slit %d was not traced!",
13774 cpl_msg_warning(func,
"Upper edge of slit %d was not traced: "
13775 "the spectral curvature of the lower edge "
13776 "is used instead.", slit_id[i]);
13777 polytop = cpl_polynomial_duplicate(polybot);
13778 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
13779 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
13781 coeff = cpl_polynomial_get_coeff(polybot, &k);
13782 coeff += ytop - ybot;
13783 cpl_polynomial_set_coeff(polytop, &k, coeff);
13787 cpl_msg_warning(func,
"Lower edge of slit %d was not traced: "
13788 "the spectral curvature of the upper edge "
13789 "is used instead.", slit_id[i]);
13790 polybot = cpl_polynomial_duplicate(polytop);
13791 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
13792 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
13794 coeff = cpl_polynomial_get_coeff(polytop, &k);
13795 coeff -= ytop - ybot;
13796 cpl_polynomial_set_coeff(polybot, &k, coeff);
13799 top = cpl_polynomial_eval_1d(polytop, refpixel, NULL);
13800 bot = cpl_polynomial_eval_1d(polybot, refpixel, NULL);
13801 npseudo = ceil(top-bot) + 1;
13804 cpl_polynomial_delete(polytop);
13805 cpl_polynomial_delete(polybot);
13806 cpl_msg_warning(func,
"Slit %d was badly traced: no extraction!",
13811 for (j = start_pixel; j < end_pixel; j++) {
13812 top = cpl_polynomial_eval_1d(polytop, j, NULL);
13813 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
13814 factor = (top-bot)/npseudo;
13815 for (k = 0; k <= npseudo; k++) {
13816 ypos = top - k*factor;
13818 yfra = ypos - yint;
13819 if (yint >= 0 && yint < ny-1) {
13820 data[j + nx*yint] = (top-yint)/factor;
13829 if (yprev - yint > 1) {
13830 data[j + nx*(yint+1)] = (top-yint-1)/factor;
13837 cpl_polynomial_delete(polytop);
13838 cpl_polynomial_delete(polybot);
13841 return calibration;
13908 int maxradius,
int conradius)
13910 const char *func =
"mos_detect_objects";
13912 cpl_image *profile;
13916 char name[MAX_COLNAME];
13920 int nobjects, objpos, totobj;
13927 double mindistance;
13934 const int min_pixels = 10;
13937 if (cpl_error_get_code() != CPL_ERROR_NONE)
13940 if (image == NULL || slits == NULL) {
13941 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13948 if (maxradius < 0) {
13949 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13953 if (conradius < 0) {
13954 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13958 nslits = cpl_table_get_nrow(slits);
13959 position = cpl_table_get_data_int(slits,
"position");
13960 length = cpl_table_get_data_int(slits,
"length");
13962 profile = cpl_image_collapse_create(image, 1);
13963 cpl_image_divide_scalar(profile, cpl_image_get_size_x(image));
13964 pdata = cpl_image_get_data(profile);
13969 for (i = 0; i < nslits; i++) {
13971 if (length[i] == 0)
13974 pos = position[i] + margin;
13975 count = length[i] - 2*margin;
13977 if (count < min_pixels)
13988 if (p[0] > p[1] && p[1] > p[2] && p[2] > p[3] && p[3] > 0) {
13993 for (j = 0; j < count - 3; j++) {
13995 if (p[j+1] > p[j]) {
14000 if (p[j+1] > p[j+2] && p[j+2] > 0) {
14006 if (p[j+1] > p[j+2] && p[j+2] > p[j+3] && p[j+3] > 0) {
14019 if (p[count-1] > p[count-2] && p[count-2] > p[count-3]
14020 && p[count-3] > p[count-4] && p[count-4] > 0) {
14032 reject = cpl_calloc(npeaks,
sizeof(
int));
14033 bright = cpl_calloc(npeaks,
sizeof(
double));
14034 place = cpl_calloc(npeaks,
sizeof(
double));
14037 if (p[0] > p[1] && p[1] > p[2] && p[2] > p[3] && p[3] > 0) {
14039 place[0] = position[i] + margin;
14044 for (j = 0; j < count - 3; j++) {
14046 if (p[j+1] > p[j]) {
14051 if (p[j+1] > p[j+2] && p[j+2] > 0) {
14053 bright[npeaks] = p[j];
14054 place[npeaks] = position[i] + margin + j + 1
14055 + values_to_dx(p[j-1], p[j], p[j+1]);
14061 if (p[j+1] > p[j+2] && p[j+2] > p[j+3] && p[j+3] > 0) {
14063 bright[npeaks] = p[j];
14064 place[npeaks] = position[i] + margin + j + 1
14065 + values_to_dx(p[j-1], p[j], p[j+1]);
14078 if (p[count-1] > p[count-2] && p[count-2] > p[count-3]
14079 && p[count-3] > p[count-4] && p[count-4] > 0) {
14080 bright[npeaks] = p[count-1];
14081 place[npeaks] = position[i] + count;
14090 if (fabs(place[0] - pos) < 1.0)
14092 if (fabs(place[npeaks-1] - pos - count) < 1.0)
14093 reject[npeaks-1] = 1;
14094 for (j = 0; j < npeaks; j++) {
14095 for (k = 0; k < npeaks; k++) {
14098 mindistance = conradius * bright[k] / bright[j]
14099 * bright[k] / bright[j];
14100 if (fabs(place[j] - place[k]) < mindistance)
14106 for (j = 0; j < npeaks; j++) {
14110 low = (place[j-1]*bright[j] + place[j]*bright[j-1])
14111 / (bright[j-1] + bright[j]) + 1;
14116 if (j < npeaks - 1) {
14117 hig = (place[j+1]*bright[j] + place[j]*bright[j+1])
14118 / (bright[j+1] + bright[j]) + 1;
14126 if (hig > pos + count)
14128 if (place[j] - low > maxradius)
14129 low = place[j] - maxradius;
14130 if (hig - place[j] > maxradius)
14131 hig = place[j] + maxradius;
14138 for (j = 0; j < npeaks; j++)
14142 for (j = 0; j < nobjects; j++) {
14143 snprintf(name, MAX_COLNAME,
"object_%d", j+1);
14144 if (cpl_table_has_column(slits, name))
14146 cpl_table_new_column(slits, name, CPL_TYPE_DOUBLE);
14147 snprintf(name, MAX_COLNAME,
"start_%d", j+1);
14148 cpl_table_new_column(slits, name, CPL_TYPE_INT);
14149 cpl_table_set_column_unit(slits, name,
"pixel");
14150 snprintf(name, MAX_COLNAME,
"end_%d", j+1);
14151 cpl_table_new_column(slits, name, CPL_TYPE_INT);
14152 cpl_table_set_column_unit(slits, name,
"pixel");
14153 snprintf(name, MAX_COLNAME,
"row_%d", j+1);
14154 cpl_table_new_column(slits, name, CPL_TYPE_INT);
14155 cpl_table_set_column_unit(slits, name,
"pixel");
14159 for (j = 0; j < npeaks; j++) {
14163 low = (place[j-1]*bright[j] + place[j]*bright[j-1])
14164 / (bright[j-1] + bright[j]) + 1;
14169 if (j < npeaks - 1) {
14170 hig = (place[j+1]*bright[j] + place[j]*bright[j+1])
14171 / (bright[j+1] + bright[j]) + 1;
14179 if (hig > pos + count)
14181 if (place[j] - low > maxradius)
14182 low = place[j] - maxradius;
14183 if (hig - place[j] > maxradius)
14184 hig = place[j] + maxradius;
14186 snprintf(name, MAX_COLNAME,
"object_%d", objpos);
14187 cpl_table_set_double(slits, name, i, place[j]);
14188 snprintf(name, MAX_COLNAME,
"start_%d", objpos);
14189 cpl_table_set_int(slits, name, i, low);
14190 snprintf(name, MAX_COLNAME,
"end_%d", objpos);
14191 cpl_table_set_int(slits, name, i, hig);
14192 snprintf(name, MAX_COLNAME,
"row_%d", objpos);
14193 cpl_table_set_int(slits, name, i, row + objpos - 1);
14200 if (maxobjects < nobjects)
14201 maxobjects = nobjects;
14210 row = cpl_table_get_nrow(slits);
14212 for (i = 0; i < row; i++) {
14213 for (j = 0; j < maxobjects; j++) {
14214 snprintf(name, MAX_COLNAME,
"row_%d", j+1);
14215 if (cpl_table_is_valid(slits, name, i))
14216 cpl_table_set_int(slits, name, i, totobj -
14217 cpl_table_get_int(slits, name, i, NULL));
14221 for (i = 0; i < maxobjects; i++) {
14222 snprintf(name, MAX_COLNAME,
"start_%d", i+1);
14223 cpl_table_fill_invalid_int(slits, name, -1);
14224 snprintf(name, MAX_COLNAME,
"end_%d", i+1);
14225 cpl_table_fill_invalid_int(slits, name, -1);
14226 snprintf(name, MAX_COLNAME,
"row_%d", i+1);
14227 cpl_table_fill_invalid_int(slits, name, -1);
14260 cpl_table *objects,
int extraction,
double ron,
14261 double gain,
int ncombined)
14263 const char *func =
"mos_extract_objects";
14265 char name[MAX_COLNAME];
14267 cpl_image **output;
14268 cpl_image *extracted;
14269 cpl_image *extr_sky;
14272 cpl_image *sci_var_win = NULL;
14282 if (science == NULL || sky == NULL) {
14283 cpl_msg_error(func,
"Both scientific exposures are required in input");
14284 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14288 if (objects == NULL) {
14289 cpl_msg_error(func,
"An object table is required in input");
14290 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14294 if (extraction < 0 || extraction > 1) {
14295 cpl_msg_error(func,
"Invalid extraction mode (%d): it should be "
14296 "either 0 or 1", extraction);
14297 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14302 cpl_msg_error(func,
"Invalid read-out-noise (%f ADU)", ron);
14303 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14308 cpl_msg_error(func,
"Invalid gain factor (%f e-/ADU)", gain);
14309 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14313 if (ncombined < 1) {
14314 cpl_msg_error(func,
"Invalid number of combined frames (%d): "
14315 "it should be at least 1", ncombined);
14316 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14327 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
14328 while (cpl_table_has_column(objects, name)) {
14330 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
14339 nslits = cpl_table_get_nrow(objects);
14341 for (i = 0; i < nslits; i++) {
14342 for (j = 1; j < maxobjects; j++) {
14343 snprintf(name, MAX_COLNAME,
"object_%d", j);
14344 if (cpl_table_is_valid(objects, name, i))
14352 nx = cpl_image_get_size_x(science);
14354 output = cpl_calloc(3,
sizeof(cpl_image *));
14355 extracted = output[0] = cpl_image_new(nx, nobjects, CPL_TYPE_FLOAT);
14356 extr_sky = output[1] = cpl_image_new(nx, nobjects, CPL_TYPE_FLOAT);
14357 error = output[2] = cpl_image_new(nx, nobjects, CPL_TYPE_FLOAT);
14365 for (i = 0; i < nslits; i++) {
14366 for (j = 1; j < maxobjects; j++) {
14367 snprintf(name, MAX_COLNAME,
"object_%d", j);
14368 if (cpl_table_is_valid(objects, name, i)) {
14369 snprintf(name, MAX_COLNAME,
"start_%d", j);
14370 ylow = cpl_table_get_int(objects, name, i, NULL);
14371 snprintf(name, MAX_COLNAME,
"end_%d", j);
14372 yhig = cpl_table_get_int(objects, name, i, NULL);
14373 snprintf(name, MAX_COLNAME,
"row_%d", j);
14374 nobjects = cpl_table_get_int(objects, name, i, NULL);
14375 sciwin = cpl_image_extract(science, 1, ylow+1, nx, yhig);
14376 if(science_var != NULL)
14377 sci_var_win = cpl_image_extract(science_var, 1, ylow+1, nx, yhig);
14378 skywin = cpl_image_extract(sky, 1, ylow+1, nx, yhig);
14387 mos_extraction(sciwin, sci_var_win, skywin, extracted, extr_sky, error,
14388 nobjects, extraction, ron, gain, ncombined);
14395 cpl_image *total = cpl_image_add_create(sciwin, skywin);
14396 float *data = cpl_image_get_data_float(total);
14397 int size = cpl_image_get_size_x(total)
14398 * cpl_image_get_size_y(total);
14400 char *saturation_level = getenv(
"SATURATION_LEVEL");
14401 float saturation = 62000.0;
14402 char *max_saturated = getenv(
"MAX_SATURATED");
14403 int max_satur = 10;
14406 if (saturation_level)
14407 saturation = atof(saturation_level);
14410 max_satur = atoi(max_saturated);
14413 for (k = 0; k < size; k++) {
14414 if (data[k] > saturation) {
14416 if (saturated > max_satur) {
14422 if (saturated > max_satur)
14427 data = cpl_image_get_data(extracted);
14428 data[nobjects * nx] = saturated;
14431 cpl_image_delete(sciwin);
14432 cpl_image_delete(skywin);
14466 double dispersion,
int saturation,
14467 double *mfwhm,
double *rmsfwhm,
14468 double *resolution,
double *rmsres,
int *nlines)
14470 cpl_vector *vector;
14473 int position, maxpos;
14478 int threshold = 250;
14483 double min, max, halfmax;
14494 xlen = cpl_image_get_size_x(image);
14495 ylen = cpl_image_get_size_y(image);
14496 data = cpl_image_get_data(image);
14498 buffer = cpl_malloc(ylen *
sizeof(
double));
14504 position = floor((lambda - startwave) / dispersion + 0.5);
14506 sp = position - sradius;
14507 ep = position + sradius;
14509 if (sp < 0 || ep > xlen) {
14514 for (i = 0, n = 0; i < ylen; i++) {
14525 sp = position - radius;
14526 ep = position + radius;
14528 if (sp < 0 || ep > xlen) {
14539 min = max = data[sp + i * xlen];
14540 for (j = sp; j < ep; j++) {
14541 if (data[j + i * xlen] > max) {
14542 max = data[j + i * xlen];
14545 if (data[j + i * xlen] < min) {
14546 min = data[j + i * xlen];
14550 if (fabs(min) < 0.0000001)
14553 if (max - min < threshold)
14556 if (max > saturation)
14566 halfmax = (max + min)/ 2.0;
14570 for (j = maxpos; j < maxpos + radius; j++) {
14572 if (data[j + i * xlen] < halfmax) {
14573 fwhm = ifwhm + (data[j - 1 + i * xlen] - halfmax)
14574 / (data[j - 1 + i * xlen] - data[j + i * xlen]);
14582 for (j = maxpos; j > maxpos - radius; j--) {
14584 if (data[j + i * xlen] < halfmax) {
14585 fwhm += ifwhm + (data[j + 1 + i * xlen] - halfmax)
14586 / (data[j + 1 + i * xlen] - data[j + i * xlen]);
14594 buffer[n] = fwhm - 2.0;
14605 vector = cpl_vector_wrap(n, buffer);
14606 value = cpl_vector_get_median_const(vector);
14607 cpl_vector_unwrap(vector);
14610 for (i = 0, m = 0; i < n; i++) {
14611 if (fabs(buffer[i] - value) < cut) {
14612 rms += fabs(buffer[i] - value);
14625 value *= dispersion;
14631 *resolution = lambda / value;
14632 *rmsres = *resolution * rms / value;
14662 double dispersion,
int saturation,
14677 nref = cpl_vector_get_size(lines);
14678 line = cpl_vector_get_data(lines);
14680 table = cpl_table_new(nref);
14681 cpl_table_new_column(table,
"wavelength", CPL_TYPE_DOUBLE);
14682 cpl_table_set_column_unit(table,
"wavelength",
"Angstrom");
14683 cpl_table_new_column(table,
"fwhm", CPL_TYPE_DOUBLE);
14684 cpl_table_set_column_unit(table,
"fwhm",
"Angstrom");
14685 cpl_table_new_column(table,
"fwhm_rms", CPL_TYPE_DOUBLE);
14686 cpl_table_set_column_unit(table,
"fwhm_rms",
"Angstrom");
14687 cpl_table_new_column(table,
"resolution", CPL_TYPE_DOUBLE);
14688 cpl_table_new_column(table,
"resolution_rms", CPL_TYPE_DOUBLE);
14689 cpl_table_new_column(table,
"nlines", CPL_TYPE_INT);
14691 for (i = 0; i < nref; i++) {
14693 saturation, &fwhm, &rmsfwhm,
14694 &resolution, &rmsres, &nlines)) {
14695 cpl_table_set_double(table,
"wavelength", i, line[i]);
14696 cpl_table_set_double(table,
"fwhm", i, fwhm);
14697 cpl_table_set_double(table,
"fwhm_rms", i, rmsfwhm);
14698 cpl_table_set_double(table,
"resolution", i, resolution);
14699 cpl_table_set_double(table,
"resolution_rms", i, rmsres);
14700 cpl_table_set_int(table,
"nlines", i, nlines);
14703 cpl_table_set_int(table,
"nlines", i, 0);
14706 if (cpl_table_has_valid(table,
"wavelength"))
14709 cpl_table_delete(table);
14734 int ystart,
int yend,
double wstart,
double wend)
14736 const char *func =
"mos_integrate_signal";
14745 if (image == NULL || wavemap == NULL) {
14746 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14750 if (ystart > yend || wstart >= wend) {
14751 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14755 nx = cpl_image_get_size_x(image);
14756 ny = cpl_image_get_size_y(image);
14758 if (!(nx == cpl_image_get_size_x(wavemap)
14759 && ny == cpl_image_get_size_y(wavemap))) {
14760 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
14764 if (ystart < 0 || yend > ny) {
14765 cpl_error_set(func, CPL_ERROR_ACCESS_OUT_OF_RANGE);
14769 sdata = cpl_image_get_data(image);
14770 wdata = cpl_image_get_data(wavemap);
14772 sdata += ystart*nx;
14773 wdata += ystart*nx;
14776 for (y = ystart; y < yend; y++) {
14777 for (x = 0; x < nx; x++) {
14778 if (wdata[x] < wstart || wdata[x] > wend)
14823 const char *func =
"mos_load_slits_fors_mxu";
14826 char keyname[MAX_COLNAME];
14827 const char *instrume;
14828 const char *target_name;
14833 double arc2mm = 0.528;
14846 float low_limit1 = 10.0;
14847 float hig_limit2 = 30.0;
14850 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14854 if (header == NULL) {
14855 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14864 instrume = cpl_propertylist_get_string(header,
"INSTRUME");
14867 if (instrume[4] ==
'1')
14869 if (instrume[4] ==
'2')
14873 cpl_msg_error(func,
"Wrong instrument: %s\n"
14874 "FORS2 is expected for MXU data", instrume);
14875 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14886 chip = cpl_propertylist_get_int(header,
"ESO DET CHIP1 Y");
14888 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14889 cpl_msg_error(func,
"Missing keyword ESO DET CHIP1 Y "
14891 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14895 if (chip != 1 && chip != 2) {
14896 cpl_msg_error(func,
"Unexpected chip position in keyword "
14897 "ESO DET CHIP1 Y: %d", chip);
14898 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14914 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d YPOS", slit_id + 100);
14915 if (cpl_propertylist_has(header, keyname)) {
14916 slit_y = cpl_propertylist_get_double(header, keyname);
14919 if (slit_y < low_limit1)
14922 if (slit_y > hig_limit2)
14925 snprintf(keyname, MAX_COLNAME,
"ESO INS TARG%d NAME",
14927 if (cpl_propertylist_has(header, keyname)) {
14928 target_name = cpl_propertylist_get_string(header, keyname);
14929 if (strncmp(target_name,
"refslit", 7))
14939 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14940 cpl_msg_error(func,
"%s while loading slits coordinates from "
14941 "FITS header", cpl_error_get_message());
14942 cpl_error_set_where(func);
14947 cpl_msg_error(func,
"No slits coordinates found in header");
14948 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
14952 slits = cpl_table_new(nslits);
14953 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
14954 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
14955 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
14956 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
14957 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
14958 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
14959 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
14960 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
14961 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
14968 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d YPOS", slit_id + 100);
14969 if (cpl_propertylist_has(header, keyname)) {
14970 slit_y = cpl_propertylist_get_double(header, keyname);
14973 if (slit_y < low_limit1)
14976 if (slit_y > hig_limit2)
14986 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d XPOS", slit_id + 100);
14987 slit_x = cpl_propertylist_get_double(header, keyname);
14988 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14989 cpl_table_delete(slits);
14990 cpl_msg_error(func,
"Missing keyword %s in FITS header",
14992 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14996 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d LEN", slit_id + 100);
14997 length = cpl_propertylist_get_double(header, keyname);
14998 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14999 cpl_table_delete(slits);
15000 cpl_msg_error(func,
"Missing keyword %s in FITS header",
15002 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15008 snprintf(keyname, MAX_COLNAME,
"ESO INS TARG%d NAME",
15010 if (cpl_propertylist_has(header, keyname)) {
15011 target_name = cpl_propertylist_get_string(header, keyname);
15012 if (strncmp(target_name,
"refslit", 7)) {
15013 cpl_table_set_int(slits,
"slit_id", nslits, slit_id);
15014 cpl_table_set(slits,
"xtop", nslits, slit_x);
15015 cpl_table_set(slits,
"ytop", nslits, slit_y + length/2);
15016 cpl_table_set(slits,
"xbottom", nslits, slit_x);
15017 cpl_table_set(slits,
"ybottom", nslits, slit_y - length/2);
15022 cpl_table_set_int(slits,
"slit_id", nslits, slit_id);
15023 cpl_table_set(slits,
"xtop", nslits, slit_x);
15024 cpl_table_set(slits,
"ytop", nslits, slit_y + length/2);
15025 cpl_table_set(slits,
"xbottom", nslits, slit_x);
15026 cpl_table_set(slits,
"ybottom", nslits, slit_y - length/2);
15062 int * nslits_out_det)
15064 const char *func =
"mos_load_slits_fors_mos";
15067 char keyname[MAX_COLNAME];
15068 const char *instrume;
15069 const char *chipname;
15071 int first_slit, last_slit;
15082 float ytop[19] = { 113.9, 101.3, 89.9, 77.3, 65.9, 53.3,
15083 41.9, 29.3, 17.9, 5.3, -6.1, -18.7,
15084 -30.1, -42.7, -54.1, -66.7, -78.1, -90.7,
15086 float ybottom[19] = { 102.1, 90.7, 78.1, 66.7, 54.1, 42.7,
15087 30.1, 18.7, 6.1, -5.3, -17.9, -29.3,
15088 -41.9, -53.3, -65.9, -77.3, -89.9, -101.3,
15092 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15096 if (header == NULL) {
15097 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15106 instrume = cpl_propertylist_get_string(header,
"INSTRUME");
15109 if (instrume[4] ==
'1')
15111 if (instrume[4] ==
'2')
15115 cpl_msg_error(func,
"Wrong instrument found in FITS header: %s",
15117 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15127 chipname = cpl_propertylist_get_string(header,
"ESO DET CHIP1 ID");
15129 if (chipname[0] ==
'M' || chipname[0] ==
'N')
15134 if (fors == 1 && fors_is_old) {
15146 chip = cpl_propertylist_get_int(header,
"ESO DET CHIP1 Y");
15148 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15149 cpl_msg_error(func,
"Missing keyword ESO DET CHIP1 Y "
15151 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15155 if (chip != 1 && chip != 2) {
15156 cpl_msg_error(func,
"Unexpected chip position in keyword "
15157 "ESO DET CHIP1 Y: %d", chip);
15158 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15181 for (slit_id = first_slit; slit_id <= last_slit; slit_id++) {
15182 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d POS", slit_id);
15183 if (cpl_propertylist_has(header, keyname)) {
15184 slit_x = cpl_propertylist_get_double(header, keyname);
15185 if (fabs(slit_x) < 115.0)
15188 (*nslits_out_det)++;
15191 cpl_msg_error(func,
"Missing keyword %s in FITS header", keyname);
15192 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15197 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15198 cpl_msg_error(func,
"%s while loading slits coordinates from "
15199 "FITS header", cpl_error_get_message());
15200 cpl_error_set_where(func);
15205 cpl_msg_error(func,
"No slits coordinates found in header");
15206 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
15210 slits = cpl_table_new(nslits);
15211 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
15212 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15213 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15214 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15215 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15216 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15217 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15218 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15219 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15223 for (slit_id = first_slit; slit_id <= last_slit; slit_id++) {
15224 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d POS", slit_id);
15225 slit_x = cpl_propertylist_get_double(header, keyname);
15226 if (fabs(slit_x) < 115.0) {
15227 cpl_table_set_int(slits,
"slit_id", nslits, slit_id);
15228 cpl_table_set(slits,
"xtop", nslits, slit_x);
15229 cpl_table_set(slits,
"ytop", nslits, ytop[slit_id-1]);
15230 cpl_table_set(slits,
"xbottom", nslits, slit_x);
15231 cpl_table_set(slits,
"ybottom", nslits, ybottom[slit_id-1]);
15265 const char *func =
"mos_load_slits_fors_lss";
15269 const char *instrume;
15275 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15279 if (header == NULL) {
15280 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15289 instrume = cpl_propertylist_get_string(header,
"INSTRUME");
15292 if (instrume[4] ==
'1')
15294 if (instrume[4] ==
'2')
15298 cpl_msg_error(func,
"Wrong instrument found in FITS header: %s",
15300 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15316 chip = cpl_propertylist_get_int(header,
"ESO DET CHIP1 Y");
15318 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15319 cpl_msg_error(func,
"Missing keyword ESO DET CHIP1 Y "
15321 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15325 if (chip != 1 && chip != 2) {
15326 cpl_msg_error(func,
"Unexpected chip position in keyword "
15327 "ESO DET CHIP1 Y: %d", chip);
15328 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15343 slits = cpl_table_new(1);
15344 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
15345 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15346 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15347 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15348 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15349 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15350 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15351 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15352 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15354 slit_name = (
char *)cpl_propertylist_get_string(header,
15355 "ESO INS SLIT NAME");
15357 cpl_table_set(slits,
"ytop", 0, ytop);
15358 cpl_table_set(slits,
"ybottom", 0, ybottom);
15360 if (!strncmp(slit_name,
"lSlit0_3arcsec", 14)) {
15361 cpl_table_set_int(slits,
"slit_id", 0, 1);
15362 cpl_table_set(slits,
"xbottom", 0, -0.075);
15363 cpl_table_set(slits,
"xtop", 0, 0.075);
15365 else if (!strncmp(slit_name,
"lSlit0_4arcsec", 14)) {
15366 cpl_table_set_int(slits,
"slit_id", 0, 2);
15367 cpl_table_set(slits,
"xbottom", 0, 5.895);
15368 cpl_table_set(slits,
"xtop", 0, 6.105);
15370 else if (!strncmp(slit_name,
"lSlit0_5arcsec", 14)) {
15371 cpl_table_set_int(slits,
"slit_id", 0, 3);
15372 cpl_table_set(slits,
"xbottom", 0, -6.135);
15373 cpl_table_set(slits,
"xtop", 0, -5.865);
15375 else if (!strncmp(slit_name,
"lSlit0_7arcsec", 14)) {
15376 cpl_table_set_int(slits,
"slit_id", 0, 4);
15377 cpl_table_set(slits,
"xbottom", 0, 11.815);
15378 cpl_table_set(slits,
"xtop", 0, 12.185);
15380 else if (!strncmp(slit_name,
"lSlit1_0arcsec", 14)) {
15381 cpl_table_set_int(slits,
"slit_id", 0, 5);
15382 cpl_table_set(slits,
"xbottom", 0, -12.265);
15383 cpl_table_set(slits,
"xtop", 0, -11.735);
15385 else if (!strncmp(slit_name,
"lSlit1_3arcsec", 14)) {
15386 cpl_table_set_int(slits,
"slit_id", 0, 6);
15387 cpl_table_set(slits,
"xbottom", 0, 17.655);
15388 cpl_table_set(slits,
"xtop", 0, 18.345);
15390 else if (!strncmp(slit_name,
"lSlit1_6arcsec", 14)) {
15391 cpl_table_set_int(slits,
"slit_id", 0, 7);
15392 cpl_table_set(slits,
"xbottom", 0, -18.425);
15393 cpl_table_set(slits,
"xtop", 0, -17.575);
15395 else if (!strncmp(slit_name,
"lSlit2_0arcsec", 14)) {
15396 cpl_table_set_int(slits,
"slit_id", 0, 8);
15397 cpl_table_set(slits,
"xbottom", 0, 23.475);
15398 cpl_table_set(slits,
"xtop", 0, 24.525);
15400 else if (!strncmp(slit_name,
"lSlit2_5arcsec", 14)) {
15401 cpl_table_set_int(slits,
"slit_id", 0, 9);
15402 cpl_table_set(slits,
"xbottom", 0, -24.66);
15403 cpl_table_set(slits,
"xtop", 0, -23.34);
15406 cpl_msg_error(func,
"Invalid slit %s in keyword ESO INS SLIT NAME",
15408 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
15409 cpl_table_delete(slits);
15433 const char *func =
"mos_get_gain_vimos";
15435 double gain = -1.0;
15438 if (cpl_error_get_code() != CPL_ERROR_NONE)
15441 if (header == NULL) {
15442 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15446 gain = cpl_propertylist_get_double(header,
"ESO DET OUT1 CONAD");
15447 if (cpl_error_get_code()) {
15448 cpl_error_set_where(func);
15478 const char *func =
"mos_load_slits_vimos";
15481 char keyname[MAX_COLNAME];
15492 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15496 if (header == NULL) {
15497 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15501 nslits = cpl_propertylist_get_int(header,
"ESO INS SLIT NO");
15503 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15504 cpl_error_set_where(func);
15508 slits = cpl_table_new(nslits);
15509 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
15510 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15511 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15512 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15513 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15514 cpl_table_new_column(slits,
"xwidth", CPL_TYPE_DOUBLE);
15515 cpl_table_new_column(slits,
"ywidth", CPL_TYPE_DOUBLE);
15516 cpl_table_new_column(slits,
"curved", CPL_TYPE_INT);
15517 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15518 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15519 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15520 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15521 cpl_table_set_column_unit(slits,
"xwidth",
"mm");
15522 cpl_table_set_column_unit(slits,
"ywidth",
"mm");
15524 for (i = 0; i < nslits; i++) {
15525 sprintf(keyname,
"ESO INS SLIT%d ID", i+1);
15526 slit_id = cpl_propertylist_get_int(header, keyname);
15527 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15528 cpl_error_set_where(func);
15531 sprintf(keyname,
"ESO INS SLIT%d X", i+1);
15532 slit_x = cpl_propertylist_get_double(header, keyname);
15533 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15534 cpl_error_set_where(func);
15537 sprintf(keyname,
"ESO INS SLIT%d Y", i+1);
15538 slit_y = cpl_propertylist_get_double(header, keyname);
15539 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15540 cpl_error_set_where(func);
15543 sprintf(keyname,
"ESO INS SLIT%d DIMX", i+1);
15544 dim_x = cpl_propertylist_get_double(header, keyname);
15545 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15546 cpl_error_set_where(func);
15550 sprintf(keyname,
"ESO INS SLIT%d BEZIER DY", i+1);
15551 if (cpl_propertylist_has(header, keyname)) {
15555 sprintf(keyname,
"ESO INS SLIT%d DIMY", i+1);
15558 dim_y = cpl_propertylist_get_double(header, keyname);
15559 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15560 cpl_error_set_where(func);
15564 cpl_table_set_int(slits,
"slit_id", i, slit_id);
15565 cpl_table_set(slits,
"xtop", i, slit_x - dim_x/2);
15566 cpl_table_set(slits,
"ytop", i, slit_y);
15567 cpl_table_set(slits,
"xbottom", i, slit_x + dim_x/2);
15568 cpl_table_set(slits,
"ybottom", i, slit_y);
15569 cpl_table_set(slits,
"xwidth", i, dim_x);
15570 cpl_table_set(slits,
"ywidth", i, dim_y);
15571 cpl_table_set_int(slits,
"curved", i, curved);
15589 cpl_propertylist *sort;
15591 int i, multiplex, xprev, xcur;
15593 double tolerance = 1.0;
15603 sort = cpl_propertylist_new();
15604 cpl_propertylist_append_bool(sort,
"xtop", 0);
15605 cpl_table_sort(slits, sort);
15606 cpl_propertylist_delete(sort);
15608 prev = cpl_table_get_double(slits,
"xtop", 0, NULL);
15609 cpl_table_new_column(slits,
"xind", CPL_TYPE_INT);
15610 cpl_table_set_int(slits,
"xind", 0, prev);
15611 nrow = cpl_table_get_nrow(slits);
15612 for (i = 1; i < nrow; i++) {
15613 cur = cpl_table_get_double(slits,
"xtop", i, NULL);
15614 if (fabs(prev - cur) > tolerance)
15616 cpl_table_set_int(slits,
"xind", i, prev);
15624 sort = cpl_propertylist_new();
15625 cpl_propertylist_append_bool(sort,
"xind", 0);
15626 cpl_propertylist_append_bool(sort,
"ytop", 0);
15627 cpl_table_sort(slits, sort);
15628 cpl_propertylist_delete(sort);
15635 cpl_table_new_column(slits,
"multiplex", CPL_TYPE_INT);
15636 xprev = cpl_table_get_int(slits,
"xind", 0, NULL);
15637 cpl_table_set_int(slits,
"multiplex", 0, multiplex);
15638 nrow = cpl_table_get_nrow(slits);
15639 for (i = 1; i < nrow; i++) {
15640 xcur = cpl_table_get_int(slits,
"xind", i, NULL);
15641 if (xcur == xprev) {
15648 cpl_table_set_int(slits,
"multiplex", i, multiplex);
15651 cpl_table_save(slits, NULL, NULL,
"multiplex.fits", CPL_IO_DEFAULT);
15653 cpl_table_erase_column(slits,
"xind");
15655 return 1 + cpl_table_get_column_max(slits,
"multiplex");
15687 int check_consistency)
15689 const char *func =
"mos_load_overscans_vimos";
15700 cpl_table *overscans;
15703 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15704 cpl_msg_error(func,
"Reset your error: %s", cpl_error_get_message());
15708 if (header == NULL) {
15709 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15713 if (cpl_propertylist_has(header,
"NAXIS1"))
15714 nx = cpl_propertylist_get_int(header,
"NAXIS1");
15715 if (cpl_propertylist_has(header,
"NAXIS2"))
15716 ny = cpl_propertylist_get_int(header,
"NAXIS2");
15717 if (cpl_propertylist_has(header,
"ESO DET OUT1 PRSCX"))
15718 px = cpl_propertylist_get_int(header,
"ESO DET OUT1 PRSCX");
15719 if (cpl_propertylist_has(header,
"ESO DET OUT1 PRSCY"))
15720 py = cpl_propertylist_get_int(header,
"ESO DET OUT1 PRSCY");
15721 if (cpl_propertylist_has(header,
"ESO DET OUT1 OVSCX"))
15722 ox = cpl_propertylist_get_int(header,
"ESO DET OUT1 OVSCX");
15723 if (cpl_propertylist_has(header,
"ESO DET OUT1 OVSCY"))
15724 oy = cpl_propertylist_get_int(header,
"ESO DET OUT1 OVSCY");
15725 if (cpl_propertylist_has(header,
"ESO DET OUT1 NX"))
15726 vx = cpl_propertylist_get_int(header,
"ESO DET OUT1 NX");
15727 if (cpl_propertylist_has(header,
"ESO DET OUT1 NY"))
15728 vy = cpl_propertylist_get_int(header,
"ESO DET OUT1 NY");
15730 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15731 cpl_msg_error(func,
"Missing overscan keywords in header");
15732 cpl_error_set_where(func);
15736 if (px < 0 || py < 0 || ox < 0 || oy < 0) {
15737 cpl_msg_error(func,
"Missing overscan keywords in header");
15738 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15742 if ((px + vx + ox != nx) || (py + vy + oy != ny)) {
15743 if (check_consistency) {
15744 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15748 cpl_msg_debug(func,
"Overscans description conflicts with "
15749 "reported image sizes, "
15750 "%d + %d + %d != %d or "
15751 "%d + %d + %d != %d",
15768 cpl_msg_error(func,
"Unexpected overscan regions "
15769 "(both in X and Y direction)");
15770 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15782 overscans = cpl_table_new(nrows);
15783 cpl_table_new_column(overscans,
"xlow", CPL_TYPE_INT);
15784 cpl_table_new_column(overscans,
"ylow", CPL_TYPE_INT);
15785 cpl_table_new_column(overscans,
"xhig", CPL_TYPE_INT);
15786 cpl_table_new_column(overscans,
"yhig", CPL_TYPE_INT);
15790 cpl_table_set_int(overscans,
"xlow", nrows, px);
15791 cpl_table_set_int(overscans,
"ylow", nrows, py);
15792 cpl_table_set_int(overscans,
"xhig", nrows, nx - ox);
15793 cpl_table_set_int(overscans,
"yhig", nrows, ny - oy);
15797 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15798 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15799 cpl_table_set_int(overscans,
"xhig", nrows, px);
15800 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15805 cpl_table_set_int(overscans,
"xlow", nrows, nx - ox);
15806 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15807 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15808 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15813 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15814 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15815 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15816 cpl_table_set_int(overscans,
"yhig", nrows, py);
15821 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15822 cpl_table_set_int(overscans,
"ylow", nrows, ny - oy);
15823 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15824 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15833 cpl_table *mos_load_overscans_fors(
const cpl_propertylist *header)
15835 const char *func =
"mos_load_overscans_fors";
15846 cpl_table *overscans;
15849 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15850 cpl_msg_error(func,
"Reset your error: %s", cpl_error_get_message());
15854 if (header == NULL) {
15855 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15859 if (cpl_propertylist_has(header,
"ESO DET OUTPUTS"))
15860 nports = cpl_propertylist_get_int(header,
"ESO DET OUTPUTS");
15863 cpl_propertylist_has(header,
"ESO DET OUT1 PRSCX") &&
15864 cpl_propertylist_has(header,
"ESO DET WIN1 BINX")) {
15866 rebin = cpl_propertylist_get_int(header,
"ESO DET WIN1 BINX");
15868 overscans = cpl_table_new(3);
15869 cpl_table_new_column(overscans,
"xlow", CPL_TYPE_INT);
15870 cpl_table_new_column(overscans,
"ylow", CPL_TYPE_INT);
15871 cpl_table_new_column(overscans,
"xhig", CPL_TYPE_INT);
15872 cpl_table_new_column(overscans,
"yhig", CPL_TYPE_INT);
15880 cpl_table_set_int(overscans,
"xlow", nrows, px);
15881 cpl_table_set_int(overscans,
"ylow", nrows, py);
15882 cpl_table_set_int(overscans,
"xhig", nrows, nx - ox);
15883 cpl_table_set_int(overscans,
"yhig", nrows, ny - oy);
15886 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15887 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15888 cpl_table_set_int(overscans,
"xhig", nrows, px);
15889 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15892 cpl_table_set_int(overscans,
"xlow", nrows, nx - ox);
15893 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15894 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15895 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15940 cpl_polynomial *mos_montecarlo_polyfit(cpl_table *points, cpl_table *evaluate,
15941 int samples,
int order)
15944 const char *func =
"mos_montecarlo_polyfit";
15958 int npoints, nevaluate;
15962 if (points == NULL || evaluate == NULL) {
15963 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15967 if (!cpl_table_has_column(points,
"x")) {
15968 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
15972 if (cpl_table_get_column_type(points,
"x") != CPL_TYPE_DOUBLE) {
15973 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
15977 if (cpl_table_has_invalid(points,
"x")) {
15978 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15982 if (!cpl_table_has_column(points,
"y")) {
15983 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
15987 if (cpl_table_get_column_type(points,
"y") != CPL_TYPE_DOUBLE) {
15988 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
15992 if (cpl_table_has_invalid(points,
"y")) {
15993 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15997 if (cpl_table_has_column(points,
"y_err")) {
15999 if (cpl_table_get_column_type(points,
"y_err") != CPL_TYPE_DOUBLE) {
16000 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
16004 if (cpl_table_has_invalid(points,
"y_err")) {
16005 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16010 if (!cpl_table_has_column(evaluate,
"x")) {
16011 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
16015 if (cpl_table_get_column_type(evaluate,
"x") != CPL_TYPE_DOUBLE) {
16016 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
16020 if (cpl_table_has_invalid(evaluate,
"x")) {
16021 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16025 if (samples < 2 || order < 0) {
16026 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16030 npoints = cpl_table_get_nrow(points);
16031 listx = cpl_vector_wrap(npoints, cpl_table_get_data_double(points,
"x"));
16032 listy = cpl_vector_wrap(npoints, cpl_table_get_data_double(points,
"y"));
16034 p = cpl_polynomial_fit_1d_create(listx, listy, order, &err);
16036 if (!cpl_table_has_column(points,
"y_err")) {
16038 cpl_table_new_column(points,
"y_err", CPL_TYPE_DOUBLE);
16039 cpl_table_fill_column_window_double(points,
"y_err", 0, npoints, err);
16040 cpl_msg_info(func,
"Error column not found - set to %f\n", err);
16047 if (cpl_table_has_column(points,
"px"))
16048 cpl_table_erase_column(points,
"px");
16049 cpl_table_new_column(points,
"px", CPL_TYPE_DOUBLE);
16050 cpl_table_fill_column_window_double(points,
"px", 0, npoints, 0);
16051 x = cpl_table_get_data_double(points,
"x");
16052 px = cpl_table_get_data_double(points,
"px");
16053 for (i = 0; i < npoints; i++)
16054 px[i] = cpl_polynomial_eval_1d(p, x[i], NULL);
16056 nevaluate = cpl_table_get_nrow(evaluate);
16058 if (cpl_table_has_column(evaluate,
"px"))
16059 cpl_table_erase_column(evaluate,
"px");
16060 cpl_table_new_column(evaluate,
"px", CPL_TYPE_DOUBLE);
16061 cpl_table_fill_column_window_double(evaluate,
"px", 0, nevaluate, 0);
16062 x_eval = cpl_table_get_data_double(evaluate,
"x");
16063 px_eval = cpl_table_get_data_double(evaluate,
"px");
16064 for (i = 0; i < nevaluate; i++)
16065 px_eval[i] = cpl_polynomial_eval_1d(p, x_eval[i], NULL);
16071 if (cpl_table_has_column(evaluate,
"sigma"))
16072 cpl_table_erase_column(evaluate,
"sigma");
16073 cpl_table_new_column(evaluate,
"sigma", CPL_TYPE_DOUBLE);
16074 cpl_table_fill_column_window_double(evaluate,
"sigma", 0, nevaluate, 0);
16075 sigma = cpl_table_get_data_double(evaluate,
"sigma");
16081 if (cpl_table_has_column(points,
"vy"))
16082 cpl_table_erase_column(points,
"vy");
16083 cpl_table_new_column(points,
"vy", CPL_TYPE_DOUBLE);
16084 cpl_table_fill_column_window_double(points,
"vy", 0, npoints, 0);
16085 vy = cpl_table_get_data_double(points,
"vy");
16086 dy = cpl_table_get_data_double(points,
"y_err");
16087 cpl_vector_unwrap(listy);
16088 listy = cpl_vector_wrap(npoints, vy);
16090 for (i = 0; i < samples; i++) {
16091 for (j = 0; j < npoints; j++)
16092 vy[j] = px[j] + dy[j] * mos_randg(1);
16093 q = cpl_polynomial_fit_1d_create(listx, listy, order, NULL);
16094 for (j = 0; j < nevaluate; j++)
16095 sigma[j] += fabs(px_eval[j]
16096 - cpl_polynomial_eval_1d(q, x_eval[j], NULL));
16097 cpl_polynomial_delete(q);
16104 cpl_table_multiply_scalar(evaluate,
"sigma", 1.25);
16105 cpl_table_divide_scalar(evaluate,
"sigma", samples);
16107 cpl_vector_unwrap(listx);
16108 cpl_vector_unwrap(listy);
16138 double gain,
double bias)
16145 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
16147 if (ron < 0.0 || gain <= FLT_EPSILON)
16148 return cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
16150 data = cpl_image_get_data_float(image);
16151 npix = cpl_image_get_size_x(image) * cpl_image_get_size_y(image);
16154 for (i = 0; i < npix; i++) {
16155 if (data[i] < bias) {
16156 data[i] += sqrt(ron) * mos_randg(1);
16159 data[i] += sqrt(ron + (data[i] - bias) / gain) * mos_randg(1);
16163 return CPL_ERROR_NONE;
16182 cpl_image *master_flat,
16185 int nx = cpl_mask_get_size_x(refmask);
16186 int ny = cpl_mask_get_size_y(refmask);
16188 int * xpos = cpl_calloc(
sizeof(
int), ny);
16190 cpl_image * filtered = cpl_image_duplicate(master_flat);
16191 cpl_mask * kernel = cpl_mask_new(9, 3);
16192 cpl_vector * v = cpl_vector_new(ny);
16193 cpl_vector * truev;
16195 double * flats = cpl_vector_get_data(v);
16197 double median, stdev, delta;
16201 cpl_mask_not(kernel);
16202 cpl_image_filter_mask(filtered, master_flat, kernel,
16203 CPL_FILTER_MEDIAN, CPL_BORDER_COPY);
16204 cpl_mask_delete(kernel);
16206 for (i = 1; i <= ny; i++) {
16210 while (!cpl_mask_get(refmask, j, i) && j < nx);
16216 flats[nvalid] = cpl_image_get(filtered, j, i, &rejected);
16225 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
16227 truev = cpl_vector_wrap(nvalid, flats);
16229 median = cpl_vector_get_median(truev);
16232 stdev = cpl_vector_get_stdev(truev);
16234 cpl_vector_unwrap(truev);
16235 cpl_vector_delete(v);
16237 for (i = 1; i <= ny; i++) {
16238 if (xpos[i - 1] > 0) {
16240 double kappa = 1.5;
16242 delta = cpl_image_get(filtered, xpos[i - 1], i, &rejected) - median;
16245 kill = fabs(delta) > stdev * kappa;
16247 kill = delta < level;
16252 while (cpl_mask_get(refmask, xpos[i - 1] + j, i)) {
16253 cpl_mask_set(refmask, xpos[i - 1] + j, i, CPL_BINARY_0);
16260 cpl_image_delete(filtered);
16263 return cpl_error_get_code();
16275 int nx = cpl_image_get_size_x(image);
16276 int ny = cpl_image_get_size_y(image);
16277 int npix = nx * ny;
16278 float * sdata = cpl_image_get_data_float(image);
16280 int count, i, j, k;
16304 for (i = 0; i < npix; i++) {
16305 if (sdata[i] >= 65535.0) {
16307 for (j = i; j < npix; j++) {
16308 if (sdata[j] < 65535.0) {
16315 if (count < 30 && count > 2) {
16316 for (j = i; j < i + count/2; j++)
16317 sdata[j] = sdata[i] + 1000.0 * (j - i);
16318 if (count % 2 != 0) {
16319 sdata[j] = sdata[j-1] + 1000.0;
16322 for (k = j; k <= i + count; k++)
16323 sdata[k] = sdata[i] - 1000.0 * (k - i - count);
16329 return cpl_error_get_code();
16348 cpl_image_subtract(image, bimage);
16349 cpl_image_delete(bimage);
16351 return cpl_error_get_code();
16372 int nscience,
float tolerance)
16376 cpl_table *summary;
16377 int summary_nobjs = 0;
16382 int nslits = cpl_table_get_nrow(slitss[0]);
16386 int nstokes, sstokes;
16390 work = (cpl_table **)cpl_malloc(
sizeof(cpl_table *) * nscience);
16401 for (j = 0; j < nscience; j++) {
16404 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
16405 summary_nobjs += c_nobjs;
16408 summary = cpl_table_new(summary_nobjs);
16410 cpl_table_new_column(summary,
"offset", CPL_TYPE_DOUBLE);
16411 cpl_table_new_column(summary,
"pair", CPL_TYPE_INT);
16412 cpl_table_new_column(summary,
"absolute", CPL_TYPE_DOUBLE);
16413 cpl_table_new_column(summary,
"pos", CPL_TYPE_DOUBLE);
16422 for (j = 0; j < nscience; j++) {
16426 for (k = 0; k < nslits; k++) {
16429 for (m = 0; m < c_maxobjs; m++) {
16431 char *name = cpl_sprintf(
"object_%d", m + 1);
16432 double obj = cpl_table_get_double(slitss[j], name, k, &null);
16448 pos = cpl_table_get_int(slitss[j],
"position", k, &null);
16449 pair = cpl_table_get_int(slitss[j],
"pair_id", k, &null);
16450 cpl_table_set(summary,
"absolute", nobjs, obj);
16451 cpl_table_set(summary,
"pos", nobjs, pos);
16452 cpl_table_set(summary,
"offset", nobjs, obj - pos);
16453 cpl_table_set(summary,
"pair", nobjs, pair);
16483 for (k = 0; k < nslits; k+=2) {
16484 int slitmatches = 0;
16486 if (k + 1 < nslits ) {
16487 if (cpl_table_get_int(slitss[0],
"pair_id", k, NULL) !=
16488 cpl_table_get_int(slitss[0],
"pair_id", k + 1, NULL)) {
16501 for (m = 0; m < maxobjs; m++) {
16503 char *name = cpl_sprintf(
"object_%d", m + 1);
16504 double obj = cpl_table_get_double(slitss[0], name, k, &null);
16508 char *name_obj = NULL;
16509 char *name_start = NULL;
16510 char *name_end = NULL;
16511 char *name_row = NULL;
16512 char *name_row_s = NULL;
16514 char *name_start_o = NULL;
16515 char *name_end_o = NULL;
16516 char *name_row_o = NULL;
16517 char *name_start_v = NULL;
16518 char *name_end_v = NULL;
16519 char *name_obj_v = NULL;
16525 int v, start_v, end_v;
16526 double min_v, obj_v;
16541 pos = cpl_table_get_int(slitss[0],
"position", k, &null);
16542 pair = cpl_table_get_int(slitss[0],
"pair_id", k, &null);
16551 cpl_table_select_all(summary);
16553 cpl_table_and_selected_int(summary,
"pair", CPL_EQUAL_TO, pair);
16554 cpl_table_and_selected_double(summary,
"offset", CPL_LESS_THAN,
16555 obj - pos + tolerance);
16557 cpl_table_and_selected_double(summary,
"offset", CPL_GREATER_THAN,
16558 obj - pos - tolerance);
16568 if (selected != nscience * 2)
16589 name_obj = cpl_sprintf(
"object_%d", slitmatches);
16590 name_start = cpl_sprintf(
"start_%d", slitmatches);
16591 name_end = cpl_sprintf(
"end_%d", slitmatches);
16592 name_row = cpl_sprintf(
"row_%d", slitmatches);
16593 name_row_s = cpl_sprintf(
"row_stokes_%d", slitmatches);
16600 name_start_o = cpl_sprintf(
"start_%d", m + 1);
16601 name_end_o = cpl_sprintf(
"end_%d", m + 1);
16602 name_row_o = cpl_sprintf(
"row_%d", m + 1);
16608 if (!cpl_table_has_column(origslits, name_obj)) {
16609 cpl_table_new_column(origslits, name_obj, CPL_TYPE_DOUBLE);
16610 cpl_table_new_column(origslits, name_start, CPL_TYPE_INT);
16611 cpl_table_new_column(origslits, name_end, CPL_TYPE_INT);
16612 cpl_table_new_column(origslits, name_row, CPL_TYPE_INT);
16613 cpl_table_new_column(origslits, name_row_s, CPL_TYPE_INT);
16623 length = cpl_table_get_int(origslits,
"length", k + 1, &null);
16631 for (v = 0; v < maxobjs; v++) {
16632 char *name_v = cpl_sprintf(
"object_%d", v + 1);
16633 double obj_v = cpl_table_get_double(slitss[0], name_v,
16642 if (fabs(obj - length - obj_v) < min_v) {
16643 min_v = fabs(obj - length - obj_v);
16644 cpl_free(name_start_v);
16645 cpl_free(name_end_v);
16646 cpl_free(name_obj_v);
16647 name_start_v = cpl_sprintf(
"start_%d", v + 1);
16648 name_end_v = cpl_sprintf(
"end_%d", v + 1);
16649 name_obj_v = cpl_sprintf(
"object_%d", v + 1);
16653 min_v = fabs(obj - length - obj_v);
16654 name_start_v = cpl_sprintf(
"start_%d", v + 1);
16655 name_end_v = cpl_sprintf(
"end_%d", v + 1);
16656 name_obj_v = cpl_sprintf(
"object_%d", v + 1);
16665 start = cpl_table_get_int(slitss[0], name_start_o, k, &null);
16666 end = cpl_table_get_int(slitss[0], name_end_o, k, &null);
16672 start_v = cpl_table_get_int(slitss[0], name_start_v, k + 1, &null);
16673 end_v = cpl_table_get_int(slitss[0], name_end_v, k + 1, &null);
16674 obj_v = cpl_table_get_double(slitss[0], name_obj_v, k + 1, &null);
16685 cpl_table_set_double(origslits, name_obj, k, obj);
16686 cpl_table_set_double(origslits, name_obj, k + 1, obj_v);
16689 cpl_table_set_int(origslits, name_start, k, start);
16690 cpl_table_set_int(origslits, name_start, k + 1, start_v);
16693 cpl_table_set_int(origslits, name_end, k, end);
16694 cpl_table_set_int(origslits, name_end, k + 1, end_v);
16709 cpl_table_set_int(origslits, name_row, k, nmatches);
16711 cpl_table_set_int(origslits, name_row, k + 1, nmatches);
16714 cpl_free(name_obj);
16715 cpl_free(name_start);
16716 cpl_free(name_end);
16717 cpl_free(name_row);
16718 cpl_free(name_row_s);
16720 cpl_free(name_start_o);
16721 cpl_free(name_end_o);
16722 cpl_free(name_row_o);
16724 cpl_free(name_start_v); name_start_v = NULL;
16725 cpl_free(name_end_v); name_end_v = NULL;
16726 cpl_free(name_obj_v); name_obj_v = NULL;
16735 cpl_table_delete(summary);
16738 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
16751 nstokes = nmatches / 2;
16753 for (k = 0; k < nslits; k++) {
16761 for (m = 0; m < maxobjs; m++) {
16762 char *name = cpl_sprintf(
"row_%d", m + 1);
16763 char *namestokes = cpl_sprintf(
"row_stokes_%d", m + 1);
16765 if (!cpl_table_is_valid(origslits, name, k)) {
16767 cpl_free(namestokes);
16773 cpl_table_set_int(origslits, name, k, nmatches);
16774 cpl_table_set_int(origslits, namestokes, k, nstokes);
16778 cpl_free(namestokes);
16790 for (j = 0; j < maxobjs; j++) {
16791 char *name = cpl_sprintf(
"object_%d", j + 1);
16792 cpl_table_fill_invalid_double(origslits, name, -1);
16795 name = cpl_sprintf(
"start_%d", j + 1);
16796 cpl_table_fill_invalid_int(origslits, name, -1);
16799 name = cpl_sprintf(
"end_%d", j + 1);
16800 cpl_table_fill_invalid_int(origslits, name, -1);
16803 name = cpl_sprintf(
"row_%d", j + 1);
16804 cpl_table_fill_invalid_int(origslits, name, -1);
16807 name = cpl_sprintf(
"row_stokes_%d", j + 1);
16808 cpl_table_fill_invalid_int(origslits, name, -1);
16823 for (i = 0; i < nscience; i++) {
16826 work[i] = cpl_table_duplicate(slitss[i]);
16828 for (m = 0; m < c_maxobjs; m++) {
16829 char *object_o = cpl_sprintf(
"object_%d", m + 1);
16830 char *start_o = cpl_sprintf(
"start_%d", m + 1);
16831 char *end_o = cpl_sprintf(
"end_%d", m + 1);
16832 char *row_o = cpl_sprintf(
"row_%d", m + 1);
16834 cpl_table_erase_column(slitss[i], object_o);
16835 cpl_table_erase_column(slitss[i], start_o);
16836 cpl_table_erase_column(slitss[i], end_o);
16837 cpl_table_erase_column(slitss[i], row_o);
16845 for (k = 0; k < nslits; k++) {
16846 for (j = 0; j < maxobjs; j++) {
16847 double object_w, object_r;
16850 char *object_i = cpl_sprintf(
"object_%d", j + 1);
16851 char *start_i = cpl_sprintf(
"start_%d", j + 1);
16852 char *end_i = cpl_sprintf(
"end_%d", j + 1);
16853 char *row_i = cpl_sprintf(
"row_%d", j + 1);
16856 if (!cpl_table_is_valid(origslits, object_i, k))
16869 object_w = cpl_table_get_double(origslits, object_i, k, NULL);
16870 row_w = cpl_table_get_int (origslits, row_i, k, NULL);
16872 for (i = 0; i < nscience; i++) {
16875 double mindiff, diff;
16881 for (m = 0; m < c_maxobjs; m++) {
16882 object_o = cpl_sprintf(
"object_%d", m + 1);
16883 start_o = cpl_sprintf(
"start_%d", m + 1);
16884 end_o = cpl_sprintf(
"end_%d", m + 1);
16885 row_o = cpl_sprintf(
"row_%d", m + 1);
16887 if (!cpl_table_is_valid(work[i], object_o, k))
16890 object_r = cpl_table_get_double(work[i], object_o, k, NULL);
16893 diff = fabs(object_w - object_r);
16895 if (mindiff > diff) {
16905 cpl_free(object_o);
16911 object_o = cpl_sprintf(
"object_%d", minpos + 1);
16912 start_o = cpl_sprintf(
"start_%d", minpos + 1);
16913 end_o = cpl_sprintf(
"end_%d", minpos + 1);
16914 row_o = cpl_sprintf(
"row_%d", minpos + 1);
16916 if (!cpl_table_has_column(slitss[i], object_i)) {
16917 cpl_table_new_column(slitss[i], object_i, CPL_TYPE_DOUBLE);
16918 cpl_table_new_column(slitss[i], start_i, CPL_TYPE_INT);
16919 cpl_table_new_column(slitss[i], end_i, CPL_TYPE_INT);
16920 cpl_table_new_column(slitss[i], row_i, CPL_TYPE_INT);
16921 cpl_table_fill_invalid_double(slitss[i], object_i, -1);
16922 cpl_table_fill_invalid_int (slitss[i], start_i, -1);
16923 cpl_table_fill_invalid_int (slitss[i], end_i, -1);
16924 cpl_table_fill_invalid_int (slitss[i], row_i, -1);
16927 cpl_table_set_double(slitss[i], object_i, k,
16928 cpl_table_get_double(work[i], object_o,
16930 cpl_table_set_int(slitss[i], start_i , k,
16931 cpl_table_get_int(work[i], start_o, k, NULL));
16932 cpl_table_set_int(slitss[i], end_i , k,
16933 cpl_table_get_int(work[i], end_o, k, NULL));
16934 cpl_table_set_int(slitss[i], row_i , k, row_w);
16936 cpl_free(object_o);
16942 cpl_free(object_i);
16949 for (i = 0; i < nscience; i++)
16950 cpl_table_delete(work[i]);
16955 return cpl_error_get_code();
16970 char * colname = cpl_sprintf(
"object_%d", maxobjs);
16972 while (cpl_table_has_column(slits, colname)) {
16975 colname = cpl_sprintf(
"object_%d", maxobjs);
16996 int nslits = cpl_table_get_nrow(slits);
17001 for (k = 0; k < nslits; k++) {
17002 for (m = 0; m < maxobjs; m++) {
17003 char * name = cpl_sprintf(
"object_%d", m + 1);
17004 int null = !cpl_table_is_valid(slits, name, k);
17026 cpl_propertylist *sort;
17028 int nslits = cpl_table_get_nrow(slits);
17032 const float interval = 90.0 * rescale;
17033 const float offset = (90.0 - 5) * rescale;
17036 for (k = 0; k < nslits; k++) {
17037 double ytop = cpl_table_get_double(slits,
"ytop", k, &null);
17038 double ybottom = cpl_table_get_double(slits,
"ybottom", k, &null);
17040 double xtop = cpl_table_get_double(slits,
"xtop", k, &null);
17041 double xbottom = cpl_table_get_double(slits,
"xbottom", k, &null);
17043 int nmiss = (int)((ytop - ybottom) / interval + 0.5);
17046 cpl_msg_warning(cpl_func,
17047 "Some slits could not be properly detected. "
17048 "There might be accountable inaccuracies.");
17049 while (nmiss > 1) {
17050 cpl_table_set_size(slits, nslits + 1);
17055 cpl_table_set_double(slits,
"xtop", nslits, xtop);
17056 cpl_table_set_double(slits,
"xbottom", nslits, xbottom);
17060 cpl_table_set_double(slits,
"ybottom", nslits, ybottom);
17061 cpl_table_set_double(slits,
"ytop", nslits, ybottom
17063 ybottom += interval;
17064 cpl_table_set_double(slits,
"ybottom", k, ybottom);
17066 cpl_table_set_double(slits,
"ytop", nslits, ytop);
17067 cpl_table_set_double(slits,
"ybottom", nslits, ytop
17070 cpl_table_set_double(slits,
"ytop", k, ytop);
17078 sort = cpl_propertylist_new();
17079 cpl_propertylist_append_bool(sort,
"ytop", 1);
17080 cpl_table_sort(slits, sort);
17081 cpl_propertylist_delete(sort);
17088 k = cpl_table_get_nrow(slits) - 1;
17091 double ytop = cpl_table_get_double(slits,
"ytop", k, &null);
17092 double ybottom = cpl_table_get_double(slits,
"ybottom", k, &null);
17093 double length = (ytop - ybottom) / interval;
17095 if (length > 1.1) {
17096 cpl_table_set_double(slits,
"ybottom", k, ytop - offset);
17127 int * nslits_out_det)
17132 cpl_propertylist * sort;
17136 halfsize = cpl_table_get_nrow(slits);
17138 cpl_table_set_size(slits, 2 * halfsize);
17140 for (m = 0; m < halfsize; m++) {
17145 cpl_table_get(slits,
"ytop", m, &null) -
17146 cpl_table_get(slits,
"ybottom", m, &null);
17150 cpl_table_get(slits,
"ybottom", m - 1, &null) -
17151 cpl_table_get(slits,
"ytop", m, &null);
17153 gap = (interval - length) / 2;
17156 cpl_table_set(slits,
"slit_id", m + halfsize,
17157 cpl_table_get(slits,
"slit_id", m, &null) - 1);
17159 cpl_table_set(slits,
"xtop", m + halfsize,
17160 cpl_table_get(slits,
"xtop", m, &null));
17162 cpl_table_set(slits,
"xbottom", m + halfsize,
17163 cpl_table_get(slits,
"xbottom", m, &null));
17165 cpl_table_set(slits,
"ytop", m + halfsize,
17166 cpl_table_get(slits,
"ytop", m, &null) + gap + length);
17168 cpl_table_set(slits,
"ybottom", m + halfsize,
17169 cpl_table_get(slits,
"ytop", m, &null) + gap);
17172 for (m = 0; m < 2 * halfsize; m++) {
17173 cpl_table_set(slits,
"ytop", m,
17174 cpl_table_get(slits,
"ytop", m, &null) - 5.3);
17176 cpl_table_set(slits,
"ybottom", m,
17177 cpl_table_get(slits,
"ybottom", m, &null) - 5.3);
17181 sort = cpl_propertylist_new();
17182 cpl_propertylist_append_bool(sort,
"ytop", 1);
17183 cpl_table_sort(slits, sort);
17185 cpl_propertylist_delete(sort);
17190 int * fors_get_nobjs_perslit(cpl_table * slits)
17192 int nslits = cpl_table_get_nrow(slits);
17195 int * nobjs_per_slit = cpl_malloc(
sizeof(
int) * nslits);
17199 for (k = 0; k < nslits; k++) {
17201 for (m = 0; m < maxobjs; m++) {
17202 char * name = cpl_sprintf(
"object_%d", m + 1);
17203 int null = !cpl_table_is_valid(slits, name, k);
17211 nobjs_per_slit[k] = nobjs;
17214 return nobjs_per_slit;
17217 double fors_get_object_position(cpl_table *slits,
int slit,
int object)
17219 char *name = cpl_sprintf(
"object_%d",
object);
17222 position = cpl_table_get_double(slits, name, slit, NULL)
17223 - cpl_table_get_int(slits,
"position", slit, NULL);
17230 int mos_rebin_signal(cpl_image **image,
int rebin)
17232 cpl_image *rebinned;
17235 if (*image == NULL)
17241 rebinned = cpl_image_rebin(*image, 1, 1, rebin, 1);
17243 cpl_image_delete(*image);
17250 int mos_rebin_error(cpl_image **image,
int rebin)
17252 if (*image == NULL)
17258 cpl_image_power(*image, 2);
17259 mos_rebin_signal(image, rebin);
17260 cpl_image_power(*image, 0.5);
17282 int map_table(cpl_image *image,
double start,
double step,
17283 cpl_table *table,
const char *xname,
const char *yname)
17285 int length = cpl_image_get_size_x(image);
17286 int nrows = cpl_table_get_nrow(table);
17287 float *data = cpl_image_get_data_float(image);
17288 float *fdata = NULL;
17289 double *xdata = NULL;
17290 double *ydata = NULL;
17291 cpl_type xtype = cpl_table_get_column_type(table, xname);
17292 cpl_type ytype = cpl_table_get_column_type(table, yname);
17302 for (i = 0; i < length; i++)
17310 if (xtype == CPL_TYPE_FLOAT) {
17311 fdata = cpl_table_get_data_float(table, xname);
17312 xdata = cpl_malloc(nrows *
sizeof(
double));
17313 for (i = 0; i < nrows; i++) {
17314 xdata[i] = fdata[i];
17318 xdata = cpl_table_get_data_double(table, xname);
17321 if (ytype == CPL_TYPE_FLOAT) {
17322 fdata = cpl_table_get_data_float(table, yname);
17323 ydata = cpl_malloc(nrows *
sizeof(
double));
17324 for (i = 0; i < nrows; i++) {
17325 ydata[i] = fdata[i];
17329 ydata = cpl_table_get_data_double(table, yname);
17339 for (i = 0; i < length; i++) {
17340 pos = start + step * i;
17343 for (j = n; j < nrows; j++) {
17344 if (xdata[j] > pos) {
17346 data[i] = ydata[j-1]
17347 + (ydata[j] - ydata[j-1])
17348 * (pos - xdata[j-1]) / (xdata[j] - xdata[j-1]);
17354 if (xtype == CPL_TYPE_FLOAT)
17357 if (ytype == CPL_TYPE_FLOAT)
17377 static cpl_image *polysmooth(cpl_image *image,
int order,
int hw)
17384 cpl_polynomial *poly;
17385 cpl_vector *ysmooth;
17386 cpl_image *smoothed;
17391 npoints = cpl_image_get_size_x(image);
17393 if (2 * hw + 1 > npoints)
17396 x = cpl_vector_new(npoints);
17397 y = cpl_vector_new(npoints);
17398 xdata = cpl_vector_get_data(x);
17399 ydata = cpl_vector_get_data(y);
17401 smoothed = cpl_image_duplicate(image);
17402 sdata = cpl_image_get_data_float(smoothed);
17404 for (i = 0; i < npoints; i++) {
17406 ydata[i] = sdata[i];
17409 ysmooth = cpl_vector_filter_median_create(y, hw);
17410 cpl_vector_delete(y);
17412 poly = cpl_polynomial_fit_1d_create(x, ysmooth, order, NULL);
17413 cpl_vector_delete(x);
17414 cpl_vector_delete(ysmooth);
17417 for (i = 0; i < npoints; i++)
17418 sdata[i] = cpl_polynomial_eval_1d(poly, i, NULL);
17420 cpl_polynomial_delete(poly);
17423 cpl_image_delete(smoothed);
17433 cpl_image_delete(spectrum); \
17434 cpl_image_delete(flux); \
17435 cpl_image_delete(efficiency); \
17436 cpl_image_delete(smo_efficiency); \
17437 cpl_image_delete(extinction); \
17438 cpl_image_delete(response); \
17439 cpl_image_delete(smo_response); \
17440 cpl_image_delete(physical); \
17467 double dispersion,
double gain,
17468 double exptime, cpl_table *ext_table,
17469 double airmass, cpl_table *flux_table,
17473 cpl_image *spectrum = NULL;
17475 cpl_image *extinction = NULL;
17477 cpl_image *flux = NULL;
17479 cpl_image *physical = NULL;
17481 cpl_image *efficiency = NULL;
17483 cpl_image *smo_efficiency = NULL;
17484 float *smo_eff_data;
17485 cpl_image *response = NULL;
17487 cpl_image *smo_response = NULL;
17488 float *smo_res_data;
17490 cpl_image *smo_image;
17494 int ext_count, ext_pos;
17495 int eff_count, eff_pos;
17496 int flux_count, flux_pos;
17501 if (spectra == NULL || ext_table == NULL || flux_table == NULL) {
17502 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
17506 if (!cpl_table_has_column(ext_table,
"WAVE")) {
17507 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17508 "Column WAVE in atmospheric extinction table");
17512 if (!cpl_table_has_column(ext_table,
"EXTINCTION")) {
17513 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17514 "Column EXTINCTION in atmospheric extinction table");
17518 if (!cpl_table_has_column(flux_table,
"WAVE")) {
17519 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17520 "Column WAVE in standard star flux table");
17524 if (!cpl_table_has_column(flux_table,
"FLUX")) {
17525 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17526 "Column FLUX in standard star flux table");
17531 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17532 "Invalid gain factor (%.2f)", gain);
17536 if (exptime < 0.001) {
17537 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17538 "Invalid exposure time (%.2f)", exptime);
17542 if (dispersion < 0.001) {
17543 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17544 "Invalid dispersion (%.2f)", dispersion);
17549 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17550 "Order of the polynomial fitting the "
17551 "instrument response must be at least 2");
17555 nx = cpl_image_get_size_x(spectra);
17556 ny = cpl_image_get_size_y(spectra);
17564 spectrum = cpl_image_duplicate(spectra);
17568 cpl_image *brights = cpl_image_collapse_create(spectra, 1);
17570 cpl_image_get_maxpos(brights, &x, &y);
17571 cpl_image_delete(brights);
17572 spectrum = cpl_image_extract(spectra, 1, y, nx, y);
17580 cpl_image_multiply_scalar(spectrum, gain / exptime / dispersion);
17588 extinction = cpl_image_duplicate(spectrum);
17589 map_table(extinction, startwave + dispersion/2, dispersion,
17590 ext_table,
"WAVE",
"EXTINCTION");
17597 cpl_image_multiply_scalar(extinction, 0.4 * airmass);
17598 cpl_image_exponential(extinction, 10.);
17605 cpl_image_multiply(spectrum, extinction);
17613 ext_data = cpl_image_get_data_float(extinction);
17617 for (i = 0; i < nx; i++) {
17618 if (ext_data[i] > 0.0) {
17619 if (ext_count == 0) {
17631 cpl_image_delete(extinction); extinction = NULL;
17639 flux = cpl_image_duplicate(spectrum);
17640 map_table(flux, startwave + dispersion/2, dispersion,
17641 flux_table,
"WAVE",
"FLUX");
17649 flux_data = cpl_image_get_data_float(flux);
17653 for (i = 0; i < nx; i++) {
17654 if (flux_data[i] > 0.0) {
17655 if (flux_count == 0) {
17672 start = ext_pos > flux_pos ? ext_pos : flux_pos;
17673 end = (ext_pos + ext_count) < (flux_pos + flux_count) ?
17674 (ext_pos + ext_count) : (flux_pos + flux_count);
17676 flux_count = end - start;
17687 physical = cpl_image_duplicate(spectrum);
17688 phys_data = cpl_image_get_data_float(physical);
17690 for (i = 0; i < nx; i++) {
17691 lambda = startwave + dispersion * (i + 0.5);
17692 phys_data[i] = 0.0026 * lambda * flux_data[i];
17695 efficiency = cpl_image_duplicate(spectrum);
17696 eff_data = cpl_image_get_data_float(efficiency);
17697 data = cpl_image_get_data_float(spectrum);
17699 for (i = 0; i < nx; i++) {
17700 if (phys_data[i] > 0.0)
17701 eff_data[i] = data[i] / phys_data[i];
17706 cpl_image_delete(physical); physical = NULL;
17716 for (i = 0; i < nx; i++) {
17717 if (eff_data[i] > 0.01) {
17718 if (eff_count == 0) {
17724 if (eff_count > 300) {
17735 start = eff_pos > flux_pos ? eff_pos : flux_pos;
17736 end = (eff_pos + eff_count) < (flux_pos + flux_count) ?
17737 (eff_pos + eff_count) : (flux_pos + flux_count);
17739 eff_count = end - start;
17741 if (eff_count < 1) {
17742 cpl_error_set_message(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
17743 "No overlap between catalog and spectrum");
17753 image = cpl_image_extract(efficiency, eff_pos + 1, 1,
17754 eff_pos + eff_count, 1);
17756 smo_image = polysmooth(image, order, 50);
17757 cpl_image_delete(image);
17759 smo_efficiency = cpl_image_duplicate(efficiency);
17760 smo_eff_data = cpl_image_get_data_float(smo_efficiency);
17761 cpl_image_copy(smo_efficiency, smo_image, eff_pos + 1, 1);
17763 cpl_image_delete(smo_image);
17774 response = cpl_image_duplicate(spectrum);
17775 res_data = cpl_image_get_data_float(response);
17777 for (i = 0; i < nx; i++) {
17778 if (eff_data[i] > 0.01 && flux_data[i] > 0.0)
17779 res_data[i] = data[i] / flux_data[i];
17789 image = cpl_image_extract(response, eff_pos + 1, 1, eff_pos + eff_count, 1);
17791 smo_image = polysmooth(image, order, 50);
17792 cpl_image_delete(image);
17794 smo_response = cpl_image_duplicate(response);
17795 smo_res_data = cpl_image_get_data_float(smo_response);
17796 cpl_image_copy(smo_response, smo_image, eff_pos + 1, 1);
17798 cpl_image_delete(smo_image);
17800 for (i = 0; i < nx; i++) {
17801 if (eff_data[i] > 0.01) {
17802 res_data[i] = 1 / res_data[i];
17803 smo_res_data[i] = 1 / smo_res_data[i];
17807 smo_res_data[i] = 0.0;
17816 table = cpl_table_new(nx);
17818 cpl_table_new_column(table,
"WAVE", CPL_TYPE_FLOAT);
17819 cpl_table_set_column_unit(table,
"WAVE",
"Angstrom");
17821 for (i = 0; i < nx; i++)
17822 cpl_table_set_float(table,
"WAVE", i, startwave + dispersion*(i+0.5));
17824 cpl_table_new_column(table,
"STD_FLUX", CPL_TYPE_FLOAT);
17825 cpl_table_set_column_unit(table,
"STD_FLUX",
17826 "10^(-16) erg/(cm^2 s Angstrom)");
17827 cpl_table_copy_data_float(table,
"STD_FLUX", flux_data);
17828 cpl_image_delete(flux); flux = NULL;
17830 cpl_table_new_column(table,
"OBS_FLUX", CPL_TYPE_FLOAT);
17831 cpl_table_set_column_unit(table,
"OBS_FLUX",
"electron/(s Angstrom)");
17832 cpl_table_copy_data_float(table,
"OBS_FLUX", data);
17833 cpl_image_delete(spectrum); spectrum = NULL;
17835 cpl_table_new_column(table,
"RAW_EFFICIENCY", CPL_TYPE_FLOAT);
17836 cpl_table_set_column_unit(table,
"RAW_EFFICIENCY",
"electron/photon");
17837 cpl_table_copy_data_float(table,
"RAW_EFFICIENCY", eff_data);
17838 cpl_image_delete(efficiency); efficiency = NULL;
17840 cpl_table_new_column(table,
"EFFICIENCY", CPL_TYPE_FLOAT);
17841 cpl_table_set_column_unit(table,
"EFFICIENCY",
"electron/photon");
17842 cpl_table_copy_data_float(table,
"EFFICIENCY", smo_eff_data);
17843 cpl_image_delete(smo_efficiency); smo_efficiency = NULL;
17845 cpl_table_new_column(table,
"RAW_RESPONSE", CPL_TYPE_FLOAT);
17846 cpl_table_set_column_unit(table,
"RAW_RESPONSE",
17847 "10^(-16) erg/(cm^2 electron)");
17848 cpl_table_copy_data_float(table,
"RAW_RESPONSE", res_data);
17849 cpl_image_delete(response); response = NULL;
17851 cpl_table_new_column(table,
"RESPONSE", CPL_TYPE_FLOAT);
17852 cpl_table_set_column_unit(table,
17853 "RESPONSE",
"10^(-16) erg/(cm^2 electron)");
17854 cpl_table_copy_data_float(table,
"RESPONSE", smo_res_data);
17855 cpl_image_delete(smo_response); smo_response = NULL;
17862 static double ksigma_vector(cpl_vector *values,
17863 double klow,
double khigh,
int kiter,
int *good)
17865 cpl_vector *accepted;
17867 double sigma = 0.0;
17868 double *data = cpl_vector_get_data(values);
17869 int n = cpl_vector_get_size(values);
17880 mean = cpl_vector_get_median(values);
17882 for (i = 0; i < n; i++)
17883 sigma += (mean - data[i]) * (mean - data[i]);
17885 sigma = sqrt(sigma / (n - 1));
17889 for (i = 0; i < ngood; i++) {
17890 if (data[i]-mean < khigh*sigma && mean-data[i] < klow*sigma) {
17891 data[count] = data[i];
17905 accepted = cpl_vector_wrap(count, data);
17906 mean = cpl_vector_get_mean(accepted);
17908 sigma = cpl_vector_get_stdev(accepted);
17909 cpl_vector_unwrap(accepted);
17911 if (count == ngood || count == 1)
17944 double klow,
double khigh,
int kiter,
17947 int ni, nx, ny, npix;
17948 cpl_image *out_ima;
17953 cpl_vector *time_line;
17954 double *ptime_line;
17959 ni = cpl_imagelist_get_size(imlist);
17961 image = cpl_imagelist_get(imlist, 0);
17962 nx = cpl_image_get_size_x(image);
17963 ny = cpl_image_get_size_y(image);
17966 out_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
17967 pout_ima = cpl_image_get_data_float(out_ima);
17970 *good = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
17971 good_ima = cpl_image_get_data_float(*good);
17974 time_line = cpl_vector_new(ni);
17975 ptime_line = cpl_vector_get_data(time_line);
17977 data = cpl_calloc(
sizeof(
float *), ni);
17979 for (i = 0; i < ni; i++) {
17980 image = cpl_imagelist_get(imlist, i);
17981 data[i] = cpl_image_get_data_float(image);
17984 for (i = 0; i < npix; i++) {
17985 for (j = 0; j < ni; j++) {
17986 ptime_line[j] = data[j][i];
17988 pout_ima[i] = ksigma_vector(time_line, klow, khigh, kiter, &ngood);
17990 good_ima[i] = ngood;
17995 cpl_vector_delete(time_line);
18019 cpl_table *ext_table,
double startwave,
18020 double dispersion,
double gain,
18021 double exptime,
double airmass)
18023 cpl_image *extinction;
18024 cpl_image *outspectra;
18025 cpl_image *mapresponse;
18029 int tlength, xlength, ylength;
18033 if (spectra == NULL || ext_table == NULL || response == NULL) {
18034 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
18038 if(cpl_table_has_column(response,
"RESPONSE"))
18039 cpl_table_cast_column(response,
"RESPONSE",
"RESPONSE_F", CPL_TYPE_FLOAT);
18040 else if(cpl_table_has_column(response,
"RESPONSE_FFSED"))
18041 cpl_table_cast_column(response,
"RESPONSE_FFSED",
"RESPONSE_F", CPL_TYPE_FLOAT);
18045 res_data = cpl_table_get_data_float(response,
"RESPONSE_F");
18047 if (res_data == NULL) {
18048 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
18052 tlength = cpl_table_get_nrow(response);
18053 xlength = cpl_image_get_size_x(spectra);
18054 ylength = cpl_image_get_size_y(spectra);
18056 if (xlength != tlength) {
18057 mapresponse = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18058 map_table(mapresponse, startwave + dispersion/2, dispersion,
18059 response,
"WAVE",
"RESPONSE_F");
18060 res_data = cpl_image_get_data_float(mapresponse);
18068 extinction = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18069 map_table(extinction, startwave + dispersion/2, dispersion,
18070 ext_table,
"WAVE",
"EXTINCTION");
18077 cpl_image_multiply_scalar(extinction, 0.4 * airmass);
18078 cpl_image_exponential(extinction, 10.);
18080 outspectra = cpl_image_duplicate(spectra);
18082 ext_data = cpl_image_get_data_float(extinction);
18083 out_data = cpl_image_get_data_float(outspectra);
18085 for (k = 0, i = 0; i < ylength; i++) {
18086 for (j = 0; j < xlength; j++, k++) {
18087 out_data[k] *= ext_data[j] * res_data[j];
18091 cpl_image_delete(extinction);
18092 if (xlength != tlength) {
18093 cpl_image_delete(mapresponse);
18096 cpl_image_multiply_scalar(outspectra, gain / exptime / dispersion);
18098 cpl_table_erase_column(response,
"RESPONSE_F");
18122 cpl_table *response,
18123 cpl_table *ext_table,
18125 double dispersion,
double gain,
18126 double exptime,
double airmass)
18128 cpl_image *extinction;
18129 cpl_image *outerrors;
18130 cpl_image *mapresponse;
18131 cpl_image *maperror;
18137 int tlength, xlength, ylength;
18141 if (errors == NULL || ext_table == NULL || response == NULL) {
18142 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
18146 if (!cpl_table_has_column(response,
"ERROR")) {
18148 dispersion, gain, exptime, airmass);
18151 cpl_table_cast_column(response,
"RESPONSE",
"RESPONSE_F", CPL_TYPE_FLOAT);
18152 res_data = cpl_table_get_data_float(response,
"RESPONSE_F");
18154 if (res_data == NULL) {
18155 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
18159 err_data = cpl_table_get_data_float(response,
"ERROR");
18161 if (err_data == NULL) {
18162 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
18166 tlength = cpl_table_get_nrow(response);
18167 xlength = cpl_image_get_size_x(errors);
18168 ylength = cpl_image_get_size_y(errors);
18170 if (xlength != tlength) {
18171 mapresponse = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18172 map_table(mapresponse, startwave + dispersion/2, dispersion,
18173 response,
"WAVE",
"RESPONSE_F");
18174 res_data = cpl_image_get_data_float(mapresponse);
18176 maperror = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18177 map_table(maperror, startwave + dispersion/2, dispersion,
18178 response,
"WAVE",
"ERROR");
18179 err_data = cpl_image_get_data_float(maperror);
18187 extinction = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18188 map_table(extinction, startwave + dispersion/2, dispersion,
18189 ext_table,
"WAVE",
"EXTINCTION");
18196 cpl_image_multiply_scalar(extinction, 0.4 * airmass);
18197 cpl_image_exponential(extinction, 10.);
18199 outerrors = cpl_image_duplicate(errors);
18201 ext_data = cpl_image_get_data_float(extinction);
18202 out_data = cpl_image_get_data_float(outerrors);
18203 spe_data = cpl_image_get_data_float(spectra);
18205 for (k = 0, i = 0; i < ylength; i++) {
18206 for (j = 0; j < xlength; j++, k++) {
18207 out_data[k] = ext_data[j] *
18208 sqrt(err_data[j] * err_data[j] * spe_data[k] * spe_data[k] +
18209 res_data[j] * res_data[j] * out_data[k] * out_data[k]);
18213 cpl_image_delete(extinction);
18214 if (xlength != tlength) {
18215 cpl_image_delete(maperror);
18218 cpl_image_multiply_scalar(outerrors, gain / exptime / dispersion);
18220 cpl_table_erase_column(response,
"RESPONSE_F");
18301 cpl_image *u_image, cpl_image *u_error,
18302 double startwave,
double dispersion,
18303 double band, cpl_table *pol_sta,
18304 double ra,
double dec,
char *filter,
18306 double *p_offset,
double *p_error,
18307 double *a_offset,
double *a_error)
18309 cpl_table *standard;
18310 cpl_image *q_noise;
18311 cpl_image *q_signal;
18312 cpl_image *u_noise;
18313 cpl_image *u_signal;
18319 double arctol = 0.5;
18322 double bwave[] = {3650., 4450., 5510., 6580., 8060};
18323 char *bands =
"UBVRI";
18324 char p_label[] = {
' ',
'p',
'\0'};
18325 char dp_label[] = {
' ',
'd',
'p',
'\0'};
18326 char a_label[] = {
' ',
'a',
'\0'};
18327 char da_label[] = {
' ',
'd',
'a',
'\0'};
18328 int nbands = strlen(bands);
18330 int first, last, count, center;
18333 int i, found, closest;
18361 cpl_table_select_all(pol_sta);
18362 cpl_table_and_selected_double(pol_sta,
"RA", CPL_GREATER_THAN, ra-arctol);
18363 cpl_table_and_selected_double(pol_sta,
"RA", CPL_LESS_THAN, ra+arctol);
18364 cpl_table_and_selected_double(pol_sta,
"DEC", CPL_GREATER_THAN, dec-arctol);
18366 cpl_table_and_selected_double(pol_sta,
"DEC", CPL_LESS_THAN, dec+arctol);
18368 if (selected == 0) {
18369 cpl_msg_warning(cpl_func,
"No standard star found in FOV");
18373 if (selected > 1) {
18374 cpl_msg_warning(cpl_func,
18375 "Ambiguity: %d standard stars found in FOV", selected);
18379 standard = cpl_table_extract_selected(pol_sta);
18381 cpl_msg_info(cpl_func,
"Standard star: %s",
18382 cpl_table_get_string(standard,
"name", 0));
18388 polarised = cpl_table_get_int(standard,
"polarised", 0, NULL);
18390 cpl_msg_info(cpl_func,
"This star is%sexpected to be polarised",
18391 polarised ?
" " :
" not ");
18401 nx = cpl_image_get_size_x(q_error);
18403 noise = cpl_image_collapse_median_create(q_error, 1, 0, 0);
18404 cpl_image_get_minpos(noise, &col, &row);
18406 cpl_image_delete(noise);
18409 cpl_table_delete(standard);
18410 cpl_msg_error(cpl_func,
18411 "Assertion failure!!! col = %"CPL_SIZE_FORMAT
" (it should be 1)", col);
18415 q_signal = cpl_image_extract(q_image, 1, row, nx, row);
18416 q_noise = cpl_image_extract(q_error, 1, row, nx, row);
18417 u_signal = cpl_image_extract(u_image, 1, row, nx, row);
18418 u_noise = cpl_image_extract(u_error, 1, row, nx, row);
18420 q_sdata = cpl_image_get_data_double(q_signal);
18421 q_ndata = cpl_image_get_data_double(q_noise);
18422 u_sdata = cpl_image_get_data_double(u_signal);
18423 u_ndata = cpl_image_get_data_double(u_noise);
18431 last = nx = cpl_image_get_size_x(q_signal);
18432 for (i = 0; i < nx; i++) {
18434 if (q_ndata[i] > 0.0) {
18439 if (q_ndata[i] <= 0.0) {
18446 count = last - first + 1;
18448 if (first < 0 || count < band) {
18449 cpl_table_delete(standard);
18450 cpl_image_delete(q_signal);
18451 cpl_image_delete(q_noise);
18452 cpl_image_delete(u_signal);
18453 cpl_image_delete(u_noise);
18454 cpl_msg_warning(cpl_func,
"Too short spectrum (%d pixels)", count);
18458 center = (first + last) / 2;
18459 cwave = startwave + dispersion * center;
18467 for (i = 0; i < nbands; i++) {
18468 p_label[0] = bands[i];
18469 if (cpl_table_is_valid(standard, p_label, 0)) {
18472 mindist = fabs(bwave[i] - cwave);
18475 else if (mindist > fabs(bwave[i] - cwave)) {
18476 mindist = fabs(bwave[i] - cwave);
18483 cpl_table_delete(standard);
18484 cpl_image_delete(q_signal);
18485 cpl_image_delete(q_noise);
18486 cpl_image_delete(u_signal);
18487 cpl_image_delete(u_noise);
18488 cpl_msg_warning(cpl_func,
"No reference value available");
18492 center = (bwave[closest] - startwave) / dispersion;
18493 cwave = bwave[closest];
18501 pband = floor(band / dispersion);
18503 if (center - pband/2 < first || center + pband/2 > last) {
18504 cpl_table_delete(standard);
18505 cpl_image_delete(q_signal);
18506 cpl_image_delete(q_noise);
18507 cpl_image_delete(u_signal);
18508 cpl_image_delete(u_noise);
18509 cpl_msg_warning(cpl_func,
"No reference value available");
18513 first = center - pband/2;
18514 last = center + pband/2;
18521 p_label[0] = bands[closest];
18522 dp_label[0] = bands[closest];
18523 a_label[0] = bands[closest];
18524 da_label[0] = bands[closest];
18526 p_ref = cpl_table_get(standard, p_label, 0, NULL);
18527 dp_ref = cpl_table_get(standard, dp_label, 0, NULL);
18528 a_ref = cpl_table_get(standard, a_label, 0, NULL);
18529 da_ref = cpl_table_get(standard, da_label, 0, NULL);
18531 cpl_msg_info(cpl_func,
18532 "The expected polarisation is %.2f +- %.2f %%",
18536 cpl_msg_info(cpl_func,
18537 "The expected polarisation angle is %.2f +- %.2f degrees",
18545 q_obs = cpl_image_get_median_window(q_image, first, 1, last, 1);
18546 q_err = cpl_image_get_median_window(q_error, first, 1, last, 1);
18547 u_obs = cpl_image_get_median_window(u_image, first, 1, last, 1);
18548 u_err = cpl_image_get_median_window(u_error, first, 1, last, 1);
18554 p_obs = sqrt(q_obs * q_obs + u_obs * u_obs);
18555 p_err = CPL_MATH_SQRT1_2 * 0.5 * (q_err + u_err);
18563 if (fabs(q_obs) < 0.00001) {
18572 a_obs = 0.5 * atan(u_obs / q_obs) * 180 / CPL_MATH_PI;
18590 a_err = sqrt(q_obs*q_obs*u_err*u_err + u_obs*u_obs*q_err*q_err)
18592 * 90 / CPL_MATH_PI;
18597 cpl_msg_info(cpl_func,
18598 "The measured polarisation is %.2f +- %.2f %%",
18602 cpl_msg_info(cpl_func,
18603 "The measured polarisation angle is %.2f +- %.2f degrees",
18607 *filter = bands[closest];
18608 *polarisation = polarised;
18611 *p_offset = (p_obs - p_ref) / p_ref;
18612 *p_error = sqrt(p_err * p_err + dp_ref * dp_ref) / p_ref;
18615 *p_offset = p_obs - p_ref;
18616 *p_error = sqrt(p_err * p_err + dp_ref * dp_ref);
18619 *a_offset = a_obs - a_ref;
18620 *a_error = sqrt(a_err*a_err + da_ref*da_ref);
18660 cpl_array *offsets;
18662 int nslits = cpl_table_get_nrow(reference);
18669 cpl_error_code status = CPL_ERROR_NONE;
18674 if (objects == NULL)
18675 return CPL_ERROR_NULL_INPUT;
18677 if (nslits != cpl_table_get_nrow(objects))
18678 return CPL_ERROR_INCOMPATIBLE_INPUT;
18680 nref = fors_get_nobjs_perslit(reference);
18681 nobj = fors_get_nobjs_perslit(objects);
18684 for (i = 0; i < nslits; i++)
18685 noffset += nobj[i];
18687 if (noffset == 0) {
18690 return CPL_ERROR_DATA_NOT_FOUND;
18694 for (i = 0; i < nslits; i++)
18695 noffset += nref[i];
18697 if (noffset == 0) {
18700 return CPL_ERROR_DATA_NOT_FOUND;
18703 offsets = cpl_array_new(noffset, CPL_TYPE_DOUBLE);
18707 for (i = 0; i < nslits; i++) {
18708 if (nref[i] > 0 && nobj[i] > 0) {
18710 int length = cpl_table_get_int(objects,
"length", i, NULL);
18711 double ytop = cpl_table_get_double(objects,
"xtop", i, NULL);
18712 double ybottom = cpl_table_get_double(objects,
"xbottom", i, NULL);
18713 int *aref = cpl_calloc(length,
sizeof(
int));
18714 int *aobj = cpl_calloc(length,
sizeof(
int));
18715 float *pref = cpl_calloc(nref[i],
sizeof(
float));
18716 float *pobj = cpl_calloc(nobj[i],
sizeof(
float));
18718 for (j = 0; j < nref[i]; j++) {
18719 pref[j] = fors_get_object_position(reference, i, j + 1);
18720 aref[(int)pref[j]] = 1;
18723 for (j = 0; j < nobj[i]; j++) {
18724 pobj[j] = fors_get_object_position(objects, i, j + 1);
18725 aobj[(int)pobj[j]] = 1;
18733 aref[length - 1] = 0;
18735 aobj[length - 1] = 0;
18755 best_shift = length;
18757 for (shift = length/2, j = 0; j <= length; shift--, j++) {
18758 int rstart, ostart, count;
18763 count = length - shift;
18768 count = length + shift;
18772 for (k = 0; k < count; k++) {
18773 corr += aref[rstart + k] * aobj[ostart + k];
18776 if (maxcorr < corr) {
18778 best_shift = shift;
18782 if (best_shift == length) {
18792 for (j = 0; j < nref[i]; j++) {
18793 for (k = 0; k < nobj[i]; k++) {
18794 if (fabs(pref[j] - pobj[k] - best_shift) < 2) {
18795 double ccd_offset = (pref[j] - pobj[k])
18805 cpl_array_set(offsets, noffset, ccd_offset);
18827 *offset = cpl_array_get_median(offsets);
18830 double *a = cpl_malloc(
sizeof(
double) * noffset);
18831 for (i = 0; i < noffset; i++) {
18832 a[i] = cpl_array_get_double(offsets, i, NULL);
18840 status = CPL_ERROR_DATA_NOT_FOUND;
18843 cpl_array_delete(offsets);
18864 int nx = cpl_image_get_size_x(image);
18865 int ny = cpl_image_get_size_y(image);
18869 double xpos, ypos, xfrac, yfrac;
18873 if (fabs(dx) >= nx || fabs(dy) >= ny)
18874 return CPL_ERROR_ACCESS_OUT_OF_RANGE;
18876 source = cpl_image_duplicate(image);
18877 idata = cpl_image_get_data_float(image);
18878 sdata = cpl_image_get_data_float(source);
18884 yfrac = - dy - floor(- dy);
18885 xfrac = - dx - floor(- dx);
18887 for (pos = 0, j = 0; j < ny; j++) {
18889 yint = floor(ypos);
18890 for (i = 0; i < nx; i++) {
18892 xint = floor(xpos);
18893 if (xint < 0 || yint < 0 || xint > nx - 2 || yint > ny - 2) {
18897 idata[pos] = sdata[xint + nx*yint] * (1 - xfrac) * (1 - yfrac)
18898 + sdata[xint + 1 + nx*yint] * xfrac * (1 - yfrac)
18899 + sdata[xint + nx*(yint + 1)] * (1 - xfrac) * yfrac
18900 + sdata[xint + 1 + nx*(yint + 1)] * xfrac * yfrac;
18906 cpl_image_delete(source);
18908 return CPL_ERROR_NONE;
18924 #ifdef CPL_SIZE_FORMAT
18930 cpl_table_duplicate_column(slits,
"x", slits,
"xtop");
18931 cpl_table_add_columns(slits,
"x",
"xbottom");
18932 cpl_table_divide_scalar(slits,
"x", 2);
18933 cpl_table_subtract_scalar(slits,
"x", nx/2);
18934 cpl_table_multiply_columns(slits,
"x",
"x");
18936 cpl_table_duplicate_column(slits,
"y", slits,
"ytop");
18937 cpl_table_add_columns(slits,
"y",
"ybottom");
18938 cpl_table_divide_scalar(slits,
"y", 2);
18939 cpl_table_subtract_scalar(slits,
"y", ny/2);
18940 cpl_table_multiply_columns(slits,
"y",
"y");
18942 cpl_table_add_columns(slits,
"x",
"y");
18943 cpl_table_get_column_minpos(slits,
"x", &row);
18945 cpl_table_erase_column(slits,
"x");
18946 cpl_table_erase_column(slits,
"y");
18971 double xwidth,
double ywidth,
18972 int dx,
double gain,
double *o_flux,
double *o_err)
18974 int nx = cpl_image_get_size_x(image);
18975 int ny = cpl_image_get_size_y(image);
18977 int ytop = (int)cpl_table_get(slits,
"ytop", slit, NULL);
18978 int ybottom = (int)cpl_table_get(slits,
"ybottom", slit, NULL);
18979 int dy = ytop - ybottom;
18980 int xcenter = (int)((cpl_table_get(slits,
"xtop", slit, NULL) +
18981 cpl_table_get(slits,
"xbottom", slit, NULL)) / 2);
18982 int xleft = xcenter - dx;
18983 int xright = xcenter + dx + 1;
18984 double area = xwidth * ywidth;
18985 int npix = (2*dx + 1) * dy;
18987 float *data = cpl_image_get_data_float(image);
18989 double error = 0.0;
18994 if (cpl_table_has_column(slits,
"ywidth")) {
18995 area = cpl_table_get(slits,
"xwidth", slit, NULL)
18996 * cpl_table_get(slits,
"ywidth", slit, NULL);
19026 count = (xright - xleft) * (ytop - ybottom);
19029 return CPL_ERROR_ACCESS_OUT_OF_RANGE;
19033 for (y = ybottom; y < ytop; y++) {
19034 for (x = xleft; x < xright; x++) {
19035 double value = data[x + y * nx];
19036 if (value < satur) {
19044 return CPL_ERROR_DIVISION_BY_ZERO;
19046 error = sqrt(flux/gain);
19052 flux *= (float)npix / count;
19053 error *= (float)npix / count;
19061 return CPL_ERROR_NONE;
19088 double xwidth,
double ywidth,
19089 double lambda,
double startwave,
19090 double dispersion,
int dx,
double gain,
19091 double *o_flux,
double *o_err)
19093 int nx = cpl_image_get_size_x(image);
19094 int ny = cpl_image_get_size_y(image);
19096 int dy = (int)cpl_table_get(slits,
"length", slit, NULL);
19097 int ybottom = (int)cpl_table_get(slits,
"position", slit, NULL);
19098 int ytop = ybottom + dy;
19099 int xcenter = (int)floor((lambda - startwave) / dispersion + 0.5);
19100 int xleft = xcenter - dx;
19101 int xright = xcenter + dx + 1;
19102 double area = xwidth * ywidth;
19103 int npix = (2*dx + 1) * dy;
19105 float *data = cpl_image_get_data_float(image);
19107 double error = 0.0;
19112 if (cpl_table_has_column(slits,
"ywidth")) {
19113 area = cpl_table_get(slits,
"xwidth", slit, NULL)
19114 * cpl_table_get(slits,
"ywidth", slit, NULL);
19144 count = (xright - xleft) * (ytop - ybottom);
19147 return CPL_ERROR_ACCESS_OUT_OF_RANGE;
19151 for (y = ybottom; y < ytop; y++) {
19152 for (x = xleft; x < xright; x++) {
19153 double value = data[x + y * nx];
19154 if (value < satur) {
19162 return CPL_ERROR_DIVISION_BY_ZERO;
19164 error = sqrt(flux/gain);
19170 flux *= (float)npix / count;
19171 error *= (float)npix / count;
19179 return CPL_ERROR_NONE;
19198 char *label,
double *mvalue)
19200 int position = cpl_table_get_int(slits,
"position", slit, NULL);
19201 int length = cpl_table_get_int(slits,
"length", slit, NULL);
19202 cpl_table *tmp = cpl_table_extract(table, position, length);
19204 *mvalue = cpl_table_get_column_median(tmp, label);
19205 cpl_table_delete(tmp);
19207 if (cpl_error_get_code() != CPL_ERROR_NONE)
19227 cpl_mask *kernel = cpl_mask_new(nx, ny);
19228 cpl_image *filtered = cpl_image_new(cpl_image_get_size_x(image),
19229 cpl_image_get_size_y(image),
19230 cpl_image_get_type(image));
19232 cpl_mask_not(kernel);
19233 cpl_image_filter_mask(filtered, image, kernel,
19234 CPL_FILTER_MEDIAN, CPL_BORDER_FILTER);
19235 cpl_mask_delete(kernel);
19240 int fors_mos_is_lss_like(cpl_table *maskslits,
int nslits_out_det)
19242 int treat_as_lss = 1;
19243 double mxpos = cpl_table_get_column_median(maskslits,
"xtop");
19244 double * slit_xpos = cpl_table_get_data_double(maskslits,
"xtop");
19245 cpl_size nslits = cpl_table_get_nrow(maskslits);
19249 if(nslits_out_det != 0)
19252 for (cpl_size i = 0; i < nslits; i++) {
19253 if (fabs(mxpos-slit_xpos[i]) > 0.01) {
19258 return treat_as_lss;
cpl_image * mos_spatial_calibration(cpl_image *spectra, cpl_table *slits, cpl_table *polytraces, double reference, double blue, double red, double dispersion, int flux, cpl_image *calibration)
Spatial remapping of CCD spectra eliminating the spectral curvature.
cpl_table * mos_photometric_calibration(cpl_image *spectra, double startwave, double dispersion, double gain, double exptime, cpl_table *ext_table, double airmass, cpl_table *flux_table, int order)
Produce instrument response curve, with some ancillary information.
cpl_table * mos_build_disp_coeff(cpl_table *global, cpl_table *slits)
Build the IDS coefficients table from a global distortions table.
cpl_image * mos_map_pixel(cpl_table *idscoeff, double reference, double blue, double red, double dispersion, int trend)
Create a pixel map from an IDS coefficients table.
double mos_integrate_signal(cpl_image *image, cpl_image *wavemap, int ystart, int yend, double wstart, double wend)
Integrate signal from wavelength and spatial interval.
cpl_bivector * mos_identify_peaks(cpl_vector *peaks, cpl_vector *lines, double min_disp, double max_disp, double tolerance)
Identify peak candidates.
cpl_vector * mos_refine_peaks(const float *spectrum, int length, cpl_vector *peaks, int sradius)
Improve (when possible) accuracy of peaks candidates positions.
cpl_table * mos_sky_map(cpl_image *spectra, cpl_image *wavemap, double dispersion, cpl_image *skymap)
Create a CCD median sky map.
cpl_error_code mos_interpolate_wavecalib(cpl_table *idscoeff, cpl_image *wavemap, int mode, int degree)
Interpolate LSS wavelength calibration.
cpl_image * mos_wavelength_calibration_raw(const cpl_image *image, cpl_vector *lines, double dispersion, float level, int sradius, int order, double reject, double refwave, double *wavestart, double *waveend, int *nlines, double *error, cpl_table *idscoeff, cpl_image *calibration, cpl_image *residuals, cpl_table *restable, cpl_mask *refmask, cpl_table *detected_lines)
Derive wavelength calibration from a raw arc lamp or sky exposure.
double mos_distortions_rms(cpl_image *rectified, cpl_vector *lines, double wavestart, double dispersion, int radius, int highres)
Estimate the spectral distortion modeling goodness.
cpl_image * mos_propagate_photometry_error(cpl_image *spectra, cpl_image *errors, cpl_table *response, cpl_table *ext_table, double startwave, double dispersion, double gain, double exptime, double airmass)
Propagate errors from response curve and extracted spectra.
cpl_table * mos_load_slits_fors_mxu(cpl_propertylist *header)
Create slit location table from FITS header of FORS2-MXU data.
cpl_image * mos_normalise_flat(cpl_image *flat, cpl_image *spatial, cpl_table *slits, cpl_table *polytraces, double reference, double blue, double red, double dispersion, int sradius, int polyorder)
Normalise a flat field exposure.
int mos_get_nobjects(cpl_table *slits)
Get the total number of objects detected in a slits table.
cpl_error_code mos_rotate_slits(cpl_table *slits, int rotation, int nx, int ny)
Rotate a slit location table.
cpl_image * mos_map_wavelengths(cpl_image *spatial, cpl_image *calibration, cpl_table *slits, cpl_table *polytraces, double reference, double blue, double red, double dispersion)
Remapping of spatially rectified wavelengths to original CCD pixels.
cpl_image * mos_wavelength_calibration(cpl_image *image, double refwave, double firstLambda, double lastLambda, double dispersion, cpl_table *idscoeff, int flux)
Remap at constant wavelength step an image of rectified scientific spectra.
cpl_table * mos_identify_slits(cpl_table *slits, cpl_table *maskslits, cpl_table *global)
Identify slits listed in a slit location table.
cpl_table * mos_sky_map_super(cpl_image *spectra, cpl_image *wavemap, double dispersion, double factor, int minpoints, cpl_image *skymap)
Create a CCD median sky map.
cpl_image * mos_sky_local(cpl_image *spectra, cpl_table *slits, int order)
Local determination of sky.
double fors_tools_get_kth_double(double *a, int n, int k)
Same as cpl_tools_get_kth_double.
cpl_table * mos_load_slits_fors_mos(cpl_propertylist *header, int *nslits_out_det)
Create slit location table from FITS header of FORS1/2 MOS data.
double mos_get_gain_vimos(cpl_propertylist *header)
Return gain factor for a VIMOS exposure.
cpl_table * mos_wavelength_align(cpl_image *image, cpl_table *slits, double refwave, double firstLambda, double lastLambda, cpl_table *idscoeff, cpl_vector *skylines, int highres, int order, cpl_image *calibration, int sradius)
Modify the input wavelength solution to match reference sky lines.
cpl_error_code mos_validate_slits(cpl_table *slits)
Check validity of a slit location table.
cpl_error_code mos_subtract_background(cpl_image *image)
Subtract the background.
int mos_get_maxobjs_per_slit(cpl_table *slits)
Get the maximum possible number of objects in a slit.
cpl_error_code mos_interpolate_wavecalib_slit(cpl_table *idscoeff, cpl_table *slits, int order, int global)
Interpolate MOS wavelength calibration.
cpl_image * mos_remove_bias(cpl_image *image, cpl_image *bias, cpl_table *overscans)
Subtract the bias from a CCD exposure.
cpl_table * mos_poly_trace(cpl_table *slits, cpl_table *traces, int order)
Fit spectral traces.
cpl_image * mos_detect_objects(cpl_image *image, cpl_table *slits, int margin, int maxradius, int conradius)
Detect objects in rectified scientific frame.
cpl_image * mos_sky_local_old(cpl_image *spectra, cpl_table *slits)
Local determination of sky.
int mos_check_polarisation(cpl_image *q_image, cpl_image *q_error, cpl_image *u_image, cpl_image *u_error, double startwave, double dispersion, double band, cpl_table *pol_sta, double ra, double dec, char *filter, int *polarisation, double *p_offset, double *p_error, double *a_offset, double *a_error)
Estimate linear polarisation parameters on spectral interval.
cpl_polynomial * mos_poly_wav2pix(cpl_bivector *pixwav, int order, double reject, int minlines, int *nlines, double *err)
Fit polynomial relation from wavelengths to pixels.
cpl_error_code mos_image_shift(cpl_image *image, double dx, double dy)
Shift values in an image.
int mos_check_multiplex(cpl_table *slits)
Determining whether a VIMOS mask has spectral multplexing or not.
cpl_error_code mos_extract_flux(cpl_image *image, cpl_table *slits, double xwidth, double ywidth, int dx, double gain, double *o_flux, double *o_err)
Measure flux from spectral interval on CCD.
int mos_lines_width(const float *spectrum, int length)
Estimate lines widths (in pixel) in arc lamp spectrum.
cpl_error_code mos_global_trace(cpl_table *slits, cpl_table *polytraces, int mode)
Recompute tracing coefficients globally.
cpl_bivector * mos_find_peaks(const float *spectrum, int length, cpl_vector *lines, cpl_polynomial *ids, double refwave, int sradius)
Find the reference lines peaks using a polynomial first-guess.
cpl_vector * mos_peak_candidates(const float *spectrum, int length, float level, float exp_width)
Find positions of peaks candidates.
cpl_error_code mos_object_intersect(cpl_table **slitss, cpl_table *origslits, int nscience, float tolerance)
Intersect a number of slit tables.
cpl_table * mos_wavelength_align_lss(cpl_image *image, double refwave, double firstLambda, double lastLambda, cpl_table *idscoeff, cpl_vector *skylines, int highres, int order, cpl_image *calibration, int sradius)
Modify the input wavelength solution to match reference sky lines (LSS).
cpl_image * mos_subtract_sky(cpl_image *science, cpl_table *slits, cpl_table *polytraces, double reference, double blue, double red, double dispersion)
Subtract the sky from the scientific CCD exposure.
cpl_image * mos_map_idscoeff(cpl_table *idscoeff, int xsize, double reference, double blue, double red)
Create a wavelengths map from an IDS coefficients table.
double mos_eval_dds(cpl_polynomial *ids, double blue, double red, double refwave, double pixel)
Evaluate the wavelength of a pixel position.
cpl_table * mos_trace_flat(cpl_image *flat, cpl_table *slits, double reference, double blue, double red, double dispersion)
Trace flat field spectra.
cpl_image * mos_wavelength_calibration_final(cpl_image *image, cpl_table *slits, cpl_vector *lines, double dispersion, float level, int sradius, int order, double reject, double refwave, double *wavestart, double *waveend, int *nlines, double *error, cpl_table *idscoeff, cpl_image *calibration, cpl_image *residuals, cpl_table *restable, cpl_table *detected_lines)
Derive wavelength calibration from a rectified arc lamp or sky exposure.
int mos_check_slits(cpl_table *slits, float rescale)
Check that all slit have been detected, insert them if not.
cpl_image * mos_apply_photometry(cpl_image *spectra, cpl_table *response, cpl_table *ext_table, double startwave, double dispersion, double gain, double exptime, double airmass)
Apply response curve to extracted spectra.
cpl_error_code mos_refmask_find_gaps(cpl_mask *refmask, cpl_image *master_flat, double level)
Reconstruct the gaps required for slit location.
cpl_image * mos_ksigma_stack(cpl_imagelist *imlist, double klow, double khigh, int kiter, cpl_image **good)
Stack images using k-sigma clipping.
cpl_image ** mos_extract_objects(cpl_image *science, cpl_image *science_var, cpl_image *sky, cpl_table *objects, int extraction, double ron, double gain, int ncombined)
Extract detected objects from rectified scientific frame.
cpl_error_code mos_clean_cosmics(cpl_image *image, float gain, float threshold, float ratio)
Remove cosmic rays from sky-subtracted CCD spectral exposure.
cpl_table * mos_global_distortion(cpl_table *slits, cpl_table *maskslits, cpl_table *ids, cpl_table *crv, double reference)
Determine all global distortions models.
int mos_spectral_resolution(cpl_image *image, double lambda, double startwave, double dispersion, int saturation, double *mfwhm, double *rmsfwhm, double *resolution, double *rmsres, int *nlines)
Compute mean spectral resolution at a given arc lamp line.
cpl_error_code mos_arc_background_1D(float *spectrum, float *back, int length, int msize, int fsize)
Background determination on 1D emission line spectrum (arc)
cpl_table * mos_resolution_table(cpl_image *image, double startwave, double dispersion, int saturation, cpl_vector *lines)
Compute mean spectral resolution at a given arc lamp line.
cpl_table * mos_load_slits_fors_pmos(cpl_propertylist *header, int *nslits_out_det)
Create PMOS slit location table from FITS header of FORS1/2 MOS data.
cpl_table * mos_load_slits_vimos(cpl_propertylist *header)
Create slit location table from FITS header of VIMOS data.
cpl_error_code mos_randomise_image(cpl_image *image, double ron, double gain, double bias)
Randomise image.
cpl_table * mos_build_curv_coeff(cpl_table *global, cpl_table *maskslits, cpl_table *slits)
Build the curvature coefficients table from a global distortions table.
cpl_table * mos_locate_spectra(cpl_mask *mask)
Find the location of detected spectra on the CCD.
cpl_table * mos_load_slits_fors_lss(cpl_propertylist *header)
Create slit location table from FITS header of FORS1/2 LSS data.
cpl_polynomial * mos_poly_pix2wav(cpl_bivector *pixwav, int order, double reject, int minlines, int *nlines, double *err)
Fit polynomial relation from pixels to wavelengths.
cpl_error_code mos_interpolate_wavecalib_mos(cpl_table *idscoeff, int mode, int degree)
Interpolate wavelength calibration for a single MOS slit.
cpl_image * mos_arc_background(cpl_image *image, int msize, int fsize)
Background determination on emission line spectrum (arc)
cpl_error_code mos_saturation_process(cpl_image *image)
Process saturation.
cpl_image * mos_map_spectrum(cpl_image *spectra, cpl_image *wavecalib, cpl_image *spatial, cpl_table *slits, cpl_table *polytraces, double reference, double blue, double red, double dispersion, int flux)
Remapping of slit spectra into a grid of lambda-space coordinates.
int mos_slit_closest_to_center(cpl_table *slits, int nx, int ny)
Return slit closest to CCD center.
cpl_image * mos_image_filter_median(cpl_image *image, int nx, int ny)
Convenience function for standard median filtering.
cpl_image * mos_spatial_map(cpl_image *spectra, cpl_table *slits, cpl_table *polytraces, double reference, double blue, double red, double dispersion)
Create coordinate map from spectral curvature table.
cpl_error_code mos_extract_flux_mapped(cpl_image *image, cpl_table *slits, double xwidth, double ywidth, double lambda, double startwave, double dispersion, int dx, double gain, double *o_flux, double *o_err)
Measure flux from spectral interval on remapped frame.
int mos_compute_offset(cpl_table *reference, cpl_table *objects, double *offset)
Estimate offset between two object tables.
cpl_image * mos_normalise_longflat(cpl_image *flat, int sradius, int dradius, int polyorder)
Normalise a long slit flat field exposure.
cpl_table * mos_load_overscans_vimos(const cpl_propertylist *header, int check_consistency)
Get the overscan positions from FITS header of VIMOS data.
int mos_median_in_slit(cpl_table *table, cpl_table *slits, int slit, char *label, double *mvalue)
Compute median from a table column section corresponding to a slit.
cpl_table * mos_build_slit_location(cpl_table *global, cpl_table *maskslits, int ysize)
Build the slit location table from a global distortions table.