00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #ifdef HAVE_CONFIG_H
00071 # include <config.h>
00072 #endif
00073
00074
00078
00081
00082
00083
00084
00085 #include <uves_wavecal_utils.h>
00086 #include <uves_utils.h>
00087 #include <uves_utils_wrappers.h>
00088 #include <uves_error.h>
00089 #include <uves_msg.h>
00090
00091 #include <cpl.h>
00092
00093
00094
00095
00096
00097
00098
00099
00100
00108
00109
00110 lt_type *uves_lt_new(int windows, int traces)
00111 {
00112
00113 lt_type *lt = cpl_malloc(sizeof(lt_type));
00114
00115 assure_mem( lt );
00116
00117 lt->windows = windows;
00118 lt->traces = traces;
00119
00120
00121 lt->table = cpl_calloc(windows*traces, sizeof(cpl_table *));
00122 lt->dispersion_relation = cpl_calloc(windows*traces, sizeof(polynomial *));
00123 lt->absolute_order = cpl_calloc(windows*traces, sizeof(polynomial *));
00124 lt->first_absolute_order = cpl_calloc(windows*traces, sizeof(int));
00125 lt->last_absolute_order = cpl_calloc(windows*traces, sizeof(int));
00126
00127 cleanup:
00128 return lt;
00129 }
00130
00131
00136
00137
00138 void uves_lt_delete(lt_type **lt)
00139 {
00140 if (lt != NULL && *lt != NULL)
00141 {
00142 int i;
00143 for (i = 0; i < (*lt)->windows * (*lt)->traces; i++)
00144 {
00145 uves_free_table (&((*lt)->table[i]));
00146 uves_polynomial_delete(&((*lt)->dispersion_relation[i]));
00147 uves_polynomial_delete(&((*lt)->absolute_order[i]));
00148 }
00149
00150 cpl_free((*lt)->table);
00151 cpl_free((*lt)->dispersion_relation);
00152 cpl_free((*lt)->absolute_order);
00153 cpl_free((*lt)->first_absolute_order);
00154 cpl_free((*lt)->last_absolute_order);
00155
00156 cpl_free(*lt);
00157 }
00158
00159 return;
00160 }
00161
00162
00170
00171 cpl_table **
00172 uves_lt_get_table(const lt_type *lt, int window, int trace)
00173 {
00174 return &(lt->table[trace + (window-1)*lt->traces]);
00175 }
00176
00177
00185
00186 polynomial **
00187 uves_lt_get_disprel(const lt_type *lt, int window, int trace)
00188 {
00189 return &(lt->dispersion_relation[trace + (window-1)*lt->traces]);
00190 }
00191
00199
00200 polynomial **
00201 uves_lt_get_absord(const lt_type *lt, int window, int trace)
00202 {
00203 return &(lt->absolute_order[trace + (window-1)*lt->traces]);
00204 }
00205
00213
00214 int *
00215 uves_lt_get_firstabs(const lt_type *lt, int window, int trace)
00216 {
00217 return &(lt->first_absolute_order[trace + (window-1)*lt->traces]);
00218 }
00219
00227
00228 int *
00229 uves_lt_get_lastabs(const lt_type *lt, int window, int trace)
00230 {
00231 return &(lt->last_absolute_order[trace + (window-1)*lt->traces]);
00232 }
00233
00234
00250
00251 int
00252 uves_wavecal_find_nearest(const cpl_table *line_refer, double lambda, int lo, int hi)
00253 {
00254 if (lo == hi)
00255 {
00256 return lo;
00257 }
00258 else if (lo + 1 == hi)
00259 {
00260 double llo, lhi;
00261 lhi = cpl_table_get_double(line_refer, "Wave", hi, NULL);
00262 llo = cpl_table_get_double(line_refer, "Wave", lo, NULL);
00263
00264
00265 return ((llo-lambda)*(llo-lambda) < (lhi-lambda)*(lhi-lambda)) ? lo : hi;
00266 }
00267 else
00268 {
00269 double lmid;
00270 int mid = (lo + hi)/2;
00271
00272
00273 lmid = cpl_table_get_double(line_refer, "Wave", mid, NULL);
00274
00275 if (lmid < lambda)
00276 {
00277 return uves_wavecal_find_nearest(line_refer, lambda, mid, hi);
00278 }
00279 else
00280 {
00281 return uves_wavecal_find_nearest(line_refer, lambda, lo, mid);
00282 }
00283 }
00284 }
00285
00286
00287
00302
00303 int
00304 uves_delete_bad_lines(cpl_table *table, double TOLERANCE, double kappa)
00305 {
00306 int result = 0;
00307 int numb_lines = cpl_table_get_nrow(table);
00308
00309
00310 check( uves_erase_invalid_table_rows(table, "Ident"),
00311 "Error erasing un-identified lines");
00312
00313 assure( cpl_table_has_column(table, "Residual_pix"),
00314 CPL_ERROR_DATA_NOT_FOUND,
00315 "Missing column 'Residual_pix'");
00316
00317 assure( cpl_table_has_column(table, LINETAB_RESIDUAL),
00318 CPL_ERROR_DATA_NOT_FOUND,
00319 "Missing column '" LINETAB_RESIDUAL "'");
00320
00321 if (TOLERANCE > 0)
00322 {
00323
00324 check(( uves_erase_table_rows(table,
00325 "Residual_pix", CPL_GREATER_THAN, TOLERANCE),
00326 uves_erase_table_rows(table,
00327 "Residual_pix", CPL_LESS_THAN , -TOLERANCE)),
00328 "Error removing rows");
00329 }
00330 else
00331 {
00332
00333 check(( uves_erase_table_rows(table,
00334 LINETAB_RESIDUAL, CPL_GREATER_THAN, -TOLERANCE),
00335 uves_erase_table_rows(table,
00336 LINETAB_RESIDUAL, CPL_LESS_THAN , TOLERANCE)),
00337 "Error removing rows");
00338 }
00339
00340
00341
00342
00343
00344 if (cpl_table_get_nrow(table) - cpl_table_count_invalid(table, "Residual_pix") >= 2)
00345 {
00346 check_nomsg( uves_average_reject(table,
00347 "Residual_pix", "temp",
00348 kappa));
00349
00350 check_nomsg( uves_average_reject(table,
00351 "Xwidth", "temp",
00352 kappa));
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 }
00365
00366 result = numb_lines - cpl_table_get_nrow(table);
00367
00368 cleanup:
00369 return result;
00370 }
00371
00372
00402
00403 cpl_error_code
00404 uves_draw_lines(cpl_image *image, polynomial *dispersion,
00405 const polynomial *order_locations, const cpl_table *t,
00406 const char *lambda_column, const char *abs_order,
00407 const int *relative_order, int minorder, int maxorder,
00408 bool vertical, int offset)
00409 {
00410 int nx, ny;
00411 int row;
00412
00413
00414 passure( image != NULL, " ");
00415 passure( dispersion != NULL, " ");
00416 passure( uves_polynomial_get_dimension(dispersion) == 2, "%d",
00417 uves_polynomial_get_dimension(dispersion));
00418 passure( order_locations != NULL, " ");
00419 passure( uves_polynomial_get_dimension(order_locations) == 2, "%d",
00420 uves_polynomial_get_dimension(order_locations));
00421 passure( t != NULL, " ");
00422 passure( cpl_table_has_column(t, lambda_column), "%s", lambda_column);
00423 if (abs_order != NULL)
00424 {
00425 passure( cpl_table_has_column(t, abs_order), "%s", abs_order);
00426 }
00427
00428 nx = cpl_image_get_size_x(image);
00429 ny = cpl_image_get_size_y(image);
00430
00431 for (row = 0; row < cpl_table_get_nrow(t); row++) {
00432 double x, xguess;
00433 double lambda, lambda_left, lambda_right;
00434 int order;
00435
00436 check( lambda = cpl_table_get_double(t, lambda_column, row, NULL),
00437 "Error reading table");
00438
00439 if (abs_order != NULL)
00440 check (minorder = maxorder = cpl_table_get_int(t, abs_order, row, NULL),
00441 "Error reading table");
00442
00443 for (order = minorder; order <= maxorder; order++) {
00444 lambda_left = uves_polynomial_evaluate_2d(dispersion, 1 , order)/order;
00445 lambda_right = uves_polynomial_evaluate_2d(dispersion, nx, order)/order;
00446
00447
00448 xguess = 1 + (nx - 1) * (lambda - lambda_left)/(lambda_right - lambda_left);
00449
00450
00451
00452 if (1 <= xguess && xguess <= nx) {
00453 x = uves_polynomial_solve_2d(dispersion, lambda*order, xguess,
00454 1,
00455 2,
00456 order);
00457
00458
00459 if (cpl_error_get_code() != CPL_ERROR_NONE)
00460 {
00461 uves_error_reset();
00462 }
00463 else {
00464
00465 uves_msg_debug("lambda(x=%f)\t = %f", x ,
00466 uves_polynomial_evaluate_2d(dispersion, x , order)/order);
00467 uves_msg_debug("lambda(x0=%f)\t = %f", xguess,
00468 uves_polynomial_evaluate_2d(dispersion, xguess, order)/order);
00469
00470 if (1 <= x && x <= nx) {
00471 double y = uves_polynomial_evaluate_2d(order_locations, x,
00472 relative_order[order]);
00473 int i;
00474
00475 for (i = -3; i <= 3; i++) {
00476 if (vertical) {
00477 check( cpl_image_set(image,
00478 uves_min_int(nx, uves_max_int(1, x )),
00479 uves_min_int(ny, uves_max_int(1, (int) y + i +
00480 offset)), 0),
00481 "Error writing image");
00482 }
00483 else {
00484 check( cpl_image_set(image,
00485 uves_min_int(nx, uves_max_int(1, x - i)),
00486 uves_min_int(ny, uves_max_int(1, (int) y +
00487 offset)), 0),
00488 "Error writing image");
00489 }
00490 }
00491 }
00492 }
00493 }
00494
00495 }
00496
00497 }
00498
00499 cleanup:
00500 return cpl_error_get_code();
00501 }
00502