00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef HAVE_CONFIG_H
00020 # include <config.h>
00021 #endif
00022 #include <irplib_stdstar.h>
00023
00024
00025 #include "sinfo_dfs.h"
00026
00027
00028 #include "sinfo_tpl_utils.h"
00029
00030 #include "sinfo_utilities_scired.h"
00031 #include "sinfo_functions.h"
00032 #include "sinfo_pfits.h"
00033 #include "sinfo_spiffi_types.h"
00034 #include "sinfo_utils_wrappers.h"
00035 #include "sinfo_error.h"
00036
00037 static double
00038 sinfo_sess2deg(const double sess);
00039
00040 static void
00041 sinfo_set_spect_coord1(cpl_propertylist** plist,
00042 const int crpix1,
00043 const double crval1,
00044 const double cdelt1);
00045
00046 static void
00047 sinfo_set_spect_coord2(cpl_propertylist** plist,
00048 const int crpix2,
00049 const double crval2,
00050 const double cdelt2);
00051
00052
00053 static void
00054 sinfo_set_coord1(cpl_propertylist** plist,
00055 const double crpix1,
00056 const double crval1,
00057 const double cdelt1);
00058 static void
00059 sinfo_set_coord2(cpl_propertylist** plist,
00060 const double crpix2,
00061 const double crval2,
00062 const double cdelt2);
00063 static void
00064 sinfo_set_coord3(cpl_propertylist** plist,
00065 const int crpix3,
00066 const double crval3,
00067 const double cdelt3);
00068
00069
00070 static void
00071 sinfo_set_cd_matrix2(cpl_propertylist** plist,
00072 const double cd1_1,
00073 const double cd1_2,
00074 const double cd2_1,
00075 const double cd2_2);
00076
00077
00078 static void
00079 sinfo_set_cd_matrix3(cpl_propertylist** plist,
00080 const double cd1_3,
00081 const double cd2_3,
00082 const double cd3_1,
00083 const double cd3_2,
00084 const double cd3_3);
00085
00086
00087 static void
00088 sinfo_new_change_plist_cube (cpl_propertylist * plist,
00089 float cenLambda,
00090 float dispersion,
00091 int center_z,
00092 float center_x,
00093 float center_y );
00094
00095
00096 static void
00097 sinfo_new_change_plist_image (cpl_propertylist * plist,
00098 float center_x,
00099 float center_y );
00100
00101
00102 static void
00103 sinfo_new_change_plist_spectrum (cpl_propertylist * plist,
00104 double cenLambda,
00105 double dispersion,
00106 int cenpix);
00107
00108 static cpl_image *
00109 sinfo_new_image_getvig(
00110 cpl_image * image_in,
00111 int loleft_x,
00112 int loleft_y,
00113 int upright_x,
00114 int upright_y);
00115
00116
00117
00130 int
00131 sinfo_check_input_data(object_config* cfg)
00132 {
00133
00134
00135 if (cfg == NULL)
00136 {
00137 sinfo_msg_error (" could not parse cpl input!\n") ;
00138 return -1 ;
00139 }
00140
00141
00142 if(sinfo_is_fits_file(cfg->wavemap) != 1) {
00143 sinfo_msg_error("Input file wavemap %s is not FITS",cfg->wavemap);
00144 return -1;
00145 }
00146
00147
00148 if (cfg->halocorrectInd == 1)
00149 {
00150 if(sinfo_is_fits_file(cfg->halospectrum) != 1) {
00151 sinfo_msg_error("Input file %s is not FITS",cfg->halospectrum);
00152 return -1;
00153 }
00154
00155 }
00156
00157 if (cfg->northsouthInd == 0) {
00158 if (sinfo_is_fits_file(cfg->poslist) != 1)
00159 {
00160 sinfo_msg_error("File %s with tag %s is not FITS!",
00161 cfg->poslist,PRO_SLIT_POS);
00162 return -1 ;
00163 }
00164 } else {
00165
00166 if (sinfo_is_fits_file(cfg->distlist) != 1)
00167 {
00168 sinfo_msg_error("File %s with tag %s is not FITS!",
00169 cfg->distlist,PRO_SLITLETS_DISTANCE);
00170 return -1;
00171 }
00172 }
00173
00174
00175 return 0;
00176
00177
00178 }
00186 static double
00187 sinfo_hms2deg(const double hms)
00188 {
00189 int hrs=0;
00190 int min=0;
00191 double sec=0;
00192 double deg=0;
00193 double rest=hms;
00194 int sign=1;
00195
00196
00197
00198 if(hms<0) {
00199 sign=-1;
00200 rest=-hms;
00201 }
00202
00203
00204
00205 hrs=(int)(rest/10000.);
00206
00207
00208 rest=rest-(double)(hrs*10000.);
00209 min=(int)(rest/100.);
00210
00211
00212 sec=rest-(double)(min*100.);
00213
00214
00215 deg=hrs*15+(double)(min/4.)+(double)(sec/240.);
00216
00217
00218 deg=sign*deg;
00219
00220
00221 return deg;
00222
00223 }
00224
00232 static double
00233 sinfo_sess2deg(const double sess)
00234 {
00235 int grad=0;
00236 int min=0;
00237 double sec=0;
00238 double deg=0;
00239 double rest=sess;
00240 int sign=1;
00241
00242
00243
00244 if(sess<0) {
00245 sign=-1;
00246 rest=-sess;
00247 }
00248
00249
00250
00251 grad=(int)(rest/10000.);
00252
00253
00254 rest=rest-(double)(grad*10000.);
00255 min=(int)(rest/100.);
00256
00257
00258 sec=rest-(double)(min*100.);
00259
00260
00261 deg=grad+(double)(min/60.)+(double)(sec/3600.);
00262
00263
00264 deg=sign*deg;
00265
00266
00267 return deg;
00268
00269 }
00270
00271
00284 int
00285 sinfo_auto_size_cube(float* offsetx,
00286 float* offsety,
00287 const int nframes,
00288 float* ref_offx,
00289 float* ref_offy,
00290 int* size_x,
00291 int* size_y)
00292 {
00293
00294 int n=0;
00295 float offx=0;
00296 float offy=0;
00297 float min_offx=0;
00298 float max_offx=0;
00299 float min_offy=0;
00300 float max_offy=0;
00301
00302 sinfo_msg ("Computation of output cube size") ;
00303 for ( n = 0 ; n < nframes ; n++ ) {
00304 offx = offsetx[n];
00305 offy = offsety[n];
00306
00307 if(n==0) {
00308 min_offx=offx;
00309 min_offy=offy;
00310 max_offx=offx;
00311 max_offy=offy;
00312 } else {
00313 if(offx > max_offx) max_offx=offx;
00314 if(offy > max_offy) max_offy=offy;
00315 if(offx < min_offx) min_offx=offx;
00316 if(offy < min_offy) min_offy=offy;
00317 }
00318 }
00319
00320
00321
00322
00323 *ref_offx=(min_offx+max_offx)/2;
00324 *ref_offy=(min_offy+max_offy)/2;
00325 *size_x+=2*floor(max_offx-min_offx+0.5);
00326 *size_y+=2*floor(max_offy-min_offy+0.5);
00327 sinfo_msg("Output cube size: %d x %d",*size_x,*size_y);
00328 sinfo_msg("Ref offset. x: %f y: %f",*ref_offx,*ref_offy);
00329 sinfo_msg_debug("Max offset. x: %f y: %f",max_offx,max_offy);
00330 sinfo_msg_debug("Min offset. x: %f y: %f",min_offx,min_offy);
00331 return 0;
00332
00333 }
00334
00342 int
00343 sinfo_auto_size_cube5(object_config * cfg,
00344 float* ref_offx, float* ref_offy,
00345 float* min_offx, float* min_offy,
00346 float* max_offx, float* max_offy)
00347 {
00348
00349 char* name =NULL;
00350 int n=0;
00351 float offx=0;
00352 float offy=0;
00353
00354 cpl_propertylist * plist=NULL;
00355 sinfo_msg ("Automatic computation of output cube size") ;
00356 for ( n = 0 ; n < cfg->nframes ; n++ ) {
00357 name = cfg->framelist[n] ;
00358 plist=cpl_propertylist_load(name,0);
00359 offx = sinfo_pfits_get_cumoffsetx(plist);
00360 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00361 sinfo_msg_warning(" could not read fits header keyword cummoffsetx!");
00362 sinfo_msg_warning(" set it to 0");
00363 offx = 0;
00364 cpl_error_reset();
00365
00366 }
00367
00368 offy = sinfo_pfits_get_cumoffsety(plist);
00369 sinfo_free_propertylist(&plist);
00370 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00371 sinfo_msg_warning(" could not read fits header keyword! cumoffsety") ;
00372 sinfo_msg_warning(" set it to 0");
00373 offy = 0;
00374 cpl_error_reset();
00375
00376 }
00377
00378
00379 if(n==0) {
00380 *min_offx=offx;
00381 *min_offy=offy;
00382 *max_offx=offx;
00383 *max_offy=offy;
00384 } else {
00385 if(offx > *max_offx) *max_offx=offx;
00386 if(offy > *max_offy) *max_offy=offy;
00387 if(offx < *min_offx) *min_offx=offx;
00388 if(offy < *min_offy) *min_offy=offy;
00389 }
00390 }
00391 *ref_offx=(*min_offx+*max_offx)/2;
00392 *ref_offy=(*min_offy+*max_offy)/2;
00393
00394 if(cfg->size_x == 0) cfg->size_x=2*floor(*max_offx-*min_offx+0.5)+64 ;
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404 if(cfg->size_y == 0) cfg->size_y=2*floor(*max_offy-*min_offy+0.5)+64 ;
00405
00406 sinfo_msg("Output cube size: %d x %d",cfg->size_x,cfg->size_y);
00407 sinfo_msg("Ref offset. x: %f y: %f",*ref_offx,*ref_offy);
00408 sinfo_msg("Max offset. x: %f y: %f",*max_offx,*max_offy);
00409 sinfo_msg("Min offset. x: %f y: %f",*min_offx,*min_offy);
00410 return 0;
00411
00412
00413 }
00414
00424 float*
00425 sinfo_read_distances(const int nslits, const char* distlist)
00426 {
00427 int i=0;
00428 int* status=NULL;
00429 float * distances = NULL;
00430 float tmp_float=0;
00431 char tbl_distances_name[FILE_NAME_SZ];
00432 cpl_table* tbl_distances = NULL;
00433
00434 sinfo_msg("Read distances");
00435 distances = (float*) cpl_calloc (nslits - 1, sizeof (float));
00436
00437 if ( NULL == distances )
00438 {
00439 sinfo_msg_error ("could allocate memory!") ;
00440 return NULL ;
00441 }
00442
00443
00444 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00445 sinfo_msg_error("Before loading input table");
00446 sinfo_msg_error((char* ) cpl_error_get_message());
00447 return NULL;
00448 }
00449 strcpy(tbl_distances_name,distlist);
00450 tbl_distances = cpl_table_load(tbl_distances_name,1,0);
00451 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00452 sinfo_msg_error("loading input table %s",tbl_distances_name);
00453 sinfo_msg_error((char* ) cpl_error_get_message());
00454 return NULL;
00455 }
00456
00457 for (i =0 ; i< nslits-1; i++){
00458 tmp_float=cpl_table_get_float(tbl_distances,"slitlet_distance",i,status);
00459 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00460 sinfo_msg_error("reading col %s from table %s","slitlet_distance",
00461 tbl_distances_name);
00462 sinfo_msg_error((char* ) cpl_error_get_message());
00463 return NULL;
00464 }
00465 sinfo_new_array_set_value(distances,tmp_float,i);
00466 }
00467 cpl_table_delete(tbl_distances);
00468 return distances;
00469
00470 }
00471
00472
00473
00481 float**
00482 sinfo_read_slitlets_edges(const int nslits, const char* poslist)
00483 {
00484
00485
00486 char tbl_slitpos_name[FILE_NAME_SZ];
00487 cpl_table* tbl_slitpos=NULL;
00488 int n=0;
00489 int i=0;
00490 int* status=NULL;
00491 float edge_x=0;
00492 float edge_y=0;
00493 float ** slit_edges = NULL;
00494
00495 slit_edges = sinfo_new_2Dfloatarray(nslits, 2) ;
00496
00497 strcpy(tbl_slitpos_name,poslist);
00498 tbl_slitpos = cpl_table_load(tbl_slitpos_name,1,0);
00499 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00500 sinfo_msg_error("error loading tbl %s",tbl_slitpos_name);
00501 sinfo_msg_error((char* ) cpl_error_get_message());
00502 return NULL;
00503 }
00504 n = cpl_table_get_nrow(tbl_slitpos);
00505 if (n != nslits) {
00506 sinfo_msg_error("No of slitlets in table is n = %d != %d !",n,nslits);
00507 return NULL;
00508 }
00509
00510 for (i =0 ; i< nslits; i++){
00511 edge_x=cpl_table_get_double(tbl_slitpos,"pos1",i,status);
00512 edge_y=cpl_table_get_double(tbl_slitpos,"pos2",i,status);
00513 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00514 sinfo_msg_error("error reading tbl %s row %d",tbl_slitpos_name,i);
00515 sinfo_msg_error((char* ) cpl_error_get_message());
00516 return NULL;
00517 }
00518 sinfo_new_array2D_set_value(slit_edges,edge_x,i,0);
00519 sinfo_new_array2D_set_value(slit_edges,edge_y,i,1);
00520 }
00521 cpl_table_delete(tbl_slitpos);
00522 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00523 sinfo_msg_error("error reading tbl %s",tbl_slitpos_name);
00524 sinfo_msg_error((char* ) cpl_error_get_message());
00525 return NULL;
00526 }
00527
00528 return slit_edges;
00529
00530 }
00531
00532
00533
00556
00557 cpl_imagelist * sinfo_new_cube_getvig(
00558 cpl_imagelist * cube_in,
00559 int loleft_x,
00560 int loleft_y,
00561 int upright_x,
00562 int upright_y)
00563 {
00564 cpl_imagelist * cube_out ;
00565 int i ;
00566 int outlx,
00567 outly ;
00568
00569 int ilx=0;
00570 int ily=0;
00571 int inp=0;
00572 cpl_image* i_img=NULL;
00573 cpl_image* o_img=NULL;
00574
00575 if (cube_in==NULL) return NULL ;
00576
00577 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube_in,0));
00578 ily=cpl_image_get_size_y(cpl_imagelist_get(cube_in,0));
00579 inp=cpl_imagelist_get_size(cube_in);
00580
00581 if ((loleft_x>upright_x) ||
00582 (loleft_y>upright_y)) {
00583 sinfo_msg_error("ill-defined slit for extraction: aborting");
00584 return NULL ;
00585 }
00586
00587
00588 outlx = upright_x - loleft_x + 1 ;
00589 outly = upright_y - loleft_y + 1 ;
00590
00591
00592
00593 cube_out = cpl_imagelist_new() ;
00594
00595 for (i=0 ; i<cpl_imagelist_get_size(cube_in) ; i++) {
00596
00597 i_img=cpl_imagelist_get(cube_in,i);
00598
00599 o_img = sinfo_new_image_getvig(i_img,
00600 loleft_x, loleft_y,
00601 upright_x, upright_y) ;
00602 cpl_imagelist_set(cube_out,o_img,i);
00603 }
00604 return cube_out ;
00605 }
00606
00623
00624
00625 static cpl_image *
00626 sinfo_new_image_getvig(
00627 cpl_image * image_in,
00628 int loleft_x,
00629 int loleft_y,
00630 int upright_x,
00631 int upright_y)
00632 {
00633 cpl_image * slit_img ;
00634 int i, j ;
00635 register
00636 pixelvalue * inpt,
00637 * outpt ;
00638 int outlx, outly ;
00639 int ilx=0;
00640 int ily=0;
00641
00642 float* pidata=NULL;
00643 float* podata=NULL;
00644
00645 if (image_in==NULL) return NULL ;
00646
00647 ilx=cpl_image_get_size_x(image_in);
00648 ily=cpl_image_get_size_y(image_in);
00649 pidata=cpl_image_get_data_float(image_in);
00650
00651 if ((loleft_x<1) || (loleft_x>ilx) ||
00652 (loleft_y<1) || (loleft_y>ily) ||
00653 (upright_x<1) || (upright_x>ilx) ||
00654 (upright_y<1) || (upright_y>ily) ||
00655 (loleft_x>upright_x) || (loleft_y>upright_y)) {
00656 sinfo_msg_error("extraction zone is [%d %d] [%d %d]\n"
00657 "cannot extract such zone: aborting slit extraction",
00658 loleft_x, loleft_y, upright_x, upright_y) ;
00659 return NULL ;
00660 }
00661
00662 outlx = upright_x - loleft_x + 1 ;
00663 outly = upright_y - loleft_y + 1 ;
00664 slit_img = cpl_image_new(outlx, outly,CPL_TYPE_FLOAT) ;
00665 podata=cpl_image_get_data_float(slit_img);
00666
00667 for (j=0 ; j<outly ; j++) {
00668 inpt = pidata+loleft_x-1 + (j+loleft_y-1)*ilx ;
00669 outpt = podata + j*outlx ;
00670 for (i=0 ; i<outlx ; i++) {
00671 *outpt++ = *inpt++ ;
00672 }
00673 }
00674 return slit_img ;
00675 }
00676
00688 int
00689 sinfo_new_set_wcs_cube(cpl_imagelist* cub, const char* name, double clambda,
00690 double dis, double cpix, double cx, double cy)
00691 {
00692 cpl_propertylist* plist=NULL;
00693
00694 if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
00695 sinfo_msg_error( "getting header from frame %s",name);
00696 cpl_propertylist_delete(plist) ;
00697 return -1 ;
00698 }
00699
00700 sinfo_new_change_plist_cube(plist, clambda, dis, cpix, cx, cy) ;
00701 sinfo_plist_set_extra_keys(plist,"IMAGE","DATA","RMSE",
00702 "DATA","ERRS","QUAL",0);
00703 if (cpl_imagelist_save(cub, name, CPL_BPP_IEEE_FLOAT,
00704 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00705 sinfo_msg_error( "Cannot save the product %s",name);
00706 cpl_propertylist_delete(plist) ;
00707 return -1 ;
00708 }
00709 cpl_propertylist_delete(plist) ;
00710 return 0;
00711
00712 }
00721 int
00722 sinfo_new_set_wcs_image(cpl_image* img,
00723 const char* name,
00724 double cx,
00725 double cy)
00726 {
00727 cpl_propertylist* plist=NULL;
00728
00729 if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
00730 sinfo_msg_error( "getting header from frame %s",name);
00731 cpl_propertylist_delete(plist) ;
00732 return -1 ;
00733 }
00734 sinfo_new_change_plist_image(plist, cx, cy) ;
00735
00736 if (cpl_image_save(img, name, CPL_BPP_IEEE_FLOAT,
00737 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00738 sinfo_msg_error( "Cannot save the product %s",name);
00739 cpl_propertylist_delete(plist) ;
00740 return -1 ;
00741 }
00742 cpl_propertylist_delete(plist) ;
00743 return 0;
00744
00745 }
00746
00747
00757 int
00758 sinfo_new_set_wcs_spectrum(cpl_image* img, const char* name, double clambda,
00759 double dis, double cpix)
00760 {
00761 cpl_propertylist* plist=NULL;
00762
00763 if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
00764 sinfo_msg_error( "getting header from frame %s",name);
00765 cpl_propertylist_delete(plist) ;
00766 return -1 ;
00767 }
00768 sinfo_new_change_plist_spectrum(plist, clambda, dis,cpix) ;
00769
00770 if (cpl_image_save(img, name, CPL_BPP_IEEE_FLOAT,
00771 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00772 sinfo_msg_error( "Cannot save the product %s",name);
00773 cpl_propertylist_delete(plist) ;
00774 return -1 ;
00775 }
00776 cpl_propertylist_delete(plist) ;
00777 return 0;
00778
00779 }
00791 static void
00792 sinfo_new_change_plist_cube (cpl_propertylist * plist,
00793 float cenLambda,
00794 float dispersion,
00795 int center_z,
00796 float center_x,
00797 float center_y )
00798 {
00799
00800 float pixelscale ;
00801 double ra ;
00802 double dec ;
00803 double angle ;
00804 float radangle ;
00805 double cd1_1, cd1_2, cd2_1, cd2_2 ;
00806 int sign_swap = -1;
00807 char firsttext[2*FILE_NAME_SZ] ;
00808
00809 double cdelt1=0;
00810 double cdelt2=0;
00811 double cdelt3=dispersion;
00812
00813 double crpix1=center_x;
00814 double crpix2=center_y;
00815 int crpix3=center_z;
00816
00817 double crval1=0;
00818 double crval2=0;
00819 double crval3=cenLambda;
00820
00821
00822 strcpy(firsttext, "sinfo_rec_objnod -f \0") ;
00823
00824 pixelscale = sinfo_pfits_get_pixscale(plist)/2. ;
00825 ra = sinfo_pfits_get_ra(plist) ;
00826 dec = sinfo_pfits_get_DEC(plist) ;
00827
00828
00829 ra=sinfo_pfits_get_targ_alpha(plist);
00830 dec=sinfo_pfits_get_targ_delta(plist);
00831
00832
00833 ra=sinfo_hms2deg(ra);
00834 dec=sinfo_sess2deg(dec);
00835
00836
00837
00838 crval1=ra;
00839 crval2=dec;
00840
00841 angle = sinfo_pfits_get_posangle(plist) ;
00842
00843 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00844 cpl_error_reset();
00845 }
00846 cdelt1=sign_swap*pixelscale / 3600.;
00847 cdelt2= +pixelscale / 3600.;
00848
00849
00850 radangle = angle * PI_NUMB / 180. ;
00851 cd1_1 = +cdelt1*cos(radangle);
00852 cd1_2 = -cdelt2*sin(radangle);
00853 cd2_1 = +cdelt1*sin(radangle);
00854 cd2_2 = +cdelt2*cos(radangle);
00855
00856
00857 sinfo_set_coord1(&plist,crpix1,crval1,cdelt1);
00858 sinfo_set_coord2(&plist,crpix2,crval2,cdelt2);
00859 sinfo_set_coord3(&plist,crpix3,crval3,cdelt3);
00860 sinfo_set_cd_matrix2(&plist,cd1_1,cd1_2,cd2_1,cd2_2);
00861 sinfo_set_cd_matrix3(&plist,0,0,0,0,dispersion);
00862
00863
00864 }
00865
00866
00867
00868
00877 static void
00878 sinfo_set_coord1(cpl_propertylist** plist,
00879 const double crpix1,
00880 const double crval1,
00881 const double cdelt1)
00882 {
00883 cpl_propertylist_erase_regexp(*plist, "^CTYPE1",0);
00884 cpl_propertylist_insert_after_string(*plist,"EXPTIME","CTYPE1","RA---TAN");
00885 cpl_propertylist_set_comment(*plist, "CTYPE1", "Projected Rectascension");
00886 cpl_propertylist_erase_regexp(*plist, "^CRPIX1",0);
00887 cpl_propertylist_insert_after_double(*plist,"CTYPE1","CRPIX1", crpix1) ;
00888 cpl_propertylist_set_comment(*plist, "CRPIX1","Reference pixel in RA" ) ;
00889
00890 cpl_propertylist_erase_regexp(*plist, "^CRVAL1",0);
00891 cpl_propertylist_insert_after_double(*plist, "CRPIX1", "CRVAL1", crval1 ) ;
00892 cpl_propertylist_set_comment(*plist, "CRVAL1","Reference RA" ) ;
00893
00894 cpl_propertylist_erase_regexp(*plist, "^CDELT1",0);
00895 cpl_propertylist_insert_after_double(*plist,"CRVAL1","CDELT1",cdelt1 ) ;
00896 cpl_propertylist_set_comment(*plist, "CDELT1","pixel scale" ) ;
00897
00898 cpl_propertylist_erase_regexp(*plist, "^CUNIT1",0);
00899 cpl_propertylist_insert_after_string(*plist, "CDELT1", "CUNIT1", "deg" ) ;
00900 cpl_propertylist_set_comment(*plist, "CUNIT1","RA-UNIT" ) ;
00901
00902 return;
00903 }
00904
00905
00914 static void
00915 sinfo_set_coord2(cpl_propertylist** plist,
00916 const double crpix2,
00917 const double crval2,
00918 const double cdelt2)
00919 {
00920 cpl_propertylist_erase_regexp(*plist, "^CTYPE2",0);
00921 cpl_propertylist_insert_after_string(*plist,"CUNIT1","CTYPE2","DEC--TAN");
00922 cpl_propertylist_set_comment(*plist, "CTYPE2", "Projected Declination") ;
00923
00924 cpl_propertylist_erase_regexp(*plist, "^CRPIX2",0);
00925 cpl_propertylist_insert_after_double(*plist,"CTYPE2","CRPIX2",crpix2 ) ;
00926 cpl_propertylist_set_comment(*plist, "CRPIX2", "Reference pixel in DEC") ;
00927
00928 cpl_propertylist_erase_regexp(*plist,"^CRVAL2",0);
00929 cpl_propertylist_insert_after_double(*plist,"CRPIX2","CRVAL2",crval2) ;
00930 cpl_propertylist_set_comment(*plist,"CRVAL2","Reference DEC") ;
00931
00932 cpl_propertylist_erase_regexp(*plist,"^CDELT2",0);
00933 cpl_propertylist_insert_after_double(*plist,"CRVAL2","CDELT2",cdelt2 ) ;
00934 cpl_propertylist_set_comment(*plist,"CDELT2","pixel scale") ;
00935
00936 cpl_propertylist_erase_regexp(*plist,"^CUNIT2",0);
00937 cpl_propertylist_insert_after_string(*plist,"CDELT2","CUNIT2", "deg" ) ;
00938 cpl_propertylist_set_comment(*plist,"CUNIT2","DEC-UNIT") ;
00939
00940
00941 }
00942
00943
00944
00953 static void
00954 sinfo_set_coord3(cpl_propertylist** plist,
00955 const int crpix3,
00956 const double crval3,
00957 const double cdelt3)
00958 {
00959 cpl_propertylist_erase_regexp(*plist, "^CTYPE3",0);
00960 cpl_propertylist_insert_after_string(*plist,"EXPTIME", "CTYPE3", "WAVE" ) ;
00961 cpl_propertylist_set_comment(*plist,"CTYPE3","wavelength axis in microns") ;
00962
00963 cpl_propertylist_erase_regexp(*plist, "^CRPIX3",0);
00964 cpl_propertylist_insert_after_double(*plist,"CTYPE3","CRPIX3", (double)crpix3 ) ;
00965 cpl_propertylist_set_comment(*plist, "CRPIX3", "Reference pixel in z") ;
00966
00967 cpl_propertylist_erase_regexp(*plist, "^CRVAL3",0);
00968 cpl_propertylist_insert_after_double(*plist,"CRPIX3", "CRVAL3", crval3) ;
00969 cpl_propertylist_set_comment(*plist, "CRVAL3", "central wavelength") ;
00970
00971 cpl_propertylist_erase_regexp(*plist, "^CDELT3",0);
00972
00973 cpl_propertylist_insert_after_double(*plist,"CRVAL3","CDELT3",cdelt3) ;
00974 cpl_propertylist_set_comment(*plist, "CDELT3", "microns per pixel") ;
00975
00976 cpl_propertylist_erase_regexp(*plist, "^CUNIT3",0);
00977 cpl_propertylist_insert_after_string(*plist,"CDELT3", "CUNIT3", "um" ) ;
00978 cpl_propertylist_set_comment(*plist, "CUNIT3", "spectral unit" ) ;
00979
00980 cpl_propertylist_erase_regexp(*plist, "^SPECSYS",0);
00981 cpl_propertylist_insert_after_string(*plist,"CUNIT3", "SPECSYS", "TOPOCENT" ) ;
00982 cpl_propertylist_set_comment(*plist, "SPECSYS", "Coordinate reference frame" ) ;
00983 }
00984
00985
00996 static void
00997 sinfo_set_cd_matrix2(cpl_propertylist** plist,
00998 const double cd1_1,
00999 const double cd1_2,
01000 const double cd2_1,
01001 const double cd2_2)
01002 {
01003
01004 check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD1_1",0));
01005 check_nomsg(cpl_propertylist_insert_after_double(*plist,"EXPTIME",
01006 "CD1_1", cd1_1 )) ;
01007 check_nomsg(cpl_propertylist_set_comment(*plist, "CD1_1",
01008 "CD rotation matrix" )) ;
01009
01010 check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD1_2",0));
01011 check_nomsg(cpl_propertylist_insert_after_double(*plist, "CD1_1",
01012 "CD1_2", cd1_2 )) ;
01013 check_nomsg(cpl_propertylist_set_comment(*plist, "CD1_2",
01014 "CD rotation matrix" )) ;
01015
01016 check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD2_1",0));
01017 check_nomsg(cpl_propertylist_insert_after_double(*plist, "CD1_2",
01018 "CD2_1", cd2_1 )) ;
01019 check_nomsg(cpl_propertylist_set_comment(*plist, "CD2_1",
01020 "CD rotation matrix" )) ;
01021
01022 check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD2_2",0));
01023 check_nomsg(cpl_propertylist_insert_after_double(*plist, "CD2_1",
01024 "CD2_2", cd2_2 )) ;
01025 check_nomsg(cpl_propertylist_set_comment(*plist, "CD2_2",
01026 "CD rotation matrix" )) ;
01027
01028 cleanup:
01029 return;
01030
01031
01032 }
01033
01034
01047 static void
01048 sinfo_set_cd_matrix3(cpl_propertylist** plist,
01049 const double cd1_3,
01050 const double cd2_3,
01051 const double cd3_1,
01052 const double cd3_2,
01053 const double cd3_3)
01054 {
01055
01056
01057 check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD1_3",0));
01058 check_nomsg(cpl_propertylist_insert_after_double(*plist,"EXPTIME",
01059 "CD1_3", cd1_3 )) ;
01060 check_nomsg(cpl_propertylist_set_comment(*plist, "CD1_3",
01061 "CD rotation matrix" )) ;
01062
01063
01064 check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD2_3",0));
01065 check_nomsg(cpl_propertylist_insert_after_double(*plist,"CD1_3",
01066 "CD2_3", cd2_3 )) ;
01067 check_nomsg(cpl_propertylist_set_comment(*plist, "CD2_3",
01068 "CD rotation matrix" )) ;
01069
01070
01071
01072 check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD3_1",0));
01073 check_nomsg(cpl_propertylist_insert_after_double(*plist,"CD2_3",
01074 "CD3_1", cd3_1 )) ;
01075 check_nomsg(cpl_propertylist_set_comment(*plist, "CD3_1",
01076 "CD rotation matrix" )) ;
01077
01078 check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD3_2",0));
01079 check_nomsg(cpl_propertylist_insert_after_double(*plist, "CD3_1",
01080 "CD3_2", cd3_2 )) ;
01081 check_nomsg(cpl_propertylist_set_comment(*plist, "CD3_2",
01082 "CD rotation matrix" )) ;
01083
01084 check_nomsg(cpl_propertylist_erase_regexp(*plist, "^CD3_3",0));
01085 check_nomsg(cpl_propertylist_insert_after_double(*plist, "CD3_2",
01086 "CD3_3", cd3_3 )) ;
01087 check_nomsg(cpl_propertylist_set_comment(*plist, "CD3_3",
01088 "CD rotation matrix" )) ;
01089
01090 cleanup:
01091 return;
01092
01093
01094 }
01095
01096
01105 static void
01106 sinfo_set_spect_coord1(cpl_propertylist** plist,
01107 const int crpix1,
01108 const double crval1,
01109 const double cdelt1)
01110 {
01111
01112 cpl_propertylist_erase_regexp(*plist, "^CTYPE1",0);
01113 cpl_propertylist_insert_after_string(*plist,"EXPTIME", "CTYPE1", "PIXEL");
01114 cpl_propertylist_set_comment(*plist, "CTYPE1", "Pixel coordinate system.");
01115
01116 cpl_propertylist_erase_regexp(*plist, "^CRPIX1",0);
01117 cpl_propertylist_insert_after_double(*plist, "CTYPE1", "CRPIX1", (double)crpix1 ) ;
01118 cpl_propertylist_set_comment(*plist, "CRPIX1", "Reference pixel in x") ;
01119
01120 cpl_propertylist_erase_regexp(*plist, "^CRVAL1",0);
01121 cpl_propertylist_insert_after_double(*plist,"CRPIX1", "CRVAL1", crval1 ) ;
01122 cpl_propertylist_set_comment(*plist, "CRVAL1", "value of ref pixel.") ;
01123
01124 cpl_propertylist_erase_regexp(*plist, "^CDELT1",0);
01125 cpl_propertylist_insert_after_double(*plist,"CRVAL1", "CDELT1", cdelt1 ) ;
01126 cpl_propertylist_set_comment(*plist,"CDELT1", "pixel scale") ;
01127
01128
01129 cpl_propertylist_erase_regexp(*plist, "^CUNIT1",0);
01130 cpl_propertylist_insert_after_string(*plist,"CDELT1", "CUNIT1", "Pixel" );
01131 cpl_propertylist_set_comment(*plist, "CUNIT1", "spectral unit" );
01132
01133 }
01134
01135
01136
01137
01138
01147 static void
01148 sinfo_set_spect_coord2(cpl_propertylist** plist,
01149 const int crpix2,
01150 const double crval2,
01151 const double cdelt2)
01152 {
01153
01154 cpl_propertylist_erase_regexp(*plist, "^CTYPE2",0);
01155 cpl_propertylist_insert_after_string(*plist, "EXPTIME","CTYPE2","WAVE" );
01156 cpl_propertylist_set_comment(*plist,"CTYPE2","wavelength axis in microns");
01157
01158 cpl_propertylist_erase_regexp(*plist, "^CRPIX2",0);
01159 cpl_propertylist_insert_after_double(*plist, "CTYPE2", "CRPIX2",(double)crpix2 ) ;
01160 cpl_propertylist_set_comment(*plist, "CRPIX2", "Reference pixel in x") ;
01161
01162 cpl_propertylist_erase_regexp(*plist, "^CRVAL2",0);
01163 cpl_propertylist_insert_after_double(*plist, "CRPIX2","CRVAL2",crval2 ) ;
01164 cpl_propertylist_set_comment(*plist,"CRVAL2", "central wavelength") ;
01165
01166 cpl_propertylist_erase_regexp(*plist, "^CDELT2",0);
01167 cpl_propertylist_insert_after_double(*plist, "CRVAL2", "CDELT2",cdelt2);
01168 cpl_propertylist_set_comment(*plist,"CDELT2", "microns per pixel");
01169
01170 cpl_propertylist_erase_regexp(*plist, "^CUNIT2",0);
01171 cpl_propertylist_insert_after_string(*plist,"CDELT2", "CUNIT2", "um");
01172 cpl_propertylist_set_comment(*plist, "CUNIT2", "spectral unit" );
01173
01174
01175 }
01185 static void
01186 sinfo_new_change_plist_spectrum (cpl_propertylist * plist,
01187 double cenLambda,
01188 double dispersion,
01189 int cenpix)
01190 {
01191
01192 int crpix1=1;
01193 double crval1=1;
01194 double cdelt1=1;
01195
01196 int crpix2=cenpix;
01197 double crval2=cenLambda;
01198 double cdelt2=dispersion;
01199
01200
01201
01202 cpl_propertylist_erase_regexp(plist, "^CTYPE3",0);
01203 cpl_propertylist_erase_regexp(plist, "^CRPIX3",0);
01204 cpl_propertylist_erase_regexp(plist, "^CRVAL3",0);
01205 cpl_propertylist_erase_regexp(plist, "^CDELT3",0);
01206 cpl_propertylist_erase_regexp(plist, "^CUNIT3",0);
01207
01208 cpl_propertylist_erase_regexp(plist, "^CTYPE2",0);
01209 cpl_propertylist_erase_regexp(plist, "^CRPIX2",0);
01210 cpl_propertylist_erase_regexp(plist, "^CRVAL2",0);
01211 cpl_propertylist_erase_regexp(plist, "^CDELT2",0);
01212 cpl_propertylist_erase_regexp(plist, "^CUNIT2",0);
01213
01214
01215 cpl_propertylist_erase_regexp(plist, "^CD1_1",0);
01216 cpl_propertylist_erase_regexp(plist, "^CD1_2",0);
01217 cpl_propertylist_erase_regexp(plist, "^CD2_1",0);
01218 cpl_propertylist_erase_regexp(plist, "^CD2_2",0);
01219
01220
01221
01222
01223 sinfo_set_spect_coord1(&plist,crpix1,crval1,cdelt1);
01224 sinfo_set_spect_coord2(&plist,crpix2,crval2,cdelt2);
01225
01226
01227
01228 }
01238 static void
01239 sinfo_new_change_plist_image (cpl_propertylist * plist,
01240 float center_x,
01241 float center_y )
01242 {
01243
01244 float pixelscale ;
01245 double ra ;
01246 double dec ;
01247 double angle ;
01248 float radangle ;
01249 float cd1_1, cd1_2, cd2_1, cd2_2 ;
01250 char firsttext[2*FILE_NAME_SZ] ;
01251 int sign_swap = -1;
01252
01253
01254 double cdelt1=0;
01255 double cdelt2=0;
01256
01257 double crpix1=center_x;
01258 double crpix2=center_y;
01259
01260 double crval1=0;
01261 double crval2=0;
01262
01263
01264
01265
01266 strcpy(firsttext, "sinfo_rec_objnod -f \0") ;
01267
01268 pixelscale = sinfo_pfits_get_pixscale(plist)/2. ;
01269 ra = sinfo_pfits_get_ra(plist) ;
01270 dec = sinfo_pfits_get_DEC(plist) ;
01271
01272
01273
01274
01275 ra=sinfo_pfits_get_targ_alpha(plist);
01276 dec=sinfo_pfits_get_targ_delta(plist);
01277 ra=sinfo_hms2deg(ra);
01278 dec=sinfo_sess2deg(dec);
01279
01280
01281
01282 crval1=ra;
01283 crval2=dec;
01284
01285 angle = sinfo_pfits_get_posangle(plist) ;
01286
01287 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01288 cpl_error_reset();
01289 }
01290
01291 radangle = angle * PI_NUMB / 180. ;
01292
01293 cdelt1=sign_swap * pixelscale / 3600. ;
01294 cdelt2= pixelscale / 3600. ;
01295
01296
01297 cd1_1 = +cdelt1*cos(radangle) ;
01298 cd1_2 = -cdelt2*sin(radangle) ;
01299 cd2_1 = +cdelt1*sin(radangle) ;
01300 cd2_2 = +cdelt2*cos(radangle) ;
01301
01302
01303
01304
01305
01306 check_nomsg(sinfo_set_coord1(&plist,crpix1,crval1,cdelt1));
01307 check_nomsg(sinfo_set_coord2(&plist,crpix2,crval2,cdelt2));
01308 check_nomsg(sinfo_set_cd_matrix2(&plist,cd1_1,cd1_2,cd2_1,cd2_2));
01309
01310
01311 cleanup:
01312 return;
01313 }
01314
01322 cpl_imagelist**
01323 sinfo_new_sinfoni_correct_median(cpl_imagelist** cubes, const int n_cubes)
01324 {
01325 int i=0;
01326 cpl_imagelist** cubes_cor=NULL;
01327 double local_median=0;
01328 int z=0;
01329 cpl_image* i_img=NULL;
01330 cpl_image* o_img=NULL;
01331
01332
01333 if ( cubes == NULL ) {
01334 sinfo_msg_error ("no cube list given!") ;
01335 return NULL ;
01336 }
01337 if ( n_cubes <= 0 ) {
01338 sinfo_msg_error ("wrong number of data cubes in list!") ;
01339 return NULL ;
01340 }
01341
01342 cubes_cor = (cpl_imagelist**) cpl_calloc (n_cubes, sizeof (cpl_imagelist*)) ;
01343
01344 for ( i = 0 ; i < n_cubes ; i++ ) {
01345 cubes_cor[i] = cpl_imagelist_new();
01346 for(z=0;z< cpl_imagelist_get_size(cubes[i]); z++) {
01347 i_img=cpl_imagelist_get(cubes[i],z);
01348 local_median=cpl_image_get_median(i_img);;
01349 o_img=cpl_image_duplicate(i_img);
01350 if(!isnan(local_median)) {
01351 cpl_image_subtract_scalar(o_img,local_median);
01352 }
01353 cpl_imagelist_set(cubes_cor[i],o_img,z);
01354 }
01355 }
01356
01357 return cubes_cor;
01358 }
01359
01366 int sinfo_new_sinfoni_correct_median_it(cpl_imagelist** inp)
01367 {
01368
01369 double local_median=0;
01370 int z=0;
01371 cpl_image* img=NULL;
01372
01373 for(z=0;z< cpl_imagelist_get_size((*inp)); z++) {
01374 img=cpl_imagelist_get((*inp),z);
01375 local_median=sinfo_new_my_median_image(img);
01376 if(!isnan(local_median)) {
01377 cpl_image_subtract_scalar(img,local_median);
01378 } else {
01379 sinfo_msg_error("local_median is NAN");
01380 }
01381 cpl_imagelist_set((*inp),img,z);
01382 }
01383
01384 return 0;
01385 }
01386
01396 cpl_imagelist** sinfo_new_sinfoni_correct_sky(cpl_imagelist** cubes,
01397 const int nc,
01398 cpl_imagelist* sky_cube)
01399
01400 {
01401 cpl_imagelist** cubes_sky=NULL;
01402 int x=0;
01403 int y=0;
01404 int z=0;
01405 int i=0;
01406 float k=0.5;
01407 int ovr=0;
01408 int ks=0;
01409 int nclip=0;
01410 double med=0;
01411 double avg=0;
01412 double sig=0;
01413 int msk_sum=0;
01414 double val_msk_sum=0;
01415 cpl_vector* val=NULL;
01416 cpl_vector* msk=NULL;
01417 int ilx=0;
01418 int ily=0;
01419 int inp=0;
01420
01421
01422 int sky_lx=0;
01423 int sky_ly=0;
01424
01425 float* pidata=NULL;
01426 float* p_sky_data=NULL;
01427 cpl_image* i_img=NULL;
01428 cpl_image* sky_img=NULL;
01429
01430
01431 if ( cubes == NULL ) {
01432 sinfo_msg_error ("no cube list given!") ;
01433 return NULL ;
01434 }
01435 ilx=cpl_image_get_size_x(cpl_imagelist_get(cubes[0],0));
01436 ily=cpl_image_get_size_y(cpl_imagelist_get(cubes[0],0));
01437 inp=cpl_imagelist_get_size(cubes[0]);
01438
01439
01440 sky_lx=cpl_image_get_size_x(cpl_imagelist_get(sky_cube,0));
01441 sky_ly=cpl_image_get_size_y(cpl_imagelist_get(sky_cube,0));
01442 if ( nc <= 0 ) {
01443 sinfo_msg_error ("wrong number of data cubes in list!") ;
01444 return NULL ;
01445 }
01446
01447 cubes_sky = (cpl_imagelist**) cpl_calloc (nc, sizeof (cpl_imagelist*)) ;
01448 for(z=0;z< inp; z++) {
01449 sky_img=cpl_imagelist_get(sky_cube,z);
01450 p_sky_data=cpl_image_get_data_float(sky_img);
01451 for(y=0;y< ily; y++) {
01452 for(x=0;x< ilx; x++) {
01453
01454 msk=cpl_vector_new(nc);
01455 for (i=0;i<nc;i++) {
01456 cpl_vector_set(msk,i,1);
01457 }
01458 nclip=0;
01459 for (ks=0;ks<nc;ks++) {
01460 sig=0;
01461 med=0;
01462 ovr=0;
01463 val=cpl_vector_new(nc-nclip);
01464
01465 for ( i = 0 ; i < nc ; i++ ) {
01466 i_img=cpl_imagelist_get(cubes[i],z);
01467 pidata=cpl_image_get_data_float(i_img);
01468 if ((!isnan(pidata[x+y*ilx])) &&
01469 (cpl_vector_get(msk,i) != 0) ) {
01470 cpl_vector_set(val,ovr,(double)pidata[x+y*ilx]);
01471 ovr++;
01472 }
01473 }
01474
01475 if(ovr>0) {
01476 avg=cpl_vector_get_mean(val);
01477 med=cpl_vector_get_median_const(val);
01478 if(ovr>1) {
01479 sig=cpl_vector_get_stdev(val);
01480 } else {
01481 sig=0;
01482 }
01483 } else {
01484 avg=cpl_vector_get(val,0);
01485 med=avg;
01486 sig=0;
01487 }
01488
01489 cpl_vector_delete(val);
01490 for ( i = 0 ; i < nc ; i++ ) {
01491 i_img=cpl_imagelist_get(cubes[i],z);
01492 pidata=cpl_image_get_data_float(i_img);
01493
01494 if ((!isnan(pidata[x+y*ilx])) &&
01495 (cpl_vector_get(msk,i) != 0)) {
01496 if(abs((pidata[x+y*ilx]-med))> k*sig) {
01497 cpl_vector_set(msk,i,0);
01498 nclip++;
01499 }
01500 }
01501 }
01502 }
01503 msk_sum=0;
01504 val_msk_sum=0;
01505 for ( i = 0 ; i < nc ; i++ ) {
01506
01507 if (!isnan(pidata[x+y*ilx])) {
01508 msk_sum+=cpl_vector_get(msk,i);
01509 val_msk_sum+=pidata[x+y*ilx]*
01510 cpl_vector_get(msk,i);
01511 }
01512 }
01513 p_sky_data[x+y*sky_lx]=val_msk_sum/msk_sum;
01514 cpl_vector_delete(msk);
01515 }
01516 }
01517 }
01518 for ( i = 0 ; i < nc ; i++ ) {
01519 cubes_sky[i]=cpl_imagelist_duplicate(cubes[i]);
01520
01521 cpl_imagelist_subtract(cubes_sky[i],sky_cube);
01522
01523 }
01524
01525
01526 return cubes_sky;
01527 }
01528
01543 cpl_imagelist** sinfo_new_sinfoni_correct_sky2(cpl_imagelist** cubes,
01544 const int nc,
01545 cpl_imagelist* sky_cube,
01546 cpl_imagelist* med_cube,
01547 cpl_imagelist* msk_cube,
01548 cpl_imagelist* avg_cube,
01549 cpl_imagelist* sig_cube,
01550 cpl_imagelist* ovr_cube)
01551 {
01552 cpl_imagelist** cubes_sky=NULL;
01553 int x=0;
01554 int y=0;
01555 int z=0;
01556 int i=0;
01557 float k=0.5;
01558 int ovr=0;
01559 int ks=0;
01560 int nclip=0;
01561 double med=0;
01562 double avg=0;
01563 double sig=0;
01564 int msk_sum=0;
01565 double val_msk_sum=0;
01566 cpl_vector* val=NULL;
01567 cpl_vector* msk=NULL;
01568
01569 int ilx=0;
01570 int ily=0;
01571 int inp=0;
01572
01573 int ovr_lx=0;
01574 int msk_lx=0;
01575 int avg_lx=0;
01576 int sig_lx=0;
01577 int sky_lx=0;
01578 int med_lx=0;
01579
01580
01581 float* p_ovr_data=NULL;
01582 float* p_msk_data=NULL;
01583 float* p_avg_data=NULL;
01584 float* p_sig_data=NULL;
01585 float* p_sky_data=NULL;
01586 float* p_med_data=NULL;
01587
01588 float* pidata=NULL;
01589
01590 if ( cubes == NULL ) {
01591 sinfo_msg_error ("no cube list given!") ;
01592 return NULL ;
01593 }
01594
01595 ilx=cpl_image_get_size_x(cpl_imagelist_get(cubes[0],0));
01596 ily=cpl_image_get_size_y(cpl_imagelist_get(cubes[0],0));
01597 inp=cpl_imagelist_get_size(cubes[0]);
01598
01599 if ( nc <= 0 ) {
01600 sinfo_msg_error ("wrong number of data cubes in list!") ;
01601 return NULL ;
01602 }
01603
01604 cubes_sky = (cpl_imagelist**) cpl_calloc (nc, sizeof (cpl_imagelist*)) ;
01605
01606 ovr_lx=ilx;
01607 msk_lx=ilx;
01608 avg_lx=ilx;
01609 sig_lx=ilx;
01610 sky_lx=ilx;
01611 med_lx=ilx;
01612
01613 for(z=0;z< inp; z++) {
01614 p_ovr_data=cpl_image_get_data_float(cpl_imagelist_get(ovr_cube,z));
01615 p_msk_data=cpl_image_get_data_float(cpl_imagelist_get(msk_cube,z));
01616 p_avg_data=cpl_image_get_data_float(cpl_imagelist_get(avg_cube,z));
01617 p_sig_data=cpl_image_get_data_float(cpl_imagelist_get(sig_cube,z));
01618 p_sky_data=cpl_image_get_data_float(cpl_imagelist_get(sky_cube,z));
01619 p_med_data=cpl_image_get_data_float(cpl_imagelist_get(med_cube,z));
01620
01621
01622 for(y=0;y< ily; y++) {
01623 for(x=0;x< ilx; x++) {
01624
01625 msk=cpl_vector_new(nc);
01626 for (i=0;i<nc;i++) {
01627 cpl_vector_set(msk,i,1);
01628 }
01629 p_ovr_data[x+y*ovr_lx]=nc;
01630 p_msk_data[x+y*msk_lx]=nc;
01631 nclip=0;
01632 for (ks=0;ks<nc;ks++) {
01633 sig=0;
01634 med=0;
01635 ovr=0;
01636 val=cpl_vector_new(nc-nclip);
01637
01638 for ( i = 0 ; i < nc ; i++ ) {
01639 pidata=cpl_image_get_data_float(cpl_imagelist_get(cubes[i],z));
01640 if ((!isnan(pidata[x+y*ilx])) &&
01641 (cpl_vector_get(msk,i) != 0) ) {
01642 cpl_vector_set(val,ovr,(double)pidata[x+y*ilx]);
01643 ovr++;
01644 }
01645 }
01646
01647 if(ovr>1) {
01648 avg=cpl_vector_get_mean(val);
01649 med=cpl_vector_get_median_const(val);
01650 sig=cpl_vector_get_stdev(val);
01651 } else {
01652 avg=cpl_vector_get(val,0);
01653 med=avg;
01654 sig=0;
01655 }
01656
01657 p_med_data[x+y*med_lx]=med;
01658 p_avg_data[x+y*avg_lx]=avg;
01659 p_sig_data[x+y*sig_lx]=sig;
01660 cpl_vector_delete(val);
01661 for ( i = 0 ; i < nc ; i++ ) {
01662 pidata=cpl_image_get_data_float(cpl_imagelist_get(cubes[i],z));
01663
01664 if ((!isnan(pidata[x+y*ilx])) &&
01665 (cpl_vector_get(msk,i) != 0)) {
01666 if(abs((pidata[x+y*ilx]-med))> k*sig) {
01667
01668 p_msk_data[x+y*msk_lx]-=1;
01669 cpl_vector_set(msk,i,0);
01670 nclip++;
01671 }
01672 }
01673 }
01674 }
01675 msk_sum=0;
01676 val_msk_sum=0;
01677 for ( i = 0 ; i < nc ; i++ ) {
01678 pidata=cpl_image_get_data_float(cpl_imagelist_get(cubes[i],z));
01679
01680 if (!isnan(pidata[x+y*ilx])) {
01681
01682
01683
01684
01685
01686 msk_sum+=cpl_vector_get(msk,i);
01687 val_msk_sum+=pidata[x+y*ilx]*
01688 cpl_vector_get(msk,i);
01689 }
01690 }
01691 p_sky_data[x+y*sky_lx]=val_msk_sum/msk_sum;
01692 cpl_vector_delete(msk);
01693 }
01694 }
01695 }
01696 for ( i = 0 ; i < nc ; i++ ) {
01697 cubes_sky[i]=cpl_imagelist_duplicate(cubes[i]);
01698
01699 cpl_imagelist_subtract(cubes_sky[i],sky_cube);
01700 }
01701
01702
01703 return cubes_sky;
01704 }
01705
01706
01719 int
01720 sinfo_new_assign_offset(const int n,
01721 const char* name,
01722 float* offsetx,
01723 float* offsety,
01724 const float ref_offx,
01725 const float ref_offy)
01726 {
01727
01728 float offx=0;
01729 float offy=0;
01730 double mjd_obs=0;
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741
01742
01743 cpl_propertylist * plist=NULL;
01744 sinfo_msg_debug("Assign offsets");
01745
01746 if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
01747 sinfo_msg_error( "getting header from reference frame %s",name);
01748 cpl_propertylist_delete(plist) ;
01749 return -1 ;
01750 }
01751
01752 offx = sinfo_pfits_get_cumoffsetx(plist) - ref_offx ;
01753 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01754 sinfo_msg_warning(" could not read fits header keyword cummoffsetx!") ;
01755 sinfo_msg_warning(" Set relative offset to 0 - %f!",ref_offx) ;
01756 offx = - ref_offx;
01757 cpl_error_reset();
01758
01759 }
01760
01761 offy = sinfo_pfits_get_cumoffsety(plist) - ref_offy ;
01762 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01763 sinfo_msg_warning(" could not read fits header keyword! cumoffsety") ;
01764 sinfo_msg_warning(" Set relative offset to 0 - %f!",ref_offx) ;
01765 offy = - ref_offy;
01766 cpl_error_reset();
01767
01768 }
01769 sinfo_msg_debug("offx=%f offy=%f",offx,offy);
01770
01771 if (sinfo_propertylist_has(plist, KEY_NAME_MJD_OBS)) {
01772 mjd_obs=cpl_propertylist_get_double(plist, KEY_NAME_MJD_OBS);
01773 } else {
01774 sinfo_msg_error("keyword %s does not exist",KEY_NAME_MJD_OBS);
01775 cpl_propertylist_delete(plist) ;
01776 return -1;
01777 }
01778
01779 cpl_propertylist_delete(plist) ;
01780
01781 if (mjd_obs > 53825. ) {
01782
01783
01784 sinfo_new_array_set_value(offsetx,2*offx,n);
01785 sinfo_new_array_set_value(offsety,2*offy,n);
01786 } else if ((mjd_obs > 53421.58210082 ) && (mjd_obs <= 53825.)){
01787
01788
01789
01790
01791
01792 sinfo_new_array_set_value(offsetx,-2*offx,n);
01793 sinfo_new_array_set_value(offsety,2*offy,n);
01794 } else {
01795
01796
01797
01798
01799
01800 sinfo_new_array_set_value(offsetx,2*offx,n);
01801 sinfo_new_array_set_value(offsety,-2*offy,n);
01802 }
01803
01804 return 0;
01805
01806
01807 }
01808
01809
01810
01811
01812
01825 int
01826 sinfo_new_assign_offset2(const int n,
01827 const char* name,
01828 float* offsetx,
01829 float* offsety,
01830 const float ref_offx,
01831 const float ref_offy)
01832 {
01833
01834 float offx=0;
01835 float offy=0;
01836 double mjd_obs=0;
01837
01838 cpl_propertylist * plist=NULL;
01839 sinfo_msg_debug("Assign offsets as set by user");
01840 offx = offsetx[n] - ref_offx ;
01841 offy = offsety[n] - ref_offy ;
01842 sinfo_msg_debug("offx=%f offy=%f",offx,offy);
01843
01844 sinfo_msg_debug("Assign offsets");
01845
01846 if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
01847 sinfo_msg_error( "getting header from reference frame %s",name);
01848 cpl_propertylist_delete(plist) ;
01849 return -1 ;
01850 }
01851
01852 if (sinfo_propertylist_has(plist, KEY_NAME_MJD_OBS)) {
01853 mjd_obs=cpl_propertylist_get_double(plist, KEY_NAME_MJD_OBS);
01854 } else {
01855 sinfo_msg_error("keyword %s does not exist",KEY_NAME_MJD_OBS);
01856 cpl_propertylist_delete(plist) ;
01857 return -1;
01858 }
01859 cpl_propertylist_delete(plist) ;
01860
01861
01862
01863
01864 if (mjd_obs > 53825. ) {
01865
01866
01867 sinfo_new_array_set_value(offsetx,2*offx,n);
01868 sinfo_new_array_set_value(offsety,2*offy,n);
01869 } else if ((mjd_obs > 53421.58210082 ) && (mjd_obs <= 53825.)){
01870
01871
01872
01873
01874
01875 sinfo_new_array_set_value(offsetx,-2*offx,n);
01876 sinfo_new_array_set_value(offsety,2*offy,n);
01877 } else {
01878
01879
01880
01881
01882
01883 sinfo_new_array_set_value(offsetx,2*offx,n);
01884 sinfo_new_array_set_value(offsety,-2*offy,n);
01885 }
01886
01887
01888 return 0;
01889
01890
01891 }
01892
01893
01906 int
01907 sinfo_new_object_assign_offset(const char* name,
01908 const int n,
01909 double* ref_offx,
01910 double* ref_offy,
01911 float** offsetx,
01912 float** offsety)
01913 {
01914
01915 float offx=0;
01916 float offy=0;
01917 double mjd_obs=0;
01918 cpl_propertylist * plist=NULL;
01919 sinfo_msg_debug("Assign offsets");
01920
01921 if ((cpl_error_code)((plist = cpl_propertylist_load(name, 0)) == NULL)) {
01922 sinfo_msg_error( "getting header from reference frame %s",name);
01923 cpl_propertylist_delete(plist) ;
01924 return -1 ;
01925 }
01926 if ( n == 0 ) {
01927
01928 *ref_offx = sinfo_pfits_get_cumoffsetx(plist) ;
01929 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01930 sinfo_msg_error(" could not read fits header keyword cummoffsetx!") ;
01931
01932 cpl_error_reset();
01933 }
01934
01935 *ref_offy = sinfo_pfits_get_cumoffsety(plist) ;
01936 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01937 sinfo_msg_error(" could not read fits header keyword! cumoffsety") ;
01938 cpl_error_reset();
01939
01940 }
01941 sinfo_msg_debug("Reference offx=%f offy=%f",*ref_offx,*ref_offy);
01942
01943 offx = 0. ;
01944 offy = 0. ;
01945
01946 } else {
01947
01948 offx = sinfo_pfits_get_cumoffsetx(plist) - *ref_offx ;
01949 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01950 sinfo_msg_error(" could not read fits header keyword cummoffsetx!") ;
01951 cpl_error_reset();
01952
01953 }
01954
01955 offy = sinfo_pfits_get_cumoffsety(plist) - *ref_offy ;
01956 if(cpl_error_get_code() != CPL_ERROR_NONE) {
01957 sinfo_msg_error(" could not read fits header keyword! cumoffsety") ;
01958
01959 cpl_error_reset();
01960 }
01961 sinfo_msg_debug("offx=%f offy=%f",offx,offy);
01962 }
01963
01964
01965
01966
01967
01968
01969
01970
01971
01972
01973
01974
01975
01976 if (sinfo_propertylist_has(plist, KEY_NAME_MJD_OBS)) {
01977 mjd_obs=cpl_propertylist_get_double(plist, KEY_NAME_MJD_OBS);
01978 } else {
01979 sinfo_msg_error("keyword %s does not exist",KEY_NAME_MJD_OBS);
01980 cpl_propertylist_delete(plist) ;
01981 return -1;
01982 }
01983
01984 cpl_propertylist_delete(plist) ;
01985
01986 if (mjd_obs > 53825. ) {
01987
01988
01989 sinfo_new_array_set_value(*offsetx,2*offx,n);
01990 sinfo_new_array_set_value(*offsety,2*offy,n);
01991 } else if ((mjd_obs > 53421.58210082 ) && (mjd_obs <= 53825.)){
01992
01993 sinfo_new_array_set_value(*offsetx,-offx*2,n);
01994 sinfo_new_array_set_value(*offsety,+offy*2,n);
01995 } else {
01996
01997 sinfo_new_array_set_value(*offsetx,+offx*2,n);
01998 sinfo_new_array_set_value(*offsety,-offy*2,n);
01999 }
02000
02001 return 0;
02002 }
02003
02015 cpl_imagelist*
02016 sinfo_new_fine_tune(cpl_imagelist* cube,
02017 float* correct_dist,
02018 const char* method,
02019 const int order,
02020 const int nslits) {
02021 int i =0;
02022 cpl_imagelist* outcube2=NULL;
02023 float* neg_dist=NULL;
02024 sinfo_msg("Finetuning, method=%s",method);
02025
02026 if (strcmp(method,"P")==0)
02027 {
02028 outcube2 = sinfo_new_fine_tune_cube( cube, correct_dist, order ) ;
02029 if (outcube2 == NULL)
02030 {
02031 sinfo_msg_error (" could not fine tune the data cube\n") ;
02032 return NULL ;
02033 }
02034 }
02035 else if (strcmp(method,"F")==0)
02036 {
02037 neg_dist=cpl_calloc(nslits,sizeof(float));
02038 for ( i = 0 ; i < nslits ; i++ )
02039 {
02040 neg_dist[i] = -correct_dist[i] ;
02041 }
02042 outcube2 = sinfo_new_fine_tune_cube_by_FFT( cube, neg_dist ) ;
02043 cpl_free(neg_dist);
02044 if ( outcube2 == NULL )
02045 {
02046 sinfo_msg_error (" could not fine tune the data cube\n") ;
02047 return NULL ;
02048 }
02049 }
02050 else if (strcmp(method,"S")==0)
02051 {
02052 outcube2 = sinfo_new_fine_tune_cube_by_spline( cube, correct_dist ) ;
02053 if ( outcube2 == NULL )
02054 {
02055 sinfo_msg_error (" could not fine tune the data cube\n") ;
02056 return NULL ;
02057 }
02058 }
02059 else
02060 {
02061 sinfo_msg_error (" wrong method indicator given!") ;
02062 return NULL ;
02063 }
02064
02065
02066
02067 return outcube2;
02068
02069 }
02070
02071
02077
02078 sinfo_band sinfo_get_associated_filter(const char * f)
02079 {
02080 if (!strcmp(f, "J")) return SINFO_BAND_J ;
02081 if (!strcmp(f, "Js")) return SINFO_BAND_JS ;
02082 if (!strcmp(f, "Z")) return SINFO_BAND_Z ;
02083 if (!strcmp(f, "SZ")) return SINFO_BAND_SZ ;
02084 if (!strcmp(f, "SH")) return SINFO_BAND_SH ;
02085 if (!strcmp(f, "H")) return SINFO_BAND_H ;
02086 if (!strcmp(f, "Ks")) return SINFO_BAND_KS ;
02087 if (!strcmp(f, "K")) return SINFO_BAND_K ;
02088 if (!strcmp(f, "SK")) return SINFO_BAND_SK ;
02089 if (!strcmp(f, "L")) return SINFO_BAND_L ;
02090 if (!strcmp(f, "SL")) return SINFO_BAND_SL ;
02091 if (!strcmp(f, "M")) return SINFO_BAND_M ;
02092 if (!strcmp(f, "M_NB")) return SINFO_BAND_M ;
02093 if (!strcmp(f, "NB_1.06")) return SINFO_BAND_J ;
02094 if (!strcmp(f, "NB_1.08")) return SINFO_BAND_J ;
02095 if (!strcmp(f, "NB_1.19")) return SINFO_BAND_J ;
02096 if (!strcmp(f, "NB_1.21")) return SINFO_BAND_J ;
02097 if (!strcmp(f, "NB_1.26")) return SINFO_BAND_J ;
02098 if (!strcmp(f, "NB_1.28")) return SINFO_BAND_J ;
02099 if (!strcmp(f, "NB_1.64")) return SINFO_BAND_H ;
02100 if (!strcmp(f, "NB_1.71")) return SINFO_BAND_H ;
02101 if (!strcmp(f, "NB_2.07")) return SINFO_BAND_KS ;
02102 if (!strcmp(f, "NB_2.09")) return SINFO_BAND_KS ;
02103 if (!strcmp(f, "NB_2.13")) return SINFO_BAND_KS ;
02104 if (!strcmp(f, "NB_2.17")) return SINFO_BAND_KS ;
02105 if (!strcmp(f, "NB_2.19")) return SINFO_BAND_KS ;
02106 if (!strcmp(f, "NB_2.25")) return SINFO_BAND_KS ;
02107 if (!strcmp(f, "NB_2.29")) return SINFO_BAND_KS ;
02108 if (!strcmp(f, "NB_2.34")) return SINFO_BAND_KS ;
02109 if (!strcmp(f, "NB_3.21")) return SINFO_BAND_L ;
02110 if (!strcmp(f, "NB_3.28")) return SINFO_BAND_L ;
02111 if (!strcmp(f, "NB_3.80")) return SINFO_BAND_L ;
02112 if (!strcmp(f, "NB_4.07")) return SINFO_BAND_L ;
02113 return SINFO_BAND_UNKNOWN ;
02114 }
02115
02116
02117
02118
02124
02125 sinfo_band sinfo_get_bbfilter(const char * f)
02126 {
02127 if (!strcmp(f, "J")) return SINFO_BAND_J ;
02128 if (!strcmp(f, "J+Block")) return SINFO_BAND_J ;
02129 if (!strcmp(f, "Js")) return SINFO_BAND_J ;
02130 if (!strcmp(f, "Z")) return SINFO_BAND_J ;
02131 if (!strcmp(f, "SZ")) return SINFO_BAND_J ;
02132 if (!strcmp(f, "SH")) return SINFO_BAND_H ;
02133 if (!strcmp(f, "H")) return SINFO_BAND_H ;
02134 if (!strcmp(f, "Ks")) return SINFO_BAND_KS ;
02135 if (!strcmp(f, "K")) return SINFO_BAND_K ;
02136 if (!strcmp(f, "SK")) return SINFO_BAND_K ;
02137 if (!strcmp(f, "L")) return SINFO_BAND_L ;
02138 if (!strcmp(f, "SL")) return SINFO_BAND_L ;
02139 if (!strcmp(f, "M")) return SINFO_BAND_M ;
02140 if (!strcmp(f, "M_NB")) return SINFO_BAND_M ;
02141 if (!strcmp(f, "NB_1.06")) return SINFO_BAND_J ;
02142 if (!strcmp(f, "NB_1.08")) return SINFO_BAND_J ;
02143 if (!strcmp(f, "NB_1.19")) return SINFO_BAND_J ;
02144 if (!strcmp(f, "NB_1.21")) return SINFO_BAND_J ;
02145 if (!strcmp(f, "NB_1.26")) return SINFO_BAND_J ;
02146 if (!strcmp(f, "NB_1.28")) return SINFO_BAND_J ;
02147 if (!strcmp(f, "NB_1.64")) return SINFO_BAND_H ;
02148 if (!strcmp(f, "NB_1.71")) return SINFO_BAND_H ;
02149 if (!strcmp(f, "NB_2.07")) return SINFO_BAND_KS ;
02150 if (!strcmp(f, "NB_2.09")) return SINFO_BAND_KS ;
02151 if (!strcmp(f, "NB_2.13")) return SINFO_BAND_KS ;
02152 if (!strcmp(f, "NB_2.17")) return SINFO_BAND_KS ;
02153 if (!strcmp(f, "NB_2.19")) return SINFO_BAND_KS ;
02154 if (!strcmp(f, "NB_2.25")) return SINFO_BAND_KS ;
02155 if (!strcmp(f, "NB_2.29")) return SINFO_BAND_KS ;
02156 if (!strcmp(f, "NB_2.34")) return SINFO_BAND_KS ;
02157 if (!strcmp(f, "NB_3.21")) return SINFO_BAND_L ;
02158 if (!strcmp(f, "NB_3.28")) return SINFO_BAND_L ;
02159 if (!strcmp(f, "NB_3.80")) return SINFO_BAND_L ;
02160 if (!strcmp(f, "NB_4.07")) return SINFO_BAND_L ;
02161 return SINFO_BAND_UNKNOWN ;
02162 }
02163
02164