29#define REGDEBUG_BSPLINE 0
57#include <gsl/gsl_multifit.h>
77 if ( fname == NULL ) fout = stdout ;
78 else fout = fopen( fname,
"w" ) ;
81 fprintf( fout,
"Wavemap List. Nb of orders: %d\n", list->
size ) ;
82 for( i = 0 ; i<list->
size ; i++ ) {
83 fprintf( fout,
" Entry %2d: Order %d, Ndata: %d\n",
88 if ( fname != NULL && fout != NULL ) fclose( fout ) ;
127 for( i = 0 ; i<result->
size ; i++ ) {
132 if (cpl_error_get_code() != CPL_ERROR_NONE) {
150 int absorder,
int max_size)
157 pwavemap = &list->
list[idx] ;
160 pwavemap->
order = absorder;
184 if (list != NULL && *list != NULL ) {
187 for (i = 0; i < plist->
size; i++) {
191 if ( pr == NULL ) continue ;
205 cpl_free((*list)->list);
218 double xmin, xmax, ymin, ymax, lmin, lmax;
219 double *tcheb_lambda = NULL, *tcheb_xpos = NULL, *tcheb_ypos = NULL;
220 int nbcoefs, xdeg, ydeg;
221 cpl_vector *tcheb_coef_x = NULL;
222 cpl_vector *tcheb_coef_y = NULL;
225 gsl_matrix *X = NULL, *cov = NULL;
226 gsl_vector *
y = NULL, *c = NULL;
227 gsl_multifit_linear_workspace *work = NULL;
228 cpl_polynomial* result = NULL;
241 xdeg = dispsol_param->
deg_x;
242 ydeg = dispsol_param->
deg_y;
243 nbcoefs = (xdeg+1)*(ydeg+1) ;
263 " Min,Max. Lambda: %.6lf,%.6lf - X: %.6lf,%.6lf - Y: %.6lf,%.6lf",
264 lmin, lmax, xmin, xmax, ymin, ymax);
277 X = gsl_matrix_alloc(
size, nbcoefs);
278 y = gsl_vector_alloc(
size);
279 c = gsl_vector_alloc( nbcoefs);
280 cov = gsl_matrix_alloc( nbcoefs, nbcoefs);
282 for (i = 0; i <
size; i++) {
284 xdeg, tcheb_xpos[i]));
286 ydeg, tcheb_ypos[i]));
288 for(j = 0; j < dispsol_param->
deg_x +1 ; j++) {
289 for(k = 0; k < dispsol_param->
deg_y +1 ; k++){
292 check( vx = cpl_vector_get( tcheb_coef_x, j));
293 check( vy = cpl_vector_get( tcheb_coef_y, k));
294 gsl_matrix_set (X, i, k+j*(xdeg+1), vx*vy );
297 gsl_vector_set (
y, i, tcheb_lambda[i]);
306 "Not enough Points vs Number of Coeffs" ) ;
307 work = gsl_multifit_linear_alloc(
size, nbcoefs);
308 gsl_multifit_linear(X,
y, c, cov, &chisq, work);
316 for(i = 0; i < (xdeg +1) ; i++){
317 for(j = 0; j < (ydeg +1) ; j++){
320 cpl_polynomial_set_coeff(result, pows,
321 gsl_vector_get(c, j+i*(ydeg+1)));
326 gsl_multifit_linear_free (work);
330 gsl_matrix_free (cov);
373 int i, ordersize, nborder ;
374 double *vx = NULL, *vy = NULL, *vl = NULL, *vs = NULL;
377 xsh_msg(
"Entering xsh_wavemap_compute" ) ;
389 xsh_msg(
" X degree = %d, Y degree = %d", dispsol_param->
deg_x,
390 dispsol_param->
deg_y) ;
399 xsh_msg(
"Compute POLY for lambda");
402 for( i = 1 ; i<=nitems ; i++ ) {
404 if ( i < nitems && orders[i-1] == orders[i] ) {
408 cpl_vector* posx = NULL;
409 cpl_vector* posy = NULL;
410 cpl_vector* lvalues = NULL;
411 cpl_vector* svalues = NULL;
412 cpl_bivector *positions = NULL;
416 int i_minus_ordersize=i-ordersize;
418 memcpy( vx, &(xpos[i_minus_ordersize]), ordersize*
sizeof(
double));
421 memcpy( vy, &(ypos[i_minus_ordersize]), ordersize*
sizeof(
double));
424 memcpy( vl, &(vlambda[i_minus_ordersize]), ordersize*
sizeof(
double) ) ;
427 memcpy( vs, &( vslit[i_minus_ordersize]), ordersize*
sizeof(
double) ) ;
431 wmap->
list[nborder].
order = orders[i-1] ;
435 posx = cpl_vector_wrap( ordersize, vx);
436 posy = cpl_vector_wrap( ordersize, vy);
438 positions = cpl_bivector_wrap_vectors( posx, posy);
439 lvalues = cpl_vector_wrap( ordersize, vl);
440 svalues = cpl_vector_wrap( ordersize, vs);
441 cpl_size loc_degx=(cpl_size)dispsol_param->
deg_x;
450 cpl_bivector_unwrap_vectors( positions);
452 cpl_vector_unwrap( posx);
453 cpl_vector_unwrap( posy);
454 cpl_vector_unwrap( lvalues);
455 cpl_vector_unwrap( svalues);
499 int i, ordersize, nborder ;
500 double *vx = NULL, *vy = NULL, *vl = NULL;
502 xsh_msg(
"Entering xsh_wavemap_compute" ) ;
513 xsh_msg(
" X degree = %d, Y degree = %d", dispsol_param->
deg_x,
514 dispsol_param->
deg_y) ;
525 for( i = 1 ; i<=nitems ; i++ ) {
527 if ( i < nitems && orders[i-1] == orders[i] ) {
533 memcpy( vx, &(xpos[i-ordersize]), ordersize*
sizeof(
double));
536 memcpy( vy, &(ypos[i-ordersize]), ordersize*
sizeof(
double));
539 memcpy( vl, &(vlambda[i-ordersize]), ordersize*
sizeof(
double) ) ;
543 wmap->
list[nborder].
order = orders[i-1] ;
547 check(
lambda_fit( vl, vx, vy, ordersize, wmap, dispsol_param, nborder ) ) ;
577 double x,
double y,
int ord )
579 double tcheb_x = 0.0, tcheb_y = 0.0;
585 cpl_vector *tcheb_coef_x = NULL;
586 cpl_vector *tcheb_coef_y = NULL;
612 for ( i=0; i < degx+1; i++) {
613 for( j=0; j < degy+1; j++) {
617 check( vx = cpl_vector_get( tcheb_coef_x, i));
618 check( vy = cpl_vector_get( tcheb_coef_y, j));
649 cpl_frame * order_frame,
655 cpl_frame * result = NULL ;
661 cpl_image * wimg = NULL ;
662 double * dimg = NULL ;
663 cpl_propertylist * wheader = NULL ;
664 char * final_name = NULL;
674 xsh_msg(
"Entering xsh_wavemap_save, file \"%s\"", final_name ) ;
693 check( wimg = cpl_image_new(
nx,
ny, CPL_TYPE_DOUBLE ) ) ;
694 check( dimg = cpl_image_get_data_double( wimg ) ) ;
705 xsh_msg(
"NOT ENOUGH DATA FOR ORDER %d",
713 order, start_y, end_y ) ;
715 for (
y = start_y ;
y < end_y ;
y++ ) {
716 double dxmin, dxmax ;
720 (
double)
y, NULL ) ) ;
722 (
double)
y, NULL ) ) ;
723 xmin = floor(dxmin) ;
725 for (
x = xmin ;
x<xmax ;
x++ ) {
730 if ( cpl_error_get_code() != CPL_ERROR_NONE){
735 *(dimg +
x + (
y*
nx)) = (float)lambda ;
743 check( cpl_image_save( wimg, final_name, CPL_BPP_IEEE_FLOAT,
744 wheader, CPL_IO_DEFAULT ) ) ;
747 CPL_FRAME_TYPE_IMAGE,
748 CPL_FRAME_GROUP_PRODUCT,
749 CPL_FRAME_LEVEL_TEMPORARY));
765 const int abs_ord,
const int sid,
const int iter)
770 cpl_image* sky_wmap = NULL;
771 cpl_image* sky_smap = NULL;
773 float* pdata_sky =NULL;
774 float* perrs_sky =NULL;
775 int* pqual_sky =NULL;
776 float* pdata_fit =NULL;
777 float* perrs_fit =NULL;
778 int* pqual_fit =NULL;
780 float* psky_wmap =NULL;
781 float* psky_smap =NULL;
786 char * final_name = NULL;
793 xsh_msg(
"Image size:%d,%d", sx, sy ) ;
812 psky_wmap = cpl_image_get_data_float(sky_wmap);
813 psky_smap = cpl_image_get_data_float(sky_smap);
821 for (
int k = 0; k < sky_size; k++) {
825 pdata_sky[pix]=psky->
flux;
826 perrs_sky[pix]=psky->
sigma;
827 pqual_sky[pix]=psky->
qual;
828 pdata_fit[pix]=psky->
fitted;
830 pqual_fit[pix]=psky->
qual;
832 psky_wmap[pix]=psky->
lambda;
833 psky_smap[pix]=psky->
slit;
840 cpl_frame* frm = NULL;
841 sprintf(fname,
"sky_model_ord_%2.2d_slice_%2.2d_iter_%2.2d.fits",abs_ord,sid,iter);
844 sprintf(fname,
"sky_fit_ord_%2.2d_slice_%2.2d_iter_%2.2d.fits",abs_ord,sid,iter);
847 sprintf(fname,
"sky_wmap_%2.2d.fits",iter);
848 cpl_image_save(sky_wmap,
"sky_wmap.fits",
XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
849 sprintf(fname,
"sky_smap_%2.2d.fits",iter);
850 cpl_image_save(sky_smap,
"sky_smap.fits",
XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
854 cpl_free(final_name);
860 return cpl_error_get_code() ;
878 cpl_image* obj_wmap = NULL;
879 cpl_image* obj_smap = NULL;
881 float* pdata_obj =NULL;
882 float* perrs_obj =NULL;
883 int* pqual_obj =NULL;
885 float* pobj_wmap =NULL;
886 float* pobj_smap =NULL;
909 pobj_wmap = cpl_image_get_data_float(obj_wmap);
910 pobj_smap = cpl_image_get_data_float(obj_smap);
918 for (
int k = 0; k < obj_size; k++) {
923 pdata_obj[pix]=pobj->
flux;
924 perrs_obj[pix]=pobj->
sigma;
925 pqual_obj[pix]=pobj->
qual;
926 pobj_wmap[pix]=pobj->
lambda;
927 pobj_smap[pix]=pobj->
slit;
935 cpl_frame* frm = NULL;
936 sprintf(fname,
"object_model_%2.2d.fits",iter);
940 sprintf(fname,fname,iter);
941 cpl_image_save(obj_wmap,
"object_wmap.fits",
XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
942 sprintf(fname,
"object_smap_%2.2d.fits",iter);
951 return cpl_error_get_code() ;
972 cpl_image* obj_wmap = NULL;
973 cpl_image* obj_smap = NULL;
974 cpl_image* sky_wmap = NULL;
975 cpl_image* sky_smap = NULL;
977 float* pdata_obj =NULL;
978 float* perrs_obj =NULL;
979 int* pqual_obj =NULL;
981 float* pdata_sky =NULL;
982 float* perrs_sky =NULL;
983 int* pqual_sky =NULL;
985 float* pobj_wmap =NULL;
986 float* pobj_smap =NULL;
988 float* psky_wmap =NULL;
989 float* psky_smap =NULL;
993 char * final_name = NULL;
1000 xsh_msg(
"Entering xsh_wavemap_save, file \"%s\"", final_name ) ;
1004 xsh_msg(
"Image size:%d,%d", sx, sy ) ;
1024 pobj_wmap = cpl_image_get_data_float(obj_wmap);
1025 pobj_smap = cpl_image_get_data_float(obj_smap);
1027 psky_wmap = cpl_image_get_data_float(sky_wmap);
1028 psky_smap = cpl_image_get_data_float(sky_smap);
1038 for (
int k = 0; k < obj_size; k++) {
1042 pdata_obj[pix]=pobj->
flux;
1043 perrs_obj[pix]=pobj->
sigma;
1044 pqual_obj[pix]=pobj->
qual;
1045 pobj_wmap[pix]=pobj->
lambda;
1046 pobj_smap[pix]=pobj->
slit;
1050 for (
int k = 0; k < sky_size; k++) {
1054 pdata_sky[pix]=psky->
flux;
1055 perrs_sky[pix]=psky->
sigma;
1056 pqual_sky[pix]=psky->
qual;
1057 psky_wmap[pix]=psky->
lambda;
1058 psky_smap[pix]=psky->
slit;
1064 cpl_frame* frm = NULL;
1065 frm =
xsh_pre_save(obj_model,
"object_model.fits",
"OBJECT_MODEL",0);
1067 frm =
xsh_pre_save(sky_model,
"sky_model.fits",
"SKY_MODEL",0);
1070 cpl_image_save(obj_wmap,
"object_wmap.fits",
XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
1071 cpl_image_save(obj_smap,
"object_smap.fits",
XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
1072 cpl_image_save(sky_wmap,
"sky_wmap.fits",
XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
1073 cpl_image_save(sky_smap,
"sky_smap.fits",
XSH_PRE_DATA_BPP, NULL, CPL_IO_DEFAULT);
1084 return cpl_error_get_code() ;
1104 const char * prefix)
1106 cpl_frame * result = NULL ;
1108 int start_y, end_y ;
1112 cpl_image * wimg = NULL ;
1113 double * dimg = NULL ;
1114 cpl_propertylist * wheader = NULL ;
1115 char * final_name = NULL;
1124 xsh_msg(
"Entering xsh_wavemap_save, file \"%s\"", final_name ) ;
1143 check( wimg = cpl_image_new(
nx,
ny, CPL_TYPE_DOUBLE ) ) ;
1144 check( dimg = cpl_image_get_data_double( wimg ) ) ;
1165 order, start_y, end_y ) ;
1166 xsh_msg(
" Order %d, Ymin: %d, Ymax: %d",
1167 order, start_y, end_y ) ;
1168 for (
y = start_y ;
y < end_y ;
y++ ) {
1169 double dxmin, dxmax ;
1173 (
double)
y, NULL ) ) ;
1175 (
double)
y, NULL ) ) ;
1176 xmin = floor(dxmin) ;
1177 xmax = ceil(dxmax) ;
1178 xsh_msg(
" Order %d, Xmin: %d, Xmax: %d",
1179 order, xmin, xmax ) ;
1180 for (
x = xmin ;
x<xmax ;
x++ ) {
1185 if ( cpl_error_get_code() != CPL_ERROR_NONE){
1192 *(dimg +
x + (
y*
nx)) = (float)lambda ;
1201 check( cpl_image_save( wimg, final_name, CPL_BPP_IEEE_FLOAT,
1202 wheader, CPL_IO_DEFAULT ) ) ;
1205 CPL_FRAME_TYPE_IMAGE,
1206 CPL_FRAME_GROUP_PRODUCT,
1207 CPL_FRAME_LEVEL_TEMPORARY));
1236 cpl_frame *order_frame,
1240 cpl_frame **dispersol_frame,
1241 cpl_frame **slitmap_frame)
1243 cpl_frame *result = NULL;
1244 const char *slit_tag=NULL;
1247 const char* disp_tag=NULL;
1282 if(strstr(cpl_frame_get_tag(order_frame),
"AFC") != NULL) {
1290 if (cpl_error_get_code() != CPL_ERROR_NONE){
cpl_frame * xsh_dispersol_list_save(xsh_dispersol_list *list, const char *tag)
Save a dispersion list on the disk.
void xsh_dispersol_list_add(xsh_dispersol_list *list, int idx, int absorder, cpl_polynomial *lambda_poly, cpl_polynomial *slit_poly)
Add a dispersion solution in the list.
void xsh_dispersol_list_free(xsh_dispersol_list **list)
Free the dispersion list.
cpl_frame * xsh_dispersol_list_to_slitmap(xsh_dispersol_list *list, cpl_frame *order_frame, xsh_pre *pre, xsh_instrument *instr, const char *tag)
Save a SLIT MAP image.
xsh_dispersol_list * xsh_dispersol_list_new(int size, int degx, int degy, xsh_instrument *instrument)
Create a new dispersion solution list.
cpl_frame * xsh_dispersol_list_to_wavemap(xsh_dispersol_list *list, cpl_frame *order_frame, xsh_pre *pre, xsh_instrument *instr, const char *tag)
Save a WAVE MAP image.
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
cpl_image * xsh_pre_get_data(xsh_pre *pre)
Get data.
cpl_image * xsh_pre_get_qual(xsh_pre *pre)
Get qual.
xsh_pre * xsh_pre_new(int nx, int ny)
Create new PRE image.
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
cpl_image * xsh_pre_get_errs(xsh_pre *pre)
Get errs.
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
void xsh_wavemap_list_compute_poly(double *vlambda, double *vslit, double *xpos, double *ypos, int nitems, double *orders, xsh_dispersol_param *dispsol_param, xsh_wavemap_list *wmap)
compute a wave-map-list
void xsh_wavemap_list_dump(xsh_wavemap_list *list, const char *fname)
Dump main info about a rec table (for each order of the list)
void xsh_wavemap_list_free(xsh_wavemap_list **list)
free memory associated to a wavemap_list
cpl_frame * xsh_wavemap_list_save_poly(xsh_wavemap_list *wmap, cpl_frame *order_frame, xsh_pre *pre, xsh_instrument *instr, const char *prefix, cpl_frame **dispersol_frame, cpl_frame **slitmap_frame)
Save the wave_map slit_map and disp_tab.
cpl_error_code xsh_wavemap_list_sky_image_save(xsh_wavemap_list *smap, xsh_instrument *instr, const int abs_ord, const int sid, const int iter)
cpl_error_code xsh_wavemap_list_object_image_save(xsh_wavemap_list *omap, xsh_instrument *instr, const int iter)
static double xsh_wavemap_list_eval_lambda(xsh_wavemap_list *wmap, double x, double y, int ord)
eval the polynomial wave solution in x,y,ord
cpl_frame * xsh_wavemap_list_save2(xsh_wavemap_list *wmap, xsh_order_list *order_list, xsh_pre *pre, xsh_instrument *instr, const char *prefix)
void xsh_wavemap_list_compute(double *vlambda, double *xpos, double *ypos, int nitems, double *orders, xsh_dispersol_param *dispsol_param, xsh_wavemap_list *wmap)
compute a wave-map-list
cpl_error_code xsh_wavemap_list_save4debug(xsh_wavemap_list *wmap, xsh_instrument *instr, const char *prefix)
static void lambda_fit(double *lambda, double *xpos, double *ypos, int size, xsh_wavemap_list *wmap, xsh_dispersol_param *dispsol_param, int order)
void xsh_wavemap_list_set_max_size(xsh_wavemap_list *list, int idx, int absorder, int max_size)
set max size of wavemap
xsh_wavemap_list * xsh_wavemap_list_create(xsh_instrument *instr)
create an empty order list
cpl_frame * xsh_wavemap_list_save(xsh_wavemap_list *wmap, cpl_frame *order_frame, xsh_pre *pre, xsh_instrument *instr, const char *prefix)
#define XSH_REGDEBUG(...)
#define XSH_ASSURE_NOT_ILLEGAL(cond)
#define xsh_error_reset()
#define XSH_ASSURE_NOT_ILLEGAL_MSG(cond, msg)
#define XSH_ASSURE_NOT_NULL(pointer)
XSH_INSTRCONFIG * xsh_instrument_get_config(xsh_instrument *i)
Get the instrument default set of keywords.
#define xsh_msg_dbg_medium(...)
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_low(...)
#define xsh_msg_dbg_high(...)
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_tools_min_max(int size, double *tab, double *min, double *max)
computes min & max in ab array
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.
double xsh_tools_tchebitchev_transform(double pos, double min, double max)
computes Tchebitchev transformation
cpl_vector * xsh_tools_tchebitchev_poly_eval(int n, double X)
Compute tchebitchev Tn(X) first coefficient for tchebitchev polynomial.
double xsh_tools_tchebitchev_reverse_transform(double pos, double min, double max)
computes reverse Tchebitchev transformation
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
void xsh_tools_tchebitchev_transform_tab(int size, double *pos, double min, double max, double *tcheb_pos)
computes Tchebitchev transformation
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
cpl_propertylist * data_header
xsh_instrument * instrument
cpl_propertylist * header
cpl_polynomial * pol_lambda
cpl_polynomial * pol_slit
cpl_polynomial * tcheb_pol_lambda
#define XSH_PRE_DATA_TYPE
int xsh_print_rec_status(const int val)
Check if an error has happened and returns error kind and location.
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
#define XSH_SLIT_MAP_POLY
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
#define XSH_NEW_PROPERTYLIST(POINTER)
#define XSH_FREE(POINTER)
#define XSH_MALLOC(POINTER, TYPE, SIZE)
#define XSH_CALLOC(POINTER, TYPE, SIZE)
cpl_polynomial * xsh_polynomial_fit_2d_create(cpl_bivector *xy_pos, cpl_vector *values, cpl_size *degree, double *mse)