58#define MODULE_ID "XSH_FLUX_CONSERVATION"
78 280., 0.3, -0.0025 } ;
79static const double step = 40. ;
81static int nx = 400,
ny = 400 ;
107 cpl_propertylist * header = NULL ;
108 double residavg = 0. ;
110 xsh_msg(
" ====== verify_order_tables" ) ;
115 check( hname = cpl_frame_get_filename( result ) ) ;
116 check( header = cpl_propertylist_load( hname, 0 ) ) ;
118 CPL_TYPE_DOUBLE, &residavg ) ) ;
120 assure( residavg < 1., CPL_ERROR_ILLEGAL_INPUT,
"Error Too large" ) ;
121 xsh_msg(
" Orders Detected OK, RESIDAVG = %lf", residavg ) ;
132 cpl_polynomial * poly0 = NULL ;
133 cpl_polynomial * poly1 = NULL ;
134 cpl_polynomial * poly2 = NULL ;
135 cpl_image *image = NULL ;
136 cpl_image *bias = NULL;
137 cpl_frame * img_frame = NULL, * tbl_frame = NULL, * dtc_frame = NULL,
138 * result_frame = NULL ;
144 cpl_propertylist * img_header ;
147 cpl_vector* xprofile=NULL;
148 cpl_vector* yprofile=NULL;
149 cpl_polynomial* poly_u=NULL;
150 cpl_polynomial* poly_v=NULL;
153 cpl_image* warped=NULL;
154 cpl_image* wone=NULL;
156 cpl_image* dXdx=NULL;
157 cpl_image* dYdy=NULL;
158 cpl_image* dXdy=NULL;
159 cpl_image* dYdx=NULL;
160 cpl_image* corr=NULL;
162 cpl_vector* sup=NULL;
164 cpl_vector* inf=NULL;
182 cpl_frame* resid_frame=NULL;
202 poly0 = cpl_polynomial_new(
dimension ) ;
203 poly1 = cpl_polynomial_new(
dimension ) ;
204 poly2 = cpl_polynomial_new(
dimension ) ;
210 cpl_polynomial_set_coeff( poly0, °,
poly0_coeff[deg] ) ;
214 for( deg = 1 ; deg<=
degree ; deg++ ) {
215 cpl_polynomial_set_coeff( poly0, °,
poly0_coeff[deg] ) ;
216 cpl_polynomial_set_coeff( poly1, °,
poly0_coeff[deg] ) ;
217 cpl_polynomial_set_coeff( poly2, °,
poly0_coeff[deg] ) ;
221 xsh_msg(
"Add to order list" ) ;
229 img_frame = cpl_frame_new() ;
233 cpl_image_save( image,
img_name, CPL_BPP_IEEE_DOUBLE, img_header,
236 cpl_frame_set_filename( img_frame,
img_name ) ;
237 cpl_frame_set_tag( img_frame,
"ORDERDEF_VIS_D2" );
238 cpl_frame_set_level( img_frame, CPL_FRAME_LEVEL_TEMPORARY);
239 cpl_frame_set_group( img_frame, CPL_FRAME_GROUP_RAW ) ;
246 "Cant save pre structure" ) ;
248 cpl_frame_set_tag( dtc_frame,
"ORDERDEF_UVB_D2" );
249 cpl_frame_set_level( dtc_frame, CPL_FRAME_LEVEL_TEMPORARY);
250 cpl_frame_set_group( dtc_frame, CPL_FRAME_GROUP_RAW ) ;
257 poly_u=cpl_polynomial_new(2);
258 poly_v=cpl_polynomial_new(2);
263 cpl_polynomial_set_coeff(poly_v,pows,
poly0_coeff[0]);
268 cpl_polynomial_set_coeff(poly_v,pows,-
poly0_coeff[1]);
273 cpl_polynomial_set_coeff(poly_v,pows,-
poly0_coeff[2]);
278 cpl_polynomial_set_coeff(poly_u,pows,1.);
279 cpl_polynomial_dump(poly_u,stdout);
280 cpl_polynomial_dump(poly_v,stdout);
282 check(warped=cpl_image_new(
nx,
ny,CPL_TYPE_FLOAT));
283 check(one=cpl_image_new(
nx,
ny,CPL_TYPE_FLOAT));
284 check(cpl_image_add_scalar(one,1.));
285 check(xprofile=cpl_vector_new(
nx));
286 check(yprofile=cpl_vector_new(
ny));
287 cpl_vector_fill_kernel_profile(xprofile, CPL_KERNEL_DEFAULT,
288 CPL_KERNEL_DEF_WIDTH);
289 cpl_vector_fill_kernel_profile(yprofile, CPL_KERNEL_DEFAULT,
290 CPL_KERNEL_DEF_WIDTH);
302 check(cpl_image_divide(warped,wone));
305 check(f_org=cpl_image_get_flux(img_pre->
data));
306 check(f_bad=cpl_image_get_flux_window(warped,1,140,
nx,216));
308 xsh_msg(
"Flux PRE frame: %g",f_org);
309 xsh_msg(
"Flux warp frame: %g",f_bad);
312 check(dXdx=cpl_image_new(
nx,
ny,CPL_TYPE_FLOAT));
313 check(dYdy=cpl_image_new(
nx,
ny,CPL_TYPE_FLOAT));
314 check(dXdy=cpl_image_new(
nx,
ny,CPL_TYPE_FLOAT));
315 check(dYdx=cpl_image_new(
nx,
ny,CPL_TYPE_FLOAT));
316 check(corr=cpl_image_new(
nx,
ny,CPL_TYPE_FLOAT));
318 check(pdXdx=cpl_image_get_data_float(dXdx));
319 check(pdYdy=cpl_image_get_data_float(dYdy));
320 check(pdXdy=cpl_image_get_data_float(dXdy));
321 check(pdYdx=cpl_image_get_data_float(dYdx));
322 check(pcor=cpl_image_get_data_float(corr));
324 check(sup=cpl_vector_new(2));
325 check(psup=cpl_vector_get_data(sup));
326 check(inf=cpl_vector_new(2));
327 check(pinf=cpl_vector_get_data(inf));
330 for(j=1;j<
ny-1;j++) {
331 for(i=1;i<
nx-1;i++) {
332 psup[0]=(double)(i+1);
335 pinf[0]=(double)(i-1);
353 psup[1]=(double)(j+1);
356 pinf[1]=(double)(j-1);
380 pcor[i+j*
nx]=fabs(pdXdx[i+j*
nx]*pdYdy[i+j*
nx]-
381 pdXdy[i+j*
nx]*pdYdx[i+j*
nx]);
386 check(cpl_image_save(warped,
"order_warped.fits",CPL_BPP_IEEE_FLOAT,NULL,
388 check(cpl_image_save(wone,
"wone.fits",CPL_BPP_IEEE_FLOAT,NULL,
390 check(cpl_image_save(corr,
"corr.fits",CPL_BPP_IEEE_FLOAT,NULL,
392 cpl_image_add_scalar(corr,add);
393 check(cpl_image_divide(warped,corr));
394 f_cor=cpl_image_get_flux_window(corr,1,140,
nx,216);
397 xsh_msg(
"Flux add frame: %g",f_add);
398 f_end=cpl_image_get_flux_window(warped,1,140,
nx,216);
401 xsh_msg(
"Flux corrected frame: %g",f_end);
402 xsh_msg(
"Flux predicted frame: %g",f_end*(f_add+f_cor)/f_cor);
405 check(cpl_image_save(warped,
"corrected.fits",CPL_BPP_IEEE_FLOAT,NULL,
409 dcn_clipping.
sigma = 2.5 ;
410 dcn_clipping.
niter = 5 ;
411 dcn_clipping.
frac = 0.7 ;
467 if (cpl_error_get_code() != CPL_ERROR_NONE) {
static const char * tbl_name
static double derivative_x(const double a1, const double a2, const double x)
static void verify_order_table(cpl_frame *result)
static double derivative_y(const double a1, const double a2, const double y)
static const char * dtc_pre_name
static const double poly0_coeff[]
Unit test of XSH_DETECT_CONTINUUM.
static const char * img_name
static xsh_instrument * instrument
xsh_order_list * create_order_list(int norder, xsh_instrument *instrument)
void add_to_order_list(xsh_order_list *list, int order, int absorder, cpl_polynomial *poly, int xdelta, int starty, int endy)
cpl_image * xsh_test_create_bias_image(const char *name, int nx, int ny, xsh_instrument *instrument)
cpl_image * create_order_image(xsh_order_list *list, int nx, int ny)
cpl_propertylist * mkHeader(XSH_INSTRCONFIG *iconfig, int nx, int ny, double exptime)
cpl_frame * xsh_order_list_save(xsh_order_list *order_list, xsh_instrument *instrument, const char *filename, const char *tag, const int ny)
Save an order list to a frame.
void xsh_order_list_dump(xsh_order_list *list, const char *fname)
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
xsh_pre * xsh_pre_create(cpl_frame *raw, cpl_frame *bpmap, cpl_image *bias_data, xsh_instrument *instr, const int pre_overscan_corr, const bool flag_neg_and_thresh_pix)
Create a XSH_PRE from a raw frame.
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
cpl_frame * xsh_detect_continuum(cpl_frame *frame, cpl_frame *order_table, cpl_frame *spectral_frame, xsh_detect_continuum_param *detect_param, xsh_clipping_param *dcn_clipping, xsh_instrument *instr, cpl_frame **resid_frame)
Detect order and compute polynomial description of ordermin and order max. Uses a guess order table i...
#define assure(CONDITION, ERROR_CODE,...)
#define xsh_error_dump(level)
#define check_msg(COMMAND,...)
void xsh_instrument_set_mode(xsh_instrument *i, XSH_MODE mode)
Set a mode on instrument structure.
void xsh_instrument_set_recipe_id(xsh_instrument *instrument, const char *recipe_id)
Set the recipe_id into the instrument structure.
void xsh_instrument_set_arm(xsh_instrument *i, XSH_ARM arm)
Set an arm on instrument structure.
XSH_INSTRCONFIG * xsh_instrument_get_config(xsh_instrument *i)
Get the instrument default set of keywords.
void xsh_instrument_set_lamp(xsh_instrument *i, XSH_LAMP lamp)
Set a lamp on instrument structure.
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
xsh_instrument * xsh_instrument_new(void)
create new instrument structure
#define xsh_msg(...)
Print a message on info level.
void xsh_free_polynomial(cpl_polynomial **p)
Deallocate a polynomial and set the pointer to NULL.
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
cpl_error_code xsh_get_property_value(const cpl_propertylist *plist, const char *keyword, cpl_type keywordtype, void *result)
Read a property value from a property list.
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
int xsh_debug_level_set(int level)
set debug level
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
#define TESTS_CLEAN_WORKSPACE(DRL_ID)
#define TESTS_INIT_WORKSPACE(DRL_ID)
#define TESTS_INIT(DRL_ID)
#define QC_ORD_ORDERPOS_RESIDAVG
cpl_image * xsh_warp_image_generic(cpl_image *image_in, char *kernel_type, cpl_polynomial *poly_u, cpl_polynomial *poly_v)