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
00031
00034 #ifdef HAVE_CONFIG_H
00035 # include <config.h>
00036 #endif
00037
00038
00039
00040 #include <uves_physmod_msrawxy.h>
00041
00042 #include <uves_physmod_center_gauss.h>
00043 #include <uves_pfits.h>
00044 #include <uves_utils_polynomial.h>
00045 #include <uves_utils_wrappers.h>
00046 #include <uves_msg.h>
00047 #include <uves_error.h>
00048
00049
00050
00051
00052
00053
00083
00084
00085
00086 int uves_physmod_msrawxy(const cpl_image *raw_image,
00087 const uves_propertylist *raw_header,
00088 const char *recipe_id,
00089 const cpl_parameterlist* parameters,
00090 cpl_table* mod_tbl,
00091 cpl_table** l_tbl,
00092 cpl_table** m_tbl,
00093 cpl_table** p_tbl,
00094 cpl_table** rline_tbl,
00095 cpl_table** mline_tbl,
00096 cpl_table** npline_tbl)
00097
00098 {
00099 double scl_fct=0;
00100 double add_fct=0;
00101 double X_AXIS_SCALE=0;
00102 double Y_AXIS_SCALE=0;
00103 double OFFSET_X=0;
00104 double OFFSET_Y=0;
00105
00106
00107 int status=0;
00108
00109
00110 double limit[4]={0.,0.,0.,0.};
00111 int MBOX_X=0;
00112 int MBOX_Y=0;
00113
00114 double tmp_x=0;
00115 double tmp_y=0;
00116 double tmp_val_x=0;
00117 double tmp_val_y=0;
00118
00119
00120
00121 polynomial* poly2d_x=NULL;
00122 polynomial* poly2d_y=NULL;
00123 double mean_err_xdif=0;
00124 double mean_err_ydif=0;
00125
00126 double binx=0;
00127 double biny=0;
00128
00129 double mbox_x=0;
00130 double mbox_y=0;
00131 double outputd[3][3];
00132 int k=0;
00133
00134 cpl_table * tmp_tbl=NULL;
00135 cpl_table * tmp_tbl1=NULL;
00136 cpl_table * tmp_tbl2=NULL;
00137 cpl_table * tmp_tbl3=NULL;
00138 cpl_table * tmp_tbl4=NULL;
00139
00140 uves_msg_debug("start msrawxy");
00141
00142 check (biny = uves_pfits_get_binx(raw_header),
00143 "Could not read x binning factor from input header");
00144
00145 check (binx = uves_pfits_get_biny(raw_header),
00146 "Could not read x binning factor from input header");
00147
00148 check( uves_get_parameter(parameters, NULL, recipe_id,
00149 "mbox_x", CPL_TYPE_INT, &MBOX_X ) ,
00150 "Could not read parameter");
00151
00152 check( uves_get_parameter(parameters, NULL, recipe_id,
00153 "mbox_y", CPL_TYPE_INT, &MBOX_Y ) ,
00154 "Could not read parameter");
00155
00156 mbox_x=MBOX_X/binx;
00157 mbox_y=MBOX_Y/biny;
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 check(cpl_table_and_selected_double(mod_tbl,"IDENT",CPL_GREATER_THAN,0),
00182 "Error selecting IDENT");
00183 *l_tbl=cpl_table_extract_selected(mod_tbl);
00184
00185 uves_msg_debug("xs=%f ofx=%f bx=%f",(1.+X_AXIS_SCALE),OFFSET_X,mbox_x/2);
00186 uves_msg_debug("ys=%f ofy=%f by=%f",(1.+Y_AXIS_SCALE),OFFSET_Y,mbox_y/2);
00187
00188 scl_fct=(1.+X_AXIS_SCALE);
00189 cpl_table_erase_column(*l_tbl,"XSTART");
00190 cpl_table_duplicate_column(*l_tbl,"XSTART",*l_tbl,"XMOD");
00191 cpl_table_multiply_scalar(*l_tbl,"XSTART",scl_fct);
00192 add_fct=OFFSET_X-(double)mbox_x/2;
00193
00194 uves_msg_debug("add_fct=%f",add_fct);
00195 cpl_table_add_scalar(*l_tbl,"XSTART",add_fct);
00196
00197 cpl_table_erase_column(*l_tbl,"XEND");
00198 cpl_table_duplicate_column(*l_tbl,"XEND",*l_tbl,"XMOD");
00199 cpl_table_multiply_scalar(*l_tbl,"XEND",scl_fct);
00200 add_fct=OFFSET_X+(double)mbox_x/2;
00201 uves_msg_debug("add_fct=%f",add_fct);
00202 cpl_table_add_scalar(*l_tbl,"XEND",add_fct);
00203
00204 scl_fct=(1.+Y_AXIS_SCALE);
00205 cpl_table_erase_column(*l_tbl,"YSTART");
00206 cpl_table_duplicate_column(*l_tbl,"YSTART",*l_tbl,"YMOD");
00207 cpl_table_multiply_scalar(*l_tbl,"YSTART",scl_fct);
00208 add_fct=OFFSET_Y-(double)mbox_y/2;
00209 uves_msg_debug("add_fct=%f",add_fct);
00210 cpl_table_add_scalar(*l_tbl,"YSTART",add_fct);
00211
00212 cpl_table_erase_column(*l_tbl,"YEND");
00213 cpl_table_duplicate_column(*l_tbl,"YEND",*l_tbl,"YMOD");
00214 cpl_table_multiply_scalar(*l_tbl,"YEND",scl_fct);
00215 add_fct=OFFSET_Y+(double)mbox_y/2;
00216 uves_msg_debug("add_fct=%f",add_fct);
00217 cpl_table_add_scalar(*l_tbl,"YEND",add_fct);
00218
00219
00220
00221 limit[2] = mbox_x/2;
00222 check(tmp_tbl1=uves_extract_table_rows(*l_tbl,"XSTART",CPL_GREATER_THAN,
00223 limit[2]),"Error selecting XSTART");
00224
00225 check_nomsg(limit[0] = cpl_image_get_size_x(raw_image)-mbox_x/2);
00226
00227 check(tmp_tbl2=uves_extract_table_rows(tmp_tbl1,"XSTART",CPL_LESS_THAN,
00228 limit[0]),"Error selecting XSTART");
00229
00230 limit[3] = mbox_y/2;
00231
00232 check(tmp_tbl3=uves_extract_table_rows(tmp_tbl2,"YSTART",CPL_GREATER_THAN,
00233 limit[3]),"Error selecting YSTART");
00234
00235 check_nomsg(limit[1] = cpl_image_get_size_y(raw_image)-mbox_y/2);
00236
00237 check(tmp_tbl4=uves_extract_table_rows(tmp_tbl3,"YSTART",CPL_LESS_THAN,
00238 limit[1]),"Error selecting YSTART");
00239
00240 uves_msg_debug("limits %f %f %f %f",
00241 limit[0],limit[1],limit[2],limit[3]);
00242 check_nomsg(*m_tbl = cpl_table_extract_selected(tmp_tbl4));
00243 check_nomsg(*rline_tbl = cpl_table_extract_selected(tmp_tbl4));
00244
00245
00246 check_nomsg(tmp_tbl=cpl_table_extract_selected(*l_tbl));
00247 uves_free_table(l_tbl);
00248 *l_tbl=cpl_table_duplicate(tmp_tbl);
00249 uves_free_table(&tmp_tbl);
00250
00251
00252 assure( !cpl_table_has_invalid(*m_tbl,"XMOD") &&
00253 !cpl_table_has_invalid(*m_tbl,"YMOD"),
00254 CPL_ERROR_ILLEGAL_INPUT,
00255 "Invalid content in table m_tbl");
00256
00257 uves_msg_debug("min=%f max=%f std=%f",
00258 cpl_image_get_min(raw_image),
00259 cpl_image_get_max(raw_image),
00260 cpl_image_get_stdev(raw_image));
00261
00262 check_nomsg( uves_physmod_center_gauss(raw_image,m_tbl) );
00263
00264
00265
00266
00267
00268 check_nomsg(
00269 (cpl_table_duplicate_column(*rline_tbl,"XMES",*m_tbl,"XCEN"),
00270 cpl_table_duplicate_column(*rline_tbl,"YMES",*m_tbl,"YCEN"),
00271 cpl_table_duplicate_column(*rline_tbl,"STATUS",*m_tbl,"STATUS")));
00272
00273
00274 *mline_tbl=cpl_table_duplicate(*rline_tbl);
00275
00276 uves_msg_debug("nraw=%" CPL_SIZE_FORMAT "",
00277 cpl_table_and_selected_int(*rline_tbl,"STATUS",CPL_EQUAL_TO,0.));
00278
00279
00280 check_nomsg( *p_tbl=cpl_table_extract_selected(*rline_tbl) );
00281
00282 uves_free_table(rline_tbl);
00283 *rline_tbl=cpl_table_duplicate(*p_tbl);
00284
00285 uves_msg_debug("middump nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*p_tbl));
00286
00287 cpl_table_select_all(*rline_tbl);
00288 uves_free_table(rline_tbl);
00289 *rline_tbl=cpl_table_duplicate(*p_tbl);
00290 uves_msg_debug("rline nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
00291
00292
00293 check_nomsg( (cpl_table_duplicate_column(*rline_tbl,"XDIF",*rline_tbl,"XMES"),
00294 cpl_table_subtract_columns(*rline_tbl,"XDIF","XMOD")) );
00295 uves_msg_debug("rline1 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
00296
00297
00298 cpl_table_duplicate_column(*rline_tbl,"YDIF",*rline_tbl,"YMES");
00299 cpl_table_subtract_columns(*rline_tbl,"YDIF","YMOD");
00300
00301 uves_msg_debug("rline2 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
00302
00303
00304 check(poly2d_x=uves_polynomial_regression_2d(*rline_tbl,"XMOD","YMOD","XDIF",
00305 NULL,2,2,"XDIFREG",NULL,NULL,
00306 &mean_err_xdif,NULL,NULL,-1,-1),
00307 "2D poly regression to fit XDIF failed. Try to increase mbox_x value, "
00308 "and possibly the mbox_y value");
00309
00310 outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_x,0,0);
00311 outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_x,1,0);
00312 outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_x,0,1);
00313 outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_x,1,1);
00314 outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_x,2,0);
00315 outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_x,0,2);
00316 outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_x,1,2);
00317 outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_x,2,1);
00318 outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_x,2,2);
00319
00320 check(poly2d_y=uves_polynomial_regression_2d(*rline_tbl,"XMOD","YMOD","YDIF",
00321 NULL,2,2,"YDIFREG",NULL,NULL,
00322 &mean_err_ydif,NULL,NULL,-1,-1),
00323 "2D poly regression to fit YDIF failed. Try to increase mbox_y value");
00324
00325
00326 outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_y,0,0);
00327 outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_y,1,0);
00328 outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_y,0,1);
00329 outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_y,1,1);
00330 outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_y,2,0);
00331 outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_y,0,2);
00332 outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_y,1,2);
00333 outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_y,2,1);
00334 outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_y,2,2);
00335
00336
00337 uves_msg_debug("rline3 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
00338 cpl_table_duplicate_column(*rline_tbl,"XMODREG",*rline_tbl,"XMOD");
00339 cpl_table_add_columns(*rline_tbl,"XMODREG","XDIFREG");
00340
00341 cpl_table_duplicate_column(*rline_tbl,"YMODREG",*rline_tbl,"YMOD");
00342 cpl_table_add_columns(*rline_tbl,"YMODREG","YDIFREG");
00343
00344 uves_msg_debug("rline4 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
00345
00346
00347
00348
00349
00350
00351 *npline_tbl=cpl_table_extract_selected(mod_tbl);
00352
00353
00354 cpl_table_select_all(mod_tbl);
00355
00356
00357
00358 uves_msg_debug("npline nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*npline_tbl));
00359
00360
00361
00362 tmp_tbl=cpl_table_new(cpl_table_get_nrow(*rline_tbl));
00363 cpl_table_duplicate_column(tmp_tbl,"XDIF",*rline_tbl,"XDIF");
00364 cpl_table_duplicate_column(tmp_tbl,"YDIF",*rline_tbl,"YDIF");
00365
00366 cpl_table_set_size(tmp_tbl,cpl_table_get_nrow(*npline_tbl));
00367
00368 cpl_table_duplicate_column(*npline_tbl,"XDIF",tmp_tbl,"XDIF");
00369 cpl_table_duplicate_column(*npline_tbl,"YDIF",tmp_tbl,"YDIF");
00370 uves_free_table(&tmp_tbl);
00371
00372 cpl_table_new_column(*npline_tbl,"XDIFREG",CPL_TYPE_DOUBLE);
00373 cpl_table_new_column(*npline_tbl,"YDIFREG",CPL_TYPE_DOUBLE);
00374
00375 for (k=0;k<cpl_table_get_nrow(*npline_tbl);k++) {
00376
00377 tmp_x=cpl_table_get_double(*npline_tbl,"XMOD",k,&status);
00378 tmp_y=cpl_table_get_double(*npline_tbl,"YMOD",k,&status);
00379
00380 tmp_val_x=uves_polynomial_evaluate_2d(poly2d_x,tmp_x,tmp_y);
00381 tmp_val_y=uves_polynomial_evaluate_2d(poly2d_y,tmp_x,tmp_y);
00382
00383 cpl_table_set_double(*npline_tbl,"XDIFREG",k,tmp_val_x);
00384 cpl_table_set_double(*npline_tbl,"YDIFREG",k,tmp_val_y);
00385
00386 }
00387
00388 cpl_table_add_columns(*npline_tbl,"XMOD","XDIFREG");
00389 cpl_table_add_columns(*npline_tbl,"YMOD","YDIFREG");
00390
00391
00392 cleanup:
00393 uves_polynomial_delete(&poly2d_x);
00394 uves_polynomial_delete(&poly2d_y);
00395 uves_free_table(&tmp_tbl);
00396 uves_free_table(&tmp_tbl1);
00397 uves_free_table(&tmp_tbl2);
00398 uves_free_table(&tmp_tbl3);
00399 uves_free_table(&tmp_tbl4);
00400
00401 uves_msg_debug("end msrawxy");
00402 return 0;
00403 }