64#define MODULE_ID "XSH_LAMBDA_ERR"
73 {
"debug", required_argument, 0,
DEBUG_OPT},
80 puts(
"Unitary test of xsh_lambda_err");
81 puts(
"Usage: test_xsh_lambda_err [options] <input_files>");
84 puts(
" --order-min=<n> : Minimum abs order" );
85 puts(
" --order-max=<n> : Maximum abs order" );
86 puts(
" --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
87 puts(
" --help : What you see" ) ;
88 puts(
"\nInput Files" ) ;
89 puts(
"The input files argument MUST be in this order:" ) ;
90 puts(
" 1. Science frame in PRE format" ) ;
91 puts(
" 2. Localization table" );
92 puts(
" 3. SOF [SPECTRAL_FORMAT, WAVEMAP, SLITMAP]\n" ) ;
97 int *order_min,
int *order_max)
100 int option_index = 0;
102 while (( opt = getopt_long (argc, argv,
"oversample:box-hsize:chunk-size",
107 sscanf( optarg,
"%64d", order_min);
110 sscanf( optarg,
"%64d", order_max);
113 if ( strcmp( optarg,
"LOW")==0){
116 else if ( strcmp( optarg,
"HIGH")==0){
139int main(
int argc,
char **argv)
144 const char *sof_name = NULL;
145 cpl_frameset *set = NULL;
147 const char* sci_name = NULL;
148 const char* loc_name = NULL;
151 int rec_min_index = -1;
152 int rec_max_index = -1;
155 cpl_propertylist *header = NULL;
156 const char* pcatg = NULL;
159 float *pre_flux = NULL;
160 float *pre_errs = NULL;
162 cpl_image *wavemap = NULL;
163 cpl_image *slitmap = NULL;
164 float* wavemap_data = NULL;
165 float* slitmap_data = NULL;
175 const char *prefix = NULL;
177 cpl_frame *sci_frame = NULL;
178 cpl_frame *loc_frame = NULL;
179 cpl_frame *orderlist_frame = NULL;
180 cpl_frame *wavemap_frame = NULL;
181 cpl_frame *slitmap_frame = NULL;
182 cpl_frame *spectralformat_frame = NULL;
186 cpl_msg_set_level( CPL_MSG_DEBUG);
192 if ( (argc - optind) >= 3 ) {
193 sci_name = argv[optind];
194 loc_name = argv[optind+1];
195 sof_name = argv[optind+2];
217 cpl_frame_get_filename( sci_frame));
219 cpl_frame_get_filename( loc_frame));
221 cpl_frame_get_filename( orderlist_frame));
223 cpl_frame_get_filename( spectralformat_frame));
225 cpl_frame_get_filename( wavemap_frame));
227 cpl_frame_get_filename( slitmap_frame));
229 pch=strrchr( sci_name,
'/');
239 if ( order_min != -1) {
242 xsh_msg(
"Order min %d => index %d", order_min, rec_min_index);
248 if ( order_max != -1) {
251 xsh_msg(
"Order max %d => index %d", order_max, rec_max_index);
254 rec_max_index = order_list->
size-1;
258 check( wavemap = cpl_image_load( cpl_frame_get_filename( wavemap_frame),
259 CPL_TYPE_FLOAT, 0, 0));
260 check( slitmap = cpl_image_load( cpl_frame_get_filename( slitmap_frame),
261 CPL_TYPE_FLOAT, 0, 0));
263 check(
nx = cpl_image_get_size_x( wavemap));
264 check( wavemap_data = cpl_image_get_data_float( wavemap));
265 check( slitmap_data = cpl_image_get_data_float( slitmap));
268 obj_slit = cpl_polynomial_eval_1d( loc_list->
cenpoly,
271 xsh_msg(
"Localization center is around %f arcsec", obj_slit);
273 check( header = cpl_propertylist_load( sci_name, 0));
276 if (strstr( pcatg,
"ORDER2D") != NULL){
279 float *slitdata=NULL;
289 for( iorder=rec_min_index; iorder<= rec_max_index; iorder++){
291 double *lambdadata = NULL;
292 float *flux = NULL, *errs = NULL;
302 sprintf( name,
"%s_lambda_err_%d.dat", prefix, abs_order);
306 file = fopen( name,
"w");
308 fprintf( file,
"#lambda flux err sn\n");
310 for( ilambda =0; ilambda < nlambda; ilambda++){
312 double flux_v, err_v;
315 lambda_v = lambdadata[ilambda];
316 flux_v = flux[ilambda+nlambda*
s];
317 err_v = errs[ilambda+nlambda*
s];
319 fprintf( file,
"%f %f %f %f\n", lambda_v, flux_v, err_v,
326 else if( strstr( pcatg,
"MERGE2D") != NULL){
337 sprintf( name,
"%s_lambda_err.dat", prefix);
338 file = fopen( name,
"w");
339 fprintf( file,
"#lambda flux err sn\n");
341 for(ilambda=0; ilambda< spectrum->
size_lambda; ilambda++){
343 double flux_v, err_v;
350 fprintf( file,
"%f %f %f %f\n", lambda_v, flux_v, err_v, sn);
357 check( pre_errs = cpl_image_get_data_float( pre->
errs));
358 check( pre_flux = cpl_image_get_data_float( pre->
data));
360 xsh_msg(
"Work with frame in PRE format binning: %dx%d", pre->
binx, pre->
biny);
366 for( iorder=rec_min_index; iorder<= rec_max_index; iorder++){
367 int abs_order, start_y, end_y;
374 sprintf( name,
"%s_lambda_err_%d.dat", prefix, abs_order);
376 file = fopen( name,
"w");
377 fprintf( file,
"#lambda flux err sn\n");
379 for(
y=start_y;
y <= end_y;
y++){
385 int xlow, xup, xnear=0;
392 for(
x=xlow-1;
x < xup;
x++){
393 slit = slitmap_data[
x+(
y-1)*
nx];
394 if ( fabs( slit-obj_slit) < diff){
395 if ( wavemap_data[
x+(
y-1)*
nx] > 0){
396 diff = fabs( slit-obj_slit);
401 lambda = wavemap_data[xnear+(
y-1)*
nx];
403 err = pre_errs[xnear+(
y-1)*
nx];
404 flux = pre_flux[xnear+(
y-1)*
nx];
406 fprintf( file,
"%f %f %f %f\n", lambda, flux, err, sn);
409 xsh_msg(
"Skipping Lambda %f for x %d y %d", lambda, xnear,
y);
417 if (cpl_error_get_code() != CPL_ERROR_NONE) {
int main()
Unit test of xsh_bspline_interpol.
static xsh_instrument * instrument
cpl_frameset * sof_to_frameset(const char *sof_name)
xsh_localization * xsh_localization_load(cpl_frame *frame)
Load a localization list from a frame.
void xsh_localization_free(xsh_localization **list)
free memory associated to a localization_list
void xsh_order_list_set_bin_y(xsh_order_list *list, int bin)
Set the bin of image in y.
int xsh_order_list_get_index_by_absorder(xsh_order_list *list, double absorder)
void xsh_order_list_set_bin_x(xsh_order_list *list, int bin)
Set the bin of image in x.
int xsh_order_list_eval_int(xsh_order_list *list, cpl_polynomial *poly, double y)
Evaluate an order list poly but return the central pixel position rounding the polynomial.
xsh_order_list * xsh_order_list_load(cpl_frame *frame, xsh_instrument *instr)
load an order list from a frame
int xsh_order_list_get_starty(xsh_order_list *list, int i)
get position on Y axis of first pixel detected on order
int xsh_order_list_get_endy(xsh_order_list *list, int i)
get position on Y axis of last pixel detected on order
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
int xsh_rec_list_get_nslit(xsh_rec_list *list, int idx)
double * xsh_rec_list_get_lambda(xsh_rec_list *list, int idx)
xsh_rec_list * xsh_rec_list_load(cpl_frame *frame, xsh_instrument *instrument)
load an rec list from a frame
float * xsh_rec_list_get_data1(xsh_rec_list *list, int idx)
float * xsh_rec_list_get_errs1(xsh_rec_list *list, int idx)
int xsh_rec_list_get_order(xsh_rec_list *list, int idx)
float * xsh_rec_list_get_slit(xsh_rec_list *list, int idx)
int xsh_rec_list_get_nlambda(xsh_rec_list *list, int idx)
void xsh_rec_list_free(xsh_rec_list **list)
free memory associated to a rec_list
xsh_spectrum * xsh_spectrum_load(cpl_frame *s1d_frame)
Load a 1D spectrum structure.
double * xsh_spectrum_get_errs(xsh_spectrum *s)
Get errs of spectrum.
double * xsh_spectrum_get_flux(xsh_spectrum *s)
Get flux of spectrum.
void xsh_spectrum_free(xsh_spectrum **s)
free memory associated to an 1D spectrum
#define xsh_error_dump(level)
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
#define xsh_msg(...)
Print a message on info level.
double xsh_pfits_get_rectify_bin_space(cpl_propertylist *plist)
find out the rectify space (slit) binning
const char * xsh_pfits_get_pcatg(const cpl_propertylist *plist)
find out the pcatg
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
int xsh_debug_level_set(int level)
set debug level
long xsh_round_double(double x)
Computes round(x)
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
#define TESTS_XSH_FRAME_CREATE(frame, tag, name)
#define TESTS_INIT(DRL_ID)
cpl_frame * xsh_find_wavemap(cpl_frameset *frames, xsh_instrument *instr)
Find Wave Map frame. The frame returned should not be free by the caller.
cpl_frame * xsh_find_spectral_format(cpl_frameset *frames, xsh_instrument *instr)
Find spectral format frame.
cpl_frame * xsh_find_order_tab_edges(cpl_frameset *frames, xsh_instrument *instr)
Find an order tab EDGES.
cpl_frame * xsh_find_slitmap(cpl_frameset *frames, xsh_instrument *instr)
Find a slit map.
xsh_instrument * xsh_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset and return the instrument detected.