63#define MODULE_ID "XSH_DATA_SPECTRUM_MERGE_3D"
78 puts(
"Creates for each slitlet:");
79 puts(
" - a median profile along the slit");
80 puts(
" - a localization as a function of wavelength");
82 puts(
"Usage: test_xsh_data_spectrum_merge_3d [options] CUBE SKY_LINE");
85 puts(
" --lambda-chunk-hsize=<n> : Chunk half size in pixel [50]");
87 puts(
"\nInput Files" ) ;
88 puts(
"CUBE : data cube (tag = *_IFU_MERGE3D_IFU_UVB)" );
89 puts(
"SKY_LINE [OPTIONAL] : sky lines (tag = SKY_LINE_LIST_arm)" );
91 puts(
"\nOutput Files");
92 puts(
"1) MEDIAN_CUBE.fits : median profile in fits image");
93 puts(
"2) median_profile_slitbin_*.dat : ASCII profile along the slit direction for every slitlet by collapsing in wavelength");
94 puts(
"3) profile_lambda_*.dat : ASCII file containing for each chunk around lref gaussian central fitting positions");
95 puts(
"4) gsl_locfit_*.dat : ASCII central slit position for each slitlet");
104 int option_index = 0;
106 while (( opt = getopt_long (argc, argv,
Options,
110 *zchunk_hsize = atoi( optarg);
112 default:
Help() ; exit( 0);
126int main(
int argc,
char **argv)
130 const char* sci_name = NULL;
131 cpl_frame *sci_frame = NULL;
133 const char* sky_name = NULL;
134 cpl_frame *sky_frame = NULL;
138 int cube_x, cube_y, cube_z;
139 float *cube_data = NULL;
140 double *median = NULL;
143 int zchunk_hsize = 100;
144 cpl_vector *med_vect = NULL;
145 cpl_vector *coadd_vect = NULL;
146 cpl_vector *positions = NULL;
149 cpl_image *med_img = NULL;
150 double *med_img_data = NULL;
151 FILE *profil_file = NULL;
154 cpl_propertylist *header = NULL;
155 double crval1, crval2, crval3;
156 double crpix1, crpix2, crpix3;
157 double cdelt1, cdelt2, cdelt3;
158 double x0= 0.0,
sigma =0.0, area=0.0, offset=0.0;
164 cpl_msg_set_level( CPL_MSG_DEBUG);
170 if (argc-optind >= 1 ) {
171 sci_name = argv[optind];
172 if (argc-optind >= 2 ) {
173 sky_name = argv[optind+1];
182 if ( sky_name != NULL){
188 cpl_frame_get_filename( sci_frame));
189 if ( sky_frame != NULL){
191 cpl_frame_get_filename( sky_frame));
194 xsh_msg(
"Don't use sky line list!!");
208 xsh_msg(
" Cube SLITLET*SLIT*LAMBDAS (%dx%dx%d)", cube_x, cube_y, cube_z);
222 xsh_msg(
" SLITLET pix %f ref %f with step %f", crpix1, crval1, cdelt1);
223 xsh_msg(
" SLIT pix %f ref %f with step %f", crpix2, crval2, cdelt2);
224 xsh_msg(
" LAMBDAS pix %f ref %f with step %f", crpix3, crval3, cdelt3);
230 check( med_vect = cpl_vector_wrap( cube_z, median));
231 check( med_img = cpl_image_new( cube_x, cube_y, CPL_TYPE_DOUBLE));
232 check( med_img_data = cpl_image_get_data_double( med_img));
234 for(
y=0;
y<cube_y;
y++){
235 for(
x=0;
x<cube_x;
x++){
236 for(z=0; z< cube_z; z++){
237 median[z] = cube_data[cube_x*cube_y*z+cube_x*
y+
x];
239 check (med_z = cpl_vector_get_median( med_vect));
240 med_img_data[
y*cube_x+
x] = med_z;
243 check( cpl_image_save( med_img,
"MEDIAN_CUBE.fits", CPL_BPP_IEEE_DOUBLE,
244 NULL, CPL_IO_CREATE));
246 sprintf( filename,
"median_profile_slitbin_%.3f.dat", cdelt2);
247 profil_file = fopen( filename,
"w");
248 fprintf( profil_file,
"#slit UP CEN LO\n");
250 for(
y=0;
y<cube_y;
y++){
251 fprintf( profil_file,
"%f ", crval2+
y*cdelt2);
252 for(
x=0;
x<cube_x;
x++){
253 fprintf( profil_file,
"%f ", med_img_data[
y*cube_x+
x]);
255 fprintf( profil_file,
"\n");
257 fclose( profil_file);
260 check( coadd_vect = cpl_vector_wrap( cube_y, coadd));
261 check( positions = cpl_vector_new( cube_y));
263 for(
y=0;
y<cube_y;
y++){
264 cpl_vector_set( positions,
y,
y);
265 cpl_vector_set( coadd_vect,
y, med_img_data[
y*cube_x+1]);
267 cpl_vector_fit_gaussian( positions, NULL, coadd_vect, NULL,CPL_FIT_ALL,&x0,&
sigma,&area,&offset,NULL,NULL,NULL);
269 if (cpl_error_get_code() != CPL_ERROR_NONE){
273 sprintf( filename,
"gaussian_fit.dat");
274 profil_file = fopen( filename,
"w");
275 fprintf( profil_file,
"#slit cen gauss_cen\n");
277 for(
y=0;
y<cube_y;
y++){
279 double cen_val = 0.0;
280 double gauss_val = 0.0;
285 sval = crval2+
y*cdelt2;
286 cen_val = cpl_vector_get( coadd_vect,
y);
288 fprintf( profil_file,
"%f %f %f\n", sval, cen_val, gauss_val);
291 fclose( profil_file);
292 xsh_msg(
" Median cube central slitlet gaussian fit %f arcsec\n", crval2+x0*cdelt2);
296 sprintf( filename,
"profile_lambda_%d.dat", zchunk_hsize);
298 profil_file = fopen( filename,
"w");
304 fprintf( profil_file,
"#lref up cen low\n");
305 for(z=zchunk_hsize; z< cube_z; z+=zchunk_hsize*2){
308 for(
x=0;
x<cube_x;
x++){
310 for(
y=0;
y<cube_y;
y++){
313 for(zmed=z-zchunk_hsize; zmed< z+zchunk_hsize; zmed++){
314 for(
y=0;
y<cube_y;
y++){
315 coadd[
y] += cube_data[cube_x*cube_y*zmed+cube_x*
y+
x];
318 cpl_vector_fit_gaussian( positions, NULL, coadd_vect, NULL,CPL_FIT_ALL,&x0,&
sigma,&area,&offset,NULL,NULL,NULL);
319 if (cpl_error_get_code() != CPL_ERROR_NONE){
327 cen[
x] = crval2+x0*cdelt2;
329 median[median_size] = cen[1];
334 fprintf( profil_file,
"%f %f %f %f\n", crval3+z*cdelt3, cen[0], cen[1], cen[2]);
336 fclose( profil_file);
337 check( med_vect = cpl_vector_wrap( median_size, median));
338 med_z = cpl_vector_get_median( med_vect);
339 xsh_msg(
" Statistics of gaussian fit bad %d lines, good %d lines",
340 bad_fit, median_size);
341 xsh_msg(
" Median from gaussian fit %f arcsec\n", med_z);
344 for(
x=0;
x< cube_x;
x++){
348 float slitcen, fit_err;
350 sprintf( filename,
"gsl_localize_pos%d.dat",
x);
351 profil_file = fopen( filename,
"w");
352 fprintf( profil_file,
"#wavelength slit_fit fit_err\n");
354 for( z=0; z < cube_z; z++){
355 for(
y=0;
y < cube_y;
y++){
356 cpl_vector_set( coadd_vect,
y, cube_data[cube_x*cube_y*z+cube_x*
y+
x]);
360 slitcen = crval2+init_par[4]*cdelt2;
361 fit_err = fit_errs[4]*cdelt2;
362 fprintf( profil_file,
"%f %f %f\n", crval3+z*cdelt3, slitcen, fit_err);
364 fclose( profil_file);
371 if (cpl_error_get_code() != CPL_ERROR_NONE) {
int main()
Unit test of xsh_bspline_interpol.
static xsh_instrument * instrument
static const char * Options
static void HandleOptions(int argc, char **argv, int *zchunk_hsize)
static struct option long_options[]
xsh_instrument * create_instrument(const char *filename)
void * xsh_image_3d_get_data(xsh_image_3d *img_3d)
int xsh_image_3d_get_size_x(xsh_image_3d *img_3d)
int xsh_image_3d_get_size_y(xsh_image_3d *img_3d)
int xsh_image_3d_get_size_z(xsh_image_3d *img_3d)
void xsh_pre_3d_free(xsh_pre_3d **pre_3d)
xsh_pre_3d * xsh_pre_3d_load(cpl_frame *frame)
Load a xsh_pre_3d structure from a frame.
xsh_image_3d * xsh_pre_3d_get_data(xsh_pre_3d *pre_3d)
Get data.
#define xsh_error_dump(level)
#define xsh_error_reset()
void xsh_instrument_set_mode(xsh_instrument *i, XSH_MODE mode)
Set a mode on instrument structure.
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
#define xsh_msg(...)
Print a message on info level.
double xsh_pfits_get_crval2(const cpl_propertylist *plist)
find out the crval2
double xsh_pfits_get_cdelt2(const cpl_propertylist *plist)
find out the cdelt2
double xsh_pfits_get_cdelt3(const cpl_propertylist *plist)
find out the cdelt3
double xsh_pfits_get_crpix1(const cpl_propertylist *plist)
find out the CRPIX1 value
double xsh_pfits_get_crpix3(const cpl_propertylist *plist)
find out the CRPIX3 value
double xsh_pfits_get_cdelt1(const cpl_propertylist *plist)
find out the cdelt1
double xsh_pfits_get_crpix2(const cpl_propertylist *plist)
find out the CRPIX2 value
double xsh_pfits_get_crval1(const cpl_propertylist *plist)
find out the crval1
double xsh_pfits_get_crval3(const cpl_propertylist *plist)
find out the crval3
void xsh_unwrap_vector(cpl_vector **v)
Unwrap a vector and set the pointer to NULL.
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.
int xsh_debug_level_set(int level)
set debug level
void xsh_gsl_init_gaussian_fit(cpl_vector *xpos_vect, cpl_vector *ypos_vect, double *init_par)
void xsh_gsl_fit_gaussian(cpl_vector *xpos_vect, cpl_vector *ypos_vect, int deg, double *params, double *errs, int *status)
cpl_propertylist * data_header
#define TESTS_XSH_FRAME_CREATE(frame, tag, name)
#define TESTS_INIT(DRL_ID)
#define XSH_FREE(POINTER)
#define XSH_MALLOC(POINTER, TYPE, SIZE)