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
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00102
00105 #ifdef HAVE_CONFIG_H
00106 # include <config.h>
00107 #endif
00108
00109
00110
00111 #include <uves_physmod_msrawxy.h>
00112
00113 #include <uves_physmod_center_gauss.h>
00114 #include <uves_pfits.h>
00115 #include <uves_utils_polynomial.h>
00116 #include <uves_utils_wrappers.h>
00117 #include <uves_msg.h>
00118 #include <uves_error.h>
00119
00120
00121
00122
00123
00124
00154
00155
00156
00157 int uves_physmod_msrawxy(const cpl_image *raw_image,
00158 const uves_propertylist *raw_header,
00159 const char *recipe_id,
00160 const cpl_parameterlist* parameters,
00161 cpl_table* mod_tbl,
00162 cpl_table** l_tbl,
00163 cpl_table** m_tbl,
00164 cpl_table** p_tbl,
00165 cpl_table** rline_tbl,
00166 cpl_table** mline_tbl,
00167 cpl_table** npline_tbl)
00168
00169 {
00170 double scl_fct=0;
00171 double add_fct=0;
00172 double X_AXIS_SCALE=0;
00173 double Y_AXIS_SCALE=0;
00174 double OFFSET_X=0;
00175 double OFFSET_Y=0;
00176
00177
00178 int status=0;
00179
00180
00181 double limit[4]={0.,0.,0.,0.};
00182 int MBOX_X=0;
00183 int MBOX_Y=0;
00184
00185 double tmp_x=0;
00186 double tmp_y=0;
00187 double tmp_val_x=0;
00188 double tmp_val_y=0;
00189
00190
00191
00192 polynomial* poly2d_x=NULL;
00193 polynomial* poly2d_y=NULL;
00194 double mean_err_xdif=0;
00195 double mean_err_ydif=0;
00196
00197 double binx=0;
00198 double biny=0;
00199
00200 double mbox_x=0;
00201 double mbox_y=0;
00202 double outputd[3][3];
00203 int k=0;
00204
00205 cpl_table * tmp_tbl=NULL;
00206 cpl_table * tmp_tbl1=NULL;
00207 cpl_table * tmp_tbl2=NULL;
00208 cpl_table * tmp_tbl3=NULL;
00209 cpl_table * tmp_tbl4=NULL;
00210
00211 uves_msg_debug("start msrawxy");
00212
00213 check (biny = uves_pfits_get_binx(raw_header),
00214 "Could not read x binning factor from input header");
00215
00216 check (binx = uves_pfits_get_biny(raw_header),
00217 "Could not read x binning factor from input header");
00218
00219 check( uves_get_parameter(parameters, NULL, recipe_id,
00220 "mbox_x", CPL_TYPE_INT, &MBOX_X ) ,
00221 "Could not read parameter");
00222
00223 check( uves_get_parameter(parameters, NULL, recipe_id,
00224 "mbox_y", CPL_TYPE_INT, &MBOX_Y ) ,
00225 "Could not read parameter");
00226
00227 mbox_x=MBOX_X/binx;
00228 mbox_y=MBOX_Y/biny;
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 check( uves_get_parameter(parameters, NULL, recipe_id,
00242 "x_axis_scale", CPL_TYPE_DOUBLE, &X_AXIS_SCALE ) ,
00243 "Could not read parameter");
00244
00245 check( uves_get_parameter(parameters, NULL, recipe_id,
00246 "y_axis_scale", CPL_TYPE_DOUBLE, &Y_AXIS_SCALE ),
00247 "Could not read parameter");
00248
00249 check(cpl_table_and_selected_double(mod_tbl,"IDENT",CPL_GREATER_THAN,0),
00250 "Error selecting IDENT");
00251 *l_tbl=cpl_table_extract_selected(mod_tbl);
00252
00253 uves_msg_debug("xs=%f ofx=%f bx=%f",(1.+X_AXIS_SCALE),OFFSET_X,mbox_x/2);
00254 uves_msg_debug("ys=%f ofy=%f by=%f",(1.+Y_AXIS_SCALE),OFFSET_Y,mbox_y/2);
00255
00256 scl_fct=(1.+X_AXIS_SCALE);
00257 cpl_table_erase_column(*l_tbl,"XSTART");
00258 cpl_table_duplicate_column(*l_tbl,"XSTART",*l_tbl,"XMOD");
00259 cpl_table_multiply_scalar(*l_tbl,"XSTART",scl_fct);
00260 add_fct=OFFSET_X-(double)mbox_x/2;
00261
00262 uves_msg_debug("add_fct=%f",add_fct);
00263 cpl_table_add_scalar(*l_tbl,"XSTART",add_fct);
00264
00265 cpl_table_erase_column(*l_tbl,"XEND");
00266 cpl_table_duplicate_column(*l_tbl,"XEND",*l_tbl,"XMOD");
00267 cpl_table_multiply_scalar(*l_tbl,"XEND",scl_fct);
00268 add_fct=OFFSET_X+(double)mbox_x/2;
00269 uves_msg_debug("add_fct=%f",add_fct);
00270 cpl_table_add_scalar(*l_tbl,"XEND",add_fct);
00271
00272 scl_fct=(1.+Y_AXIS_SCALE);
00273 cpl_table_erase_column(*l_tbl,"YSTART");
00274 cpl_table_duplicate_column(*l_tbl,"YSTART",*l_tbl,"YMOD");
00275 cpl_table_multiply_scalar(*l_tbl,"YSTART",scl_fct);
00276 add_fct=OFFSET_Y-(double)mbox_y/2;
00277 uves_msg_debug("add_fct=%f",add_fct);
00278 cpl_table_add_scalar(*l_tbl,"YSTART",add_fct);
00279
00280 cpl_table_erase_column(*l_tbl,"YEND");
00281 cpl_table_duplicate_column(*l_tbl,"YEND",*l_tbl,"YMOD");
00282 cpl_table_multiply_scalar(*l_tbl,"YEND",scl_fct);
00283 add_fct=OFFSET_Y+(double)mbox_y/2;
00284 uves_msg_debug("add_fct=%f",add_fct);
00285 cpl_table_add_scalar(*l_tbl,"YEND",add_fct);
00286
00287
00288
00289 limit[2] = mbox_x/2;
00290 check(tmp_tbl1=uves_extract_table_rows(*l_tbl,"XSTART",CPL_GREATER_THAN,
00291 limit[2]),"Error selecting XSTART");
00292
00293 limit[0] = cpl_image_get_size_x(raw_image)-mbox_x/2;
00294
00295 check(tmp_tbl2=uves_extract_table_rows(tmp_tbl1,"XSTART",CPL_LESS_THAN,
00296 limit[0]),"Error selecting XSTART");
00297
00298 limit[3] = mbox_y/2;
00299
00300 check(tmp_tbl3=uves_extract_table_rows(tmp_tbl2,"YSTART",CPL_GREATER_THAN,
00301 limit[3]),"Error selecting YSTART");
00302
00303 limit[1] = cpl_image_get_size_y(raw_image)-mbox_y/2;
00304
00305 check(tmp_tbl4=uves_extract_table_rows(tmp_tbl3,"YSTART",CPL_LESS_THAN,
00306 limit[1]),"Error selecting YSTART");
00307
00308 uves_msg_debug("limits %f %f %f %f",
00309 limit[0],limit[1],limit[2],limit[3]);
00310 *m_tbl = cpl_table_extract_selected(tmp_tbl4);
00311 *rline_tbl = cpl_table_extract_selected(tmp_tbl4);
00312
00313
00314 tmp_tbl=cpl_table_extract_selected(*l_tbl);
00315 uves_free_table(l_tbl);
00316 *l_tbl=cpl_table_duplicate(tmp_tbl);
00317 uves_free_table(&tmp_tbl);
00318
00319
00320 assure( !cpl_table_has_invalid(*m_tbl,"XMOD") &&
00321 !cpl_table_has_invalid(*m_tbl,"YMOD"),
00322 CPL_ERROR_ILLEGAL_INPUT,
00323 "Invalid content in table m_tbl");
00324
00325 uves_msg_debug("min=%f max=%f std=%f",
00326 cpl_image_get_min(raw_image),
00327 cpl_image_get_max(raw_image),
00328 cpl_image_get_stdev(raw_image));
00329
00330 check_nomsg( uves_physmod_center_gauss(raw_image,m_tbl) );
00331
00332
00333
00334
00335
00336 check_nomsg(
00337 (cpl_table_duplicate_column(*rline_tbl,"XMES",*m_tbl,"XCEN"),
00338 cpl_table_duplicate_column(*rline_tbl,"YMES",*m_tbl,"YCEN"),
00339 cpl_table_duplicate_column(*rline_tbl,"STATUS",*m_tbl,"STATUS")));
00340
00341
00342 *mline_tbl=cpl_table_duplicate(*rline_tbl);
00343
00344 uves_msg_debug("nraw=%d",
00345 cpl_table_and_selected_int(*rline_tbl,"STATUS",CPL_EQUAL_TO,0.));
00346
00347
00348 check_nomsg( *p_tbl=cpl_table_extract_selected(*rline_tbl) );
00349
00350 uves_free_table(rline_tbl);
00351 *rline_tbl=cpl_table_duplicate(*p_tbl);
00352
00353 uves_msg_debug("middump nraw=%d",cpl_table_get_nrow(*p_tbl));
00354
00355 cpl_table_select_all(*rline_tbl);
00356 uves_free_table(rline_tbl);
00357 *rline_tbl=cpl_table_duplicate(*p_tbl);
00358 uves_msg_debug("rline nraw=%d",cpl_table_get_nrow(*rline_tbl));
00359
00360
00361 check_nomsg( (cpl_table_duplicate_column(*rline_tbl,"XDIF",*rline_tbl,"XMES"),
00362 cpl_table_subtract_columns(*rline_tbl,"XDIF","XMOD")) );
00363 uves_msg_debug("rline1 nraw=%d",cpl_table_get_nrow(*rline_tbl));
00364
00365
00366 cpl_table_duplicate_column(*rline_tbl,"YDIF",*rline_tbl,"YMES");
00367 cpl_table_subtract_columns(*rline_tbl,"YDIF","YMOD");
00368
00369 uves_msg_debug("rline2 nraw=%d",cpl_table_get_nrow(*rline_tbl));
00370
00371
00372 check(poly2d_x=uves_polynomial_regression_2d(*rline_tbl,"XMOD","YMOD","XDIF",
00373 NULL,2,2,"XDIFREG",NULL,NULL,
00374 &mean_err_xdif,NULL,NULL,-1,-1),
00375 "Fitting XDIF failed");
00376
00377 outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_x,0,0);
00378 outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_x,1,0);
00379 outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_x,0,1);
00380 outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_x,1,1);
00381 outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_x,2,0);
00382 outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_x,0,2);
00383 outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_x,1,2);
00384 outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_x,2,1);
00385 outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_x,2,2);
00386
00387 check(poly2d_y=uves_polynomial_regression_2d(*rline_tbl,"XMOD","YMOD","YDIF",
00388 NULL,2,2,"YDIFREG",NULL,NULL,
00389 &mean_err_ydif,NULL,NULL,-1,-1),
00390 "Fitting YDIF failed");
00391
00392
00393 outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_y,0,0);
00394 outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_y,1,0);
00395 outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_y,0,1);
00396 outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_y,1,1);
00397 outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_y,2,0);
00398 outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_y,0,2);
00399 outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_y,1,2);
00400 outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_y,2,1);
00401 outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_y,2,2);
00402
00403
00404 uves_msg_debug("rline3 nraw=%d",cpl_table_get_nrow(*rline_tbl));
00405 cpl_table_duplicate_column(*rline_tbl,"XMODREG",*rline_tbl,"XMOD");
00406 cpl_table_add_columns(*rline_tbl,"XMODREG","XDIFREG");
00407
00408 cpl_table_duplicate_column(*rline_tbl,"YMODREG",*rline_tbl,"YMOD");
00409 cpl_table_add_columns(*rline_tbl,"YMODREG","YDIFREG");
00410
00411 uves_msg_debug("rline4 nraw=%d",cpl_table_get_nrow(*rline_tbl));
00412
00413
00414
00415
00416
00417
00418 *npline_tbl=cpl_table_extract_selected(mod_tbl);
00419
00420
00421 cpl_table_select_all(mod_tbl);
00422
00423
00424
00425 uves_msg_debug("npline nraw=%d",cpl_table_get_nrow(*npline_tbl));
00426
00427
00428
00429 tmp_tbl=cpl_table_new(cpl_table_get_nrow(*rline_tbl));
00430 cpl_table_duplicate_column(tmp_tbl,"XDIF",*rline_tbl,"XDIF");
00431 cpl_table_duplicate_column(tmp_tbl,"YDIF",*rline_tbl,"YDIF");
00432
00433 cpl_table_set_size(tmp_tbl,cpl_table_get_nrow(*npline_tbl));
00434
00435 cpl_table_duplicate_column(*npline_tbl,"XDIF",tmp_tbl,"XDIF");
00436 cpl_table_duplicate_column(*npline_tbl,"YDIF",tmp_tbl,"YDIF");
00437 uves_free_table(&tmp_tbl);
00438
00439 cpl_table_new_column(*npline_tbl,"XDIFREG",CPL_TYPE_DOUBLE);
00440 cpl_table_new_column(*npline_tbl,"YDIFREG",CPL_TYPE_DOUBLE);
00441
00442 for (k=0;k<cpl_table_get_nrow(*npline_tbl);k++) {
00443
00444 tmp_x=cpl_table_get_double(*npline_tbl,"XMOD",k,&status);
00445 tmp_y=cpl_table_get_double(*npline_tbl,"YMOD",k,&status);
00446
00447 tmp_val_x=uves_polynomial_evaluate_2d(poly2d_x,tmp_x,tmp_y);
00448 tmp_val_y=uves_polynomial_evaluate_2d(poly2d_y,tmp_x,tmp_y);
00449
00450 cpl_table_set_double(*npline_tbl,"XDIFREG",k,tmp_val_x);
00451 cpl_table_set_double(*npline_tbl,"YDIFREG",k,tmp_val_y);
00452
00453 }
00454
00455 cpl_table_add_columns(*npline_tbl,"XMOD","XDIFREG");
00456 cpl_table_add_columns(*npline_tbl,"YMOD","YDIFREG");
00457
00458
00459 cleanup:
00460 uves_polynomial_delete(&poly2d_x);
00461 uves_polynomial_delete(&poly2d_y);
00462 uves_free_table(&tmp_tbl);
00463 uves_free_table(&tmp_tbl1);
00464 uves_free_table(&tmp_tbl2);
00465 uves_free_table(&tmp_tbl3);
00466 uves_free_table(&tmp_tbl4);
00467
00468 uves_msg_debug("end msrawxy");
00469 return 0;
00470 }