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);
5624 for (i = 0; i < ny; i++) {
5627 if (width > sradius) {
5643 cpl_bivector * peaks_ident_used_fit;
5644 countLines = cpl_bivector_get_size(output);
5645 if (countLines < 4) {
5646 cpl_bivector_delete(output);
5647 cpl_vector_delete(peaks);
5659 wavel = cpl_bivector_get_y(output);
5660 cpl_vector_subtract_scalar(wavel, refwave);
5662 uorder = countLines / 2 - 1;
5676 2 * (uorder + 1), &usedLines,
5677 &ids_err, &peaks_ident_used_fit);
5680 cpl_bivector_delete(output);
5681 cpl_vector_delete(peaks);
5698 for (k = 0; k <= order; k++) {
5700 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
5703 cpl_table_set_double(idscoeff, clab[k], i,
5704 cpl_polynomial_get_coeff(ids, &k));
5711 cpl_size newlines = cpl_vector_get_size(peaks);
5712 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
5713 cpl_table_set_size(detected_lines, oldsize + newlines);
5714 for(cpl_size iline = 0; iline < newlines; ++iline)
5716 cpl_table_set_double(detected_lines,
"xpos",
5717 oldsize + iline, cpl_vector_get(peaks, iline) + 1);
5718 cpl_table_set_double(detected_lines,
"ypos",
5719 oldsize + iline, (
double)i + 1);
5720 cpl_table_set_double(detected_lines,
"peak_flux",
5722 sdata[i*nx+(
int)(cpl_vector_get(peaks, iline)+0.5)]);
5730 cpl_size nidentlines = cpl_bivector_get_size(output);
5731 cpl_size ndetectlines = cpl_vector_get_size(peaks);
5732 cpl_size totalsize = cpl_table_get_nrow(detected_lines);
5733 for(cpl_size idline = 0; idline < nidentlines; ++idline)
5735 for(cpl_size detline = 0; detline < ndetectlines; ++detline)
5737 if(cpl_vector_get(peaks, detline) ==
5738 cpl_bivector_get_x_data(output)[idline])
5740 cpl_size table_pos = totalsize - ndetectlines + detline;
5741 double wave_ident = cpl_bivector_get_y_data(output)[idline] + refwave;
5742 double xpix_fit = cpl_polynomial_eval_1d(ids,
5743 wave_ident - refwave, NULL);
5744 double xpos_det = cpl_table_get_double(detected_lines,
5747 cpl_table_set_double(detected_lines,
5751 cpl_table_set_double(detected_lines,
5752 "xpos_fit_rect_wavecal",
5755 cpl_table_set_double(detected_lines,
5758 xpos_det - xpix_fit - 1);
5766 cpl_bivector * peaks_ident_used_fit;
5773 ids, refwave, uradius);
5776 cpl_bivector_delete(output);
5777 output = new_output;
5783 cpl_polynomial_delete(ids);
5785 countLines = cpl_bivector_get_size(output);
5787 if (countLines < 4) {
5788 cpl_bivector_delete(output);
5789 cpl_vector_delete(peaks);
5802 for (k = 0; k <= order; k++)
5803 cpl_table_set_invalid(idscoeff, clab[k], i);
5807 wavel = cpl_bivector_get_y(output);
5808 cpl_vector_subtract_scalar(wavel, refwave);
5810 uorder = countLines / 2 - 1;
5815 2 * (uorder + 1), &usedLines,
5816 &ids_err, &peaks_ident_used_fit);
5819 cpl_bivector_delete(output);
5820 cpl_vector_delete(peaks);
5833 for (k = 0; k <= order; k++)
5834 cpl_table_set_invalid(idscoeff, clab[k], i);
5840 for (k = 0; k <= order; k++) {
5842 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
5845 cpl_table_set_double(idscoeff, clab[k], i,
5846 cpl_polynomial_get_coeff(ids, &k));
5854 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
5855 cpl_size nidentlines = cpl_bivector_get_size(output);
5856 cpl_table_set_size(detected_lines, oldsize + nidentlines);
5857 for(cpl_size idline = 0; idline < nidentlines ; ++idline)
5859 double wave_ident = cpl_bivector_get_y_data(output)[idline] + refwave;
5860 double xpix_fit = cpl_polynomial_eval_1d(ids,
5861 wave_ident - refwave, NULL);
5862 cpl_table_set_double(detected_lines,
"xpos_iter",
5863 oldsize + idline, cpl_bivector_get_x_data(output)[idline] + 1);
5864 cpl_table_set_double(detected_lines,
"ypos_iter",
5865 oldsize + idline, (
double)i + 1);
5866 cpl_table_set_double(detected_lines,
"peak_flux",
5868 sdata[i*nx+(
int)(cpl_bivector_get_x_data(output)[idline]+0.5)]);
5869 cpl_table_set_double(detected_lines,
"wave_ident_iter",
5870 oldsize + idline, wave_ident);
5871 cpl_table_set_double(detected_lines,
"xpos_fit_rect_wavecal",
5872 oldsize + idline, xpix_fit + 1);
5879 nlines[i] = usedLines;
5881 error[i] = ids_err / sqrt(usedLines/(uorder + 1));
5883 pixstart = cpl_polynomial_eval_1d(ids,
5884 cpl_bivector_get_y_data(output)[0], NULL);
5885 pixend = cpl_polynomial_eval_1d(ids,
5886 cpl_bivector_get_y_data(output)[countLines-1], NULL);
5887 extrapolation = (pixend - pixstart) / 5;
5888 pixstart -= extrapolation;
5889 pixend += extrapolation;
5900 for (j = pixstart; j < pixend; j++) {
5902 lastLambda, refwave,
5911 for (j = 0; j < nl; j++) {
5912 lambda = firstLambda + j * dispersion;
5913 fpixel = cpl_polynomial_eval_1d(ids, lambda - refwave,
5916 if (pixel >= 0 && pixel < nx-1) {
5917 v1 = (sdata + i*nx)[pixel];
5918 v2 = (sdata + i*nx)[pixel+1];
5919 vi = v1 + (v2-v1)*(fpixel-pixel);
5920 (rdata + i*nl)[j] = vi;
5928 if (residuals || (restable && !(i%step))) {
5929 if (restable && !(i%step)) {
5930 lin = cpl_polynomial_new(1);
5931 for (k = 0; k < 2; k++)
5932 cpl_polynomial_set_coeff(lin, &k,
5933 cpl_polynomial_get_coeff(ids, &k));
5935 for (j = 0; j < countLines; j++) {
5936 pixe = cpl_bivector_get_x_data(output)[j];
5937 wave = cpl_bivector_get_y_data(output)[j];
5938 value = pixe - cpl_polynomial_eval_1d(ids, wave, NULL);
5941 (ddata + i*nx)[pixel] = value;
5943 if (restable && !(i%step)) {
5944 for (k = 0; k < nref; k++) {
5945 if (fabs(line[k] - refwave - wave) < 0.1) {
5946 snprintf(name, MAX_COLNAME,
"r%d", i);
5947 cpl_table_set_double(restable, name,
5950 - cpl_polynomial_eval_1d(lin, wave,
5952 snprintf(name, MAX_COLNAME,
"d%d", i);
5953 cpl_table_set_double(restable, name,
5955 snprintf(name, MAX_COLNAME,
"p%d", i);
5956 cpl_table_set_double(restable, name,
5963 if (restable && !(i%step)) {
5964 cpl_polynomial_delete(lin);
5973 mdata = cpl_mask_get_data(refmask);
5974 pixel = cpl_polynomial_eval_1d(ids, 0.0, NULL) + 0.5;
5975 if (pixel - 1 >= 0 && pixel + 1 < nx) {
5976 mdata[pixel-1 + i*nx] = CPL_BINARY_1;
5977 mdata[pixel + i*nx] = CPL_BINARY_1;
5978 mdata[pixel+1 + i*nx] = CPL_BINARY_1;
5982 cpl_polynomial_delete(ids);
5983 cpl_bivector_delete(output);
5985 cpl_vector_delete(peaks);
5990 kernel = cpl_matrix_new(3, 3);
5991 cpl_matrix_set(kernel, 0, 1, 1.0);
5992 cpl_matrix_set(kernel, 1, 1, 1.0);
5993 cpl_matrix_set(kernel, 2, 1, 1.0);
5995 cpl_mask_dilation(refmask, kernel);
5996 cpl_mask_erosion(refmask, kernel);
5997 cpl_mask_erosion(refmask, kernel);
5998 cpl_mask_dilation(refmask, kernel);
6000 cpl_matrix_delete(kernel);
6006 mdata = cpl_mask_get_data(refmask);
6007 have_it = cpl_calloc(ny,
sizeof(
int));
6009 for (i = 0; i < ny; i++, mdata += nx) {
6010 for (j = 0; j < nx; j++) {
6011 if (mdata[j] == CPL_BINARY_1) {
6018 mdata = cpl_mask_get_data(refmask);
6022 for (i = 0; i < ny; i++) {
6028 if (abs(have_it[first] - have_it[last]) < 3) {
6029 for (j = first; j < last; j++) {
6030 mdata[have_it[first] + nx*j + 0] = CPL_BINARY_1;
6031 mdata[have_it[first] + nx*j + 1] = CPL_BINARY_1;
6032 mdata[have_it[first] + nx*j + 2] = CPL_BINARY_1;
6174 const char *func =
"mos_locate_spectra";
6176 cpl_apertures *slits;
6177 cpl_image *labimage;
6178 cpl_image *refimage;
6180 cpl_propertylist *sort_col;
6186 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
6190 labimage = cpl_image_labelise_mask_create(mask, &nslits);
6193 cpl_image_delete(labimage);
6194 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6198 refimage = cpl_image_new_from_mask(mask);
6200 slits = cpl_apertures_new_from_image(refimage, labimage);
6202 cpl_image_delete(labimage);
6203 cpl_image_delete(refimage);
6205 nslits = cpl_apertures_get_size(slits);
6207 cpl_apertures_delete(slits);
6208 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6212 slitpos = cpl_table_new(nslits);
6213 cpl_table_new_column(slitpos,
"xtop", CPL_TYPE_DOUBLE);
6214 cpl_table_new_column(slitpos,
"ytop", CPL_TYPE_DOUBLE);
6215 cpl_table_new_column(slitpos,
"xbottom", CPL_TYPE_DOUBLE);
6216 cpl_table_new_column(slitpos,
"ybottom", CPL_TYPE_DOUBLE);
6217 cpl_table_set_column_unit(slitpos,
"xtop",
"pixel");
6218 cpl_table_set_column_unit(slitpos,
"ytop",
"pixel");
6219 cpl_table_set_column_unit(slitpos,
"xbottom",
"pixel");
6220 cpl_table_set_column_unit(slitpos,
"ybottom",
"pixel");
6222 for (i = 0; i < nslits; i++) {
6223 cpl_table_set_double(slitpos,
"xtop", i,
6224 cpl_apertures_get_top_x(slits, i+1) - 1);
6225 cpl_table_set_double(slitpos,
"ytop", i,
6226 cpl_apertures_get_top(slits, i+1));
6227 cpl_table_set_double(slitpos,
"xbottom", i,
6228 cpl_apertures_get_bottom_x(slits, i+1) - 1);
6229 cpl_table_set_double(slitpos,
"ybottom", i,
6230 cpl_apertures_get_bottom(slits, i+1));
6233 cpl_apertures_delete(slits);
6235 sort_col = cpl_propertylist_new();
6236 cpl_propertylist_append_bool(sort_col,
"ytop", 1);
6237 cpl_table_sort(slitpos, sort_col);
6238 cpl_propertylist_delete(sort_col);
6262 const char *func =
"mos_validate_slits";
6266 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
6268 if (1 != cpl_table_has_column(slits,
"xtop"))
6269 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6271 if (1 != cpl_table_has_column(slits,
"ytop"))
6272 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6274 if (1 != cpl_table_has_column(slits,
"xbottom"))
6275 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6277 if (1 != cpl_table_has_column(slits,
"ybottom"))
6278 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
6280 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"xtop"))
6281 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6283 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"ytop"))
6284 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6286 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"xbottom"))
6287 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6289 if (CPL_TYPE_DOUBLE != cpl_table_get_column_type(slits,
"ybottom"))
6290 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
6292 return CPL_ERROR_NONE;
6326 const char *func =
"mos_rotate_slits";
6328 cpl_error_code error;
6329 char aux_name[] =
"_0";
6338 return CPL_ERROR_NONE;
6342 return cpl_error_set(func, error);
6344 if (rotation == 1 || rotation == 3) {
6350 for (i = 0; i < 77; i++)
6351 if (1 == cpl_table_has_column(slits, aux_name))
6353 if (1 == cpl_table_has_column(slits, aux_name))
6354 return cpl_error_set(func, CPL_ERROR_CONTINUE);
6355 cpl_table_name_column(slits,
"xtop", aux_name);
6356 cpl_table_name_column(slits,
"ytop",
"xtop");
6357 cpl_table_name_column(slits, aux_name,
"ytop");
6358 cpl_table_name_column(slits,
"xbottom", aux_name);
6359 cpl_table_name_column(slits,
"ybottom",
"xbottom");
6360 cpl_table_name_column(slits, aux_name,
"ybottom");
6363 if (rotation == 1 || rotation == 2) {
6364 cpl_table_multiply_scalar(slits,
"xtop", -1.0);
6365 cpl_table_multiply_scalar(slits,
"xbottom", -1.0);
6366 cpl_table_add_scalar(slits,
"xtop", nx);
6367 cpl_table_add_scalar(slits,
"xbottom", nx);
6370 if (rotation == 3 || rotation == 2) {
6371 cpl_table_multiply_scalar(slits,
"ytop", -1.0);
6372 cpl_table_multiply_scalar(slits,
"ybottom", -1.0);
6373 cpl_table_add_scalar(slits,
"ytop", ny);
6374 cpl_table_add_scalar(slits,
"ybottom", ny);
6377 return CPL_ERROR_NONE;
6441 cpl_array *top_ident = NULL;;
6442 cpl_array *bot_ident = NULL;;
6444 cpl_matrix *mpattern;
6445 cpl_matrix *top_data;
6446 cpl_matrix *top_pattern;
6447 cpl_matrix *top_mdata;
6448 cpl_matrix *top_mpattern;
6449 cpl_matrix *bot_data;
6450 cpl_matrix *bot_pattern;
6451 cpl_matrix *bot_mdata;
6452 cpl_matrix *bot_mpattern;
6453 cpl_propertylist *sort_col;
6462 double top_scale, bot_scale;
6463 double angle, top_angle, bot_angle;
6465 double xrms, top_xrms, bot_xrms;
6466 double yrms, top_yrms, bot_yrms;
6468 int nmaskslits, use_pattern;
6469 int found_slits, found_slits_top, found_slits_bot;
6471 cpl_table *positions;
6472 cpl_error_code error;
6481 cpl_polynomial *xpoly = NULL;
6482 cpl_polynomial *ypoly = NULL;
6483 cpl_polynomial *top_xpoly = NULL;
6484 cpl_polynomial *top_ypoly = NULL;
6485 cpl_polynomial *bot_xpoly = NULL;
6486 cpl_polynomial *bot_ypoly = NULL;
6488 char *msg_multiplex =
" ";
6493 cpl_msg_error(cpl_func,
"CCD slits table validation: %s",
6494 cpl_error_get_message());
6495 cpl_error_set(cpl_func, error);
6501 cpl_msg_error(cpl_func,
"Mask slits table validation: %s",
6502 cpl_error_get_message());
6503 cpl_error_set(cpl_func, error);
6507 if (1 != cpl_table_has_column(maskslits,
"slit_id")) {
6508 cpl_msg_error(cpl_func,
"Missing slits identifiers");
6509 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
6513 if (CPL_TYPE_INT != cpl_table_get_column_type(maskslits,
"slit_id")) {
6514 cpl_msg_error(cpl_func,
"Wrong type used for slits identifiers");
6515 cpl_error_set(cpl_func, CPL_ERROR_INVALID_TYPE);
6519 nslits = cpl_table_get_nrow(slits);
6520 nmaskslits = cpl_table_get_nrow(maskslits);
6522 if (nslits == 0 || nmaskslits == 0) {
6523 cpl_msg_error(cpl_func,
"Empty slits table");
6524 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
6528 if (nslits > 100 && mos_multiplex < 0) {
6529 cpl_msg_info(cpl_func,
"Many slits: using 'fast' pattern matching...");
6530 positions = mos_identify_slits_fast(slits, maskslits, global);
6531 if (positions == NULL)
6532 cpl_error_set_where(cpl_func);
6540 sort_col = cpl_propertylist_new();
6541 cpl_propertylist_append_bool(sort_col,
"ytop", 1);
6542 cpl_table_sort(slits, sort_col);
6543 cpl_table_sort(maskslits, sort_col);
6544 cpl_propertylist_delete(sort_col);
6550 if (nslits < 3 && nmaskslits > nslits) {
6560 cpl_msg_warning(cpl_func,
"Cannot match the %d found CCD slits "
6561 "with the %d mask slits: process will continue "
6562 "using the detected CCD slits positions", nslits,
6565 cpl_msg_warning(cpl_func,
"Cannot match the found CCD slit with "
6566 "the %d mask slits: process will continue using "
6567 "the detected CCD slit position", nmaskslits);
6571 if (nmaskslits < 3 && nslits > nmaskslits) {
6579 cpl_msg_warning(cpl_func,
"Cannot match the %d found CCD slits with "
6580 "the %d mask slits: process will continue using "
6581 "the detected CCD slits positions", nslits,
6593 xtop = cpl_table_get_data_double(slits,
"xtop");
6594 ytop = cpl_table_get_data_double(slits,
"ytop");
6595 xmtop = cpl_table_get_data_double(maskslits,
"xtop");
6596 ymtop = cpl_table_get_data_double(maskslits,
"ytop");
6598 xbot = cpl_table_get_data_double(slits,
"xbottom");
6599 ybot = cpl_table_get_data_double(slits,
"ybottom");
6600 xmbot = cpl_table_get_data_double(maskslits,
"xbottom");
6601 ymbot = cpl_table_get_data_double(maskslits,
"ybottom");
6603 top_data = cpl_matrix_new(2, nslits);
6604 top_pattern = cpl_matrix_new(2, nmaskslits);
6605 bot_data = cpl_matrix_new(2, nslits);
6606 bot_pattern = cpl_matrix_new(2, nmaskslits);
6608 for (i = 0; i < nslits; i++)
6609 cpl_matrix_set(top_data, 0, i, xtop[i]);
6611 for (i = 0; i < nslits; i++)
6612 cpl_matrix_set(top_data, 1, i, ytop[i]);
6614 for (i = 0; i < nmaskslits; i++)
6615 cpl_matrix_set(top_pattern, 0, i, xmtop[i]);
6617 for (i = 0; i < nmaskslits; i++)
6618 cpl_matrix_set(top_pattern, 1, i, ymtop[i]);
6620 for (i = 0; i < nslits; i++)
6621 cpl_matrix_set(bot_data, 0, i, xbot[i]);
6623 for (i = 0; i < nslits; i++)
6624 cpl_matrix_set(bot_data, 1, i, ybot[i]);
6626 for (i = 0; i < nmaskslits; i++)
6627 cpl_matrix_set(bot_pattern, 0, i, xmbot[i]);
6629 for (i = 0; i < nmaskslits; i++)
6630 cpl_matrix_set(bot_pattern, 1, i, ymbot[i]);
6632 if (nmaskslits > nslits)
6633 use_pattern = nslits;
6635 use_pattern = nmaskslits;
6637 top_ident = cpl_ppm_match_points(top_data, nslits, 1.0, top_pattern,
6638 use_pattern, 0.0, 0.1, 5, &top_mdata,
6639 &top_mpattern, &top_scale, &top_angle);
6641 bot_ident = cpl_ppm_match_points(bot_data, nslits, 1.0, bot_pattern,
6642 use_pattern, 0.0, 0.1, 5, &bot_mdata,
6643 &bot_mpattern, &bot_scale, &bot_angle);
6644 cpl_matrix_delete(top_data);
6645 cpl_matrix_delete(top_pattern);
6646 cpl_matrix_delete(bot_data);
6647 cpl_matrix_delete(bot_pattern);
6649 if (top_ident == NULL && bot_ident == NULL) {
6650 cpl_msg_warning(cpl_func,
"Pattern matching failure: cannot match "
6651 "the %d found CCD slits with the %d mask slits: "
6652 "process will continue using the detected CCD "
6653 "slits positions", nslits, nmaskslits);
6657 found_slits_top = 0;
6658 found_slits_bot = 0;
6659 if (top_ident && bot_ident) {
6660 cpl_msg_info(cpl_func,
"Median platescale: %f +/- %f pixel/mm",
6661 (top_scale + bot_scale) / 2, fabs(top_scale - bot_scale));
6662 cpl_msg_info(cpl_func,
"Median rotation: %f +/- %f degrees",
6663 (top_angle + bot_angle) / 2, fabs(top_angle - bot_angle));
6664 if (fabs(top_angle) < fabs(bot_angle))
6665 angle = fabs(top_angle);
6667 angle = fabs(bot_angle);
6668 found_slits_top = cpl_matrix_get_ncol(top_mdata);
6669 found_slits_bot = cpl_matrix_get_ncol(bot_mdata);
6671 else if (top_ident) {
6672 cpl_msg_info(cpl_func,
"Median platescale: %f pixel/mm", top_scale);
6673 cpl_msg_info(cpl_func,
"Median rotation: %f degrees", top_angle);
6674 angle = fabs(top_angle);
6675 found_slits_top = cpl_matrix_get_ncol(top_mdata);
6678 cpl_msg_info(cpl_func,
"Median platescale: %f pixel/mm", bot_scale);
6679 cpl_msg_info(cpl_func,
"Median rotation: %f degrees", bot_angle);
6680 angle = fabs(bot_angle);
6681 found_slits_bot = cpl_matrix_get_ncol(bot_mdata);
6684 cpl_array_delete(top_ident);
6685 cpl_array_delete(bot_ident);
6688 cpl_msg_warning(cpl_func,
"Uncertain pattern matching: the rotation "
6689 "angle is expected to be around zero. This match is "
6690 "rejected: the process will continue using the %d "
6691 "detected CCD slits positions", nslits);
6695 found_slits = found_slits_top;
6696 if (found_slits < found_slits_bot)
6697 found_slits = found_slits_bot;
6699 if (found_slits < 4) {
6700 cpl_msg_warning(cpl_func,
6701 "Too few safely identified slits: %d out of %d "
6702 "candidates (%d expected). Process will continue "
6703 "using the detected CCD slits positions", found_slits,
6704 nslits, nmaskslits);
6708 cpl_msg_info(cpl_func,
"Preliminary identified slits: %d out of %d "
6709 "candidates\n(%d expected)", found_slits, nslits,
6712 if (found_slits_top < 4)
6713 found_slits_top = 0;
6715 if (found_slits_bot < 4)
6716 found_slits_bot = 0;
6724 for (i = 0; i < 2; i++) {
6725 cpl_size mindeg2d[] = {0, 0};
6726 cpl_size maxdeg2d[2];
6727 cpl_vector * fitresidual;
6729 found_slits = found_slits_top;
6731 mpattern = top_mpattern;
6734 found_slits = found_slits_bot;
6736 mpattern = bot_mpattern;
6739 if (found_slits == 0)
6741 else if (found_slits < 10)
6742 maxdeg2d[0] = maxdeg2d[1] = 1;
6744 maxdeg2d[0] = maxdeg2d[1] = 2;
6746 xpos = cpl_vector_wrap(found_slits,
6747 cpl_matrix_get_data(mdata) );
6748 ypos = cpl_vector_wrap(found_slits,
6749 cpl_matrix_get_data(mdata) + found_slits);
6750 xmpos = cpl_vector_wrap(found_slits,
6751 cpl_matrix_get_data(mpattern) );
6752 ympos = cpl_vector_wrap(found_slits,
6753 cpl_matrix_get_data(mpattern) + found_slits);
6754 mpos = cpl_bivector_wrap_vectors(xmpos, ympos);
6755 fitresidual = cpl_vector_new(cpl_vector_get_size(xpos));
6756 xpoly = cpl_polynomial_new(2);
6757 cpl_polynomial_fit(xpoly, mpattern, NULL, xpos, NULL, CPL_FALSE, mindeg2d, maxdeg2d);
6758 cpl_vector_fill_polynomial_fit_residual(fitresidual, xpos, NULL, xpoly, mpattern, NULL);
6759 xmse = cpl_vector_product(fitresidual, fitresidual)
6760 / cpl_vector_get_size(fitresidual);
6761 ypoly = cpl_polynomial_new(2);
6762 cpl_polynomial_fit(ypoly, mpattern, NULL, ypos, NULL, CPL_FALSE, mindeg2d, maxdeg2d);
6763 cpl_vector_fill_polynomial_fit_residual(fitresidual, ypos, NULL, ypoly, mpattern, NULL);
6764 ymse = cpl_vector_product(fitresidual, fitresidual)
6765 / cpl_vector_get_size(fitresidual);
6767 cpl_bivector_unwrap_vectors(mpos);
6768 cpl_vector_unwrap(xpos);
6769 cpl_vector_unwrap(ypos);
6770 cpl_vector_unwrap(xmpos);
6771 cpl_vector_unwrap(ympos);
6772 cpl_matrix_delete(mdata);
6773 cpl_matrix_delete(mpattern);
6774 cpl_vector_delete(fitresidual);
6779 top_xrms = sqrt(xmse*2*maxdeg2d[0]/(found_slits - 1));
6780 top_yrms = sqrt(ymse*2*maxdeg2d[0]/(found_slits - 1));
6785 bot_xrms = sqrt(xmse*2*maxdeg2d[0]/(found_slits - 1));
6786 bot_yrms = sqrt(ymse*2*maxdeg2d[0]/(found_slits - 1));
6790 if (top_xpoly && bot_xpoly) {
6791 if (top_xrms < bot_xrms) {
6794 cpl_polynomial_delete(bot_xpoly);
6799 cpl_polynomial_delete(top_xpoly);
6802 else if (top_xpoly) {
6811 if (top_ypoly && bot_ypoly) {
6812 if (top_yrms < bot_yrms) {
6815 cpl_polynomial_delete(bot_ypoly);
6820 cpl_polynomial_delete(top_ypoly);
6823 else if (top_ypoly) {
6832 if (xpoly == NULL || ypoly == NULL) {
6833 cpl_msg_warning(cpl_func,
"Fit failure: the accuracy of the "
6834 "identified slits positions cannot be improved.");
6835 cpl_polynomial_delete(xpoly);
6836 cpl_polynomial_delete(ypoly);
6841 cpl_msg_info(cpl_func,
6842 "Fit successful: X rms = %.3g, Y rms = %.3g (pixel)",
6846 write_global_distortion(global, 0, xpoly);
6847 write_global_distortion(global, 7, ypoly);
6855 positions = cpl_table_duplicate(maskslits);
6856 cpl_table_duplicate_column(positions,
"xmtop", positions,
"xtop");
6857 cpl_table_duplicate_column(positions,
"ymtop", positions,
"ytop");
6858 cpl_table_duplicate_column(positions,
"xmbottom", positions,
"xbottom");
6859 cpl_table_duplicate_column(positions,
"ymbottom", positions,
"ybottom");
6861 point = cpl_vector_new(2);
6862 dpoint = cpl_vector_get_data(point);
6864 for (i = 0; i < nmaskslits; i++) {
6868 dpoint[0] = cpl_table_get_double(positions,
"xmtop", i, NULL);
6869 dpoint[1] = cpl_table_get_double(positions,
"ymtop", i, NULL);
6870 position_x = cpl_polynomial_eval(xpoly, point);
6877 cpl_table_set_double(positions,
"xtop", i, position_x);
6878 position_y = cpl_polynomial_eval(ypoly, point);
6879 cpl_table_set_double(positions,
"ytop", i, position_y);
6880 dpoint[0] = cpl_table_get_double(positions,
"xmbottom", i, NULL);
6881 dpoint[1] = cpl_table_get_double(positions,
"ymbottom", i, NULL);
6882 position_x = cpl_polynomial_eval(xpoly, point);
6883 cpl_table_set_double(positions,
"xbottom", i, position_x);
6884 position_y = cpl_polynomial_eval(ypoly, point);
6885 cpl_table_set_double(positions,
"ybottom", i, position_y);
6894 cpl_vector_delete(point);
6895 cpl_polynomial_delete(xpoly);
6896 cpl_polynomial_delete(ypoly);
6898 cpl_table_erase_column(positions,
"xmtop");
6899 cpl_table_erase_column(positions,
"ymtop");
6900 cpl_table_erase_column(positions,
"xmbottom");
6901 cpl_table_erase_column(positions,
"ymbottom");
6903 if (mos_multiplex >= 0) {
6905 cpl_sprintf(
"in the CCD section between %d and %d pixel",
6906 mos_multiplex * mos_region_size,
6907 (mos_multiplex + 1) * mos_region_size);
6910 if (nmaskslits > nslits)
6911 cpl_msg_info(cpl_func,
6912 "Finally identified slits: %d out of %d expected %s\n"
6913 "(%d recovered)", nmaskslits, nmaskslits, msg_multiplex,
6914 nmaskslits - nslits);
6915 else if (nmaskslits < nslits)
6916 cpl_msg_info(cpl_func,
6917 "Finally identified slits: %d out of %d expected %s\n"
6918 "(%d rejected)", nmaskslits, nmaskslits, msg_multiplex,
6919 nslits - nmaskslits);
6921 cpl_msg_info(cpl_func,
6922 "Finally identified slits: %d out of %d expected %s",
6923 nmaskslits, nmaskslits, msg_multiplex);
6925 if (mos_multiplex >= 0) {
6926 cpl_free(msg_multiplex);
6934 cpl_table *mos_identify_slits_fast(cpl_table *slits, cpl_table *maskslits,
6937 const char *func =
"mos_identify_slits_fast";
6939 cpl_propertylist *sort_col;
6940 cpl_table *positions;
6949 cpl_polynomial *xpoly = NULL;
6950 cpl_polynomial *ypoly = NULL;
6951 cpl_error_code error;
6957 double dist1, dist2, dist3, dist, mindist;
6958 double scale, minscale, maxscale;
6959 double angle, minangle, maxangle;
6986 double sradius = 0.01;
6989 double pi = 3.14159265358979323846;
6994 cpl_msg_error(func,
"CCD slits table validation: %s",
6995 cpl_error_get_message());
6996 cpl_error_set(func, error);
7002 cpl_msg_error(func,
"Mask slits table validation: %s",
7003 cpl_error_get_message());
7004 cpl_error_set(func, error);
7008 if (1 != cpl_table_has_column(maskslits,
"slit_id")) {
7009 cpl_msg_error(func,
"Missing slits identifiers");
7010 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
7014 if (CPL_TYPE_INT != cpl_table_get_column_type(maskslits,
"slit_id")) {
7015 cpl_msg_error(func,
"Wrong type used for slits identifiers");
7016 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
7020 nslits = cpl_table_get_nrow(slits);
7021 nmaskslits = cpl_table_get_nrow(maskslits);
7023 if (nslits == 0 || nmaskslits == 0) {
7024 cpl_msg_error(func,
"Empty slits table");
7025 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
7035 if (cpl_table_has_column(slits,
"xcenter"))
7036 cpl_table_erase_column(slits,
"xcenter");
7038 if (cpl_table_has_column(slits,
"ycenter"))
7039 cpl_table_erase_column(slits,
"ycenter");
7041 if (cpl_table_has_column(maskslits,
"xcenter"))
7042 cpl_table_erase_column(maskslits,
"xcenter");
7044 if (cpl_table_has_column(maskslits,
"ycenter"))
7045 cpl_table_erase_column(maskslits,
"ycenter");
7047 cpl_table_duplicate_column(slits,
"xcenter", slits,
"xtop");
7048 cpl_table_add_columns(slits,
"xcenter",
"xbottom");
7049 cpl_table_divide_scalar(slits,
"xcenter", 2.0);
7050 cpl_table_duplicate_column(slits,
"ycenter", slits,
"ytop");
7051 cpl_table_add_columns(slits,
"ycenter",
"ybottom");
7052 cpl_table_divide_scalar(slits,
"ycenter", 2.0);
7054 cpl_table_duplicate_column(maskslits,
"xcenter", maskslits,
"xtop");
7055 cpl_table_add_columns(maskslits,
"xcenter",
"xbottom");
7056 cpl_table_divide_scalar(maskslits,
"xcenter", 2.0);
7057 cpl_table_duplicate_column(maskslits,
"ycenter", maskslits,
"ytop");
7058 cpl_table_add_columns(maskslits,
"ycenter",
"ybottom");
7059 cpl_table_divide_scalar(maskslits,
"ycenter", 2.0);
7066 sort_col = cpl_propertylist_new();
7067 cpl_propertylist_append_bool(sort_col,
"ycenter", 1);
7068 cpl_table_sort(slits, sort_col);
7069 cpl_table_sort(maskslits, sort_col);
7070 cpl_propertylist_delete(sort_col);
7077 if (nslits < 3 && nmaskslits > nslits) {
7087 cpl_msg_warning(func,
"Cannot match the found CCD slit with the "
7088 "%d mask slits: process will continue using the "
7089 "detected CCD slit position", nmaskslits);
7091 cpl_msg_warning(func,
"Cannot match the %d found CCD slits with "
7092 "the %d mask slits: process will continue using "
7093 "the detected CCD slits positions", nslits,
7098 if (nslits <= 3 && nslits == nmaskslits) {
7100 cpl_msg_warning(func,
"Too few slits (%d) on mask and CCD", nslits);
7101 cpl_msg_warning(func,
"Their detected positions are left unchanged");
7112 positions = cpl_table_duplicate(slits);
7113 cpl_table_erase_column(slits,
"xcenter");
7114 cpl_table_erase_column(slits,
"ycenter");
7115 cpl_table_duplicate_column(positions,
"xmtop", maskslits,
"xtop");
7116 cpl_table_duplicate_column(positions,
"ymtop", maskslits,
"ytop");
7117 cpl_table_duplicate_column(positions,
"xmbottom", maskslits,
"xbottom");
7118 cpl_table_duplicate_column(positions,
"ymbottom", maskslits,
"ybottom");
7119 cpl_table_duplicate_column(positions,
"xmcenter", maskslits,
"xcenter");
7120 cpl_table_duplicate_column(positions,
"ymcenter", maskslits,
"ycenter");
7121 cpl_table_duplicate_column(positions,
"slit_id", maskslits,
"slit_id");
7122 cpl_table_erase_column(maskslits,
"xcenter");
7123 cpl_table_erase_column(maskslits,
"ycenter");
7126 xcenter = cpl_table_get_data_double(positions,
"xcenter");
7127 ycenter = cpl_table_get_data_double(positions,
"ycenter");
7128 xmcenter = cpl_table_get_data_double(positions,
"xmcenter");
7129 ymcenter = cpl_table_get_data_double(positions,
"ymcenter");
7131 dist1 = (xcenter[0] - xcenter[1])*(xcenter[0] - xcenter[1])
7132 + (ycenter[0] - ycenter[1])*(ycenter[0] - ycenter[1]);
7133 dist2 = (xmcenter[0] - xmcenter[1])*(xmcenter[0] - xmcenter[1])
7134 + (ymcenter[0] - ymcenter[1])*(ymcenter[0] - ymcenter[1]);
7135 scale = sqrt(dist1/dist2);
7138 dist1 = (xcenter[1] - xcenter[2])*(xcenter[1] - xcenter[2])
7139 + (ycenter[1] - ycenter[2])*(ycenter[1] - ycenter[2]);
7140 dist2 = (xmcenter[1] - xmcenter[2])*(xmcenter[1] - xmcenter[2])
7141 + (ymcenter[1] - ymcenter[2])*(ymcenter[1] - ymcenter[2]);
7142 scale += sqrt(dist1/dist2);
7146 cpl_msg_info(func,
"Platescale: %f pixel/mm", scale);
7152 if (nmaskslits < 3 && nslits > nmaskslits) {
7160 cpl_msg_warning(func,
"Cannot match the %d found CCD slits with "
7161 "the %d mask slits: process will continue using "
7162 "the detected CCD slits positions", nslits,
7194 if (cpl_table_has_column(slits,
"xpseudo"))
7195 cpl_table_erase_column(slits,
"xpseudo");
7197 if (cpl_table_has_column(slits,
"ypseudo"))
7198 cpl_table_erase_column(slits,
"ypseudo");
7200 if (cpl_table_has_column(maskslits,
"xpseudo"))
7201 cpl_table_erase_column(maskslits,
"xpseudo");
7203 if (cpl_table_has_column(maskslits,
"ypseudo"))
7204 cpl_table_erase_column(maskslits,
"ypseudo");
7206 cpl_table_duplicate_column(slits,
"xpseudo", slits,
"xcenter");
7207 cpl_table_duplicate_column(slits,
"ypseudo", slits,
"ycenter");
7209 xcenter = cpl_table_get_data_double(slits,
"xcenter");
7210 ycenter = cpl_table_get_data_double(slits,
"ycenter");
7211 xpseudo = cpl_table_get_data_double(slits,
"xpseudo");
7212 ypseudo = cpl_table_get_data_double(slits,
"ypseudo");
7214 for (i = 1; i < nslits - 1; i++) {
7215 dist1 = (xcenter[i-1] - xcenter[i]) * (xcenter[i-1] - xcenter[i])
7216 + (ycenter[i-1] - ycenter[i]) * (ycenter[i-1] - ycenter[i]);
7217 dist2 = (xcenter[i-1] - xcenter[i+1]) * (xcenter[i-1] - xcenter[i+1])
7218 + (ycenter[i-1] - ycenter[i+1]) * (ycenter[i-1] - ycenter[i+1]);
7219 dist3 = (xcenter[i] - xcenter[i+1]) * (xcenter[i] - xcenter[i+1])
7220 + (ycenter[i] - ycenter[i+1]) * (ycenter[i] - ycenter[i+1]);
7221 xpseudo[i] = sqrt(dist1/dist2);
7222 ypseudo[i] = sqrt(dist3/dist2);
7225 cpl_table_set_invalid(slits,
"xpseudo", 0);
7226 cpl_table_set_invalid(slits,
"xpseudo", nslits-1);
7227 cpl_table_set_invalid(slits,
"ypseudo", 0);
7228 cpl_table_set_invalid(slits,
"ypseudo", nslits-1);
7230 cpl_table_duplicate_column(maskslits,
"xpseudo", maskslits,
"xcenter");
7231 cpl_table_duplicate_column(maskslits,
"ypseudo", maskslits,
"ycenter");
7233 xcenter = cpl_table_get_data_double(maskslits,
"xcenter");
7234 ycenter = cpl_table_get_data_double(maskslits,
"ycenter");
7235 xmpseudo = cpl_table_get_data_double(maskslits,
"xpseudo");
7236 ympseudo = cpl_table_get_data_double(maskslits,
"ypseudo");
7238 for (i = 1; i < nmaskslits - 1; i++) {
7239 dist1 = (xcenter[i-1] - xcenter[i])*(xcenter[i-1] - xcenter[i])
7240 + (ycenter[i-1] - ycenter[i])*(ycenter[i-1] - ycenter[i]);
7241 dist2 = (xcenter[i-1] - xcenter[i+1])*(xcenter[i-1] - xcenter[i+1])
7242 + (ycenter[i-1] - ycenter[i+1])*(ycenter[i-1] - ycenter[i+1]);
7243 dist3 = (xcenter[i] - xcenter[i+1])*(xcenter[i] - xcenter[i+1])
7244 + (ycenter[i] - ycenter[i+1])*(ycenter[i] - ycenter[i+1]);
7245 xmpseudo[i] = sqrt(dist1/dist2);
7246 ympseudo[i] = sqrt(dist3/dist2);
7249 cpl_table_set_invalid(maskslits,
"xpseudo", 0);
7250 cpl_table_set_invalid(maskslits,
"xpseudo", nmaskslits-1);
7251 cpl_table_set_invalid(maskslits,
"ypseudo", 0);
7252 cpl_table_set_invalid(maskslits,
"ypseudo", nmaskslits-1);
7264 if (cpl_table_has_column(slits,
"slit_id"))
7265 cpl_table_erase_column(slits,
"slit_id");
7266 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
7267 slit_id = cpl_table_get_data_int(maskslits,
"slit_id");
7269 for (i = 1; i < nmaskslits - 1; i++) {
7271 mindist = (xmpseudo[i] - xpseudo[1]) * (xmpseudo[i] - xpseudo[1])
7272 + (ympseudo[i] - ypseudo[1]) * (ympseudo[i] - ypseudo[1]);
7274 if (mindist < sradius*sradius)
7276 for (j = 2; j < nslits - 1; j++) {
7277 dist = (xmpseudo[i] - xpseudo[j]) * (xmpseudo[i] - xpseudo[j])
7278 + (ympseudo[i] - ypseudo[j]) * (ympseudo[i] - ypseudo[j]);
7279 if (dist < sradius*sradius)
7283 if (mindist > dist) {
7289 mindist = sqrt(mindist);
7291 if (mindist < sradius && in_sradius == 1) {
7292 cpl_table_set_int(slits,
"slit_id", minpos-1, slit_id[i-1]);
7293 cpl_table_set_int(slits,
"slit_id", minpos, slit_id[i]);
7294 cpl_table_set_int(slits,
"slit_id", minpos+1, slit_id[i+1]);
7304 found_slits = nslits - cpl_table_count_invalid(slits,
"slit_id");
7306 if (found_slits < 3) {
7307 cpl_msg_warning(func,
"Too few preliminarily identified slits: "
7308 "%d out of %d", found_slits, nslits);
7309 if (nslits == nmaskslits) {
7310 cpl_msg_warning(func,
"(this is not an error, it could be caused "
7311 "by a mask with regularly located slits)");
7312 cpl_msg_warning(func,
"The detected slits positions are left "
7322 cpl_table_erase_column(slits,
"slit_id");
7323 cpl_table_erase_column(slits,
"xpseudo");
7324 cpl_table_erase_column(slits,
"ypseudo");
7325 positions = cpl_table_duplicate(slits);
7326 cpl_table_erase_column(slits,
"xcenter");
7327 cpl_table_erase_column(slits,
"ycenter");
7329 cpl_table_erase_column(maskslits,
"xpseudo");
7330 cpl_table_erase_column(maskslits,
"ypseudo");
7331 cpl_table_duplicate_column(positions,
"xmtop",
7333 cpl_table_duplicate_column(positions,
"ymtop",
7335 cpl_table_duplicate_column(positions,
"xmbottom",
7336 maskslits,
"xbottom");
7337 cpl_table_duplicate_column(positions,
"ymbottom",
7338 maskslits,
"ybottom");
7339 cpl_table_duplicate_column(positions,
"xmcenter",
7340 maskslits,
"xcenter");
7341 cpl_table_duplicate_column(positions,
"ymcenter",
7342 maskslits,
"ycenter");
7343 cpl_table_duplicate_column(positions,
"slit_id",
7344 maskslits,
"slit_id");
7345 cpl_table_erase_column(maskslits,
"xcenter");
7346 cpl_table_erase_column(maskslits,
"ycenter");
7350 cpl_table_erase_column(slits,
"slit_id");
7351 cpl_table_erase_column(slits,
"xpseudo");
7352 cpl_table_erase_column(slits,
"ypseudo");
7353 positions = cpl_table_duplicate(slits);
7354 cpl_table_erase_column(slits,
"xcenter");
7355 cpl_table_erase_column(slits,
"ycenter");
7356 cpl_msg_warning(func,
"(the failure could be caused "
7357 "by a mask with regularly located slits)");
7362 cpl_msg_info(func,
"Preliminarily identified slits: %d out of %d "
7363 "candidates (%d expected)", found_slits, nslits,
7374 positions = cpl_table_new(found_slits);
7375 cpl_table_new_column(positions,
"slit_id", CPL_TYPE_INT);
7376 cpl_table_new_column(positions,
"xtop", CPL_TYPE_DOUBLE);
7377 cpl_table_new_column(positions,
"ytop", CPL_TYPE_DOUBLE);
7378 cpl_table_new_column(positions,
"xbottom", CPL_TYPE_DOUBLE);
7379 cpl_table_new_column(positions,
"ybottom", CPL_TYPE_DOUBLE);
7380 cpl_table_new_column(positions,
"xcenter", CPL_TYPE_DOUBLE);
7381 cpl_table_new_column(positions,
"ycenter", CPL_TYPE_DOUBLE);
7382 cpl_table_new_column(positions,
"xmtop", CPL_TYPE_DOUBLE);
7383 cpl_table_new_column(positions,
"ymtop", CPL_TYPE_DOUBLE);
7384 cpl_table_new_column(positions,
"xmbottom", CPL_TYPE_DOUBLE);
7385 cpl_table_new_column(positions,
"ymbottom", CPL_TYPE_DOUBLE);
7386 cpl_table_new_column(positions,
"xmcenter", CPL_TYPE_DOUBLE);
7387 cpl_table_new_column(positions,
"ymcenter", CPL_TYPE_DOUBLE);
7388 cpl_table_new_column(positions,
"good", CPL_TYPE_INT);
7389 cpl_table_fill_column_window_int(positions,
"good", 0, found_slits, 0);
7391 slit_id = cpl_table_get_data_int (slits,
"slit_id");
7392 xtop = cpl_table_get_data_double(slits,
"xtop");
7393 ytop = cpl_table_get_data_double(slits,
"ytop");
7394 xbottom = cpl_table_get_data_double(slits,
"xbottom");
7395 ybottom = cpl_table_get_data_double(slits,
"ybottom");
7396 xcenter = cpl_table_get_data_double(slits,
"xcenter");
7397 ycenter = cpl_table_get_data_double(slits,
"ycenter");
7399 mslit_id = cpl_table_get_data_int (maskslits,
"slit_id");
7400 xmtop = cpl_table_get_data_double(maskslits,
"xtop");
7401 ymtop = cpl_table_get_data_double(maskslits,
"ytop");
7402 xmbottom = cpl_table_get_data_double(maskslits,
"xbottom");
7403 ymbottom = cpl_table_get_data_double(maskslits,
"ybottom");
7404 xmcenter = cpl_table_get_data_double(maskslits,
"xcenter");
7405 ymcenter = cpl_table_get_data_double(maskslits,
"ycenter");
7415 cpl_table_fill_invalid_int(slits,
"slit_id", 0);
7416 for (i = 0; i < nmaskslits; i++) {
7417 for (j = 0; j < nslits; j++) {
7418 if (slit_id[j] == 0)
7420 if (mslit_id[i] == slit_id[j]) {
7421 cpl_table_set_int (positions,
"slit_id", k, slit_id[j]);
7423 cpl_table_set_double(positions,
"xtop", k, xtop[j]);
7424 cpl_table_set_double(positions,
"ytop", k, ytop[j]);
7425 cpl_table_set_double(positions,
"xbottom", k, xbottom[j]);
7426 cpl_table_set_double(positions,
"ybottom", k, ybottom[j]);
7427 cpl_table_set_double(positions,
"xcenter", k, xcenter[j]);
7428 cpl_table_set_double(positions,
"ycenter", k, ycenter[j]);
7430 cpl_table_set_double(positions,
"xmtop", k, xmtop[i]);
7431 cpl_table_set_double(positions,
"ymtop", k, ymtop[i]);
7432 cpl_table_set_double(positions,
"xmbottom", k, xmbottom[i]);
7433 cpl_table_set_double(positions,
"ymbottom", k, ymbottom[i]);
7434 cpl_table_set_double(positions,
"xmcenter", k, xmcenter[i]);
7435 cpl_table_set_double(positions,
"ymcenter", k, ymcenter[i]);
7446 cpl_table_erase_column(slits,
"slit_id");
7447 cpl_table_erase_column(slits,
"xpseudo");
7448 cpl_table_erase_column(slits,
"ypseudo");
7449 cpl_table_erase_column(slits,
"xcenter");
7450 cpl_table_erase_column(slits,
"ycenter");
7451 cpl_table_erase_column(maskslits,
"xpseudo");
7452 cpl_table_erase_column(maskslits,
"ypseudo");
7453 cpl_table_erase_column(maskslits,
"xcenter");
7454 cpl_table_erase_column(maskslits,
"ycenter");
7464 ytop = cpl_table_get_data_double(positions,
"ytop");
7465 ybottom = cpl_table_get_data_double(positions,
"ybottom");
7466 xcenter = cpl_table_get_data_double(positions,
"xcenter");
7467 ycenter = cpl_table_get_data_double(positions,
"ycenter");
7468 xmcenter = cpl_table_get_data_double(positions,
"xmcenter");
7469 ymcenter = cpl_table_get_data_double(positions,
"ymcenter");
7471 scales = cpl_vector_new(found_slits - 1);
7472 dscale = cpl_vector_get_data(scales);
7473 angles = cpl_vector_new(found_slits - 1);
7474 dangle = cpl_vector_get_data(angles);
7476 for (i = 1; i < found_slits; i++) {
7477 dist1 = (xcenter[i-1] - xcenter[i]) * (xcenter[i-1] - xcenter[i])
7478 + (ycenter[i-1] - ycenter[i]) * (ycenter[i-1] - ycenter[i]);
7479 dist2 = (xmcenter[i-1] - xmcenter[i]) * (xmcenter[i-1] - xmcenter[i])
7480 + (ymcenter[i-1] - ymcenter[i]) * (ymcenter[i-1] - ymcenter[i]);
7481 dscale[i-1] = sqrt(dist1/dist2);
7482 dangle[i-1] = atan2(ycenter[i-1] - ycenter[i],
7483 xcenter[i-1] - xcenter[i])
7484 - atan2(ymcenter[i-1] - ymcenter[i],
7485 xmcenter[i-1] - xmcenter[i]);
7490 minscale = cpl_vector_get_min(scales);
7491 scale = cpl_vector_get_median_const(scales);
7492 maxscale = cpl_vector_get_max(scales);
7494 minangle = cpl_vector_get_min(angles);
7495 angle = cpl_vector_get_median_const(angles);
7496 maxangle = cpl_vector_get_max(angles);
7498 cpl_msg_info(func,
"Median platescale: %f pixel/mm", scale);
7499 cpl_msg_info(func,
"Minmax platescale: %f, %f pixel/mm",
7500 minscale, maxscale);
7502 cpl_msg_info(func,
"Median rotation: %f degrees", angle);
7503 cpl_msg_info(func,
"Minmax rotation: %f, %f degrees",
7504 minangle, maxangle);
7506 good = cpl_table_get_data_int(positions,
"good");
7508 good[0] = good[found_slits - 1] = 1;
7509 for (i = 1; i < found_slits; i++) {
7510 if (fabs((dscale[i-1] - scale)/scale) < 0.10
7511 && fabs(dangle[i-1] - angle) < 2) {
7517 for (i = 0; i < found_slits; i++) {
7557 cpl_vector_delete(scales);
7558 cpl_vector_delete(angles);
7560 cpl_table_and_selected_int(positions,
"good", CPL_EQUAL_TO, 0);
7561 cpl_table_erase_selected(positions);
7562 cpl_table_erase_column(positions,
"good");
7563 found_slits = cpl_table_get_nrow(positions);
7565 if (found_slits < 4) {
7573 cpl_msg_warning(func,
"Too few safely identified slits: %d out of %d "
7574 "candidates (%d expected). Process will continue "
7575 "using the detected CCD slits positions", found_slits,
7576 nslits, nmaskslits);
7577 cpl_table_delete(positions);
7581 cpl_msg_info(func,
"Safely identified slits: %d out of %d "
7582 "candidates\n(%d expected)", found_slits, nslits,
7593 xpos = cpl_vector_wrap(found_slits,
7594 cpl_table_get_data_double(positions,
"xcenter"));
7595 ypos = cpl_vector_wrap(found_slits,
7596 cpl_table_get_data_double(positions,
"ycenter"));
7597 xmpos = cpl_vector_wrap(found_slits,
7598 cpl_table_get_data_double(positions,
"xmcenter"));
7599 ympos = cpl_vector_wrap(found_slits,
7600 cpl_table_get_data_double(positions,
"ymcenter"));
7601 mpos = cpl_bivector_wrap_vectors(xmpos, ympos);
7603 if (found_slits < 10)
7608 xpoly = cpl_polynomial_fit_2d_create(mpos, xpos, degree, &xmse);
7610 ypoly = cpl_polynomial_fit_2d_create(mpos, ypos, degree, &ymse);
7611 cpl_bivector_unwrap_vectors(mpos);
7612 cpl_vector_unwrap(xpos);
7613 cpl_vector_unwrap(ypos);
7614 cpl_vector_unwrap(xmpos);
7615 cpl_vector_unwrap(ympos);
7616 if (ypoly == NULL) {
7617 if (found_slits == nmaskslits) {
7618 cpl_msg_warning(func,
"Fit failure: the accuracy of the "
7619 "identified slits positions is not improved.");
7630 cpl_msg_info(func,
"Fit failure: not all slits have been "
7631 "identified. Process will continue using "
7632 "the detected CCD slits positions");
7635 cpl_polynomial_delete(xpoly);
7639 cpl_msg_info(func,
"Fit successful: X rms = %.3g, Y rms = %.3g (pixel)",
7640 sqrt(xmse), sqrt(ymse));
7643 write_global_distortion(global, 0, xpoly);
7644 write_global_distortion(global, 7, ypoly);
7652 cpl_table_delete(positions);
7654 positions = cpl_table_duplicate(maskslits);
7655 cpl_table_duplicate_column(positions,
"xmtop", positions,
"xtop");
7656 cpl_table_duplicate_column(positions,
"ymtop", positions,
"ytop");
7657 cpl_table_duplicate_column(positions,
"xmbottom", positions,
"xbottom");
7658 cpl_table_duplicate_column(positions,
"ymbottom", positions,
"ybottom");
7660 point = cpl_vector_new(2);
7661 dpoint = cpl_vector_get_data(point);
7663 for (i = 0; i < nmaskslits; i++) {
7664 dpoint[0] = cpl_table_get_double(positions,
"xmtop", i, NULL);
7665 dpoint[1] = cpl_table_get_double(positions,
"ymtop", i, NULL);
7666 cpl_table_set_double(positions,
"xtop", i,
7667 cpl_polynomial_eval(xpoly, point));
7668 cpl_table_set_double(positions,
"ytop", i,
7669 cpl_polynomial_eval(ypoly, point));
7670 dpoint[0] = cpl_table_get_double(positions,
"xmbottom", i, NULL);
7671 dpoint[1] = cpl_table_get_double(positions,
"ymbottom", i, NULL);
7672 cpl_table_set_double(positions,
"xbottom", i,
7673 cpl_polynomial_eval(xpoly, point));
7674 cpl_table_set_double(positions,
"ybottom", i,
7675 cpl_polynomial_eval(ypoly, point));
7678 cpl_vector_delete(point);
7679 cpl_polynomial_delete(xpoly);
7680 cpl_polynomial_delete(ypoly);
7682 cpl_table_erase_column(positions,
"xmtop");
7683 cpl_table_erase_column(positions,
"ymtop");
7684 cpl_table_erase_column(positions,
"xmbottom");
7685 cpl_table_erase_column(positions,
"ymbottom");
7687 if (nmaskslits > nslits)
7688 cpl_msg_info(func,
"Finally identified slits: %d out of %d expected\n"
7689 "(%d recovered)", nmaskslits, nmaskslits, nmaskslits - nslits);
7690 else if (nmaskslits < nslits)
7691 cpl_msg_info(func,
"Finally identified slits: %d out of %d expected\n"
7692 "(%d rejected)", nmaskslits, nmaskslits, nslits - nmaskslits);
7694 cpl_msg_info(func,
"Finally identified slits: %d out of %d expected",
7695 nmaskslits, nmaskslits);
7743 double blue,
double red,
double dispersion)
7746 const char *func =
"mos_trace_flat";
7748 cpl_image *gradient;
7749 cpl_image *sgradient;
7766 double start_y, prev_y;
7775 int pixel_above, pixel_below;
7777 char trace_id[MAX_COLNAME];
7782 if (flat == NULL || slits == NULL) {
7783 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
7787 if (dispersion <= 0.0) {
7788 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
7792 if (red - blue < dispersion) {
7793 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
7802 nslits = cpl_table_get_nrow(slits);
7803 if (1 != cpl_table_has_column(slits,
"slit_id")) {
7804 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
7805 for (i = 0; i < nslits; i++)
7806 cpl_table_set_int(slits,
"slit_id", i, -(i+1));
7809 slit_id = cpl_table_get_data_int(slits,
"slit_id");
7811 nx = cpl_image_get_size_x(flat);
7812 ny = cpl_image_get_size_y(flat);
7815 gradient = cpl_image_duplicate(flat);
7816 dgradient = cpl_image_get_data_float(gradient);
7818 for (i = 0; i < ny - 1; i++) {
7820 for (j = 0; j < nx; j++) {
7822 dgradient[l] = fabs(dgradient[l] - dgradient[l + nx]);
7827 for (j = 0; j < nx; j++)
7828 dgradient[npix - j] = 0.0;
7830 cpl_image_turn(gradient, -1);
7831 nx = cpl_image_get_size_x(gradient);
7832 ny = cpl_image_get_size_y(gradient);
7833 sgradient = mos_image_vertical_median_filter(gradient,
7834 filtbox, 0, ny, 0, step);
7835 cpl_image_delete(gradient);
7842 dgradient = cpl_image_get_data_float(sgradient);
7844 for (i = 1; i <= ny; i += step) {
7845 row = cpl_vector_new_from_image_row(sgradient, i);
7846 srow = cpl_vector_filter_median_create(row, filtbox);
7847 cpl_vector_subtract(row, srow);
7848 cpl_vector_delete(srow);
7849 g = dgradient + (i-1)*nx;
7850 r = cpl_vector_get_data(row);
7851 for (j = 0; j < nx; j++)
7853 cpl_vector_delete(row);
7863 xtop = cpl_table_get_data_double(slits,
"xtop");
7864 ytop = cpl_table_get_data_double(slits,
"ytop");
7865 xbottom = cpl_table_get_data_double(slits,
"xbottom");
7866 ybottom = cpl_table_get_data_double(slits,
"ybottom");
7874 peaks = cpl_calloc(ny,
sizeof(cpl_vector *));
7876 for (i = 0; i < ny; i += step) {
7877 g = dgradient + i*nx;
7883 cpl_vector_subtract_scalar(peaks[i], 0.5);
7887 cpl_image_delete(sgradient);
7913 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
7914 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
7921 nrows = (ny-1)/step + 1;
7922 traces = cpl_table_new(nrows);
7923 cpl_table_new_column(traces,
"x", CPL_TYPE_DOUBLE);
7924 cpl_table_set_column_unit(traces,
"x",
"pixel");
7925 for (i = 0, j = 0; i < ny; i += step, j++)
7926 cpl_table_set(traces,
"x", j, i);
7928 for (i = 0; i < nslits; i++) {
7949 peak = cpl_vector_get_data(peaks[pos]);
7950 npeaks = cpl_vector_get_size(peaks[pos]);
7952 min = fabs(peak[0] - xtop[i]);
7954 for (j = 1; j < npeaks; j++) {
7955 dist = fabs(peak[j] - xtop[i]);
7966 snprintf(trace_id, MAX_COLNAME,
"t%d", slit_id[i]);
7967 cpl_table_new_column(traces, trace_id, CPL_TYPE_DOUBLE);
7969 if (min > sradius || npeaks == 0) {
7970 cpl_msg_warning(func,
"Cannot find spectrum edge for "
7971 "top (or left) end of slit %d", slit_id[i]);
7983 cpl_table_set(traces, trace_id, pos/step, nx - peak[minpos]);
7984 start_y = peak[minpos];
7992 for (j = pos + step; j < ny; j += step) {
7993 if (j - pos > pixel_above)
7996 peak = cpl_vector_get_data(peaks[j]);
7997 npeaks = cpl_vector_get_size(peaks[j]);
7998 min = fabs(peak[0] - prev_y);
8000 for (k = 1; k < npeaks; k++) {
8001 dist = fabs(peak[k] - prev_y);
8007 if (min < tolerance) {
8008 cpl_table_set(traces, trace_id, j/step,
8010 prev_y = peak[minpos];
8021 for (j = pos - step; j >= 0; j -= step) {
8022 if (pos - j > pixel_below)
8025 peak = cpl_vector_get_data(peaks[j]);
8026 npeaks = cpl_vector_get_size(peaks[j]);
8027 min = fabs(peak[0] - prev_y);
8029 for (k = 1; k < npeaks; k++) {
8030 dist = fabs(peak[k] - prev_y);
8036 if (min < tolerance) {
8037 cpl_table_set(traces, trace_id, j/step,
8039 prev_y = peak[minpos];
8051 peak = cpl_vector_get_data(peaks[pos]);
8052 npeaks = cpl_vector_get_size(peaks[pos]);
8054 min = fabs(peak[0] - xbottom[i]);
8056 for (j = 1; j < npeaks; j++) {
8057 dist = fabs(peak[j] - xbottom[i]);
8068 snprintf(trace_id, MAX_COLNAME,
"b%d", slit_id[i]);
8069 cpl_table_new_column(traces, trace_id, CPL_TYPE_DOUBLE);
8071 if (min > sradius || npeaks == 0) {
8072 cpl_msg_warning(func,
"Cannot find spectrum edge for "
8073 "bottom (or right) end of slit %d", slit_id[i]);
8077 cpl_table_set(traces, trace_id, pos/step, nx - peak[minpos]);
8078 start_y = peak[minpos];
8086 for (j = pos + step; j < ny; j += step) {
8087 if (j - pos > pixel_above)
8090 peak = cpl_vector_get_data(peaks[j]);
8091 npeaks = cpl_vector_get_size(peaks[j]);
8092 min = fabs(peak[0] - prev_y);
8094 for (k = 1; k < npeaks; k++) {
8095 dist = fabs(peak[k] - prev_y);
8101 if (min < tolerance) {
8102 cpl_table_set(traces, trace_id, j/step,
8104 prev_y = peak[minpos];
8115 for (j = pos - step; j >= 0; j -= step) {
8116 if (pos - j > pixel_below)
8119 peak = cpl_vector_get_data(peaks[j]);
8120 npeaks = cpl_vector_get_size(peaks[j]);
8121 min = fabs(peak[0] - prev_y);
8123 for (k = 1; k < npeaks; k++) {
8124 dist = fabs(peak[k] - prev_y);
8130 if (min < tolerance) {
8131 cpl_table_set(traces, trace_id, j/step,
8133 prev_y = peak[minpos];
8141 for (i = 0; i < ny; i += step)
8142 cpl_vector_delete(peaks[i]);
8178 const char *func =
"mos_poly_trace";
8180 cpl_table *polytraces;
8184 cpl_polynomial *polytrace;
8185 char trace_id[MAX_COLNAME];
8186 char trace_res[MAX_COLNAME];
8187 char trace_mod[MAX_COLNAME];
8188 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8199 if (traces == NULL || slits == NULL) {
8200 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8205 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8209 nrows = cpl_table_get_nrow(traces);
8210 xdata = cpl_table_get_data_double(traces,
"x");
8211 nslits = cpl_table_get_nrow(slits);
8212 slit_id = cpl_table_get_data_int(slits,
"slit_id");
8214 polytraces = cpl_table_new(2*nslits);
8215 cpl_table_new_column(polytraces,
"slit_id", CPL_TYPE_INT);
8216 for (i = 0; i <= order; i++)
8217 cpl_table_new_column(polytraces, clab[i], CPL_TYPE_DOUBLE);
8219 for (i = 0; i < nslits; i++) {
8220 for (j = 0; j < 2; j++) {
8223 snprintf(trace_id, MAX_COLNAME,
"b%d", slit_id[i]);
8224 snprintf(trace_res, MAX_COLNAME,
"b%d_res", slit_id[i]);
8225 snprintf(trace_mod, MAX_COLNAME,
"b%d_mod", slit_id[i]);
8228 snprintf(trace_id, MAX_COLNAME,
"t%d", slit_id[i]);
8229 snprintf(trace_res, MAX_COLNAME,
"t%d_res", slit_id[i]);
8230 snprintf(trace_mod, MAX_COLNAME,
"t%d_mod", slit_id[i]);
8233 cpl_table_set_int(polytraces,
"slit_id", 2*i+j, slit_id[i]);
8240 dummy = cpl_table_new(nrows);
8241 cpl_table_duplicate_column(dummy,
"x", traces,
"x");
8242 cpl_table_duplicate_column(dummy, trace_id, traces, trace_id);
8243 npoints = nrows - cpl_table_count_invalid(dummy, trace_id);
8244 if (npoints < 2 * order) {
8245 cpl_table_delete(dummy);
8248 cpl_table_erase_invalid(dummy);
8249 x = cpl_vector_wrap(npoints,
8250 cpl_table_get_data_double(dummy,
"x"));
8251 trace = cpl_vector_wrap(npoints,
8252 cpl_table_get_data_double(dummy, trace_id));
8253 polytrace = cpl_polynomial_fit_1d_create(x, trace, order, NULL);
8254 cpl_vector_unwrap(x);
8255 cpl_vector_unwrap(trace);
8256 cpl_table_delete(dummy);
8265 if (fabs(cpl_polynomial_get_coeff(polytrace, &k)) > 1.E-4) {
8266 cpl_polynomial_delete(polytrace);
8267 cpl_table_new_column(traces, trace_mod, CPL_TYPE_DOUBLE);
8268 cpl_table_duplicate_column(traces, trace_res, traces,
8271 cpl_msg_warning(func,
"Exclude bad curvature solution "
8272 "for bottom (right) edge of slit %d", slit_id[i]);
8274 cpl_msg_warning(func,
"Exclude bad curvature solution "
8275 "for top (left) edge of slit %d", slit_id[i]);
8284 for (k = 0; k <= order; k++)
8285 cpl_table_set_double(polytraces, clab[k], 2*i+j,
8286 cpl_polynomial_get_coeff(polytrace, &k));
8292 cpl_table_new_column(traces, trace_mod, CPL_TYPE_DOUBLE);
8293 cpl_table_set_column_unit(traces, trace_mod,
"pixel");
8295 for (k = 0; k < nrows; k++) {
8296 cpl_table_set_double(traces, trace_mod, k,
8297 cpl_polynomial_eval_1d(polytrace, xdata[k], NULL));
8300 cpl_polynomial_delete(polytrace);
8302 cpl_table_duplicate_column(traces, trace_res, traces, trace_mod);
8303 cpl_table_subtract_columns(traces, trace_res, trace_id);
8304 cpl_table_multiply_scalar(traces, trace_res, -1.0);
8340 const char *func =
"mos_global_trace";
8342 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8355 int order, nrows, nslits;
8359 if (polytraces == NULL) {
8360 cpl_msg_error(func,
"Missing spectral curvature table");
8361 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8364 if (slits == NULL) {
8365 cpl_msg_error(func,
"Missing slits positions table");
8366 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8369 nslits = cpl_table_get_nrow(slits);
8371 table = cpl_table_duplicate(polytraces);
8372 cpl_table_erase_invalid(table);
8374 nrows = cpl_table_get_nrow(table);
8377 cpl_msg_warning(func,
"Too few successful spectral curvature tracings "
8378 "(%d): the determination of a global curvature model "
8380 return CPL_ERROR_NONE;
8383 order = cpl_table_get_ncol(polytraces) - 2;
8385 for (i = 0; i <= order; i++) {
8386 if (!cpl_table_has_column(table, clab[i])) {
8387 cpl_msg_error(func,
"Wrong spectral curvature table");
8388 return cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8397 for (i = 0; i < nslits; i++) {
8398 if (!cpl_table_is_valid(polytraces, clab[0], 2*i)) {
8399 cpl_table_set_double(polytraces, clab[0], 2*i,
8400 cpl_table_get_double(slits,
"ytop", i, NULL));
8402 if (!cpl_table_is_valid(polytraces, clab[0], 2*i+1)) {
8403 cpl_table_set_double(polytraces, clab[0], 2*i+1,
8404 cpl_table_get_double(slits,
"ybottom", i, NULL));
8408 offset = cpl_table_get_data_double(polytraces, clab[0]);
8415 c0 = cpl_vector_wrap(nrows, cpl_table_get_data_double(table, clab[0]));
8417 for (i = 1; i <= order; i++) {
8418 cn = cpl_vector_wrap(nrows, cpl_table_get_data_double(table, clab[i]));
8419 list = cpl_bivector_wrap_vectors(c0, cn);
8420 robustLinearFit(list, &q, &m, &rms);
8424 for (j = 0; j < 2*nslits; j++) {
8426 if (cpl_table_is_valid(polytraces, clab[i], j))
8428 cpl_table_set_double(polytraces, clab[i], j, offset[j]*m + q);
8434 cpl_bivector_unwrap_vectors(list);
8438 cpl_vector_unwrap(cn);
8441 cpl_vector_unwrap(c0);
8442 cpl_table_delete(table);
8444 return CPL_ERROR_NONE;
8519 cpl_table *polytraces,
double reference,
8520 double blue,
double red,
double dispersion,
8521 int flux, cpl_image *calibration)
8523 const char *func =
"mos_spatial_calibration";
8525 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8527 cpl_polynomial *polytop;
8528 cpl_polynomial *polybot;
8530 cpl_image *resampled;
8534 double vtop, vbot, value;
8540 int yint, ysize, yprev;
8546 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
8547 int missing_top, missing_bot;
8553 int create_position = 1;
8556 if (spectra == NULL || slits == NULL || polytraces == NULL) {
8557 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
8561 if (dispersion <= 0.0) {
8562 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8566 if (red - blue < dispersion) {
8567 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
8571 nx = cpl_image_get_size_x(spectra);
8572 ny = cpl_image_get_size_y(spectra);
8573 sdata = cpl_image_get_data(spectra);
8575 data = cpl_image_get_data(calibration);
8577 if (cpl_table_has_column(slits,
"position"))
8578 create_position = 0;
8580 if (create_position) {
8581 cpl_table_new_column(slits,
"position", CPL_TYPE_INT);
8582 cpl_table_new_column(slits,
"length", CPL_TYPE_INT);
8583 cpl_table_set_column_unit(slits,
"position",
"pixel");
8584 cpl_table_set_column_unit(slits,
"length",
"pixel");
8587 length = cpl_table_get_data_int(slits,
"length");
8589 nslits = cpl_table_get_nrow(slits);
8590 slit_id = cpl_table_get_data_int(slits,
"slit_id");
8591 order = cpl_table_get_ncol(polytraces) - 2;
8598 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
8599 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
8601 exslit = cpl_calloc(nslits,
sizeof(cpl_image *));
8603 for (i = 0; i < nslits; i++) {
8605 if (create_position == 0)
8620 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
8622 start_pixel = refpixel - pixel_below;
8623 if (start_pixel < 0)
8626 end_pixel = refpixel + pixel_above;
8636 polytop = cpl_polynomial_new(1);
8637 for (k = 0; k <= order; k++) {
8638 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
8640 cpl_polynomial_delete(polytop);
8644 cpl_polynomial_set_coeff(polytop, &k, coeff);
8648 polybot = cpl_polynomial_new(1);
8649 for (k = 0; k <= order; k++) {
8650 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
8652 cpl_polynomial_delete(polybot);
8656 cpl_polynomial_set_coeff(polybot, &k, coeff);
8659 if (missing_top && missing_bot) {
8660 cpl_msg_warning(func,
"Spatial calibration, slit %d was not "
8661 "traced: no extraction!",
8673 cpl_msg_warning(func,
"Upper edge of slit %d was not traced: "
8674 "the spectral curvature of the lower edge "
8675 "is used instead.", slit_id[i]);
8676 polytop = cpl_polynomial_duplicate(polybot);
8677 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
8678 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
8680 coeff = cpl_polynomial_get_coeff(polybot, &k);
8681 coeff += ytop - ybot;
8682 cpl_polynomial_set_coeff(polytop, &k, coeff);
8686 cpl_msg_warning(func,
"Lower edge of slit %d was not traced: "
8687 "the spectral curvature of the upper edge "
8688 "is used instead.", slit_id[i]);
8689 polybot = cpl_polynomial_duplicate(polytop);
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(polytop, &k);
8694 coeff -= ytop - ybot;
8695 cpl_polynomial_set_coeff(polybot, &k, coeff);
8702 top = cpl_polynomial_eval_1d(polytop, refpixel, NULL);
8703 bot = cpl_polynomial_eval_1d(polybot, refpixel, NULL);
8704 npseudo = ceil(top-bot) + 1;
8707 cpl_polynomial_delete(polytop);
8708 cpl_polynomial_delete(polybot);
8709 cpl_msg_warning(func,
"Slit %d was badly traced: no extraction!",
8714 exslit[i] = cpl_image_new(nx, npseudo+1, CPL_TYPE_FLOAT);
8715 xdata = cpl_image_get_data(exslit[i]);
8721 for (j = start_pixel; j < end_pixel; j++) {
8722 top = cpl_polynomial_eval_1d(polytop, j, NULL);
8723 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
8724 factor = (top-bot)/npseudo;
8725 for (k = 0; k <= npseudo; k++) {
8726 ypos = top - k*factor;
8729 if (yint >= 0 && yint < ny-1) {
8730 vtop = sdata[j + nx*yint];
8731 vbot = sdata[j + nx*(yint+1)];
8737 else if(vtop == FLT_MAX || vbot == FLT_MAX)
8741 value = vtop*(1-yfra) + vbot*yfra;
8745 xdata[j + nx*(npseudo-k)] = value;
8747 data[j + nx*yint] = (top-yint)/factor;
8756 if (yprev - yint > 1) {
8757 data[j + nx*(yint+1)] = (top-yint-1)/factor;
8765 cpl_polynomial_delete(polytop);
8766 cpl_polynomial_delete(polybot);
8774 for (i = 0; i < nslits; i++)
8776 ysize += cpl_image_get_size_y(exslit[i]);
8781 resampled = cpl_image_new(nx, ysize, CPL_TYPE_FLOAT);
8784 for (i = 0; i < nslits; i++) {
8786 yint += cpl_image_get_size_y(exslit[i]);
8787 cpl_image_copy(resampled, exslit[i], 1, ysize - yint);
8788 if (create_position) {
8789 cpl_table_set_int(slits,
"position", i, ysize - yint - 1);
8790 cpl_table_set_int(slits,
"length", i,
8791 cpl_image_get_size_y(exslit[i]));
8793 cpl_image_delete(exslit[i]);
8795 else if (create_position) {
8796 cpl_table_set_int(slits,
"position", i, -1);
8797 cpl_table_set_int(slits,
"length", i, 0);
8938 double dispersion,
float level,
8939 int sradius,
int order,
8940 double reject,
double refwave,
8941 double *wavestart,
double *waveend,
8942 int *nlines,
double *error,
8943 cpl_table *idscoeff,
8944 cpl_image *calibration,
8945 cpl_image *residuals,
8946 cpl_table *restable,
8947 cpl_table *detected_lines)
8950 const char *func =
"mos_wavelength_calibration_final";
8952 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
8955 double tolerance = 20.0;
8958 char name[MAX_COLNAME];
8960 cpl_image *resampled;
8961 cpl_bivector *peaks_ident;
8964 cpl_polynomial *ids;
8965 cpl_polynomial *lin;
8966 cpl_polynomial *fguess;
8969 double max_disp, min_disp;
8971 double firstLambda, lastLambda, lambda;
8972 double wave, pixe, value;
8981 int pixstart, pixend;
8982 int row_top, row_bot;
8987 int nl, nx, ny, pixel;
8988 int countLines, usedLines;
8997 if (dispersion == 0.0) {
8998 cpl_msg_error(func,
"The expected dispersion (A/pixel) must be given");
8999 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9003 if (dispersion < 0.0) {
9004 cpl_msg_error(func,
"The expected dispersion must be positive");
9005 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9009 if (idscoeff == NULL) {
9010 cpl_msg_error(func,
"A preallocated IDS coeff table must be given");
9011 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9015 max_disp = dispersion + dispersion * tolerance / 100;
9016 min_disp = dispersion - dispersion * tolerance / 100;
9019 cpl_msg_error(func,
"The order of the fitting polynomial "
9020 "must be at least 1");
9021 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9025 if (image == NULL || lines == NULL) {
9026 cpl_msg_error(func,
"Both spectral exposure and reference line "
9027 "catalog are required in input");
9028 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9032 nx = cpl_image_get_size_x(image);
9033 ny = cpl_image_get_size_y(image);
9034 sdata = cpl_image_get_data_float(image);
9036 nref = cpl_vector_get_size(lines);
9037 line = cpl_vector_get_data(lines);
9039 if (*wavestart < 1.0 && *waveend < 1.0) {
9040 firstLambda = line[0];
9041 lastLambda = line[nref-1];
9042 extrapolation = (lastLambda - firstLambda) / 10;
9043 firstLambda -= extrapolation;
9044 lastLambda += extrapolation;
9045 *wavestart = firstLambda;
9046 *waveend = lastLambda;
9049 firstLambda = *wavestart;
9050 lastLambda = *waveend;
9053 nl = (lastLambda - firstLambda) / dispersion;
9054 resampled = cpl_image_new(nl, ny, CPL_TYPE_FLOAT);
9055 rdata = cpl_image_get_data_float(resampled);
9061 for (j = 0; j <= order; j++)
9062 cpl_table_new_column(idscoeff, clab[j], CPL_TYPE_DOUBLE);
9065 idata = cpl_image_get_data_float(calibration);
9068 ddata = cpl_image_get_data_float(residuals);
9071 cpl_table_set_size(restable, nref);
9072 cpl_table_new_column(restable,
"wavelength", CPL_TYPE_DOUBLE);
9073 cpl_table_copy_data_double(restable,
"wavelength", line);
9074 for (i = 0; i < ny; i += step) {
9075 snprintf(name, MAX_COLNAME,
"r%d", i);
9076 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
9077 snprintf(name, MAX_COLNAME,
"d%d", i);
9078 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
9079 snprintf(name, MAX_COLNAME,
"p%d", i);
9080 cpl_table_new_column(restable, name, CPL_TYPE_DOUBLE);
9084 if (detected_lines) {
9085 cpl_table_set_size(detected_lines, 0);
9086 cpl_table_new_column(detected_lines,
"slit_id", CPL_TYPE_INT);
9087 cpl_table_new_column(detected_lines,
"xpos_rectified", CPL_TYPE_DOUBLE);
9088 cpl_table_new_column(detected_lines,
"ypos_rectified", CPL_TYPE_DOUBLE);
9089 cpl_table_new_column(detected_lines,
"xpos_rectified_iter", CPL_TYPE_DOUBLE);
9090 cpl_table_new_column(detected_lines,
"ypos_rectified_iter", CPL_TYPE_DOUBLE);
9091 cpl_table_new_column(detected_lines,
"peak_flux", CPL_TYPE_DOUBLE);
9092 cpl_table_new_column(detected_lines,
"wave_ident", CPL_TYPE_DOUBLE);
9093 cpl_table_new_column(detected_lines,
"wave_ident_iter", CPL_TYPE_DOUBLE);
9094 cpl_table_new_column(detected_lines,
"xpos_fit_rect_wavecal", CPL_TYPE_DOUBLE);
9095 cpl_table_new_column(detected_lines,
"res_xpos", CPL_TYPE_DOUBLE);
9096 cpl_table_new_column(detected_lines,
"fit_used", CPL_TYPE_INT);
9104 nslits = cpl_table_get_nrow(slits);
9105 length = cpl_table_get_data_int(slits,
"length");
9108 for (s = 0; s < nslits; s++) {
9111 slit_id = cpl_table_get_int(slits,
"slit_id", s, NULL);
9121 row_bot = cpl_table_get_int(slits,
"position", s, NULL);
9133 coeff = cpl_table_new(row_top - row_bot);
9134 for (j = 0; j <= order; j++)
9135 cpl_table_new_column(coeff, clab[j], CPL_TYPE_DOUBLE);
9143 for (i = row_bot; i < row_top; i++) {
9152 int keep_multiplex = mos_multiplex;
9156 cpl_size newlines = cpl_vector_get_size(peaks);
9157 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
9158 cpl_table_set_size(detected_lines, oldsize + newlines);
9159 for(cpl_size iline = 0; iline < newlines; ++iline)
9161 cpl_table_set_int(detected_lines,
"slit_id",
9162 oldsize + iline, slit_id);
9163 cpl_table_set_double(detected_lines,
"xpos_rectified",
9164 oldsize + iline, cpl_vector_get(peaks, iline) + 1);
9165 cpl_table_set_double(detected_lines,
"ypos_rectified",
9166 oldsize + iline, (
double)i + 1);
9167 cpl_table_set_double(detected_lines,
"peak_flux",
9169 sdata[i*nx+(
int)(cpl_vector_get(peaks, iline)+0.5)]);
9170 cpl_table_set_int(detected_lines,
9172 oldsize + iline, 0);
9176 min_disp, max_disp, 0.05);
9177 mos_multiplex = keep_multiplex;
9179 cpl_bivector * peaks_ident_used_fit;
9180 countLines = cpl_bivector_get_size(peaks_ident);
9181 if (countLines < 4) {
9182 cpl_bivector_delete(peaks_ident);
9183 cpl_vector_delete(peaks);
9195 wavel = cpl_bivector_get_y(peaks_ident);
9196 cpl_vector_subtract_scalar(wavel, refwave);
9198 uorder = countLines / 2 - 1;
9203 2 * (uorder + 1), &usedLines,
9204 &ids_err, &peaks_ident_used_fit);
9207 cpl_bivector_delete(peaks_ident);
9208 cpl_vector_delete(peaks);
9218 for (k = 0; k <= order; k++) {
9220 cpl_table_set_double(coeff, clab[k],
9224 cpl_table_set_double(coeff, clab[k],
9225 i - row_bot, cpl_polynomial_get_coeff(ids, &k));
9231 pixstart = cpl_polynomial_eval_1d(ids,
9232 cpl_bivector_get_y_data(peaks_ident)[0],
9234 pixend = cpl_polynomial_eval_1d(ids,
9235 cpl_bivector_get_y_data(peaks_ident)[countLines-1],
9237 extrapolation = (pixend - pixstart) / 5;
9238 pixstart -= extrapolation;
9239 pixend += extrapolation;
9245 for (j = pixstart; j < pixend; j++) {
9247 firstLambda, lastLambda, refwave, j);
9255 if (residuals || (restable && !(i%step))) {
9256 if (restable && !(i%step)) {
9257 lin = cpl_polynomial_new(1);
9258 for (k = 0; k < 2; k++)
9259 cpl_polynomial_set_coeff(lin, &k,
9260 cpl_polynomial_get_coeff(ids, &k));
9262 for (j = 0; j < countLines; j++) {
9263 pixe = cpl_bivector_get_x_data(peaks_ident)[j];
9264 wave = cpl_bivector_get_y_data(peaks_ident)[j];
9266 - cpl_polynomial_eval_1d(ids, wave, NULL);
9269 (ddata + i*nx)[pixel] = value;
9271 if (restable && !(i%step)) {
9272 for (k = 0; k < nref; k++) {
9273 if (fabs(line[k]-refwave-wave) < 0.1) {
9274 snprintf(name, MAX_COLNAME,
9276 cpl_table_set_double(restable, name,
9279 - cpl_polynomial_eval_1d(lin,
9281 snprintf(name, MAX_COLNAME,
9283 cpl_table_set_double(restable, name,
9285 snprintf(name, MAX_COLNAME,
9287 cpl_table_set_double(restable, name,
9294 if (restable && !(i%step)) {
9295 cpl_polynomial_delete(lin);
9312 cpl_size nidentlines = cpl_bivector_get_size(peaks_ident);
9313 cpl_size ndetectlines = cpl_vector_get_size(peaks);
9314 cpl_size totalsize = cpl_table_get_nrow(detected_lines);
9315 for(cpl_size idline = 0; idline < nidentlines; ++idline)
9317 for(cpl_size detline = 0; detline < ndetectlines; ++detline)
9319 if(cpl_vector_get(peaks, detline) ==
9320 cpl_bivector_get_x_data(peaks_ident)[idline])
9322 cpl_size table_pos = totalsize - ndetectlines + detline;
9323 double wave_ident = cpl_bivector_get_y_data(peaks_ident)[idline] + refwave;
9324 double xpix_fit = cpl_polynomial_eval_1d(ids,
9325 wave_ident - refwave, NULL);
9326 double xpos_det = cpl_table_get_double(detected_lines,
9329 cpl_table_set_double(detected_lines,
9333 cpl_table_set_double(detected_lines,
9334 "xpos_fit_rect_wavecal",
9337 cpl_table_set_double(detected_lines,
9340 xpos_det - xpix_fit - 1);
9341 cpl_table_set_int(detected_lines,
9344 for(cpl_size i_used = 0; i_used < cpl_bivector_get_size(peaks_ident_used_fit); ++i_used)
9346 if(cpl_bivector_get_x_data(peaks_ident)[idline] == cpl_bivector_get_x_data(peaks_ident_used_fit)[i_used])
9347 cpl_table_set_int(detected_lines,
9366 nlines[i] = usedLines;
9368 error[i] = ids_err / sqrt(usedLines/(uorder + 1));
9370 for (k = 0; k <= order; k++) {
9372 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
9375 cpl_table_set_double(idscoeff, clab[k], i,
9376 cpl_polynomial_get_coeff(ids, &k));
9380 cpl_polynomial_delete(ids);
9381 cpl_bivector_delete(peaks_ident);
9383 cpl_vector_delete(peaks);
9394 nfits = row_top - row_bot - cpl_table_count_invalid(coeff, clab[0]);
9399 fguess = cpl_polynomial_new(1);
9409 for (k = 0; k <= order; k++) {
9410 c = cpl_table_get_column_median(coeff, clab[k]);
9411 cpl_polynomial_set_coeff(fguess, &k, c);
9418 for (i = row_bot; i < row_top; i++) {
9419 cpl_bivector * peaks_ident_used_fit;
9426 if (width > sradius) {
9434 for (k = 0; k <= order; k++) {
9435 c = cpl_table_get_double(coeff, clab[k],
9437 cpl_polynomial_set_coeff(fguess, &k, c);
9442 fguess, refwave, uradius);
9444 if (peaks_ident == NULL) {
9449 countLines = cpl_bivector_get_size(peaks_ident);
9451 if (countLines < 4) {
9452 cpl_bivector_delete(peaks_ident);
9460 wavel = cpl_bivector_get_y(peaks_ident);
9461 cpl_vector_subtract_scalar(wavel, refwave);
9463 uorder = countLines / 2 - 1;
9468 2 * (uorder + 1), &usedLines,
9469 &ids_err, &peaks_ident_used_fit);
9473 cpl_bivector_delete(peaks_ident);
9478 nlines[i] = usedLines;
9480 error[i] = ids_err / sqrt(usedLines/(uorder + 1));
9483 pixstart = cpl_polynomial_eval_1d(ids,
9484 cpl_bivector_get_y_data(peaks_ident)[0],
9486 pixend = cpl_polynomial_eval_1d(ids,
9487 cpl_bivector_get_y_data(peaks_ident)[countLines-1],
9489 extrapolation = (pixend - pixstart) / 5;
9490 pixstart -= extrapolation;
9491 pixend += extrapolation;
9497 for (j = pixstart; j < pixend; j++) {
9499 firstLambda, lastLambda, refwave, j);
9507 if (residuals || (restable && !(i%step))) {
9508 if (restable && !(i%step)) {
9509 lin = cpl_polynomial_new(1);
9510 for (k = 0; k < 2; k++)
9511 cpl_polynomial_set_coeff(lin, &k,
9512 cpl_polynomial_get_coeff(ids, &k));
9514 for (j = 0; j < countLines; j++) {
9515 pixe = cpl_bivector_get_x_data(peaks_ident)[j];
9516 wave = cpl_bivector_get_y_data(peaks_ident)[j];
9518 - cpl_polynomial_eval_1d(ids, wave, NULL);
9521 (ddata + i*nx)[pixel] = value;
9523 if (restable && !(i%step)) {
9524 for (k = 0; k < nref; k++) {
9525 if (fabs(line[k]-refwave-wave) < 0.1) {
9526 snprintf(name, MAX_COLNAME,
9528 cpl_table_set_double(restable, name,
9531 - cpl_polynomial_eval_1d(lin,
9533 snprintf(name, MAX_COLNAME,
9535 cpl_table_set_double(restable, name,
9537 snprintf(name, MAX_COLNAME,
9539 cpl_table_set_double(restable, name,
9546 if (restable && !(i%step)) {
9547 cpl_polynomial_delete(lin);
9564 cpl_size oldsize = cpl_table_get_nrow(detected_lines);
9565 cpl_size nidentlines = cpl_bivector_get_size(peaks_ident);
9566 cpl_table_set_size(detected_lines, oldsize + nidentlines);
9567 for(cpl_size idline = 0; idline < nidentlines ; ++idline)
9569 double wave_ident = cpl_bivector_get_y_data(peaks_ident)[idline] + refwave;
9570 double xpix_fit = cpl_polynomial_eval_1d(ids,
9571 wave_ident - refwave, NULL);
9572 cpl_table_set_int(detected_lines,
"slit_id",
9573 oldsize + idline, slit_id);
9574 cpl_table_set_double(detected_lines,
"xpos_rectified_iter",
9575 oldsize + idline, cpl_bivector_get_x_data(peaks_ident)[idline] + 1);
9576 cpl_table_set_double(detected_lines,
"ypos_rectified_iter",
9577 oldsize + idline, (
double)i + 1);
9578 cpl_table_set_double(detected_lines,
"peak_flux",
9580 sdata[i*nx+(
int)(cpl_bivector_get_x_data(peaks_ident)[idline]+0.5)]);
9581 cpl_table_set_double(detected_lines,
"wave_ident_iter",
9582 oldsize + idline, wave_ident);
9583 cpl_table_set_double(detected_lines,
"xpos_fit_rect_wavecal",
9584 oldsize + idline, xpix_fit + 1);
9585 cpl_table_set_int(detected_lines,
9587 oldsize + idline, 0);
9588 for(cpl_size i_used = 0; i_used < cpl_bivector_get_size(peaks_ident_used_fit); ++i_used)
9590 if(cpl_bivector_get_x_data(peaks_ident)[idline] == cpl_bivector_get_x_data(peaks_ident_used_fit)[i_used])
9591 cpl_table_set_int(detected_lines,
9593 oldsize + idline, 1);
9599 for (k = 0; k <= order; k++) {
9601 cpl_table_set_double(idscoeff, clab[k], i, 0.0);
9604 cpl_table_set_double(idscoeff, clab[k], i,
9605 cpl_polynomial_get_coeff(ids, &k));
9609 cpl_bivector_delete(peaks_ident);
9610 cpl_polynomial_delete(ids);
9614 cpl_polynomial_delete(fguess);
9617 cpl_table_delete(coeff);
9633 for (i = 0; i < ny; i++) {
9636 ids = cpl_polynomial_new(1);
9637 for (k = 0; k <= order; k++) {
9638 c = cpl_table_get_double(idscoeff, clab[k], i, &null);
9640 cpl_polynomial_delete(ids);
9644 cpl_polynomial_set_coeff(ids, &k, c);
9649 pixstart = cpl_polynomial_eval_1d(ids, firstLambda - refwave, NULL);
9650 pixend = cpl_polynomial_eval_1d(ids, lastLambda - refwave, NULL);
9660 for (j = 0; j < nl; j++) {
9661 lambda = firstLambda + j * dispersion;
9662 fpixel = cpl_polynomial_eval_1d(ids, lambda - refwave, NULL);
9664 if (pixel >= 0 && pixel < nx-1) {
9665 v1 = (sdata + i*nx)[pixel];
9666 v2 = (sdata + i*nx)[pixel+1];
9667 vi = v1 + (v2-v1)*(fpixel-pixel);
9668 (rdata + i*nl)[j] = vi;
9672 cpl_polynomial_delete(ids);
9706 double firstLambda,
double lastLambda,
9707 double dispersion, cpl_table *idscoeff,
9711 const char *func =
"mos_wavelength_calibration";
9713 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
9716 cpl_image *resampled;
9717 cpl_polynomial *ids;
9718 double pixel_per_lambda;
9723 float v0, v1, v2, v3, vi;
9726 int pixstart, pixend;
9727 int nl, nx, ny, pixel;
9734 if (dispersion <= 0.0) {
9735 cpl_msg_error(func,
"The resampling step must be positive");
9736 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9740 if (lastLambda - firstLambda < dispersion) {
9741 cpl_msg_error(func,
"Invalid spectral range: %.2f to %.2f",
9742 firstLambda, lastLambda);
9743 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
9747 if (idscoeff == NULL) {
9748 cpl_msg_error(func,
"An IDS coeff table must be given");
9749 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9753 if (image == NULL) {
9754 cpl_msg_error(func,
"A scientific spectral image must be given");
9755 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
9759 nx = cpl_image_get_size_x(image);
9760 ny = cpl_image_get_size_y(image);
9761 sdata = cpl_image_get_data_float(image);
9763 nl = (lastLambda - firstLambda) / dispersion;
9764 resampled = cpl_image_new(nl, ny, CPL_TYPE_FLOAT);
9765 rdata = cpl_image_get_data_float(resampled);
9768 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
9772 for (i = 0; i < ny; i++) {
9775 ids = cpl_polynomial_new(1);
9776 for (k = 0; k <= order; k++) {
9777 c = cpl_table_get_double(idscoeff, clab[k], i, &null);
9779 cpl_polynomial_delete(ids);
9783 cpl_polynomial_set_coeff(ids, &k, c);
9788 pixstart = cpl_polynomial_eval_1d(ids, firstLambda - refwave, NULL);
9789 pixend = cpl_polynomial_eval_1d(ids, lastLambda - refwave, NULL);
9799 for (j = 0; j < nl; j++) {
9800 lambda = firstLambda + j * dispersion;
9801 fpixel = cpl_polynomial_eval_1d(ids, lambda - refwave,
9823 if(pixel_per_lambda <= 0)
9825 else if (fpixel < 0)
9827 else if (pixel >= 1 && pixel < nx-2) {
9828 v0 = (sdata + i*nx)[pixel-1];
9829 v1 = (sdata + i*nx)[pixel];
9830 v2 = (sdata + i*nx)[pixel+1];
9831 v3 = (sdata + i*nx)[pixel+2];
9832 vi = (fpixel-pixel)*(fpixel-pixel)*(v3 - v2 - v1 + v0)
9833 + (fpixel-pixel)*(3*v2 - v3 - v1 - v0)
9853 vi *= dispersion * pixel_per_lambda;
9855 else if (pixel >= 0 && pixel < nx-1) {
9856 v1 = (sdata + i*nx)[pixel];
9857 v2 = (sdata + i*nx)[pixel+1];
9858 vi = v1 + (v2-v1)*(fpixel-pixel);
9860 vi *= dispersion * pixel_per_lambda;
9864 (rdata + i*nl)[j] = vi;
9877 double spos = fpixel - dispersion * pixel_per_lambda / 2;
9878 double epos = fpixel + dispersion * pixel_per_lambda / 2;
9885 int epix = epos + 1;
9894 for (k = spix; k < epix; k++) {
9895 if (pixel >= 0 && pixel < nx) {
9896 vi += (sdata + i*nx)[k];
9907 vi *= dispersion * pixel_per_lambda / (epix - spix);
9915 vi *= dispersion * pixel_per_lambda;
9917 (rdata + i*nl)[j] = vi;
9921 cpl_polynomial_delete(ids);
9995 double refwave,
double firstLambda,
9996 double lastLambda, cpl_table *idscoeff,
9997 cpl_vector *skylines,
int highres,
int order,
9998 cpl_image *calibration,
int sradius)
10000 const char *func =
"mos_wavelength_align";
10002 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
10006 double expPos, offset;
10008 double lambda1, lambda2;
10014 int startPos, endPos;
10015 int window = 2*sradius + 1;
10021 int xlow, ylow, xhig, yhig;
10022 int idsorder, uorder;
10031 char offname[MAX_COLNAME];
10032 char name[MAX_COLNAME];
10034 cpl_polynomial *ids;
10035 cpl_polynomial *polycorr;
10038 cpl_table *offsets;
10044 if (idscoeff == NULL) {
10045 cpl_msg_error(func,
"An IDS coeff table must be given");
10046 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10050 if (image == NULL) {
10051 cpl_msg_error(func,
"A scientific spectral image must be given");
10052 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10056 if (slits == NULL) {
10057 cpl_msg_error(func,
"A slit position table must be given");
10058 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10063 line = cpl_vector_get_data(skylines);
10064 nlines = cpl_vector_get_size(skylines);
10067 cpl_msg_warning(func,
"A catalog of sky lines wavelengths was not "
10068 "given: using internal list of reference sky lines");
10070 line = default_lines_hi;
10071 nlines =
sizeof(default_lines_hi) /
sizeof(
double);
10074 line = default_lines_lo;
10075 nlines =
sizeof(default_lines_lo) /
sizeof(
double);
10080 cdata = cpl_image_get_data(calibration);
10082 nx = cpl_image_get_size_x(image);
10083 ny = cpl_image_get_size_y(image);
10085 nslits = cpl_table_get_nrow(slits);
10086 slit_id = cpl_table_get_data_int(slits,
"slit_id");
10087 position = cpl_table_get_data_int(slits,
"position");
10088 length = cpl_table_get_data_int(slits,
"length");
10096 for (i = 0; i < nlines; i++)
10097 if (line[i] > firstLambda && line[i] < lastLambda)
10100 offsets = cpl_table_new(nrows);
10101 cpl_table_new_column(offsets,
"wave", CPL_TYPE_DOUBLE);
10102 cpl_table_set_column_unit(offsets,
"wave",
"Angstrom");
10105 for (i = 0; i < nlines; i++) {
10106 if (line[i] > firstLambda && line[i] < lastLambda) {
10107 cpl_table_set_double(offsets,
"wave", nrows, line[i]);
10116 line = cpl_table_get_data_double(offsets,
"wave");
10120 while (idsorder < 6 && cpl_table_has_column(idscoeff, clab[idsorder]))
10126 for (i = 0; i < nslits; i++) {
10128 if (length[i] == 0)
10131 snprintf(offname, MAX_COLNAME,
"offset%d", slit_id[i]);
10132 cpl_table_new_column(offsets, offname, CPL_TYPE_DOUBLE);
10144 ylow = position[i] + 1;
10145 yhig = ylow + length[i] - 1;
10147 exslit = cpl_image_extract(image, xlow, ylow, xhig, yhig);
10148 sky = cpl_image_collapse_median_create(exslit, 0, 0, 1);
10149 sdata = cpl_image_get_data(sky);
10151 cpl_image_delete(exslit);
10166 dummy = cpl_table_new(yhig - ylow);
10167 for (j = 0; j < nlines; j++) {
10168 snprintf(name, MAX_COLNAME,
"%"CPL_SIZE_FORMAT, j);
10169 cpl_table_new_column(dummy, name, CPL_TYPE_DOUBLE);
10172 for (j = ylow; j < yhig; j++) {
10179 ids = cpl_polynomial_new(1);
10180 for (k = 0; k <= idsorder; k++) {
10181 c = cpl_table_get_double(idscoeff, clab[k], j, &null);
10183 cpl_polynomial_delete(ids);
10187 cpl_polynomial_set_coeff(ids, &k, c);
10192 for (k = 0; k < nlines; k++) {
10193 expPos = cpl_polynomial_eval_1d(ids, line[k] - refwave, NULL);
10194 startPos = expPos - sradius;
10195 endPos = startPos + window;
10196 if (startPos < 0 || endPos >= nx)
10199 if (0 == peakPosition(sdata + startPos, window, &pos, 1)) {
10201 offset = pos - expPos;
10202 snprintf(name, MAX_COLNAME,
"%"CPL_SIZE_FORMAT, k);
10203 cpl_table_set_double(dummy, name, j - ylow, offset);
10207 cpl_polynomial_delete(ids);
10210 cpl_image_delete(sky);
10212 for (j = 0; j < nlines; j++) {
10213 snprintf(name, MAX_COLNAME,
"%"CPL_SIZE_FORMAT, j);
10214 if (cpl_table_has_valid(dummy, name)) {
10215 offset = cpl_table_get_column_median(dummy, name);
10216 cpl_table_set_double(offsets, offname, j, offset);
10220 cpl_table_delete(dummy);
10231 for (i = 0; i < nslits; i++) {
10233 if (length[i] == 0)
10236 snprintf(offname, MAX_COLNAME,
"offset%d", slit_id[i]);
10243 dummy = cpl_table_new(nlines);
10244 cpl_table_duplicate_column(dummy,
"wave", offsets,
"wave");
10245 cpl_table_duplicate_column(dummy,
"offset", offsets, offname);
10247 npoints = nlines - cpl_table_count_invalid(dummy,
"offset");
10248 if (npoints == 0) {
10249 cpl_msg_warning(func,
"No sky lines alignment was possible "
10250 "for slit ID=%d: no sky line found", slit_id[i]);
10251 cpl_table_delete(dummy);
10256 if (npoints <= uorder) {
10257 uorder = npoints - 1;
10259 cpl_msg_warning(func,
"Just %d sky lines detected for slit "
10260 "ID=%d, while a polynomial order %d was "
10261 "requested. Using polynomial order %d for "
10262 "this slit!", npoints, slit_id[i], order,
10266 cpl_msg_warning(func,
"Just %d sky lines detected for slit "
10267 "ID=%d, while a polynomial order %d was "
10268 "requested. Computing a median offset for "
10269 "this slit!", npoints, slit_id[i], order);
10273 cpl_table_erase_invalid(dummy);
10281 wave = cpl_vector_wrap(npoints,
10282 cpl_table_get_data_double(dummy,
"wave"));
10283 offs = cpl_vector_wrap(npoints,
10284 cpl_table_get_data_double(dummy,
"offset"));
10290 cpl_vector_subtract_scalar(wave, refwave);
10292 polycorr = cpl_polynomial_fit_1d_create(wave, offs, uorder, &rms);
10294 rms = sqrt(rms * (uorder + 1) / npoints);
10296 cpl_vector_unwrap(wave);
10297 cpl_vector_unwrap(offs);
10298 cpl_table_delete(dummy);
10305 ylow = position[i];
10306 yhig = ylow + length[i];
10308 for (j = 0; j <= uorder; j++) {
10309 data = cpl_table_get_data_double(idscoeff, clab[j]);
10310 c = cpl_polynomial_get_coeff(polycorr, &j);
10311 for (k = ylow; k < yhig; k++)
10315 data = cpl_table_get_data_double(idscoeff,
"error");
10316 for (k = ylow; k < yhig; k++)
10317 data[k] = sqrt(data[k]*data[k] + rms*rms);
10319 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10320 for (k = ylow; k < yhig; k++)
10321 idata[k] = npoints;
10329 for (j = ylow; j < yhig; j++) {
10330 for (k = 1; k < nx; k++) {
10331 lambda1 = cdata[k - 1 + j*nx];
10332 lambda2 = cdata[k + j*nx];
10333 if (lambda1 < 1.0 || lambda2 < 1.0)
10335 offset = cpl_polynomial_eval_1d(polycorr,
10336 lambda1-refwave, NULL);
10337 cdata[k - 1 + j*nx] -= offset * (lambda2-lambda1);
10342 cpl_polynomial_delete(polycorr);
10344 else if (uorder == 1) {
10351 cpl_bivector *list;
10354 wave = cpl_vector_wrap(npoints,
10355 cpl_table_get_data_double(dummy,
"wave"));
10356 offs = cpl_vector_wrap(npoints,
10357 cpl_table_get_data_double(dummy,
"offset"));
10359 list = cpl_bivector_wrap_vectors(wave, offs);
10365 cpl_vector_subtract_scalar(wave, refwave);
10367 robustLinearFit(list, &q, &m, &rms);
10369 rms = sqrt(rms * (uorder + 1) / npoints);
10371 cpl_bivector_unwrap_vectors(list);
10372 cpl_vector_unwrap(wave);
10373 cpl_vector_unwrap(offs);
10374 cpl_table_delete(dummy);
10381 ylow = position[i];
10382 yhig = ylow + length[i];
10384 for (j = 0; j <= uorder; j++) {
10385 data = cpl_table_get_data_double(idscoeff, clab[j]);
10390 for (k = ylow; k < yhig; k++)
10394 data = cpl_table_get_data_double(idscoeff,
"error");
10395 for (k = ylow; k < yhig; k++)
10396 data[k] = sqrt(data[k]*data[k] + rms*rms);
10398 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10399 for (k = ylow; k < yhig; k++)
10400 idata[k] = npoints;
10408 for (j = ylow; j < yhig; j++) {
10409 for (k = 1; k < nx; k++) {
10410 lambda1 = cdata[k - 1 + j*nx];
10411 lambda2 = cdata[k + j*nx];
10412 if (lambda1 < 1.0 || lambda2 < 1.0)
10414 offset = q + m*(lambda1-refwave);
10415 cdata[k - 1 + j*nx] -= offset * (lambda2-lambda1);
10426 offs = cpl_vector_wrap(npoints,
10427 cpl_table_get_data_double(dummy,
"offset"));
10429 offset = cpl_vector_get_median_const(offs);
10432 rms = cpl_table_get_column_stdev(dummy,
"offset");
10436 rms /= sqrt(npoints);
10438 cpl_vector_unwrap(offs);
10439 cpl_table_delete(dummy);
10446 ylow = position[i];
10447 yhig = ylow + length[i];
10449 data = cpl_table_get_data_double(idscoeff, clab[0]);
10450 for (k = ylow; k < yhig; k++)
10453 data = cpl_table_get_data_double(idscoeff,
"error");
10454 for (k = ylow; k < yhig; k++)
10455 data[k] = sqrt(data[k]*data[k] + rms*rms);
10457 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10458 for (k = ylow; k < yhig; k++)
10459 idata[k] = npoints;
10468 for (j = ylow; j < yhig; j++) {
10469 for (k = 1; k < nx; k++) {
10470 lambda1 = cdata[k - 1 + j*nx];
10471 lambda2 = cdata[k + j*nx];
10472 if (lambda1 < 1.0 || lambda2 < 1.0)
10474 cdata[k - 1 + j*nx] -= offset * (lambda2-lambda1);
10548 double firstLambda,
double lastLambda,
10549 cpl_table *idscoeff, cpl_vector *skylines,
10550 int highres,
int order,
10551 cpl_image *calibration,
int sradius)
10553 const char *func =
"mos_wavelength_align_lss";
10555 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
10561 double expPos, offset;
10563 double lambda1, lambda2;
10569 int startPos, endPos;
10570 int window = 2*sradius + 1;
10575 int idsorder, uorder;
10580 char name[MAX_COLNAME];
10581 char fname[MAX_COLNAME];
10583 cpl_polynomial *ids;
10584 cpl_polynomial *polycorr;
10585 cpl_table *offsets;
10586 cpl_table *fittable;
10593 if (idscoeff == NULL) {
10594 cpl_msg_error(func,
"An IDS coeff table must be given");
10595 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10599 if (image == NULL) {
10600 cpl_msg_error(func,
"A scientific spectral image must be given");
10601 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
10606 line = cpl_vector_get_data(skylines);
10607 nlines = cpl_vector_get_size(skylines);
10610 cpl_msg_warning(func,
"A catalog of sky lines wavelengths was not "
10611 "given: using internal list of reference sky lines");
10613 line = default_lines_hi;
10614 nlines =
sizeof(default_lines_hi) /
sizeof(
double);
10617 line = default_lines_lo;
10618 nlines =
sizeof(default_lines_lo) /
sizeof(
double);
10623 cdata = cpl_image_get_data(calibration);
10625 nx = cpl_image_get_size_x(image);
10626 ny = cpl_image_get_size_y(image);
10628 sdata = cpl_image_get_data(image);
10630 if (ny != cpl_table_get_nrow(idscoeff)) {
10631 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
10645 for (i = 0; i < nlines; i++)
10646 if (line[i] > firstLambda && line[i] < lastLambda)
10649 offsets = cpl_table_new(nrows);
10650 cpl_table_new_column(offsets,
"wave", CPL_TYPE_DOUBLE);
10651 cpl_table_set_column_unit(offsets,
"wave",
"Angstrom");
10654 for (i = 0; i < nlines; i++) {
10655 if (line[i] > firstLambda && line[i] < lastLambda) {
10656 cpl_table_set_double(offsets,
"wave", nrows, line[i]);
10665 line = cpl_table_get_data_double(offsets,
"wave");
10669 while (idsorder < 6 && cpl_table_has_column(idscoeff, clab[idsorder]))
10679 dummy = cpl_table_new(ny);
10680 for (j = 0; j < nlines; j++) {
10681 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[j]);
10682 snprintf(fname, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10683 cpl_table_new_column(dummy, name, CPL_TYPE_DOUBLE);
10684 cpl_table_new_column(dummy, fname, CPL_TYPE_DOUBLE);
10687 for (j = 0; j < ny; j++, sdata += nx) {
10694 ids = cpl_polynomial_new(1);
10695 for (k = 0; k <= idsorder; k++) {
10696 c = cpl_table_get_double(idscoeff, clab[k], j, &missing);
10698 cpl_polynomial_delete(ids);
10701 cpl_polynomial_set_coeff(ids, &k, c);
10706 for (k = 0; k < nlines; k++) {
10707 expPos = cpl_polynomial_eval_1d(ids, line[k] - refwave, NULL);
10708 startPos = expPos - sradius;
10709 endPos = startPos + window;
10710 if (startPos < 0 || endPos >= nx)
10713 if (0 == peakPosition(sdata + startPos, window, &pos, 1)) {
10715 offset = pos - expPos;
10716 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[k]);
10717 cpl_table_set_double(dummy, name, j, offset);
10721 cpl_polynomial_delete(ids);
10730 for (j = 0; j < nlines; j++) {
10731 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[j]);
10732 snprintf(fname, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10733 if (cpl_table_has_valid(dummy, name)) {
10741 cpl_bivector *list;
10743 fittable = cpl_table_new(ny);
10744 cpl_table_new_column(fittable,
"row", CPL_TYPE_DOUBLE);
10745 cpl_table_set_column_unit(fittable,
"row",
"pixel");
10746 for (k = 0; k < ny; k++)
10747 cpl_table_set_double(fittable,
"row", k, k);
10748 cpl_table_duplicate_column(fittable,
"offset", dummy, name);
10749 npoints = ny - cpl_table_count_invalid(fittable,
"offset");
10750 cpl_table_erase_invalid(fittable);
10751 row = cpl_vector_wrap(npoints,
10752 cpl_table_get_data_double(fittable,
"row"));
10753 offs = cpl_vector_wrap(npoints,
10754 cpl_table_get_data_double(fittable,
"offset"));
10755 list = cpl_bivector_wrap_vectors(row, offs);
10756 robustLinearFit(list, &q, &m, &rms);
10757 cpl_bivector_unwrap_vectors(list);
10758 cpl_vector_unwrap(row);
10759 cpl_vector_unwrap(offs);
10760 cpl_table_delete(fittable);
10761 for (k = 0; k < ny; k++)
10762 cpl_table_set_double(dummy, fname, k, q + m*k);
10775 for (i = 0; i < ny; i++) {
10777 if (!cpl_table_is_valid(idscoeff, clab[0], i))
10781 for (j = 0; j < nlines; j++) {
10782 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10783 if (cpl_table_is_valid(dummy, name, i))
10791 if (npoints <= uorder)
10792 uorder = npoints - 1;
10800 wave = cpl_vector_new(npoints);
10801 wdata = cpl_vector_get_data(wave);
10802 offs = cpl_vector_new(npoints);
10803 odata = cpl_vector_get_data(offs);
10806 for (j = 0; j < nlines; j++) {
10807 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10808 if (cpl_table_is_valid(dummy, name, i)) {
10809 wdata[npoints] = line[j] - refwave;
10810 odata[npoints] = cpl_table_get_double(dummy, name, i, NULL);
10815 polycorr = cpl_polynomial_fit_1d_create(wave, offs, uorder, &rms);
10817 rms = sqrt(rms * (uorder + 1) / npoints);
10819 cpl_vector_delete(wave);
10820 cpl_vector_delete(offs);
10827 for (j = 0; j <= uorder; j++) {
10828 data = cpl_table_get_data_double(idscoeff, clab[j]);
10829 c = cpl_polynomial_get_coeff(polycorr, &j);
10833 data = cpl_table_get_data_double(idscoeff,
"error");
10834 data[i] = sqrt(data[i]*data[i] + rms*rms);
10836 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10837 idata[i] = npoints;
10845 for (k = 1; k < nx; k++) {
10846 lambda1 = cdata[k - 1 + i*nx];
10847 lambda2 = cdata[k + i*nx];
10848 if (lambda1 < 1.0 || lambda2 < 1.0)
10850 offset = cpl_polynomial_eval_1d(polycorr,
10851 lambda1-refwave, NULL);
10852 cdata[k - 1 + i*nx] -= offset * (lambda2-lambda1);
10856 cpl_polynomial_delete(polycorr);
10859 else if (uorder == 1) {
10865 cpl_bivector *list;
10868 wave = cpl_vector_new(npoints);
10869 wdata = cpl_vector_get_data(wave);
10870 offs = cpl_vector_new(npoints);
10871 odata = cpl_vector_get_data(offs);
10874 for (j = 0; j < nlines; j++) {
10875 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10876 if (cpl_table_is_valid(dummy, name, i)) {
10877 wdata[npoints] = line[j] - refwave;
10878 odata[npoints] = cpl_table_get_double(dummy, name, i, NULL);
10883 list = cpl_bivector_wrap_vectors(wave, offs);
10884 robustLinearFit(list, &q, &m, &rms);
10886 rms = sqrt(rms * (uorder + 1) / npoints);
10888 cpl_bivector_unwrap_vectors(list);
10889 cpl_vector_delete(wave);
10890 cpl_vector_delete(offs);
10897 for (j = 0; j <= uorder; j++) {
10898 data = cpl_table_get_data_double(idscoeff, clab[j]);
10906 data = cpl_table_get_data_double(idscoeff,
"error");
10907 data[i] = sqrt(data[i]*data[i] + rms*rms);
10909 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10910 idata[i] = npoints;
10918 for (k = 1; k < nx; k++) {
10919 lambda1 = cdata[k - 1 + i*nx];
10920 lambda2 = cdata[k + i*nx];
10921 if (lambda1 < 1.0 || lambda2 < 1.0)
10923 offset = q + m*(lambda1-refwave);
10924 cdata[k - 1 + i*nx] -= offset * (lambda2-lambda1);
10934 offs = cpl_vector_new(npoints);
10935 odata = cpl_vector_get_data(offs);
10938 for (j = 0; j < nlines; j++) {
10939 snprintf(name, MAX_COLNAME,
"fit_%d", (
int)line[j]);
10940 if (cpl_table_is_valid(dummy, name, i)) {
10941 odata[npoints] = cpl_table_get_double(dummy, name, i, NULL);
10946 offset = cpl_vector_get_median_const(offs);
10949 rms = cpl_vector_get_stdev(offs);
10951 else if (npoints == 1) {
10952 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[0]);
10953 if (cpl_table_has_valid(dummy, name)) {
10954 rms = cpl_table_get_column_stdev(dummy, name);
10955 rms /= sqrt(ny - cpl_table_count_invalid(dummy, name));
10965 rms /= sqrt(npoints);
10967 cpl_vector_delete(offs);
10974 data = cpl_table_get_data_double(idscoeff, clab[0]);
10977 data = cpl_table_get_data_double(idscoeff,
"error");
10978 data[i] = sqrt(data[i]*data[i] + rms*rms);
10980 idata = cpl_table_get_data_int(idscoeff,
"nlines");
10981 idata[i] = npoints;
10990 for (k = 1; k < nx; k++) {
10991 lambda1 = cdata[k - 1 + i*nx];
10992 lambda2 = cdata[k + i*nx];
10993 if (lambda1 < 1.0 || lambda2 < 1.0)
10995 cdata[k - 1 + i*nx] -= offset * (lambda2-lambda1);
11002 for (j = 0; j < nlines; j++) {
11003 snprintf(name, MAX_COLNAME,
"off_%d", (
int)line[j]);
11004 if (cpl_table_has_valid(dummy, name)) {
11006 offset = cpl_table_get_column_median(dummy, name);
11007 cpl_msg_info(func,
"Median offset for %.3f: %.3f pixel",
11012 "Median offset for %.2f: not available", line[j]);
11016 cpl_table_delete(offsets);
11019 cpl_table_delete(dummy);
11056 double wavestart,
double dispersion,
int radius,
11060 const char *func =
"mos_distortions_rms";
11065 int cpix, npix, nzero;
11068 int npeaks, allPeaks;
11071 float peak, expectPeak, offset;
11075 double rms, oneRms;
11081 xlen = cpl_image_get_size_x(rectified);
11082 ylen = cpl_image_get_size_y(rectified);
11083 sdata = cpl_image_get_data(rectified);
11086 wdata = cpl_vector_get_data(lines);
11087 numLines = cpl_vector_get_size(lines);
11090 cpl_msg_warning(func,
"A catalog of sky lines wavelengths was not "
11091 "given: using internal list of reference sky lines");
11093 wdata = default_lines_hi;
11094 numLines =
sizeof(default_lines_hi) /
sizeof(
double);
11097 wdata = default_lines_lo;
11098 numLines =
sizeof(default_lines_lo) /
sizeof(
double);
11102 npix = 2 * radius + 1;
11103 profile = cpl_calloc(npix,
sizeof(
float));
11108 for (i = 0; i < numLines; i++) {
11115 expectPeak = (lambda - wavestart) / dispersion;
11116 cpix = floor(expectPeak + 0.5);
11122 sp = cpix - radius;
11123 ep = cpix + radius;
11125 if (sp < 0 || ep > xlen)
11132 for (j = 0; j < ylen; j++) {
11134 for (k = 0; k < npix; k++) {
11135 profile[k] = sdata[sp + k + j * xlen];
11136 if (fabs(profile[k]) < 0.0001)
11142 if (peakPosition(profile, npix, &peak, 1) == 0) {
11143 offset = (sp + peak) - expectPeak;
11145 rms += fabs(offset);
11146 oneRms += fabs(offset);
11153 cpl_msg_info(func,
"RMS for %.2f: %.3f pixel (%d points)",
11154 lambda, oneRms / npeaks * 1.25, npeaks);
11156 cpl_msg_info(func,
"RMS for %.2f: line not available", lambda);
11193 double blue,
double red,
double dispersion,
int trend)
11195 const char *func =
"mos_map_pixel";
11197 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11200 cpl_polynomial *ids;
11212 if (idscoeff == NULL) {
11213 cpl_msg_error(func,
"An IDS coeff table must be given");
11214 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11218 xsize = (red - blue) / dispersion;
11219 ysize = cpl_table_get_nrow(idscoeff);
11220 map = cpl_image_new(xsize, ysize, CPL_TYPE_FLOAT);
11221 mdata = cpl_image_get_data(map);
11224 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
11228 for (i = 0; i < ysize; i++, mdata += xsize) {
11231 ids = cpl_polynomial_new(1);
11232 for (k = trend; k <= order; k++) {
11233 c = cpl_table_get_double(idscoeff, clab[k], i, &missing);
11235 cpl_polynomial_delete(ids);
11238 cpl_polynomial_set_coeff(ids, &k, c);
11243 for (j = 0; j < xsize; j++) {
11244 lambda = blue + j*dispersion;
11245 mdata[j] = cpl_polynomial_eval_1d(ids, lambda-reference, NULL);
11248 cpl_polynomial_delete(ids);
11278 double blue,
double red)
11280 const char *func =
"mos_map_idscoeff";
11282 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11285 cpl_polynomial *ids;
11297 if (idscoeff == NULL) {
11298 cpl_msg_error(func,
"An IDS coeff table must be given");
11299 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11304 cpl_msg_error(func,
"Invalid image size");
11305 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11309 if (xsize < 20 || xsize > 5000) {
11310 cpl_msg_warning(func,
"Do you really have a detector %d pixels long?",
11314 ysize = cpl_table_get_nrow(idscoeff);
11315 map = cpl_image_new(xsize, ysize, CPL_TYPE_FLOAT);
11316 mdata = cpl_image_get_data(map);
11319 while (order < 6 && cpl_table_has_column(idscoeff, clab[order]))
11323 for (i = 0; i < ysize; i++, mdata += xsize) {
11326 ids = cpl_polynomial_new(1);
11327 for (k = 0; k <= order; k++) {
11328 c = cpl_table_get_double(idscoeff, clab[k], i, &missing);
11330 cpl_polynomial_delete(ids);
11333 cpl_polynomial_set_coeff(ids, &k, c);
11338 for (j = 0; j < xsize; j++) {
11341 if (lambda >= blue && lambda <= red) {
11346 cpl_polynomial_delete(ids);
11389 cpl_table *slits, cpl_table *polytraces,
11390 double reference,
double blue,
double red,
11393 const char *func =
"mos_map_wavelengths";
11395 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11397 cpl_polynomial *polytop;
11398 cpl_polynomial *polybot;
11399 cpl_image *remapped;
11404 double vtop, vbot, value;
11411 int yint, ysize, yprev;
11418 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
11419 int missing_top, missing_bot;
11426 if (spatial == NULL || calibration == NULL ||
11427 slits == NULL || polytraces == NULL) {
11428 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11432 if (dispersion <= 0.0) {
11433 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11437 if (red - blue < dispersion) {
11438 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11442 nx = cpl_image_get_size_x(spatial);
11443 ny = cpl_image_get_size_y(spatial);
11444 ysize = cpl_image_get_size_y(calibration);
11445 remapped = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
11446 data = cpl_image_get_data(remapped);
11447 sdata = cpl_image_get_data(spatial);
11448 wdata = cpl_image_get_data(calibration);
11450 nslits = cpl_table_get_nrow(slits);
11451 slit_id = cpl_table_get_data_int(slits,
"slit_id");
11452 order = cpl_table_get_ncol(polytraces) - 2;
11453 position = cpl_table_get_data_int(slits,
"position");
11454 length = cpl_table_get_data_int(slits,
"length");
11461 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
11462 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
11464 for (i = 0; i < nslits; i++) {
11466 if (length[i] == 0)
11480 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
11482 start_pixel = refpixel - pixel_below;
11483 if (start_pixel < 0)
11486 end_pixel = refpixel + pixel_above;
11487 if (end_pixel > nx)
11496 polytop = cpl_polynomial_new(1);
11497 for (k = 0; k <= order; k++) {
11498 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
11500 cpl_polynomial_delete(polytop);
11504 cpl_polynomial_set_coeff(polytop, &k, coeff);
11508 polybot = cpl_polynomial_new(1);
11509 for (k = 0; k <= order; k++) {
11510 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
11512 cpl_polynomial_delete(polybot);
11516 cpl_polynomial_set_coeff(polybot, &k, coeff);
11519 if (missing_top && missing_bot) {
11520 cpl_msg_debug(func,
"Slit %d was not traced: no extraction!",
11532 cpl_msg_debug(func,
"Upper edge of slit %d was not traced: "
11533 "the spectral curvature of the lower edge "
11534 "is used instead.", slit_id[i]);
11535 polytop = cpl_polynomial_duplicate(polybot);
11536 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
11537 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
11539 coeff = cpl_polynomial_get_coeff(polybot, &k);
11540 coeff += ytop - ybot;
11541 cpl_polynomial_set_coeff(polytop, &k, coeff);
11545 cpl_msg_debug(func,
"Lower edge of slit %d was not traced: "
11546 "the spectral curvature of the upper edge "
11547 "is used instead.", slit_id[i]);
11548 polybot = cpl_polynomial_duplicate(polytop);
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(polytop, &k);
11553 coeff -= ytop - ybot;
11554 cpl_polynomial_set_coeff(polybot, &k, coeff);
11564 xdata = wdata + nx*position[i];
11565 npseudo = length[i] - 1;
11571 for (j = start_pixel; j < end_pixel; j++) {
11572 top = cpl_polynomial_eval_1d(polytop, j, NULL);
11573 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
11574 for (k = 0; k <= npseudo; k++) {
11575 ypos = top - k*(top-bot)/npseudo;
11585 if (yint < 0 || yint >= ny-1) {
11590 value = sdata[j + nx*yint];
11592 fvalue = value - ivalue;
11593 if (ivalue < npseudo && ivalue >= 0) {
11594 vtop = xdata[j + nx*(npseudo-ivalue)];
11595 vbot = xdata[j + nx*(npseudo-ivalue-1)];
11604 else if (vbot < 1.0) {
11610 else if (fabs(vbot-vtop) > 10*dispersion) {
11614 value = vtop*(1-fvalue) + vbot*fvalue;
11616 data[j + nx*yint] = value;
11626 if (yprev - yint > 1) {
11627 value = sdata[j + nx*(yint+1)];
11629 fvalue = value - ivalue;
11630 if (ivalue < npseudo && ivalue >= 0) {
11631 vtop = xdata[j + nx*(npseudo-ivalue)];
11632 vbot = xdata[j + nx*(npseudo-ivalue-1)];
11635 value = data[j + nx*(yint+1)];
11641 else if (vbot < 1.0) {
11644 else if (fabs(vbot-vtop) > 2*dispersion) {
11648 value = vtop*(1-fvalue) + vbot*fvalue;
11650 data[j + nx*(yint+1)] = value;
11658 cpl_polynomial_delete(polytop);
11659 cpl_polynomial_delete(polybot);
11739 cpl_image *spatial, cpl_table *slits,
11740 cpl_table *polytraces,
double reference,
11741 double blue,
double red,
double dispersion,
11744 const char *func =
"mos_map_spectrum";
11746 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
11748 cpl_polynomial *polytop;
11749 cpl_polynomial *polybot;
11750 cpl_image *remapped;
11751 cpl_image **exslit;
11756 double lambda00, lambda01, lambda10, lambda11, lambda;
11757 double space00, space01, space10, space11, space;
11758 double value00, value01, value10, value11, value0, value1, value;
11763 double xfrac, yfrac;
11776 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
11777 int missing_top, missing_bot;
11786 if (spectra == NULL || spatial == NULL || wavecalib == NULL ||
11787 slits == NULL || polytraces == NULL) {
11788 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
11792 if (dispersion <= 0.0) {
11793 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11797 if (red - blue < dispersion) {
11798 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
11802 nx = cpl_image_get_size_x(spectra);
11803 ny = cpl_image_get_size_y(spectra);
11805 if (nx != cpl_image_get_size_x(spatial) ||
11806 ny != cpl_image_get_size_y(spatial) ||
11807 nx != cpl_image_get_size_x(wavecalib) ||
11808 ny != cpl_image_get_size_y(wavecalib)) {
11809 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
11813 nlambda = STRETCH_FACTOR * (red - blue) / dispersion;
11814 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
11815 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
11817 data = cpl_image_get_data(spectra);
11818 sdata = cpl_image_get_data(spatial);
11819 wdata = cpl_image_get_data(wavecalib);
11821 nslits = cpl_table_get_nrow(slits);
11822 slit_id = cpl_table_get_data_int(slits,
"slit_id");
11823 order = cpl_table_get_ncol(polytraces) - 2;
11824 position = cpl_table_get_data_int(slits,
"position");
11825 length = cpl_table_get_data_int(slits,
"length");
11827 exslit = cpl_calloc(nslits,
sizeof(cpl_image *));
11829 for (i = 0; i < nslits; i++) {
11845 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
11847 start_pixel = refpixel - pixel_below;
11848 if (start_pixel < 1)
11851 end_pixel = refpixel + pixel_above;
11852 if (end_pixel > nx)
11861 polytop = cpl_polynomial_new(1);
11862 for (k = 0; k <= order; k++) {
11863 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
11865 cpl_polynomial_delete(polytop);
11869 cpl_polynomial_set_coeff(polytop, &k, coeff);
11873 polybot = cpl_polynomial_new(1);
11874 for (k = 0; k <= order; k++) {
11875 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
11877 cpl_polynomial_delete(polybot);
11881 cpl_polynomial_set_coeff(polybot, &k, coeff);
11884 if (missing_top && missing_bot) {
11885 cpl_msg_debug(func,
"Slit %d was not traced: no extraction!",
11897 cpl_msg_debug(func,
"Upper edge of slit %d was not traced: "
11898 "the spectral curvature of the lower edge "
11899 "is used instead.", slit_id[i]);
11900 polytop = cpl_polynomial_duplicate(polybot);
11901 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
11902 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
11904 coeff = cpl_polynomial_get_coeff(polybot, &k);
11905 coeff += ytop - ybot;
11906 cpl_polynomial_set_coeff(polytop, &k, coeff);
11910 cpl_msg_debug(func,
"Lower edge of slit %d was not traced: "
11911 "the spectral curvature of the upper edge "
11912 "is used instead.", slit_id[i]);
11913 polybot = cpl_polynomial_duplicate(polytop);
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(polytop, &k);
11918 coeff -= ytop - ybot;
11919 cpl_polynomial_set_coeff(polybot, &k, coeff);
11926 top = cpl_polynomial_eval_1d(polytop, refpixel, NULL);
11927 bot = cpl_polynomial_eval_1d(polybot, refpixel, NULL);
11928 npseudo = ceil(top-bot) + 1;
11931 cpl_polynomial_delete(polytop);
11932 cpl_polynomial_delete(polybot);
11933 cpl_msg_debug(func,
"Slit %d was badly traced: no extraction!",
11938 exslit[i] = cpl_image_new(nlambda, npseudo+1, CPL_TYPE_FLOAT);
11939 xdata = cpl_image_get_data(exslit[i]);
11945 for (x = start_pixel; x < end_pixel; x++) {
11946 top = cpl_polynomial_eval_1d(polytop, x, NULL);
11947 bot = cpl_polynomial_eval_1d(polybot, x, NULL);
11958 for (y = ibot; y < itop; y++) {
11959 lambda11 = wdata[x + y*nx];
11960 if (lambda11 < 1.0)
11962 space11 = sdata[x + y*nx];
11965 lambda01 = wdata[x - 1 + y*nx];
11966 if (lambda01 < 1.0)
11968 space01 = sdata[x - 1 + y*nx];
12001 lambda10 = wdata[x + shift + (y+1)*nx];
12002 if (lambda10 < 1.0)
12004 space10 = sdata[x + shift + (y+1)*nx];
12007 lambda00 = wdata[x - 1 + shift + (y+1)*nx];
12008 if (lambda00 < 1.0)
12010 space00 = sdata[x - 1 + shift + (y+1)*nx];
12020 dL = lambda11 - lambda01;
12021 dS = space11 - space10;
12028 L = (lambda11 - blue)/dispersion + 0.5;
12031 if (L < 0 || L >= nlambda)
12033 if (S < 0 || S > npseudo)
12040 lambda = blue + L*dispersion;
12052 xfrac = (lambda11-lambda)/dL;
12053 yfrac = (space11-space)/dS;
12064 value11 = data[x + y*nx];
12065 value01 = data[x - 1 + y*nx];
12066 value10 = data[x + shift + (y+1)*nx];
12067 value00 = data[x + shift - 1 + (y+1)*nx];
12073 value1 = (1-xfrac)*value11 + xfrac*value01;
12074 value0 = (1-xfrac)*value10 + xfrac*value00;
12075 value = (1-yfrac)*value1 + yfrac*value0;
12082 xdata[L + nlambda*(npseudo-S)] = value;
12086 cpl_polynomial_delete(polytop);
12087 cpl_polynomial_delete(polybot);
12095 for (i = 0; i < nslits; i++)
12097 ysize += cpl_image_get_size_y(exslit[i]);
12099 remapped = cpl_image_new(nlambda, ysize, CPL_TYPE_FLOAT);
12102 for (i = 0; i < nslits; i++) {
12104 yint += cpl_image_get_size_y(exslit[i]);
12105 cpl_image_copy(remapped, exslit[i], 1, ysize - yint);
12106 cpl_image_delete(exslit[i]);
12107 cpl_table_set_int(slits,
"position", i, ysize - yint - 1);
12151 double dispersion,
double factor,
int minpoints,
12154 const char *func =
"mos_sky_map_super";
12156 cpl_vector **vector;
12157 cpl_vector **wvector;
12158 double firstLambda, lastLambda;
12159 double lambda, lambda1, lambda2;
12160 double value, value1, value2;
12166 int first_valid, valid_bins;
12170 double *sky_spectrum;
12177 if (spectra == NULL || wavemap == NULL || skymap == NULL) {
12178 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12182 if (dispersion <= 0.0) {
12183 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12184 cpl_msg_error(func,
"Negative dispersion: %s", cpl_error_get_message());
12188 nx = cpl_image_get_size_x(spectra);
12189 ny = cpl_image_get_size_y(spectra);
12192 if (nx != cpl_image_get_size_x(wavemap) ||
12193 ny != cpl_image_get_size_y(wavemap) ||
12194 nx != cpl_image_get_size_x(skymap) ||
12195 ny != cpl_image_get_size_y(skymap)) {
12196 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
12197 cpl_msg_error(func,
"Image sizes: %s", cpl_error_get_message());
12201 if (factor < 1.0) {
12202 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12203 cpl_msg_error(func,
"Undersampling (%f): %s", factor,
12204 cpl_error_get_message());
12208 if (minpoints < 0) {
12209 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12210 cpl_msg_error(func,
"Negative threshold: %s", cpl_error_get_message());
12214 dispersion /= factor;
12221 data = cpl_image_get_data(wavemap);
12223 for (i = 0; i < npix; i++) {
12224 if (data[i] > 1.0) {
12225 min = max = data[i];
12231 for (i = j; i < npix; i++) {
12248 nbin = (lastLambda - firstLambda) / dispersion;
12257 count = cpl_calloc(nbin,
sizeof(
int));
12259 data = cpl_image_get_data(wavemap);
12261 for (i = 0; i < npix; i++) {
12264 bin = (data[i] - firstLambda) / dispersion;
12270 for (i = 0; i < nbin; i++)
12271 if (count[i] >= minpoints)
12274 if (valid_bins < nbin/3) {
12275 cpl_msg_warning(func,
"Cannot determine a good global sky "
12276 "spectrum from input data");
12288 vector = cpl_calloc(nbin,
sizeof(cpl_vector *));
12289 wvector = cpl_calloc(nbin,
sizeof(cpl_vector *));
12290 for (i = 0; i < nbin; i++) {
12291 if (count[i] >= minpoints) {
12292 vector[i] = cpl_vector_new(count[i]);
12293 wvector[i] = cpl_vector_new(count[i]);
12304 data = cpl_image_get_data(wavemap);
12305 sdata = cpl_image_get_data(spectra);
12307 for (i = 0; i < npix; i++) {
12310 bin = (data[i] - firstLambda) / dispersion;
12313 cpl_vector_set(vector[bin], count[bin], sdata[i]);
12314 cpl_vector_set(wvector[bin], count[bin], data[i]);
12326 sky_spectrum = cpl_calloc(nbin,
sizeof(
double));
12327 sky_wave = cpl_calloc(nbin,
sizeof(
double));
12328 for (i = 0; i < nbin; i++) {
12330 sky_spectrum[i] = cpl_vector_get_median_const(vector[i]);
12331 sky_wave[i] = cpl_vector_get_median_const(wvector[i]);
12332 cpl_vector_delete(vector[i]);
12333 cpl_vector_delete(wvector[i]);
12345 for (i = 0; i < nbin; i++) {
12346 if (count[i] >= minpoints) {
12352 for (i = first_valid; i < nbin; i++) {
12353 if (count[i] < minpoints) {
12354 sky_wave[i] = firstLambda + (i+0.5)*dispersion;
12355 for (j = i+1; j < nbin; j++) {
12356 if (count[j] >= minpoints) {
12357 if (sky_wave[j] - sky_wave[i-1] < 0.1) {
12358 sky_spectrum[i] = (sky_spectrum[j] + sky_spectrum[i-1])
12362 frac = (sky_wave[i] - sky_wave[i-1])
12363 / (sky_wave[j] - sky_wave[i-1]);
12364 sky_spectrum[i] = frac * sky_spectrum[j]
12365 + (1 - frac) * sky_spectrum[i-1];
12377 sky = cpl_table_new(nbin);
12378 cpl_table_wrap_double(sky, sky_wave,
"wavelength");
12379 cpl_table_wrap_double(sky, sky_spectrum,
"sky");
12380 cpl_table_wrap_int(sky, count,
"npoints");
12387 data = cpl_image_get_data(wavemap);
12388 sdata = cpl_image_get_data(spectra);
12389 kdata = cpl_image_get_data(skymap);
12391 for (i = 0; i < npix; i++) {
12400 bin = (lambda - firstLambda) / dispersion;
12401 lambda1 = sky_wave[bin];
12402 value1 = sky_spectrum[bin];
12403 if (lambda1 < lambda) {
12406 lambda2 = sky_wave[bin];
12407 value2 = sky_spectrum[bin];
12408 if (lambda2 - lambda1 < 0.1) {
12409 value = (value1 + value2) / 2;
12412 frac = (lambda - lambda1) / (lambda2 - lambda1);
12413 value = frac * value2 + (1 - frac) * value1;
12425 lambda1 = sky_wave[bin];
12426 value1 = sky_spectrum[bin];
12427 if (lambda2 - lambda1 < 0.1) {
12428 value = (value1 + value2) / 2;
12431 frac = (lambda - lambda1) / (lambda2 - lambda1);
12432 value = frac * value2 + (1 - frac) * value1;
12443 cpl_table_erase_window(sky, 0, first_valid);
12484 double dispersion, cpl_image *skymap)
12486 const char *func =
"mos_sky_map";
12488 cpl_vector **vector;
12489 double firstLambda, lastLambda;
12490 double lambda, lambda1, lambda2;
12491 double value, value1, value2;
12499 double *sky_spectrum;
12506 if (spectra == NULL || wavemap == NULL || skymap == NULL) {
12507 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12511 if (dispersion <= 0.0) {
12512 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
12516 nx = cpl_image_get_size_x(spectra);
12517 ny = cpl_image_get_size_y(spectra);
12520 if (nx != cpl_image_get_size_x(wavemap) ||
12521 ny != cpl_image_get_size_y(wavemap) ||
12522 nx != cpl_image_get_size_x(skymap) ||
12523 ny != cpl_image_get_size_y(skymap)) {
12524 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
12533 data = cpl_image_get_data(wavemap);
12535 for (i = 0; i < npix; i++) {
12536 if (data[i] > 1.0) {
12537 min = max = data[i];
12543 for (i = j; i < npix; i++) {
12560 nbin = (lastLambda - firstLambda) / dispersion;
12569 count = cpl_calloc(nbin,
sizeof(
int));
12571 data = cpl_image_get_data(wavemap);
12573 for (i = 0; i < npix; i++) {
12576 bin = (data[i] - firstLambda) / dispersion;
12589 vector = cpl_calloc(nbin,
sizeof(cpl_vector *));
12590 for (i = 0; i < nbin; i++) {
12592 vector[i] = cpl_vector_new(count[i]);
12604 data = cpl_image_get_data(wavemap);
12605 sdata = cpl_image_get_data(spectra);
12607 for (i = 0; i < npix; i++) {
12610 bin = (data[i] - firstLambda) / dispersion;
12612 cpl_vector_set(vector[bin], count[bin], sdata[i]);
12623 sky_spectrum = cpl_calloc(nbin,
sizeof(
double));
12624 for (i = 0; i < nbin; i++) {
12626 sky_spectrum[i] = cpl_vector_get_median_const(vector[i]);
12627 cpl_vector_delete(vector[i]);
12645 sky = cpl_table_new(nbin);
12646 cpl_table_new_column(sky,
"wavelength", CPL_TYPE_DOUBLE);
12647 cpl_table_set_column_unit(sky,
"wavelength",
"pixel");
12648 cpl_table_wrap_double(sky, sky_spectrum,
"sky");
12649 cpl_table_wrap_int(sky, count,
"npoints");
12650 for (i = 0; i < nbin; i++)
12651 cpl_table_set_double(sky,
"wavelength", i,
12652 firstLambda + (i+0.5)*dispersion);
12659 data = cpl_image_get_data(wavemap);
12660 sdata = cpl_image_get_data(spectra);
12661 kdata = cpl_image_get_data(skymap);
12662 wdata = cpl_table_get_data_double(sky,
"wavelength");
12664 for (i = 0; i < npix; i++) {
12673 bin = (lambda - firstLambda) / dispersion;
12674 lambda1 = wdata[bin];
12675 value1 = sky_spectrum[bin];
12676 if (lambda1 < lambda) {
12679 lambda2 = wdata[bin];
12680 value2 = sky_spectrum[bin];
12681 value = ((lambda2 - lambda)*value1
12682 + (lambda - lambda1)*value2) / dispersion;
12693 lambda1 = wdata[bin];
12694 value1 = sky_spectrum[bin];
12695 value = ((lambda2 - lambda)*value1
12696 + (lambda - lambda1)*value2)/dispersion;
12727 const char *func =
"mos_sky_local_old";
12735 int xlow, ylow, xhig, yhig;
12743 if (spectra == NULL) {
12744 cpl_msg_error(func,
12745 "A scientific rectified spectral image must be given");
12746 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12750 if (slits == NULL) {
12751 cpl_msg_error(func,
"A slits position table must be given");
12752 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12756 nslits = cpl_table_get_nrow(slits);
12757 slit_id = cpl_table_get_data_int(slits,
"slit_id");
12758 position = cpl_table_get_data_int(slits,
"position");
12759 length = cpl_table_get_data_int(slits,
"length");
12761 nx = cpl_image_get_size_x(spectra);
12762 ny = cpl_image_get_size_y(spectra);
12764 skymap = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
12768 for (i = 0; i < nslits; i++) {
12770 if (length[i] == 0)
12783 ylow = position[i] + 1;
12784 yhig = ylow + length[i] - 1;
12786 exslit = cpl_image_extract(spectra, xlow, ylow, xhig, yhig);
12787 sky = cpl_image_collapse_median_create(exslit, 0, 0, 1);
12788 cpl_image_delete(exslit);
12790 data = cpl_image_get_data(skymap);
12791 data += nx * position[i];
12793 for (j = 0; j < length[i]; j++) {
12794 sdata = cpl_image_get_data(sky);
12795 for (k = 0; k < nx; k++) {
12796 *data++ = *sdata++;
12800 cpl_image_delete(sky);
12829 const char *func =
"mos_sky_local";
12831 char name[MAX_COLNAME];
12833 cpl_polynomial *fit;
12834 cpl_vector *points;
12835 cpl_vector *values;
12836 cpl_vector *keep_points;
12837 cpl_vector *keep_values;
12840 cpl_image *subtracted;
12841 cpl_image *profile;
12843 cpl_table *objects;
12851 int xlow, ylow, xhig, yhig;
12864 if (spectra == NULL) {
12865 cpl_msg_error(func,
12866 "A scientific rectified spectral image must be given");
12867 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12871 if (slits == NULL) {
12872 cpl_msg_error(func,
"A slits position table must be given");
12873 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12878 cpl_msg_error(func,
"Invalid fit order");
12879 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
12883 nslits = cpl_table_get_nrow(slits);
12884 slit_id = cpl_table_get_data_int(slits,
"slit_id");
12885 position = cpl_table_get_data_int(slits,
"position");
12886 length = cpl_table_get_data_int(slits,
"length");
12888 nx = cpl_image_get_size_x(spectra);
12889 ny = cpl_image_get_size_y(spectra);
12891 skymap = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
12895 for (i = 0; i < nslits; i++) {
12897 if (length[i] == 0)
12910 ylow = position[i] + 1;
12911 yhig = ylow + length[i] - 1;
12913 exslit = cpl_image_extract(spectra, xlow, ylow, xhig, yhig);
12914 sky = cpl_image_collapse_median_create(exslit, 0, 0, 1);
12915 cpl_image_delete(exslit);
12917 data = cpl_image_get_data(skymap);
12918 data += nx * position[i];
12920 for (j = 0; j < length[i]; j++) {
12921 sdata = cpl_image_get_data(sky);
12922 for (k = 0; k < nx; k++) {
12923 *data++ = *sdata++;
12927 cpl_image_delete(sky);
12935 subtracted = cpl_image_duplicate(spectra);
12936 cpl_image_subtract(subtracted, skymap);
12937 cpl_image_delete(skymap);
12944 objects = cpl_table_duplicate(slits);
12946 cpl_image_delete(profile);
12947 cpl_image_delete(subtracted);
12956 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
12957 while (cpl_table_has_column(objects, name)) {
12959 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
12962 is_sky = cpl_calloc(ny,
sizeof(
int));
12964 for (i = 0; i < nslits; i++) {
12966 if (length[i] == 0)
12969 ylow = position[i] + margin;
12970 yhig = position[i] + length[i] - margin;
12972 for (j = ylow; j < yhig; j++)
12975 for (j = 1; j < maxobjects; j++) {
12976 snprintf(name, MAX_COLNAME,
"object_%d", j);
12977 if (cpl_table_is_valid(objects, name, i)) {
12978 snprintf(name, MAX_COLNAME,
"start_%d", j);
12979 ylow = cpl_table_get_int(objects, name, i, NULL);
12980 snprintf(name, MAX_COLNAME,
"end_%d", j);
12981 yhig = cpl_table_get_int(objects, name, i, NULL);
12982 for (k = ylow; k <= yhig; k++)
12992 ylow = position[i] + margin + 1;
12993 yhig = position[i] + length[i] - margin - 1;
12995 for (j = ylow; j < yhig; j++)
12997 if (is_sky[j-1] == 0 && is_sky[j+1] == 0)
13007 skymap = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
13009 for (i = 0; i < nslits; i++) {
13011 if (length[i] == 0)
13014 ylow = position[i];
13015 yhig = ylow + length[i];
13018 for (j = ylow; j < yhig; j++)
13022 if (nsky > order + 1) {
13024 points = cpl_vector_new(nsky);
13026 for (j = ylow; j < yhig; j++) {
13028 cpl_vector_set(points, nsky, j);
13033 exslit = cpl_image_extract(spectra, 1, ylow+1, nx, yhig);
13034 xdata = cpl_image_get_data(exslit);
13035 values = cpl_vector_new(nsky);
13037 for (j = 0; j < nx; j++) {
13039 for (k = ylow; k < yhig; k++) {
13041 cpl_vector_set(values, nsky, xdata[j+(k-ylow)*nx]);
13050 median = cpl_vector_get_median_const(values);
13051 vdata = cpl_vector_get_data(values);
13052 pdata = cpl_vector_get_data(points);
13054 for (k = 0; k < nsky; k++) {
13055 if (fabs(vdata[k] - median) < 100) {
13057 vdata[k-nbad] = vdata[k];
13058 pdata[k-nbad] = pdata[k];
13068 if (nbad && nsky - nbad > order + 1) {
13069 keep_values = values;
13070 keep_points = points;
13071 values = cpl_vector_wrap(nsky-nbad, vdata);
13072 points = cpl_vector_wrap(nsky-nbad, pdata);
13075 if (nsky - nbad > order + 1) {
13077 fit = cpl_polynomial_fit_1d_create(points, values,
13081 for (k = ylow; k < yhig; k++) {
13082 xdata[j+(k-ylow)*nx] =
13083 cpl_polynomial_eval_1d(fit, k, NULL);
13086 cpl_polynomial_delete(fit);
13092 for (k = 0; k < nsky; k++) {
13093 xdata[j+k*nx] = median;
13097 if (nbad && nsky - nbad > order + 1) {
13098 cpl_vector_unwrap(values);
13099 cpl_vector_unwrap(points);
13100 values = keep_values;
13101 points = keep_points;
13106 for (k = ylow; k < yhig; k++) {
13108 cpl_vector_set(points, nsky, k);
13116 cpl_vector_delete(values);
13117 cpl_vector_delete(points);
13119 cpl_image_copy(skymap, exslit, 1, ylow+1);
13120 cpl_image_delete(exslit);
13124 exslit = cpl_image_extract(spectra, 1, ylow+1, nx, yhig);
13125 xdata = cpl_image_get_data(exslit);
13126 values = cpl_vector_new(nsky);
13128 for (j = 0; j < nx; j++) {
13130 for (k = ylow; k < yhig; k++) {
13132 cpl_vector_set(values, nsky, xdata[j+(k-ylow)*nx]);
13137 median = cpl_vector_get_median_const(values);
13139 for (k = ylow; k < yhig; k++)
13140 xdata[j+(k-ylow)*nx] = median;
13144 cpl_vector_delete(values);
13146 cpl_image_copy(skymap, exslit, 1, ylow+1);
13147 cpl_image_delete(exslit);
13151 cpl_msg_warning(func,
"Too few sky points in slit %d", i + 1);
13183 float threshold,
float ratio)
13185 const char *func =
"mos_clean_cosmics";
13187 cpl_image *smoothImage;
13189 cpl_matrix *kernel;
13194 float sigma, sum, value, smoothValue;
13198 int iMin, iMax, jMin, jMax, iPosMax, jPosMax;
13204 int pos, i, j, k, l, ii, jj, iii = 0, jjj = 0;
13206 int found, foundContiguousCandidate;
13211 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13224 xLen = cpl_image_get_size_x(image);
13225 yLen = cpl_image_get_size_y(image);
13227 if (xLen < 4 || yLen < 4)
13228 return CPL_ERROR_NONE;
13230 nPix = xLen * yLen;
13253 idata = cpl_image_get_data(image);
13257 for (i = 0; i < nPix; i++) {
13258 if (idata[i] < -0.00001) {
13267 cosmic = cpl_calloc(nPix,
sizeof(
int));
13269 if (threshold < 0.)
13274 kernel = cpl_matrix_new(3, 3);
13275 cpl_matrix_fill(kernel, 1.0);
13276 cpl_matrix_set(kernel, 1, 1, 0.0);
13277 smoothImage = cpl_image_filter_median(image, kernel);
13278 cpl_matrix_delete(kernel);
13289 sdata = cpl_image_get_data(smoothImage);
13291 for (j = 1; j < yLen - 1; j++) {
13292 for (i = 1; i < xLen - 1; i++) {
13293 value = idata[i + j * xLen];
13294 smoothValue = sdata[i + j * xLen];
13295 if (smoothValue < 1.0)
13297 sigma = sqrt(noise * noise + smoothValue / gain);
13298 if (value - smoothValue >= threshold * sigma)
13299 cosmic[i + j * xLen] = -1;
13303 cpl_image_delete(smoothImage);
13312 for (pos = first; pos < nPix; pos++) {
13313 if (cosmic[pos] == -1) {
13333 iMin = iMax = iPosMax = i;
13334 jMin = jMax = jPosMax = j;
13335 fMax = idata[i + j * xLen];
13338 foundContiguousCandidate = 0;
13339 for (l = 0; l <= 1; l++) {
13340 for (k = 0; k <= 1; k++) {
13347 jj = j + k + l - 1;
13348 if (cosmic[ii + jj * xLen] == -1) {
13349 foundContiguousCandidate = 1;
13350 cosmic[ii + jj * xLen] = 2;
13368 if (idata[ii + jj * xLen] > fMax) {
13369 fMax = idata[ii + jj * xLen];
13382 cosmic[i + j * xLen] = 3;
13384 if (foundContiguousCandidate) {
13406 for (l = jMin; l <= jMax; l++) {
13407 for (k = iMin; k <= iMax; k++) {
13408 if (cosmic[k + l * xLen] == 2) {
13411 foundContiguousCandidate = 1;
13415 if (foundContiguousCandidate)
13418 }
while (foundContiguousCandidate);
13427 for (l = -1; l <= 1; l++) {
13428 for (k = -1; k <= 1; k++) {
13429 if (l != 0 || k != 0) {
13430 sum += idata[iPosMax + k + (jPosMax + l) * xLen];
13436 if (fMax > ratio * sum) {
13437 for (l = jMin - 1; l <= jMax + 1; l++) {
13438 for (k = iMin - 1; k <= iMax + 1; k++) {
13439 if (cosmic[k + l * xLen] == 3) {
13440 cosmic[k + l * xLen] = 1;
13447 for (l = jMin - 1; l <= jMax + 1; l++) {
13448 for (k = iMin - 1; k <= iMax + 1; k++) {
13449 if (cosmic[k + l * xLen] != -1) {
13450 if (cosmic[k + l * xLen] == 1)
13452 cosmic[k + l * xLen] = 0;
13465 table = cpl_table_new(numCosmic);
13466 cpl_table_new_column(table,
"x", CPL_TYPE_INT);
13467 cpl_table_new_column(table,
"y", CPL_TYPE_INT);
13468 cpl_table_set_column_unit(table,
"x",
"pixel");
13469 cpl_table_set_column_unit(table,
"y",
"pixel");
13470 xdata = cpl_table_get_data_int(table,
"x");
13471 ydata = cpl_table_get_data_int(table,
"y");
13473 for (pos = 0, i = 0; pos < nPix; pos++) {
13474 if (cosmic[pos] == 1) {
13475 xdata[i] = (pos % xLen);
13476 ydata[i] = (pos / xLen);
13481 mos_clean_bad_pixels(image, table, 1);
13484 cpl_table_delete(table);
13486 return CPL_ERROR_NONE;
13491 cpl_error_code mos_clean_bad_pixels(cpl_image *image, cpl_table *table,
13494 const char *func =
"mos_clean_cosmics";
13499 int xlen, ylen, totPix;
13500 int nBadPixels = 0;
13501 int sign, foundFirst;
13502 int *xValue = NULL;
13503 int *yValue = NULL;
13509 int sx[] = {0, 1, 1, 1};
13510 int sy[] = {1,-1, 0, 1};
13511 int searchHorizon = 100;
13515 if (image == NULL || table == NULL)
13516 return cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13518 if (1 != cpl_table_has_column(table,
"x"))
13519 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
13521 if (1 != cpl_table_has_column(table,
"y"))
13522 return cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
13524 if (CPL_TYPE_INT != cpl_table_get_column_type(table,
"x"))
13525 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
13527 if (CPL_TYPE_INT != cpl_table_get_column_type(table,
"y"))
13528 return cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
13530 nBadPixels = cpl_table_get_nrow(table);
13533 xlen = cpl_image_get_size_x(image);
13534 ylen = cpl_image_get_size_y(image);
13535 idata = cpl_image_get_data(image);
13536 totPix = xlen * ylen;
13537 if (((
float) nBadPixels) / ((
float) totPix) < percent/100.) {
13538 isBadPix = cpl_calloc(totPix,
sizeof(
int));
13541 cpl_msg_warning(func,
"Too many bad pixels (> %d%%): "
13542 "skip bad pixel correction", percent);
13543 return cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13547 cpl_msg_debug(func,
"No pixel values to interpolate");
13548 return CPL_ERROR_NONE;
13551 xValue = cpl_table_get_data_int(table,
"x");
13552 yValue = cpl_table_get_data_int(table,
"y");
13554 for (i = 0; i < nBadPixels; i++)
13555 isBadPix[xValue[i] + yValue[i] * xlen] = 1;
13557 for (i = 0; i < nBadPixels; i++) {
13572 for (j = 0; j < 4; j++) {
13578 estimate[nPairs] = 0.;
13581 for (k = 0; k < 2; k++) {
13587 cx += sign * sx[j];
13588 cy += sign * sy[j];
13589 if (cx < 0 || cx >= xlen || cy < 0 || cy >= ylen)
13592 }
while (isBadPix[cx + cy * xlen] && d < searchHorizon);
13594 if (cx >= 0 && cx < xlen &&
13595 cy >= 0 && cy < ylen && d < searchHorizon) {
13601 save = idata[cx + cy * xlen];
13602 estimate[nPairs] += save / d;
13603 sumd += 1. / (double) d;
13605 estimate[nPairs] /= sumd;
13620 estimate[nPairs] = save;
13635 idata[xValue[i] + yValue[i] * xlen] =
13636 cpl_tools_get_median_float(estimate, nPairs);
13638 else if (nPairs == 2) {
13639 idata[xValue[i] + yValue[i] * xlen] =
13640 (estimate[0] + estimate[1]) / 2.;
13642 else if (nPairs == 1) {
13643 idata[xValue[i] + yValue[i] * xlen] = estimate[0];
13646 cpl_msg_debug(func,
"Cannot correct bad pixel %d,%d\n",
13647 xValue[i], yValue[i]);
13651 cpl_free(isBadPix);
13653 return CPL_ERROR_NONE;
13687 cpl_table *polytraces,
double reference,
13688 double blue,
double red,
double dispersion)
13690 const char *func =
"mos_spatial_map";
13692 const char *clab[6] = {
"c0",
"c1",
"c2",
"c3",
"c4",
"c5"};
13694 cpl_polynomial *polytop;
13695 cpl_polynomial *polybot;
13696 cpl_image *calibration;
13709 int pixel_above, pixel_below, refpixel, start_pixel, end_pixel;
13710 int missing_top, missing_bot;
13717 if (spectra == NULL || slits == NULL || polytraces == NULL) {
13718 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13722 if (dispersion <= 0.0) {
13723 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13727 if (red - blue < dispersion) {
13728 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13732 nx = cpl_image_get_size_x(spectra);
13733 ny = cpl_image_get_size_y(spectra);
13735 calibration = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
13736 data = cpl_image_get_data(calibration);
13738 length = cpl_table_get_data_int(slits,
"length");
13739 nslits = cpl_table_get_nrow(slits);
13740 slit_id = cpl_table_get_data_int(slits,
"slit_id");
13741 order = cpl_table_get_ncol(polytraces) - 2;
13748 pixel_above = STRETCH_FACTOR * (red - reference) / dispersion;
13749 pixel_below = STRETCH_FACTOR * (reference - blue) / dispersion;
13751 for (i = 0; i < nslits; i++) {
13753 if (length[i] == 0)
13767 refpixel = cpl_table_get_double(slits,
"xtop", i, NULL);
13769 start_pixel = refpixel - pixel_below;
13770 if (start_pixel < 0)
13773 end_pixel = refpixel + pixel_above;
13774 if (end_pixel > nx)
13783 polytop = cpl_polynomial_new(1);
13784 for (k = 0; k <= order; k++) {
13785 coeff = cpl_table_get_double(polytraces, clab[k], 2*i, &null);
13787 cpl_polynomial_delete(polytop);
13791 cpl_polynomial_set_coeff(polytop, &k, coeff);
13795 polybot = cpl_polynomial_new(1);
13796 for (k = 0; k <= order; k++) {
13797 coeff = cpl_table_get_double(polytraces, clab[k], 2*i+1, &null);
13799 cpl_polynomial_delete(polybot);
13803 cpl_polynomial_set_coeff(polybot, &k, coeff);
13806 if (missing_top && missing_bot) {
13807 cpl_msg_warning(func,
"Spatial map, slit %d was not traced!",
13819 cpl_msg_warning(func,
"Upper edge of slit %d was not traced: "
13820 "the spectral curvature of the lower edge "
13821 "is used instead.", slit_id[i]);
13822 polytop = cpl_polynomial_duplicate(polybot);
13823 ytop = cpl_table_get_double(slits,
"ytop", i, NULL);
13824 ybot = cpl_table_get_double(slits,
"ybottom", i, NULL);
13826 coeff = cpl_polynomial_get_coeff(polybot, &k);
13827 coeff += ytop - ybot;
13828 cpl_polynomial_set_coeff(polytop, &k, coeff);
13832 cpl_msg_warning(func,
"Lower edge of slit %d was not traced: "
13833 "the spectral curvature of the upper edge "
13834 "is used instead.", slit_id[i]);
13835 polybot = cpl_polynomial_duplicate(polytop);
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(polytop, &k);
13840 coeff -= ytop - ybot;
13841 cpl_polynomial_set_coeff(polybot, &k, coeff);
13844 top = cpl_polynomial_eval_1d(polytop, refpixel, NULL);
13845 bot = cpl_polynomial_eval_1d(polybot, refpixel, NULL);
13846 npseudo = ceil(top-bot) + 1;
13849 cpl_polynomial_delete(polytop);
13850 cpl_polynomial_delete(polybot);
13851 cpl_msg_warning(func,
"Slit %d was badly traced: no extraction!",
13856 for (j = start_pixel; j < end_pixel; j++) {
13857 top = cpl_polynomial_eval_1d(polytop, j, NULL);
13858 bot = cpl_polynomial_eval_1d(polybot, j, NULL);
13859 factor = (top-bot)/npseudo;
13860 for (k = 0; k <= npseudo; k++) {
13861 ypos = top - k*factor;
13863 yfra = ypos - yint;
13864 if (yint >= 0 && yint < ny-1) {
13865 data[j + nx*yint] = (top-yint)/factor;
13874 if (yprev - yint > 1) {
13875 data[j + nx*(yint+1)] = (top-yint-1)/factor;
13882 cpl_polynomial_delete(polytop);
13883 cpl_polynomial_delete(polybot);
13886 return calibration;
13953 int maxradius,
int conradius)
13955 const char *func =
"mos_detect_objects";
13957 cpl_image *profile;
13961 char name[MAX_COLNAME];
13965 int nobjects, objpos, totobj;
13972 double mindistance;
13979 const int min_pixels = 10;
13982 if (cpl_error_get_code() != CPL_ERROR_NONE)
13985 if (image == NULL || slits == NULL) {
13986 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
13993 if (maxradius < 0) {
13994 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
13998 if (conradius < 0) {
13999 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14003 nslits = cpl_table_get_nrow(slits);
14004 position = cpl_table_get_data_int(slits,
"position");
14005 length = cpl_table_get_data_int(slits,
"length");
14007 profile = cpl_image_collapse_create(image, 1);
14008 cpl_image_divide_scalar(profile, cpl_image_get_size_x(image));
14009 pdata = cpl_image_get_data(profile);
14014 for (i = 0; i < nslits; i++) {
14016 if (length[i] == 0)
14019 pos = position[i] + margin;
14020 count = length[i] - 2*margin;
14022 if (count < min_pixels)
14033 if (p[0] > p[1] && p[1] > p[2] && p[2] > p[3] && p[3] > 0) {
14038 for (j = 0; j < count - 3; j++) {
14040 if (p[j+1] > p[j]) {
14045 if (p[j+1] > p[j+2] && p[j+2] > 0) {
14051 if (p[j+1] > p[j+2] && p[j+2] > p[j+3] && p[j+3] > 0) {
14064 if (p[count-1] > p[count-2] && p[count-2] > p[count-3]
14065 && p[count-3] > p[count-4] && p[count-4] > 0) {
14077 reject = cpl_calloc(npeaks,
sizeof(
int));
14078 bright = cpl_calloc(npeaks,
sizeof(
double));
14079 place = cpl_calloc(npeaks,
sizeof(
double));
14082 if (p[0] > p[1] && p[1] > p[2] && p[2] > p[3] && p[3] > 0) {
14084 place[0] = position[i] + margin;
14089 for (j = 0; j < count - 3; j++) {
14091 if (p[j+1] > p[j]) {
14096 if (p[j+1] > p[j+2] && p[j+2] > 0) {
14098 bright[npeaks] = p[j];
14099 place[npeaks] = position[i] + margin + j + 1
14100 + values_to_dx(p[j-1], p[j], p[j+1]);
14106 if (p[j+1] > p[j+2] && p[j+2] > p[j+3] && p[j+3] > 0) {
14108 bright[npeaks] = p[j];
14109 place[npeaks] = position[i] + margin + j + 1
14110 + values_to_dx(p[j-1], p[j], p[j+1]);
14123 if (p[count-1] > p[count-2] && p[count-2] > p[count-3]
14124 && p[count-3] > p[count-4] && p[count-4] > 0) {
14125 bright[npeaks] = p[count-1];
14126 place[npeaks] = position[i] + count;
14135 if (fabs(place[0] - pos) < 1.0)
14137 if (fabs(place[npeaks-1] - pos - count) < 1.0)
14138 reject[npeaks-1] = 1;
14139 for (j = 0; j < npeaks; j++) {
14140 for (k = 0; k < npeaks; k++) {
14143 mindistance = conradius * bright[k] / bright[j]
14144 * bright[k] / bright[j];
14145 if (fabs(place[j] - place[k]) < mindistance)
14151 for (j = 0; j < npeaks; j++) {
14155 low = (place[j-1]*bright[j] + place[j]*bright[j-1])
14156 / (bright[j-1] + bright[j]) + 1;
14161 if (j < npeaks - 1) {
14162 hig = (place[j+1]*bright[j] + place[j]*bright[j+1])
14163 / (bright[j+1] + bright[j]) + 1;
14171 if (hig > pos + count)
14173 if (place[j] - low > maxradius)
14174 low = place[j] - maxradius;
14175 if (hig - place[j] > maxradius)
14176 hig = place[j] + maxradius;
14183 for (j = 0; j < npeaks; j++)
14187 for (j = 0; j < nobjects; j++) {
14188 snprintf(name, MAX_COLNAME,
"object_%d", j+1);
14189 if (cpl_table_has_column(slits, name))
14191 cpl_table_new_column(slits, name, CPL_TYPE_DOUBLE);
14192 snprintf(name, MAX_COLNAME,
"start_%d", j+1);
14193 cpl_table_new_column(slits, name, CPL_TYPE_INT);
14194 cpl_table_set_column_unit(slits, name,
"pixel");
14195 snprintf(name, MAX_COLNAME,
"end_%d", j+1);
14196 cpl_table_new_column(slits, name, CPL_TYPE_INT);
14197 cpl_table_set_column_unit(slits, name,
"pixel");
14198 snprintf(name, MAX_COLNAME,
"row_%d", j+1);
14199 cpl_table_new_column(slits, name, CPL_TYPE_INT);
14200 cpl_table_set_column_unit(slits, name,
"pixel");
14204 for (j = 0; j < npeaks; j++) {
14208 low = (place[j-1]*bright[j] + place[j]*bright[j-1])
14209 / (bright[j-1] + bright[j]) + 1;
14214 if (j < npeaks - 1) {
14215 hig = (place[j+1]*bright[j] + place[j]*bright[j+1])
14216 / (bright[j+1] + bright[j]) + 1;
14224 if (hig > pos + count)
14226 if (place[j] - low > maxradius)
14227 low = place[j] - maxradius;
14228 if (hig - place[j] > maxradius)
14229 hig = place[j] + maxradius;
14231 snprintf(name, MAX_COLNAME,
"object_%d", objpos);
14232 cpl_table_set_double(slits, name, i, place[j]);
14233 snprintf(name, MAX_COLNAME,
"start_%d", objpos);
14234 cpl_table_set_int(slits, name, i, low);
14235 snprintf(name, MAX_COLNAME,
"end_%d", objpos);
14236 cpl_table_set_int(slits, name, i, hig);
14237 snprintf(name, MAX_COLNAME,
"row_%d", objpos);
14238 cpl_table_set_int(slits, name, i, row + objpos - 1);
14245 if (maxobjects < nobjects)
14246 maxobjects = nobjects;
14255 row = cpl_table_get_nrow(slits);
14257 for (i = 0; i < row; i++) {
14258 for (j = 0; j < maxobjects; j++) {
14259 snprintf(name, MAX_COLNAME,
"row_%d", j+1);
14260 if (cpl_table_is_valid(slits, name, i))
14261 cpl_table_set_int(slits, name, i, totobj -
14262 cpl_table_get_int(slits, name, i, NULL));
14266 for (i = 0; i < maxobjects; i++) {
14267 snprintf(name, MAX_COLNAME,
"start_%d", i+1);
14268 cpl_table_fill_invalid_int(slits, name, -1);
14269 snprintf(name, MAX_COLNAME,
"end_%d", i+1);
14270 cpl_table_fill_invalid_int(slits, name, -1);
14271 snprintf(name, MAX_COLNAME,
"row_%d", i+1);
14272 cpl_table_fill_invalid_int(slits, name, -1);
14305 cpl_table *objects,
int extraction,
double ron,
14306 double gain,
int ncombined)
14308 const char *func =
"mos_extract_objects";
14310 char name[MAX_COLNAME];
14312 cpl_image **output;
14313 cpl_image *extracted;
14314 cpl_image *extr_sky;
14317 cpl_image *sci_var_win = NULL;
14327 if (science == NULL || sky == NULL) {
14328 cpl_msg_error(func,
"Both scientific exposures are required in input");
14329 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14333 if (objects == NULL) {
14334 cpl_msg_error(func,
"An object table is required in input");
14335 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14339 if (extraction < 0 || extraction > 1) {
14340 cpl_msg_error(func,
"Invalid extraction mode (%d): it should be "
14341 "either 0 or 1", extraction);
14342 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14347 cpl_msg_error(func,
"Invalid read-out-noise (%f ADU)", ron);
14348 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14353 cpl_msg_error(func,
"Invalid gain factor (%f e-/ADU)", gain);
14354 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14358 if (ncombined < 1) {
14359 cpl_msg_error(func,
"Invalid number of combined frames (%d): "
14360 "it should be at least 1", ncombined);
14361 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14372 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
14373 while (cpl_table_has_column(objects, name)) {
14375 snprintf(name, MAX_COLNAME,
"object_%d", maxobjects);
14384 nslits = cpl_table_get_nrow(objects);
14386 for (i = 0; i < nslits; i++) {
14387 for (j = 1; j < maxobjects; j++) {
14388 snprintf(name, MAX_COLNAME,
"object_%d", j);
14389 if (cpl_table_is_valid(objects, name, i))
14397 nx = cpl_image_get_size_x(science);
14399 output = cpl_calloc(3,
sizeof(cpl_image *));
14400 extracted = output[0] = cpl_image_new(nx, nobjects, CPL_TYPE_FLOAT);
14401 extr_sky = output[1] = cpl_image_new(nx, nobjects, CPL_TYPE_FLOAT);
14402 error = output[2] = cpl_image_new(nx, nobjects, CPL_TYPE_FLOAT);
14410 for (i = 0; i < nslits; i++) {
14411 for (j = 1; j < maxobjects; j++) {
14412 snprintf(name, MAX_COLNAME,
"object_%d", j);
14413 if (cpl_table_is_valid(objects, name, i)) {
14414 snprintf(name, MAX_COLNAME,
"start_%d", j);
14415 ylow = cpl_table_get_int(objects, name, i, NULL);
14416 snprintf(name, MAX_COLNAME,
"end_%d", j);
14417 yhig = cpl_table_get_int(objects, name, i, NULL);
14418 snprintf(name, MAX_COLNAME,
"row_%d", j);
14419 nobjects = cpl_table_get_int(objects, name, i, NULL);
14420 sciwin = cpl_image_extract(science, 1, ylow+1, nx, yhig);
14421 if(science_var != NULL)
14422 sci_var_win = cpl_image_extract(science_var, 1, ylow+1, nx, yhig);
14423 skywin = cpl_image_extract(sky, 1, ylow+1, nx, yhig);
14432 mos_extraction(sciwin, sci_var_win, skywin, extracted, extr_sky, error,
14433 nobjects, extraction, ron, gain, ncombined);
14440 cpl_image *total = cpl_image_add_create(sciwin, skywin);
14441 float *data = cpl_image_get_data_float(total);
14442 int size = cpl_image_get_size_x(total)
14443 * cpl_image_get_size_y(total);
14445 char *saturation_level = getenv(
"SATURATION_LEVEL");
14446 float saturation = 62000.0;
14447 char *max_saturated = getenv(
"MAX_SATURATED");
14448 int max_satur = 10;
14451 if (saturation_level)
14452 saturation = atof(saturation_level);
14455 max_satur = atoi(max_saturated);
14458 for (k = 0; k < size; k++) {
14459 if (data[k] > saturation) {
14461 if (saturated > max_satur) {
14467 if (saturated > max_satur)
14472 data = cpl_image_get_data(extracted);
14473 data[nobjects * nx] = saturated;
14476 cpl_image_delete(sciwin);
14477 cpl_image_delete(skywin);
14511 double dispersion,
int saturation,
14512 double *mfwhm,
double *rmsfwhm,
14513 double *resolution,
double *rmsres,
int *nlines)
14515 cpl_vector *vector;
14518 int position, maxpos;
14523 int threshold = 250;
14528 double min, max, halfmax;
14539 xlen = cpl_image_get_size_x(image);
14540 ylen = cpl_image_get_size_y(image);
14541 data = cpl_image_get_data(image);
14543 buffer = cpl_malloc(ylen *
sizeof(
double));
14549 position = floor((lambda - startwave) / dispersion + 0.5);
14551 sp = position - sradius;
14552 ep = position + sradius;
14554 if (sp < 0 || ep > xlen) {
14559 for (i = 0, n = 0; i < ylen; i++) {
14570 sp = position - radius;
14571 ep = position + radius;
14573 if (sp < 0 || ep > xlen) {
14584 min = max = data[sp + i * xlen];
14585 for (j = sp; j < ep; j++) {
14586 if (data[j + i * xlen] > max) {
14587 max = data[j + i * xlen];
14590 if (data[j + i * xlen] < min) {
14591 min = data[j + i * xlen];
14595 if (fabs(min) < 0.0000001)
14598 if (max - min < threshold)
14601 if (max > saturation)
14611 halfmax = (max + min)/ 2.0;
14615 for (j = maxpos; j < maxpos + radius; j++) {
14617 if (data[j + i * xlen] < halfmax) {
14618 fwhm = ifwhm + (data[j - 1 + i * xlen] - halfmax)
14619 / (data[j - 1 + i * xlen] - data[j + i * xlen]);
14627 for (j = maxpos; j > maxpos - radius; j--) {
14629 if (data[j + i * xlen] < halfmax) {
14630 fwhm += ifwhm + (data[j + 1 + i * xlen] - halfmax)
14631 / (data[j + 1 + i * xlen] - data[j + i * xlen]);
14639 buffer[n] = fwhm - 2.0;
14650 vector = cpl_vector_wrap(n, buffer);
14651 value = cpl_vector_get_median_const(vector);
14652 cpl_vector_unwrap(vector);
14655 for (i = 0, m = 0; i < n; i++) {
14656 if (fabs(buffer[i] - value) < cut) {
14657 rms += fabs(buffer[i] - value);
14670 value *= dispersion;
14676 *resolution = lambda / value;
14677 *rmsres = *resolution * rms / value;
14707 double dispersion,
int saturation,
14722 nref = cpl_vector_get_size(lines);
14723 line = cpl_vector_get_data(lines);
14725 table = cpl_table_new(nref);
14726 cpl_table_new_column(table,
"wavelength", CPL_TYPE_DOUBLE);
14727 cpl_table_set_column_unit(table,
"wavelength",
"Angstrom");
14728 cpl_table_new_column(table,
"fwhm", CPL_TYPE_DOUBLE);
14729 cpl_table_set_column_unit(table,
"fwhm",
"Angstrom");
14730 cpl_table_new_column(table,
"fwhm_rms", CPL_TYPE_DOUBLE);
14731 cpl_table_set_column_unit(table,
"fwhm_rms",
"Angstrom");
14732 cpl_table_new_column(table,
"resolution", CPL_TYPE_DOUBLE);
14733 cpl_table_new_column(table,
"resolution_rms", CPL_TYPE_DOUBLE);
14734 cpl_table_new_column(table,
"nlines", CPL_TYPE_INT);
14736 for (i = 0; i < nref; i++) {
14738 saturation, &fwhm, &rmsfwhm,
14739 &resolution, &rmsres, &nlines)) {
14740 cpl_table_set_double(table,
"wavelength", i, line[i]);
14741 cpl_table_set_double(table,
"fwhm", i, fwhm);
14742 cpl_table_set_double(table,
"fwhm_rms", i, rmsfwhm);
14743 cpl_table_set_double(table,
"resolution", i, resolution);
14744 cpl_table_set_double(table,
"resolution_rms", i, rmsres);
14745 cpl_table_set_int(table,
"nlines", i, nlines);
14748 cpl_table_set_int(table,
"nlines", i, 0);
14751 if (cpl_table_has_valid(table,
"wavelength"))
14754 cpl_table_delete(table);
14779 int ystart,
int yend,
double wstart,
double wend)
14781 const char *func =
"mos_integrate_signal";
14790 if (image == NULL || wavemap == NULL) {
14791 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14795 if (ystart > yend || wstart >= wend) {
14796 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14800 nx = cpl_image_get_size_x(image);
14801 ny = cpl_image_get_size_y(image);
14803 if (!(nx == cpl_image_get_size_x(wavemap)
14804 && ny == cpl_image_get_size_y(wavemap))) {
14805 cpl_error_set(func, CPL_ERROR_INCOMPATIBLE_INPUT);
14809 if (ystart < 0 || yend > ny) {
14810 cpl_error_set(func, CPL_ERROR_ACCESS_OUT_OF_RANGE);
14814 sdata = cpl_image_get_data(image);
14815 wdata = cpl_image_get_data(wavemap);
14817 sdata += ystart*nx;
14818 wdata += ystart*nx;
14821 for (y = ystart; y < yend; y++) {
14822 for (x = 0; x < nx; x++) {
14823 if (wdata[x] < wstart || wdata[x] > wend)
14868 const char *func =
"mos_load_slits_fors_mxu";
14871 char keyname[MAX_COLNAME];
14872 const char *instrume;
14873 const char *target_name;
14878 double arc2mm = 0.528;
14891 float low_limit1 = 10.0;
14892 float hig_limit2 = 30.0;
14895 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14899 if (header == NULL) {
14900 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
14909 instrume = cpl_propertylist_get_string(header,
"INSTRUME");
14912 if (instrume[4] ==
'1')
14914 if (instrume[4] ==
'2')
14918 cpl_msg_error(func,
"Wrong instrument: %s\n"
14919 "FORS2 is expected for MXU data", instrume);
14920 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14931 chip = cpl_propertylist_get_int(header,
"ESO DET CHIP1 Y");
14933 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14934 cpl_msg_error(func,
"Missing keyword ESO DET CHIP1 Y "
14936 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14940 if (chip != 1 && chip != 2) {
14941 cpl_msg_error(func,
"Unexpected chip position in keyword "
14942 "ESO DET CHIP1 Y: %d", chip);
14943 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
14959 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d YPOS", slit_id + 100);
14960 if (cpl_propertylist_has(header, keyname)) {
14961 slit_y = cpl_propertylist_get_double(header, keyname);
14964 if (slit_y < low_limit1)
14967 if (slit_y > hig_limit2)
14970 snprintf(keyname, MAX_COLNAME,
"ESO INS TARG%d NAME",
14972 if (cpl_propertylist_has(header, keyname)) {
14973 target_name = cpl_propertylist_get_string(header, keyname);
14974 if (strncmp(target_name,
"refslit", 7))
14984 if (cpl_error_get_code() != CPL_ERROR_NONE) {
14985 cpl_msg_error(func,
"%s while loading slits coordinates from "
14986 "FITS header", cpl_error_get_message());
14987 cpl_error_set_where(func);
14992 cpl_msg_error(func,
"No slits coordinates found in header");
14993 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
14997 slits = cpl_table_new(nslits);
14998 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
14999 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15000 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15001 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15002 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15003 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15004 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15005 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15006 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15013 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d YPOS", slit_id + 100);
15014 if (cpl_propertylist_has(header, keyname)) {
15015 slit_y = cpl_propertylist_get_double(header, keyname);
15018 if (slit_y < low_limit1)
15021 if (slit_y > hig_limit2)
15031 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d XPOS", slit_id + 100);
15032 slit_x = cpl_propertylist_get_double(header, keyname);
15033 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15034 cpl_table_delete(slits);
15035 cpl_msg_error(func,
"Missing keyword %s in FITS header",
15037 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15041 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d LEN", slit_id + 100);
15042 length = cpl_propertylist_get_double(header, keyname);
15043 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15044 cpl_table_delete(slits);
15045 cpl_msg_error(func,
"Missing keyword %s in FITS header",
15047 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15053 snprintf(keyname, MAX_COLNAME,
"ESO INS TARG%d NAME",
15055 if (cpl_propertylist_has(header, keyname)) {
15056 target_name = cpl_propertylist_get_string(header, keyname);
15057 if (strncmp(target_name,
"refslit", 7)) {
15058 cpl_table_set_int(slits,
"slit_id", nslits, slit_id);
15059 cpl_table_set(slits,
"xtop", nslits, slit_x);
15060 cpl_table_set(slits,
"ytop", nslits, slit_y + length/2);
15061 cpl_table_set(slits,
"xbottom", nslits, slit_x);
15062 cpl_table_set(slits,
"ybottom", nslits, slit_y - length/2);
15067 cpl_table_set_int(slits,
"slit_id", nslits, slit_id);
15068 cpl_table_set(slits,
"xtop", nslits, slit_x);
15069 cpl_table_set(slits,
"ytop", nslits, slit_y + length/2);
15070 cpl_table_set(slits,
"xbottom", nslits, slit_x);
15071 cpl_table_set(slits,
"ybottom", nslits, slit_y - length/2);
15107 int * nslits_out_det)
15109 const char *func =
"mos_load_slits_fors_mos";
15112 char keyname[MAX_COLNAME];
15113 const char *instrume;
15114 const char *chipname;
15116 int first_slit, last_slit;
15127 float ytop[19] = { 113.9, 101.3, 89.9, 77.3, 65.9, 53.3,
15128 41.9, 29.3, 17.9, 5.3, -6.1, -18.7,
15129 -30.1, -42.7, -54.1, -66.7, -78.1, -90.7,
15131 float ybottom[19] = { 102.1, 90.7, 78.1, 66.7, 54.1, 42.7,
15132 30.1, 18.7, 6.1, -5.3, -17.9, -29.3,
15133 -41.9, -53.3, -65.9, -77.3, -89.9, -101.3,
15137 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15141 if (header == NULL) {
15142 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15151 instrume = cpl_propertylist_get_string(header,
"INSTRUME");
15154 if (instrume[4] ==
'1')
15156 if (instrume[4] ==
'2')
15160 cpl_msg_error(func,
"Wrong instrument found in FITS header: %s",
15162 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15172 chipname = cpl_propertylist_get_string(header,
"ESO DET CHIP1 ID");
15174 if (chipname[0] ==
'M' || chipname[0] ==
'N')
15179 if (fors == 1 && fors_is_old) {
15191 chip = cpl_propertylist_get_int(header,
"ESO DET CHIP1 Y");
15193 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15194 cpl_msg_error(func,
"Missing keyword ESO DET CHIP1 Y "
15196 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15200 if (chip != 1 && chip != 2) {
15201 cpl_msg_error(func,
"Unexpected chip position in keyword "
15202 "ESO DET CHIP1 Y: %d", chip);
15203 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15226 for (slit_id = first_slit; slit_id <= last_slit; slit_id++) {
15227 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d POS", slit_id);
15228 if (cpl_propertylist_has(header, keyname)) {
15229 slit_x = cpl_propertylist_get_double(header, keyname);
15230 if (fabs(slit_x) < 115.0)
15233 (*nslits_out_det)++;
15236 cpl_msg_error(func,
"Missing keyword %s in FITS header", keyname);
15237 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15242 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15243 cpl_msg_error(func,
"%s while loading slits coordinates from "
15244 "FITS header", cpl_error_get_message());
15245 cpl_error_set_where(func);
15250 cpl_msg_error(func,
"No slits coordinates found in header");
15251 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
15255 slits = cpl_table_new(nslits);
15256 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
15257 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15258 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15259 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15260 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15261 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15262 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15263 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15264 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15268 for (slit_id = first_slit; slit_id <= last_slit; slit_id++) {
15269 snprintf(keyname, MAX_COLNAME,
"ESO INS MOS%d POS", slit_id);
15270 slit_x = cpl_propertylist_get_double(header, keyname);
15271 if (fabs(slit_x) < 115.0) {
15272 cpl_table_set_int(slits,
"slit_id", nslits, slit_id);
15273 cpl_table_set(slits,
"xtop", nslits, slit_x);
15274 cpl_table_set(slits,
"ytop", nslits, ytop[slit_id-1]);
15275 cpl_table_set(slits,
"xbottom", nslits, slit_x);
15276 cpl_table_set(slits,
"ybottom", nslits, ybottom[slit_id-1]);
15310 const char *func =
"mos_load_slits_fors_lss";
15314 const char *instrume;
15320 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15324 if (header == NULL) {
15325 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15334 instrume = cpl_propertylist_get_string(header,
"INSTRUME");
15337 if (instrume[4] ==
'1')
15339 if (instrume[4] ==
'2')
15343 cpl_msg_error(func,
"Wrong instrument found in FITS header: %s",
15345 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15361 chip = cpl_propertylist_get_int(header,
"ESO DET CHIP1 Y");
15363 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15364 cpl_msg_error(func,
"Missing keyword ESO DET CHIP1 Y "
15366 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15370 if (chip != 1 && chip != 2) {
15371 cpl_msg_error(func,
"Unexpected chip position in keyword "
15372 "ESO DET CHIP1 Y: %d", chip);
15373 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15388 slits = cpl_table_new(1);
15389 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
15390 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15391 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15392 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15393 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15394 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15395 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15396 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15397 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15399 slit_name = (
char *)cpl_propertylist_get_string(header,
15400 "ESO INS SLIT NAME");
15402 cpl_table_set(slits,
"ytop", 0, ytop);
15403 cpl_table_set(slits,
"ybottom", 0, ybottom);
15405 if (!strncmp(slit_name,
"lSlit0_3arcsec", 14)) {
15406 cpl_table_set_int(slits,
"slit_id", 0, 1);
15407 cpl_table_set(slits,
"xbottom", 0, -0.075);
15408 cpl_table_set(slits,
"xtop", 0, 0.075);
15410 else if (!strncmp(slit_name,
"lSlit0_4arcsec", 14)) {
15411 cpl_table_set_int(slits,
"slit_id", 0, 2);
15412 cpl_table_set(slits,
"xbottom", 0, 5.895);
15413 cpl_table_set(slits,
"xtop", 0, 6.105);
15415 else if (!strncmp(slit_name,
"lSlit0_5arcsec", 14)) {
15416 cpl_table_set_int(slits,
"slit_id", 0, 3);
15417 cpl_table_set(slits,
"xbottom", 0, -6.135);
15418 cpl_table_set(slits,
"xtop", 0, -5.865);
15420 else if (!strncmp(slit_name,
"lSlit0_7arcsec", 14)) {
15421 cpl_table_set_int(slits,
"slit_id", 0, 4);
15422 cpl_table_set(slits,
"xbottom", 0, 11.815);
15423 cpl_table_set(slits,
"xtop", 0, 12.185);
15425 else if (!strncmp(slit_name,
"lSlit1_0arcsec", 14)) {
15426 cpl_table_set_int(slits,
"slit_id", 0, 5);
15427 cpl_table_set(slits,
"xbottom", 0, -12.265);
15428 cpl_table_set(slits,
"xtop", 0, -11.735);
15430 else if (!strncmp(slit_name,
"lSlit1_3arcsec", 14)) {
15431 cpl_table_set_int(slits,
"slit_id", 0, 6);
15432 cpl_table_set(slits,
"xbottom", 0, 17.655);
15433 cpl_table_set(slits,
"xtop", 0, 18.345);
15435 else if (!strncmp(slit_name,
"lSlit1_6arcsec", 14)) {
15436 cpl_table_set_int(slits,
"slit_id", 0, 7);
15437 cpl_table_set(slits,
"xbottom", 0, -18.425);
15438 cpl_table_set(slits,
"xtop", 0, -17.575);
15440 else if (!strncmp(slit_name,
"lSlit2_0arcsec", 14)) {
15441 cpl_table_set_int(slits,
"slit_id", 0, 8);
15442 cpl_table_set(slits,
"xbottom", 0, 23.475);
15443 cpl_table_set(slits,
"xtop", 0, 24.525);
15445 else if (!strncmp(slit_name,
"lSlit2_5arcsec", 14)) {
15446 cpl_table_set_int(slits,
"slit_id", 0, 9);
15447 cpl_table_set(slits,
"xbottom", 0, -24.66);
15448 cpl_table_set(slits,
"xtop", 0, -23.34);
15451 cpl_msg_error(func,
"Invalid slit %s in keyword ESO INS SLIT NAME",
15453 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
15454 cpl_table_delete(slits);
15478 const char *func =
"mos_get_gain_vimos";
15480 double gain = -1.0;
15483 if (cpl_error_get_code() != CPL_ERROR_NONE)
15486 if (header == NULL) {
15487 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15491 gain = cpl_propertylist_get_double(header,
"ESO DET OUT1 CONAD");
15492 if (cpl_error_get_code()) {
15493 cpl_error_set_where(func);
15523 const char *func =
"mos_load_slits_vimos";
15526 char keyname[MAX_COLNAME];
15537 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15541 if (header == NULL) {
15542 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15546 nslits = cpl_propertylist_get_int(header,
"ESO INS SLIT NO");
15548 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15549 cpl_error_set_where(func);
15553 slits = cpl_table_new(nslits);
15554 cpl_table_new_column(slits,
"slit_id", CPL_TYPE_INT);
15555 cpl_table_new_column(slits,
"xtop", CPL_TYPE_DOUBLE);
15556 cpl_table_new_column(slits,
"ytop", CPL_TYPE_DOUBLE);
15557 cpl_table_new_column(slits,
"xbottom", CPL_TYPE_DOUBLE);
15558 cpl_table_new_column(slits,
"ybottom", CPL_TYPE_DOUBLE);
15559 cpl_table_new_column(slits,
"xwidth", CPL_TYPE_DOUBLE);
15560 cpl_table_new_column(slits,
"ywidth", CPL_TYPE_DOUBLE);
15561 cpl_table_new_column(slits,
"curved", CPL_TYPE_INT);
15562 cpl_table_set_column_unit(slits,
"xtop",
"pixel");
15563 cpl_table_set_column_unit(slits,
"ytop",
"pixel");
15564 cpl_table_set_column_unit(slits,
"xbottom",
"pixel");
15565 cpl_table_set_column_unit(slits,
"ybottom",
"pixel");
15566 cpl_table_set_column_unit(slits,
"xwidth",
"mm");
15567 cpl_table_set_column_unit(slits,
"ywidth",
"mm");
15569 for (i = 0; i < nslits; i++) {
15570 sprintf(keyname,
"ESO INS SLIT%d ID", i+1);
15571 slit_id = cpl_propertylist_get_int(header, keyname);
15572 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15573 cpl_error_set_where(func);
15576 sprintf(keyname,
"ESO INS SLIT%d X", i+1);
15577 slit_x = cpl_propertylist_get_double(header, keyname);
15578 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15579 cpl_error_set_where(func);
15582 sprintf(keyname,
"ESO INS SLIT%d Y", i+1);
15583 slit_y = cpl_propertylist_get_double(header, keyname);
15584 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15585 cpl_error_set_where(func);
15588 sprintf(keyname,
"ESO INS SLIT%d DIMX", i+1);
15589 dim_x = cpl_propertylist_get_double(header, keyname);
15590 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15591 cpl_error_set_where(func);
15595 sprintf(keyname,
"ESO INS SLIT%d BEZIER DY", i+1);
15596 if (cpl_propertylist_has(header, keyname)) {
15600 sprintf(keyname,
"ESO INS SLIT%d DIMY", i+1);
15603 dim_y = cpl_propertylist_get_double(header, keyname);
15604 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15605 cpl_error_set_where(func);
15609 cpl_table_set_int(slits,
"slit_id", i, slit_id);
15610 cpl_table_set(slits,
"xtop", i, slit_x - dim_x/2);
15611 cpl_table_set(slits,
"ytop", i, slit_y);
15612 cpl_table_set(slits,
"xbottom", i, slit_x + dim_x/2);
15613 cpl_table_set(slits,
"ybottom", i, slit_y);
15614 cpl_table_set(slits,
"xwidth", i, dim_x);
15615 cpl_table_set(slits,
"ywidth", i, dim_y);
15616 cpl_table_set_int(slits,
"curved", i, curved);
15634 cpl_propertylist *sort;
15636 int i, multiplex, xprev, xcur;
15638 double tolerance = 1.0;
15648 sort = cpl_propertylist_new();
15649 cpl_propertylist_append_bool(sort,
"xtop", 0);
15650 cpl_table_sort(slits, sort);
15651 cpl_propertylist_delete(sort);
15653 prev = cpl_table_get_double(slits,
"xtop", 0, NULL);
15654 cpl_table_new_column(slits,
"xind", CPL_TYPE_INT);
15655 cpl_table_set_int(slits,
"xind", 0, prev);
15656 nrow = cpl_table_get_nrow(slits);
15657 for (i = 1; i < nrow; i++) {
15658 cur = cpl_table_get_double(slits,
"xtop", i, NULL);
15659 if (fabs(prev - cur) > tolerance)
15661 cpl_table_set_int(slits,
"xind", i, prev);
15669 sort = cpl_propertylist_new();
15670 cpl_propertylist_append_bool(sort,
"xind", 0);
15671 cpl_propertylist_append_bool(sort,
"ytop", 0);
15672 cpl_table_sort(slits, sort);
15673 cpl_propertylist_delete(sort);
15680 cpl_table_new_column(slits,
"multiplex", CPL_TYPE_INT);
15681 xprev = cpl_table_get_int(slits,
"xind", 0, NULL);
15682 cpl_table_set_int(slits,
"multiplex", 0, multiplex);
15683 nrow = cpl_table_get_nrow(slits);
15684 for (i = 1; i < nrow; i++) {
15685 xcur = cpl_table_get_int(slits,
"xind", i, NULL);
15686 if (xcur == xprev) {
15693 cpl_table_set_int(slits,
"multiplex", i, multiplex);
15696 cpl_table_save(slits, NULL, NULL,
"multiplex.fits", CPL_IO_DEFAULT);
15698 cpl_table_erase_column(slits,
"xind");
15700 return 1 + cpl_table_get_column_max(slits,
"multiplex");
15732 int check_consistency)
15734 const char *func =
"mos_load_overscans_vimos";
15745 cpl_table *overscans;
15748 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15749 cpl_msg_error(func,
"Reset your error: %s", cpl_error_get_message());
15753 if (header == NULL) {
15754 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15758 if (cpl_propertylist_has(header,
"NAXIS1"))
15759 nx = cpl_propertylist_get_int(header,
"NAXIS1");
15760 if (cpl_propertylist_has(header,
"NAXIS2"))
15761 ny = cpl_propertylist_get_int(header,
"NAXIS2");
15762 if (cpl_propertylist_has(header,
"ESO DET OUT1 PRSCX"))
15763 px = cpl_propertylist_get_int(header,
"ESO DET OUT1 PRSCX");
15764 if (cpl_propertylist_has(header,
"ESO DET OUT1 PRSCY"))
15765 py = cpl_propertylist_get_int(header,
"ESO DET OUT1 PRSCY");
15766 if (cpl_propertylist_has(header,
"ESO DET OUT1 OVSCX"))
15767 ox = cpl_propertylist_get_int(header,
"ESO DET OUT1 OVSCX");
15768 if (cpl_propertylist_has(header,
"ESO DET OUT1 OVSCY"))
15769 oy = cpl_propertylist_get_int(header,
"ESO DET OUT1 OVSCY");
15770 if (cpl_propertylist_has(header,
"ESO DET OUT1 NX"))
15771 vx = cpl_propertylist_get_int(header,
"ESO DET OUT1 NX");
15772 if (cpl_propertylist_has(header,
"ESO DET OUT1 NY"))
15773 vy = cpl_propertylist_get_int(header,
"ESO DET OUT1 NY");
15775 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15776 cpl_msg_error(func,
"Missing overscan keywords in header");
15777 cpl_error_set_where(func);
15781 if (px < 0 || py < 0 || ox < 0 || oy < 0) {
15782 cpl_msg_error(func,
"Missing overscan keywords in header");
15783 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15787 if ((px + vx + ox != nx) || (py + vy + oy != ny)) {
15788 if (check_consistency) {
15789 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15793 cpl_msg_debug(func,
"Overscans description conflicts with "
15794 "reported image sizes, "
15795 "%d + %d + %d != %d or "
15796 "%d + %d + %d != %d",
15813 cpl_msg_error(func,
"Unexpected overscan regions "
15814 "(both in X and Y direction)");
15815 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
15827 overscans = cpl_table_new(nrows);
15828 cpl_table_new_column(overscans,
"xlow", CPL_TYPE_INT);
15829 cpl_table_new_column(overscans,
"ylow", CPL_TYPE_INT);
15830 cpl_table_new_column(overscans,
"xhig", CPL_TYPE_INT);
15831 cpl_table_new_column(overscans,
"yhig", CPL_TYPE_INT);
15835 cpl_table_set_int(overscans,
"xlow", nrows, px);
15836 cpl_table_set_int(overscans,
"ylow", nrows, py);
15837 cpl_table_set_int(overscans,
"xhig", nrows, nx - ox);
15838 cpl_table_set_int(overscans,
"yhig", nrows, ny - oy);
15842 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15843 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15844 cpl_table_set_int(overscans,
"xhig", nrows, px);
15845 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15850 cpl_table_set_int(overscans,
"xlow", nrows, nx - ox);
15851 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15852 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15853 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15858 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15859 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15860 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15861 cpl_table_set_int(overscans,
"yhig", nrows, py);
15866 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15867 cpl_table_set_int(overscans,
"ylow", nrows, ny - oy);
15868 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15869 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15878 cpl_table *mos_load_overscans_fors(
const cpl_propertylist *header)
15880 const char *func =
"mos_load_overscans_fors";
15891 cpl_table *overscans;
15894 if (cpl_error_get_code() != CPL_ERROR_NONE) {
15895 cpl_msg_error(func,
"Reset your error: %s", cpl_error_get_message());
15899 if (header == NULL) {
15900 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
15904 if (cpl_propertylist_has(header,
"ESO DET OUTPUTS"))
15905 nports = cpl_propertylist_get_int(header,
"ESO DET OUTPUTS");
15908 cpl_propertylist_has(header,
"ESO DET OUT1 PRSCX") &&
15909 cpl_propertylist_has(header,
"ESO DET WIN1 BINX")) {
15911 rebin = cpl_propertylist_get_int(header,
"ESO DET WIN1 BINX");
15913 overscans = cpl_table_new(3);
15914 cpl_table_new_column(overscans,
"xlow", CPL_TYPE_INT);
15915 cpl_table_new_column(overscans,
"ylow", CPL_TYPE_INT);
15916 cpl_table_new_column(overscans,
"xhig", CPL_TYPE_INT);
15917 cpl_table_new_column(overscans,
"yhig", CPL_TYPE_INT);
15925 cpl_table_set_int(overscans,
"xlow", nrows, px);
15926 cpl_table_set_int(overscans,
"ylow", nrows, py);
15927 cpl_table_set_int(overscans,
"xhig", nrows, nx - ox);
15928 cpl_table_set_int(overscans,
"yhig", nrows, ny - oy);
15931 cpl_table_set_int(overscans,
"xlow", nrows, 0);
15932 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15933 cpl_table_set_int(overscans,
"xhig", nrows, px);
15934 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15937 cpl_table_set_int(overscans,
"xlow", nrows, nx - ox);
15938 cpl_table_set_int(overscans,
"ylow", nrows, 0);
15939 cpl_table_set_int(overscans,
"xhig", nrows, nx);
15940 cpl_table_set_int(overscans,
"yhig", nrows, ny);
15985 cpl_polynomial *mos_montecarlo_polyfit(cpl_table *points, cpl_table *evaluate,
15986 int samples,
int order)
15989 const char *func =
"mos_montecarlo_polyfit";
16003 int npoints, nevaluate;
16007 if (points == NULL || evaluate == NULL) {
16008 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
16012 if (!cpl_table_has_column(points,
"x")) {
16013 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
16017 if (cpl_table_get_column_type(points,
"x") != CPL_TYPE_DOUBLE) {
16018 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
16022 if (cpl_table_has_invalid(points,
"x")) {
16023 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16027 if (!cpl_table_has_column(points,
"y")) {
16028 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
16032 if (cpl_table_get_column_type(points,
"y") != CPL_TYPE_DOUBLE) {
16033 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
16037 if (cpl_table_has_invalid(points,
"y")) {
16038 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16042 if (cpl_table_has_column(points,
"y_err")) {
16044 if (cpl_table_get_column_type(points,
"y_err") != CPL_TYPE_DOUBLE) {
16045 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
16049 if (cpl_table_has_invalid(points,
"y_err")) {
16050 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16055 if (!cpl_table_has_column(evaluate,
"x")) {
16056 cpl_error_set(func, CPL_ERROR_DATA_NOT_FOUND);
16060 if (cpl_table_get_column_type(evaluate,
"x") != CPL_TYPE_DOUBLE) {
16061 cpl_error_set(func, CPL_ERROR_INVALID_TYPE);
16065 if (cpl_table_has_invalid(evaluate,
"x")) {
16066 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16070 if (samples < 2 || order < 0) {
16071 cpl_error_set(func, CPL_ERROR_ILLEGAL_INPUT);
16075 npoints = cpl_table_get_nrow(points);
16076 listx = cpl_vector_wrap(npoints, cpl_table_get_data_double(points,
"x"));
16077 listy = cpl_vector_wrap(npoints, cpl_table_get_data_double(points,
"y"));
16079 p = cpl_polynomial_fit_1d_create(listx, listy, order, &err);
16081 if (!cpl_table_has_column(points,
"y_err")) {
16083 cpl_table_new_column(points,
"y_err", CPL_TYPE_DOUBLE);
16084 cpl_table_fill_column_window_double(points,
"y_err", 0, npoints, err);
16085 cpl_msg_info(func,
"Error column not found - set to %f\n", err);
16092 if (cpl_table_has_column(points,
"px"))
16093 cpl_table_erase_column(points,
"px");
16094 cpl_table_new_column(points,
"px", CPL_TYPE_DOUBLE);
16095 cpl_table_fill_column_window_double(points,
"px", 0, npoints, 0);
16096 x = cpl_table_get_data_double(points,
"x");
16097 px = cpl_table_get_data_double(points,
"px");
16098 for (i = 0; i < npoints; i++)
16099 px[i] = cpl_polynomial_eval_1d(p, x[i], NULL);
16101 nevaluate = cpl_table_get_nrow(evaluate);
16103 if (cpl_table_has_column(evaluate,
"px"))
16104 cpl_table_erase_column(evaluate,
"px");
16105 cpl_table_new_column(evaluate,
"px", CPL_TYPE_DOUBLE);
16106 cpl_table_fill_column_window_double(evaluate,
"px", 0, nevaluate, 0);
16107 x_eval = cpl_table_get_data_double(evaluate,
"x");
16108 px_eval = cpl_table_get_data_double(evaluate,
"px");
16109 for (i = 0; i < nevaluate; i++)
16110 px_eval[i] = cpl_polynomial_eval_1d(p, x_eval[i], NULL);
16116 if (cpl_table_has_column(evaluate,
"sigma"))
16117 cpl_table_erase_column(evaluate,
"sigma");
16118 cpl_table_new_column(evaluate,
"sigma", CPL_TYPE_DOUBLE);
16119 cpl_table_fill_column_window_double(evaluate,
"sigma", 0, nevaluate, 0);
16120 sigma = cpl_table_get_data_double(evaluate,
"sigma");
16126 if (cpl_table_has_column(points,
"vy"))
16127 cpl_table_erase_column(points,
"vy");
16128 cpl_table_new_column(points,
"vy", CPL_TYPE_DOUBLE);
16129 cpl_table_fill_column_window_double(points,
"vy", 0, npoints, 0);
16130 vy = cpl_table_get_data_double(points,
"vy");
16131 dy = cpl_table_get_data_double(points,
"y_err");
16132 cpl_vector_unwrap(listy);
16133 listy = cpl_vector_wrap(npoints, vy);
16135 for (i = 0; i < samples; i++) {
16136 for (j = 0; j < npoints; j++)
16137 vy[j] = px[j] + dy[j] * mos_randg(1);
16138 q = cpl_polynomial_fit_1d_create(listx, listy, order, NULL);
16139 for (j = 0; j < nevaluate; j++)
16140 sigma[j] += fabs(px_eval[j]
16141 - cpl_polynomial_eval_1d(q, x_eval[j], NULL));
16142 cpl_polynomial_delete(q);
16149 cpl_table_multiply_scalar(evaluate,
"sigma", 1.25);
16150 cpl_table_divide_scalar(evaluate,
"sigma", samples);
16152 cpl_vector_unwrap(listx);
16153 cpl_vector_unwrap(listy);
16183 double gain,
double bias)
16190 return cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
16192 if (ron < 0.0 || gain <= FLT_EPSILON)
16193 return cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
16195 data = cpl_image_get_data_float(image);
16196 npix = cpl_image_get_size_x(image) * cpl_image_get_size_y(image);
16199 for (i = 0; i < npix; i++) {
16200 if (data[i] < bias) {
16201 data[i] += sqrt(ron) * mos_randg(1);
16204 data[i] += sqrt(ron + (data[i] - bias) / gain) * mos_randg(1);
16208 return CPL_ERROR_NONE;
16227 cpl_image *master_flat,
16230 int nx = cpl_mask_get_size_x(refmask);
16231 int ny = cpl_mask_get_size_y(refmask);
16233 int * xpos = cpl_calloc(
sizeof(
int), ny);
16235 cpl_image * filtered = cpl_image_duplicate(master_flat);
16236 cpl_mask * kernel = cpl_mask_new(9, 3);
16237 cpl_vector * v = cpl_vector_new(ny);
16238 cpl_vector * truev;
16240 double * flats = cpl_vector_get_data(v);
16242 double median, stdev, delta;
16246 cpl_mask_not(kernel);
16247 cpl_image_filter_mask(filtered, master_flat, kernel,
16248 CPL_FILTER_MEDIAN, CPL_BORDER_COPY);
16249 cpl_mask_delete(kernel);
16251 for (i = 1; i <= ny; i++) {
16255 while (!cpl_mask_get(refmask, j, i) && j < nx);
16261 flats[nvalid] = cpl_image_get(filtered, j, i, &rejected);
16270 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
16272 truev = cpl_vector_wrap(nvalid, flats);
16274 median = cpl_vector_get_median(truev);
16277 stdev = cpl_vector_get_stdev(truev);
16279 cpl_vector_unwrap(truev);
16280 cpl_vector_delete(v);
16282 for (i = 1; i <= ny; i++) {
16283 if (xpos[i - 1] > 0) {
16285 double kappa = 1.5;
16287 delta = cpl_image_get(filtered, xpos[i - 1], i, &rejected) - median;
16290 kill = fabs(delta) > stdev * kappa;
16292 kill = delta < level;
16297 while (cpl_mask_get(refmask, xpos[i - 1] + j, i)) {
16298 cpl_mask_set(refmask, xpos[i - 1] + j, i, CPL_BINARY_0);
16305 cpl_image_delete(filtered);
16308 return cpl_error_get_code();
16320 int nx = cpl_image_get_size_x(image);
16321 int ny = cpl_image_get_size_y(image);
16322 int npix = nx * ny;
16323 float * sdata = cpl_image_get_data_float(image);
16325 int count, i, j, k;
16349 for (i = 0; i < npix; i++) {
16350 if (sdata[i] >= 65535.0) {
16352 for (j = i; j < npix; j++) {
16353 if (sdata[j] < 65535.0) {
16360 if (count < 30 && count > 2) {
16361 for (j = i; j < i + count/2; j++)
16362 sdata[j] = sdata[i] + 1000.0 * (j - i);
16363 if (count % 2 != 0) {
16364 sdata[j] = sdata[j-1] + 1000.0;
16367 for (k = j; k <= i + count; k++)
16368 sdata[k] = sdata[i] - 1000.0 * (k - i - count);
16374 return cpl_error_get_code();
16393 cpl_image_subtract(image, bimage);
16394 cpl_image_delete(bimage);
16396 return cpl_error_get_code();
16417 int nscience,
float tolerance)
16421 cpl_table *summary;
16422 int summary_nobjs = 0;
16427 int nslits = cpl_table_get_nrow(slitss[0]);
16431 int nstokes, sstokes;
16435 work = (cpl_table **)cpl_malloc(
sizeof(cpl_table *) * nscience);
16446 for (j = 0; j < nscience; j++) {
16449 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
16450 summary_nobjs += c_nobjs;
16453 summary = cpl_table_new(summary_nobjs);
16455 cpl_table_new_column(summary,
"offset", CPL_TYPE_DOUBLE);
16456 cpl_table_new_column(summary,
"pair", CPL_TYPE_INT);
16457 cpl_table_new_column(summary,
"absolute", CPL_TYPE_DOUBLE);
16458 cpl_table_new_column(summary,
"pos", CPL_TYPE_DOUBLE);
16467 for (j = 0; j < nscience; j++) {
16471 for (k = 0; k < nslits; k++) {
16474 for (m = 0; m < c_maxobjs; m++) {
16476 char *name = cpl_sprintf(
"object_%d", m + 1);
16477 double obj = cpl_table_get_double(slitss[j], name, k, &null);
16493 pos = cpl_table_get_int(slitss[j],
"position", k, &null);
16494 pair = cpl_table_get_int(slitss[j],
"pair_id", k, &null);
16495 cpl_table_set(summary,
"absolute", nobjs, obj);
16496 cpl_table_set(summary,
"pos", nobjs, pos);
16497 cpl_table_set(summary,
"offset", nobjs, obj - pos);
16498 cpl_table_set(summary,
"pair", nobjs, pair);
16528 for (k = 0; k < nslits; k+=2) {
16529 int slitmatches = 0;
16531 if (k + 1 < nslits ) {
16532 if (cpl_table_get_int(slitss[0],
"pair_id", k, NULL) !=
16533 cpl_table_get_int(slitss[0],
"pair_id", k + 1, NULL)) {
16546 for (m = 0; m < maxobjs; m++) {
16548 char *name = cpl_sprintf(
"object_%d", m + 1);
16549 double obj = cpl_table_get_double(slitss[0], name, k, &null);
16553 char *name_obj = NULL;
16554 char *name_start = NULL;
16555 char *name_end = NULL;
16556 char *name_row = NULL;
16557 char *name_row_s = NULL;
16559 char *name_start_o = NULL;
16560 char *name_end_o = NULL;
16561 char *name_row_o = NULL;
16562 char *name_start_v = NULL;
16563 char *name_end_v = NULL;
16564 char *name_obj_v = NULL;
16570 int v, start_v, end_v;
16571 double min_v, obj_v;
16586 pos = cpl_table_get_int(slitss[0],
"position", k, &null);
16587 pair = cpl_table_get_int(slitss[0],
"pair_id", k, &null);
16596 cpl_table_select_all(summary);
16598 cpl_table_and_selected_int(summary,
"pair", CPL_EQUAL_TO, pair);
16599 cpl_table_and_selected_double(summary,
"offset", CPL_LESS_THAN,
16600 obj - pos + tolerance);
16602 cpl_table_and_selected_double(summary,
"offset", CPL_GREATER_THAN,
16603 obj - pos - tolerance);
16613 if (selected != nscience * 2)
16634 name_obj = cpl_sprintf(
"object_%d", slitmatches);
16635 name_start = cpl_sprintf(
"start_%d", slitmatches);
16636 name_end = cpl_sprintf(
"end_%d", slitmatches);
16637 name_row = cpl_sprintf(
"row_%d", slitmatches);
16638 name_row_s = cpl_sprintf(
"row_stokes_%d", slitmatches);
16645 name_start_o = cpl_sprintf(
"start_%d", m + 1);
16646 name_end_o = cpl_sprintf(
"end_%d", m + 1);
16647 name_row_o = cpl_sprintf(
"row_%d", m + 1);
16653 if (!cpl_table_has_column(origslits, name_obj)) {
16654 cpl_table_new_column(origslits, name_obj, CPL_TYPE_DOUBLE);
16655 cpl_table_new_column(origslits, name_start, CPL_TYPE_INT);
16656 cpl_table_new_column(origslits, name_end, CPL_TYPE_INT);
16657 cpl_table_new_column(origslits, name_row, CPL_TYPE_INT);
16658 cpl_table_new_column(origslits, name_row_s, CPL_TYPE_INT);
16668 length = cpl_table_get_int(origslits,
"length", k + 1, &null);
16676 for (v = 0; v < maxobjs; v++) {
16677 char *name_v = cpl_sprintf(
"object_%d", v + 1);
16678 double obj_v = cpl_table_get_double(slitss[0], name_v,
16687 if (fabs(obj - length - obj_v) < min_v) {
16688 min_v = fabs(obj - length - obj_v);
16689 cpl_free(name_start_v);
16690 cpl_free(name_end_v);
16691 cpl_free(name_obj_v);
16692 name_start_v = cpl_sprintf(
"start_%d", v + 1);
16693 name_end_v = cpl_sprintf(
"end_%d", v + 1);
16694 name_obj_v = cpl_sprintf(
"object_%d", v + 1);
16698 min_v = fabs(obj - length - obj_v);
16699 name_start_v = cpl_sprintf(
"start_%d", v + 1);
16700 name_end_v = cpl_sprintf(
"end_%d", v + 1);
16701 name_obj_v = cpl_sprintf(
"object_%d", v + 1);
16710 start = cpl_table_get_int(slitss[0], name_start_o, k, &null);
16711 end = cpl_table_get_int(slitss[0], name_end_o, k, &null);
16717 start_v = cpl_table_get_int(slitss[0], name_start_v, k + 1, &null);
16718 end_v = cpl_table_get_int(slitss[0], name_end_v, k + 1, &null);
16719 obj_v = cpl_table_get_double(slitss[0], name_obj_v, k + 1, &null);
16730 cpl_table_set_double(origslits, name_obj, k, obj);
16731 cpl_table_set_double(origslits, name_obj, k + 1, obj_v);
16734 cpl_table_set_int(origslits, name_start, k, start);
16735 cpl_table_set_int(origslits, name_start, k + 1, start_v);
16738 cpl_table_set_int(origslits, name_end, k, end);
16739 cpl_table_set_int(origslits, name_end, k + 1, end_v);
16754 cpl_table_set_int(origslits, name_row, k, nmatches);
16756 cpl_table_set_int(origslits, name_row, k + 1, nmatches);
16759 cpl_free(name_obj);
16760 cpl_free(name_start);
16761 cpl_free(name_end);
16762 cpl_free(name_row);
16763 cpl_free(name_row_s);
16765 cpl_free(name_start_o);
16766 cpl_free(name_end_o);
16767 cpl_free(name_row_o);
16769 cpl_free(name_start_v); name_start_v = NULL;
16770 cpl_free(name_end_v); name_end_v = NULL;
16771 cpl_free(name_obj_v); name_obj_v = NULL;
16780 cpl_table_delete(summary);
16783 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
16796 nstokes = nmatches / 2;
16798 for (k = 0; k < nslits; k++) {
16806 for (m = 0; m < maxobjs; m++) {
16807 char *name = cpl_sprintf(
"row_%d", m + 1);
16808 char *namestokes = cpl_sprintf(
"row_stokes_%d", m + 1);
16810 if (!cpl_table_is_valid(origslits, name, k)) {
16812 cpl_free(namestokes);
16818 cpl_table_set_int(origslits, name, k, nmatches);
16819 cpl_table_set_int(origslits, namestokes, k, nstokes);
16823 cpl_free(namestokes);
16835 for (j = 0; j < maxobjs; j++) {
16836 char *name = cpl_sprintf(
"object_%d", j + 1);
16837 cpl_table_fill_invalid_double(origslits, name, -1);
16840 name = cpl_sprintf(
"start_%d", j + 1);
16841 cpl_table_fill_invalid_int(origslits, name, -1);
16844 name = cpl_sprintf(
"end_%d", j + 1);
16845 cpl_table_fill_invalid_int(origslits, name, -1);
16848 name = cpl_sprintf(
"row_%d", j + 1);
16849 cpl_table_fill_invalid_int(origslits, name, -1);
16852 name = cpl_sprintf(
"row_stokes_%d", j + 1);
16853 cpl_table_fill_invalid_int(origslits, name, -1);
16868 for (i = 0; i < nscience; i++) {
16871 work[i] = cpl_table_duplicate(slitss[i]);
16873 for (m = 0; m < c_maxobjs; m++) {
16874 char *object_o = cpl_sprintf(
"object_%d", m + 1);
16875 char *start_o = cpl_sprintf(
"start_%d", m + 1);
16876 char *end_o = cpl_sprintf(
"end_%d", m + 1);
16877 char *row_o = cpl_sprintf(
"row_%d", m + 1);
16879 cpl_table_erase_column(slitss[i], object_o);
16880 cpl_table_erase_column(slitss[i], start_o);
16881 cpl_table_erase_column(slitss[i], end_o);
16882 cpl_table_erase_column(slitss[i], row_o);
16890 for (k = 0; k < nslits; k++) {
16891 for (j = 0; j < maxobjs; j++) {
16892 double object_w, object_r;
16895 char *object_i = cpl_sprintf(
"object_%d", j + 1);
16896 char *start_i = cpl_sprintf(
"start_%d", j + 1);
16897 char *end_i = cpl_sprintf(
"end_%d", j + 1);
16898 char *row_i = cpl_sprintf(
"row_%d", j + 1);
16901 if (!cpl_table_is_valid(origslits, object_i, k))
16914 object_w = cpl_table_get_double(origslits, object_i, k, NULL);
16915 row_w = cpl_table_get_int (origslits, row_i, k, NULL);
16917 for (i = 0; i < nscience; i++) {
16920 double mindiff, diff;
16926 for (m = 0; m < c_maxobjs; m++) {
16927 object_o = cpl_sprintf(
"object_%d", m + 1);
16928 start_o = cpl_sprintf(
"start_%d", m + 1);
16929 end_o = cpl_sprintf(
"end_%d", m + 1);
16930 row_o = cpl_sprintf(
"row_%d", m + 1);
16932 if (!cpl_table_is_valid(work[i], object_o, k))
16935 object_r = cpl_table_get_double(work[i], object_o, k, NULL);
16938 diff = fabs(object_w - object_r);
16940 if (mindiff > diff) {
16950 cpl_free(object_o);
16956 object_o = cpl_sprintf(
"object_%d", minpos + 1);
16957 start_o = cpl_sprintf(
"start_%d", minpos + 1);
16958 end_o = cpl_sprintf(
"end_%d", minpos + 1);
16959 row_o = cpl_sprintf(
"row_%d", minpos + 1);
16961 if (!cpl_table_has_column(slitss[i], object_i)) {
16962 cpl_table_new_column(slitss[i], object_i, CPL_TYPE_DOUBLE);
16963 cpl_table_new_column(slitss[i], start_i, CPL_TYPE_INT);
16964 cpl_table_new_column(slitss[i], end_i, CPL_TYPE_INT);
16965 cpl_table_new_column(slitss[i], row_i, CPL_TYPE_INT);
16966 cpl_table_fill_invalid_double(slitss[i], object_i, -1);
16967 cpl_table_fill_invalid_int (slitss[i], start_i, -1);
16968 cpl_table_fill_invalid_int (slitss[i], end_i, -1);
16969 cpl_table_fill_invalid_int (slitss[i], row_i, -1);
16972 cpl_table_set_double(slitss[i], object_i, k,
16973 cpl_table_get_double(work[i], object_o,
16975 cpl_table_set_int(slitss[i], start_i , k,
16976 cpl_table_get_int(work[i], start_o, k, NULL));
16977 cpl_table_set_int(slitss[i], end_i , k,
16978 cpl_table_get_int(work[i], end_o, k, NULL));
16979 cpl_table_set_int(slitss[i], row_i , k, row_w);
16981 cpl_free(object_o);
16987 cpl_free(object_i);
16994 for (i = 0; i < nscience; i++)
16995 cpl_table_delete(work[i]);
17000 return cpl_error_get_code();
17015 char * colname = cpl_sprintf(
"object_%d", maxobjs);
17017 while (cpl_table_has_column(slits, colname)) {
17020 colname = cpl_sprintf(
"object_%d", maxobjs);
17041 int nslits = cpl_table_get_nrow(slits);
17046 for (k = 0; k < nslits; k++) {
17047 for (m = 0; m < maxobjs; m++) {
17048 char * name = cpl_sprintf(
"object_%d", m + 1);
17049 int null = !cpl_table_is_valid(slits, name, k);
17071 cpl_propertylist *sort;
17073 int nslits = cpl_table_get_nrow(slits);
17077 const float interval = 90.0 * rescale;
17078 const float offset = (90.0 - 5) * rescale;
17081 for (k = 0; k < nslits; k++) {
17082 double ytop = cpl_table_get_double(slits,
"ytop", k, &null);
17083 double ybottom = cpl_table_get_double(slits,
"ybottom", k, &null);
17085 double xtop = cpl_table_get_double(slits,
"xtop", k, &null);
17086 double xbottom = cpl_table_get_double(slits,
"xbottom", k, &null);
17088 int nmiss = (int)((ytop - ybottom) / interval + 0.5);
17091 cpl_msg_warning(cpl_func,
17092 "Some slits could not be properly detected. "
17093 "There might be accountable inaccuracies.");
17094 while (nmiss > 1) {
17095 cpl_table_set_size(slits, nslits + 1);
17100 cpl_table_set_double(slits,
"xtop", nslits, xtop);
17101 cpl_table_set_double(slits,
"xbottom", nslits, xbottom);
17105 cpl_table_set_double(slits,
"ybottom", nslits, ybottom);
17106 cpl_table_set_double(slits,
"ytop", nslits, ybottom
17108 ybottom += interval;
17109 cpl_table_set_double(slits,
"ybottom", k, ybottom);
17111 cpl_table_set_double(slits,
"ytop", nslits, ytop);
17112 cpl_table_set_double(slits,
"ybottom", nslits, ytop
17115 cpl_table_set_double(slits,
"ytop", k, ytop);
17123 sort = cpl_propertylist_new();
17124 cpl_propertylist_append_bool(sort,
"ytop", 1);
17125 cpl_table_sort(slits, sort);
17126 cpl_propertylist_delete(sort);
17133 k = cpl_table_get_nrow(slits) - 1;
17136 double ytop = cpl_table_get_double(slits,
"ytop", k, &null);
17137 double ybottom = cpl_table_get_double(slits,
"ybottom", k, &null);
17138 double length = (ytop - ybottom) / interval;
17140 if (length > 1.1) {
17141 cpl_table_set_double(slits,
"ybottom", k, ytop - offset);
17172 int * nslits_out_det)
17177 cpl_propertylist * sort;
17181 halfsize = cpl_table_get_nrow(slits);
17183 cpl_table_set_size(slits, 2 * halfsize);
17185 for (m = 0; m < halfsize; m++) {
17190 cpl_table_get(slits,
"ytop", m, &null) -
17191 cpl_table_get(slits,
"ybottom", m, &null);
17195 cpl_table_get(slits,
"ybottom", m - 1, &null) -
17196 cpl_table_get(slits,
"ytop", m, &null);
17198 gap = (interval - length) / 2;
17201 cpl_table_set(slits,
"slit_id", m + halfsize,
17202 cpl_table_get(slits,
"slit_id", m, &null) - 1);
17204 cpl_table_set(slits,
"xtop", m + halfsize,
17205 cpl_table_get(slits,
"xtop", m, &null));
17207 cpl_table_set(slits,
"xbottom", m + halfsize,
17208 cpl_table_get(slits,
"xbottom", m, &null));
17210 cpl_table_set(slits,
"ytop", m + halfsize,
17211 cpl_table_get(slits,
"ytop", m, &null) + gap + length);
17213 cpl_table_set(slits,
"ybottom", m + halfsize,
17214 cpl_table_get(slits,
"ytop", m, &null) + gap);
17217 for (m = 0; m < 2 * halfsize; m++) {
17218 cpl_table_set(slits,
"ytop", m,
17219 cpl_table_get(slits,
"ytop", m, &null) - 5.3);
17221 cpl_table_set(slits,
"ybottom", m,
17222 cpl_table_get(slits,
"ybottom", m, &null) - 5.3);
17226 sort = cpl_propertylist_new();
17227 cpl_propertylist_append_bool(sort,
"ytop", 1);
17228 cpl_table_sort(slits, sort);
17230 cpl_propertylist_delete(sort);
17235 int * fors_get_nobjs_perslit(cpl_table * slits)
17237 int nslits = cpl_table_get_nrow(slits);
17240 int * nobjs_per_slit = cpl_malloc(
sizeof(
int) * nslits);
17244 for (k = 0; k < nslits; k++) {
17246 for (m = 0; m < maxobjs; m++) {
17247 char * name = cpl_sprintf(
"object_%d", m + 1);
17248 int null = !cpl_table_is_valid(slits, name, k);
17256 nobjs_per_slit[k] = nobjs;
17259 return nobjs_per_slit;
17262 double fors_get_object_position(cpl_table *slits,
int slit,
int object)
17264 char *name = cpl_sprintf(
"object_%d",
object);
17267 position = cpl_table_get_double(slits, name, slit, NULL)
17268 - cpl_table_get_int(slits,
"position", slit, NULL);
17275 int mos_rebin_signal(cpl_image **image,
int rebin)
17277 cpl_image *rebinned;
17280 if (*image == NULL)
17286 rebinned = cpl_image_rebin(*image, 1, 1, rebin, 1);
17288 cpl_image_delete(*image);
17295 int mos_rebin_error(cpl_image **image,
int rebin)
17297 if (*image == NULL)
17303 cpl_image_power(*image, 2);
17304 mos_rebin_signal(image, rebin);
17305 cpl_image_power(*image, 0.5);
17327 int map_table(cpl_image *image,
double start,
double step,
17328 cpl_table *table,
const char *xname,
const char *yname)
17330 int length = cpl_image_get_size_x(image);
17331 int nrows = cpl_table_get_nrow(table);
17332 float *data = cpl_image_get_data_float(image);
17333 float *fdata = NULL;
17334 double *xdata = NULL;
17335 double *ydata = NULL;
17336 cpl_type xtype = cpl_table_get_column_type(table, xname);
17337 cpl_type ytype = cpl_table_get_column_type(table, yname);
17347 for (i = 0; i < length; i++)
17355 if (xtype == CPL_TYPE_FLOAT) {
17356 fdata = cpl_table_get_data_float(table, xname);
17357 xdata = cpl_malloc(nrows *
sizeof(
double));
17358 for (i = 0; i < nrows; i++) {
17359 xdata[i] = fdata[i];
17363 xdata = cpl_table_get_data_double(table, xname);
17366 if (ytype == CPL_TYPE_FLOAT) {
17367 fdata = cpl_table_get_data_float(table, yname);
17368 ydata = cpl_malloc(nrows *
sizeof(
double));
17369 for (i = 0; i < nrows; i++) {
17370 ydata[i] = fdata[i];
17374 ydata = cpl_table_get_data_double(table, yname);
17384 for (i = 0; i < length; i++) {
17385 pos = start + step * i;
17388 for (j = n; j < nrows; j++) {
17389 if (xdata[j] > pos) {
17391 data[i] = ydata[j-1]
17392 + (ydata[j] - ydata[j-1])
17393 * (pos - xdata[j-1]) / (xdata[j] - xdata[j-1]);
17399 if (xtype == CPL_TYPE_FLOAT)
17402 if (ytype == CPL_TYPE_FLOAT)
17422 static cpl_image *polysmooth(cpl_image *image,
int order,
int hw)
17429 cpl_polynomial *poly;
17430 cpl_vector *ysmooth;
17431 cpl_image *smoothed;
17436 npoints = cpl_image_get_size_x(image);
17438 if (2 * hw + 1 > npoints)
17441 x = cpl_vector_new(npoints);
17442 y = cpl_vector_new(npoints);
17443 xdata = cpl_vector_get_data(x);
17444 ydata = cpl_vector_get_data(y);
17446 smoothed = cpl_image_duplicate(image);
17447 sdata = cpl_image_get_data_float(smoothed);
17449 for (i = 0; i < npoints; i++) {
17451 ydata[i] = sdata[i];
17454 ysmooth = cpl_vector_filter_median_create(y, hw);
17455 cpl_vector_delete(y);
17457 poly = cpl_polynomial_fit_1d_create(x, ysmooth, order, NULL);
17458 cpl_vector_delete(x);
17459 cpl_vector_delete(ysmooth);
17462 for (i = 0; i < npoints; i++)
17463 sdata[i] = cpl_polynomial_eval_1d(poly, i, NULL);
17465 cpl_polynomial_delete(poly);
17468 cpl_image_delete(smoothed);
17478 cpl_image_delete(spectrum); \
17479 cpl_image_delete(flux); \
17480 cpl_image_delete(efficiency); \
17481 cpl_image_delete(smo_efficiency); \
17482 cpl_image_delete(extinction); \
17483 cpl_image_delete(response); \
17484 cpl_image_delete(smo_response); \
17485 cpl_image_delete(physical); \
17512 double dispersion,
double gain,
17513 double exptime, cpl_table *ext_table,
17514 double airmass, cpl_table *flux_table,
17518 cpl_image *spectrum = NULL;
17520 cpl_image *extinction = NULL;
17522 cpl_image *flux = NULL;
17524 cpl_image *physical = NULL;
17526 cpl_image *efficiency = NULL;
17528 cpl_image *smo_efficiency = NULL;
17529 float *smo_eff_data;
17530 cpl_image *response = NULL;
17532 cpl_image *smo_response = NULL;
17533 float *smo_res_data;
17535 cpl_image *smo_image;
17539 int ext_count, ext_pos;
17540 int eff_count, eff_pos;
17541 int flux_count, flux_pos;
17546 if (spectra == NULL || ext_table == NULL || flux_table == NULL) {
17547 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
17551 if (!cpl_table_has_column(ext_table,
"WAVE")) {
17552 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17553 "Column WAVE in atmospheric extinction table");
17557 if (!cpl_table_has_column(ext_table,
"EXTINCTION")) {
17558 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17559 "Column EXTINCTION in atmospheric extinction table");
17563 if (!cpl_table_has_column(flux_table,
"WAVE")) {
17564 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17565 "Column WAVE in standard star flux table");
17569 if (!cpl_table_has_column(flux_table,
"FLUX")) {
17570 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
17571 "Column FLUX in standard star flux table");
17576 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17577 "Invalid gain factor (%.2f)", gain);
17581 if (exptime < 0.001) {
17582 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17583 "Invalid exposure time (%.2f)", exptime);
17587 if (dispersion < 0.001) {
17588 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17589 "Invalid dispersion (%.2f)", dispersion);
17594 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
17595 "Order of the polynomial fitting the "
17596 "instrument response must be at least 2");
17600 nx = cpl_image_get_size_x(spectra);
17601 ny = cpl_image_get_size_y(spectra);
17609 spectrum = cpl_image_duplicate(spectra);
17613 cpl_image *brights = cpl_image_collapse_create(spectra, 1);
17615 cpl_image_get_maxpos(brights, &x, &y);
17616 cpl_image_delete(brights);
17617 spectrum = cpl_image_extract(spectra, 1, y, nx, y);
17625 cpl_image_multiply_scalar(spectrum, gain / exptime / dispersion);
17633 extinction = cpl_image_duplicate(spectrum);
17634 map_table(extinction, startwave + dispersion/2, dispersion,
17635 ext_table,
"WAVE",
"EXTINCTION");
17642 cpl_image_multiply_scalar(extinction, 0.4 * airmass);
17643 cpl_image_exponential(extinction, 10.);
17650 cpl_image_multiply(spectrum, extinction);
17658 ext_data = cpl_image_get_data_float(extinction);
17662 for (i = 0; i < nx; i++) {
17663 if (ext_data[i] > 0.0) {
17664 if (ext_count == 0) {
17676 cpl_image_delete(extinction); extinction = NULL;
17684 flux = cpl_image_duplicate(spectrum);
17685 map_table(flux, startwave + dispersion/2, dispersion,
17686 flux_table,
"WAVE",
"FLUX");
17694 flux_data = cpl_image_get_data_float(flux);
17698 for (i = 0; i < nx; i++) {
17699 if (flux_data[i] > 0.0) {
17700 if (flux_count == 0) {
17717 start = ext_pos > flux_pos ? ext_pos : flux_pos;
17718 end = (ext_pos + ext_count) < (flux_pos + flux_count) ?
17719 (ext_pos + ext_count) : (flux_pos + flux_count);
17721 flux_count = end - start;
17732 physical = cpl_image_duplicate(spectrum);
17733 phys_data = cpl_image_get_data_float(physical);
17735 for (i = 0; i < nx; i++) {
17736 lambda = startwave + dispersion * (i + 0.5);
17737 phys_data[i] = 0.0026 * lambda * flux_data[i];
17740 efficiency = cpl_image_duplicate(spectrum);
17741 eff_data = cpl_image_get_data_float(efficiency);
17742 data = cpl_image_get_data_float(spectrum);
17744 for (i = 0; i < nx; i++) {
17745 if (phys_data[i] > 0.0)
17746 eff_data[i] = data[i] / phys_data[i];
17751 cpl_image_delete(physical); physical = NULL;
17761 for (i = 0; i < nx; i++) {
17762 if (eff_data[i] > 0.01) {
17763 if (eff_count == 0) {
17769 if (eff_count > 300) {
17780 start = eff_pos > flux_pos ? eff_pos : flux_pos;
17781 end = (eff_pos + eff_count) < (flux_pos + flux_count) ?
17782 (eff_pos + eff_count) : (flux_pos + flux_count);
17784 eff_count = end - start;
17786 if (eff_count < 1) {
17787 cpl_error_set_message(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
17788 "No overlap between catalog and spectrum");
17798 image = cpl_image_extract(efficiency, eff_pos + 1, 1,
17799 eff_pos + eff_count, 1);
17801 smo_image = polysmooth(image, order, 50);
17802 cpl_image_delete(image);
17804 smo_efficiency = cpl_image_duplicate(efficiency);
17805 smo_eff_data = cpl_image_get_data_float(smo_efficiency);
17806 cpl_image_copy(smo_efficiency, smo_image, eff_pos + 1, 1);
17808 cpl_image_delete(smo_image);
17819 response = cpl_image_duplicate(spectrum);
17820 res_data = cpl_image_get_data_float(response);
17822 for (i = 0; i < nx; i++) {
17823 if (eff_data[i] > 0.01 && flux_data[i] > 0.0)
17824 res_data[i] = data[i] / flux_data[i];
17834 image = cpl_image_extract(response, eff_pos + 1, 1, eff_pos + eff_count, 1);
17836 smo_image = polysmooth(image, order, 50);
17837 cpl_image_delete(image);
17839 smo_response = cpl_image_duplicate(response);
17840 smo_res_data = cpl_image_get_data_float(smo_response);
17841 cpl_image_copy(smo_response, smo_image, eff_pos + 1, 1);
17843 cpl_image_delete(smo_image);
17845 for (i = 0; i < nx; i++) {
17846 if (eff_data[i] > 0.01) {
17847 res_data[i] = 1 / res_data[i];
17848 smo_res_data[i] = 1 / smo_res_data[i];
17852 smo_res_data[i] = 0.0;
17861 table = cpl_table_new(nx);
17863 cpl_table_new_column(table,
"WAVE", CPL_TYPE_FLOAT);
17864 cpl_table_set_column_unit(table,
"WAVE",
"Angstrom");
17866 for (i = 0; i < nx; i++)
17867 cpl_table_set_float(table,
"WAVE", i, startwave + dispersion*(i+0.5));
17869 cpl_table_new_column(table,
"STD_FLUX", CPL_TYPE_FLOAT);
17870 cpl_table_set_column_unit(table,
"STD_FLUX",
17871 "10^(-16) erg/(cm^2 s Angstrom)");
17872 cpl_table_copy_data_float(table,
"STD_FLUX", flux_data);
17873 cpl_image_delete(flux); flux = NULL;
17875 cpl_table_new_column(table,
"OBS_FLUX", CPL_TYPE_FLOAT);
17876 cpl_table_set_column_unit(table,
"OBS_FLUX",
"electron/(s Angstrom)");
17877 cpl_table_copy_data_float(table,
"OBS_FLUX", data);
17878 cpl_image_delete(spectrum); spectrum = NULL;
17880 cpl_table_new_column(table,
"RAW_EFFICIENCY", CPL_TYPE_FLOAT);
17881 cpl_table_set_column_unit(table,
"RAW_EFFICIENCY",
"electron/photon");
17882 cpl_table_copy_data_float(table,
"RAW_EFFICIENCY", eff_data);
17883 cpl_image_delete(efficiency); efficiency = NULL;
17885 cpl_table_new_column(table,
"EFFICIENCY", CPL_TYPE_FLOAT);
17886 cpl_table_set_column_unit(table,
"EFFICIENCY",
"electron/photon");
17887 cpl_table_copy_data_float(table,
"EFFICIENCY", smo_eff_data);
17888 cpl_image_delete(smo_efficiency); smo_efficiency = NULL;
17890 cpl_table_new_column(table,
"RAW_RESPONSE", CPL_TYPE_FLOAT);
17891 cpl_table_set_column_unit(table,
"RAW_RESPONSE",
17892 "10^(-16) erg/(cm^2 electron)");
17893 cpl_table_copy_data_float(table,
"RAW_RESPONSE", res_data);
17894 cpl_image_delete(response); response = NULL;
17896 cpl_table_new_column(table,
"RESPONSE", CPL_TYPE_FLOAT);
17897 cpl_table_set_column_unit(table,
17898 "RESPONSE",
"10^(-16) erg/(cm^2 electron)");
17899 cpl_table_copy_data_float(table,
"RESPONSE", smo_res_data);
17900 cpl_image_delete(smo_response); smo_response = NULL;
17907 static double ksigma_vector(cpl_vector *values,
17908 double klow,
double khigh,
int kiter,
int *good)
17910 cpl_vector *accepted;
17912 double sigma = 0.0;
17913 double *data = cpl_vector_get_data(values);
17914 int n = cpl_vector_get_size(values);
17925 mean = cpl_vector_get_median(values);
17927 for (i = 0; i < n; i++)
17928 sigma += (mean - data[i]) * (mean - data[i]);
17930 sigma = sqrt(sigma / (n - 1));
17934 for (i = 0; i < ngood; i++) {
17935 if (data[i]-mean < khigh*sigma && mean-data[i] < klow*sigma) {
17936 data[count] = data[i];
17950 accepted = cpl_vector_wrap(count, data);
17951 mean = cpl_vector_get_mean(accepted);
17953 sigma = cpl_vector_get_stdev(accepted);
17954 cpl_vector_unwrap(accepted);
17956 if (count == ngood || count == 1)
17989 double klow,
double khigh,
int kiter,
17992 int ni, nx, ny, npix;
17993 cpl_image *out_ima;
17998 cpl_vector *time_line;
17999 double *ptime_line;
18004 ni = cpl_imagelist_get_size(imlist);
18006 image = cpl_imagelist_get(imlist, 0);
18007 nx = cpl_image_get_size_x(image);
18008 ny = cpl_image_get_size_y(image);
18011 out_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
18012 pout_ima = cpl_image_get_data_float(out_ima);
18015 *good = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
18016 good_ima = cpl_image_get_data_float(*good);
18019 time_line = cpl_vector_new(ni);
18020 ptime_line = cpl_vector_get_data(time_line);
18022 data = cpl_calloc(
sizeof(
float *), ni);
18024 for (i = 0; i < ni; i++) {
18025 image = cpl_imagelist_get(imlist, i);
18026 data[i] = cpl_image_get_data_float(image);
18029 for (i = 0; i < npix; i++) {
18030 for (j = 0; j < ni; j++) {
18031 ptime_line[j] = data[j][i];
18033 pout_ima[i] = ksigma_vector(time_line, klow, khigh, kiter, &ngood);
18035 good_ima[i] = ngood;
18040 cpl_vector_delete(time_line);
18064 cpl_table *ext_table,
double startwave,
18065 double dispersion,
double gain,
18066 double exptime,
double airmass)
18068 cpl_image *extinction;
18069 cpl_image *outspectra;
18070 cpl_image *mapresponse;
18074 int tlength, xlength, ylength;
18076 double resp_startwave;
18077 double resp_endwave;
18081 if (spectra == NULL || ext_table == NULL || response == NULL) {
18082 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
18090 if(cpl_table_has_column(response,
"RESPONSE"))
18091 cpl_table_cast_column(response,
"RESPONSE",
"RESPONSE_F", CPL_TYPE_FLOAT);
18092 else if(cpl_table_has_column(response,
"RESPONSE_FFSED"))
18093 cpl_table_cast_column(response,
"RESPONSE_FFSED",
"RESPONSE_F", CPL_TYPE_FLOAT);
18097 res_data = cpl_table_get_data_float(response,
"RESPONSE_F");
18099 if (res_data == NULL) {
18100 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
18104 tlength = cpl_table_get_nrow(response);
18105 xlength = cpl_image_get_size_x(spectra);
18106 ylength = cpl_image_get_size_y(spectra);
18109 mapresponse = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18110 map_table(mapresponse, startwave + dispersion/2, dispersion,
18111 response,
"WAVE",
"RESPONSE_F");
18112 res_data = cpl_image_get_data_float(mapresponse);
18119 extinction = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18120 map_table(extinction, startwave + dispersion/2, dispersion,
18121 ext_table,
"WAVE",
"EXTINCTION");
18128 cpl_image_multiply_scalar(extinction, 0.4 * airmass);
18129 cpl_image_exponential(extinction, 10.);
18131 outspectra = cpl_image_duplicate(spectra);
18133 ext_data = cpl_image_get_data_float(extinction);
18134 out_data = cpl_image_get_data_float(outspectra);
18136 for (k = 0, i = 0; i < ylength; i++) {
18137 for (j = 0; j < xlength; j++, k++)
18138 out_data[k] *= ext_data[j] * res_data[j];
18141 cpl_image_delete(extinction);
18142 cpl_image_delete(mapresponse);
18144 cpl_image_multiply_scalar(outspectra, gain / exptime / dispersion);
18149 resp_startwave = cpl_table_get(response,
"WAVE", 0, &null);
18150 resp_endwave = cpl_table_get(response,
"WAVE",
18151 cpl_table_get_nrow(response) -1, &null);
18152 for (j = 0; j < xlength; j++) {
18153 double this_wave = startwave + j * dispersion;
18154 if(this_wave < resp_startwave ||this_wave > resp_endwave)
18156 for (i = 0; i < ylength; i++)
18157 out_data[j + xlength * i] = -1;
18161 cpl_table_erase_column(response,
"RESPONSE_F");
18185 cpl_table *response,
18186 cpl_table *ext_table,
18188 double dispersion,
double gain,
18189 double exptime,
double airmass)
18191 cpl_image *extinction;
18192 cpl_image *outerrors;
18193 cpl_image *mapresponse;
18194 cpl_image *maperror;
18200 int tlength, xlength, ylength;
18204 if (errors == NULL || ext_table == NULL || response == NULL) {
18205 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
18209 if (!cpl_table_has_column(response,
"ERROR")) {
18211 dispersion, gain, exptime, airmass);
18214 cpl_table_cast_column(response,
"RESPONSE",
"RESPONSE_F", CPL_TYPE_FLOAT);
18215 res_data = cpl_table_get_data_float(response,
"RESPONSE_F");
18217 if (res_data == NULL) {
18218 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
18222 err_data = cpl_table_get_data_float(response,
"ERROR");
18224 if (err_data == NULL) {
18225 cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
18229 tlength = cpl_table_get_nrow(response);
18230 xlength = cpl_image_get_size_x(errors);
18231 ylength = cpl_image_get_size_y(errors);
18233 if (xlength != tlength) {
18234 mapresponse = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18235 map_table(mapresponse, startwave + dispersion/2, dispersion,
18236 response,
"WAVE",
"RESPONSE_F");
18237 res_data = cpl_image_get_data_float(mapresponse);
18239 maperror = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18240 map_table(maperror, startwave + dispersion/2, dispersion,
18241 response,
"WAVE",
"ERROR");
18242 err_data = cpl_image_get_data_float(maperror);
18250 extinction = cpl_image_new(xlength, 1, CPL_TYPE_FLOAT);
18251 map_table(extinction, startwave + dispersion/2, dispersion,
18252 ext_table,
"WAVE",
"EXTINCTION");
18259 cpl_image_multiply_scalar(extinction, 0.4 * airmass);
18260 cpl_image_exponential(extinction, 10.);
18262 outerrors = cpl_image_duplicate(errors);
18264 ext_data = cpl_image_get_data_float(extinction);
18265 out_data = cpl_image_get_data_float(outerrors);
18266 spe_data = cpl_image_get_data_float(spectra);
18268 for (k = 0, i = 0; i < ylength; i++) {
18269 for (j = 0; j < xlength; j++, k++) {
18270 out_data[k] = ext_data[j] *
18271 sqrt(err_data[j] * err_data[j] * spe_data[k] * spe_data[k] +
18272 res_data[j] * res_data[j] * out_data[k] * out_data[k]);
18276 cpl_image_delete(extinction);
18277 if (xlength != tlength) {
18278 cpl_image_delete(maperror);
18281 cpl_image_multiply_scalar(outerrors, gain / exptime / dispersion);
18283 cpl_table_erase_column(response,
"RESPONSE_F");
18364 cpl_image *u_image, cpl_image *u_error,
18365 double startwave,
double dispersion,
18366 double band, cpl_table *pol_sta,
18367 double ra,
double dec,
char *filter,
18369 double *p_offset,
double *p_error,
18370 double *a_offset,
double *a_error)
18372 cpl_table *standard;
18373 cpl_image *q_noise;
18374 cpl_image *q_signal;
18375 cpl_image *u_noise;
18376 cpl_image *u_signal;
18382 double arctol = 0.5;
18385 double bwave[] = {3650., 4450., 5510., 6580., 8060};
18386 char *bands =
"UBVRI";
18387 char p_label[] = {
' ',
'p',
'\0'};
18388 char dp_label[] = {
' ',
'd',
'p',
'\0'};
18389 char a_label[] = {
' ',
'a',
'\0'};
18390 char da_label[] = {
' ',
'd',
'a',
'\0'};
18391 int nbands = strlen(bands);
18393 int first, last, count, center;
18396 int i, found, closest;
18424 cpl_table_select_all(pol_sta);
18425 cpl_table_and_selected_double(pol_sta,
"RA", CPL_GREATER_THAN, ra-arctol);
18426 cpl_table_and_selected_double(pol_sta,
"RA", CPL_LESS_THAN, ra+arctol);
18427 cpl_table_and_selected_double(pol_sta,
"DEC", CPL_GREATER_THAN, dec-arctol);
18429 cpl_table_and_selected_double(pol_sta,
"DEC", CPL_LESS_THAN, dec+arctol);
18431 if (selected == 0) {
18432 cpl_msg_warning(cpl_func,
"No standard star found in FOV");
18436 if (selected > 1) {
18437 cpl_msg_warning(cpl_func,
18438 "Ambiguity: %d standard stars found in FOV", selected);
18442 standard = cpl_table_extract_selected(pol_sta);
18444 cpl_msg_info(cpl_func,
"Standard star: %s",
18445 cpl_table_get_string(standard,
"name", 0));
18451 polarised = cpl_table_get_int(standard,
"polarised", 0, NULL);
18453 cpl_msg_info(cpl_func,
"This star is%sexpected to be polarised",
18454 polarised ?
" " :
" not ");
18464 nx = cpl_image_get_size_x(q_error);
18466 noise = cpl_image_collapse_median_create(q_error, 1, 0, 0);
18467 cpl_image_get_minpos(noise, &col, &row);
18469 cpl_image_delete(noise);
18472 cpl_table_delete(standard);
18473 cpl_msg_error(cpl_func,
18474 "Assertion failure!!! col = %"CPL_SIZE_FORMAT
" (it should be 1)", col);
18478 q_signal = cpl_image_extract(q_image, 1, row, nx, row);
18479 q_noise = cpl_image_extract(q_error, 1, row, nx, row);
18480 u_signal = cpl_image_extract(u_image, 1, row, nx, row);
18481 u_noise = cpl_image_extract(u_error, 1, row, nx, row);
18483 q_sdata = cpl_image_get_data_double(q_signal);
18484 q_ndata = cpl_image_get_data_double(q_noise);
18485 u_sdata = cpl_image_get_data_double(u_signal);
18486 u_ndata = cpl_image_get_data_double(u_noise);
18494 last = nx = cpl_image_get_size_x(q_signal);
18495 for (i = 0; i < nx; i++) {
18497 if (q_ndata[i] > 0.0) {
18502 if (q_ndata[i] <= 0.0) {
18509 count = last - first + 1;
18511 if (first < 0 || count < band) {
18512 cpl_table_delete(standard);
18513 cpl_image_delete(q_signal);
18514 cpl_image_delete(q_noise);
18515 cpl_image_delete(u_signal);
18516 cpl_image_delete(u_noise);
18517 cpl_msg_warning(cpl_func,
"Too short spectrum (%d pixels)", count);
18521 center = (first + last) / 2;
18522 cwave = startwave + dispersion * center;
18530 for (i = 0; i < nbands; i++) {
18531 p_label[0] = bands[i];
18532 if (cpl_table_is_valid(standard, p_label, 0)) {
18535 mindist = fabs(bwave[i] - cwave);
18538 else if (mindist > fabs(bwave[i] - cwave)) {
18539 mindist = fabs(bwave[i] - cwave);
18546 cpl_table_delete(standard);
18547 cpl_image_delete(q_signal);
18548 cpl_image_delete(q_noise);
18549 cpl_image_delete(u_signal);
18550 cpl_image_delete(u_noise);
18551 cpl_msg_warning(cpl_func,
"No reference value available");
18555 center = (bwave[closest] - startwave) / dispersion;
18556 cwave = bwave[closest];
18564 pband = floor(band / dispersion);
18566 if (center - pband/2 < first || center + pband/2 > last) {
18567 cpl_table_delete(standard);
18568 cpl_image_delete(q_signal);
18569 cpl_image_delete(q_noise);
18570 cpl_image_delete(u_signal);
18571 cpl_image_delete(u_noise);
18572 cpl_msg_warning(cpl_func,
"No reference value available");
18576 first = center - pband/2;
18577 last = center + pband/2;
18584 p_label[0] = bands[closest];
18585 dp_label[0] = bands[closest];
18586 a_label[0] = bands[closest];
18587 da_label[0] = bands[closest];
18589 p_ref = cpl_table_get(standard, p_label, 0, NULL);
18590 dp_ref = cpl_table_get(standard, dp_label, 0, NULL);
18591 a_ref = cpl_table_get(standard, a_label, 0, NULL);
18592 da_ref = cpl_table_get(standard, da_label, 0, NULL);
18594 cpl_msg_info(cpl_func,
18595 "The expected polarisation is %.2f +- %.2f %%",
18599 cpl_msg_info(cpl_func,
18600 "The expected polarisation angle is %.2f +- %.2f degrees",
18608 q_obs = cpl_image_get_median_window(q_image, first, 1, last, 1);
18609 q_err = cpl_image_get_median_window(q_error, first, 1, last, 1);
18610 u_obs = cpl_image_get_median_window(u_image, first, 1, last, 1);
18611 u_err = cpl_image_get_median_window(u_error, first, 1, last, 1);
18617 p_obs = sqrt(q_obs * q_obs + u_obs * u_obs);
18618 p_err = CPL_MATH_SQRT1_2 * 0.5 * (q_err + u_err);
18626 if (fabs(q_obs) < 0.00001) {
18635 a_obs = 0.5 * atan(u_obs / q_obs) * 180 / CPL_MATH_PI;
18653 a_err = sqrt(q_obs*q_obs*u_err*u_err + u_obs*u_obs*q_err*q_err)
18655 * 90 / CPL_MATH_PI;
18660 cpl_msg_info(cpl_func,
18661 "The measured polarisation is %.2f +- %.2f %%",
18665 cpl_msg_info(cpl_func,
18666 "The measured polarisation angle is %.2f +- %.2f degrees",
18670 *filter = bands[closest];
18671 *polarisation = polarised;
18674 *p_offset = (p_obs - p_ref) / p_ref;
18675 *p_error = sqrt(p_err * p_err + dp_ref * dp_ref) / p_ref;
18678 *p_offset = p_obs - p_ref;
18679 *p_error = sqrt(p_err * p_err + dp_ref * dp_ref);
18682 *a_offset = a_obs - a_ref;
18683 *a_error = sqrt(a_err*a_err + da_ref*da_ref);
18723 cpl_array *offsets;
18725 int nslits = cpl_table_get_nrow(reference);
18732 cpl_error_code status = CPL_ERROR_NONE;
18737 if (objects == NULL)
18738 return CPL_ERROR_NULL_INPUT;
18740 if (nslits != cpl_table_get_nrow(objects))
18741 return CPL_ERROR_INCOMPATIBLE_INPUT;
18743 nref = fors_get_nobjs_perslit(reference);
18744 nobj = fors_get_nobjs_perslit(objects);
18747 for (i = 0; i < nslits; i++)
18748 noffset += nobj[i];
18750 if (noffset == 0) {
18753 return CPL_ERROR_DATA_NOT_FOUND;
18757 for (i = 0; i < nslits; i++)
18758 noffset += nref[i];
18760 if (noffset == 0) {
18763 return CPL_ERROR_DATA_NOT_FOUND;
18766 offsets = cpl_array_new(noffset, CPL_TYPE_DOUBLE);
18770 for (i = 0; i < nslits; i++) {
18771 if (nref[i] > 0 && nobj[i] > 0) {
18773 int length = cpl_table_get_int(objects,
"length", i, NULL);
18774 double ytop = cpl_table_get_double(objects,
"xtop", i, NULL);
18775 double ybottom = cpl_table_get_double(objects,
"xbottom", i, NULL);
18776 int *aref = cpl_calloc(length,
sizeof(
int));
18777 int *aobj = cpl_calloc(length,
sizeof(
int));
18778 float *pref = cpl_calloc(nref[i],
sizeof(
float));
18779 float *pobj = cpl_calloc(nobj[i],
sizeof(
float));
18781 for (j = 0; j < nref[i]; j++) {
18782 pref[j] = fors_get_object_position(reference, i, j + 1);
18783 aref[(int)pref[j]] = 1;
18786 for (j = 0; j < nobj[i]; j++) {
18787 pobj[j] = fors_get_object_position(objects, i, j + 1);
18788 aobj[(int)pobj[j]] = 1;
18796 aref[length - 1] = 0;
18798 aobj[length - 1] = 0;
18818 best_shift = length;
18820 for (shift = length/2, j = 0; j <= length; shift--, j++) {
18821 int rstart, ostart, count;
18826 count = length - shift;
18831 count = length + shift;
18835 for (k = 0; k < count; k++) {
18836 corr += aref[rstart + k] * aobj[ostart + k];
18839 if (maxcorr < corr) {
18841 best_shift = shift;
18845 if (best_shift == length) {
18855 for (j = 0; j < nref[i]; j++) {
18856 for (k = 0; k < nobj[i]; k++) {
18857 if (fabs(pref[j] - pobj[k] - best_shift) < 2) {
18858 double ccd_offset = (pref[j] - pobj[k])
18868 cpl_array_set(offsets, noffset, ccd_offset);
18890 *offset = cpl_array_get_median(offsets);
18893 double *a = cpl_malloc(
sizeof(
double) * noffset);
18894 for (i = 0; i < noffset; i++) {
18895 a[i] = cpl_array_get_double(offsets, i, NULL);
18903 status = CPL_ERROR_DATA_NOT_FOUND;
18906 cpl_array_delete(offsets);
18927 int nx = cpl_image_get_size_x(image);
18928 int ny = cpl_image_get_size_y(image);
18932 double xpos, ypos, xfrac, yfrac;
18936 if (fabs(dx) >= nx || fabs(dy) >= ny)
18937 return CPL_ERROR_ACCESS_OUT_OF_RANGE;
18939 source = cpl_image_duplicate(image);
18940 idata = cpl_image_get_data_float(image);
18941 sdata = cpl_image_get_data_float(source);
18947 yfrac = - dy - floor(- dy);
18948 xfrac = - dx - floor(- dx);
18950 for (pos = 0, j = 0; j < ny; j++) {
18952 yint = floor(ypos);
18953 for (i = 0; i < nx; i++) {
18955 xint = floor(xpos);
18956 if (xint < 0 || yint < 0 || xint > nx - 2 || yint > ny - 2) {
18960 idata[pos] = sdata[xint + nx*yint] * (1 - xfrac) * (1 - yfrac)
18961 + sdata[xint + 1 + nx*yint] * xfrac * (1 - yfrac)
18962 + sdata[xint + nx*(yint + 1)] * (1 - xfrac) * yfrac
18963 + sdata[xint + 1 + nx*(yint + 1)] * xfrac * yfrac;
18969 cpl_image_delete(source);
18971 return CPL_ERROR_NONE;
18987 #ifdef CPL_SIZE_FORMAT
18993 cpl_table_duplicate_column(slits,
"x", slits,
"xtop");
18994 cpl_table_add_columns(slits,
"x",
"xbottom");
18995 cpl_table_divide_scalar(slits,
"x", 2);
18996 cpl_table_subtract_scalar(slits,
"x", nx/2);
18997 cpl_table_multiply_columns(slits,
"x",
"x");
18999 cpl_table_duplicate_column(slits,
"y", slits,
"ytop");
19000 cpl_table_add_columns(slits,
"y",
"ybottom");
19001 cpl_table_divide_scalar(slits,
"y", 2);
19002 cpl_table_subtract_scalar(slits,
"y", ny/2);
19003 cpl_table_multiply_columns(slits,
"y",
"y");
19005 cpl_table_add_columns(slits,
"x",
"y");
19006 cpl_table_get_column_minpos(slits,
"x", &row);
19008 cpl_table_erase_column(slits,
"x");
19009 cpl_table_erase_column(slits,
"y");
19034 double xwidth,
double ywidth,
19035 int dx,
double gain,
double *o_flux,
double *o_err)
19037 int nx = cpl_image_get_size_x(image);
19038 int ny = cpl_image_get_size_y(image);
19040 int ytop = (int)cpl_table_get(slits,
"ytop", slit, NULL);
19041 int ybottom = (int)cpl_table_get(slits,
"ybottom", slit, NULL);
19042 int dy = ytop - ybottom;
19043 int xcenter = (int)((cpl_table_get(slits,
"xtop", slit, NULL) +
19044 cpl_table_get(slits,
"xbottom", slit, NULL)) / 2);
19045 int xleft = xcenter - dx;
19046 int xright = xcenter + dx + 1;
19047 double area = xwidth * ywidth;
19048 int npix = (2*dx + 1) * dy;
19050 float *data = cpl_image_get_data_float(image);
19052 double error = 0.0;
19057 if (cpl_table_has_column(slits,
"ywidth")) {
19058 area = cpl_table_get(slits,
"xwidth", slit, NULL)
19059 * cpl_table_get(slits,
"ywidth", slit, NULL);
19089 count = (xright - xleft) * (ytop - ybottom);
19092 return CPL_ERROR_ACCESS_OUT_OF_RANGE;
19096 for (y = ybottom; y < ytop; y++) {
19097 for (x = xleft; x < xright; x++) {
19098 double value = data[x + y * nx];
19099 if (value < satur) {
19107 return CPL_ERROR_DIVISION_BY_ZERO;
19109 error = sqrt(flux/gain);
19115 flux *= (float)npix / count;
19116 error *= (float)npix / count;
19124 return CPL_ERROR_NONE;
19151 double xwidth,
double ywidth,
19152 double lambda,
double startwave,
19153 double dispersion,
int dx,
double gain,
19154 double *o_flux,
double *o_err)
19156 int nx = cpl_image_get_size_x(image);
19157 int ny = cpl_image_get_size_y(image);
19159 int dy = (int)cpl_table_get(slits,
"length", slit, NULL);
19160 int ybottom = (int)cpl_table_get(slits,
"position", slit, NULL);
19161 int ytop = ybottom + dy;
19162 int xcenter = (int)floor((lambda - startwave) / dispersion + 0.5);
19163 int xleft = xcenter - dx;
19164 int xright = xcenter + dx + 1;
19165 double area = xwidth * ywidth;
19166 int npix = (2*dx + 1) * dy;
19168 float *data = cpl_image_get_data_float(image);
19170 double error = 0.0;
19175 if (cpl_table_has_column(slits,
"ywidth")) {
19176 area = cpl_table_get(slits,
"xwidth", slit, NULL)
19177 * cpl_table_get(slits,
"ywidth", slit, NULL);
19207 count = (xright - xleft) * (ytop - ybottom);
19210 return CPL_ERROR_ACCESS_OUT_OF_RANGE;
19214 for (y = ybottom; y < ytop; y++) {
19215 for (x = xleft; x < xright; x++) {
19216 double value = data[x + y * nx];
19217 if (value < satur) {
19225 return CPL_ERROR_DIVISION_BY_ZERO;
19227 error = sqrt(flux/gain);
19233 flux *= (float)npix / count;
19234 error *= (float)npix / count;
19242 return CPL_ERROR_NONE;
19261 char *label,
double *mvalue)
19263 int position = cpl_table_get_int(slits,
"position", slit, NULL);
19264 int length = cpl_table_get_int(slits,
"length", slit, NULL);
19265 cpl_table *tmp = cpl_table_extract(table, position, length);
19267 *mvalue = cpl_table_get_column_median(tmp, label);
19268 cpl_table_delete(tmp);
19270 if (cpl_error_get_code() != CPL_ERROR_NONE)
19290 cpl_mask *kernel = cpl_mask_new(nx, ny);
19291 cpl_image *filtered = cpl_image_new(cpl_image_get_size_x(image),
19292 cpl_image_get_size_y(image),
19293 cpl_image_get_type(image));
19295 cpl_mask_not(kernel);
19296 cpl_image_filter_mask(filtered, image, kernel,
19297 CPL_FILTER_MEDIAN, CPL_BORDER_FILTER);
19298 cpl_mask_delete(kernel);
19303 int fors_mos_is_lss_like(cpl_table *maskslits,
int nslits_out_det)
19305 int treat_as_lss = 1;
19306 double mxpos = cpl_table_get_column_median(maskslits,
"xtop");
19307 double * slit_xpos = cpl_table_get_data_double(maskslits,
"xtop");
19308 cpl_size nslits = cpl_table_get_nrow(maskslits);
19312 if(nslits_out_det != 0)
19315 for (cpl_size i = 0; i < nslits; i++) {
19316 if (fabs(mxpos-slit_xpos[i]) > 0.01) {
19321 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.