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