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,
5063 cpl_bivector **pixwav_used)
5065 const char *func =
"mos_poly_wav2pix";
5067 cpl_bivector *pixwav2;
5077 cpl_polynomial *ids;
5083 if (pixwav == NULL) {
5084 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
5088 fitlines = cpl_bivector_get_size(pixwav);
5090 if (fitlines < minlines) {
5091 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
5105 pixwav2 = cpl_bivector_duplicate(pixwav);
5115 pixel = cpl_bivector_get_x(pixwav2);
5116 wavel = cpl_bivector_get_y(pixwav2);
5124 cpl_bivector_unwrap_vectors(pixwav2);
5131 while (fitlines >= minlines) {
5133 ids = cpl_polynomial_fit_1d_create(wavel, pixel, order, err);
5137 cpl_msg_debug(cpl_error_get_where(),
"%s", cpl_error_get_message());
5138 cpl_msg_debug(func,
"Fitting IDS");
5139 cpl_error_set_where(func);
5141 cpl_vector_delete(wavel);
5142 cpl_vector_delete(pixel);
5148 cpl_vector * wavel_used = cpl_vector_duplicate(wavel);
5149 cpl_vector * pixel_used = cpl_vector_duplicate(pixel);
5156 d_pixel = cpl_vector_unwrap(pixel);
5157 d_wavel = cpl_vector_unwrap(wavel);
5159 for (i = 0, j = 0; i < fitlines; i++) {
5160 pixpos = cpl_polynomial_eval_1d(ids, d_wavel[i], NULL);
5161 if (fabs(pixpos - d_pixel[i]) < reject) {
5162 d_pixel[j] = d_pixel[i];
5163 d_wavel[j] = d_wavel[i];
5168 if (j == fitlines) {
5169 cpl_bivector * pixwav_used_temp =
5170 cpl_bivector_wrap_vectors(pixel_used, wavel_used);
5171 *pixwav_used = cpl_bivector_duplicate(pixwav_used_temp);
5172 cpl_bivector_unwrap_vectors(pixwav_used_temp);
5173 cpl_vector_delete(wavel_used);
5174 cpl_vector_delete(pixel_used);
5182 cpl_polynomial_delete(ids);
5183 if (fitlines >= minlines) {
5184 pixel = cpl_vector_wrap(fitlines, d_pixel);
5185 wavel = cpl_vector_wrap(fitlines, d_wavel);
5190 cpl_error_set(func, CPL_ERROR_CONTINUE);
5194 cpl_vector_delete(wavel_used);
5195 cpl_vector_delete(pixel_used);
5199 *pixwav_used = cpl_bivector_duplicate(pixwav2);
5233 double reject,
int minlines,
5234 int *nlines,
double *err)
5237 cpl_bivector *wavpix;
5241 cpl_polynomial *dds;
5243 cpl_bivector *wavepix_used;
5250 pixel = cpl_bivector_get_x(pixwav);
5251 wavel = cpl_bivector_get_y(pixwav);
5253 wavpix = cpl_bivector_wrap_vectors(wavel, pixel);
5258 cpl_bivector_unwrap_vectors(wavpix);
5260 cpl_bivector_delete(wavepix_used);
5290 cpl_vector *lines, cpl_polynomial *ids,
5291 double refwave,
int sradius)
5293 const char *func =
"mos_find_peaks";
5304 if (spectrum == NULL || lines == NULL || ids == NULL) {
5305 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
5309 nlines = cpl_vector_get_size(lines);
5311 if (sradius < 1 || length < 2*sradius+1 || nlines < 1) {
5312 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
5316 d_wavel = cpl_malloc(nlines *
sizeof(
double));
5317 d_pixel = cpl_malloc(nlines *
sizeof(
double));
5319 data = cpl_vector_get_data(lines);
5321 for (i = 0, j = 0; i < nlines; i++) {
5322 pixel = cpl_polynomial_eval_1d(ids, data[i]-refwave, NULL) + 0.5;
5323 if (pixel < 0 || pixel - sradius < 0 || pixel + sradius >= length)
5325 if (peakPosition(spectrum+pixel-sradius, 2*sradius+1, &pos, 1) == 0) {
5326 pos += pixel - sradius;
5328 d_wavel[j] = data[i];
5334 return cpl_bivector_wrap_vectors(cpl_vector_wrap(j, d_pixel),
5335 cpl_vector_wrap(j, d_wavel));
5340 cpl_error_set(func, CPL_ERROR_ILLEGAL_OUTPUT);
5471 double dispersion,
float level,
5472 int sradius,
int order,
5473 double reject,
double refwave,
5474 double *wavestart,
double *waveend,
5475 int *nlines,
double *error,
5476 cpl_table *idscoeff,
5477 cpl_image *calibration,
5478 cpl_image *residuals,
5479 cpl_table *restable,
5481 cpl_table *detected_lines)
5484 const char *func =
"mos_wavelength_calibration_raw";
5486 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
5489 double tolerance = 20.0;
5492 char name[MAX_COLNAME];
5493 cpl_image *resampled;
5494 cpl_bivector *output;
5495 cpl_bivector *new_output;
5498 cpl_polynomial *ids;
5499 cpl_polynomial *lin;
5502 double max_disp, min_disp;
5504 double firstLambda, lastLambda, lambda;
5505 double value, wave, pixe;
5514 int pixstart, pixend;
5517 int nl, nx, ny, pixel;
5518 int countLines, usedLines;
5520 int in, first, last;
5527 if (dispersion == 0.0) {
5528 cpl_msg_error(func,
"The expected dispersion (A/pixel) must be given");
5529 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
5533 if (dispersion < 0.0) {
5534 cpl_msg_error(func,
"The expected dispersion must be positive");
5535 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
5539 max_disp = dispersion + dispersion * tolerance / 100;
5540 min_disp = dispersion - dispersion * tolerance / 100;
5543 cpl_msg_error(func,
"The order of the fitting polynomial "
5544 "must be at least 1");
5545 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
5549 if (image == NULL || lines == NULL) {
5550 cpl_msg_error(func,
"Both spectral exposure and reference line "
5551 "catalog are required in input");
5552 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
5556 nx = cpl_image_get_size_x(image);
5557 ny = cpl_image_get_size_y(image);
5558 sdata = cpl_image_get_data_float_const(image);
5560 nref = cpl_vector_get_size(lines);
5561 line = cpl_vector_get_data(lines);
5563 if (*wavestart < 1.0 && *waveend < 1.0) {
5564 firstLambda = line[0];
5565 lastLambda = line[nref-1];
5566 extrapolation = (lastLambda - firstLambda) / 10;
5567 firstLambda -= extrapolation;
5568 lastLambda += extrapolation;
5569 *wavestart = firstLambda;
5570 *waveend = lastLambda;
5573 firstLambda = *wavestart;
5574 lastLambda = *waveend;
5577 nl = (lastLambda - firstLambda) / dispersion;
5578 resampled = cpl_image_new(nl, ny, CPL_TYPE_FLOAT);
5579 rdata = cpl_image_get_data_float(resampled);
5582 idata = cpl_image_get_data_float(calibration);
5585 ddata = cpl_image_get_data_float(residuals);
5588 for (j = 0; j <= order; j++)
5589 cpl_table_new_column(idscoeff, clab[j], CPL_TYPE_DOUBLE);
5592 cpl_table_set_size(restable, nref);
5593 cpl_table_new_column(restable,
"wavelength", CPL_TYPE_DOUBLE);
5594 cpl_table_copy_data_double(restable,
"wavelength", line);
5595 for (i = 0; i < ny; i += step) {
5596 snprintf(name, MAX_COLNAME,
"r%d", i);
5597 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
5598 snprintf(name, MAX_COLNAME,
"d%d", i);
5599 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
5600 snprintf(name, MAX_COLNAME,
"p%d", i);
5601 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
5605 if (detected_lines) {
5606 cpl_table_set_size(detected_lines, 0);
5607 cpl_table_new_column(detected_lines,
"xpos", CPL_TYPE_DOUBLE);
5608 cpl_table_new_column(detected_lines,
"ypos", CPL_TYPE_DOUBLE);
5609 cpl_table_new_column(detected_lines,
"xpos_iter", CPL_TYPE_DOUBLE);
5610 cpl_table_new_column(detected_lines,
"ypos_iter", CPL_TYPE_DOUBLE);
5611 cpl_table_new_column(detected_lines,
"peak_flux", CPL_TYPE_DOUBLE);
5612 cpl_table_new_column(detected_lines,
"wave_ident", CPL_TYPE_DOUBLE);
5613 cpl_table_new_column(detected_lines,
"wave_ident_iter", CPL_TYPE_DOUBLE);
5614 cpl_table_new_column(detected_lines,
"xpos_fit_rect_wavecal", CPL_TYPE_DOUBLE);
5615 cpl_table_new_column(detected_lines,
"res_xpos", CPL_TYPE_DOUBLE);
5616 cpl_table_new_column(detected_lines,
"fit_used", CPL_TYPE_INT);
5625 for (i = 0; i < ny; i++) {
5628 if (width > sradius) {
5644 cpl_bivector * peaks_ident_used_fit;
5645 countLines = cpl_bivector_get_size(output);
5646 if (countLines < 4) {
5647 cpl_bivector_delete(output);
5648 cpl_vector_delete(peaks);
5660 wavel = cpl_bivector_get_y(output);
5661 cpl_vector_subtract_scalar(wavel, refwave);
5663 uorder = countLines / 2 - 1;
5677 2 * (uorder + 1), &usedLines,
5678 &ids_err, &peaks_ident_used_fit);
5681 cpl_bivector_delete(output);
5682 cpl_vector_delete(peaks);
5699 for (k = 0; k <= order; k++) {
5701 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
5704 cpl_table_set_double(idscoeff, clab[k], i,
5705 cpl_polynomial_get_coeff(ids, &k));
5712 cpl_size newlines = cpl_vector_get_size(peaks);
5713 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
5714 cpl_table_set_size(detected_lines, oldsize + newlines);
5715 for(cpl_size iline = 0; iline < newlines; ++iline)
5717 cpl_table_set_double(detected_lines,
"xpos",
5718 oldsize + iline, cpl_vector_get(peaks, iline) + 1);
5719 cpl_table_set_double(detected_lines,
"ypos",
5720 oldsize + iline, (
double)i + 1);
5721 cpl_table_set_double(detected_lines,
"peak_flux",
5723 sdata[i*nx+(
int)(cpl_vector_get(peaks, iline)+0.5)]);
5724 cpl_table_set_int(detected_lines,
5726 oldsize + iline, 0);
5734 cpl_size nidentlines = cpl_bivector_get_size(output);
5735 cpl_size ndetectlines = cpl_vector_get_size(peaks);
5736 cpl_size totalsize = cpl_table_get_nrow(detected_lines);
5737 for(cpl_size idline = 0; idline < nidentlines; ++idline)
5739 for(cpl_size detline = 0; detline < ndetectlines; ++detline)
5741 if(cpl_vector_get(peaks, detline) ==
5742 cpl_bivector_get_x_data(output)[idline])
5744 cpl_size table_pos = totalsize - ndetectlines + detline;
5745 double wave_ident = cpl_bivector_get_y_data(output)[idline] + refwave;
5746 double xpix_fit = cpl_polynomial_eval_1d(ids,
5747 wave_ident - refwave, NULL);
5748 double xpos_det = cpl_table_get_double(detected_lines,
5751 cpl_table_set_double(detected_lines,
5755 cpl_table_set_double(detected_lines,
5756 "xpos_fit_rect_wavecal",
5759 cpl_table_set_double(detected_lines,
5762 xpos_det - xpix_fit - 1);
5763 cpl_table_set_int(detected_lines,
5766 for(cpl_size i_used = 0; i_used < cpl_bivector_get_size(peaks_ident_used_fit); ++i_used)
5768 if(cpl_bivector_get_x_data(output)[idline] == cpl_bivector_get_x_data(peaks_ident_used_fit)[i_used])
5769 cpl_table_set_int(detected_lines,
5779 cpl_bivector * peaks_ident_used_fit;
5786 ids, refwave, uradius);
5789 cpl_bivector_delete(output);
5790 output = new_output;
5796 cpl_polynomial_delete(ids);
5798 countLines = cpl_bivector_get_size(output);
5800 if (countLines < 4) {
5801 cpl_bivector_delete(output);
5802 cpl_vector_delete(peaks);
5815 for (k = 0; k <= order; k++)
5816 cpl_table_set_invalid(idscoeff, clab[k], i);
5820 wavel = cpl_bivector_get_y(output);
5821 cpl_vector_subtract_scalar(wavel, refwave);
5823 uorder = countLines / 2 - 1;
5828 2 * (uorder + 1), &usedLines,
5829 &ids_err, &peaks_ident_used_fit);
5832 cpl_bivector_delete(output);
5833 cpl_vector_delete(peaks);
5846 for (k = 0; k <= order; k++)
5847 cpl_table_set_invalid(idscoeff, clab[k], i);
5853 for (k = 0; k <= order; k++) {
5855 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
5858 cpl_table_set_double(idscoeff, clab[k], i,
5859 cpl_polynomial_get_coeff(ids, &k));
5867 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
5868 cpl_size nidentlines = cpl_bivector_get_size(output);
5869 cpl_table_set_size(detected_lines, oldsize + nidentlines);
5870 for(cpl_size idline = 0; idline < nidentlines ; ++idline)
5872 double wave_ident = cpl_bivector_get_y_data(output)[idline] + refwave;
5873 double xpix_fit = cpl_polynomial_eval_1d(ids,
5874 wave_ident - refwave, NULL);
5875 cpl_table_set_double(detected_lines,
"xpos_iter",
5876 oldsize + idline, cpl_bivector_get_x_data(output)[idline] + 1);
5877 cpl_table_set_double(detected_lines,
"ypos_iter",
5878 oldsize + idline, (
double)i + 1);
5879 cpl_table_set_double(detected_lines,
"peak_flux",
5881 sdata[i*nx+(
int)(cpl_bivector_get_x_data(output)[idline]+0.5)]);
5882 cpl_table_set_double(detected_lines,
"wave_ident_iter",
5883 oldsize + idline, wave_ident);
5884 cpl_table_set_double(detected_lines,
"xpos_fit_rect_wavecal",
5885 oldsize + idline, xpix_fit + 1);
5892 nlines[i] = usedLines;
5894 error[i] = ids_err / sqrt(usedLines/(uorder + 1));
5896 pixstart = cpl_polynomial_eval_1d(ids,
5897 cpl_bivector_get_y_data(output)[0], NULL);
5898 pixend = cpl_polynomial_eval_1d(ids,
5899 cpl_bivector_get_y_data(output)[countLines-1], NULL);
5900 extrapolation = (pixend - pixstart) / 5;
5901 pixstart -= extrapolation;
5902 pixend += extrapolation;
5913 for (j = pixstart; j < pixend; j++) {
5915 lastLambda, refwave,
5924 for (j = 0; j < nl; j++) {
5925 lambda = firstLambda + j * dispersion;
5926 fpixel = cpl_polynomial_eval_1d(ids, lambda - refwave,
5929 if (pixel >= 0 && pixel < nx-1) {
5930 v1 = (sdata + i*nx)[pixel];
5931 v2 = (sdata + i*nx)[pixel+1];
5932 vi = v1 + (v2-v1)*(fpixel-pixel);
5933 (rdata + i*nl)[j] = vi;
5941 if (residuals || (restable && !(i%step))) {
5942 if (restable && !(i%step)) {
5943 lin = cpl_polynomial_new(1);
5944 for (k = 0; k < 2; k++)
5945 cpl_polynomial_set_coeff(lin, &k,
5946 cpl_polynomial_get_coeff(ids, &k));
5948 for (j = 0; j < countLines; j++) {
5949 pixe = cpl_bivector_get_x_data(output)[j];
5950 wave = cpl_bivector_get_y_data(output)[j];
5951 value = pixe - cpl_polynomial_eval_1d(ids, wave, NULL);
5954 (ddata + i*nx)[pixel] = value;
5956 if (restable && !(i%step)) {
5957 for (k = 0; k < nref; k++) {
5958 if (fabs(line[k] - refwave - wave) < 0.1) {
5959 snprintf(name, MAX_COLNAME,
"r%d", i);
5960 cpl_table_set_double(restable, name,
5963 - cpl_polynomial_eval_1d(lin, wave,
5965 snprintf(name, MAX_COLNAME,
"d%d", i);
5966 cpl_table_set_double(restable, name,
5968 snprintf(name, MAX_COLNAME,
"p%d", i);
5969 cpl_table_set_double(restable, name,
5976 if (restable && !(i%step)) {
5977 cpl_polynomial_delete(lin);
5986 mdata = cpl_mask_get_data(refmask);
5987 pixel = cpl_polynomial_eval_1d(ids, 0.0, NULL) + 0.5;
5988 if (pixel - 1 >= 0 && pixel + 1 < nx) {
5989 mdata[pixel-1 + i*nx] = CPL_BINARY_1;
5990 mdata[pixel + i*nx] = CPL_BINARY_1;
5991 mdata[pixel+1 + i*nx] = CPL_BINARY_1;
5995 cpl_polynomial_delete(ids);
5996 cpl_bivector_delete(output);
5998 cpl_vector_delete(peaks);
6003 kernel = cpl_matrix_new(3, 3);
6004 cpl_matrix_set(kernel, 0, 1, 1.0);
6005 cpl_matrix_set(kernel, 1, 1, 1.0);
6006 cpl_matrix_set(kernel, 2, 1, 1.0);
6008 cpl_mask_dilation(refmask, kernel);
6009 cpl_mask_erosion(refmask, kernel);
6010 cpl_mask_erosion(refmask, kernel);
6011 cpl_mask_dilation(refmask, kernel);
6013 cpl_matrix_delete(kernel);
6019 mdata = cpl_mask_get_data(refmask);
6020 have_it = cpl_calloc(ny,
sizeof(
int));
6022 for (i = 0; i < ny; i++, mdata += nx) {
6023 for (j = 0; j < nx; j++) {
6024 if (mdata[j] == CPL_BINARY_1) {
6031 mdata = cpl_mask_get_data(refmask);
6035 for (i = 0; i < ny; i++) {
6041 if (abs(have_it[first] - have_it[last]) < 3) {
6042 for (j = first; j < last; j++) {
6043 mdata[have_it[first] + nx*j + 0] = CPL_BINARY_1;
6044 mdata[have_it[first] + nx*j + 1] = CPL_BINARY_1;
6045 mdata[have_it[first] + nx*j + 2] = CPL_BINARY_1;
6187 const char *func =
"mos_locate_spectra";
6189 cpl_apertures *slits;
6190 cpl_image *labimage;
6191 cpl_image *refimage;
6193 cpl_propertylist *sort_col;
6199 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
6203 labimage = cpl_image_labelise_mask_create(mask, &nslits);
6206 cpl_image_delete(labimage);
6207 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6211 refimage = cpl_image_new_from_mask(mask);
6213 slits = cpl_apertures_new_from_image(refimage, labimage);
6215 cpl_image_delete(labimage);
6216 cpl_image_delete(refimage);
6218 nslits = cpl_apertures_get_size(slits);
6220 cpl_apertures_delete(slits);
6221 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6225 slitpos = cpl_table_new(nslits);
6226 cpl_table_new_column(slitpos,
"xtop", CPL_TYPE_DOUBLE);
6227 cpl_table_new_column(slitpos,
"ytop", CPL_TYPE_DOUBLE);
6228 cpl_table_new_column(slitpos,
"xbottom", CPL_TYPE_DOUBLE);
6229 cpl_table_new_column(slitpos,
"ybottom", CPL_TYPE_DOUBLE);
6230 cpl_table_set_column_unit(slitpos,
"xtop",
"pixel");
6231 cpl_table_set_column_unit(slitpos,
"ytop",
"pixel");
6232 cpl_table_set_column_unit(slitpos,
"xbottom",
"pixel");
6233 cpl_table_set_column_unit(slitpos,
"ybottom",
"pixel");
6235 for (i = 0; i < nslits; i++) {
6236 cpl_table_set_double(slitpos,
"xtop", i,
6237 cpl_apertures_get_top_x(slits, i+1) - 1);
6238 cpl_table_set_double(slitpos,
"ytop", i,
6239 cpl_apertures_get_top(slits, i+1));
6240 cpl_table_set_double(slitpos,
"xbottom", i,
6241 cpl_apertures_get_bottom_x(slits, i+1) - 1);
6242 cpl_table_set_double(slitpos,
"ybottom", i,
6243 cpl_apertures_get_bottom(slits, i+1));
6246 cpl_apertures_delete(slits);
6248 sort_col = cpl_propertylist_new();
6249 cpl_propertylist_append_bool(sort_col,
"ytop", 1);
6250 cpl_table_sort(slitpos, sort_col);
6251 cpl_propertylist_delete(sort_col);
6275 const char *func =
"mos_validate_slits";
6279 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
6281 if (1 != cpl_table_has_column(slits,
"xtop"))
6282 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6284 if (1 != cpl_table_has_column(slits,
"ytop"))
6285 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6287 if (1 != cpl_table_has_column(slits,
"xbottom"))
6288 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6290 if (1 != cpl_table_has_column(slits,
"ybottom"))
6291 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6293 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"xtop"))
6294 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6296 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"ytop"))
6297 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6299 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"xbottom"))
6300 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6302 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"ybottom"))
6303 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6305 return CPL_ERROR_NONE;
6339 const char *func =
"mos_rotate_slits";
6341 cpl_error_code error;
6342 char aux_name[] =
"_0";
6351 return CPL_ERROR_NONE;
6355 return cpl_error_set(func, error);
6357 if (rotation == 1 || rotation == 3) {
6363 for (i = 0; i < 77; i++)
6364 if (1 == cpl_table_has_column(slits, aux_name))
6366 if (1 == cpl_table_has_column(slits, aux_name))
6367 return cpl_error_set(func, CPL_ERROR_CONTINUE);
6368 cpl_table_name_column(slits,
"xtop", aux_name);
6369 cpl_table_name_column(slits,
"ytop",
"xtop");
6370 cpl_table_name_column(slits, aux_name,
"ytop");
6371 cpl_table_name_column(slits,
"xbottom", aux_name);
6372 cpl_table_name_column(slits,
"ybottom",
"xbottom");
6373 cpl_table_name_column(slits, aux_name,
"ybottom");
6376 if (rotation == 1 || rotation == 2) {
6377 cpl_table_multiply_scalar(slits,
"xtop", -1.0);
6378 cpl_table_multiply_scalar(slits,
"xbottom", -1.0);
6379 cpl_table_add_scalar(slits,
"xtop", nx);
6380 cpl_table_add_scalar(slits,
"xbottom", nx);
6383 if (rotation == 3 || rotation == 2) {
6384 cpl_table_multiply_scalar(slits,
"ytop", -1.0);
6385 cpl_table_multiply_scalar(slits,
"ybottom", -1.0);
6386 cpl_table_add_scalar(slits,
"ytop", ny);
6387 cpl_table_add_scalar(slits,
"ybottom", ny);
6390 return CPL_ERROR_NONE;
6454 cpl_array *top_ident = NULL;;
6455 cpl_array *bot_ident = NULL;;
6457 cpl_matrix *mpattern;
6458 cpl_matrix *top_data;
6459 cpl_matrix *top_pattern;
6460 cpl_matrix *top_mdata;
6461 cpl_matrix *top_mpattern;
6462 cpl_matrix *bot_data;
6463 cpl_matrix *bot_pattern;
6464 cpl_matrix *bot_mdata;
6465 cpl_matrix *bot_mpattern;
6466 cpl_propertylist *sort_col;
6475 double top_scale, bot_scale;
6476 double angle, top_angle, bot_angle;
6478 double xrms, top_xrms, bot_xrms;
6479 double yrms, top_yrms, bot_yrms;
6481 int nmaskslits, use_pattern;
6482 int found_slits, found_slits_top, found_slits_bot;
6484 cpl_table *positions;
6485 cpl_error_code error;
6494 cpl_polynomial *xpoly = NULL;
6495 cpl_polynomial *ypoly = NULL;
6496 cpl_polynomial *top_xpoly = NULL;
6497 cpl_polynomial *top_ypoly = NULL;
6498 cpl_polynomial *bot_xpoly = NULL;
6499 cpl_polynomial *bot_ypoly = NULL;
6501 char *msg_multiplex =
" ";
6506 cpl_msg_error(cpl_func,
"CCD slits table validation: %s",
6507 cpl_error_get_message());
6508 cpl_error_set(cpl_func, error);
6514 cpl_msg_error(cpl_func,
"Mask slits table validation: %s",
6515 cpl_error_get_message());
6516 cpl_error_set(cpl_func, error);
6520 if (1 != cpl_table_has_column(maskslits,
"slit_id")) {
6521 cpl_msg_error(cpl_func,
"Missing slits identifiers");
6522 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
6526 if (CPL_TYPE_INT != cpl_table_get_column_type(maskslits,
"slit_id")) {
6527 cpl_msg_error(cpl_func,
"Wrong type used for slits identifiers");
6528 cpl_error_set(cpl_func, CPL_ERROR_INVALID_TYPE);
6532 nslits = cpl_table_get_nrow(slits);
6533 nmaskslits = cpl_table_get_nrow(maskslits);
6535 if (nslits == 0 || nmaskslits == 0) {
6536 cpl_msg_error(cpl_func,
"Empty slits table");
6537 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
6541 if (nslits > 100 && mos_multiplex < 0) {
6542 cpl_msg_info(cpl_func,
"Many slits: using 'fast' pattern matching...");
6543 positions = mos_identify_slits_fast(slits, maskslits, global);
6544 if (positions == NULL)
6545 cpl_error_set_where(cpl_func);
6553 sort_col = cpl_propertylist_new();
6554 cpl_propertylist_append_bool(sort_col,
"ytop", 1);
6555 cpl_table_sort(slits, sort_col);
6556 cpl_table_sort(maskslits, sort_col);
6557 cpl_propertylist_delete(sort_col);
6563 if (nslits < 3 && nmaskslits > nslits) {
6573 cpl_msg_warning(cpl_func,
"Cannot match the %d found CCD slits "
6574 "with the %d mask slits: process will continue "
6575 "using the detected CCD slits positions", nslits,
6578 cpl_msg_warning(cpl_func,
"Cannot match the found CCD slit with "
6579 "the %d mask slits: process will continue using "
6580 "the detected CCD slit position", nmaskslits);
6584 if (nmaskslits < 3 && nslits > nmaskslits) {
6592 cpl_msg_warning(cpl_func,
"Cannot match the %d found CCD slits with "
6593 "the %d mask slits: process will continue using "
6594 "the detected CCD slits positions", nslits,
6606 xtop = cpl_table_get_data_double(slits,
"xtop");
6607 ytop = cpl_table_get_data_double(slits,
"ytop");
6608 xmtop = cpl_table_get_data_double(maskslits,
"xtop");
6609 ymtop = cpl_table_get_data_double(maskslits,
"ytop");
6611 xbot = cpl_table_get_data_double(slits,
"xbottom");
6612 ybot = cpl_table_get_data_double(slits,
"ybottom");
6613 xmbot = cpl_table_get_data_double(maskslits,
"xbottom");
6614 ymbot = cpl_table_get_data_double(maskslits,
"ybottom");
6616 top_data = cpl_matrix_new(2, nslits);
6617 top_pattern = cpl_matrix_new(2, nmaskslits);
6618 bot_data = cpl_matrix_new(2, nslits);
6619 bot_pattern = cpl_matrix_new(2, nmaskslits);
6621 for (i = 0; i < nslits; i++)
6622 cpl_matrix_set(top_data, 0, i, xtop[i]);
6624 for (i = 0; i < nslits; i++)
6625 cpl_matrix_set(top_data, 1, i, ytop[i]);
6627 for (i = 0; i < nmaskslits; i++)
6628 cpl_matrix_set(top_pattern, 0, i, xmtop[i]);
6630 for (i = 0; i < nmaskslits; i++)
6631 cpl_matrix_set(top_pattern, 1, i, ymtop[i]);
6633 for (i = 0; i < nslits; i++)
6634 cpl_matrix_set(bot_data, 0, i, xbot[i]);
6636 for (i = 0; i < nslits; i++)
6637 cpl_matrix_set(bot_data, 1, i, ybot[i]);
6639 for (i = 0; i < nmaskslits; i++)
6640 cpl_matrix_set(bot_pattern, 0, i, xmbot[i]);
6642 for (i = 0; i < nmaskslits; i++)
6643 cpl_matrix_set(bot_pattern, 1, i, ymbot[i]);
6645 if (nmaskslits > nslits)
6646 use_pattern = nslits;
6648 use_pattern = nmaskslits;
6650 top_ident = cpl_ppm_match_points(top_data, nslits, 1.0, top_pattern,
6651 use_pattern, 0.0, 0.1, 5, &top_mdata,
6652 &top_mpattern, &top_scale, &top_angle);
6654 bot_ident = cpl_ppm_match_points(bot_data, nslits, 1.0, bot_pattern,
6655 use_pattern, 0.0, 0.1, 5, &bot_mdata,
6656 &bot_mpattern, &bot_scale, &bot_angle);
6657 cpl_matrix_delete(top_data);
6658 cpl_matrix_delete(top_pattern);
6659 cpl_matrix_delete(bot_data);
6660 cpl_matrix_delete(bot_pattern);
6662 if (top_ident == NULL && bot_ident == NULL) {
6663 cpl_msg_warning(cpl_func,
"Pattern matching failure: cannot match "
6664 "the %d found CCD slits with the %d mask slits: "
6665 "process will continue using the detected CCD "
6666 "slits positions", nslits, nmaskslits);
6670 found_slits_top = 0;
6671 found_slits_bot = 0;
6672 if (top_ident && bot_ident) {
6673 cpl_msg_info(cpl_func,
"Median platescale: %f +/- %f pixel/mm",
6674 (top_scale + bot_scale) / 2, fabs(top_scale - bot_scale));
6675 cpl_msg_info(cpl_func,
"Median rotation: %f +/- %f degrees",
6676 (top_angle + bot_angle) / 2, fabs(top_angle - bot_angle));
6677 if (fabs(top_angle) < fabs(bot_angle))
6678 angle = fabs(top_angle);
6680 angle = fabs(bot_angle);
6681 found_slits_top = cpl_matrix_get_ncol(top_mdata);
6682 found_slits_bot = cpl_matrix_get_ncol(bot_mdata);
6684 else if (top_ident) {
6685 cpl_msg_info(cpl_func,
"Median platescale: %f pixel/mm", top_scale);
6686 cpl_msg_info(cpl_func,
"Median rotation: %f degrees", top_angle);
6687 angle = fabs(top_angle);
6688 found_slits_top = cpl_matrix_get_ncol(top_mdata);
6691 cpl_msg_info(cpl_func,
"Median platescale: %f pixel/mm", bot_scale);
6692 cpl_msg_info(cpl_func,
"Median rotation: %f degrees", bot_angle);
6693 angle = fabs(bot_angle);
6694 found_slits_bot = cpl_matrix_get_ncol(bot_mdata);
6697 cpl_array_delete(top_ident);
6698 cpl_array_delete(bot_ident);
6701 cpl_msg_warning(cpl_func,
"Uncertain pattern matching: the rotation "
6702 "angle is expected to be around zero. This match is "
6703 "rejected: the process will continue using the %d "
6704 "detected CCD slits positions", nslits);
6708 found_slits = found_slits_top;
6709 if (found_slits < found_slits_bot)
6710 found_slits = found_slits_bot;
6712 if (found_slits < 4) {
6713 cpl_msg_warning(cpl_func,
6714 "Too few safely identified slits: %d out of %d "
6715 "candidates (%d expected). Process will continue "
6716 "using the detected CCD slits positions", found_slits,
6717 nslits, nmaskslits);
6721 cpl_msg_info(cpl_func,
"Preliminary identified slits: %d out of %d "
6722 "candidates\n(%d expected)", found_slits, nslits,
6725 if (found_slits_top < 4)
6726 found_slits_top = 0;
6728 if (found_slits_bot < 4)
6729 found_slits_bot = 0;
6737 for (i = 0; i < 2; i++) {
6738 cpl_size mindeg2d[] = {0, 0};
6739 cpl_size maxdeg2d[2];
6740 cpl_vector * fitresidual;
6742 found_slits = found_slits_top;
6744 mpattern = top_mpattern;
6747 found_slits = found_slits_bot;
6749 mpattern = bot_mpattern;
6752 if (found_slits == 0)
6754 else if (found_slits < 10)
6755 maxdeg2d[0] = maxdeg2d[1] = 1;
6757 maxdeg2d[0] = maxdeg2d[1] = 2;
6759 xpos = cpl_vector_wrap(found_slits,
6760 cpl_matrix_get_data(mdata) );
6761 ypos = cpl_vector_wrap(found_slits,
6762 cpl_matrix_get_data(mdata) + found_slits);
6763 xmpos = cpl_vector_wrap(found_slits,
6764 cpl_matrix_get_data(mpattern) );
6765 ympos = cpl_vector_wrap(found_slits,
6766 cpl_matrix_get_data(mpattern) + found_slits);
6767 mpos = cpl_bivector_wrap_vectors(xmpos, ympos);
6768 fitresidual = cpl_vector_new(cpl_vector_get_size(xpos));
6769 xpoly = cpl_polynomial_new(2);
6770 cpl_polynomial_fit(xpoly, mpattern, NULL, xpos, NULL, CPL_FALSE, mindeg2d, maxdeg2d);
6771 cpl_vector_fill_polynomial_fit_residual(fitresidual, xpos, NULL, xpoly, mpattern, NULL);
6772 xmse = cpl_vector_product(fitresidual, fitresidual)
6773 / cpl_vector_get_size(fitresidual);
6774 ypoly = cpl_polynomial_new(2);
6775 cpl_polynomial_fit(ypoly, mpattern, NULL, ypos, NULL, CPL_FALSE, mindeg2d, maxdeg2d);
6776 cpl_vector_fill_polynomial_fit_residual(fitresidual, ypos, NULL, ypoly, mpattern, NULL);
6777 ymse = cpl_vector_product(fitresidual, fitresidual)
6778 / cpl_vector_get_size(fitresidual);
6780 cpl_bivector_unwrap_vectors(mpos);
6781 cpl_vector_unwrap(xpos);
6782 cpl_vector_unwrap(ypos);
6783 cpl_vector_unwrap(xmpos);
6784 cpl_vector_unwrap(ympos);
6785 cpl_matrix_delete(mdata);
6786 cpl_matrix_delete(mpattern);
6787 cpl_vector_delete(fitresidual);
6792 top_xrms = sqrt(xmse*2*maxdeg2d[0]/(found_slits - 1));
6793 top_yrms = sqrt(ymse*2*maxdeg2d[0]/(found_slits - 1));
6798 bot_xrms = sqrt(xmse*2*maxdeg2d[0]/(found_slits - 1));
6799 bot_yrms = sqrt(ymse*2*maxdeg2d[0]/(found_slits - 1));
6803 if (top_xpoly && bot_xpoly) {
6804 if (top_xrms < bot_xrms) {
6807 cpl_polynomial_delete(bot_xpoly);
6812 cpl_polynomial_delete(top_xpoly);
6815 else if (top_xpoly) {
6824 if (top_ypoly && bot_ypoly) {
6825 if (top_yrms < bot_yrms) {
6828 cpl_polynomial_delete(bot_ypoly);
6833 cpl_polynomial_delete(top_ypoly);
6836 else if (top_ypoly) {
6845 if (xpoly == NULL || ypoly == NULL) {
6846 cpl_msg_warning(cpl_func,
"Fit failure: the accuracy of the "
6847 "identified slits positions cannot be improved.");
6848 cpl_polynomial_delete(xpoly);
6849 cpl_polynomial_delete(ypoly);
6854 cpl_msg_info(cpl_func,
6855 "Fit successful: X rms = %.3g, Y rms = %.3g (pixel)",
6859 write_global_distortion(global, 0, xpoly);
6860 write_global_distortion(global, 7, ypoly);
6868 positions = cpl_table_duplicate(maskslits);
6869 cpl_table_duplicate_column(positions,
"xmtop", positions,
"xtop");
6870 cpl_table_duplicate_column(positions,
"ymtop", positions,
"ytop");
6871 cpl_table_duplicate_column(positions,
"xmbottom", positions,
"xbottom");
6872 cpl_table_duplicate_column(positions,
"ymbottom", positions,
"ybottom");
6874 point = cpl_vector_new(2);
6875 dpoint = cpl_vector_get_data(point);
6877 for (i = 0; i < nmaskslits; i++) {
6881 dpoint[0] = cpl_table_get_double(positions,
"xmtop", i, NULL);
6882 dpoint[1] = cpl_table_get_double(positions,
"ymtop", i, NULL);
6883 position_x = cpl_polynomial_eval(xpoly, point);
6890 cpl_table_set_double(positions,
"xtop", i, position_x);
6891 position_y = cpl_polynomial_eval(ypoly, point);
6892 cpl_table_set_double(positions,
"ytop", i, position_y);
6893 dpoint[0] = cpl_table_get_double(positions,
"xmbottom", i, NULL);
6894 dpoint[1] = cpl_table_get_double(positions,
"ymbottom", i, NULL);
6895 position_x = cpl_polynomial_eval(xpoly, point);
6896 cpl_table_set_double(positions,
"xbottom", i, position_x);
6897 position_y = cpl_polynomial_eval(ypoly, point);
6898 cpl_table_set_double(positions,
"ybottom", i, position_y);
6907 cpl_vector_delete(point);
6908 cpl_polynomial_delete(xpoly);
6909 cpl_polynomial_delete(ypoly);
6911 cpl_table_erase_column(positions,
"xmtop");
6912 cpl_table_erase_column(positions,
"ymtop");
6913 cpl_table_erase_column(positions,
"xmbottom");
6914 cpl_table_erase_column(positions,
"ymbottom");
6916 if (mos_multiplex >= 0) {
6918 cpl_sprintf(
"in the CCD section between %d and %d pixel",
6919 mos_multiplex * mos_region_size,
6920 (mos_multiplex + 1) * mos_region_size);
6923 if (nmaskslits > nslits)
6924 cpl_msg_info(cpl_func,
6925 "Finally identified slits: %d out of %d expected %s\n"
6926 "(%d recovered)", nmaskslits, nmaskslits, msg_multiplex,
6927 nmaskslits - nslits);
6928 else if (nmaskslits < nslits)
6929 cpl_msg_info(cpl_func,
6930 "Finally identified slits: %d out of %d expected %s\n"
6931 "(%d rejected)", nmaskslits, nmaskslits, msg_multiplex,
6932 nslits - nmaskslits);
6934 cpl_msg_info(cpl_func,
6935 "Finally identified slits: %d out of %d expected %s",
6936 nmaskslits, nmaskslits, msg_multiplex);
6938 if (mos_multiplex >= 0) {
6939 cpl_free(msg_multiplex);
6947 cpl_table *mos_identify_slits_fast(cpl_table *slits, cpl_table *maskslits,
6950 const char *func =
"mos_identify_slits_fast";
6952 cpl_propertylist *sort_col;
6953 cpl_table *positions;
6962 cpl_polynomial *xpoly = NULL;
6963 cpl_polynomial *ypoly = NULL;
6964 cpl_error_code error;
6970 double dist1, dist2, dist3, dist, mindist;
6971 double scale, minscale, maxscale;
6972 double angle, minangle, maxangle;
6999 double sradius = 0.01;
7002 double pi = 3.14159265358979323846;
7007 cpl_msg_error(func,
"CCD slits table validation: %s",
7008 cpl_error_get_message());
7009 cpl_error_set(func, error);
7015 cpl_msg_error(func,
"Mask slits table validation: %s",
7016 cpl_error_get_message());
7017 cpl_error_set(func, error);
7021 if (1 != cpl_table_has_column(maskslits,
"slit_id")) {
7022 cpl_msg_error(func,
"Missing slits identifiers");
7023 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
7027 if (CPL_TYPE_INT != cpl_table_get_column_type(maskslits,
"slit_id")) {
7028 cpl_msg_error(func,
"Wrong type used for slits identifiers");
7029 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
7033 nslits = cpl_table_get_nrow(slits);
7034 nmaskslits = cpl_table_get_nrow(maskslits);
7036 if (nslits == 0 || nmaskslits == 0) {
7037 cpl_msg_error(func,
"Empty slits table");
7038 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
7048 if (cpl_table_has_column(slits,
"xcenter"))
7049 cpl_table_erase_column(slits,
"xcenter");
7051 if (cpl_table_has_column(slits,
"ycenter"))
7052 cpl_table_erase_column(slits,
"ycenter");
7054 if (cpl_table_has_column(maskslits,
"xcenter"))
7055 cpl_table_erase_column(maskslits,
"xcenter");
7057 if (cpl_table_has_column(maskslits,
"ycenter"))
7058 cpl_table_erase_column(maskslits,
"ycenter");
7060 cpl_table_duplicate_column(slits,
"xcenter", slits,
"xtop");
7061 cpl_table_add_columns(slits,
"xcenter",
"xbottom");
7062 cpl_table_divide_scalar(slits,
"xcenter", 2.0);
7063 cpl_table_duplicate_column(slits,
"ycenter", slits,
"ytop");
7064 cpl_table_add_columns(slits,
"ycenter",
"ybottom");
7065 cpl_table_divide_scalar(slits,
"ycenter", 2.0);
7067 cpl_table_duplicate_column(maskslits,
"xcenter", maskslits,
"xtop");
7068 cpl_table_add_columns(maskslits,
"xcenter",
"xbottom");
7069 cpl_table_divide_scalar(maskslits,
"xcenter", 2.0);
7070 cpl_table_duplicate_column(maskslits,
"ycenter", maskslits,
"ytop");
7071 cpl_table_add_columns(maskslits,
"ycenter",
"ybottom");
7072 cpl_table_divide_scalar(maskslits,
"ycenter", 2.0);
7079 sort_col = cpl_propertylist_new();
7080 cpl_propertylist_append_bool(sort_col,
"ycenter", 1);
7081 cpl_table_sort(slits, sort_col);
7082 cpl_table_sort(maskslits, sort_col);
7083 cpl_propertylist_delete(sort_col);
7090 if (nslits < 3 && nmaskslits > nslits) {
7100 cpl_msg_warning(func,
"Cannot match the found CCD slit with the "
7101 "%d mask slits: process will continue using the "
7102 "detected CCD slit position", nmaskslits);
7104 cpl_msg_warning(func,
"Cannot match the %d found CCD slits with "
7105 "the %d mask slits: process will continue using "
7106 "the detected CCD slits positions", nslits,
7111 if (nslits <= 3 && nslits == nmaskslits) {
7113 cpl_msg_warning(func,
"Too few slits (%d) on mask and CCD", nslits);
7114 cpl_msg_warning(func,
"Their detected positions are left unchanged");
7125 positions = cpl_table_duplicate(slits);
7126 cpl_table_erase_column(slits,
"xcenter");
7127 cpl_table_erase_column(slits,
"ycenter");
7128 cpl_table_duplicate_column(positions,
"xmtop", maskslits,
"xtop");
7129 cpl_table_duplicate_column(positions,
"ymtop", maskslits,
"ytop");
7130 cpl_table_duplicate_column(positions,
"xmbottom", maskslits,
"xbottom");
7131 cpl_table_duplicate_column(positions,
"ymbottom", maskslits,
"ybottom");
7132 cpl_table_duplicate_column(positions,
"xmcenter", maskslits,
"xcenter");
7133 cpl_table_duplicate_column(positions,
"ymcenter", maskslits,
"ycenter");
7134 cpl_table_duplicate_column(positions,
"slit_id", maskslits,
"slit_id");
7135 cpl_table_erase_column(maskslits,
"xcenter");
7136 cpl_table_erase_column(maskslits,
"ycenter");
7139 xcenter = cpl_table_get_data_double(positions,
"xcenter");
7140 ycenter = cpl_table_get_data_double(positions,
"ycenter");
7141 xmcenter = cpl_table_get_data_double(positions,
"xmcenter");
7142 ymcenter = cpl_table_get_data_double(positions,
"ymcenter");
7144 dist1 = (xcenter[0] - xcenter[1])*(xcenter[0] - xcenter[1])
7145 + (ycenter[0] - ycenter[1])*(ycenter[0] - ycenter[1]);
7146 dist2 = (xmcenter[0] - xmcenter[1])*(xmcenter[0] - xmcenter[1])
7147 + (ymcenter[0] - ymcenter[1])*(ymcenter[0] - ymcenter[1]);
7148 scale = sqrt(dist1/dist2);
7151 dist1 = (xcenter[1] - xcenter[2])*(xcenter[1] - xcenter[2])
7152 + (ycenter[1] - ycenter[2])*(ycenter[1] - ycenter[2]);
7153 dist2 = (xmcenter[1] - xmcenter[2])*(xmcenter[1] - xmcenter[2])
7154 + (ymcenter[1] - ymcenter[2])*(ymcenter[1] - ymcenter[2]);
7155 scale += sqrt(dist1/dist2);
7159 cpl_msg_info(func,
"Platescale: %f pixel/mm", scale);
7165 if (nmaskslits < 3 && nslits > nmaskslits) {
7173 cpl_msg_warning(func,
"Cannot match the %d found CCD slits with "
7174 "the %d mask slits: process will continue using "
7175 "the detected CCD slits positions", nslits,
7207 if (cpl_table_has_column(slits,
"xpseudo"))
7208 cpl_table_erase_column(slits,
"xpseudo");
7210 if (cpl_table_has_column(slits,
"ypseudo"))
7211 cpl_table_erase_column(slits,
"ypseudo");
7213 if (cpl_table_has_column(maskslits,
"xpseudo"))
7214 cpl_table_erase_column(maskslits,
"xpseudo");
7216 if (cpl_table_has_column(maskslits,
"ypseudo"))
7217 cpl_table_erase_column(maskslits,
"ypseudo");
7219 cpl_table_duplicate_column(slits,
"xpseudo", slits,
"xcenter");
7220 cpl_table_duplicate_column(slits,
"ypseudo", slits,
"ycenter");
7222 xcenter = cpl_table_get_data_double(slits,
"xcenter");
7223 ycenter = cpl_table_get_data_double(slits,
"ycenter");
7224 xpseudo = cpl_table_get_data_double(slits,
"xpseudo");
7225 ypseudo = cpl_table_get_data_double(slits,
"ypseudo");
7227 for (i = 1; i < nslits - 1; i++) {
7228 dist1 = (xcenter[i-1] - xcenter[i]) * (xcenter[i-1] - xcenter[i])
7229 + (ycenter[i-1] - ycenter[i]) * (ycenter[i-1] - ycenter[i]);
7230 dist2 = (xcenter[i-1] - xcenter[i+1]) * (xcenter[i-1] - xcenter[i+1])
7231 + (ycenter[i-1] - ycenter[i+1]) * (ycenter[i-1] - ycenter[i+1]);
7232 dist3 = (xcenter[i] - xcenter[i+1]) * (xcenter[i] - xcenter[i+1])
7233 + (ycenter[i] - ycenter[i+1]) * (ycenter[i] - ycenter[i+1]);
7234 xpseudo[i] = sqrt(dist1/dist2);
7235 ypseudo[i] = sqrt(dist3/dist2);
7238 cpl_table_set_invalid(slits,
"xpseudo", 0);
7239 cpl_table_set_invalid(slits,
"xpseudo", nslits-1);
7240 cpl_table_set_invalid(slits,
"ypseudo", 0);
7241 cpl_table_set_invalid(slits,
"ypseudo", nslits-1);
7243 cpl_table_duplicate_column(maskslits,
"xpseudo", maskslits,
"xcenter");
7244 cpl_table_duplicate_column(maskslits,
"ypseudo", maskslits,
"ycenter");
7246 xcenter = cpl_table_get_data_double(maskslits,
"xcenter");
7247 ycenter = cpl_table_get_data_double(maskslits,
"ycenter");
7248 xmpseudo = cpl_table_get_data_double(maskslits,
"xpseudo");
7249 ympseudo = cpl_table_get_data_double(maskslits,
"ypseudo");
7251 for (i = 1; i < nmaskslits - 1; i++) {
7252 dist1 = (xcenter[i-1] - xcenter[i])*(xcenter[i-1] - xcenter[i])
7253 + (ycenter[i-1] - ycenter[i])*(ycenter[i-1] - ycenter[i]);
7254 dist2 = (xcenter[i-1] - xcenter[i+1])*(xcenter[i-1] - xcenter[i+1])
7255 + (ycenter[i-1] - ycenter[i+1])*(ycenter[i-1] - ycenter[i+1]);
7256 dist3 = (xcenter[i] - xcenter[i+1])*(xcenter[i] - xcenter[i+1])
7257 + (ycenter[i] - ycenter[i+1])*(ycenter[i] - ycenter[i+1]);
7258 xmpseudo[i] = sqrt(dist1/dist2);
7259 ympseudo[i] = sqrt(dist3/dist2);
7262 cpl_table_set_invalid(maskslits,
"xpseudo", 0);
7263 cpl_table_set_invalid(maskslits,
"xpseudo", nmaskslits-1);
7264 cpl_table_set_invalid(maskslits,
"ypseudo", 0);
7265 cpl_table_set_invalid(maskslits,
"ypseudo", nmaskslits-1);
7277 if (cpl_table_has_column(slits,
"slit_id"))
7278 cpl_table_erase_column(slits,
"slit_id");
7279 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
7280 slit_id = cpl_table_get_data_int(maskslits,
"slit_id");
7282 for (i = 1; i < nmaskslits - 1; i++) {
7284 mindist = (xmpseudo[i] - xpseudo[1]) * (xmpseudo[i] - xpseudo[1])
7285 + (ympseudo[i] - ypseudo[1]) * (ympseudo[i] - ypseudo[1]);
7287 if (mindist < sradius*sradius)
7289 for (j = 2; j < nslits - 1; j++) {
7290 dist = (xmpseudo[i] - xpseudo[j]) * (xmpseudo[i] - xpseudo[j])
7291 + (ympseudo[i] - ypseudo[j]) * (ympseudo[i] - ypseudo[j]);
7292 if (dist < sradius*sradius)
7296 if (mindist > dist) {
7302 mindist = sqrt(mindist);
7304 if (mindist < sradius && in_sradius == 1) {
7305 cpl_table_set_int(slits,
"slit_id", minpos-1, slit_id[i-1]);
7306 cpl_table_set_int(slits,
"slit_id", minpos, slit_id[i]);
7307 cpl_table_set_int(slits,
"slit_id", minpos+1, slit_id[i+1]);
7317 found_slits = nslits - cpl_table_count_invalid(slits,
"slit_id");
7319 if (found_slits < 3) {
7320 cpl_msg_warning(func,
"Too few preliminarily identified slits: "
7321 "%d out of %d", found_slits, nslits);
7322 if (nslits == nmaskslits) {
7323 cpl_msg_warning(func,
"(this is not an error, it could be caused "
7324 "by a mask with regularly located slits)");
7325 cpl_msg_warning(func,
"The detected slits positions are left "
7335 cpl_table_erase_column(slits,
"slit_id");
7336 cpl_table_erase_column(slits,
"xpseudo");
7337 cpl_table_erase_column(slits,
"ypseudo");
7338 positions = cpl_table_duplicate(slits);
7339 cpl_table_erase_column(slits,
"xcenter");
7340 cpl_table_erase_column(slits,
"ycenter");
7342 cpl_table_erase_column(maskslits,
"xpseudo");
7343 cpl_table_erase_column(maskslits,
"ypseudo");
7344 cpl_table_duplicate_column(positions,
"xmtop",
7346 cpl_table_duplicate_column(positions,
"ymtop",
7348 cpl_table_duplicate_column(positions,
"xmbottom",
7349 maskslits,
"xbottom");
7350 cpl_table_duplicate_column(positions,
"ymbottom",
7351 maskslits,
"ybottom");
7352 cpl_table_duplicate_column(positions,
"xmcenter",
7353 maskslits,
"xcenter");
7354 cpl_table_duplicate_column(positions,
"ymcenter",
7355 maskslits,
"ycenter");
7356 cpl_table_duplicate_column(positions,
"slit_id",
7357 maskslits,
"slit_id");
7358 cpl_table_erase_column(maskslits,
"xcenter");
7359 cpl_table_erase_column(maskslits,
"ycenter");
7363 cpl_table_erase_column(slits,
"slit_id");
7364 cpl_table_erase_column(slits,
"xpseudo");
7365 cpl_table_erase_column(slits,
"ypseudo");
7366 positions = cpl_table_duplicate(slits);
7367 cpl_table_erase_column(slits,
"xcenter");
7368 cpl_table_erase_column(slits,
"ycenter");
7369 cpl_msg_warning(func,
"(the failure could be caused "
7370 "by a mask with regularly located slits)");
7375 cpl_msg_info(func,
"Preliminarily identified slits: %d out of %d "
7376 "candidates (%d expected)", found_slits, nslits,
7387 positions = cpl_table_new(found_slits);
7388 cpl_table_new_column(positions,
"slit_id", CPL_TYPE_INT);
7389 cpl_table_new_column(positions,
"xtop", CPL_TYPE_DOUBLE);
7390 cpl_table_new_column(positions,
"ytop", CPL_TYPE_DOUBLE);
7391 cpl_table_new_column(positions,
"xbottom", CPL_TYPE_DOUBLE);
7392 cpl_table_new_column(positions,
"ybottom", CPL_TYPE_DOUBLE);
7393 cpl_table_new_column(positions,
"xcenter", CPL_TYPE_DOUBLE);
7394 cpl_table_new_column(positions,
"ycenter", CPL_TYPE_DOUBLE);
7395 cpl_table_new_column(positions,
"xmtop", CPL_TYPE_DOUBLE);
7396 cpl_table_new_column(positions,
"ymtop", CPL_TYPE_DOUBLE);
7397 cpl_table_new_column(positions,
"xmbottom", CPL_TYPE_DOUBLE);
7398 cpl_table_new_column(positions,
"ymbottom", CPL_TYPE_DOUBLE);
7399 cpl_table_new_column(positions,
"xmcenter", CPL_TYPE_DOUBLE);
7400 cpl_table_new_column(positions,
"ymcenter", CPL_TYPE_DOUBLE);
7401 cpl_table_new_column(positions,
"good", CPL_TYPE_INT);
7402 cpl_table_fill_column_window_int(positions,
"good", 0, found_slits, 0);
7404 slit_id = cpl_table_get_data_int (slits,
"slit_id");
7405 xtop = cpl_table_get_data_double(slits,
"xtop");
7406 ytop = cpl_table_get_data_double(slits,
"ytop");
7407 xbottom = cpl_table_get_data_double(slits,
"xbottom");
7408 ybottom = cpl_table_get_data_double(slits,
"ybottom");
7409 xcenter = cpl_table_get_data_double(slits,
"xcenter");
7410 ycenter = cpl_table_get_data_double(slits,
"ycenter");
7412 mslit_id = cpl_table_get_data_int (maskslits,
"slit_id");
7413 xmtop = cpl_table_get_data_double(maskslits,
"xtop");
7414 ymtop = cpl_table_get_data_double(maskslits,
"ytop");
7415 xmbottom = cpl_table_get_data_double(maskslits,
"xbottom");
7416 ymbottom = cpl_table_get_data_double(maskslits,
"ybottom");
7417 xmcenter = cpl_table_get_data_double(maskslits,
"xcenter");
7418 ymcenter = cpl_table_get_data_double(maskslits,
"ycenter");
7428 cpl_table_fill_invalid_int(slits,
"slit_id", 0);
7429 for (i = 0; i < nmaskslits; i++) {
7430 for (j = 0; j < nslits; j++) {
7431 if (slit_id[j] == 0)
7433 if (mslit_id[i] == slit_id[j]) {
7434 cpl_table_set_int (positions,
"slit_id", k, slit_id[j]);
7436 cpl_table_set_double(positions,
"xtop", k, xtop[j]);
7437 cpl_table_set_double(positions,
"ytop", k, ytop[j]);
7438 cpl_table_set_double(positions,
"xbottom", k, xbottom[j]);
7439 cpl_table_set_double(positions,
"ybottom", k, ybottom[j]);
7440 cpl_table_set_double(positions,
"xcenter", k, xcenter[j]);
7441 cpl_table_set_double(positions,
"ycenter", k, ycenter[j]);
7443 cpl_table_set_double(positions,
"xmtop", k, xmtop[i]);
7444 cpl_table_set_double(positions,
"ymtop", k, ymtop[i]);
7445 cpl_table_set_double(positions,
"xmbottom", k, xmbottom[i]);
7446 cpl_table_set_double(positions,
"ymbottom", k, ymbottom[i]);
7447 cpl_table_set_double(positions,
"xmcenter", k, xmcenter[i]);
7448 cpl_table_set_double(positions,
"ymcenter", k, ymcenter[i]);
7459 cpl_table_erase_column(slits,
"slit_id");
7460 cpl_table_erase_column(slits,
"xpseudo");
7461 cpl_table_erase_column(slits,
"ypseudo");
7462 cpl_table_erase_column(slits,
"xcenter");
7463 cpl_table_erase_column(slits,
"ycenter");
7464 cpl_table_erase_column(maskslits,
"xpseudo");
7465 cpl_table_erase_column(maskslits,
"ypseudo");
7466 cpl_table_erase_column(maskslits,
"xcenter");
7467 cpl_table_erase_column(maskslits,
"ycenter");
7477 ytop = cpl_table_get_data_double(positions,
"ytop");
7478 ybottom = cpl_table_get_data_double(positions,
"ybottom");
7479 xcenter = cpl_table_get_data_double(positions,
"xcenter");
7480 ycenter = cpl_table_get_data_double(positions,
"ycenter");
7481 xmcenter = cpl_table_get_data_double(positions,
"xmcenter");
7482 ymcenter = cpl_table_get_data_double(positions,
"ymcenter");
7484 scales = cpl_vector_new(found_slits - 1);
7485 dscale = cpl_vector_get_data(scales);
7486 angles = cpl_vector_new(found_slits - 1);
7487 dangle = cpl_vector_get_data(angles);
7489 for (i = 1; i < found_slits; i++) {
7490 dist1 = (xcenter[i-1] - xcenter[i]) * (xcenter[i-1] - xcenter[i])
7491 + (ycenter[i-1] - ycenter[i]) * (ycenter[i-1] - ycenter[i]);
7492 dist2 = (xmcenter[i-1] - xmcenter[i]) * (xmcenter[i-1] - xmcenter[i])
7493 + (ymcenter[i-1] - ymcenter[i]) * (ymcenter[i-1] - ymcenter[i]);
7494 dscale[i-1] = sqrt(dist1/dist2);
7495 dangle[i-1] = atan2(ycenter[i-1] - ycenter[i],
7496 xcenter[i-1] - xcenter[i])
7497 - atan2(ymcenter[i-1] - ymcenter[i],
7498 xmcenter[i-1] - xmcenter[i]);
7503 minscale = cpl_vector_get_min(scales);
7504 scale = cpl_vector_get_median_const(scales);
7505 maxscale = cpl_vector_get_max(scales);
7507 minangle = cpl_vector_get_min(angles);
7508 angle = cpl_vector_get_median_const(angles);
7509 maxangle = cpl_vector_get_max(angles);
7511 cpl_msg_info(func,
"Median platescale: %f pixel/mm", scale);
7512 cpl_msg_info(func,
"Minmax platescale: %f, %f pixel/mm",
7513 minscale, maxscale);
7515 cpl_msg_info(func,
"Median rotation: %f degrees", angle);
7516 cpl_msg_info(func,
"Minmax rotation: %f, %f degrees",
7517 minangle, maxangle);
7519 good = cpl_table_get_data_int(positions,
"good");
7521 good[0] = good[found_slits - 1] = 1;
7522 for (i = 1; i < found_slits; i++) {
7523 if (fabs((dscale[i-1] - scale)/scale) < 0.10
7524 && fabs(dangle[i-1] - angle) < 2) {
7530 for (i = 0; i < found_slits; i++) {
7570 cpl_vector_delete(scales);
7571 cpl_vector_delete(angles);
7573 cpl_table_and_selected_int(positions,
"good", CPL_EQUAL_TO, 0);
7574 cpl_table_erase_selected(positions);
7575 cpl_table_erase_column(positions,
"good");
7576 found_slits = cpl_table_get_nrow(positions);
7578 if (found_slits < 4) {
7586 cpl_msg_warning(func,
"Too few safely identified slits: %d out of %d "
7587 "candidates (%d expected). Process will continue "
7588 "using the detected CCD slits positions", found_slits,
7589 nslits, nmaskslits);
7590 cpl_table_delete(positions);
7594 cpl_msg_info(func,
"Safely identified slits: %d out of %d "
7595 "candidates\n(%d expected)", found_slits, nslits,
7606 xpos = cpl_vector_wrap(found_slits,
7607 cpl_table_get_data_double(positions,
"xcenter"));
7608 ypos = cpl_vector_wrap(found_slits,
7609 cpl_table_get_data_double(positions,
"ycenter"));
7610 xmpos = cpl_vector_wrap(found_slits,
7611 cpl_table_get_data_double(positions,
"xmcenter"));
7612 ympos = cpl_vector_wrap(found_slits,
7613 cpl_table_get_data_double(positions,
"ymcenter"));
7614 mpos = cpl_bivector_wrap_vectors(xmpos, ympos);
7616 if (found_slits < 10)
7621 xpoly = cpl_polynomial_fit_2d_create(mpos, xpos, degree, &xmse);
7623 ypoly = cpl_polynomial_fit_2d_create(mpos, ypos, degree, &ymse);
7624 cpl_bivector_unwrap_vectors(mpos);
7625 cpl_vector_unwrap(xpos);
7626 cpl_vector_unwrap(ypos);
7627 cpl_vector_unwrap(xmpos);
7628 cpl_vector_unwrap(ympos);
7629 if (ypoly == NULL) {
7630 if (found_slits == nmaskslits) {
7631 cpl_msg_warning(func,
"Fit failure: the accuracy of the "
7632 "identified slits positions is not improved.");
7643 cpl_msg_info(func,
"Fit failure: not all slits have been "
7644 "identified. Process will continue using "
7645 "the detected CCD slits positions");
7648 cpl_polynomial_delete(xpoly);
7652 cpl_msg_info(func,
"Fit successful: X rms = %.3g, Y rms = %.3g (pixel)",
7653 sqrt(xmse), sqrt(ymse));
7656 write_global_distortion(global, 0, xpoly);
7657 write_global_distortion(global, 7, ypoly);
7665 cpl_table_delete(positions);
7667 positions = cpl_table_duplicate(maskslits);
7668 cpl_table_duplicate_column(positions,
"xmtop", positions,
"xtop");
7669 cpl_table_duplicate_column(positions,
"ymtop", positions,
"ytop");
7670 cpl_table_duplicate_column(positions,
"xmbottom", positions,
"xbottom");
7671 cpl_table_duplicate_column(positions,
"ymbottom", positions,
"ybottom");
7673 point = cpl_vector_new(2);
7674 dpoint = cpl_vector_get_data(point);
7676 for (i = 0; i < nmaskslits; i++) {
7677 dpoint[0] = cpl_table_get_double(positions,
"xmtop", i, NULL);
7678 dpoint[1] = cpl_table_get_double(positions,
"ymtop", i, NULL);
7679 cpl_table_set_double(positions,
"xtop", i,
7680 cpl_polynomial_eval(xpoly, point));
7681 cpl_table_set_double(positions,
"ytop", i,
7682 cpl_polynomial_eval(ypoly, point));
7683 dpoint[0] = cpl_table_get_double(positions,
"xmbottom", i, NULL);
7684 dpoint[1] = cpl_table_get_double(positions,
"ymbottom", i, NULL);
7685 cpl_table_set_double(positions,
"xbottom", i,
7686 cpl_polynomial_eval(xpoly, point));
7687 cpl_table_set_double(positions,
"ybottom", i,
7688 cpl_polynomial_eval(ypoly, point));
7691 cpl_vector_delete(point);
7692 cpl_polynomial_delete(xpoly);
7693 cpl_polynomial_delete(ypoly);
7695 cpl_table_erase_column(positions,
"xmtop");
7696 cpl_table_erase_column(positions,
"ymtop");
7697 cpl_table_erase_column(positions,
"xmbottom");
7698 cpl_table_erase_column(positions,
"ymbottom");
7700 if (nmaskslits > nslits)
7701 cpl_msg_info(func,
"Finally identified slits: %d out of %d expected\n"
7702 "(%d recovered)", nmaskslits, nmaskslits, nmaskslits - nslits);
7703 else if (nmaskslits < nslits)
7704 cpl_msg_info(func,
"Finally identified slits: %d out of %d expected\n"
7705 "(%d rejected)", nmaskslits, nmaskslits, nslits - nmaskslits);
7707 cpl_msg_info(func,
"Finally identified slits: %d out of %d expected",
7708 nmaskslits, nmaskslits);
7756 double blue,
double red,
double dispersion)
7759 const char *func =
"mos_trace_flat";
7761 cpl_image *gradient;
7762 cpl_image *sgradient;
7779 double start_y, prev_y;
7788 int pixel_above, pixel_below;
7790 char trace_id[MAX_COLNAME];
7795 if (flat == NULL || slits == NULL) {
7796 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
7800 if (dispersion <= 0.0) {
7801 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
7805 if (red - blue < dispersion) {
7806 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
7815 nslits = cpl_table_get_nrow(slits);
7816 if (1 != cpl_table_has_column(slits,
"slit_id")) {
7817 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
7818 for (i = 0; i < nslits; i++)
7819 cpl_table_set_int(slits,
"slit_id", i, -(i+1));
7822 slit_id = cpl_table_get_data_int(slits,
"slit_id");
7824 nx = cpl_image_get_size_x(flat);
7825 ny = cpl_image_get_size_y(flat);
7828 gradient = cpl_image_duplicate(flat);
7829 dgradient = cpl_image_get_data_float(gradient);
7831 for (i = 0; i < ny - 1; i++) {
7833 for (j = 0; j < nx; j++) {
7835 dgradient[l] = fabs(dgradient[l] - dgradient[l + nx]);
7840 for (j = 0; j < nx; j++)
7841 dgradient[npix - j] = 0.0;
7843 cpl_image_turn(gradient, -1);
7844 nx = cpl_image_get_size_x(gradient);
7845 ny = cpl_image_get_size_y(gradient);
7846 sgradient = mos_image_vertical_median_filter(gradient,
7847 filtbox, 0, ny, 0, step);
7848 cpl_image_delete(gradient);
7855 dgradient = cpl_image_get_data_float(sgradient);
7857 for (i = 1; i <= ny; i += step) {
7858 row = cpl_vector_new_from_image_row(sgradient, i);
7859 srow = cpl_vector_filter_median_create(row, filtbox);
7860 cpl_vector_subtract(row, srow);
7861 cpl_vector_delete(srow);
7862 g = dgradient + (i-1)*nx;
7863 r = cpl_vector_get_data(row);
7864 for (j = 0; j < nx; j++)
7866 cpl_vector_delete(row);
7876 xtop = cpl_table_get_data_double(slits,
"xtop");
7877 ytop = cpl_table_get_data_double(slits,
"ytop");
7878 xbottom = cpl_table_get_data_double(slits,
"xbottom");
7879 ybottom = cpl_table_get_data_double(slits,
"ybottom");
7887 peaks = cpl_calloc(ny,
sizeof(cpl_vector *));
7889 for (i = 0; i < ny; i += step) {
7890 g = dgradient + i*nx;
7896 cpl_vector_subtract_scalar(peaks[i], 0.5);
7900 cpl_image_delete(sgradient);
7926 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
7927 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
7934 nrows = (ny-1)/step + 1;
7935 traces = cpl_table_new(nrows);
7936 cpl_table_new_column(traces,
"x", CPL_TYPE_DOUBLE);
7937 cpl_table_set_column_unit(traces,
"x",
"pixel");
7938 for (i = 0, j = 0; i < ny; i += step, j++)
7939 cpl_table_set(traces,
"x", j, i);
7941 for (i = 0; i < nslits; i++) {
7962 peak = cpl_vector_get_data(peaks[pos]);
7963 npeaks = cpl_vector_get_size(peaks[pos]);
7965 min = fabs(peak[0] - xtop[i]);
7967 for (j = 1; j < npeaks; j++) {
7968 dist = fabs(peak[j] - xtop[i]);
7979 snprintf(trace_id, MAX_COLNAME,
"t%d", slit_id[i]);
7980 cpl_table_new_column(traces, trace_id, CPL_TYPE_DOUBLE);
7982 if (min > sradius || npeaks == 0) {
7983 cpl_msg_warning(func,
"Cannot find spectrum edge for "
7984 "top (or left) end of slit %d", slit_id[i]);
7996 cpl_table_set(traces, trace_id, pos/step, nx - peak[minpos]);
7997 start_y = peak[minpos];
8005 for (j = pos + step; j < ny; j += step) {
8006 if (j - pos > pixel_above)
8009 peak = cpl_vector_get_data(peaks[j]);
8010 npeaks = cpl_vector_get_size(peaks[j]);
8011 min = fabs(peak[0] - prev_y);
8013 for (k = 1; k < npeaks; k++) {
8014 dist = fabs(peak[k] - prev_y);
8020 if (min < tolerance) {
8021 cpl_table_set(traces, trace_id, j/step,
8023 prev_y = peak[minpos];
8034 for (j = pos - step; j >= 0; j -= step) {
8035 if (pos - j > pixel_below)
8038 peak = cpl_vector_get_data(peaks[j]);
8039 npeaks = cpl_vector_get_size(peaks[j]);
8040 min = fabs(peak[0] - prev_y);
8042 for (k = 1; k < npeaks; k++) {
8043 dist = fabs(peak[k] - prev_y);
8049 if (min < tolerance) {
8050 cpl_table_set(traces, trace_id, j/step,
8052 prev_y = peak[minpos];
8064 peak = cpl_vector_get_data(peaks[pos]);
8065 npeaks = cpl_vector_get_size(peaks[pos]);
8067 min = fabs(peak[0] - xbottom[i]);
8069 for (j = 1; j < npeaks; j++) {
8070 dist = fabs(peak[j] - xbottom[i]);
8081 snprintf(trace_id, MAX_COLNAME,
"b%d", slit_id[i]);
8082 cpl_table_new_column(traces, trace_id, CPL_TYPE_DOUBLE);
8084 if (min > sradius || npeaks == 0) {
8085 cpl_msg_warning(func,
"Cannot find spectrum edge for "
8086 "bottom (or right) end of slit %d", slit_id[i]);
8090 cpl_table_set(traces, trace_id, pos/step, nx - peak[minpos]);
8091 start_y = peak[minpos];
8099 for (j = pos + step; j < ny; j += step) {
8100 if (j - pos > pixel_above)
8103 peak = cpl_vector_get_data(peaks[j]);
8104 npeaks = cpl_vector_get_size(peaks[j]);
8105 min = fabs(peak[0] - prev_y);
8107 for (k = 1; k < npeaks; k++) {
8108 dist = fabs(peak[k] - prev_y);
8114 if (min < tolerance) {
8115 cpl_table_set(traces, trace_id, j/step,
8117 prev_y = peak[minpos];
8128 for (j = pos - step; j >= 0; j -= step) {
8129 if (pos - j > pixel_below)
8132 peak = cpl_vector_get_data(peaks[j]);
8133 npeaks = cpl_vector_get_size(peaks[j]);
8134 min = fabs(peak[0] - prev_y);
8136 for (k = 1; k < npeaks; k++) {
8137 dist = fabs(peak[k] - prev_y);
8143 if (min < tolerance) {
8144 cpl_table_set(traces, trace_id, j/step,
8146 prev_y = peak[minpos];
8154 for (i = 0; i < ny; i += step)
8155 cpl_vector_delete(peaks[i]);
8191 const char *func =
"mos_poly_trace";
8193 cpl_table *polytraces;
8197 cpl_polynomial *polytrace;
8198 char trace_id[MAX_COLNAME];
8199 char trace_res[MAX_COLNAME];
8200 char trace_mod[MAX_COLNAME];
8201 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8212 if (traces == NULL || slits == NULL) {
8213 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8218 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8222 nrows = cpl_table_get_nrow(traces);
8223 xdata = cpl_table_get_data_double(traces,
"x");
8224 nslits = cpl_table_get_nrow(slits);
8225 slit_id = cpl_table_get_data_int(slits,
"slit_id");
8227 polytraces = cpl_table_new(2*nslits);
8228 cpl_table_new_column(polytraces,
"slit_id", CPL_TYPE_INT);
8229 for (i = 0; i <= order; i++)
8230 cpl_table_new_column(polytraces, clab[i], CPL_TYPE_DOUBLE);
8232 for (i = 0; i < nslits; i++) {
8233 for (j = 0; j < 2; j++) {
8236 snprintf(trace_id, MAX_COLNAME,
"b%d", slit_id[i]);
8237 snprintf(trace_res, MAX_COLNAME,
"b%d_res", slit_id[i]);
8238 snprintf(trace_mod, MAX_COLNAME,
"b%d_mod", slit_id[i]);
8241 snprintf(trace_id, MAX_COLNAME,
"t%d", slit_id[i]);
8242 snprintf(trace_res, MAX_COLNAME,
"t%d_res", slit_id[i]);
8243 snprintf(trace_mod, MAX_COLNAME,
"t%d_mod", slit_id[i]);
8246 cpl_table_set_int(polytraces,
"slit_id", 2*i+j, slit_id[i]);
8253 dummy = cpl_table_new(nrows);
8254 cpl_table_duplicate_column(dummy,
"x", traces,
"x");
8255 cpl_table_duplicate_column(dummy, trace_id, traces, trace_id);
8256 npoints = nrows - cpl_table_count_invalid(dummy, trace_id);
8257 if (npoints < 2 * order) {
8258 cpl_table_delete(dummy);
8261 cpl_table_erase_invalid(dummy);
8262 x = cpl_vector_wrap(npoints,
8263 cpl_table_get_data_double(dummy,
"x"));
8264 trace = cpl_vector_wrap(npoints,
8265 cpl_table_get_data_double(dummy, trace_id));
8266 polytrace = cpl_polynomial_fit_1d_create(x, trace, order, NULL);
8267 cpl_vector_unwrap(x);
8268 cpl_vector_unwrap(trace);
8269 cpl_table_delete(dummy);
8278 if (fabs(cpl_polynomial_get_coeff(polytrace, &k)) > 1.E-4) {
8279 cpl_polynomial_delete(polytrace);
8280 cpl_table_new_column(traces, trace_mod, CPL_TYPE_DOUBLE);
8281 cpl_table_duplicate_column(traces, trace_res, traces,
8284 cpl_msg_warning(func,
"Exclude bad curvature solution "
8285 "for bottom (right) edge of slit %d", slit_id[i]);
8287 cpl_msg_warning(func,
"Exclude bad curvature solution "
8288 "for top (left) edge of slit %d", slit_id[i]);
8297 for (k = 0; k <= order; k++)
8298 cpl_table_set_double(polytraces, clab[k], 2*i+j,
8299 cpl_polynomial_get_coeff(polytrace, &k));
8305 cpl_table_new_column(traces, trace_mod, CPL_TYPE_DOUBLE);
8306 cpl_table_set_column_unit(traces, trace_mod,
"pixel");
8308 for (k = 0; k < nrows; k++) {
8309 cpl_table_set_double(traces, trace_mod, k,
8310 cpl_polynomial_eval_1d(polytrace, xdata[k], NULL));
8313 cpl_polynomial_delete(polytrace);
8315 cpl_table_duplicate_column(traces, trace_res, traces, trace_mod);
8316 cpl_table_subtract_columns(traces, trace_res, trace_id);
8317 cpl_table_multiply_scalar(traces, trace_res, -1.0);
8353 const char *func =
"mos_global_trace";
8355 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8368 int order, nrows, nslits;
8372 if (polytraces == NULL) {
8373 cpl_msg_error(func,
"Missing spectral curvature table");
8374 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8377 if (slits == NULL) {
8378 cpl_msg_error(func,
"Missing slits positions table");
8379 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8382 nslits = cpl_table_get_nrow(slits);
8384 table = cpl_table_duplicate(polytraces);
8385 cpl_table_erase_invalid(table);
8387 nrows = cpl_table_get_nrow(table);
8390 cpl_msg_warning(func,
"Too few successful spectral curvature tracings "
8391 "(%d): the determination of a global curvature model "
8393 return CPL_ERROR_NONE;
8396 order = cpl_table_get_ncol(polytraces) - 2;
8398 for (i = 0; i <= order; i++) {
8399 if (!cpl_table_has_column(table, clab[i])) {
8400 cpl_msg_error(func,
"Wrong spectral curvature table");
8401 return cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8410 for (i = 0; i < nslits; i++) {
8411 if (!cpl_table_is_valid(polytraces, clab[0], 2*i)) {
8412 cpl_table_set_double(polytraces, clab[0], 2*i,
8413 cpl_table_get_double(slits,
"ytop", i, NULL));
8415 if (!cpl_table_is_valid(polytraces, clab[0], 2*i+1)) {
8416 cpl_table_set_double(polytraces, clab[0], 2*i+1,
8417 cpl_table_get_double(slits,
"ybottom", i, NULL));
8421 offset = cpl_table_get_data_double(polytraces, clab[0]);
8428 c0 = cpl_vector_wrap(nrows, cpl_table_get_data_double(table, clab[0]));
8430 for (i = 1; i <= order; i++) {
8431 cn = cpl_vector_wrap(nrows, cpl_table_get_data_double(table, clab[i]));
8432 list = cpl_bivector_wrap_vectors(c0, cn);
8433 robustLinearFit(list, &q, &m, &rms);
8437 for (j = 0; j < 2*nslits; j++) {
8439 if (cpl_table_is_valid(polytraces, clab[i], j))
8441 cpl_table_set_double(polytraces, clab[i], j, offset[j]*m + q);
8447 cpl_bivector_unwrap_vectors(list);
8451 cpl_vector_unwrap(cn);
8454 cpl_vector_unwrap(c0);
8455 cpl_table_delete(table);
8457 return CPL_ERROR_NONE;
8532 cpl_table *polytraces,
double reference,
8533 double blue,
double red,
double dispersion,
8534 int flux, cpl_image *calibration)
8536 const char *func =
"mos_spatial_calibration";
8538 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8540 cpl_polynomial *polytop;
8541 cpl_polynomial *polybot;
8543 cpl_image *resampled;
8547 double vtop, vbot, value;
8553 int yint, ysize, yprev;
8559 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
8560 int missing_top, missing_bot;
8566 int create_position = 1;
8569 if (spectra == NULL || slits == NULL || polytraces == NULL) {
8570 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8574 if (dispersion <= 0.0) {
8575 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8579 if (red - blue < dispersion) {
8580 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8584 nx = cpl_image_get_size_x(spectra);
8585 ny = cpl_image_get_size_y(spectra);
8586 sdata = cpl_image_get_data(spectra);
8588 data = cpl_image_get_data(calibration);
8590 if (cpl_table_has_column(slits,
"position"))
8591 create_position = 0;
8593 if (create_position) {
8594 cpl_table_new_column(slits,
"position", CPL_TYPE_INT);
8595 cpl_table_new_column(slits,
"length", CPL_TYPE_INT);
8596 cpl_table_set_column_unit(slits,
"position",
"pixel");
8597 cpl_table_set_column_unit(slits,
"length",
"pixel");
8600 length = cpl_table_get_data_int(slits,
"length");
8602 nslits = cpl_table_get_nrow(slits);
8603 slit_id = cpl_table_get_data_int(slits,
"slit_id");
8604 order = cpl_table_get_ncol(polytraces) - 2;
8611 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
8612 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
8614 exslit = cpl_calloc(nslits,
sizeof(cpl_image *));
8616 for (i = 0; i < nslits; i++) {
8618 if (create_position == 0)
8633 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
8635 start_pixel = refpixel - pixel_below;
8636 if (start_pixel < 0)
8639 end_pixel = refpixel + pixel_above;
8649 polytop = cpl_polynomial_new(1);
8650 for (k = 0; k <= order; k++) {
8651 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
8653 cpl_polynomial_delete(polytop);
8657 cpl_polynomial_set_coeff(polytop, &k, coeff);
8661 polybot = cpl_polynomial_new(1);
8662 for (k = 0; k <= order; k++) {
8663 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
8665 cpl_polynomial_delete(polybot);
8669 cpl_polynomial_set_coeff(polybot, &k, coeff);
8672 if (missing_top && missing_bot) {
8673 cpl_msg_warning(func,
"Spatial calibration, slit %d was not "
8674 "traced: no extraction!",
8686 cpl_msg_warning(func,
"Upper edge of slit %d was not traced: "
8687 "the spectral curvature of the lower edge "
8688 "is used instead.", slit_id[i]);
8689 polytop = cpl_polynomial_duplicate(polybot);
8690 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
8691 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
8693 coeff = cpl_polynomial_get_coeff(polybot, &k);
8694 coeff += ytop - ybot;
8695 cpl_polynomial_set_coeff(polytop, &k, coeff);
8699 cpl_msg_warning(func,
"Lower edge of slit %d was not traced: "
8700 "the spectral curvature of the upper edge "
8701 "is used instead.", slit_id[i]);
8702 polybot = cpl_polynomial_duplicate(polytop);
8703 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
8704 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
8706 coeff = cpl_polynomial_get_coeff(polytop, &k);
8707 coeff -= ytop - ybot;
8708 cpl_polynomial_set_coeff(polybot, &k, coeff);
8715 top = cpl_polynomial_eval_1d(polytop, refpixel, NULL);
8716 bot = cpl_polynomial_eval_1d(polybot, refpixel, NULL);
8717 npseudo = ceil(top-bot) + 1;
8720 cpl_polynomial_delete(polytop);
8721 cpl_polynomial_delete(polybot);
8722 cpl_msg_warning(func,
"Slit %d was badly traced: no extraction!",
8727 exslit[i] = cpl_image_new(nx, npseudo+1, CPL_TYPE_FLOAT);
8728 xdata = cpl_image_get_data(exslit[i]);
8734 for (j = start_pixel; j < end_pixel; j++) {
8735 top = cpl_polynomial_eval_1d(polytop, j, NULL);
8736 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
8737 factor = (top-bot)/npseudo;
8738 for (k = 0; k <= npseudo; k++) {
8739 ypos = top - k*factor;
8742 if (yint >= 0 && yint < ny-1) {
8743 vtop = sdata[j + nx*yint];
8744 vbot = sdata[j + nx*(yint+1)];
8750 else if(vtop == FLT_MAX || vbot == FLT_MAX)
8754 value = vtop*(1-yfra) + vbot*yfra;
8758 xdata[j + nx*(npseudo-k)] = value;
8760 data[j + nx*yint] = (top-yint)/factor;
8769 if (yprev - yint > 1) {
8770 data[j + nx*(yint+1)] = (top-yint-1)/factor;
8778 cpl_polynomial_delete(polytop);
8779 cpl_polynomial_delete(polybot);
8787 for (i = 0; i < nslits; i++)
8789 ysize += cpl_image_get_size_y(exslit[i]);
8794 resampled = cpl_image_new(nx, ysize, CPL_TYPE_FLOAT);
8797 for (i = 0; i < nslits; i++) {
8799 yint += cpl_image_get_size_y(exslit[i]);
8800 cpl_image_copy(resampled, exslit[i], 1, ysize - yint);
8801 if (create_position) {
8802 cpl_table_set_int(slits,
"position", i, ysize - yint - 1);
8803 cpl_table_set_int(slits,
"length", i,
8804 cpl_image_get_size_y(exslit[i]));
8806 cpl_image_delete(exslit[i]);
8808 else if (create_position) {
8809 cpl_table_set_int(slits,
"position", i, -1);
8810 cpl_table_set_int(slits,
"length", i, 0);
8951 double dispersion,
float level,
8952 int sradius,
int order,
8953 double reject,
double refwave,
8954 double *wavestart,
double *waveend,
8955 int *nlines,
double *error,
8956 cpl_table *idscoeff,
8957 cpl_image *calibration,
8958 cpl_image *residuals,
8959 cpl_table *restable,
8960 cpl_table *detected_lines)
8963 const char *func =
"mos_wavelength_calibration_final";
8965 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8968 double tolerance = 20.0;
8971 char name[MAX_COLNAME];
8973 cpl_image *resampled;
8974 cpl_bivector *peaks_ident;
8977 cpl_polynomial *ids;
8978 cpl_polynomial *lin;
8979 cpl_polynomial *fguess;
8982 double max_disp, min_disp;
8984 double firstLambda, lastLambda, lambda;
8985 double wave, pixe, value;
8994 int pixstart, pixend;
8995 int row_top, row_bot;
9000 int nl, nx, ny, pixel;
9001 int countLines, usedLines;
9010 if (dispersion == 0.0) {
9011 cpl_msg_error(func,
"The expected dispersion (A/pixel) must be given");
9012 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9016 if (dispersion < 0.0) {
9017 cpl_msg_error(func,
"The expected dispersion must be positive");
9018 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9022 if (idscoeff == NULL) {
9023 cpl_msg_error(func,
"A preallocated IDS coeff table must be given");
9024 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9028 max_disp = dispersion + dispersion * tolerance / 100;
9029 min_disp = dispersion - dispersion * tolerance / 100;
9032 cpl_msg_error(func,
"The order of the fitting polynomial "
9033 "must be at least 1");
9034 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9038 if (image == NULL || lines == NULL) {
9039 cpl_msg_error(func,
"Both spectral exposure and reference line "
9040 "catalog are required in input");
9041 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9045 nx = cpl_image_get_size_x(image);
9046 ny = cpl_image_get_size_y(image);
9047 sdata = cpl_image_get_data_float(image);
9049 nref = cpl_vector_get_size(lines);
9050 line = cpl_vector_get_data(lines);
9052 if (*wavestart < 1.0 && *waveend < 1.0) {
9053 firstLambda = line[0];
9054 lastLambda = line[nref-1];
9055 extrapolation = (lastLambda - firstLambda) / 10;
9056 firstLambda -= extrapolation;
9057 lastLambda += extrapolation;
9058 *wavestart = firstLambda;
9059 *waveend = lastLambda;
9062 firstLambda = *wavestart;
9063 lastLambda = *waveend;
9066 nl = (lastLambda - firstLambda) / dispersion;
9067 resampled = cpl_image_new(nl, ny, CPL_TYPE_FLOAT);
9068 rdata = cpl_image_get_data_float(resampled);
9074 for (j = 0; j <= order; j++)
9075 cpl_table_new_column(idscoeff, clab[j], CPL_TYPE_DOUBLE);
9078 idata = cpl_image_get_data_float(calibration);
9081 ddata = cpl_image_get_data_float(residuals);
9084 cpl_table_set_size(restable, nref);
9085 cpl_table_new_column(restable,
"wavelength", CPL_TYPE_DOUBLE);
9086 cpl_table_copy_data_double(restable,
"wavelength", line);
9087 for (i = 0; i < ny; i += step) {
9088 snprintf(name, MAX_COLNAME,
"r%d", i);
9089 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
9090 snprintf(name, MAX_COLNAME,
"d%d", i);
9091 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
9092 snprintf(name, MAX_COLNAME,
"p%d", i);
9093 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
9097 if (detected_lines) {
9098 cpl_table_set_size(detected_lines, 0);
9099 cpl_table_new_column(detected_lines,
"slit_id", CPL_TYPE_INT);
9100 cpl_table_new_column(detected_lines,
"xpos_rectified", CPL_TYPE_DOUBLE);
9101 cpl_table_new_column(detected_lines,
"ypos_rectified", CPL_TYPE_DOUBLE);
9102 cpl_table_new_column(detected_lines,
"xpos_rectified_iter", CPL_TYPE_DOUBLE);
9103 cpl_table_new_column(detected_lines,
"ypos_rectified_iter", CPL_TYPE_DOUBLE);
9104 cpl_table_new_column(detected_lines,
"peak_flux", CPL_TYPE_DOUBLE);
9105 cpl_table_new_column(detected_lines,
"wave_ident", CPL_TYPE_DOUBLE);
9106 cpl_table_new_column(detected_lines,
"wave_ident_iter", CPL_TYPE_DOUBLE);
9107 cpl_table_new_column(detected_lines,
"xpos_fit_rect_wavecal", CPL_TYPE_DOUBLE);
9108 cpl_table_new_column(detected_lines,
"res_xpos", CPL_TYPE_DOUBLE);
9109 cpl_table_new_column(detected_lines,
"fit_used", CPL_TYPE_INT);
9117 nslits = cpl_table_get_nrow(slits);
9118 length = cpl_table_get_data_int(slits,
"length");
9121 for (s = 0; s < nslits; s++) {
9124 slit_id = cpl_table_get_int(slits,
"slit_id", s, NULL);
9134 row_bot = cpl_table_get_int(slits,
"position", s, NULL);
9146 coeff = cpl_table_new(row_top - row_bot);
9147 for (j = 0; j <= order; j++)
9148 cpl_table_new_column(coeff, clab[j], CPL_TYPE_DOUBLE);
9156 for (i = row_bot; i < row_top; i++) {
9165 int keep_multiplex = mos_multiplex;
9169 cpl_size newlines = cpl_vector_get_size(peaks);
9170 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
9171 cpl_table_set_size(detected_lines, oldsize + newlines);
9172 for(cpl_size iline = 0; iline < newlines; ++iline)
9174 cpl_table_set_int(detected_lines,
"slit_id",
9175 oldsize + iline, slit_id);
9176 cpl_table_set_double(detected_lines,
"xpos_rectified",
9177 oldsize + iline, cpl_vector_get(peaks, iline) + 1);
9178 cpl_table_set_double(detected_lines,
"ypos_rectified",
9179 oldsize + iline, (
double)i + 1);
9180 cpl_table_set_double(detected_lines,
"peak_flux",
9182 sdata[i*nx+(
int)(cpl_vector_get(peaks, iline)+0.5)]);
9183 cpl_table_set_int(detected_lines,
9185 oldsize + iline, 0);
9189 min_disp, max_disp, 0.05);
9190 mos_multiplex = keep_multiplex;
9192 cpl_bivector * peaks_ident_used_fit;
9193 countLines = cpl_bivector_get_size(peaks_ident);
9194 if (countLines < 4) {
9195 cpl_bivector_delete(peaks_ident);
9196 cpl_vector_delete(peaks);
9208 wavel = cpl_bivector_get_y(peaks_ident);
9209 cpl_vector_subtract_scalar(wavel, refwave);
9211 uorder = countLines / 2 - 1;
9216 2 * (uorder + 1), &usedLines,
9217 &ids_err, &peaks_ident_used_fit);
9220 cpl_bivector_delete(peaks_ident);
9221 cpl_vector_delete(peaks);
9231 for (k = 0; k <= order; k++) {
9233 cpl_table_set_double(coeff, clab[k],
9237 cpl_table_set_double(coeff, clab[k],
9238 i - row_bot, cpl_polynomial_get_coeff(ids, &k));
9244 pixstart = cpl_polynomial_eval_1d(ids,
9245 cpl_bivector_get_y_data(peaks_ident)[0],
9247 pixend = cpl_polynomial_eval_1d(ids,
9248 cpl_bivector_get_y_data(peaks_ident)[countLines-1],
9250 extrapolation = (pixend - pixstart) / 5;
9251 pixstart -= extrapolation;
9252 pixend += extrapolation;
9258 for (j = pixstart; j < pixend; j++) {
9260 firstLambda, lastLambda, refwave, j);
9268 if (residuals || (restable && !(i%step))) {
9269 if (restable && !(i%step)) {
9270 lin = cpl_polynomial_new(1);
9271 for (k = 0; k < 2; k++)
9272 cpl_polynomial_set_coeff(lin, &k,
9273 cpl_polynomial_get_coeff(ids, &k));
9275 for (j = 0; j < countLines; j++) {
9276 pixe = cpl_bivector_get_x_data(peaks_ident)[j];
9277 wave = cpl_bivector_get_y_data(peaks_ident)[j];
9279 - cpl_polynomial_eval_1d(ids, wave, NULL);
9282 (ddata + i*nx)[pixel] = value;
9284 if (restable && !(i%step)) {
9285 for (k = 0; k < nref; k++) {
9286 if (fabs(line[k]-refwave-wave) < 0.1) {
9287 snprintf(name, MAX_COLNAME,
9289 cpl_table_set_double(restable, name,
9292 - cpl_polynomial_eval_1d(lin,
9294 snprintf(name, MAX_COLNAME,
9296 cpl_table_set_double(restable, name,
9298 snprintf(name, MAX_COLNAME,
9300 cpl_table_set_double(restable, name,
9307 if (restable && !(i%step)) {
9308 cpl_polynomial_delete(lin);
9325 cpl_size nidentlines = cpl_bivector_get_size(peaks_ident);
9326 cpl_size ndetectlines = cpl_vector_get_size(peaks);
9327 cpl_size totalsize = cpl_table_get_nrow(detected_lines);
9328 for(cpl_size idline = 0; idline < nidentlines; ++idline)
9330 for(cpl_size detline = 0; detline < ndetectlines; ++detline)
9332 if(cpl_vector_get(peaks, detline) ==
9333 cpl_bivector_get_x_data(peaks_ident)[idline])
9335 cpl_size table_pos = totalsize - ndetectlines + detline;
9336 double wave_ident = cpl_bivector_get_y_data(peaks_ident)[idline] + refwave;
9337 double xpix_fit = cpl_polynomial_eval_1d(ids,
9338 wave_ident - refwave, NULL);
9339 double xpos_det = cpl_table_get_double(detected_lines,
9342 cpl_table_set_double(detected_lines,
9346 cpl_table_set_double(detected_lines,
9347 "xpos_fit_rect_wavecal",
9350 cpl_table_set_double(detected_lines,
9353 xpos_det - xpix_fit - 1);
9354 cpl_table_set_int(detected_lines,
9357 for(cpl_size i_used = 0; i_used < cpl_bivector_get_size(peaks_ident_used_fit); ++i_used)
9359 if(cpl_bivector_get_x_data(peaks_ident)[idline] == cpl_bivector_get_x_data(peaks_ident_used_fit)[i_used])
9360 cpl_table_set_int(detected_lines,
9379 nlines[i] = usedLines;
9381 error[i] = ids_err / sqrt(usedLines/(uorder + 1));
9383 for (k = 0; k <= order; k++) {
9385 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
9388 cpl_table_set_double(idscoeff, clab[k], i,
9389 cpl_polynomial_get_coeff(ids, &k));
9393 cpl_polynomial_delete(ids);
9394 cpl_bivector_delete(peaks_ident);
9396 cpl_vector_delete(peaks);
9407 nfits = row_top - row_bot - cpl_table_count_invalid(coeff, clab[0]);
9412 fguess = cpl_polynomial_new(1);
9422 for (k = 0; k <= order; k++) {
9423 c = cpl_table_get_column_median(coeff, clab[k]);
9424 cpl_polynomial_set_coeff(fguess, &k, c);
9431 for (i = row_bot; i < row_top; i++) {
9432 cpl_bivector * peaks_ident_used_fit;
9439 if (width > sradius) {
9447 for (k = 0; k <= order; k++) {
9448 c = cpl_table_get_double(coeff, clab[k],
9450 cpl_polynomial_set_coeff(fguess, &k, c);
9455 fguess, refwave, uradius);
9457 if (peaks_ident == NULL) {
9462 countLines = cpl_bivector_get_size(peaks_ident);
9464 if (countLines < 4) {
9465 cpl_bivector_delete(peaks_ident);
9473 wavel = cpl_bivector_get_y(peaks_ident);
9474 cpl_vector_subtract_scalar(wavel, refwave);
9476 uorder = countLines / 2 - 1;
9481 2 * (uorder + 1), &usedLines,
9482 &ids_err, &peaks_ident_used_fit);
9486 cpl_bivector_delete(peaks_ident);
9491 nlines[i] = usedLines;
9493 error[i] = ids_err / sqrt(usedLines/(uorder + 1));
9496 pixstart = cpl_polynomial_eval_1d(ids,
9497 cpl_bivector_get_y_data(peaks_ident)[0],
9499 pixend = cpl_polynomial_eval_1d(ids,
9500 cpl_bivector_get_y_data(peaks_ident)[countLines-1],
9502 extrapolation = (pixend - pixstart) / 5;
9503 pixstart -= extrapolation;
9504 pixend += extrapolation;
9510 for (j = pixstart; j < pixend; j++) {
9512 firstLambda, lastLambda, refwave, j);
9520 if (residuals || (restable && !(i%step))) {
9521 if (restable && !(i%step)) {
9522 lin = cpl_polynomial_new(1);
9523 for (k = 0; k < 2; k++)
9524 cpl_polynomial_set_coeff(lin, &k,
9525 cpl_polynomial_get_coeff(ids, &k));
9527 for (j = 0; j < countLines; j++) {
9528 pixe = cpl_bivector_get_x_data(peaks_ident)[j];
9529 wave = cpl_bivector_get_y_data(peaks_ident)[j];
9531 - cpl_polynomial_eval_1d(ids, wave, NULL);
9534 (ddata + i*nx)[pixel] = value;
9536 if (restable && !(i%step)) {
9537 for (k = 0; k < nref; k++) {
9538 if (fabs(line[k]-refwave-wave) < 0.1) {
9539 snprintf(name, MAX_COLNAME,
9541 cpl_table_set_double(restable, name,
9544 - cpl_polynomial_eval_1d(lin,
9546 snprintf(name, MAX_COLNAME,
9548 cpl_table_set_double(restable, name,
9550 snprintf(name, MAX_COLNAME,
9552 cpl_table_set_double(restable, name,
9559 if (restable && !(i%step)) {
9560 cpl_polynomial_delete(lin);
9577 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
9578 cpl_size nidentlines = cpl_bivector_get_size(peaks_ident);
9579 cpl_table_set_size(detected_lines, oldsize + nidentlines);
9580 for(cpl_size idline = 0; idline < nidentlines ; ++idline)
9582 double wave_ident = cpl_bivector_get_y_data(peaks_ident)[idline] + refwave;
9583 double xpix_fit = cpl_polynomial_eval_1d(ids,
9584 wave_ident - refwave, NULL);
9585 cpl_table_set_int(detected_lines,
"slit_id",
9586 oldsize + idline, slit_id);
9587 cpl_table_set_double(detected_lines,
"xpos_rectified_iter",
9588 oldsize + idline, cpl_bivector_get_x_data(peaks_ident)[idline] + 1);
9589 cpl_table_set_double(detected_lines,
"ypos_rectified_iter",
9590 oldsize + idline, (
double)i + 1);
9591 cpl_table_set_double(detected_lines,
"peak_flux",
9593 sdata[i*nx+(
int)(cpl_bivector_get_x_data(peaks_ident)[idline]+0.5)]);
9594 cpl_table_set_double(detected_lines,
"wave_ident_iter",
9595 oldsize + idline, wave_ident);
9596 cpl_table_set_double(detected_lines,
"xpos_fit_rect_wavecal",
9597 oldsize + idline, xpix_fit + 1);
9598 cpl_table_set_int(detected_lines,
9600 oldsize + idline, 0);
9601 for(cpl_size i_used = 0; i_used < cpl_bivector_get_size(peaks_ident_used_fit); ++i_used)
9603 if(cpl_bivector_get_x_data(peaks_ident)[idline] == cpl_bivector_get_x_data(peaks_ident_used_fit)[i_used])
9604 cpl_table_set_int(detected_lines,
9606 oldsize + idline, 1);
9612 for (k = 0; k <= order; k++) {
9614 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
9617 cpl_table_set_double(idscoeff, clab[k], i,
9618 cpl_polynomial_get_coeff(ids, &k));
9622 cpl_bivector_delete(peaks_ident);
9623 cpl_polynomial_delete(ids);
9627 cpl_polynomial_delete(fguess);
9630 cpl_table_delete(coeff);
9646 for (i = 0; i < ny; i++) {
9649 ids = cpl_polynomial_new(1);
9650 for (k = 0; k <= order; k++) {
9651 c = cpl_table_get_double(idscoeff, clab[k], i, &null);
9653 cpl_polynomial_delete(ids);
9657 cpl_polynomial_set_coeff(ids, &k, c);
9662 pixstart = cpl_polynomial_eval_1d(ids, firstLambda - refwave, NULL);
9663 pixend = cpl_polynomial_eval_1d(ids, lastLambda - refwave, NULL);
9673 for (j = 0; j < nl; j++) {
9674 lambda = firstLambda + j * dispersion;
9675 fpixel = cpl_polynomial_eval_1d(ids, lambda - refwave, NULL);
9677 if (pixel >= 0 && pixel < nx-1) {
9678 v1 = (sdata + i*nx)[pixel];
9679 v2 = (sdata + i*nx)[pixel+1];
9680 vi = v1 + (v2-v1)*(fpixel-pixel);
9681 (rdata + i*nl)[j] = vi;
9685 cpl_polynomial_delete(ids);
9719 double firstLambda,
double lastLambda,
9720 double dispersion, cpl_table *idscoeff,
9724 const char *func =
"mos_wavelength_calibration";
9726 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
9729 cpl_image *resampled;
9730 cpl_polynomial *ids;
9731 double pixel_per_lambda;
9736 float v0, v1, v2, v3, vi;
9739 int pixstart, pixend;
9740 int nl, nx, ny, pixel;
9747 if (dispersion <= 0.0) {
9748 cpl_msg_error(func,
"The resampling step must be positive");
9749 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9753 if (lastLambda - firstLambda < dispersion) {
9754 cpl_msg_error(func,
"Invalid spectral range: %.2f to %.2f",
9755 firstLambda, lastLambda);
9756 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9760 if (idscoeff == NULL) {
9761 cpl_msg_error(func,
"An IDS coeff table must be given");
9762 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9766 if (image == NULL) {
9767 cpl_msg_error(func,
"A scientific spectral image must be given");
9768 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9772 nx = cpl_image_get_size_x(image);
9773 ny = cpl_image_get_size_y(image);
9774 sdata = cpl_image_get_data_float(image);
9776 nl = (lastLambda - firstLambda) / dispersion;
9777 resampled = cpl_image_new(nl, ny, CPL_TYPE_FLOAT);
9778 rdata = cpl_image_get_data_float(resampled);
9781 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
9785 for (i = 0; i < ny; i++) {
9788 ids = cpl_polynomial_new(1);
9789 for (k = 0; k <= order; k++) {
9790 c = cpl_table_get_double(idscoeff, clab[k], i, &null);
9792 cpl_polynomial_delete(ids);
9796 cpl_polynomial_set_coeff(ids, &k, c);
9801 pixstart = cpl_polynomial_eval_1d(ids, firstLambda - refwave, NULL);
9802 pixend = cpl_polynomial_eval_1d(ids, lastLambda - refwave, NULL);
9812 for (j = 0; j < nl; j++) {
9813 lambda = firstLambda + j * dispersion;
9814 fpixel = cpl_polynomial_eval_1d(ids, lambda - refwave,
9836 if(pixel_per_lambda <= 0)
9838 else if (fpixel < 0)
9840 else if (pixel >= 1 && pixel < nx-2) {
9841 v0 = (sdata + i*nx)[pixel-1];
9842 v1 = (sdata + i*nx)[pixel];
9843 v2 = (sdata + i*nx)[pixel+1];
9844 v3 = (sdata + i*nx)[pixel+2];
9845 vi = (fpixel-pixel)*(fpixel-pixel)*(v3 - v2 - v1 + v0)
9846 + (fpixel-pixel)*(3*v2 - v3 - v1 - v0)
9866 vi *= dispersion * pixel_per_lambda;
9868 else if (pixel >= 0 && pixel < nx-1) {
9869 v1 = (sdata + i*nx)[pixel];
9870 v2 = (sdata + i*nx)[pixel+1];
9871 vi = v1 + (v2-v1)*(fpixel-pixel);
9873 vi *= dispersion * pixel_per_lambda;
9877 (rdata + i*nl)[j] = vi;
9890 double spos = fpixel - dispersion * pixel_per_lambda / 2;
9891 double epos = fpixel + dispersion * pixel_per_lambda / 2;
9898 int epix = epos + 1;
9907 for (k = spix; k < epix; k++) {
9908 if (pixel >= 0 && pixel < nx) {
9909 vi += (sdata + i*nx)[k];
9920 vi *= dispersion * pixel_per_lambda / (epix - spix);
9928 vi *= dispersion * pixel_per_lambda;
9930 (rdata + i*nl)[j] = vi;
9934 cpl_polynomial_delete(ids);
10008 double refwave,
double firstLambda,
10009 double lastLambda, cpl_table *idscoeff,
10010 cpl_vector *skylines,
int highres,
int order,
10011 cpl_image *calibration,
int sradius)
10013 const char *func =
"mos_wavelength_align";
10015 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
10019 double expPos, offset;
10021 double lambda1, lambda2;
10027 int startPos, endPos;
10028 int window = 2*sradius + 1;
10034 int xlow, ylow, xhig, yhig;
10035 int idsorder, uorder;
10044 char offname[MAX_COLNAME];
10045 char name[MAX_COLNAME];
10047 cpl_polynomial *ids;
10048 cpl_polynomial *polycorr;
10051 cpl_table *offsets;
10057 if (idscoeff == NULL) {
10058 cpl_msg_error(func,
"An IDS coeff table must be given");
10059 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10063 if (image == NULL) {
10064 cpl_msg_error(func,
"A scientific spectral image must be given");
10065 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10069 if (slits == NULL) {
10070 cpl_msg_error(func,
"A slit position table must be given");
10071 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10076 line = cpl_vector_get_data(skylines);
10077 nlines = cpl_vector_get_size(skylines);
10080 cpl_msg_warning(func,
"A catalog of sky lines wavelengths was not "
10081 "given: using internal list of reference sky lines");
10083 line = default_lines_hi;
10084 nlines =
sizeof(default_lines_hi) /
sizeof(
double);
10087 line = default_lines_lo;
10088 nlines =
sizeof(default_lines_lo) /
sizeof(
double);
10093 cdata = cpl_image_get_data(calibration);
10095 nx = cpl_image_get_size_x(image);
10096 ny = cpl_image_get_size_y(image);
10098 nslits = cpl_table_get_nrow(slits);
10099 slit_id = cpl_table_get_data_int(slits,
"slit_id");
10100 position = cpl_table_get_data_int(slits,
"position");
10101 length = cpl_table_get_data_int(slits,
"length");
10109 for (i = 0; i < nlines; i++)
10110 if (line[i] > firstLambda && line[i] < lastLambda)
10113 offsets = cpl_table_new(nrows);
10114 cpl_table_new_column(offsets,
"wave", CPL_TYPE_DOUBLE);
10115 cpl_table_set_column_unit(offsets,
"wave",
"Angstrom");
10118 for (i = 0; i < nlines; i++) {
10119 if (line[i] > firstLambda && line[i] < lastLambda) {
10120 cpl_table_set_double(offsets,
"wave", nrows, line[i]);
10129 line = cpl_table_get_data_double(offsets,
"wave");
10133 while (idsorder < 6 && cpl_table_has_column(idscoeff, clab[idsorder]))
10139 for (i = 0; i < nslits; i++) {
10141 if (length[i] == 0)
10144 snprintf(offname, MAX_COLNAME,
"offset%d", slit_id[i]);
10145 cpl_table_new_column(offsets, offname, CPL_TYPE_DOUBLE);
10157 ylow = position[i] + 1;
10158 yhig = ylow + length[i] - 1;
10160 exslit = cpl_image_extract(image, xlow, ylow, xhig, yhig);
10161 sky = cpl_image_collapse_median_create(exslit, 0, 0, 1);
10162 sdata = cpl_image_get_data(sky);
10164 cpl_image_delete(exslit);
10179 dummy = cpl_table_new(yhig - ylow);
10180 for (j = 0; j < nlines; j++) {
10181 snprintf(name, MAX_COLNAME,
"%"CPL_SIZE_FORMAT, j);
10182 cpl_table_new_column(dummy, name, CPL_TYPE_DOUBLE);
10185 for (j = ylow; j < yhig; j++) {
10192 ids = cpl_polynomial_new(1);
10193 for (k = 0; k <= idsorder; k++) {
10194 c = cpl_table_get_double(idscoeff, clab[k], j, &null);
10196 cpl_polynomial_delete(ids);
10200 cpl_polynomial_set_coeff(ids, &k, c);
10205 for (k = 0; k < nlines; k++) {
10206 expPos = cpl_polynomial_eval_1d(ids, line[k] - refwave, NULL);
10207 startPos = expPos - sradius;
10208 endPos = startPos + window;
10209 if (startPos < 0 || endPos >= nx)
10212 if (0 == peakPosition(sdata + startPos, window, &pos, 1)) {
10214 offset = pos - expPos;
10215 snprintf(name, MAX_COLNAME,
"%"CPL_SIZE_FORMAT, k);
10216 cpl_table_set_double(dummy, name, j - ylow, offset);
10220 cpl_polynomial_delete(ids);
10223 cpl_image_delete(sky);
10225 for (j = 0; j < nlines; j++) {
10226 snprintf(name, MAX_COLNAME,
"%"CPL_SIZE_FORMAT, j);
10227 if (cpl_table_has_valid(dummy, name)) {
10228 offset = cpl_table_get_column_median(dummy, name);
10229 cpl_table_set_double(offsets, offname, j, offset);
10233 cpl_table_delete(dummy);
10244 for (i = 0; i < nslits; i++) {
10246 if (length[i] == 0)
10249 snprintf(offname, MAX_COLNAME,
"offset%d", slit_id[i]);
10256 dummy = cpl_table_new(nlines);
10257 cpl_table_duplicate_column(dummy,
"wave", offsets,
"wave");
10258 cpl_table_duplicate_column(dummy,
"offset", offsets, offname);
10260 npoints = nlines - cpl_table_count_invalid(dummy,
"offset");
10261 if (npoints == 0) {
10262 cpl_msg_warning(func,
"No sky lines alignment was possible "
10263 "for slit ID=%d: no sky line found", slit_id[i]);
10264 cpl_table_delete(dummy);
10269 if (npoints <= uorder) {
10270 uorder = npoints - 1;
10272 cpl_msg_warning(func,
"Just %d sky lines detected for slit "
10273 "ID=%d, while a polynomial order %d was "
10274 "requested. Using polynomial order %d for "
10275 "this slit!", npoints, slit_id[i], order,
10279 cpl_msg_warning(func,
"Just %d sky lines detected for slit "
10280 "ID=%d, while a polynomial order %d was "
10281 "requested. Computing a median offset for "
10282 "this slit!", npoints, slit_id[i], order);
10286 cpl_table_erase_invalid(dummy);
10294 wave = cpl_vector_wrap(npoints,
10295 cpl_table_get_data_double(dummy,
"wave"));
10296 offs = cpl_vector_wrap(npoints,
10297 cpl_table_get_data_double(dummy,
"offset"));
10303 cpl_vector_subtract_scalar(wave, refwave);
10305 polycorr = cpl_polynomial_fit_1d_create(wave, offs, uorder, &rms);
10307 rms = sqrt(rms * (uorder + 1) / npoints);
10309 cpl_vector_unwrap(wave);
10310 cpl_vector_unwrap(offs);
10311 cpl_table_delete(dummy);
10318 ylow = position[i];
10319 yhig = ylow + length[i];
10321 for (j = 0; j <= uorder; j++) {
10322 data = cpl_table_get_data_double(idscoeff, clab[j]);
10323 c = cpl_polynomial_get_coeff(polycorr, &j);
10324 for (k = ylow; k < yhig; k++)
10328 data = cpl_table_get_data_double(idscoeff,
"error");
10329 for (k = ylow; k < yhig; k++)
10330 data[k] = sqrt(data[k]*data[k] + rms*rms);
10332 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10333 for (k = ylow; k < yhig; k++)
10334 idata[k] = npoints;
10342 for (j = ylow; j < yhig; j++) {
10343 for (k = 1; k < nx; k++) {
10344 lambda1 = cdata[k - 1 + j*nx];
10345 lambda2 = cdata[k + j*nx];
10346 if (lambda1 < 1.0 || lambda2 < 1.0)
10348 offset = cpl_polynomial_eval_1d(polycorr,
10349 lambda1-refwave, NULL);
10350 cdata[k - 1 + j*nx] -= offset * (lambda2-lambda1);
10355 cpl_polynomial_delete(polycorr);
10357 else if (uorder == 1) {
10364 cpl_bivector *list;
10367 wave = cpl_vector_wrap(npoints,
10368 cpl_table_get_data_double(dummy,
"wave"));
10369 offs = cpl_vector_wrap(npoints,
10370 cpl_table_get_data_double(dummy,
"offset"));
10372 list = cpl_bivector_wrap_vectors(wave, offs);
10378 cpl_vector_subtract_scalar(wave, refwave);
10380 robustLinearFit(list, &q, &m, &rms);
10382 rms = sqrt(rms * (uorder + 1) / npoints);
10384 cpl_bivector_unwrap_vectors(list);
10385 cpl_vector_unwrap(wave);
10386 cpl_vector_unwrap(offs);
10387 cpl_table_delete(dummy);
10394 ylow = position[i];
10395 yhig = ylow + length[i];
10397 for (j = 0; j <= uorder; j++) {
10398 data = cpl_table_get_data_double(idscoeff, clab[j]);
10403 for (k = ylow; k < yhig; k++)
10407 data = cpl_table_get_data_double(idscoeff,
"error");
10408 for (k = ylow; k < yhig; k++)
10409 data[k] = sqrt(data[k]*data[k] + rms*rms);
10411 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10412 for (k = ylow; k < yhig; k++)
10413 idata[k] = npoints;
10421 for (j = ylow; j < yhig; j++) {
10422 for (k = 1; k < nx; k++) {
10423 lambda1 = cdata[k - 1 + j*nx];
10424 lambda2 = cdata[k + j*nx];
10425 if (lambda1 < 1.0 || lambda2 < 1.0)
10427 offset = q + m*(lambda1-refwave);
10428 cdata[k - 1 + j*nx] -= offset * (lambda2-lambda1);
10439 offs = cpl_vector_wrap(npoints,
10440 cpl_table_get_data_double(dummy,
"offset"));
10442 offset = cpl_vector_get_median_const(offs);
10445 rms = cpl_table_get_column_stdev(dummy,
"offset");
10449 rms /= sqrt(npoints);
10451 cpl_vector_unwrap(offs);
10452 cpl_table_delete(dummy);
10459 ylow = position[i];
10460 yhig = ylow + length[i];
10462 data = cpl_table_get_data_double(idscoeff, clab[0]);
10463 for (k = ylow; k < yhig; k++)
10466 data = cpl_table_get_data_double(idscoeff,
"error");
10467 for (k = ylow; k < yhig; k++)
10468 data[k] = sqrt(data[k]*data[k] + rms*rms);
10470 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10471 for (k = ylow; k < yhig; k++)
10472 idata[k] = npoints;
10481 for (j = ylow; j < yhig; j++) {
10482 for (k = 1; k < nx; k++) {
10483 lambda1 = cdata[k - 1 + j*nx];
10484 lambda2 = cdata[k + j*nx];
10485 if (lambda1 < 1.0 || lambda2 < 1.0)
10487 cdata[k - 1 + j*nx] -= offset * (lambda2-lambda1);
10561 double firstLambda,
double lastLambda,
10562 cpl_table *idscoeff, cpl_vector *skylines,
10563 int highres,
int order,
10564 cpl_image *calibration,
int sradius)
10566 const char *func =
"mos_wavelength_align_lss";
10568 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
10574 double expPos, offset;
10576 double lambda1, lambda2;
10582 int startPos, endPos;
10583 int window = 2*sradius + 1;
10588 int idsorder, uorder;
10593 char name[MAX_COLNAME];
10594 char fname[MAX_COLNAME];
10596 cpl_polynomial *ids;
10597 cpl_polynomial *polycorr;
10598 cpl_table *offsets;
10599 cpl_table *fittable;
10606 if (idscoeff == NULL) {
10607 cpl_msg_error(func,
"An IDS coeff table must be given");
10608 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10612 if (image == NULL) {
10613 cpl_msg_error(func,
"A scientific spectral image must be given");
10614 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10619 line = cpl_vector_get_data(skylines);
10620 nlines = cpl_vector_get_size(skylines);
10623 cpl_msg_warning(func,
"A catalog of sky lines wavelengths was not "
10624 "given: using internal list of reference sky lines");
10626 line = default_lines_hi;
10627 nlines =
sizeof(default_lines_hi) /
sizeof(
double);
10630 line = default_lines_lo;
10631 nlines =
sizeof(default_lines_lo) /
sizeof(
double);
10636 cdata = cpl_image_get_data(calibration);
10638 nx = cpl_image_get_size_x(image);
10639 ny = cpl_image_get_size_y(image);
10641 sdata = cpl_image_get_data(image);
10643 if (ny != cpl_table_get_nrow(idscoeff)) {
10644 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
10658 for (i = 0; i < nlines; i++)
10659 if (line[i] > firstLambda && line[i] < lastLambda)
10662 offsets = cpl_table_new(nrows);
10663 cpl_table_new_column(offsets,
"wave", CPL_TYPE_DOUBLE);
10664 cpl_table_set_column_unit(offsets,
"wave",
"Angstrom");
10667 for (i = 0; i < nlines; i++) {
10668 if (line[i] > firstLambda && line[i] < lastLambda) {
10669 cpl_table_set_double(offsets,
"wave", nrows, line[i]);
10678 line = cpl_table_get_data_double(offsets,
"wave");
10682 while (idsorder < 6 && cpl_table_has_column(idscoeff, clab[idsorder]))
10692 dummy = cpl_table_new(ny);
10693 for (j = 0; j < nlines; j++) {
10694 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[j]);
10695 snprintf(fname, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10696 cpl_table_new_column(dummy, name, CPL_TYPE_DOUBLE);
10697 cpl_table_new_column(dummy, fname, CPL_TYPE_DOUBLE);
10700 for (j = 0; j < ny; j++, sdata += nx) {
10707 ids = cpl_polynomial_new(1);
10708 for (k = 0; k <= idsorder; k++) {
10709 c = cpl_table_get_double(idscoeff, clab[k], j, &missing);
10711 cpl_polynomial_delete(ids);
10714 cpl_polynomial_set_coeff(ids, &k, c);
10719 for (k = 0; k < nlines; k++) {
10720 expPos = cpl_polynomial_eval_1d(ids, line[k] - refwave, NULL);
10721 startPos = expPos - sradius;
10722 endPos = startPos + window;
10723 if (startPos < 0 || endPos >= nx)
10726 if (0 == peakPosition(sdata + startPos, window, &pos, 1)) {
10728 offset = pos - expPos;
10729 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[k]);
10730 cpl_table_set_double(dummy, name, j, offset);
10734 cpl_polynomial_delete(ids);
10743 for (j = 0; j < nlines; j++) {
10744 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[j]);
10745 snprintf(fname, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10746 if (cpl_table_has_valid(dummy, name)) {
10754 cpl_bivector *list;
10756 fittable = cpl_table_new(ny);
10757 cpl_table_new_column(fittable,
"row", CPL_TYPE_DOUBLE);
10758 cpl_table_set_column_unit(fittable,
"row",
"pixel");
10759 for (k = 0; k < ny; k++)
10760 cpl_table_set_double(fittable,
"row", k, k);
10761 cpl_table_duplicate_column(fittable,
"offset", dummy, name);
10762 npoints = ny - cpl_table_count_invalid(fittable,
"offset");
10763 cpl_table_erase_invalid(fittable);
10764 row = cpl_vector_wrap(npoints,
10765 cpl_table_get_data_double(fittable,
"row"));
10766 offs = cpl_vector_wrap(npoints,
10767 cpl_table_get_data_double(fittable,
"offset"));
10768 list = cpl_bivector_wrap_vectors(row, offs);
10769 robustLinearFit(list, &q, &m, &rms);
10770 cpl_bivector_unwrap_vectors(list);
10771 cpl_vector_unwrap(row);
10772 cpl_vector_unwrap(offs);
10773 cpl_table_delete(fittable);
10774 for (k = 0; k < ny; k++)
10775 cpl_table_set_double(dummy, fname, k, q + m*k);
10788 for (i = 0; i < ny; i++) {
10790 if (!cpl_table_is_valid(idscoeff, clab[0], i))
10794 for (j = 0; j < nlines; j++) {
10795 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10796 if (cpl_table_is_valid(dummy, name, i))
10804 if (npoints <= uorder)
10805 uorder = npoints - 1;
10813 wave = cpl_vector_new(npoints);
10814 wdata = cpl_vector_get_data(wave);
10815 offs = cpl_vector_new(npoints);
10816 odata = cpl_vector_get_data(offs);
10819 for (j = 0; j < nlines; j++) {
10820 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10821 if (cpl_table_is_valid(dummy, name, i)) {
10822 wdata[npoints] = line[j] - refwave;
10823 odata[npoints] = cpl_table_get_double(dummy, name, i, NULL);
10828 polycorr = cpl_polynomial_fit_1d_create(wave, offs, uorder, &rms);
10830 rms = sqrt(rms * (uorder + 1) / npoints);
10832 cpl_vector_delete(wave);
10833 cpl_vector_delete(offs);
10840 for (j = 0; j <= uorder; j++) {
10841 data = cpl_table_get_data_double(idscoeff, clab[j]);
10842 c = cpl_polynomial_get_coeff(polycorr, &j);
10846 data = cpl_table_get_data_double(idscoeff,
"error");
10847 data[i] = sqrt(data[i]*data[i] + rms*rms);
10849 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10850 idata[i] = npoints;
10858 for (k = 1; k < nx; k++) {
10859 lambda1 = cdata[k - 1 + i*nx];
10860 lambda2 = cdata[k + i*nx];
10861 if (lambda1 < 1.0 || lambda2 < 1.0)
10863 offset = cpl_polynomial_eval_1d(polycorr,
10864 lambda1-refwave, NULL);
10865 cdata[k - 1 + i*nx] -= offset * (lambda2-lambda1);
10869 cpl_polynomial_delete(polycorr);
10872 else if (uorder == 1) {
10878 cpl_bivector *list;
10881 wave = cpl_vector_new(npoints);
10882 wdata = cpl_vector_get_data(wave);
10883 offs = cpl_vector_new(npoints);
10884 odata = cpl_vector_get_data(offs);
10887 for (j = 0; j < nlines; j++) {
10888 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10889 if (cpl_table_is_valid(dummy, name, i)) {
10890 wdata[npoints] = line[j] - refwave;
10891 odata[npoints] = cpl_table_get_double(dummy, name, i, NULL);
10896 list = cpl_bivector_wrap_vectors(wave, offs);
10897 robustLinearFit(list, &q, &m, &rms);
10899 rms = sqrt(rms * (uorder + 1) / npoints);
10901 cpl_bivector_unwrap_vectors(list);
10902 cpl_vector_delete(wave);
10903 cpl_vector_delete(offs);
10910 for (j = 0; j <= uorder; j++) {
10911 data = cpl_table_get_data_double(idscoeff, clab[j]);
10919 data = cpl_table_get_data_double(idscoeff,
"error");
10920 data[i] = sqrt(data[i]*data[i] + rms*rms);
10922 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10923 idata[i] = npoints;
10931 for (k = 1; k < nx; k++) {
10932 lambda1 = cdata[k - 1 + i*nx];
10933 lambda2 = cdata[k + i*nx];
10934 if (lambda1 < 1.0 || lambda2 < 1.0)
10936 offset = q + m*(lambda1-refwave);
10937 cdata[k - 1 + i*nx] -= offset * (lambda2-lambda1);
10947 offs = cpl_vector_new(npoints);
10948 odata = cpl_vector_get_data(offs);
10951 for (j = 0; j < nlines; j++) {
10952 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10953 if (cpl_table_is_valid(dummy, name, i)) {
10954 odata[npoints] = cpl_table_get_double(dummy, name, i, NULL);
10959 offset = cpl_vector_get_median_const(offs);
10962 rms = cpl_vector_get_stdev(offs);
10964 else if (npoints == 1) {
10965 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[0]);
10966 if (cpl_table_has_valid(dummy, name)) {
10967 rms = cpl_table_get_column_stdev(dummy, name);
10968 rms /= sqrt(ny - cpl_table_count_invalid(dummy, name));
10978 rms /= sqrt(npoints);
10980 cpl_vector_delete(offs);
10987 data = cpl_table_get_data_double(idscoeff, clab[0]);
10990 data = cpl_table_get_data_double(idscoeff,
"error");
10991 data[i] = sqrt(data[i]*data[i] + rms*rms);
10993 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10994 idata[i] = npoints;
11003 for (k = 1; k < nx; k++) {
11004 lambda1 = cdata[k - 1 + i*nx];
11005 lambda2 = cdata[k + i*nx];
11006 if (lambda1 < 1.0 || lambda2 < 1.0)
11008 cdata[k - 1 + i*nx] -= offset * (lambda2-lambda1);
11015 for (j = 0; j < nlines; j++) {
11016 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[j]);
11017 if (cpl_table_has_valid(dummy, name)) {
11019 offset = cpl_table_get_column_median(dummy, name);
11020 cpl_msg_info(func,
"Median offset for %.3f: %.3f pixel",
11025 "Median offset for %.2f: not available", line[j]);
11029 cpl_table_delete(offsets);
11032 cpl_table_delete(dummy);
11069 double wavestart,
double dispersion,
int radius,
11073 const char *func =
"mos_distortions_rms";
11078 int cpix, npix, nzero;
11081 int npeaks, allPeaks;
11084 float peak, expectPeak, offset;
11088 double rms, oneRms;
11094 xlen = cpl_image_get_size_x(rectified);
11095 ylen = cpl_image_get_size_y(rectified);
11096 sdata = cpl_image_get_data(rectified);
11099 wdata = cpl_vector_get_data(lines);
11100 numLines = cpl_vector_get_size(lines);
11103 cpl_msg_warning(func,
"A catalog of sky lines wavelengths was not "
11104 "given: using internal list of reference sky lines");
11106 wdata = default_lines_hi;
11107 numLines =
sizeof(default_lines_hi) /
sizeof(
double);
11110 wdata = default_lines_lo;
11111 numLines =
sizeof(default_lines_lo) /
sizeof(
double);
11115 npix = 2 * radius + 1;
11116 profile = cpl_calloc(npix,
sizeof(
float));
11121 for (i = 0; i < numLines; i++) {
11128 expectPeak = (lambda - wavestart) / dispersion;
11129 cpix = floor(expectPeak + 0.5);
11135 sp = cpix - radius;
11136 ep = cpix + radius;
11138 if (sp < 0 || ep > xlen)
11145 for (j = 0; j < ylen; j++) {
11147 for (k = 0; k < npix; k++) {
11148 profile[k] = sdata[sp + k + j * xlen];
11149 if (fabs(profile[k]) < 0.0001)
11155 if (peakPosition(profile, npix, &peak, 1) == 0) {
11156 offset = (sp + peak) - expectPeak;
11158 rms += fabs(offset);
11159 oneRms += fabs(offset);
11166 cpl_msg_info(func,
"RMS for %.2f: %.3f pixel (%d points)",
11167 lambda, oneRms / npeaks * 1.25, npeaks);
11169 cpl_msg_info(func,
"RMS for %.2f: line not available", lambda);
11206 double blue,
double red,
double dispersion,
int trend)
11208 const char *func =
"mos_map_pixel";
11210 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11213 cpl_polynomial *ids;
11225 if (idscoeff == NULL) {
11226 cpl_msg_error(func,
"An IDS coeff table must be given");
11227 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11231 xsize = (red - blue) / dispersion;
11232 ysize = cpl_table_get_nrow(idscoeff);
11233 map = cpl_image_new(xsize, ysize, CPL_TYPE_FLOAT);
11234 mdata = cpl_image_get_data(map);
11237 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
11241 for (i = 0; i < ysize; i++, mdata += xsize) {
11244 ids = cpl_polynomial_new(1);
11245 for (k = trend; k <= order; k++) {
11246 c = cpl_table_get_double(idscoeff, clab[k], i, &missing);
11248 cpl_polynomial_delete(ids);
11251 cpl_polynomial_set_coeff(ids, &k, c);
11256 for (j = 0; j < xsize; j++) {
11257 lambda = blue + j*dispersion;
11258 mdata[j] = cpl_polynomial_eval_1d(ids, lambda-reference, NULL);
11261 cpl_polynomial_delete(ids);
11291 double blue,
double red)
11293 const char *func =
"mos_map_idscoeff";
11295 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11298 cpl_polynomial *ids;
11310 if (idscoeff == NULL) {
11311 cpl_msg_error(func,
"An IDS coeff table must be given");
11312 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11317 cpl_msg_error(func,
"Invalid image size");
11318 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11322 if (xsize < 20 || xsize > 5000) {
11323 cpl_msg_warning(func,
"Do you really have a detector %d pixels long?",
11327 ysize = cpl_table_get_nrow(idscoeff);
11328 map = cpl_image_new(xsize, ysize, CPL_TYPE_FLOAT);
11329 mdata = cpl_image_get_data(map);
11332 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
11336 for (i = 0; i < ysize; i++, mdata += xsize) {
11339 ids = cpl_polynomial_new(1);
11340 for (k = 0; k <= order; k++) {
11341 c = cpl_table_get_double(idscoeff, clab[k], i, &missing);
11343 cpl_polynomial_delete(ids);
11346 cpl_polynomial_set_coeff(ids, &k, c);
11351 for (j = 0; j < xsize; j++) {
11354 if (lambda >= blue && lambda <= red) {
11359 cpl_polynomial_delete(ids);
11402 cpl_table *slits, cpl_table *polytraces,
11403 double reference,
double blue,
double red,
11406 const char *func =
"mos_map_wavelengths";
11408 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11410 cpl_polynomial *polytop;
11411 cpl_polynomial *polybot;
11412 cpl_image *remapped;
11417 double vtop, vbot, value;
11424 int yint, ysize, yprev;
11431 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
11432 int missing_top, missing_bot;
11439 if (spatial == NULL || calibration == NULL ||
11440 slits == NULL || polytraces == NULL) {
11441 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11445 if (dispersion <= 0.0) {
11446 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11450 if (red - blue < dispersion) {
11451 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11455 nx = cpl_image_get_size_x(spatial);
11456 ny = cpl_image_get_size_y(spatial);
11457 ysize = cpl_image_get_size_y(calibration);
11458 remapped = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
11459 data = cpl_image_get_data(remapped);
11460 sdata = cpl_image_get_data(spatial);
11461 wdata = cpl_image_get_data(calibration);
11463 nslits = cpl_table_get_nrow(slits);
11464 slit_id = cpl_table_get_data_int(slits,
"slit_id");
11465 order = cpl_table_get_ncol(polytraces) - 2;
11466 position = cpl_table_get_data_int(slits,
"position");
11467 length = cpl_table_get_data_int(slits,
"length");
11474 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
11475 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
11477 for (i = 0; i < nslits; i++) {
11479 if (length[i] == 0)
11493 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
11495 start_pixel = refpixel - pixel_below;
11496 if (start_pixel < 0)
11499 end_pixel = refpixel + pixel_above;
11500 if (end_pixel > nx)
11509 polytop = cpl_polynomial_new(1);
11510 for (k = 0; k <= order; k++) {
11511 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
11513 cpl_polynomial_delete(polytop);
11517 cpl_polynomial_set_coeff(polytop, &k, coeff);
11521 polybot = cpl_polynomial_new(1);
11522 for (k = 0; k <= order; k++) {
11523 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
11525 cpl_polynomial_delete(polybot);
11529 cpl_polynomial_set_coeff(polybot, &k, coeff);
11532 if (missing_top && missing_bot) {
11533 cpl_msg_debug(func,
"Slit %d was not traced: no extraction!",
11545 cpl_msg_debug(func,
"Upper edge of slit %d was not traced: "
11546 "the spectral curvature of the lower edge "
11547 "is used instead.", slit_id[i]);
11548 polytop = cpl_polynomial_duplicate(polybot);
11549 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
11550 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
11552 coeff = cpl_polynomial_get_coeff(polybot, &k);
11553 coeff += ytop - ybot;
11554 cpl_polynomial_set_coeff(polytop, &k, coeff);
11558 cpl_msg_debug(func,
"Lower edge of slit %d was not traced: "
11559 "the spectral curvature of the upper edge "
11560 "is used instead.", slit_id[i]);
11561 polybot = cpl_polynomial_duplicate(polytop);
11562 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
11563 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
11565 coeff = cpl_polynomial_get_coeff(polytop, &k);
11566 coeff -= ytop - ybot;
11567 cpl_polynomial_set_coeff(polybot, &k, coeff);
11577 xdata = wdata + nx*position[i];
11578 npseudo = length[i] - 1;
11584 for (j = start_pixel; j < end_pixel; j++) {
11585 top = cpl_polynomial_eval_1d(polytop, j, NULL);
11586 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
11587 for (k = 0; k <= npseudo; k++) {
11588 ypos = top - k*(top-bot)/npseudo;
11598 if (yint < 0 || yint >= ny-1) {
11603 value = sdata[j + nx*yint];
11605 fvalue = value - ivalue;
11606 if (ivalue < npseudo && ivalue >= 0) {
11607 vtop = xdata[j + nx*(npseudo-ivalue)];
11608 vbot = xdata[j + nx*(npseudo-ivalue-1)];
11617 else if (vbot < 1.0) {
11623 else if (fabs(vbot-vtop) > 10*dispersion) {
11627 value = vtop*(1-fvalue) + vbot*fvalue;
11629 data[j + nx*yint] = value;
11639 if (yprev - yint > 1) {
11640 value = sdata[j + nx*(yint+1)];
11642 fvalue = value - ivalue;
11643 if (ivalue < npseudo && ivalue >= 0) {
11644 vtop = xdata[j + nx*(npseudo-ivalue)];
11645 vbot = xdata[j + nx*(npseudo-ivalue-1)];
11648 value = data[j + nx*(yint+1)];
11654 else if (vbot < 1.0) {
11657 else if (fabs(vbot-vtop) > 2*dispersion) {
11661 value = vtop*(1-fvalue) + vbot*fvalue;
11663 data[j + nx*(yint+1)] = value;
11671 cpl_polynomial_delete(polytop);
11672 cpl_polynomial_delete(polybot);
11752 cpl_image *spatial, cpl_table *slits,
11753 cpl_table *polytraces,
double reference,
11754 double blue,
double red,
double dispersion,
11757 const char *func =
"mos_map_spectrum";
11759 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11761 cpl_polynomial *polytop;
11762 cpl_polynomial *polybot;
11763 cpl_image *remapped;
11764 cpl_image **exslit;
11769 double lambda00, lambda01, lambda10, lambda11, lambda;
11770 double space00, space01, space10, space11, space;
11771 double value00, value01, value10, value11, value0, value1, value;
11776 double xfrac, yfrac;
11789 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
11790 int missing_top, missing_bot;
11799 if (spectra == NULL || spatial == NULL || wavecalib == NULL ||
11800 slits == NULL || polytraces == NULL) {
11801 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11805 if (dispersion <= 0.0) {
11806 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11810 if (red - blue < dispersion) {
11811 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11815 nx = cpl_image_get_size_x(spectra);
11816 ny = cpl_image_get_size_y(spectra);
11818 if (nx != cpl_image_get_size_x(spatial) ||
11819 ny != cpl_image_get_size_y(spatial) ||
11820 nx != cpl_image_get_size_x(wavecalib) ||
11821 ny != cpl_image_get_size_y(wavecalib)) {
11822 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
11826 nlambda = STRETCH_FACTOR * (red - blue) / dispersion;
11827 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
11828 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
11830 data = cpl_image_get_data(spectra);
11831 sdata = cpl_image_get_data(spatial);
11832 wdata = cpl_image_get_data(wavecalib);
11834 nslits = cpl_table_get_nrow(slits);
11835 slit_id = cpl_table_get_data_int(slits,
"slit_id");
11836 order = cpl_table_get_ncol(polytraces) - 2;
11837 position = cpl_table_get_data_int(slits,
"position");
11838 length = cpl_table_get_data_int(slits,
"length");
11840 exslit = cpl_calloc(nslits,
sizeof(cpl_image *));
11842 for (i = 0; i < nslits; i++) {
11858 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
11860 start_pixel = refpixel - pixel_below;
11861 if (start_pixel < 1)
11864 end_pixel = refpixel + pixel_above;
11865 if (end_pixel > nx)
11874 polytop = cpl_polynomial_new(1);
11875 for (k = 0; k <= order; k++) {
11876 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
11878 cpl_polynomial_delete(polytop);
11882 cpl_polynomial_set_coeff(polytop, &k, coeff);
11886 polybot = cpl_polynomial_new(1);
11887 for (k = 0; k <= order; k++) {
11888 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
11890 cpl_polynomial_delete(polybot);
11894 cpl_polynomial_set_coeff(polybot, &k, coeff);
11897 if (missing_top && missing_bot) {
11898 cpl_msg_debug(func,
"Slit %d was not traced: no extraction!",
11910 cpl_msg_debug(func,
"Upper edge of slit %d was not traced: "
11911 "the spectral curvature of the lower edge "
11912 "is used instead.", slit_id[i]);
11913 polytop = cpl_polynomial_duplicate(polybot);
11914 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
11915 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
11917 coeff = cpl_polynomial_get_coeff(polybot, &k);
11918 coeff += ytop - ybot;
11919 cpl_polynomial_set_coeff(polytop, &k, coeff);
11923 cpl_msg_debug(func,
"Lower edge of slit %d was not traced: "
11924 "the spectral curvature of the upper edge "
11925 "is used instead.", slit_id[i]);
11926 polybot = cpl_polynomial_duplicate(polytop);
11927 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
11928 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
11930 coeff = cpl_polynomial_get_coeff(polytop, &k);
11931 coeff -= ytop - ybot;
11932 cpl_polynomial_set_coeff(polybot, &k, coeff);
11939 top = cpl_polynomial_eval_1d(polytop, refpixel, NULL);
11940 bot = cpl_polynomial_eval_1d(polybot, refpixel, NULL);
11941 npseudo = ceil(top-bot) + 1;
11944 cpl_polynomial_delete(polytop);
11945 cpl_polynomial_delete(polybot);
11946 cpl_msg_debug(func,
"Slit %d was badly traced: no extraction!",
11951 exslit[i] = cpl_image_new(nlambda, npseudo+1, CPL_TYPE_FLOAT);
11952 xdata = cpl_image_get_data(exslit[i]);
11958 for (x = start_pixel; x < end_pixel; x++) {
11959 top = cpl_polynomial_eval_1d(polytop, x, NULL);
11960 bot = cpl_polynomial_eval_1d(polybot, x, NULL);
11971 for (y = ibot; y < itop; y++) {
11972 lambda11 = wdata[x + y*nx];
11973 if (lambda11 < 1.0)
11975 space11 = sdata[x + y*nx];
11978 lambda01 = wdata[x - 1 + y*nx];
11979 if (lambda01 < 1.0)
11981 space01 = sdata[x - 1 + y*nx];
12014 lambda10 = wdata[x + shift + (y+1)*nx];
12015 if (lambda10 < 1.0)
12017 space10 = sdata[x + shift + (y+1)*nx];
12020 lambda00 = wdata[x - 1 + shift + (y+1)*nx];
12021 if (lambda00 < 1.0)
12023 space00 = sdata[x - 1 + shift + (y+1)*nx];
12033 dL = lambda11 - lambda01;
12034 dS = space11 - space10;
12041 L = (lambda11 - blue)/dispersion + 0.5;
12044 if (L < 0 || L >= nlambda)
12046 if (S < 0 || S > npseudo)
12053 lambda = blue + L*dispersion;
12065 xfrac = (lambda11-lambda)/dL;
12066 yfrac = (space11-space)/dS;
12077 value11 = data[x + y*nx];
12078 value01 = data[x - 1 + y*nx];
12079 value10 = data[x + shift + (y+1)*nx];
12080 value00 = data[x + shift - 1 + (y+1)*nx];
12086 value1 = (1-xfrac)*value11 + xfrac*value01;
12087 value0 = (1-xfrac)*value10 + xfrac*value00;
12088 value = (1-yfrac)*value1 + yfrac*value0;
12095 xdata[L + nlambda*(npseudo-S)] = value;
12099 cpl_polynomial_delete(polytop);
12100 cpl_polynomial_delete(polybot);
12108 for (i = 0; i < nslits; i++)
12110 ysize += cpl_image_get_size_y(exslit[i]);
12112 remapped = cpl_image_new(nlambda, ysize, CPL_TYPE_FLOAT);
12115 for (i = 0; i < nslits; i++) {
12117 yint += cpl_image_get_size_y(exslit[i]);
12118 cpl_image_copy(remapped, exslit[i], 1, ysize - yint);
12119 cpl_image_delete(exslit[i]);
12120 cpl_table_set_int(slits,
"position", i, ysize - yint - 1);
12164 double dispersion,
double factor,
int minpoints,
12167 const char *func =
"mos_sky_map_super";
12169 cpl_vector **vector;
12170 cpl_vector **wvector;
12171 double firstLambda, lastLambda;
12172 double lambda, lambda1, lambda2;
12173 double value, value1, value2;
12179 int first_valid, valid_bins;
12183 double *sky_spectrum;
12190 if (spectra == NULL || wavemap == NULL || skymap == NULL) {
12191 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12195 if (dispersion <= 0.0) {
12196 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12197 cpl_msg_error(func,
"Negative dispersion: %s", cpl_error_get_message());
12201 nx = cpl_image_get_size_x(spectra);
12202 ny = cpl_image_get_size_y(spectra);
12205 if (nx != cpl_image_get_size_x(wavemap) ||
12206 ny != cpl_image_get_size_y(wavemap) ||
12207 nx != cpl_image_get_size_x(skymap) ||
12208 ny != cpl_image_get_size_y(skymap)) {
12209 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
12210 cpl_msg_error(func,
"Image sizes: %s", cpl_error_get_message());
12214 if (factor < 1.0) {
12215 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12216 cpl_msg_error(func,
"Undersampling (%f): %s", factor,
12217 cpl_error_get_message());
12221 if (minpoints < 0) {
12222 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12223 cpl_msg_error(func,
"Negative threshold: %s", cpl_error_get_message());
12227 dispersion /= factor;
12234 data = cpl_image_get_data(wavemap);
12236 for (i = 0; i < npix; i++) {
12237 if (data[i] > 1.0) {
12238 min = max = data[i];
12244 for (i = j; i < npix; i++) {
12261 nbin = (lastLambda - firstLambda) / dispersion;
12270 count = cpl_calloc(nbin,
sizeof(
int));
12272 data = cpl_image_get_data(wavemap);
12274 for (i = 0; i < npix; i++) {
12277 bin = (data[i] - firstLambda) / dispersion;
12283 for (i = 0; i < nbin; i++)
12284 if (count[i] >= minpoints)
12287 if (valid_bins < nbin/3) {
12288 cpl_msg_warning(func,
"Cannot determine a good global sky "
12289 "spectrum from input data");
12301 vector = cpl_calloc(nbin,
sizeof(cpl_vector *));
12302 wvector = cpl_calloc(nbin,
sizeof(cpl_vector *));
12303 for (i = 0; i < nbin; i++) {
12304 if (count[i] >= minpoints) {
12305 vector[i] = cpl_vector_new(count[i]);
12306 wvector[i] = cpl_vector_new(count[i]);
12317 data = cpl_image_get_data(wavemap);
12318 sdata = cpl_image_get_data(spectra);
12320 for (i = 0; i < npix; i++) {
12323 bin = (data[i] - firstLambda) / dispersion;
12326 cpl_vector_set(vector[bin], count[bin], sdata[i]);
12327 cpl_vector_set(wvector[bin], count[bin], data[i]);
12339 sky_spectrum = cpl_calloc(nbin,
sizeof(
double));
12340 sky_wave = cpl_calloc(nbin,
sizeof(
double));
12341 for (i = 0; i < nbin; i++) {
12343 sky_spectrum[i] = cpl_vector_get_median_const(vector[i]);
12344 sky_wave[i] = cpl_vector_get_median_const(wvector[i]);
12345 cpl_vector_delete(vector[i]);
12346 cpl_vector_delete(wvector[i]);
12358 for (i = 0; i < nbin; i++) {
12359 if (count[i] >= minpoints) {
12365 for (i = first_valid; i < nbin; i++) {
12366 if (count[i] < minpoints) {
12367 sky_wave[i] = firstLambda + (i+0.5)*dispersion;
12368 for (j = i+1; j < nbin; j++) {
12369 if (count[j] >= minpoints) {
12370 if (sky_wave[j] - sky_wave[i-1] < 0.1) {
12371 sky_spectrum[i] = (sky_spectrum[j] + sky_spectrum[i-1])
12375 frac = (sky_wave[i] - sky_wave[i-1])
12376 / (sky_wave[j] - sky_wave[i-1]);
12377 sky_spectrum[i] = frac * sky_spectrum[j]
12378 + (1 - frac) * sky_spectrum[i-1];
12390 sky = cpl_table_new(nbin);
12391 cpl_table_wrap_double(sky, sky_wave,
"wavelength");
12392 cpl_table_wrap_double(sky, sky_spectrum,
"sky");
12393 cpl_table_wrap_int(sky, count,
"npoints");
12400 data = cpl_image_get_data(wavemap);
12401 sdata = cpl_image_get_data(spectra);
12402 kdata = cpl_image_get_data(skymap);
12404 for (i = 0; i < npix; i++) {
12413 bin = (lambda - firstLambda) / dispersion;
12414 lambda1 = sky_wave[bin];
12415 value1 = sky_spectrum[bin];
12416 if (lambda1 < lambda) {
12419 lambda2 = sky_wave[bin];
12420 value2 = sky_spectrum[bin];
12421 if (lambda2 - lambda1 < 0.1) {
12422 value = (value1 + value2) / 2;
12425 frac = (lambda - lambda1) / (lambda2 - lambda1);
12426 value = frac * value2 + (1 - frac) * value1;
12438 lambda1 = sky_wave[bin];
12439 value1 = sky_spectrum[bin];
12440 if (lambda2 - lambda1 < 0.1) {
12441 value = (value1 + value2) / 2;
12444 frac = (lambda - lambda1) / (lambda2 - lambda1);
12445 value = frac * value2 + (1 - frac) * value1;
12456 cpl_table_erase_window(sky, 0, first_valid);
12497 double dispersion, cpl_image *skymap)
12499 const char *func =
"mos_sky_map";
12501 cpl_vector **vector;
12502 double firstLambda, lastLambda;
12503 double lambda, lambda1, lambda2;
12504 double value, value1, value2;
12512 double *sky_spectrum;
12519 if (spectra == NULL || wavemap == NULL || skymap == NULL) {
12520 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12524 if (dispersion <= 0.0) {
12525 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12529 nx = cpl_image_get_size_x(spectra);
12530 ny = cpl_image_get_size_y(spectra);
12533 if (nx != cpl_image_get_size_x(wavemap) ||
12534 ny != cpl_image_get_size_y(wavemap) ||
12535 nx != cpl_image_get_size_x(skymap) ||
12536 ny != cpl_image_get_size_y(skymap)) {
12537 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
12546 data = cpl_image_get_data(wavemap);
12548 for (i = 0; i < npix; i++) {
12549 if (data[i] > 1.0) {
12550 min = max = data[i];
12556 for (i = j; i < npix; i++) {
12573 nbin = (lastLambda - firstLambda) / dispersion;
12582 count = cpl_calloc(nbin,
sizeof(
int));
12584 data = cpl_image_get_data(wavemap);
12586 for (i = 0; i < npix; i++) {
12589 bin = (data[i] - firstLambda) / dispersion;
12602 vector = cpl_calloc(nbin,
sizeof(cpl_vector *));
12603 for (i = 0; i < nbin; i++) {
12605 vector[i] = cpl_vector_new(count[i]);
12617 data = cpl_image_get_data(wavemap);
12618 sdata = cpl_image_get_data(spectra);
12620 for (i = 0; i < npix; i++) {
12623 bin = (data[i] - firstLambda) / dispersion;
12625 cpl_vector_set(vector[bin], count[bin], sdata[i]);
12636 sky_spectrum = cpl_calloc(nbin,
sizeof(
double));
12637 for (i = 0; i < nbin; i++) {
12639 sky_spectrum[i] = cpl_vector_get_median_const(vector[i]);
12640 cpl_vector_delete(vector[i]);
12658 sky = cpl_table_new(nbin);
12659 cpl_table_new_column(sky,
"wavelength", CPL_TYPE_DOUBLE);
12660 cpl_table_set_column_unit(sky,
"wavelength",
"pixel");
12661 cpl_table_wrap_double(sky, sky_spectrum,
"sky");
12662 cpl_table_wrap_int(sky, count,
"npoints");
12663 for (i = 0; i < nbin; i++)
12664 cpl_table_set_double(sky,
"wavelength", i,
12665 firstLambda + (i+0.5)*dispersion);
12672 data = cpl_image_get_data(wavemap);
12673 sdata = cpl_image_get_data(spectra);
12674 kdata = cpl_image_get_data(skymap);
12675 wdata = cpl_table_get_data_double(sky,
"wavelength");
12677 for (i = 0; i < npix; i++) {
12686 bin = (lambda - firstLambda) / dispersion;
12687 lambda1 = wdata[bin];
12688 value1 = sky_spectrum[bin];
12689 if (lambda1 < lambda) {
12692 lambda2 = wdata[bin];
12693 value2 = sky_spectrum[bin];
12694 value = ((lambda2 - lambda)*value1
12695 + (lambda - lambda1)*value2) / dispersion;
12706 lambda1 = wdata[bin];
12707 value1 = sky_spectrum[bin];
12708 value = ((lambda2 - lambda)*value1
12709 + (lambda - lambda1)*value2)/dispersion;
12740 const char *func =
"mos_sky_local_old";
12748 int xlow, ylow, xhig, yhig;
12756 if (spectra == NULL) {
12757 cpl_msg_error(func,
12758 "A scientific rectified spectral image must be given");
12759 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12763 if (slits == NULL) {
12764 cpl_msg_error(func,
"A slits position table must be given");
12765 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12769 nslits = cpl_table_get_nrow(slits);
12770 slit_id = cpl_table_get_data_int(slits,
"slit_id");
12771 position = cpl_table_get_data_int(slits,
"position");
12772 length = cpl_table_get_data_int(slits,
"length");
12774 nx = cpl_image_get_size_x(spectra);
12775 ny = cpl_image_get_size_y(spectra);
12777 skymap = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
12781 for (i = 0; i < nslits; i++) {
12783 if (length[i] == 0)
12796 ylow = position[i] + 1;
12797 yhig = ylow + length[i] - 1;
12799 exslit = cpl_image_extract(spectra, xlow, ylow, xhig, yhig);
12800 sky = cpl_image_collapse_median_create(exslit, 0, 0, 1);
12801 cpl_image_delete(exslit);
12803 data = cpl_image_get_data(skymap);
12804 data += nx * position[i];
12806 for (j = 0; j < length[i]; j++) {
12807 sdata = cpl_image_get_data(sky);
12808 for (k = 0; k < nx; k++) {
12809 *data++ = *sdata++;
12813 cpl_image_delete(sky);
12842 const char *func =
"mos_sky_local";
12844 char name[MAX_COLNAME];
12846 cpl_polynomial *fit;
12847 cpl_vector *points;
12848 cpl_vector *values;
12849 cpl_vector *keep_points;
12850 cpl_vector *keep_values;
12853 cpl_image *subtracted;
12854 cpl_image *profile;
12856 cpl_table *objects;
12864 int xlow, ylow, xhig, yhig;
12877 if (spectra == NULL) {
12878 cpl_msg_error(func,
12879 "A scientific rectified spectral image must be given");
12880 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12884 if (slits == NULL) {
12885 cpl_msg_error(func,
"A slits position table must be given");
12886 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12891 cpl_msg_error(func,
"Invalid fit order");
12892 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12896 nslits = cpl_table_get_nrow(slits);
12897 slit_id = cpl_table_get_data_int(slits,
"slit_id");
12898 position = cpl_table_get_data_int(slits,
"position");
12899 length = cpl_table_get_data_int(slits,
"length");
12901 nx = cpl_image_get_size_x(spectra);
12902 ny = cpl_image_get_size_y(spectra);
12904 skymap = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
12908 for (i = 0; i < nslits; i++) {
12910 if (length[i] == 0)
12923 ylow = position[i] + 1;
12924 yhig = ylow + length[i] - 1;
12926 exslit = cpl_image_extract(spectra, xlow, ylow, xhig, yhig);
12927 sky = cpl_image_collapse_median_create(exslit, 0, 0, 1);
12928 cpl_image_delete(exslit);
12930 data = cpl_image_get_data(skymap);
12931 data += nx * position[i];
12933 for (j = 0; j < length[i]; j++) {
12934 sdata = cpl_image_get_data(sky);
12935 for (k = 0; k < nx; k++) {
12936 *data++ = *sdata++;
12940 cpl_image_delete(sky);
12948 subtracted = cpl_image_duplicate(spectra);
12949 cpl_image_subtract(subtracted, skymap);
12950 cpl_image_delete(skymap);
12957 objects = cpl_table_duplicate(slits);
12959 cpl_image_delete(profile);
12960 cpl_image_delete(subtracted);
12969 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
12970 while (cpl_table_has_column(objects, name)) {
12972 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
12975 is_sky = cpl_calloc(ny,
sizeof(
int));
12977 for (i = 0; i < nslits; i++) {
12979 if (length[i] == 0)
12982 ylow = position[i] + margin;
12983 yhig = position[i] + length[i] - margin;
12985 for (j = ylow; j < yhig; j++)
12988 for (j = 1; j < maxobjects; j++) {
12989 snprintf(name, MAX_COLNAME,
"object_%d", j);
12990 if (cpl_table_is_valid(objects, name, i)) {
12991 snprintf(name, MAX_COLNAME,
"start_%d", j);
12992 ylow = cpl_table_get_int(objects, name, i, NULL);
12993 snprintf(name, MAX_COLNAME,
"end_%d", j);
12994 yhig = cpl_table_get_int(objects, name, i, NULL);
12995 for (k = ylow; k <= yhig; k++)
13005 ylow = position[i] + margin + 1;
13006 yhig = position[i] + length[i] - margin - 1;
13008 for (j = ylow; j < yhig; j++)
13010 if (is_sky[j-1] == 0 && is_sky[j+1] == 0)
13020 skymap = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
13022 for (i = 0; i < nslits; i++) {
13024 if (length[i] == 0)
13027 ylow = position[i];
13028 yhig = ylow + length[i];
13031 for (j = ylow; j < yhig; j++)
13035 if (nsky > order + 1) {
13037 points = cpl_vector_new(nsky);
13039 for (j = ylow; j < yhig; j++) {
13041 cpl_vector_set(points, nsky, j);
13046 exslit = cpl_image_extract(spectra, 1, ylow+1, nx, yhig);
13047 xdata = cpl_image_get_data(exslit);
13048 values = cpl_vector_new(nsky);
13050 for (j = 0; j < nx; j++) {
13052 for (k = ylow; k < yhig; k++) {
13054 cpl_vector_set(values, nsky, xdata[j+(k-ylow)*nx]);
13063 median = cpl_vector_get_median_const(values);
13064 vdata = cpl_vector_get_data(values);
13065 pdata = cpl_vector_get_data(points);
13067 for (k = 0; k < nsky; k++) {
13068 if (fabs(vdata[k] - median) < 100) {
13070 vdata[k-nbad] = vdata[k];
13071 pdata[k-nbad] = pdata[k];
13081 if (nbad && nsky - nbad > order + 1) {
13082 keep_values = values;
13083 keep_points = points;
13084 values = cpl_vector_wrap(nsky-nbad, vdata);
13085 points = cpl_vector_wrap(nsky-nbad, pdata);
13088 if (nsky - nbad > order + 1) {
13090 fit = cpl_polynomial_fit_1d_create(points, values,
13094 for (k = ylow; k < yhig; k++) {
13095 xdata[j+(k-ylow)*nx] =
13096 cpl_polynomial_eval_1d(fit, k, NULL);
13099 cpl_polynomial_delete(fit);
13105 for (k = 0; k < nsky; k++) {
13106 xdata[j+k*nx] = median;
13110 if (nbad && nsky - nbad > order + 1) {
13111 cpl_vector_unwrap(values);
13112 cpl_vector_unwrap(points);
13113 values = keep_values;
13114 points = keep_points;
13119 for (k = ylow; k < yhig; k++) {
13121 cpl_vector_set(points, nsky, k);
13129 cpl_vector_delete(values);
13130 cpl_vector_delete(points);
13132 cpl_image_copy(skymap, exslit, 1, ylow+1);
13133 cpl_image_delete(exslit);
13137 exslit = cpl_image_extract(spectra, 1, ylow+1, nx, yhig);
13138 xdata = cpl_image_get_data(exslit);
13139 values = cpl_vector_new(nsky);
13141 for (j = 0; j < nx; j++) {
13143 for (k = ylow; k < yhig; k++) {
13145 cpl_vector_set(values, nsky, xdata[j+(k-ylow)*nx]);
13150 median = cpl_vector_get_median_const(values);
13152 for (k = ylow; k < yhig; k++)
13153 xdata[j+(k-ylow)*nx] = median;
13157 cpl_vector_delete(values);
13159 cpl_image_copy(skymap, exslit, 1, ylow+1);
13160 cpl_image_delete(exslit);
13164 cpl_msg_warning(func,
"Too few sky points in slit %d", i + 1);
13196 float threshold,
float ratio)
13198 const char *func =
"mos_clean_cosmics";
13200 cpl_image *smoothImage;
13202 cpl_matrix *kernel;
13207 float sigma, sum, value, smoothValue;
13211 int iMin, iMax, jMin, jMax, iPosMax, jPosMax;
13217 int pos, i, j, k, l, ii, jj, iii = 0, jjj = 0;
13219 int found, foundContiguousCandidate;
13224 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13237 xLen = cpl_image_get_size_x(image);
13238 yLen = cpl_image_get_size_y(image);
13240 if (xLen < 4 || yLen < 4)
13241 return CPL_ERROR_NONE;
13243 nPix = xLen * yLen;
13266 idata = cpl_image_get_data(image);
13270 for (i = 0; i < nPix; i++) {
13271 if (idata[i] < -0.00001) {
13280 cosmic = cpl_calloc(nPix,
sizeof(
int));
13282 if (threshold < 0.)
13287 kernel = cpl_matrix_new(3, 3);
13288 cpl_matrix_fill(kernel, 1.0);
13289 cpl_matrix_set(kernel, 1, 1, 0.0);
13290 smoothImage = cpl_image_filter_median(image, kernel);
13291 cpl_matrix_delete(kernel);
13302 sdata = cpl_image_get_data(smoothImage);
13304 for (j = 1; j < yLen - 1; j++) {
13305 for (i = 1; i < xLen - 1; i++) {
13306 value = idata[i + j * xLen];
13307 smoothValue = sdata[i + j * xLen];
13308 if (smoothValue < 1.0)
13310 sigma = sqrt(noise * noise + smoothValue / gain);
13311 if (value - smoothValue >= threshold * sigma)
13312 cosmic[i + j * xLen] = -1;
13316 cpl_image_delete(smoothImage);
13325 for (pos = first; pos < nPix; pos++) {
13326 if (cosmic[pos] == -1) {
13346 iMin = iMax = iPosMax = i;
13347 jMin = jMax = jPosMax = j;
13348 fMax = idata[i + j * xLen];
13351 foundContiguousCandidate = 0;
13352 for (l = 0; l <= 1; l++) {
13353 for (k = 0; k <= 1; k++) {
13360 jj = j + k + l - 1;
13361 if (cosmic[ii + jj * xLen] == -1) {
13362 foundContiguousCandidate = 1;
13363 cosmic[ii + jj * xLen] = 2;
13381 if (idata[ii + jj * xLen] > fMax) {
13382 fMax = idata[ii + jj * xLen];
13395 cosmic[i + j * xLen] = 3;
13397 if (foundContiguousCandidate) {
13419 for (l = jMin; l <= jMax; l++) {
13420 for (k = iMin; k <= iMax; k++) {
13421 if (cosmic[k + l * xLen] == 2) {
13424 foundContiguousCandidate = 1;
13428 if (foundContiguousCandidate)
13431 }
while (foundContiguousCandidate);
13440 for (l = -1; l <= 1; l++) {
13441 for (k = -1; k <= 1; k++) {
13442 if (l != 0 || k != 0) {
13443 sum += idata[iPosMax + k + (jPosMax + l) * xLen];
13449 if (fMax > ratio * sum) {
13450 for (l = jMin - 1; l <= jMax + 1; l++) {
13451 for (k = iMin - 1; k <= iMax + 1; k++) {
13452 if (cosmic[k + l * xLen] == 3) {
13453 cosmic[k + l * xLen] = 1;
13460 for (l = jMin - 1; l <= jMax + 1; l++) {
13461 for (k = iMin - 1; k <= iMax + 1; k++) {
13462 if (cosmic[k + l * xLen] != -1) {
13463 if (cosmic[k + l * xLen] == 1)
13465 cosmic[k + l * xLen] = 0;
13478 table = cpl_table_new(numCosmic);
13479 cpl_table_new_column(table,
"x", CPL_TYPE_INT);
13480 cpl_table_new_column(table,
"y", CPL_TYPE_INT);
13481 cpl_table_set_column_unit(table,
"x",
"pixel");
13482 cpl_table_set_column_unit(table,
"y",
"pixel");
13483 xdata = cpl_table_get_data_int(table,
"x");
13484 ydata = cpl_table_get_data_int(table,
"y");
13486 for (pos = 0, i = 0; pos < nPix; pos++) {
13487 if (cosmic[pos] == 1) {
13488 xdata[i] = (pos % xLen);
13489 ydata[i] = (pos / xLen);
13494 mos_clean_bad_pixels(image, table, 1);
13497 cpl_table_delete(table);
13499 return CPL_ERROR_NONE;
13504 cpl_error_code mos_clean_bad_pixels(cpl_image *image, cpl_table *table,
13507 const char *func =
"mos_clean_cosmics";
13512 int xlen, ylen, totPix;
13513 int nBadPixels = 0;
13514 int sign, foundFirst;
13515 int *xValue = NULL;
13516 int *yValue = NULL;
13522 int sx[] = {0, 1, 1, 1};
13523 int sy[] = {1,-1, 0, 1};
13524 int searchHorizon = 100;
13528 if (image == NULL || table == NULL)
13529 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13531 if (1 != cpl_table_has_column(table,
"x"))
13532 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
13534 if (1 != cpl_table_has_column(table,
"y"))
13535 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
13537 if (CPL_TYPE_INT != cpl_table_get_column_type(table,
"x"))
13538 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
13540 if (CPL_TYPE_INT != cpl_table_get_column_type(table,
"y"))
13541 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
13543 nBadPixels = cpl_table_get_nrow(table);
13546 xlen = cpl_image_get_size_x(image);
13547 ylen = cpl_image_get_size_y(image);
13548 idata = cpl_image_get_data(image);
13549 totPix = xlen * ylen;
13550 if (((
float) nBadPixels) / ((
float) totPix) < percent/100.) {
13551 isBadPix = cpl_calloc(totPix,
sizeof(
int));
13554 cpl_msg_warning(func,
"Too many bad pixels (> %d%%): "
13555 "skip bad pixel correction", percent);
13556 return cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13560 cpl_msg_debug(func,
"No pixel values to interpolate");
13561 return CPL_ERROR_NONE;
13564 xValue = cpl_table_get_data_int(table,
"x");
13565 yValue = cpl_table_get_data_int(table,
"y");
13567 for (i = 0; i < nBadPixels; i++)
13568 isBadPix[xValue[i] + yValue[i] * xlen] = 1;
13570 for (i = 0; i < nBadPixels; i++) {
13585 for (j = 0; j < 4; j++) {
13591 estimate[nPairs] = 0.;
13594 for (k = 0; k < 2; k++) {
13600 cx += sign * sx[j];
13601 cy += sign * sy[j];
13602 if (cx < 0 || cx >= xlen || cy < 0 || cy >= ylen)
13605 }
while (isBadPix[cx + cy * xlen] && d < searchHorizon);
13607 if (cx >= 0 && cx < xlen &&
13608 cy >= 0 && cy < ylen && d < searchHorizon) {
13614 save = idata[cx + cy * xlen];
13615 estimate[nPairs] += save / d;
13616 sumd += 1. / (double) d;
13618 estimate[nPairs] /= sumd;
13633 estimate[nPairs] = save;
13648 idata[xValue[i] + yValue[i] * xlen] =
13649 cpl_tools_get_median_float(estimate, nPairs);
13651 else if (nPairs == 2) {
13652 idata[xValue[i] + yValue[i] * xlen] =
13653 (estimate[0] + estimate[1]) / 2.;
13655 else if (nPairs == 1) {
13656 idata[xValue[i] + yValue[i] * xlen] = estimate[0];
13659 cpl_msg_debug(func,
"Cannot correct bad pixel %d,%d\n",
13660 xValue[i], yValue[i]);
13664 cpl_free(isBadPix);
13666 return CPL_ERROR_NONE;
13700 cpl_table *polytraces,
double reference,
13701 double blue,
double red,
double dispersion)
13703 const char *func =
"mos_spatial_map";
13705 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
13707 cpl_polynomial *polytop;
13708 cpl_polynomial *polybot;
13709 cpl_image *calibration;
13722 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
13723 int missing_top, missing_bot;
13730 if (spectra == NULL || slits == NULL || polytraces == NULL) {
13731 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13735 if (dispersion <= 0.0) {
13736 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13740 if (red - blue < dispersion) {
13741 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13745 nx = cpl_image_get_size_x(spectra);
13746 ny = cpl_image_get_size_y(spectra);
13748 calibration = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
13749 data = cpl_image_get_data(calibration);
13751 length = cpl_table_get_data_int(slits,
"length");
13752 nslits = cpl_table_get_nrow(slits);
13753 slit_id = cpl_table_get_data_int(slits,
"slit_id");
13754 order = cpl_table_get_ncol(polytraces) - 2;
13761 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
13762 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
13764 for (i = 0; i < nslits; i++) {
13766 if (length[i] == 0)
13780 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
13782 start_pixel = refpixel - pixel_below;
13783 if (start_pixel < 0)
13786 end_pixel = refpixel + pixel_above;
13787 if (end_pixel > nx)
13796 polytop = cpl_polynomial_new(1);
13797 for (k = 0; k <= order; k++) {
13798 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
13800 cpl_polynomial_delete(polytop);
13804 cpl_polynomial_set_coeff(polytop, &k, coeff);
13808 polybot = cpl_polynomial_new(1);
13809 for (k = 0; k <= order; k++) {
13810 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
13812 cpl_polynomial_delete(polybot);
13816 cpl_polynomial_set_coeff(polybot, &k, coeff);
13819 if (missing_top && missing_bot) {
13820 cpl_msg_warning(func,
"Spatial map, slit %d was not traced!",
13832 cpl_msg_warning(func,
"Upper edge of slit %d was not traced: "
13833 "the spectral curvature of the lower edge "
13834 "is used instead.", slit_id[i]);
13835 polytop = cpl_polynomial_duplicate(polybot);
13836 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
13837 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
13839 coeff = cpl_polynomial_get_coeff(polybot, &k);
13840 coeff += ytop - ybot;
13841 cpl_polynomial_set_coeff(polytop, &k, coeff);
13845 cpl_msg_warning(func,
"Lower edge of slit %d was not traced: "
13846 "the spectral curvature of the upper edge "
13847 "is used instead.", slit_id[i]);
13848 polybot = cpl_polynomial_duplicate(polytop);
13849 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
13850 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
13852 coeff = cpl_polynomial_get_coeff(polytop, &k);
13853 coeff -= ytop - ybot;
13854 cpl_polynomial_set_coeff(polybot, &k, coeff);
13857 top = cpl_polynomial_eval_1d(polytop, refpixel, NULL);
13858 bot = cpl_polynomial_eval_1d(polybot, refpixel, NULL);
13859 npseudo = ceil(top-bot) + 1;
13862 cpl_polynomial_delete(polytop);
13863 cpl_polynomial_delete(polybot);
13864 cpl_msg_warning(func,
"Slit %d was badly traced: no extraction!",
13869 for (j = start_pixel; j < end_pixel; j++) {
13870 top = cpl_polynomial_eval_1d(polytop, j, NULL);
13871 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
13872 factor = (top-bot)/npseudo;
13873 for (k = 0; k <= npseudo; k++) {
13874 ypos = top - k*factor;
13876 yfra = ypos - yint;
13877 if (yint >= 0 && yint < ny-1) {
13878 data[j + nx*yint] = (top-yint)/factor;
13887 if (yprev - yint > 1) {
13888 data[j + nx*(yint+1)] = (top-yint-1)/factor;
13895 cpl_polynomial_delete(polytop);
13896 cpl_polynomial_delete(polybot);
13899 return calibration;
13966 int maxradius,
int conradius)
13968 const char *func =
"mos_detect_objects";
13970 cpl_image *profile;
13974 char name[MAX_COLNAME];
13978 int nobjects, objpos, totobj;
13985 double mindistance;
13992 const int min_pixels = 10;
13995 if (cpl_error_get_code() != CPL_ERROR_NONE)
13998 if (image == NULL || slits == NULL) {
13999 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14006 if (maxradius < 0) {
14007 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14011 if (conradius < 0) {
14012 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14016 nslits = cpl_table_get_nrow(slits);
14017 position = cpl_table_get_data_int(slits,
"position");
14018 length = cpl_table_get_data_int(slits,
"length");
14020 profile = cpl_image_collapse_create(image, 1);
14021 cpl_image_divide_scalar(profile, cpl_image_get_size_x(image));
14022 pdata = cpl_image_get_data(profile);
14027 for (i = 0; i < nslits; i++) {
14029 if (length[i] == 0)
14032 pos = position[i] + margin;
14033 count = length[i] - 2*margin;
14035 if (count < min_pixels)
14046 if (p[0] > p[1] && p[1] > p[2] && p[2] > p[3] && p[3] > 0) {
14051 for (j = 0; j < count - 3; j++) {
14053 if (p[j+1] > p[j]) {
14058 if (p[j+1] > p[j+2] && p[j+2] > 0) {
14064 if (p[j+1] > p[j+2] && p[j+2] > p[j+3] && p[j+3] > 0) {
14077 if (p[count-1] > p[count-2] && p[count-2] > p[count-3]
14078 && p[count-3] > p[count-4] && p[count-4] > 0) {
14090 reject = cpl_calloc(npeaks,
sizeof(
int));
14091 bright = cpl_calloc(npeaks,
sizeof(
double));
14092 place = cpl_calloc(npeaks,
sizeof(
double));
14095 if (p[0] > p[1] && p[1] > p[2] && p[2] > p[3] && p[3] > 0) {
14097 place[0] = position[i] + margin;
14102 for (j = 0; j < count - 3; j++) {
14104 if (p[j+1] > p[j]) {
14109 if (p[j+1] > p[j+2] && p[j+2] > 0) {
14111 bright[npeaks] = p[j];
14112 place[npeaks] = position[i] + margin + j + 1
14113 + values_to_dx(p[j-1], p[j], p[j+1]);
14119 if (p[j+1] > p[j+2] && p[j+2] > p[j+3] && p[j+3] > 0) {
14121 bright[npeaks] = p[j];
14122 place[npeaks] = position[i] + margin + j + 1
14123 + values_to_dx(p[j-1], p[j], p[j+1]);
14136 if (p[count-1] > p[count-2] && p[count-2] > p[count-3]
14137 && p[count-3] > p[count-4] && p[count-4] > 0) {
14138 bright[npeaks] = p[count-1];
14139 place[npeaks] = position[i] + count;
14148 if (fabs(place[0] - pos) < 1.0)
14150 if (fabs(place[npeaks-1] - pos - count) < 1.0)
14151 reject[npeaks-1] = 1;
14152 for (j = 0; j < npeaks; j++) {
14153 for (k = 0; k < npeaks; k++) {
14156 mindistance = conradius * bright[k] / bright[j]
14157 * bright[k] / bright[j];
14158 if (fabs(place[j] - place[k]) < mindistance)
14164 for (j = 0; j < npeaks; j++) {
14168 low = (place[j-1]*bright[j] + place[j]*bright[j-1])
14169 / (bright[j-1] + bright[j]) + 1;
14174 if (j < npeaks - 1) {
14175 hig = (place[j+1]*bright[j] + place[j]*bright[j+1])
14176 / (bright[j+1] + bright[j]) + 1;
14184 if (hig > pos + count)
14186 if (place[j] - low > maxradius)
14187 low = place[j] - maxradius;
14188 if (hig - place[j] > maxradius)
14189 hig = place[j] + maxradius;
14196 for (j = 0; j < npeaks; j++)
14200 for (j = 0; j < nobjects; j++) {
14201 snprintf(name, MAX_COLNAME,
"object_%d", j+1);
14202 if (cpl_table_has_column(slits, name))
14204 cpl_table_new_column(slits, name, CPL_TYPE_DOUBLE);
14205 snprintf(name, MAX_COLNAME,
"start_%d", j+1);
14206 cpl_table_new_column(slits, name, CPL_TYPE_INT);
14207 cpl_table_set_column_unit(slits, name,
"pixel");
14208 snprintf(name, MAX_COLNAME,
"end_%d", j+1);
14209 cpl_table_new_column(slits, name, CPL_TYPE_INT);
14210 cpl_table_set_column_unit(slits, name,
"pixel");
14211 snprintf(name, MAX_COLNAME,
"row_%d", j+1);
14212 cpl_table_new_column(slits, name, CPL_TYPE_INT);
14213 cpl_table_set_column_unit(slits, name,
"pixel");
14217 for (j = 0; j < npeaks; j++) {
14221 low = (place[j-1]*bright[j] + place[j]*bright[j-1])
14222 / (bright[j-1] + bright[j]) + 1;
14227 if (j < npeaks - 1) {
14228 hig = (place[j+1]*bright[j] + place[j]*bright[j+1])
14229 / (bright[j+1] + bright[j]) + 1;
14237 if (hig > pos + count)
14239 if (place[j] - low > maxradius)
14240 low = place[j] - maxradius;
14241 if (hig - place[j] > maxradius)
14242 hig = place[j] + maxradius;
14244 snprintf(name, MAX_COLNAME,
"object_%d", objpos);
14245 cpl_table_set_double(slits, name, i, place[j]);
14246 snprintf(name, MAX_COLNAME,
"start_%d", objpos);
14247 cpl_table_set_int(slits, name, i, low);
14248 snprintf(name, MAX_COLNAME,
"end_%d", objpos);
14249 cpl_table_set_int(slits, name, i, hig);
14250 snprintf(name, MAX_COLNAME,
"row_%d", objpos);
14251 cpl_table_set_int(slits, name, i, row + objpos - 1);
14258 if (maxobjects < nobjects)
14259 maxobjects = nobjects;
14268 row = cpl_table_get_nrow(slits);
14270 for (i = 0; i < row; i++) {
14271 for (j = 0; j < maxobjects; j++) {
14272 snprintf(name, MAX_COLNAME,
"row_%d", j+1);
14273 if (cpl_table_is_valid(slits, name, i))
14274 cpl_table_set_int(slits, name, i, totobj -
14275 cpl_table_get_int(slits, name, i, NULL));
14279 for (i = 0; i < maxobjects; i++) {
14280 snprintf(name, MAX_COLNAME,
"start_%d", i+1);
14281 cpl_table_fill_invalid_int(slits, name, -1);
14282 snprintf(name, MAX_COLNAME,
"end_%d", i+1);
14283 cpl_table_fill_invalid_int(slits, name, -1);
14284 snprintf(name, MAX_COLNAME,
"row_%d", i+1);
14285 cpl_table_fill_invalid_int(slits, name, -1);
14318 cpl_table *objects,
int extraction,
double ron,
14319 double gain,
int ncombined)
14321 const char *func =
"mos_extract_objects";
14323 char name[MAX_COLNAME];
14325 cpl_image **output;
14326 cpl_image *extracted;
14327 cpl_image *extr_sky;
14330 cpl_image *sci_var_win = NULL;
14340 if (science == NULL || sky == NULL) {
14341 cpl_msg_error(func,
"Both scientific exposures are required in input");
14342 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14346 if (objects == NULL) {
14347 cpl_msg_error(func,
"An object table is required in input");
14348 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14352 if (extraction < 0 || extraction > 1) {
14353 cpl_msg_error(func,
"Invalid extraction mode (%d): it should be "
14354 "either 0 or 1", extraction);
14355 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14360 cpl_msg_error(func,
"Invalid read-out-noise (%f ADU)", ron);
14361 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14366 cpl_msg_error(func,
"Invalid gain factor (%f e-/ADU)", gain);
14367 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14371 if (ncombined < 1) {
14372 cpl_msg_error(func,
"Invalid number of combined frames (%d): "
14373 "it should be at least 1", ncombined);
14374 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14385 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
14386 while (cpl_table_has_column(objects, name)) {
14388 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
14397 nslits = cpl_table_get_nrow(objects);
14399 for (i = 0; i < nslits; i++) {
14400 for (j = 1; j < maxobjects; j++) {
14401 snprintf(name, MAX_COLNAME,
"object_%d", j);
14402 if (cpl_table_is_valid(objects, name, i))
14410 nx = cpl_image_get_size_x(science);
14412 output = cpl_calloc(3,
sizeof(cpl_image *));
14413 extracted = output[0] = cpl_image_new(nx, nobjects, CPL_TYPE_FLOAT);
14414 extr_sky = output[1] = cpl_image_new(nx, nobjects, CPL_TYPE_FLOAT);
14415 error = output[2] = cpl_image_new(nx, nobjects, CPL_TYPE_FLOAT);
14423 for (i = 0; i < nslits; i++) {
14424 for (j = 1; j < maxobjects; j++) {
14425 snprintf(name, MAX_COLNAME,
"object_%d", j);
14426 if (cpl_table_is_valid(objects, name, i)) {
14427 snprintf(name, MAX_COLNAME,
"start_%d", j);
14428 ylow = cpl_table_get_int(objects, name, i, NULL);
14429 snprintf(name, MAX_COLNAME,
"end_%d", j);
14430 yhig = cpl_table_get_int(objects, name, i, NULL);
14431 snprintf(name, MAX_COLNAME,
"row_%d", j);
14432 nobjects = cpl_table_get_int(objects, name, i, NULL);
14433 sciwin = cpl_image_extract(science, 1, ylow+1, nx, yhig);
14434 if(science_var != NULL)
14435 sci_var_win = cpl_image_extract(science_var, 1, ylow+1, nx, yhig);
14436 skywin = cpl_image_extract(sky, 1, ylow+1, nx, yhig);
14445 mos_extraction(sciwin, sci_var_win, skywin, extracted, extr_sky, error,
14446 nobjects, extraction, ron, gain, ncombined);
14453 cpl_image *total = cpl_image_add_create(sciwin, skywin);
14454 float *data = cpl_image_get_data_float(total);
14455 int size = cpl_image_get_size_x(total)
14456 * cpl_image_get_size_y(total);
14458 char *saturation_level = getenv(
"SATURATION_LEVEL");
14459 float saturation = 62000.0;
14460 char *max_saturated = getenv(
"MAX_SATURATED");
14461 int max_satur = 10;
14464 if (saturation_level)
14465 saturation = atof(saturation_level);
14468 max_satur = atoi(max_saturated);
14471 for (k = 0; k < size; k++) {
14472 if (data[k] > saturation) {
14474 if (saturated > max_satur) {
14480 if (saturated > max_satur)
14485 data = cpl_image_get_data(extracted);
14486 data[nobjects * nx] = saturated;
14489 cpl_image_delete(sciwin);
14490 cpl_image_delete(skywin);
14524 double dispersion,
int saturation,
14525 double *mfwhm,
double *rmsfwhm,
14526 double *resolution,
double *rmsres,
int *nlines)
14528 cpl_vector *vector;
14531 int position, maxpos;
14536 int threshold = 250;
14541 double min, max, halfmax;
14552 xlen = cpl_image_get_size_x(image);
14553 ylen = cpl_image_get_size_y(image);
14554 data = cpl_image_get_data(image);
14556 buffer = cpl_malloc(ylen *
sizeof(
double));
14562 position = floor((lambda - startwave) / dispersion + 0.5);
14564 sp = position - sradius;
14565 ep = position + sradius;
14567 if (sp < 0 || ep > xlen) {
14572 for (i = 0, n = 0; i < ylen; i++) {
14583 sp = position - radius;
14584 ep = position + radius;
14586 if (sp < 0 || ep > xlen) {
14597 min = max = data[sp + i * xlen];
14598 for (j = sp; j < ep; j++) {
14599 if (data[j + i * xlen] > max) {
14600 max = data[j + i * xlen];
14603 if (data[j + i * xlen] < min) {
14604 min = data[j + i * xlen];
14608 if (fabs(min) < 0.0000001)
14611 if (max - min < threshold)
14614 if (max > saturation)
14624 halfmax = (max + min)/ 2.0;
14628 for (j = maxpos; j < maxpos + radius; j++) {
14630 if (data[j + i * xlen] < halfmax) {
14631 fwhm = ifwhm + (data[j - 1 + i * xlen] - halfmax)
14632 / (data[j - 1 + i * xlen] - data[j + i * xlen]);
14640 for (j = maxpos; j > maxpos - radius; j--) {
14642 if (data[j + i * xlen] < halfmax) {
14643 fwhm += ifwhm + (data[j + 1 + i * xlen] - halfmax)
14644 / (data[j + 1 + i * xlen] - data[j + i * xlen]);
14652 buffer[n] = fwhm - 2.0;
14663 vector = cpl_vector_wrap(n, buffer);
14664 value = cpl_vector_get_median_const(vector);
14665 cpl_vector_unwrap(vector);
14668 for (i = 0, m = 0; i < n; i++) {
14669 if (fabs(buffer[i] - value) < cut) {
14670 rms += fabs(buffer[i] - value);
14683 value *= dispersion;
14689 *resolution = lambda / value;
14690 *rmsres = *resolution * rms / value;
14720 double dispersion,
int saturation,
14735 nref = cpl_vector_get_size(lines);
14736 line = cpl_vector_get_data(lines);
14738 table = cpl_table_new(nref);
14739 cpl_table_new_column(table,
"wavelength", CPL_TYPE_DOUBLE);
14740 cpl_table_set_column_unit(table,
"wavelength",
"Angstrom");
14741 cpl_table_new_column(table,
"fwhm", CPL_TYPE_DOUBLE);
14742 cpl_table_set_column_unit(table,
"fwhm",
"Angstrom");
14743 cpl_table_new_column(table,
"fwhm_rms", CPL_TYPE_DOUBLE);
14744 cpl_table_set_column_unit(table,
"fwhm_rms",
"Angstrom");
14745 cpl_table_new_column(table,
"resolution", CPL_TYPE_DOUBLE);
14746 cpl_table_new_column(table,
"resolution_rms", CPL_TYPE_DOUBLE);
14747 cpl_table_new_column(table,
"nlines", CPL_TYPE_INT);
14749 for (i = 0; i < nref; i++) {
14751 saturation, &fwhm, &rmsfwhm,
14752 &resolution, &rmsres, &nlines)) {
14753 cpl_table_set_double(table,
"wavelength", i, line[i]);
14754 cpl_table_set_double(table,
"fwhm", i, fwhm);
14755 cpl_table_set_double(table,
"fwhm_rms", i, rmsfwhm);
14756 cpl_table_set_double(table,
"resolution", i, resolution);
14757 cpl_table_set_double(table,
"resolution_rms", i, rmsres);
14758 cpl_table_set_int(table,
"nlines", i, nlines);
14761 cpl_table_set_int(table,
"nlines", i, 0);
14764 if (cpl_table_has_valid(table,
"wavelength"))
14767 cpl_table_delete(table);
14792 int ystart,
int yend,
double wstart,
double wend)
14794 const char *func =
"mos_integrate_signal";
14803 if (image == NULL || wavemap == NULL) {
14804 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14808 if (ystart > yend || wstart >= wend) {
14809 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14813 nx = cpl_image_get_size_x(image);
14814 ny = cpl_image_get_size_y(image);
14816 if (!(nx == cpl_image_get_size_x(wavemap)
14817 && ny == cpl_image_get_size_y(wavemap))) {
14818 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
14822 if (ystart < 0 || yend > ny) {
14823 cpl_error_set(func, CPL_ERROR_ACCESS_OUT_OF_RANGE);
14827 sdata = cpl_image_get_data(image);
14828 wdata = cpl_image_get_data(wavemap);
14830 sdata += ystart*nx;
14831 wdata += ystart*nx;
14834 for (y = ystart; y < yend; y++) {
14835 for (x = 0; x < nx; x++) {
14836 if (wdata[x] < wstart || wdata[x] > wend)
14881 const char *func =
"mos_load_slits_fors_mxu";
14884 char keyname[MAX_COLNAME];
14885 const char *instrume;
14886 const char *target_name;
14891 double arc2mm = 0.528;
14904 float low_limit1 = 10.0;
14905 float hig_limit2 = 30.0;
14908 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14912 if (header == NULL) {
14913 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14922 instrume = cpl_propertylist_get_string(header,
"INSTRUME");
14925 if (instrume[4] ==
'1')
14927 if (instrume[4] ==
'2')
14931 cpl_msg_error(func,
"Wrong instrument: %s\n"
14932 "FORS2 is expected for MXU data", instrume);
14933 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14944 chip = cpl_propertylist_get_int(header,
"ESO DET CHIP1 Y");
14946 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14947 cpl_msg_error(func,
"Missing keyword ESO DET CHIP1 Y "
14949 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14953 if (chip != 1 && chip != 2) {
14954 cpl_msg_error(func,
"Unexpected chip position in keyword "
14955 "ESO DET CHIP1 Y: %d", chip);
14956 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14972 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d YPOS", slit_id + 100);
14973 if (cpl_propertylist_has(header, keyname)) {
14974 slit_y = cpl_propertylist_get_double(header, keyname);
14977 if (slit_y < low_limit1)
14980 if (slit_y > hig_limit2)
14983 snprintf(keyname, MAX_COLNAME,
"ESO INS TARG%d NAME",
14985 if (cpl_propertylist_has(header, keyname)) {
14986 target_name = cpl_propertylist_get_string(header, keyname);
14987 if (strncmp(target_name,
"refslit", 7))
14997 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14998 cpl_msg_error(func,
"%s while loading slits coordinates from "
14999 "FITS header", cpl_error_get_message());
15000 cpl_error_set_where(func);
15005 cpl_msg_error(func,
"No slits coordinates found in header");
15006 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
15010 slits = cpl_table_new(nslits);
15011 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
15012 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15013 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15014 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15015 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15016 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15017 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15018 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15019 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15026 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d YPOS", slit_id + 100);
15027 if (cpl_propertylist_has(header, keyname)) {
15028 slit_y = cpl_propertylist_get_double(header, keyname);
15031 if (slit_y < low_limit1)
15034 if (slit_y > hig_limit2)
15044 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d XPOS", slit_id + 100);
15045 slit_x = cpl_propertylist_get_double(header, keyname);
15046 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15047 cpl_table_delete(slits);
15048 cpl_msg_error(func,
"Missing keyword %s in FITS header",
15050 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15054 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d LEN", slit_id + 100);
15055 length = cpl_propertylist_get_double(header, keyname);
15056 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15057 cpl_table_delete(slits);
15058 cpl_msg_error(func,
"Missing keyword %s in FITS header",
15060 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15066 snprintf(keyname, MAX_COLNAME,
"ESO INS TARG%d NAME",
15068 if (cpl_propertylist_has(header, keyname)) {
15069 target_name = cpl_propertylist_get_string(header, keyname);
15070 if (strncmp(target_name,
"refslit", 7)) {
15071 cpl_table_set_int(slits,
"slit_id", nslits, slit_id);
15072 cpl_table_set(slits,
"xtop", nslits, slit_x);
15073 cpl_table_set(slits,
"ytop", nslits, slit_y + length/2);
15074 cpl_table_set(slits,
"xbottom", nslits, slit_x);
15075 cpl_table_set(slits,
"ybottom", nslits, slit_y - length/2);
15080 cpl_table_set_int(slits,
"slit_id", nslits, slit_id);
15081 cpl_table_set(slits,
"xtop", nslits, slit_x);
15082 cpl_table_set(slits,
"ytop", nslits, slit_y + length/2);
15083 cpl_table_set(slits,
"xbottom", nslits, slit_x);
15084 cpl_table_set(slits,
"ybottom", nslits, slit_y - length/2);
15120 int * nslits_out_det)
15122 const char *func =
"mos_load_slits_fors_mos";
15125 char keyname[MAX_COLNAME];
15126 const char *instrume;
15127 const char *chipname;
15129 int first_slit, last_slit;
15140 float ytop[19] = { 113.9, 101.3, 89.9, 77.3, 65.9, 53.3,
15141 41.9, 29.3, 17.9, 5.3, -6.1, -18.7,
15142 -30.1, -42.7, -54.1, -66.7, -78.1, -90.7,
15144 float ybottom[19] = { 102.1, 90.7, 78.1, 66.7, 54.1, 42.7,
15145 30.1, 18.7, 6.1, -5.3, -17.9, -29.3,
15146 -41.9, -53.3, -65.9, -77.3, -89.9, -101.3,
15150 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15154 if (header == NULL) {
15155 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15164 instrume = cpl_propertylist_get_string(header,
"INSTRUME");
15167 if (instrume[4] ==
'1')
15169 if (instrume[4] ==
'2')
15173 cpl_msg_error(func,
"Wrong instrument found in FITS header: %s",
15175 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15185 chipname = cpl_propertylist_get_string(header,
"ESO DET CHIP1 ID");
15187 if (chipname[0] ==
'M' || chipname[0] ==
'N')
15192 if (fors == 1 && fors_is_old) {
15204 chip = cpl_propertylist_get_int(header,
"ESO DET CHIP1 Y");
15206 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15207 cpl_msg_error(func,
"Missing keyword ESO DET CHIP1 Y "
15209 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15213 if (chip != 1 && chip != 2) {
15214 cpl_msg_error(func,
"Unexpected chip position in keyword "
15215 "ESO DET CHIP1 Y: %d", chip);
15216 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15239 for (slit_id = first_slit; slit_id <= last_slit; slit_id++) {
15240 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d POS", slit_id);
15241 if (cpl_propertylist_has(header, keyname)) {
15242 slit_x = cpl_propertylist_get_double(header, keyname);
15243 if (fabs(slit_x) < 115.0)
15246 (*nslits_out_det)++;
15249 cpl_msg_error(func,
"Missing keyword %s in FITS header", keyname);
15250 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15255 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15256 cpl_msg_error(func,
"%s while loading slits coordinates from "
15257 "FITS header", cpl_error_get_message());
15258 cpl_error_set_where(func);
15263 cpl_msg_error(func,
"No slits coordinates found in header");
15264 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
15268 slits = cpl_table_new(nslits);
15269 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
15270 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15271 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15272 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15273 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15274 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15275 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15276 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15277 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15281 for (slit_id = first_slit; slit_id <= last_slit; slit_id++) {
15282 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d POS", slit_id);
15283 slit_x = cpl_propertylist_get_double(header, keyname);
15284 if (fabs(slit_x) < 115.0) {
15285 cpl_table_set_int(slits,
"slit_id", nslits, slit_id);
15286 cpl_table_set(slits,
"xtop", nslits, slit_x);
15287 cpl_table_set(slits,
"ytop", nslits, ytop[slit_id-1]);
15288 cpl_table_set(slits,
"xbottom", nslits, slit_x);
15289 cpl_table_set(slits,
"ybottom", nslits, ybottom[slit_id-1]);
15323 const char *func =
"mos_load_slits_fors_lss";
15327 const char *instrume;
15333 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15337 if (header == NULL) {
15338 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15347 instrume = cpl_propertylist_get_string(header,
"INSTRUME");
15350 if (instrume[4] ==
'1')
15352 if (instrume[4] ==
'2')
15356 cpl_msg_error(func,
"Wrong instrument found in FITS header: %s",
15358 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15374 chip = cpl_propertylist_get_int(header,
"ESO DET CHIP1 Y");
15376 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15377 cpl_msg_error(func,
"Missing keyword ESO DET CHIP1 Y "
15379 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15383 if (chip != 1 && chip != 2) {
15384 cpl_msg_error(func,
"Unexpected chip position in keyword "
15385 "ESO DET CHIP1 Y: %d", chip);
15386 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15401 slits = cpl_table_new(1);
15402 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
15403 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15404 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15405 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15406 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15407 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15408 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15409 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15410 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15412 slit_name = (
char *)cpl_propertylist_get_string(header,
15413 "ESO INS SLIT NAME");
15415 cpl_table_set(slits,
"ytop", 0, ytop);
15416 cpl_table_set(slits,
"ybottom", 0, ybottom);
15418 if (!strncmp(slit_name,
"lSlit0_3arcsec", 14)) {
15419 cpl_table_set_int(slits,
"slit_id", 0, 1);
15420 cpl_table_set(slits,
"xbottom", 0, -0.075);
15421 cpl_table_set(slits,
"xtop", 0, 0.075);
15423 else if (!strncmp(slit_name,
"lSlit0_4arcsec", 14)) {
15424 cpl_table_set_int(slits,
"slit_id", 0, 2);
15425 cpl_table_set(slits,
"xbottom", 0, 5.895);
15426 cpl_table_set(slits,
"xtop", 0, 6.105);
15428 else if (!strncmp(slit_name,
"lSlit0_5arcsec", 14)) {
15429 cpl_table_set_int(slits,
"slit_id", 0, 3);
15430 cpl_table_set(slits,
"xbottom", 0, -6.135);
15431 cpl_table_set(slits,
"xtop", 0, -5.865);
15433 else if (!strncmp(slit_name,
"lSlit0_7arcsec", 14)) {
15434 cpl_table_set_int(slits,
"slit_id", 0, 4);
15435 cpl_table_set(slits,
"xbottom", 0, 11.815);
15436 cpl_table_set(slits,
"xtop", 0, 12.185);
15438 else if (!strncmp(slit_name,
"lSlit1_0arcsec", 14)) {
15439 cpl_table_set_int(slits,
"slit_id", 0, 5);
15440 cpl_table_set(slits,
"xbottom", 0, -12.265);
15441 cpl_table_set(slits,
"xtop", 0, -11.735);
15443 else if (!strncmp(slit_name,
"lSlit1_3arcsec", 14)) {
15444 cpl_table_set_int(slits,
"slit_id", 0, 6);
15445 cpl_table_set(slits,
"xbottom", 0, 17.655);
15446 cpl_table_set(slits,
"xtop", 0, 18.345);
15448 else if (!strncmp(slit_name,
"lSlit1_6arcsec", 14)) {
15449 cpl_table_set_int(slits,
"slit_id", 0, 7);
15450 cpl_table_set(slits,
"xbottom", 0, -18.425);
15451 cpl_table_set(slits,
"xtop", 0, -17.575);
15453 else if (!strncmp(slit_name,
"lSlit2_0arcsec", 14)) {
15454 cpl_table_set_int(slits,
"slit_id", 0, 8);
15455 cpl_table_set(slits,
"xbottom", 0, 23.475);
15456 cpl_table_set(slits,
"xtop", 0, 24.525);
15458 else if (!strncmp(slit_name,
"lSlit2_5arcsec", 14)) {
15459 cpl_table_set_int(slits,
"slit_id", 0, 9);
15460 cpl_table_set(slits,
"xbottom", 0, -24.66);
15461 cpl_table_set(slits,
"xtop", 0, -23.34);
15464 cpl_msg_error(func,
"Invalid slit %s in keyword ESO INS SLIT NAME",
15466 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
15467 cpl_table_delete(slits);
15491 const char *func =
"mos_get_gain_vimos";
15493 double gain = -1.0;
15496 if (cpl_error_get_code() != CPL_ERROR_NONE)
15499 if (header == NULL) {
15500 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15504 gain = cpl_propertylist_get_double(header,
"ESO DET OUT1 CONAD");
15505 if (cpl_error_get_code()) {
15506 cpl_error_set_where(func);
15536 const char *func =
"mos_load_slits_vimos";
15539 char keyname[MAX_COLNAME];
15550 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15554 if (header == NULL) {
15555 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15559 nslits = cpl_propertylist_get_int(header,
"ESO INS SLIT NO");
15561 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15562 cpl_error_set_where(func);
15566 slits = cpl_table_new(nslits);
15567 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
15568 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15569 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15570 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15571 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15572 cpl_table_new_column(slits,
"xwidth", CPL_TYPE_DOUBLE);
15573 cpl_table_new_column(slits,
"ywidth", CPL_TYPE_DOUBLE);
15574 cpl_table_new_column(slits,
"curved", CPL_TYPE_INT);
15575 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15576 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15577 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15578 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15579 cpl_table_set_column_unit(slits,
"xwidth",
"mm");
15580 cpl_table_set_column_unit(slits,
"ywidth",
"mm");
15582 for (i = 0; i < nslits; i++) {
15583 sprintf(keyname,
"ESO INS SLIT%d ID", i+1);
15584 slit_id = cpl_propertylist_get_int(header, keyname);
15585 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15586 cpl_error_set_where(func);
15589 sprintf(keyname,
"ESO INS SLIT%d X", i+1);
15590 slit_x = cpl_propertylist_get_double(header, keyname);
15591 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15592 cpl_error_set_where(func);
15595 sprintf(keyname,
"ESO INS SLIT%d Y", i+1);
15596 slit_y = cpl_propertylist_get_double(header, keyname);
15597 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15598 cpl_error_set_where(func);
15601 sprintf(keyname,
"ESO INS SLIT%d DIMX", i+1);
15602 dim_x = cpl_propertylist_get_double(header, keyname);
15603 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15604 cpl_error_set_where(func);
15608 sprintf(keyname,
"ESO INS SLIT%d BEZIER DY", i+1);
15609 if (cpl_propertylist_has(header, keyname)) {
15613 sprintf(keyname,
"ESO INS SLIT%d DIMY", i+1);
15616 dim_y = cpl_propertylist_get_double(header, keyname);
15617 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15618 cpl_error_set_where(func);
15622 cpl_table_set_int(slits,
"slit_id", i, slit_id);
15623 cpl_table_set(slits,
"xtop", i, slit_x - dim_x/2);
15624 cpl_table_set(slits,
"ytop", i, slit_y);
15625 cpl_table_set(slits,
"xbottom", i, slit_x + dim_x/2);
15626 cpl_table_set(slits,
"ybottom", i, slit_y);
15627 cpl_table_set(slits,
"xwidth", i, dim_x);
15628 cpl_table_set(slits,
"ywidth", i, dim_y);
15629 cpl_table_set_int(slits,
"curved", i, curved);
15647 cpl_propertylist *sort;
15649 int i, multiplex, xprev, xcur;
15651 double tolerance = 1.0;
15661 sort = cpl_propertylist_new();
15662 cpl_propertylist_append_bool(sort,
"xtop", 0);
15663 cpl_table_sort(slits, sort);
15664 cpl_propertylist_delete(sort);
15666 prev = cpl_table_get_double(slits,
"xtop", 0, NULL);
15667 cpl_table_new_column(slits,
"xind", CPL_TYPE_INT);
15668 cpl_table_set_int(slits,
"xind", 0, prev);
15669 nrow = cpl_table_get_nrow(slits);
15670 for (i = 1; i < nrow; i++) {
15671 cur = cpl_table_get_double(slits,
"xtop", i, NULL);
15672 if (fabs(prev - cur) > tolerance)
15674 cpl_table_set_int(slits,
"xind", i, prev);
15682 sort = cpl_propertylist_new();
15683 cpl_propertylist_append_bool(sort,
"xind", 0);
15684 cpl_propertylist_append_bool(sort,
"ytop", 0);
15685 cpl_table_sort(slits, sort);
15686 cpl_propertylist_delete(sort);
15693 cpl_table_new_column(slits,
"multiplex", CPL_TYPE_INT);
15694 xprev = cpl_table_get_int(slits,
"xind", 0, NULL);
15695 cpl_table_set_int(slits,
"multiplex", 0, multiplex);
15696 nrow = cpl_table_get_nrow(slits);
15697 for (i = 1; i < nrow; i++) {
15698 xcur = cpl_table_get_int(slits,
"xind", i, NULL);
15699 if (xcur == xprev) {
15706 cpl_table_set_int(slits,
"multiplex", i, multiplex);
15709 cpl_table_save(slits, NULL, NULL,
"multiplex.fits", CPL_IO_DEFAULT);
15711 cpl_table_erase_column(slits,
"xind");
15713 return 1 + cpl_table_get_column_max(slits,
"multiplex");
15745 int check_consistency)
15747 const char *func =
"mos_load_overscans_vimos";
15758 cpl_table *overscans;
15761 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15762 cpl_msg_error(func,
"Reset your error: %s", cpl_error_get_message());
15766 if (header == NULL) {
15767 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15771 if (cpl_propertylist_has(header,
"NAXIS1"))
15772 nx = cpl_propertylist_get_int(header,
"NAXIS1");
15773 if (cpl_propertylist_has(header,
"NAXIS2"))
15774 ny = cpl_propertylist_get_int(header,
"NAXIS2");
15775 if (cpl_propertylist_has(header,
"ESO DET OUT1 PRSCX"))
15776 px = cpl_propertylist_get_int(header,
"ESO DET OUT1 PRSCX");
15777 if (cpl_propertylist_has(header,
"ESO DET OUT1 PRSCY"))
15778 py = cpl_propertylist_get_int(header,
"ESO DET OUT1 PRSCY");
15779 if (cpl_propertylist_has(header,
"ESO DET OUT1 OVSCX"))
15780 ox = cpl_propertylist_get_int(header,
"ESO DET OUT1 OVSCX");
15781 if (cpl_propertylist_has(header,
"ESO DET OUT1 OVSCY"))
15782 oy = cpl_propertylist_get_int(header,
"ESO DET OUT1 OVSCY");
15783 if (cpl_propertylist_has(header,
"ESO DET OUT1 NX"))
15784 vx = cpl_propertylist_get_int(header,
"ESO DET OUT1 NX");
15785 if (cpl_propertylist_has(header,
"ESO DET OUT1 NY"))
15786 vy = cpl_propertylist_get_int(header,
"ESO DET OUT1 NY");
15788 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15789 cpl_msg_error(func,
"Missing overscan keywords in header");
15790 cpl_error_set_where(func);
15794 if (px < 0 || py < 0 || ox < 0 || oy < 0) {
15795 cpl_msg_error(func,
"Missing overscan keywords in header");
15796 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15800 if ((px + vx + ox != nx) || (py + vy + oy != ny)) {
15801 if (check_consistency) {
15802 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15806 cpl_msg_debug(func,
"Overscans description conflicts with "
15807 "reported image sizes, "
15808 "%d + %d + %d != %d or "
15809 "%d + %d + %d != %d",
15826 cpl_msg_error(func,
"Unexpected overscan regions "
15827 "(both in X and Y direction)");
15828 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15840 overscans = cpl_table_new(nrows);
15841 cpl_table_new_column(overscans,
"xlow", CPL_TYPE_INT);
15842 cpl_table_new_column(overscans,
"ylow", CPL_TYPE_INT);
15843 cpl_table_new_column(overscans,
"xhig", CPL_TYPE_INT);
15844 cpl_table_new_column(overscans,
"yhig", CPL_TYPE_INT);
15848 cpl_table_set_int(overscans,
"xlow", nrows, px);
15849 cpl_table_set_int(overscans,
"ylow", nrows, py);
15850 cpl_table_set_int(overscans,
"xhig", nrows, nx - ox);
15851 cpl_table_set_int(overscans,
"yhig", nrows, ny - oy);
15855 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15856 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15857 cpl_table_set_int(overscans,
"xhig", nrows, px);
15858 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15863 cpl_table_set_int(overscans,
"xlow", nrows, nx - ox);
15864 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15865 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15866 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15871 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15872 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15873 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15874 cpl_table_set_int(overscans,
"yhig", nrows, py);
15879 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15880 cpl_table_set_int(overscans,
"ylow", nrows, ny - oy);
15881 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15882 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15891 cpl_table *mos_load_overscans_fors(
const cpl_propertylist *header)
15893 const char *func =
"mos_load_overscans_fors";
15904 cpl_table *overscans;
15907 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15908 cpl_msg_error(func,
"Reset your error: %s", cpl_error_get_message());
15912 if (header == NULL) {
15913 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15917 if (cpl_propertylist_has(header,
"ESO DET OUTPUTS"))
15918 nports = cpl_propertylist_get_int(header,
"ESO DET OUTPUTS");
15921 cpl_propertylist_has(header,
"ESO DET OUT1 PRSCX") &&
15922 cpl_propertylist_has(header,
"ESO DET WIN1 BINX")) {
15924 rebin = cpl_propertylist_get_int(header,
"ESO DET WIN1 BINX");
15926 overscans = cpl_table_new(3);
15927 cpl_table_new_column(overscans,
"xlow", CPL_TYPE_INT);
15928 cpl_table_new_column(overscans,
"ylow", CPL_TYPE_INT);
15929 cpl_table_new_column(overscans,
"xhig", CPL_TYPE_INT);
15930 cpl_table_new_column(overscans,
"yhig", CPL_TYPE_INT);
15938 cpl_table_set_int(overscans,
"xlow", nrows, px);
15939 cpl_table_set_int(overscans,
"ylow", nrows, py);
15940 cpl_table_set_int(overscans,
"xhig", nrows, nx - ox);
15941 cpl_table_set_int(overscans,
"yhig", nrows, ny - oy);
15944 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15945 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15946 cpl_table_set_int(overscans,
"xhig", nrows, px);
15947 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15950 cpl_table_set_int(overscans,
"xlow", nrows, nx - ox);
15951 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15952 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15953 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15998 cpl_polynomial *mos_montecarlo_polyfit(cpl_table *points, cpl_table *evaluate,
15999 int samples,
int order)
16002 const char *func =
"mos_montecarlo_polyfit";
16016 int npoints, nevaluate;
16020 if (points == NULL || evaluate == NULL) {
16021 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
16025 if (!cpl_table_has_column(points,
"x")) {
16026 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
16030 if (cpl_table_get_column_type(points,
"x") != CPL_TYPE_DOUBLE) {
16031 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
16035 if (cpl_table_has_invalid(points,
"x")) {
16036 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16040 if (!cpl_table_has_column(points,
"y")) {
16041 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
16045 if (cpl_table_get_column_type(points,
"y") != CPL_TYPE_DOUBLE) {
16046 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
16050 if (cpl_table_has_invalid(points,
"y")) {
16051 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16055 if (cpl_table_has_column(points,
"y_err")) {
16057 if (cpl_table_get_column_type(points,
"y_err") != CPL_TYPE_DOUBLE) {
16058 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
16062 if (cpl_table_has_invalid(points,
"y_err")) {
16063 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16068 if (!cpl_table_has_column(evaluate,
"x")) {
16069 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
16073 if (cpl_table_get_column_type(evaluate,
"x") != CPL_TYPE_DOUBLE) {
16074 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
16078 if (cpl_table_has_invalid(evaluate,
"x")) {
16079 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16083 if (samples < 2 || order < 0) {
16084 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16088 npoints = cpl_table_get_nrow(points);
16089 listx = cpl_vector_wrap(npoints, cpl_table_get_data_double(points,
"x"));
16090 listy = cpl_vector_wrap(npoints, cpl_table_get_data_double(points,
"y"));
16092 p = cpl_polynomial_fit_1d_create(listx, listy, order, &err);
16094 if (!cpl_table_has_column(points,
"y_err")) {
16096 cpl_table_new_column(points,
"y_err", CPL_TYPE_DOUBLE);
16097 cpl_table_fill_column_window_double(points,
"y_err", 0, npoints, err);
16098 cpl_msg_info(func,
"Error column not found - set to %f\n", err);
16105 if (cpl_table_has_column(points,
"px"))
16106 cpl_table_erase_column(points,
"px");
16107 cpl_table_new_column(points,
"px", CPL_TYPE_DOUBLE);
16108 cpl_table_fill_column_window_double(points,
"px", 0, npoints, 0);
16109 x = cpl_table_get_data_double(points,
"x");
16110 px = cpl_table_get_data_double(points,
"px");
16111 for (i = 0; i < npoints; i++)
16112 px[i] = cpl_polynomial_eval_1d(p, x[i], NULL);
16114 nevaluate = cpl_table_get_nrow(evaluate);
16116 if (cpl_table_has_column(evaluate,
"px"))
16117 cpl_table_erase_column(evaluate,
"px");
16118 cpl_table_new_column(evaluate,
"px", CPL_TYPE_DOUBLE);
16119 cpl_table_fill_column_window_double(evaluate,
"px", 0, nevaluate, 0);
16120 x_eval = cpl_table_get_data_double(evaluate,
"x");
16121 px_eval = cpl_table_get_data_double(evaluate,
"px");
16122 for (i = 0; i < nevaluate; i++)
16123 px_eval[i] = cpl_polynomial_eval_1d(p, x_eval[i], NULL);
16129 if (cpl_table_has_column(evaluate,
"sigma"))
16130 cpl_table_erase_column(evaluate,
"sigma");
16131 cpl_table_new_column(evaluate,
"sigma", CPL_TYPE_DOUBLE);
16132 cpl_table_fill_column_window_double(evaluate,
"sigma", 0, nevaluate, 0);
16133 sigma = cpl_table_get_data_double(evaluate,
"sigma");
16139 if (cpl_table_has_column(points,
"vy"))
16140 cpl_table_erase_column(points,
"vy");
16141 cpl_table_new_column(points,
"vy", CPL_TYPE_DOUBLE);
16142 cpl_table_fill_column_window_double(points,
"vy", 0, npoints, 0);
16143 vy = cpl_table_get_data_double(points,
"vy");
16144 dy = cpl_table_get_data_double(points,
"y_err");
16145 cpl_vector_unwrap(listy);
16146 listy = cpl_vector_wrap(npoints, vy);
16148 for (i = 0; i < samples; i++) {
16149 for (j = 0; j < npoints; j++)
16150 vy[j] = px[j] + dy[j] * mos_randg(1);
16151 q = cpl_polynomial_fit_1d_create(listx, listy, order, NULL);
16152 for (j = 0; j < nevaluate; j++)
16153 sigma[j] += fabs(px_eval[j]
16154 - cpl_polynomial_eval_1d(q, x_eval[j], NULL));
16155 cpl_polynomial_delete(q);
16162 cpl_table_multiply_scalar(evaluate,
"sigma", 1.25);
16163 cpl_table_divide_scalar(evaluate,
"sigma", samples);
16165 cpl_vector_unwrap(listx);
16166 cpl_vector_unwrap(listy);
16196 double gain,
double bias)
16203 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
16205 if (ron < 0.0 || gain <= FLT_EPSILON)
16206 return cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
16208 data = cpl_image_get_data_float(image);
16209 npix = cpl_image_get_size_x(image) * cpl_image_get_size_y(image);
16212 for (i = 0; i < npix; i++) {
16213 if (data[i] < bias) {
16214 data[i] += sqrt(ron) * mos_randg(1);
16217 data[i] += sqrt(ron + (data[i] - bias) / gain) * mos_randg(1);
16221 return CPL_ERROR_NONE;
16240 cpl_image *master_flat,
16243 int nx = cpl_mask_get_size_x(refmask);
16244 int ny = cpl_mask_get_size_y(refmask);
16246 int * xpos = cpl_calloc(
sizeof(
int), ny);
16248 cpl_image * filtered = cpl_image_duplicate(master_flat);
16249 cpl_mask * kernel = cpl_mask_new(9, 3);
16250 cpl_vector * v = cpl_vector_new(ny);
16251 cpl_vector * truev;
16253 double * flats = cpl_vector_get_data(v);
16255 double median, stdev, delta;
16259 cpl_mask_not(kernel);
16260 cpl_image_filter_mask(filtered, master_flat, kernel,
16261 CPL_FILTER_MEDIAN, CPL_BORDER_COPY);
16262 cpl_mask_delete(kernel);
16264 for (i = 1; i <= ny; i++) {
16268 while (!cpl_mask_get(refmask, j, i) && j < nx);
16274 flats[nvalid] = cpl_image_get(filtered, j, i, &rejected);
16283 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
16285 truev = cpl_vector_wrap(nvalid, flats);
16287 median = cpl_vector_get_median(truev);
16290 stdev = cpl_vector_get_stdev(truev);
16292 cpl_vector_unwrap(truev);
16293 cpl_vector_delete(v);
16295 for (i = 1; i <= ny; i++) {
16296 if (xpos[i - 1] > 0) {
16298 double kappa = 1.5;
16300 delta = cpl_image_get(filtered, xpos[i - 1], i, &rejected) - median;
16303 kill = fabs(delta) > stdev * kappa;
16305 kill = delta < level;
16310 while (cpl_mask_get(refmask, xpos[i - 1] + j, i)) {
16311 cpl_mask_set(refmask, xpos[i - 1] + j, i, CPL_BINARY_0);
16318 cpl_image_delete(filtered);
16321 return cpl_error_get_code();
16333 int nx = cpl_image_get_size_x(image);
16334 int ny = cpl_image_get_size_y(image);
16335 int npix = nx * ny;
16336 float * sdata = cpl_image_get_data_float(image);
16338 int count, i, j, k;
16362 for (i = 0; i < npix; i++) {
16363 if (sdata[i] >= 65535.0) {
16365 for (j = i; j < npix; j++) {
16366 if (sdata[j] < 65535.0) {
16373 if (count < 30 && count > 2) {
16374 for (j = i; j < i + count/2; j++)
16375 sdata[j] = sdata[i] + 1000.0 * (j - i);
16376 if (count % 2 != 0) {
16377 sdata[j] = sdata[j-1] + 1000.0;
16380 for (k = j; k <= i + count; k++)
16381 sdata[k] = sdata[i] - 1000.0 * (k - i - count);
16387 return cpl_error_get_code();
16406 cpl_image_subtract(image, bimage);
16407 cpl_image_delete(bimage);
16409 return cpl_error_get_code();
16430 int nscience,
float tolerance)
16434 cpl_table *summary;
16435 int summary_nobjs = 0;
16440 int nslits = cpl_table_get_nrow(slitss[0]);
16444 int nstokes, sstokes;
16448 work = (cpl_table **)cpl_malloc(
sizeof(cpl_table *) * nscience);
16459 for (j = 0; j < nscience; j++) {
16462 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
16463 summary_nobjs += c_nobjs;
16466 summary = cpl_table_new(summary_nobjs);
16468 cpl_table_new_column(summary,
"offset", CPL_TYPE_DOUBLE);
16469 cpl_table_new_column(summary,
"pair", CPL_TYPE_INT);
16470 cpl_table_new_column(summary,
"absolute", CPL_TYPE_DOUBLE);
16471 cpl_table_new_column(summary,
"pos", CPL_TYPE_DOUBLE);
16480 for (j = 0; j < nscience; j++) {
16484 for (k = 0; k < nslits; k++) {
16487 for (m = 0; m < c_maxobjs; m++) {
16489 char *name = cpl_sprintf(
"object_%d", m + 1);
16490 double obj = cpl_table_get_double(slitss[j], name, k, &null);
16506 pos = cpl_table_get_int(slitss[j],
"position", k, &null);
16507 pair = cpl_table_get_int(slitss[j],
"pair_id", k, &null);
16508 cpl_table_set(summary,
"absolute", nobjs, obj);
16509 cpl_table_set(summary,
"pos", nobjs, pos);
16510 cpl_table_set(summary,
"offset", nobjs, obj - pos);
16511 cpl_table_set(summary,
"pair", nobjs, pair);
16541 for (k = 0; k < nslits; k+=2) {
16542 int slitmatches = 0;
16544 if (k + 1 < nslits ) {
16545 if (cpl_table_get_int(slitss[0],
"pair_id", k, NULL) !=
16546 cpl_table_get_int(slitss[0],
"pair_id", k + 1, NULL)) {
16559 for (m = 0; m < maxobjs; m++) {
16561 char *name = cpl_sprintf(
"object_%d", m + 1);
16562 double obj = cpl_table_get_double(slitss[0], name, k, &null);
16566 char *name_obj = NULL;
16567 char *name_start = NULL;
16568 char *name_end = NULL;
16569 char *name_row = NULL;
16570 char *name_row_s = NULL;
16572 char *name_start_o = NULL;
16573 char *name_end_o = NULL;
16574 char *name_row_o = NULL;
16575 char *name_start_v = NULL;
16576 char *name_end_v = NULL;
16577 char *name_obj_v = NULL;
16583 int v, start_v, end_v;
16584 double min_v, obj_v;
16599 pos = cpl_table_get_int(slitss[0],
"position", k, &null);
16600 pair = cpl_table_get_int(slitss[0],
"pair_id", k, &null);
16609 cpl_table_select_all(summary);
16611 cpl_table_and_selected_int(summary,
"pair", CPL_EQUAL_TO, pair);
16612 cpl_table_and_selected_double(summary,
"offset", CPL_LESS_THAN,
16613 obj - pos + tolerance);
16615 cpl_table_and_selected_double(summary,
"offset", CPL_GREATER_THAN,
16616 obj - pos - tolerance);
16626 if (selected != nscience * 2)
16647 name_obj = cpl_sprintf(
"object_%d", slitmatches);
16648 name_start = cpl_sprintf(
"start_%d", slitmatches);
16649 name_end = cpl_sprintf(
"end_%d", slitmatches);
16650 name_row = cpl_sprintf(
"row_%d", slitmatches);
16651 name_row_s = cpl_sprintf(
"row_stokes_%d", slitmatches);
16658 name_start_o = cpl_sprintf(
"start_%d", m + 1);
16659 name_end_o = cpl_sprintf(
"end_%d", m + 1);
16660 name_row_o = cpl_sprintf(
"row_%d", m + 1);
16666 if (!cpl_table_has_column(origslits, name_obj)) {
16667 cpl_table_new_column(origslits, name_obj, CPL_TYPE_DOUBLE);
16668 cpl_table_new_column(origslits, name_start, CPL_TYPE_INT);
16669 cpl_table_new_column(origslits, name_end, CPL_TYPE_INT);
16670 cpl_table_new_column(origslits, name_row, CPL_TYPE_INT);
16671 cpl_table_new_column(origslits, name_row_s, CPL_TYPE_INT);
16681 length = cpl_table_get_int(origslits,
"length", k + 1, &null);
16689 for (v = 0; v < maxobjs; v++) {
16690 char *name_v = cpl_sprintf(
"object_%d", v + 1);
16691 double obj_v = cpl_table_get_double(slitss[0], name_v,
16700 if (fabs(obj - length - obj_v) < min_v) {
16701 min_v = fabs(obj - length - obj_v);
16702 cpl_free(name_start_v);
16703 cpl_free(name_end_v);
16704 cpl_free(name_obj_v);
16705 name_start_v = cpl_sprintf(
"start_%d", v + 1);
16706 name_end_v = cpl_sprintf(
"end_%d", v + 1);
16707 name_obj_v = cpl_sprintf(
"object_%d", v + 1);
16711 min_v = fabs(obj - length - obj_v);
16712 name_start_v = cpl_sprintf(
"start_%d", v + 1);
16713 name_end_v = cpl_sprintf(
"end_%d", v + 1);
16714 name_obj_v = cpl_sprintf(
"object_%d", v + 1);
16723 start = cpl_table_get_int(slitss[0], name_start_o, k, &null);
16724 end = cpl_table_get_int(slitss[0], name_end_o, k, &null);
16730 start_v = cpl_table_get_int(slitss[0], name_start_v, k + 1, &null);
16731 end_v = cpl_table_get_int(slitss[0], name_end_v, k + 1, &null);
16732 obj_v = cpl_table_get_double(slitss[0], name_obj_v, k + 1, &null);
16743 cpl_table_set_double(origslits, name_obj, k, obj);
16744 cpl_table_set_double(origslits, name_obj, k + 1, obj_v);
16747 cpl_table_set_int(origslits, name_start, k, start);
16748 cpl_table_set_int(origslits, name_start, k + 1, start_v);
16751 cpl_table_set_int(origslits, name_end, k, end);
16752 cpl_table_set_int(origslits, name_end, k + 1, end_v);
16767 cpl_table_set_int(origslits, name_row, k, nmatches);
16769 cpl_table_set_int(origslits, name_row, k + 1, nmatches);
16772 cpl_free(name_obj);
16773 cpl_free(name_start);
16774 cpl_free(name_end);
16775 cpl_free(name_row);
16776 cpl_free(name_row_s);
16778 cpl_free(name_start_o);
16779 cpl_free(name_end_o);
16780 cpl_free(name_row_o);
16782 cpl_free(name_start_v); name_start_v = NULL;
16783 cpl_free(name_end_v); name_end_v = NULL;
16784 cpl_free(name_obj_v); name_obj_v = NULL;
16793 cpl_table_delete(summary);
16796 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
16809 nstokes = nmatches / 2;
16811 for (k = 0; k < nslits; k++) {
16819 for (m = 0; m < maxobjs; m++) {
16820 char *name = cpl_sprintf(
"row_%d", m + 1);
16821 char *namestokes = cpl_sprintf(
"row_stokes_%d", m + 1);
16823 if (!cpl_table_is_valid(origslits, name, k)) {
16825 cpl_free(namestokes);
16831 cpl_table_set_int(origslits, name, k, nmatches);
16832 cpl_table_set_int(origslits, namestokes, k, nstokes);
16836 cpl_free(namestokes);
16848 for (j = 0; j < maxobjs; j++) {
16849 char *name = cpl_sprintf(
"object_%d", j + 1);
16850 cpl_table_fill_invalid_double(origslits, name, -1);
16853 name = cpl_sprintf(
"start_%d", j + 1);
16854 cpl_table_fill_invalid_int(origslits, name, -1);
16857 name = cpl_sprintf(
"end_%d", j + 1);
16858 cpl_table_fill_invalid_int(origslits, name, -1);
16861 name = cpl_sprintf(
"row_%d", j + 1);
16862 cpl_table_fill_invalid_int(origslits, name, -1);
16865 name = cpl_sprintf(
"row_stokes_%d", j + 1);
16866 cpl_table_fill_invalid_int(origslits, name, -1);
16881 for (i = 0; i < nscience; i++) {
16884 work[i] = cpl_table_duplicate(slitss[i]);
16886 for (m = 0; m < c_maxobjs; m++) {
16887 char *object_o = cpl_sprintf(
"object_%d", m + 1);
16888 char *start_o = cpl_sprintf(
"start_%d", m + 1);
16889 char *end_o = cpl_sprintf(
"end_%d", m + 1);
16890 char *row_o = cpl_sprintf(
"row_%d", m + 1);
16892 cpl_table_erase_column(slitss[i], object_o);
16893 cpl_table_erase_column(slitss[i], start_o);
16894 cpl_table_erase_column(slitss[i], end_o);
16895 cpl_table_erase_column(slitss[i], row_o);
16903 for (k = 0; k < nslits; k++) {
16904 for (j = 0; j < maxobjs; j++) {
16905 double object_w, object_r;
16908 char *object_i = cpl_sprintf(
"object_%d", j + 1);
16909 char *start_i = cpl_sprintf(
"start_%d", j + 1);
16910 char *end_i = cpl_sprintf(
"end_%d", j + 1);
16911 char *row_i = cpl_sprintf(
"row_%d", j + 1);
16914 if (!cpl_table_is_valid(origslits, object_i, k))
16927 object_w = cpl_table_get_double(origslits, object_i, k, NULL);
16928 row_w = cpl_table_get_int (origslits, row_i, k, NULL);
16930 for (i = 0; i < nscience; i++) {
16933 double mindiff, diff;
16939 for (m = 0; m < c_maxobjs; m++) {
16940 object_o = cpl_sprintf(
"object_%d", m + 1);
16941 start_o = cpl_sprintf(
"start_%d", m + 1);
16942 end_o = cpl_sprintf(
"end_%d", m + 1);
16943 row_o = cpl_sprintf(
"row_%d", m + 1);
16945 if (!cpl_table_is_valid(work[i], object_o, k))
16948 object_r = cpl_table_get_double(work[i], object_o, k, NULL);
16951 diff = fabs(object_w - object_r);
16953 if (mindiff > diff) {
16963 cpl_free(object_o);
16969 object_o = cpl_sprintf(
"object_%d", minpos + 1);
16970 start_o = cpl_sprintf(
"start_%d", minpos + 1);
16971 end_o = cpl_sprintf(
"end_%d", minpos + 1);
16972 row_o = cpl_sprintf(
"row_%d", minpos + 1);
16974 if (!cpl_table_has_column(slitss[i], object_i)) {
16975 cpl_table_new_column(slitss[i], object_i, CPL_TYPE_DOUBLE);
16976 cpl_table_new_column(slitss[i], start_i, CPL_TYPE_INT);
16977 cpl_table_new_column(slitss[i], end_i, CPL_TYPE_INT);
16978 cpl_table_new_column(slitss[i], row_i, CPL_TYPE_INT);
16979 cpl_table_fill_invalid_double(slitss[i], object_i, -1);
16980 cpl_table_fill_invalid_int (slitss[i], start_i, -1);
16981 cpl_table_fill_invalid_int (slitss[i], end_i, -1);
16982 cpl_table_fill_invalid_int (slitss[i], row_i, -1);
16985 cpl_table_set_double(slitss[i], object_i, k,
16986 cpl_table_get_double(work[i], object_o,
16988 cpl_table_set_int(slitss[i], start_i , k,
16989 cpl_table_get_int(work[i], start_o, k, NULL));
16990 cpl_table_set_int(slitss[i], end_i , k,
16991 cpl_table_get_int(work[i], end_o, k, NULL));
16992 cpl_table_set_int(slitss[i], row_i , k, row_w);
16994 cpl_free(object_o);
17000 cpl_free(object_i);
17007 for (i = 0; i < nscience; i++)
17008 cpl_table_delete(work[i]);
17013 return cpl_error_get_code();
17028 char * colname = cpl_sprintf(
"object_%d", maxobjs);
17030 while (cpl_table_has_column(slits, colname)) {
17033 colname = cpl_sprintf(
"object_%d", maxobjs);
17054 int nslits = cpl_table_get_nrow(slits);
17059 for (k = 0; k < nslits; k++) {
17060 for (m = 0; m < maxobjs; m++) {
17061 char * name = cpl_sprintf(
"object_%d", m + 1);
17062 int null = !cpl_table_is_valid(slits, name, k);
17084 cpl_propertylist *sort;
17086 int nslits = cpl_table_get_nrow(slits);
17090 const float interval = 90.0 * rescale;
17091 const float offset = (90.0 - 5) * rescale;
17094 for (k = 0; k < nslits; k++) {
17095 double ytop = cpl_table_get_double(slits,
"ytop", k, &null);
17096 double ybottom = cpl_table_get_double(slits,
"ybottom", k, &null);
17098 double xtop = cpl_table_get_double(slits,
"xtop", k, &null);
17099 double xbottom = cpl_table_get_double(slits,
"xbottom", k, &null);
17101 int nmiss = (int)((ytop - ybottom) / interval + 0.5);
17104 cpl_msg_warning(cpl_func,
17105 "Some slits could not be properly detected. "
17106 "There might be accountable inaccuracies.");
17107 while (nmiss > 1) {
17108 cpl_table_set_size(slits, nslits + 1);
17113 cpl_table_set_double(slits,
"xtop", nslits, xtop);
17114 cpl_table_set_double(slits,
"xbottom", nslits, xbottom);
17118 cpl_table_set_double(slits,
"ybottom", nslits, ybottom);
17119 cpl_table_set_double(slits,
"ytop", nslits, ybottom
17121 ybottom += interval;
17122 cpl_table_set_double(slits,
"ybottom", k, ybottom);
17124 cpl_table_set_double(slits,
"ytop", nslits, ytop);
17125 cpl_table_set_double(slits,
"ybottom", nslits, ytop
17128 cpl_table_set_double(slits,
"ytop", k, ytop);
17136 sort = cpl_propertylist_new();
17137 cpl_propertylist_append_bool(sort,
"ytop", 1);
17138 cpl_table_sort(slits, sort);
17139 cpl_propertylist_delete(sort);
17146 k = cpl_table_get_nrow(slits) - 1;
17149 double ytop = cpl_table_get_double(slits,
"ytop", k, &null);
17150 double ybottom = cpl_table_get_double(slits,
"ybottom", k, &null);
17151 double length = (ytop - ybottom) / interval;
17153 if (length > 1.1) {
17154 cpl_table_set_double(slits,
"ybottom", k, ytop - offset);
17185 int * nslits_out_det)
17190 cpl_propertylist * sort;
17194 halfsize = cpl_table_get_nrow(slits);
17196 cpl_table_set_size(slits, 2 * halfsize);
17198 for (m = 0; m < halfsize; m++) {
17203 cpl_table_get(slits,
"ytop", m, &null) -
17204 cpl_table_get(slits,
"ybottom", m, &null);
17208 cpl_table_get(slits,
"ybottom", m - 1, &null) -
17209 cpl_table_get(slits,
"ytop", m, &null);
17211 gap = (interval - length) / 2;
17214 cpl_table_set(slits,
"slit_id", m + halfsize,
17215 cpl_table_get(slits,
"slit_id", m, &null) - 1);
17217 cpl_table_set(slits,
"xtop", m + halfsize,
17218 cpl_table_get(slits,
"xtop", m, &null));
17220 cpl_table_set(slits,
"xbottom", m + halfsize,
17221 cpl_table_get(slits,
"xbottom", m, &null));
17223 cpl_table_set(slits,
"ytop", m + halfsize,
17224 cpl_table_get(slits,
"ytop", m, &null) + gap + length);
17226 cpl_table_set(slits,
"ybottom", m + halfsize,
17227 cpl_table_get(slits,
"ytop", m, &null) + gap);
17230 for (m = 0; m < 2 * halfsize; m++) {
17231 cpl_table_set(slits,
"ytop", m,
17232 cpl_table_get(slits,
"ytop", m, &null) - 5.3);
17234 cpl_table_set(slits,
"ybottom", m,
17235 cpl_table_get(slits,
"ybottom", m, &null) - 5.3);
17239 sort = cpl_propertylist_new();
17240 cpl_propertylist_append_bool(sort,
"ytop", 1);
17241 cpl_table_sort(slits, sort);
17243 cpl_propertylist_delete(sort);
17248 int * fors_get_nobjs_perslit(cpl_table * slits)
17250 int nslits = cpl_table_get_nrow(slits);
17253 int * nobjs_per_slit = cpl_malloc(
sizeof(
int) * nslits);
17257 for (k = 0; k < nslits; k++) {
17259 for (m = 0; m < maxobjs; m++) {
17260 char * name = cpl_sprintf(
"object_%d", m + 1);
17261 int null = !cpl_table_is_valid(slits, name, k);
17269 nobjs_per_slit[k] = nobjs;
17272 return nobjs_per_slit;
17275 double fors_get_object_position(cpl_table *slits,
int slit,
int object)
17277 char *name = cpl_sprintf(
"object_%d",
object);
17280 position = cpl_table_get_double(slits, name, slit, NULL)
17281 - cpl_table_get_int(slits,
"position", slit, NULL);
17288 int mos_rebin_signal(cpl_image **image,
int rebin)
17290 cpl_image *rebinned;
17293 if (*image == NULL)
17299 rebinned = cpl_image_rebin(*image, 1, 1, rebin, 1);
17301 cpl_image_delete(*image);
17308 int mos_rebin_error(cpl_image **image,
int rebin)
17310 if (*image == NULL)
17316 cpl_image_power(*image, 2);
17317 mos_rebin_signal(image, rebin);
17318 cpl_image_power(*image, 0.5);
17340 int map_table(cpl_image *image,
double start,
double step,
17341 cpl_table *table,
const char *xname,
const char *yname)
17343 int length = cpl_image_get_size_x(image);
17344 int nrows = cpl_table_get_nrow(table);
17345 float *data = cpl_image_get_data_float(image);
17346 float *fdata = NULL;
17347 double *xdata = NULL;
17348 double *ydata = NULL;
17349 cpl_type xtype = cpl_table_get_column_type(table, xname);
17350 cpl_type ytype = cpl_table_get_column_type(table, yname);
17360 for (i = 0; i < length; i++)
17368 if (xtype == CPL_TYPE_FLOAT) {
17369 fdata = cpl_table_get_data_float(table, xname);
17370 xdata = cpl_malloc(nrows *
sizeof(
double));
17371 for (i = 0; i < nrows; i++) {
17372 xdata[i] = fdata[i];
17376 xdata = cpl_table_get_data_double(table, xname);
17379 if (ytype == CPL_TYPE_FLOAT) {
17380 fdata = cpl_table_get_data_float(table, yname);
17381 ydata = cpl_malloc(nrows *
sizeof(
double));
17382 for (i = 0; i < nrows; i++) {
17383 ydata[i] = fdata[i];
17387 ydata = cpl_table_get_data_double(table, yname);
17397 for (i = 0; i < length; i++) {
17398 pos = start + step * i;
17401 for (j = n; j < nrows; j++) {
17402 if (xdata[j] > pos) {
17404 data[i] = ydata[j-1]
17405 + (ydata[j] - ydata[j-1])
17406 * (pos - xdata[j-1]) / (xdata[j] - xdata[j-1]);
17412 if (xtype == CPL_TYPE_FLOAT)
17415 if (ytype == CPL_TYPE_FLOAT)
17435 static cpl_image *polysmooth(cpl_image *image,
int order,
int hw)
17442 cpl_polynomial *poly;
17443 cpl_vector *ysmooth;
17444 cpl_image *smoothed;
17449 npoints = cpl_image_get_size_x(image);
17451 if (2 * hw + 1 > npoints)
17454 x = cpl_vector_new(npoints);
17455 y = cpl_vector_new(npoints);
17456 xdata = cpl_vector_get_data(x);
17457 ydata = cpl_vector_get_data(y);
17459 smoothed = cpl_image_duplicate(image);
17460 sdata = cpl_image_get_data_float(smoothed);
17462 for (i = 0; i < npoints; i++) {
17464 ydata[i] = sdata[i];
17467 ysmooth = cpl_vector_filter_median_create(y, hw);
17468 cpl_vector_delete(y);
17470 poly = cpl_polynomial_fit_1d_create(x, ysmooth, order, NULL);
17471 cpl_vector_delete(x);
17472 cpl_vector_delete(ysmooth);
17475 for (i = 0; i < npoints; i++)
17476 sdata[i] = cpl_polynomial_eval_1d(poly, i, NULL);
17478 cpl_polynomial_delete(poly);
17481 cpl_image_delete(smoothed);
17491 cpl_image_delete(spectrum); \
17492 cpl_image_delete(flux); \
17493 cpl_image_delete(efficiency); \
17494 cpl_image_delete(smo_efficiency); \
17495 cpl_image_delete(extinction); \
17496 cpl_image_delete(response); \
17497 cpl_image_delete(smo_response); \
17498 cpl_image_delete(physical); \
17525 double dispersion,
double gain,
17526 double exptime, cpl_table *ext_table,
17527 double airmass, cpl_table *flux_table,
17531 cpl_image *spectrum = NULL;
17533 cpl_image *extinction = NULL;
17535 cpl_image *flux = NULL;
17537 cpl_image *physical = NULL;
17539 cpl_image *efficiency = NULL;
17541 cpl_image *smo_efficiency = NULL;
17542 float *smo_eff_data;
17543 cpl_image *response = NULL;
17545 cpl_image *smo_response = NULL;
17546 float *smo_res_data;
17548 cpl_image *smo_image;
17552 int ext_count, ext_pos;
17553 int eff_count, eff_pos;
17554 int flux_count, flux_pos;
17559 if (spectra == NULL || ext_table == NULL || flux_table == NULL) {
17560 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
17564 if (!cpl_table_has_column(ext_table,
"WAVE")) {
17565 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17566 "Column WAVE in atmospheric extinction table");
17570 if (!cpl_table_has_column(ext_table,
"EXTINCTION")) {
17571 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17572 "Column EXTINCTION in atmospheric extinction table");
17576 if (!cpl_table_has_column(flux_table,
"WAVE")) {
17577 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17578 "Column WAVE in standard star flux table");
17582 if (!cpl_table_has_column(flux_table,
"FLUX")) {
17583 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17584 "Column FLUX in standard star flux table");
17589 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17590 "Invalid gain factor (%.2f)", gain);
17594 if (exptime < 0.001) {
17595 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17596 "Invalid exposure time (%.2f)", exptime);
17600 if (dispersion < 0.001) {
17601 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17602 "Invalid dispersion (%.2f)", dispersion);
17607 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17608 "Order of the polynomial fitting the "
17609 "instrument response must be at least 2");
17613 nx = cpl_image_get_size_x(spectra);
17614 ny = cpl_image_get_size_y(spectra);
17622 spectrum = cpl_image_duplicate(spectra);
17626 cpl_image *brights = cpl_image_collapse_create(spectra, 1);
17628 cpl_image_get_maxpos(brights, &x, &y);
17629 cpl_image_delete(brights);
17630 spectrum = cpl_image_extract(spectra, 1, y, nx, y);
17638 cpl_image_multiply_scalar(spectrum, gain / exptime / dispersion);
17646 extinction = cpl_image_duplicate(spectrum);
17647 map_table(extinction, startwave + dispersion/2, dispersion,
17648 ext_table,
"WAVE",
"EXTINCTION");
17655 cpl_image_multiply_scalar(extinction, 0.4 * airmass);
17656 cpl_image_exponential(extinction, 10.);
17663 cpl_image_multiply(spectrum, extinction);
17671 ext_data = cpl_image_get_data_float(extinction);
17675 for (i = 0; i < nx; i++) {
17676 if (ext_data[i] > 0.0) {
17677 if (ext_count == 0) {
17689 cpl_image_delete(extinction); extinction = NULL;
17697 flux = cpl_image_duplicate(spectrum);
17698 map_table(flux, startwave + dispersion/2, dispersion,
17699 flux_table,
"WAVE",
"FLUX");
17707 flux_data = cpl_image_get_data_float(flux);
17711 for (i = 0; i < nx; i++) {
17712 if (flux_data[i] > 0.0) {
17713 if (flux_count == 0) {
17730 start = ext_pos > flux_pos ? ext_pos : flux_pos;
17731 end = (ext_pos + ext_count) < (flux_pos + flux_count) ?
17732 (ext_pos + ext_count) : (flux_pos + flux_count);
17734 flux_count = end - start;
17745 physical = cpl_image_duplicate(spectrum);
17746 phys_data = cpl_image_get_data_float(physical);
17748 for (i = 0; i < nx; i++) {
17749 lambda = startwave + dispersion * (i + 0.5);
17750 phys_data[i] = 0.0026 * lambda * flux_data[i];
17753 efficiency = cpl_image_duplicate(spectrum);
17754 eff_data = cpl_image_get_data_float(efficiency);
17755 data = cpl_image_get_data_float(spectrum);
17757 for (i = 0; i < nx; i++) {
17758 if (phys_data[i] > 0.0)
17759 eff_data[i] = data[i] / phys_data[i];
17764 cpl_image_delete(physical); physical = NULL;
17774 for (i = 0; i < nx; i++) {
17775 if (eff_data[i] > 0.01) {
17776 if (eff_count == 0) {
17782 if (eff_count > 300) {
17793 start = eff_pos > flux_pos ? eff_pos : flux_pos;
17794 end = (eff_pos + eff_count) < (flux_pos + flux_count) ?
17795 (eff_pos + eff_count) : (flux_pos + flux_count);
17797 eff_count = end - start;
17799 if (eff_count < 1) {
17800 cpl_error_set_message(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
17801 "No overlap between catalog and spectrum");
17811 image = cpl_image_extract(efficiency, eff_pos + 1, 1,
17812 eff_pos + eff_count, 1);
17814 smo_image = polysmooth(image, order, 50);
17815 cpl_image_delete(image);
17817 smo_efficiency = cpl_image_duplicate(efficiency);
17818 smo_eff_data = cpl_image_get_data_float(smo_efficiency);
17819 cpl_image_copy(smo_efficiency, smo_image, eff_pos + 1, 1);
17821 cpl_image_delete(smo_image);
17832 response = cpl_image_duplicate(spectrum);
17833 res_data = cpl_image_get_data_float(response);
17835 for (i = 0; i < nx; i++) {
17836 if (eff_data[i] > 0.01 && flux_data[i] > 0.0)
17837 res_data[i] = data[i] / flux_data[i];
17847 image = cpl_image_extract(response, eff_pos + 1, 1, eff_pos + eff_count, 1);
17849 smo_image = polysmooth(image, order, 50);
17850 cpl_image_delete(image);
17852 smo_response = cpl_image_duplicate(response);
17853 smo_res_data = cpl_image_get_data_float(smo_response);
17854 cpl_image_copy(smo_response, smo_image, eff_pos + 1, 1);
17856 cpl_image_delete(smo_image);
17858 for (i = 0; i < nx; i++) {
17859 if (eff_data[i] > 0.01) {
17860 res_data[i] = 1 / res_data[i];
17861 smo_res_data[i] = 1 / smo_res_data[i];
17865 smo_res_data[i] = 0.0;
17874 table = cpl_table_new(nx);
17876 cpl_table_new_column(table,
"WAVE", CPL_TYPE_FLOAT);
17877 cpl_table_set_column_unit(table,
"WAVE",
"Angstrom");
17879 for (i = 0; i < nx; i++)
17880 cpl_table_set_float(table,
"WAVE", i, startwave + dispersion*(i+0.5));
17882 cpl_table_new_column(table,
"STD_FLUX", CPL_TYPE_FLOAT);
17883 cpl_table_set_column_unit(table,
"STD_FLUX",
17884 "10^(-16) erg/(cm^2 s Angstrom)");
17885 cpl_table_copy_data_float(table,
"STD_FLUX", flux_data);
17886 cpl_image_delete(flux); flux = NULL;
17888 cpl_table_new_column(table,
"OBS_FLUX", CPL_TYPE_FLOAT);
17889 cpl_table_set_column_unit(table,
"OBS_FLUX",
"electron/(s Angstrom)");
17890 cpl_table_copy_data_float(table,
"OBS_FLUX", data);
17891 cpl_image_delete(spectrum); spectrum = NULL;
17893 cpl_table_new_column(table,
"RAW_EFFICIENCY", CPL_TYPE_FLOAT);
17894 cpl_table_set_column_unit(table,
"RAW_EFFICIENCY",
"electron/photon");
17895 cpl_table_copy_data_float(table,
"RAW_EFFICIENCY", eff_data);
17896 cpl_image_delete(efficiency); efficiency = NULL;
17898 cpl_table_new_column(table,
"EFFICIENCY", CPL_TYPE_FLOAT);
17899 cpl_table_set_column_unit(table,
"EFFICIENCY",
"electron/photon");
17900 cpl_table_copy_data_float(table,
"EFFICIENCY", smo_eff_data);
17901 cpl_image_delete(smo_efficiency); smo_efficiency = NULL;
17903 cpl_table_new_column(table,
"RAW_RESPONSE", CPL_TYPE_FLOAT);
17904 cpl_table_set_column_unit(table,
"RAW_RESPONSE",
17905 "10^(-16) erg/(cm^2 electron)");
17906 cpl_table_copy_data_float(table,
"RAW_RESPONSE", res_data);
17907 cpl_image_delete(response); response = NULL;
17909 cpl_table_new_column(table,
"RESPONSE", CPL_TYPE_FLOAT);
17910 cpl_table_set_column_unit(table,
17911 "RESPONSE",
"10^(-16) erg/(cm^2 electron)");
17912 cpl_table_copy_data_float(table,
"RESPONSE", smo_res_data);
17913 cpl_image_delete(smo_response); smo_response = NULL;
17920 static double ksigma_vector(cpl_vector *values,
17921 double klow,
double khigh,
int kiter,
int *good)
17923 cpl_vector *accepted;
17925 double sigma = 0.0;
17926 double *data = cpl_vector_get_data(values);
17927 int n = cpl_vector_get_size(values);
17938 mean = cpl_vector_get_median(values);
17940 for (i = 0; i < n; i++)
17941 sigma += (mean - data[i]) * (mean - data[i]);
17943 sigma = sqrt(sigma / (n - 1));
17947 for (i = 0; i < ngood; i++) {
17948 if (data[i]-mean < khigh*sigma && mean-data[i] < klow*sigma) {
17949 data[count] = data[i];
17963 accepted = cpl_vector_wrap(count, data);
17964 mean = cpl_vector_get_mean(accepted);
17966 sigma = cpl_vector_get_stdev(accepted);
17967 cpl_vector_unwrap(accepted);
17969 if (count == ngood || count == 1)
18002 double klow,
double khigh,
int kiter,
18005 int ni, nx, ny, npix;
18006 cpl_image *out_ima;
18011 cpl_vector *time_line;
18012 double *ptime_line;
18017 ni = cpl_imagelist_get_size(imlist);
18019 image = cpl_imagelist_get(imlist, 0);
18020 nx = cpl_image_get_size_x(image);
18021 ny = cpl_image_get_size_y(image);
18024 out_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
18025 pout_ima = cpl_image_get_data_float(out_ima);
18028 *good = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
18029 good_ima = cpl_image_get_data_float(*good);
18032 time_line = cpl_vector_new(ni);
18033 ptime_line = cpl_vector_get_data(time_line);
18035 data = cpl_calloc(
sizeof(
float *), ni);
18037 for (i = 0; i < ni; i++) {
18038 image = cpl_imagelist_get(imlist, i);
18039 data[i] = cpl_image_get_data_float(image);
18042 for (i = 0; i < npix; i++) {
18043 for (j = 0; j < ni; j++) {
18044 ptime_line[j] = data[j][i];
18046 pout_ima[i] = ksigma_vector(time_line, klow, khigh, kiter, &ngood);
18048 good_ima[i] = ngood;
18053 cpl_vector_delete(time_line);
18077 cpl_table *ext_table,
double startwave,
18078 double dispersion,
double gain,
18079 double exptime,
double airmass)
18081 cpl_image *extinction;
18082 cpl_image *outspectra;
18083 cpl_image *mapresponse;
18087 int tlength, xlength, ylength;
18089 double resp_startwave;
18090 double resp_endwave;
18094 if (spectra == NULL || ext_table == NULL || response == NULL) {
18095 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
18103 if(cpl_table_has_column(response,
"RESPONSE"))
18104 cpl_table_cast_column(response,
"RESPONSE",
"RESPONSE_F", CPL_TYPE_FLOAT);
18105 else if(cpl_table_has_column(response,
"RESPONSE_FFSED"))
18106 cpl_table_cast_column(response,
"RESPONSE_FFSED",
"RESPONSE_F", CPL_TYPE_FLOAT);
18110 res_data = cpl_table_get_data_float(response,
"RESPONSE_F");
18112 if (res_data == NULL) {
18113 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
18117 tlength = cpl_table_get_nrow(response);
18118 xlength = cpl_image_get_size_x(spectra);
18119 ylength = cpl_image_get_size_y(spectra);
18122 mapresponse = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18123 map_table(mapresponse, startwave + dispersion/2, dispersion,
18124 response,
"WAVE",
"RESPONSE_F");
18125 res_data = cpl_image_get_data_float(mapresponse);
18132 extinction = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18133 map_table(extinction, startwave + dispersion/2, dispersion,
18134 ext_table,
"WAVE",
"EXTINCTION");
18141 cpl_image_multiply_scalar(extinction, 0.4 * airmass);
18142 cpl_image_exponential(extinction, 10.);
18144 outspectra = cpl_image_duplicate(spectra);
18146 ext_data = cpl_image_get_data_float(extinction);
18147 out_data = cpl_image_get_data_float(outspectra);
18149 for (k = 0, i = 0; i < ylength; i++) {
18150 for (j = 0; j < xlength; j++, k++)
18151 out_data[k] *= ext_data[j] * res_data[j];
18154 cpl_image_delete(extinction);
18155 cpl_image_delete(mapresponse);
18157 cpl_image_multiply_scalar(outspectra, gain / exptime / dispersion);
18162 resp_startwave = cpl_table_get(response,
"WAVE", 0, &null);
18163 resp_endwave = cpl_table_get(response,
"WAVE",
18164 cpl_table_get_nrow(response) -1, &null);
18165 for (j = 0; j < xlength; j++) {
18166 double this_wave = startwave + j * dispersion;
18167 if(this_wave < resp_startwave ||this_wave > resp_endwave)
18169 for (i = 0; i < ylength; i++)
18170 out_data[j + xlength * i] = -1;
18174 cpl_table_erase_column(response,
"RESPONSE_F");
18198 cpl_table *response,
18199 cpl_table *ext_table,
18201 double dispersion,
double gain,
18202 double exptime,
double airmass)
18204 cpl_image *extinction;
18205 cpl_image *outerrors;
18206 cpl_image *mapresponse;
18207 cpl_image *maperror;
18213 int tlength, xlength, ylength;
18217 if (errors == NULL || ext_table == NULL || response == NULL) {
18218 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
18222 if (!cpl_table_has_column(response,
"ERROR")) {
18224 dispersion, gain, exptime, airmass);
18227 cpl_table_cast_column(response,
"RESPONSE",
"RESPONSE_F", CPL_TYPE_FLOAT);
18228 res_data = cpl_table_get_data_float(response,
"RESPONSE_F");
18230 if (res_data == NULL) {
18231 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
18235 err_data = cpl_table_get_data_float(response,
"ERROR");
18237 if (err_data == NULL) {
18238 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
18242 tlength = cpl_table_get_nrow(response);
18243 xlength = cpl_image_get_size_x(errors);
18244 ylength = cpl_image_get_size_y(errors);
18246 if (xlength != tlength) {
18247 mapresponse = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18248 map_table(mapresponse, startwave + dispersion/2, dispersion,
18249 response,
"WAVE",
"RESPONSE_F");
18250 res_data = cpl_image_get_data_float(mapresponse);
18252 maperror = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18253 map_table(maperror, startwave + dispersion/2, dispersion,
18254 response,
"WAVE",
"ERROR");
18255 err_data = cpl_image_get_data_float(maperror);
18263 extinction = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18264 map_table(extinction, startwave + dispersion/2, dispersion,
18265 ext_table,
"WAVE",
"EXTINCTION");
18272 cpl_image_multiply_scalar(extinction, 0.4 * airmass);
18273 cpl_image_exponential(extinction, 10.);
18275 outerrors = cpl_image_duplicate(errors);
18277 ext_data = cpl_image_get_data_float(extinction);
18278 out_data = cpl_image_get_data_float(outerrors);
18279 spe_data = cpl_image_get_data_float(spectra);
18281 for (k = 0, i = 0; i < ylength; i++) {
18282 for (j = 0; j < xlength; j++, k++) {
18283 out_data[k] = ext_data[j] *
18284 sqrt(err_data[j] * err_data[j] * spe_data[k] * spe_data[k] +
18285 res_data[j] * res_data[j] * out_data[k] * out_data[k]);
18289 cpl_image_delete(extinction);
18290 if (xlength != tlength) {
18291 cpl_image_delete(maperror);
18294 cpl_image_multiply_scalar(outerrors, gain / exptime / dispersion);
18296 cpl_table_erase_column(response,
"RESPONSE_F");
18377 cpl_image *u_image, cpl_image *u_error,
18378 double startwave,
double dispersion,
18379 double band, cpl_table *pol_sta,
18380 double ra,
double dec,
char *filter,
18382 double *p_offset,
double *p_error,
18383 double *a_offset,
double *a_error)
18385 cpl_table *standard;
18386 cpl_image *q_noise;
18387 cpl_image *q_signal;
18388 cpl_image *u_noise;
18389 cpl_image *u_signal;
18395 double arctol = 0.5;
18398 double bwave[] = {3650., 4450., 5510., 6580., 8060};
18399 char *bands =
"UBVRI";
18400 char p_label[] = {
' ',
'p',
'\0'};
18401 char dp_label[] = {
' ',
'd',
'p',
'\0'};
18402 char a_label[] = {
' ',
'a',
'\0'};
18403 char da_label[] = {
' ',
'd',
'a',
'\0'};
18404 int nbands = strlen(bands);
18406 int first, last, count, center;
18409 int i, found, closest;
18437 cpl_table_select_all(pol_sta);
18438 cpl_table_and_selected_double(pol_sta,
"RA", CPL_GREATER_THAN, ra-arctol);
18439 cpl_table_and_selected_double(pol_sta,
"RA", CPL_LESS_THAN, ra+arctol);
18440 cpl_table_and_selected_double(pol_sta,
"DEC", CPL_GREATER_THAN, dec-arctol);
18442 cpl_table_and_selected_double(pol_sta,
"DEC", CPL_LESS_THAN, dec+arctol);
18444 if (selected == 0) {
18445 cpl_msg_warning(cpl_func,
"No standard star found in FOV");
18449 if (selected > 1) {
18450 cpl_msg_warning(cpl_func,
18451 "Ambiguity: %d standard stars found in FOV", selected);
18455 standard = cpl_table_extract_selected(pol_sta);
18457 cpl_msg_info(cpl_func,
"Standard star: %s",
18458 cpl_table_get_string(standard,
"name", 0));
18464 polarised = cpl_table_get_int(standard,
"polarised", 0, NULL);
18466 cpl_msg_info(cpl_func,
"This star is%sexpected to be polarised",
18467 polarised ?
" " :
" not ");
18477 nx = cpl_image_get_size_x(q_error);
18479 noise = cpl_image_collapse_median_create(q_error, 1, 0, 0);
18480 cpl_image_get_minpos(noise, &col, &row);
18482 cpl_image_delete(noise);
18485 cpl_table_delete(standard);
18486 cpl_msg_error(cpl_func,
18487 "Assertion failure!!! col = %"CPL_SIZE_FORMAT
" (it should be 1)", col);
18491 q_signal = cpl_image_extract(q_image, 1, row, nx, row);
18492 q_noise = cpl_image_extract(q_error, 1, row, nx, row);
18493 u_signal = cpl_image_extract(u_image, 1, row, nx, row);
18494 u_noise = cpl_image_extract(u_error, 1, row, nx, row);
18496 q_sdata = cpl_image_get_data_double(q_signal);
18497 q_ndata = cpl_image_get_data_double(q_noise);
18498 u_sdata = cpl_image_get_data_double(u_signal);
18499 u_ndata = cpl_image_get_data_double(u_noise);
18507 last = nx = cpl_image_get_size_x(q_signal);
18508 for (i = 0; i < nx; i++) {
18510 if (q_ndata[i] > 0.0) {
18515 if (q_ndata[i] <= 0.0) {
18522 count = last - first + 1;
18524 if (first < 0 || count < band) {
18525 cpl_table_delete(standard);
18526 cpl_image_delete(q_signal);
18527 cpl_image_delete(q_noise);
18528 cpl_image_delete(u_signal);
18529 cpl_image_delete(u_noise);
18530 cpl_msg_warning(cpl_func,
"Too short spectrum (%d pixels)", count);
18534 center = (first + last) / 2;
18535 cwave = startwave + dispersion * center;
18543 for (i = 0; i < nbands; i++) {
18544 p_label[0] = bands[i];
18545 if (cpl_table_is_valid(standard, p_label, 0)) {
18548 mindist = fabs(bwave[i] - cwave);
18551 else if (mindist > fabs(bwave[i] - cwave)) {
18552 mindist = fabs(bwave[i] - cwave);
18559 cpl_table_delete(standard);
18560 cpl_image_delete(q_signal);
18561 cpl_image_delete(q_noise);
18562 cpl_image_delete(u_signal);
18563 cpl_image_delete(u_noise);
18564 cpl_msg_warning(cpl_func,
"No reference value available");
18568 center = (bwave[closest] - startwave) / dispersion;
18569 cwave = bwave[closest];
18577 pband = floor(band / dispersion);
18579 if (center - pband/2 < first || center + pband/2 > last) {
18580 cpl_table_delete(standard);
18581 cpl_image_delete(q_signal);
18582 cpl_image_delete(q_noise);
18583 cpl_image_delete(u_signal);
18584 cpl_image_delete(u_noise);
18585 cpl_msg_warning(cpl_func,
"No reference value available");
18589 first = center - pband/2;
18590 last = center + pband/2;
18597 p_label[0] = bands[closest];
18598 dp_label[0] = bands[closest];
18599 a_label[0] = bands[closest];
18600 da_label[0] = bands[closest];
18602 p_ref = cpl_table_get(standard, p_label, 0, NULL);
18603 dp_ref = cpl_table_get(standard, dp_label, 0, NULL);
18604 a_ref = cpl_table_get(standard, a_label, 0, NULL);
18605 da_ref = cpl_table_get(standard, da_label, 0, NULL);
18607 cpl_msg_info(cpl_func,
18608 "The expected polarisation is %.2f +- %.2f %%",
18612 cpl_msg_info(cpl_func,
18613 "The expected polarisation angle is %.2f +- %.2f degrees",
18621 q_obs = cpl_image_get_median_window(q_image, first, 1, last, 1);
18622 q_err = cpl_image_get_median_window(q_error, first, 1, last, 1);
18623 u_obs = cpl_image_get_median_window(u_image, first, 1, last, 1);
18624 u_err = cpl_image_get_median_window(u_error, first, 1, last, 1);
18630 p_obs = sqrt(q_obs * q_obs + u_obs * u_obs);
18631 p_err = CPL_MATH_SQRT1_2 * 0.5 * (q_err + u_err);
18639 if (fabs(q_obs) < 0.00001) {
18648 a_obs = 0.5 * atan(u_obs / q_obs) * 180 / CPL_MATH_PI;
18666 a_err = sqrt(q_obs*q_obs*u_err*u_err + u_obs*u_obs*q_err*q_err)
18668 * 90 / CPL_MATH_PI;
18673 cpl_msg_info(cpl_func,
18674 "The measured polarisation is %.2f +- %.2f %%",
18678 cpl_msg_info(cpl_func,
18679 "The measured polarisation angle is %.2f +- %.2f degrees",
18683 *filter = bands[closest];
18684 *polarisation = polarised;
18687 *p_offset = (p_obs - p_ref) / p_ref;
18688 *p_error = sqrt(p_err * p_err + dp_ref * dp_ref) / p_ref;
18691 *p_offset = p_obs - p_ref;
18692 *p_error = sqrt(p_err * p_err + dp_ref * dp_ref);
18695 *a_offset = a_obs - a_ref;
18696 *a_error = sqrt(a_err*a_err + da_ref*da_ref);
18736 cpl_array *offsets;
18738 int nslits = cpl_table_get_nrow(reference);
18745 cpl_error_code status = CPL_ERROR_NONE;
18750 if (objects == NULL)
18751 return CPL_ERROR_NULL_INPUT;
18753 if (nslits != cpl_table_get_nrow(objects))
18754 return CPL_ERROR_INCOMPATIBLE_INPUT;
18756 nref = fors_get_nobjs_perslit(reference);
18757 nobj = fors_get_nobjs_perslit(objects);
18760 for (i = 0; i < nslits; i++)
18761 noffset += nobj[i];
18763 if (noffset == 0) {
18766 return CPL_ERROR_DATA_NOT_FOUND;
18770 for (i = 0; i < nslits; i++)
18771 noffset += nref[i];
18773 if (noffset == 0) {
18776 return CPL_ERROR_DATA_NOT_FOUND;
18779 offsets = cpl_array_new(noffset, CPL_TYPE_DOUBLE);
18783 for (i = 0; i < nslits; i++) {
18784 if (nref[i] > 0 && nobj[i] > 0) {
18786 int length = cpl_table_get_int(objects,
"length", i, NULL);
18787 double ytop = cpl_table_get_double(objects,
"xtop", i, NULL);
18788 double ybottom = cpl_table_get_double(objects,
"xbottom", i, NULL);
18789 int *aref = cpl_calloc(length,
sizeof(
int));
18790 int *aobj = cpl_calloc(length,
sizeof(
int));
18791 float *pref = cpl_calloc(nref[i],
sizeof(
float));
18792 float *pobj = cpl_calloc(nobj[i],
sizeof(
float));
18794 for (j = 0; j < nref[i]; j++) {
18795 pref[j] = fors_get_object_position(reference, i, j + 1);
18796 aref[(int)pref[j]] = 1;
18799 for (j = 0; j < nobj[i]; j++) {
18800 pobj[j] = fors_get_object_position(objects, i, j + 1);
18801 aobj[(int)pobj[j]] = 1;
18809 aref[length - 1] = 0;
18811 aobj[length - 1] = 0;
18831 best_shift = length;
18833 for (shift = length/2, j = 0; j <= length; shift--, j++) {
18834 int rstart, ostart, count;
18839 count = length - shift;
18844 count = length + shift;
18848 for (k = 0; k < count; k++) {
18849 corr += aref[rstart + k] * aobj[ostart + k];
18852 if (maxcorr < corr) {
18854 best_shift = shift;
18858 if (best_shift == length) {
18868 for (j = 0; j < nref[i]; j++) {
18869 for (k = 0; k < nobj[i]; k++) {
18870 if (fabs(pref[j] - pobj[k] - best_shift) < 2) {
18871 double ccd_offset = (pref[j] - pobj[k])
18881 cpl_array_set(offsets, noffset, ccd_offset);
18903 *offset = cpl_array_get_median(offsets);
18906 double *a = cpl_malloc(
sizeof(
double) * noffset);
18907 for (i = 0; i < noffset; i++) {
18908 a[i] = cpl_array_get_double(offsets, i, NULL);
18916 status = CPL_ERROR_DATA_NOT_FOUND;
18919 cpl_array_delete(offsets);
18940 int nx = cpl_image_get_size_x(image);
18941 int ny = cpl_image_get_size_y(image);
18945 double xpos, ypos, xfrac, yfrac;
18949 if (fabs(dx) >= nx || fabs(dy) >= ny)
18950 return CPL_ERROR_ACCESS_OUT_OF_RANGE;
18952 source = cpl_image_duplicate(image);
18953 idata = cpl_image_get_data_float(image);
18954 sdata = cpl_image_get_data_float(source);
18960 yfrac = - dy - floor(- dy);
18961 xfrac = - dx - floor(- dx);
18963 for (pos = 0, j = 0; j < ny; j++) {
18965 yint = floor(ypos);
18966 for (i = 0; i < nx; i++) {
18968 xint = floor(xpos);
18969 if (xint < 0 || yint < 0 || xint > nx - 2 || yint > ny - 2) {
18973 idata[pos] = sdata[xint + nx*yint] * (1 - xfrac) * (1 - yfrac)
18974 + sdata[xint + 1 + nx*yint] * xfrac * (1 - yfrac)
18975 + sdata[xint + nx*(yint + 1)] * (1 - xfrac) * yfrac
18976 + sdata[xint + 1 + nx*(yint + 1)] * xfrac * yfrac;
18982 cpl_image_delete(source);
18984 return CPL_ERROR_NONE;
19000 #ifdef CPL_SIZE_FORMAT
19006 cpl_table_duplicate_column(slits,
"x", slits,
"xtop");
19007 cpl_table_add_columns(slits,
"x",
"xbottom");
19008 cpl_table_divide_scalar(slits,
"x", 2);
19009 cpl_table_subtract_scalar(slits,
"x", nx/2);
19010 cpl_table_multiply_columns(slits,
"x",
"x");
19012 cpl_table_duplicate_column(slits,
"y", slits,
"ytop");
19013 cpl_table_add_columns(slits,
"y",
"ybottom");
19014 cpl_table_divide_scalar(slits,
"y", 2);
19015 cpl_table_subtract_scalar(slits,
"y", ny/2);
19016 cpl_table_multiply_columns(slits,
"y",
"y");
19018 cpl_table_add_columns(slits,
"x",
"y");
19019 cpl_table_get_column_minpos(slits,
"x", &row);
19021 cpl_table_erase_column(slits,
"x");
19022 cpl_table_erase_column(slits,
"y");
19047 double xwidth,
double ywidth,
19048 int dx,
double gain,
double *o_flux,
double *o_err)
19050 int nx = cpl_image_get_size_x(image);
19051 int ny = cpl_image_get_size_y(image);
19053 int ytop = (int)cpl_table_get(slits,
"ytop", slit, NULL);
19054 int ybottom = (int)cpl_table_get(slits,
"ybottom", slit, NULL);
19055 int dy = ytop - ybottom;
19056 int xcenter = (int)((cpl_table_get(slits,
"xtop", slit, NULL) +
19057 cpl_table_get(slits,
"xbottom", slit, NULL)) / 2);
19058 int xleft = xcenter - dx;
19059 int xright = xcenter + dx + 1;
19060 double area = xwidth * ywidth;
19061 int npix = (2*dx + 1) * dy;
19063 float *data = cpl_image_get_data_float(image);
19065 double error = 0.0;
19070 if (cpl_table_has_column(slits,
"ywidth")) {
19071 area = cpl_table_get(slits,
"xwidth", slit, NULL)
19072 * cpl_table_get(slits,
"ywidth", slit, NULL);
19102 count = (xright - xleft) * (ytop - ybottom);
19105 return CPL_ERROR_ACCESS_OUT_OF_RANGE;
19109 for (y = ybottom; y < ytop; y++) {
19110 for (x = xleft; x < xright; x++) {
19111 double value = data[x + y * nx];
19112 if (value < satur) {
19120 return CPL_ERROR_DIVISION_BY_ZERO;
19122 error = sqrt(flux/gain);
19128 flux *= (float)npix / count;
19129 error *= (float)npix / count;
19137 return CPL_ERROR_NONE;
19164 double xwidth,
double ywidth,
19165 double lambda,
double startwave,
19166 double dispersion,
int dx,
double gain,
19167 double *o_flux,
double *o_err)
19169 int nx = cpl_image_get_size_x(image);
19170 int ny = cpl_image_get_size_y(image);
19172 int dy = (int)cpl_table_get(slits,
"length", slit, NULL);
19173 int ybottom = (int)cpl_table_get(slits,
"position", slit, NULL);
19174 int ytop = ybottom + dy;
19175 int xcenter = (int)floor((lambda - startwave) / dispersion + 0.5);
19176 int xleft = xcenter - dx;
19177 int xright = xcenter + dx + 1;
19178 double area = xwidth * ywidth;
19179 int npix = (2*dx + 1) * dy;
19181 float *data = cpl_image_get_data_float(image);
19183 double error = 0.0;
19188 if (cpl_table_has_column(slits,
"ywidth")) {
19189 area = cpl_table_get(slits,
"xwidth", slit, NULL)
19190 * cpl_table_get(slits,
"ywidth", slit, NULL);
19220 count = (xright - xleft) * (ytop - ybottom);
19223 return CPL_ERROR_ACCESS_OUT_OF_RANGE;
19227 for (y = ybottom; y < ytop; y++) {
19228 for (x = xleft; x < xright; x++) {
19229 double value = data[x + y * nx];
19230 if (value < satur) {
19238 return CPL_ERROR_DIVISION_BY_ZERO;
19240 error = sqrt(flux/gain);
19246 flux *= (float)npix / count;
19247 error *= (float)npix / count;
19255 return CPL_ERROR_NONE;
19274 char *label,
double *mvalue)
19276 int position = cpl_table_get_int(slits,
"position", slit, NULL);
19277 int length = cpl_table_get_int(slits,
"length", slit, NULL);
19278 cpl_table *tmp = cpl_table_extract(table, position, length);
19280 *mvalue = cpl_table_get_column_median(tmp, label);
19281 cpl_table_delete(tmp);
19283 if (cpl_error_get_code() != CPL_ERROR_NONE)
19303 cpl_mask *kernel = cpl_mask_new(nx, ny);
19304 cpl_image *filtered = cpl_image_new(cpl_image_get_size_x(image),
19305 cpl_image_get_size_y(image),
19306 cpl_image_get_type(image));
19308 cpl_mask_not(kernel);
19309 cpl_image_filter_mask(filtered, image, kernel,
19310 CPL_FILTER_MEDIAN, CPL_BORDER_FILTER);
19311 cpl_mask_delete(kernel);
19316 int fors_mos_is_lss_like(cpl_table *maskslits,
int nslits_out_det)
19318 int treat_as_lss = 1;
19319 double mxpos = cpl_table_get_column_median(maskslits,
"xtop");
19320 double * slit_xpos = cpl_table_get_data_double(maskslits,
"xtop");
19321 cpl_size nslits = cpl_table_get_nrow(maskslits);
19325 if(nslits_out_det != 0)
19328 for (cpl_size i = 0; i < nslits; i++) {
19329 if (fabs(mxpos-slit_xpos[i]) > 0.01) {
19334 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_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_polynomial * mos_poly_wav2pix(cpl_bivector *pixwav, int order, double reject, int minlines, int *nlines, double *err, cpl_bivector **pixwav_used)
Fit polynomial relation from wavelengths to pixels.
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.