67static void fill_img( cpl_image * img,
int ilambda, cpl_image * down,
68 cpl_image * cen, cpl_image * up,
int up_shift,
int cen_shift)
73 check(
nx = cpl_image_get_size_x( down ));
74 check(
ny = cpl_image_get_size_y( down));
77 for( j = 1 ; j <=
ny ; j++ ) {
81 check(flux = cpl_image_get( down, ilambda+1, j, &rej )) ;
82 check(cpl_image_set( img, 1, j+cen_shift, flux )) ;
85 check(
nx = cpl_image_get_size_x( cen));
86 check(
ny = cpl_image_get_size_y( cen));
88 for( j = 1 ; j <=
ny ; j++ ) {
92 check(flux = cpl_image_get( cen, ilambda+1, j, &rej )) ;
93 check(cpl_image_set( img, 2, j+cen_shift, flux ) );
96 check(
nx = cpl_image_get_size_x( up ));
97 check(
ny = cpl_image_get_size_y( up));
98 for( j = 1 ; j <=
ny ; j++ ) {
102 check(flux = cpl_image_get( up, ilambda+1, j, &rej )) ;
103 check(cpl_image_set( img, 3, j+up_shift, flux ) );
114 cpl_image * cur_img = NULL ;
122 xsh_msg(
"Build data cube: %d x %d x %d",
nx,
ny, nz);
124 for( i = 0 ; i<nz ; i++ ) {
127 check( cur_img = cpl_image_new(
nx,
ny, CPL_TYPE_FLOAT ) ) ;
129 up_shift,cen_shift)) ;
133 check( cur_img = cpl_image_new(
nx,
ny, CPL_TYPE_FLOAT ) ) ;
135 up_shift,cen_shift)) ;
139 check( cur_img = cpl_image_new(
nx,
ny, CPL_TYPE_INT ) ) ;
141 up_shift,cen_shift)) ;
154 const char* rec_prefix)
156 cpl_frame * result = NULL ;
160 xsh_pre * pre_down = NULL, * pre_cen = NULL, * pre_up = NULL ;
161 cpl_frame * frame_down = NULL, * frame_cen = NULL, * frame_up = NULL ;
162 int nslitlet = 3, nslit, nlambda ;
163 int nslit_up, nslit_lo, nslit_cen;
168 double crval2 = 0.0, cdelt2=0.0;
175 check( frame_down = cpl_frameset_get_frame( spectrum_frame_set, 0));
177 check( frame_cen = cpl_frameset_get_frame( spectrum_frame_set, 1));
179 check( frame_up = cpl_frameset_get_frame( spectrum_frame_set, 2));
187 xsh_msg(
"Rect Lambda min, max, bin: %lf, %lf, %lf", lambda_min, lambda_max,
198 if (nslit_up > nslit_cen){
199 nslit += nslit_up-nslit_cen;
200 crval2 -= (nslit_up-nslit_cen)*cdelt2;
202 if (nslit_lo > nslit_cen){
203 nslit += nslit_lo-nslit_cen;
208 xsh_msg(
"nslit : lo %d cen %d up %d",nslit_lo,nslit_cen,nslit_up);
211 xsh_msg(
"Pre_3d created: %dx%dx%d", nslitlet, nslit, nlambda);
225 pre_cen->data_header));
227 pre_cen->errs_header));
229 pre_cen->qual_header));
231 sprintf(pcatg,
"%s_%s",rec_prefix,
234 xsh_msg(
" Setting PRO.CATG '%s'", pcatg ) ;
257 xsh_msg(
"Saving %s", result_name) ;
261 sprintf(tag,
"%s_%s",rec_prefix,
264 xsh_msg(
" Setting TAG '%s'", tag);
265 check( cpl_frame_set_tag( result, tag));
266 check( cpl_frame_set_group( result, CPL_FRAME_GROUP_PRODUCT));
267 check( cpl_frame_set_level( result, CPL_FRAME_LEVEL_FINAL));
268 check(cpl_frame_set_type( result, CPL_FRAME_TYPE_IMAGE));
291 int *sky_mask = NULL;
292 const char* skymask_name = NULL;
293 cpl_table *skymask_table = NULL;
294 float *skymask_data = NULL;
296 double fwhm =0.0, sky_min, sky_max;
297 int isky_min, isky_max, imask;
303 if ( sky_line_frame != NULL){
305 check( skymask_name = cpl_frame_get_filename( sky_line_frame));
309 check( skymask_data = cpl_table_get_data_float( skymask_table,
311 check( nrow = cpl_table_get_nrow( skymask_table));
315 for( irow=0; irow < nrow; irow++){
316 fwhm = skymask_data[irow]/resolution;
317 sky_min = skymask_data[irow]-fwhm;
318 sky_max = skymask_data[irow]+fwhm;
321 for( imask=isky_min; imask <=isky_max; imask++){
327 if ( cpl_error_get_code() != CPL_ERROR_NONE){
348 int cube_x, cube_y, cube_z;
349 float *cube_data = NULL;
350 cpl_propertylist *header = NULL;
352 double crval2, crval3;
357 double cdelt2, cdelt3;
358 double *median = NULL;
360 cpl_vector *coadd_vect = NULL;
361 cpl_vector *positions = NULL;
362 double x0= 0.0,
sigma =0.0, area=0.0, offset=0.0;
363 int median_size, bad_fit, sky_line;
364 cpl_vector *med_vect = NULL;
366 int *sky_mask = NULL;
402 check( coadd_vect = cpl_vector_new( cube_y));
403 check( positions = cpl_vector_new( cube_y));
405 for(
y=0;
y<cube_y;
y++){
406 cpl_vector_set( positions,
y,
y);
409 for(z=0; z< (cube_z-chunk_size); z+=chunk_size){
410 for(
y=0;
y<cube_y;
y++){
411 cpl_vector_set( coadd_vect,
y, 0);
413 for( zmed=z; zmed< (z+chunk_size); zmed++){
414 if (sky_mask[zmed] == 0){
415 for(
y=0;
y<cube_y;
y++){
418 y_val = cpl_vector_get( coadd_vect,
y);
419 y_val += cube_data[cube_x*cube_y*zmed+cube_x*
y+1];
420 cpl_vector_set( coadd_vect,
y, y_val);
427 cpl_vector_fit_gaussian( positions, NULL, coadd_vect, NULL,CPL_FIT_ALL,
428 &x0,&
sigma,&area,&offset,NULL,NULL,NULL);
430 if (cpl_error_get_code() != CPL_ERROR_NONE){
437 slit_cen_fit = crval2+x0*cdelt2;
438 median[median_size] = slit_cen_fit;
442 xsh_msg(
" Statistics of gaussian fit bad %d lines, good %d lines, sky lines %d",
443 bad_fit, median_size, sky_line);
444 check( med_vect = cpl_vector_wrap( median_size, median));
445 shift_z = cpl_vector_get_median( med_vect);
446 xsh_msg(
" Measured object shift from gaussian fit: %f arcsec\n", shift_z);
472cpl_frame*
xsh_cube( cpl_frameset *merge2d_frameset,
474 const char* rec_prefix)
477 cpl_frame *down_frame = NULL, *cen_frame = NULL, *up_frame = NULL;
478 cpl_frame *result = NULL;
479 xsh_pre *pre_down = NULL, *pre_cen = NULL, *pre_up = NULL;
480 int nslitlet = 3, nslit, nlambda;
482 double crpix2=0.0, crval2=0.0, cdelt2=0.0;
483 double crpix3=0.0, crval3=0.0, cdelt3=0.0;
485 char result_name[256];
486 double waveref, sref_down, sref_up, sref_cen;
492 check( down_frame = cpl_frameset_get_frame( merge2d_frameset, 0));
494 check( cen_frame = cpl_frameset_get_frame( merge2d_frameset, 1));
496 check( up_frame = cpl_frameset_get_frame( merge2d_frameset, 2));
517 pre_cen->data_header));
519 pre_cen->errs_header));
521 pre_cen->qual_header));
523 sprintf(pcatg,
"%s_%s",rec_prefix,
575 if ( cpl_error_get_code() == CPL_ERROR_NONE){
583 sprintf( result_name,
"%s.fits", pcatg);
587 check( cpl_frame_set_tag( result, pcatg));
588 check( cpl_frame_set_group( result, CPL_FRAME_GROUP_PRODUCT));
589 check( cpl_frame_set_level( result, CPL_FRAME_LEVEL_FINAL));
590 check(cpl_frame_set_type( result, CPL_FRAME_TYPE_IMAGE));
593 if ( cpl_error_get_code() != CPL_ERROR_NONE){
static xsh_instrument * instrument
void * xsh_image_3d_get_data(xsh_image_3d *img_3d)
cpl_error_code xsh_image_3d_insert(xsh_image_3d *img_3d, cpl_image *img, int iz)
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)
int xsh_pre_3d_get_nx(const xsh_pre_3d *pre)
Get nx of pre_3d structure.
xsh_pre_3d * xsh_pre_3d_load(cpl_frame *frame)
Load a xsh_pre_3d structure from a frame.
int xsh_pre_3d_get_nz(const xsh_pre_3d *pre)
Get nz of pre_3d structure.
cpl_frame * xsh_pre_3d_save(const xsh_pre_3d *pre, const char *filename, int temp)
Save PRE_3D on disk.
xsh_pre_3d * xsh_pre_3d_new(int nx, int ny, int nz)
Create new PRE image.
int xsh_pre_3d_get_ny(const xsh_pre_3d *pre)
Get ny of pre_3d structure.
xsh_image_3d * xsh_pre_3d_get_data(xsh_pre_3d *pre_3d)
Get data.
void xsh_pre_flip(xsh_pre *pre, int angle)
Flip an image on a given miror line.
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
int xsh_pre_get_ny(const xsh_pre *pre)
Get ny of pre structure.
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
int xsh_pre_get_nx(const xsh_pre *pre)
Get nx of pre structure.
#define xsh_error_reset()
#define XSH_ASSURE_NOT_NULL(pointer)
const char * xsh_instrument_mode_tostring(xsh_instrument *i)
Get the string associated with a mode.
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
double xsh_resolution_get(xsh_instrument *instrument, double slit)
Get the resoltion.
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_low(...)
#define xsh_msg_dbg_high(...)
cpl_error_code xsh_pfits_set_wcs2(cpl_propertylist *header, const double crpix2, const double crval2, const double cdelt2)
double xsh_pfits_get_crval2(const cpl_propertylist *plist)
find out the crval2
void xsh_pfits_set_shiftifu_slitcenref(cpl_propertylist *plist, double value)
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
double xsh_pfits_get_cdelt2(const cpl_propertylist *plist)
find out the cdelt2
double xsh_pfits_get_rectify_lambda_min(cpl_propertylist *plist)
find out the rectify lambda min
double xsh_pfits_get_shiftifu_lambdaref(cpl_propertylist *plist)
double xsh_pfits_get_cdelt3(const cpl_propertylist *plist)
find out the cdelt3
double xsh_pfits_get_rectify_bin_lambda(cpl_propertylist *plist)
find out the rectify lambda binning
void xsh_pfits_set_shiftifu_lambdaref(cpl_propertylist *plist, double value)
void xsh_pfits_set_shiftifu_slitdownref(cpl_propertylist *plist, double value)
cpl_error_code xsh_pfits_set_wcs3(cpl_propertylist *header, const double crpix3, const double crval3, const double cdelt3)
double xsh_pfits_get_shiftifu_slitref(cpl_propertylist *plist)
void xsh_pfits_set_cunit3(cpl_propertylist *plist, const char *value)
Write the CUNIT1 value.
void xsh_pfits_set_shiftifu_slitupref(cpl_propertylist *plist, double value)
double xsh_pfits_get_crpix1(const cpl_propertylist *plist)
find out the CRPIX1 value
cpl_error_code xsh_pfits_set_wcs1(cpl_propertylist *header, const double crpix1, const double crval1, const double cdelt1)
double xsh_pfits_get_cdelt1(const cpl_propertylist *plist)
find out the cdelt1
void xsh_pfits_set_cunit1(cpl_propertylist *plist, const char *value)
Write the CUNIT1 value.
void xsh_pfits_set_cunit2(cpl_propertylist *plist, const char *value)
Write the CUNIT2 value.
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
void xsh_pfits_set_crval2(cpl_propertylist *plist, double value)
Write the CRVAL2 value.
double xsh_pfits_get_rectify_lambda_max(cpl_propertylist *plist)
find out the rectify lambda max
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_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.
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
cpl_error_code xsh_sort_table_1(cpl_table *t, const char *column, cpl_boolean reverse)
Sort a table by one column.
cpl_error_code xsh_set_cd_matrix3d(cpl_propertylist *plist)
Set CD matrix.
long xsh_round_double(double x)
Computes round(x)
cpl_propertylist * data_header
cpl_propertylist * errs_header
cpl_propertylist * qual_header
cpl_propertylist * data_header
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
#define XSH_FREE(POINTER)
#define XSH_MALLOC(POINTER, TYPE, SIZE)
#define XSH_CALLOC(POINTER, TYPE, SIZE)
#define XSH_TABLE_LOAD(TABLE, NAME)
#define XSH_TABLE_FREE(TABLE)