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
00071
00072
00073 #ifdef HAVE_CONFIG_H
00074 # include <config.h>
00075 #endif
00076
00077
00081
00084
00085
00086
00087
00088 #include <uves_wavecal_utils.h>
00089 #include <uves_utils.h>
00090 #include <uves_utils_wrappers.h>
00091 #include <uves_error.h>
00092 #include <uves_msg.h>
00093
00094 #include <cpl.h>
00095
00096
00097
00098
00099
00100
00101
00102
00103
00111
00112
00113 lt_type *uves_lt_new(int windows, int traces)
00114 {
00115
00116 lt_type *lt = cpl_malloc(sizeof(lt_type));
00117
00118 assure_mem( lt );
00119
00120 lt->windows = windows;
00121 lt->traces = traces;
00122
00123
00124 lt->table = cpl_calloc(windows*traces, sizeof(cpl_table *));
00125 lt->dispersion_relation = cpl_calloc(windows*traces, sizeof(polynomial *));
00126 lt->absolute_order = cpl_calloc(windows*traces, sizeof(polynomial *));
00127 lt->first_absolute_order = cpl_calloc(windows*traces, sizeof(int));
00128 lt->last_absolute_order = cpl_calloc(windows*traces, sizeof(int));
00129
00130 cleanup:
00131 return lt;
00132 }
00133
00134
00139
00140
00141 void uves_lt_delete(lt_type **lt)
00142 {
00143 if (lt != NULL && *lt != NULL)
00144 {
00145 int i;
00146 for (i = 0; i < (*lt)->windows * (*lt)->traces; i++)
00147 {
00148 uves_free_table (&((*lt)->table[i]));
00149 uves_polynomial_delete(&((*lt)->dispersion_relation[i]));
00150 uves_polynomial_delete(&((*lt)->absolute_order[i]));
00151 }
00152
00153 cpl_free((*lt)->table);
00154 cpl_free((*lt)->dispersion_relation);
00155 cpl_free((*lt)->absolute_order);
00156 cpl_free((*lt)->first_absolute_order);
00157 cpl_free((*lt)->last_absolute_order);
00158
00159 cpl_free(*lt);
00160 }
00161
00162 return;
00163 }
00164
00165
00173
00174 cpl_table **
00175 uves_lt_get_table(const lt_type *lt, int window, int trace)
00176 {
00177 return &(lt->table[trace + (window-1)*lt->traces]);
00178 }
00179
00180
00188
00189 polynomial **
00190 uves_lt_get_disprel(const lt_type *lt, int window, int trace)
00191 {
00192 return &(lt->dispersion_relation[trace + (window-1)*lt->traces]);
00193 }
00194
00202
00203 polynomial **
00204 uves_lt_get_absord(const lt_type *lt, int window, int trace)
00205 {
00206 return &(lt->absolute_order[trace + (window-1)*lt->traces]);
00207 }
00208
00216
00217 int *
00218 uves_lt_get_firstabs(const lt_type *lt, int window, int trace)
00219 {
00220 return &(lt->first_absolute_order[trace + (window-1)*lt->traces]);
00221 }
00222
00230
00231 int *
00232 uves_lt_get_lastabs(const lt_type *lt, int window, int trace)
00233 {
00234 return &(lt->last_absolute_order[trace + (window-1)*lt->traces]);
00235 }
00236
00237
00253
00254 int
00255 uves_wavecal_find_nearest(const cpl_table *line_refer, double lambda, int lo, int hi)
00256 {
00257 if (lo == hi)
00258 {
00259 return lo;
00260 }
00261 else if (lo + 1 == hi)
00262 {
00263 double llo, lhi;
00264 lhi = cpl_table_get_double(line_refer, "Wave", hi, NULL);
00265 llo = cpl_table_get_double(line_refer, "Wave", lo, NULL);
00266
00267
00268 return ((llo-lambda)*(llo-lambda) < (lhi-lambda)*(lhi-lambda)) ? lo : hi;
00269 }
00270 else
00271 {
00272 double lmid;
00273 int mid = (lo + hi)/2;
00274
00275
00276 lmid = cpl_table_get_double(line_refer, "Wave", mid, NULL);
00277
00278 if (lmid < lambda)
00279 {
00280 return uves_wavecal_find_nearest(line_refer, lambda, mid, hi);
00281 }
00282 else
00283 {
00284 return uves_wavecal_find_nearest(line_refer, lambda, lo, mid);
00285 }
00286 }
00287 }
00288
00289
00290
00305
00306 int
00307 uves_delete_bad_lines(cpl_table *table, double TOLERANCE, double kappa)
00308 {
00309 int result = 0;
00310 int numb_lines = cpl_table_get_nrow(table);
00311
00312
00313 check( uves_erase_invalid_table_rows(table, "Ident"),
00314 "Error erasing un-identified lines");
00315
00316 assure( cpl_table_has_column(table, "Residual_pix"),
00317 CPL_ERROR_DATA_NOT_FOUND,
00318 "Missing column 'Residual_pix'");
00319
00320 assure( cpl_table_has_column(table, LINETAB_RESIDUAL),
00321 CPL_ERROR_DATA_NOT_FOUND,
00322 "Missing column '" LINETAB_RESIDUAL "'");
00323
00324 if (TOLERANCE > 0)
00325 {
00326
00327 check(( uves_erase_table_rows(table,
00328 "Residual_pix", CPL_GREATER_THAN, TOLERANCE),
00329 uves_erase_table_rows(table,
00330 "Residual_pix", CPL_LESS_THAN , -TOLERANCE)),
00331 "Error removing rows");
00332 }
00333 else
00334 {
00335
00336 check(( uves_erase_table_rows(table,
00337 LINETAB_RESIDUAL, CPL_GREATER_THAN, -TOLERANCE),
00338 uves_erase_table_rows(table,
00339 LINETAB_RESIDUAL, CPL_LESS_THAN , TOLERANCE)),
00340 "Error removing rows");
00341 }
00342
00343
00344
00345
00346
00347 if (cpl_table_get_nrow(table) - cpl_table_count_invalid(table, "Residual_pix") >= 2)
00348 {
00349 check_nomsg( uves_average_reject(table,
00350 "Residual_pix", "temp",
00351 kappa));
00352
00353 check_nomsg( uves_average_reject(table,
00354 "Xwidth", "temp",
00355 kappa));
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 }
00368
00369 result = numb_lines - cpl_table_get_nrow(table);
00370
00371 cleanup:
00372 return result;
00373 }
00374
00375
00405
00406 cpl_error_code
00407 uves_draw_lines(cpl_image *image, polynomial *dispersion,
00408 const polynomial *order_locations, const cpl_table *t,
00409 const char *lambda_column, const char *abs_order,
00410 const int *relative_order, int minorder, int maxorder,
00411 bool vertical, int offset)
00412 {
00413 int nx, ny;
00414 int row;
00415
00416
00417 passure( image != NULL, " ");
00418 passure( dispersion != NULL, " ");
00419 passure( uves_polynomial_get_dimension(dispersion) == 2, "%d",
00420 uves_polynomial_get_dimension(dispersion));
00421 passure( order_locations != NULL, " ");
00422 passure( uves_polynomial_get_dimension(order_locations) == 2, "%d",
00423 uves_polynomial_get_dimension(order_locations));
00424 passure( t != NULL, " ");
00425 passure( cpl_table_has_column(t, lambda_column), "%s", lambda_column);
00426 if (abs_order != NULL)
00427 {
00428 passure( cpl_table_has_column(t, abs_order), "%s", abs_order);
00429 }
00430
00431 nx = cpl_image_get_size_x(image);
00432 ny = cpl_image_get_size_y(image);
00433
00434 for (row = 0; row < cpl_table_get_nrow(t); row++) {
00435 double x, xguess;
00436 double lambda, lambda_left, lambda_right;
00437 int order;
00438
00439 check( lambda = cpl_table_get_double(t, lambda_column, row, NULL),
00440 "Error reading table");
00441
00442 if (abs_order != NULL)
00443 check (minorder = maxorder = cpl_table_get_int(t, abs_order, row, NULL),
00444 "Error reading table");
00445
00446 for (order = minorder; order <= maxorder; order++) {
00447 lambda_left = uves_polynomial_evaluate_2d(dispersion, 1 , order)/order;
00448 lambda_right = uves_polynomial_evaluate_2d(dispersion, nx, order)/order;
00449
00450
00451 xguess = 1 + (nx - 1) * (lambda - lambda_left)/(lambda_right - lambda_left);
00452
00453
00454
00455 if (1 <= xguess && xguess <= nx) {
00456 x = uves_polynomial_solve_2d(dispersion, lambda*order, xguess,
00457 1,
00458 2,
00459 order);
00460
00461
00462 if (cpl_error_get_code() != CPL_ERROR_NONE)
00463 {
00464 uves_error_reset();
00465 }
00466 else {
00467
00468 uves_msg_debug("lambda(x=%f)\t = %f", x ,
00469 uves_polynomial_evaluate_2d(dispersion, x , order)/order);
00470 uves_msg_debug("lambda(x0=%f)\t = %f", xguess,
00471 uves_polynomial_evaluate_2d(dispersion, xguess, order)/order);
00472
00473 if (1 <= x && x <= nx) {
00474 double y = uves_polynomial_evaluate_2d(order_locations, x,
00475 relative_order[order]);
00476 int i;
00477
00478 for (i = -3; i <= 3; i++) {
00479 if (vertical) {
00480 check( cpl_image_set(image,
00481 uves_min_int(nx, uves_max_int(1, x )),
00482 uves_min_int(ny, uves_max_int(1, (int) y + i +
00483 offset)), 0),
00484 "Error writing image");
00485 }
00486 else {
00487 check( cpl_image_set(image,
00488 uves_min_int(nx, uves_max_int(1, x - i)),
00489 uves_min_int(ny, uves_max_int(1, (int) y +
00490 offset)), 0),
00491 "Error writing image");
00492 }
00493 }
00494 }
00495 }
00496 }
00497
00498 }
00499
00500 }
00501
00502 cleanup:
00503 return cpl_error_get_code();
00504 }
00505