00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030
00035
00038
00039
00040
00041
00042 #include <tests.h>
00043 #include <xsh_data_pre.h>
00044 #include <xsh_error.h>
00045 #include <xsh_msg.h>
00046 #include <xsh_data_instrument.h>
00047 #include <xsh_data_rec.h>
00048 #include <xsh_data_localization.h>
00049 #include <xsh_drl.h>
00050 #include <xsh_pfits.h>
00051 #include <xsh_data_pre_3d.h>
00052 #include <xsh_badpixelmap.h>
00053
00054 #include <cpl.h>
00055 #include <math.h>
00056
00057 #include <getopt.h>
00058
00059
00060
00061
00062
00063 #define MODULE_ID "XSH_SPECTRUM_MERGE3D"
00064
00065 static const char * Options = "" ;
00066
00067 enum {
00068 ZCHUNK_HSIZE_OPT
00069 };
00070
00071 static struct option long_options[] = {
00072 {"lambda-chunk-hsize", required_argument, 0, ZCHUNK_HSIZE_OPT},
00073 {0, 0, 0, 0}
00074 };
00075
00076 static void Help( void )
00077 {
00078 puts( "Creates for each slitlet:");
00079 puts( " - a median profile along the slit");
00080 puts( " - a localization as a function of wavelength");
00081
00082 puts( "Usage: test_xsh_data_spectrum_merge_3d [options] CUBE SKY_LINE");
00083
00084 puts( "Options" ) ;
00085 puts( " --lambda-chunk-hsize=<n> : Chunk half size in pixel [50]");
00086
00087 puts( "\nInput Files" ) ;
00088 puts( "CUBE : data cube (tag = *_IFU_MERGE3D_IFU_UVB)" );
00089 puts( "SKY_LINE [OPTIONAL] : sky lines (tag = SKY_LINE_LIST_arm)" );
00090
00091 puts( "\nOutput Files");
00092 puts( "1) MEDIAN_CUBE.fits : median profile in fits image");
00093 puts( "2) median_profile_slitbin_*.dat : ASCII profile along the slit direction for every slitlet by collapsing in wavelength");
00094 puts( "3) profile_lambda_*.dat : ASCII file containing for each chunk around lref gaussian central fitting positions");
00095 puts( "4) gsl_locfit_*.dat : ASCII central slit position for each slitlet");
00096 TEST_END();
00097 }
00098
00099
00100 static void HandleOptions( int argc, char **argv,
00101 int *zchunk_hsize)
00102 {
00103 int opt ;
00104 int option_index = 0;
00105
00106 while (( opt = getopt_long (argc, argv, Options,
00107 long_options, &option_index)) != EOF )
00108 switch ( opt ) {
00109 case ZCHUNK_HSIZE_OPT:
00110 *zchunk_hsize = atoi( optarg);
00111 break;
00112 default: Help() ; exit( 0);
00113 }
00114 }
00115
00116
00117
00118
00126 int main( int argc, char **argv)
00127 {
00128 int ret;
00129
00130 const char* sci_name = NULL;
00131 cpl_frame *sci_frame = NULL;
00132
00133 const char* sky_name = NULL;
00134 cpl_frame *sky_frame = NULL;
00135
00136 xsh_pre_3d *cube = NULL;
00137 xsh_image_3d *cube_data_img = NULL;
00138 int cube_x, cube_y, cube_z;
00139 float *cube_data = NULL;
00140 double *median = NULL;
00141 int median_size =0;
00142 double *coadd =NULL;
00143 int zchunk_hsize = 100;
00144 cpl_vector *med_vect = NULL;
00145 cpl_vector *coadd_vect = NULL;
00146 cpl_vector *positions = NULL;
00147 int x,y,z;
00148 double med_z;
00149 cpl_image *med_img = NULL;
00150 double *med_img_data = NULL;
00151 FILE *profil_file = NULL;
00152 char filename[256];
00153 int zmed;
00154 cpl_propertylist *header = NULL;
00155 double crval1, crval2, crval3;
00156 double crpix1, crpix2, crpix3;
00157 double cdelt1, cdelt2, cdelt3;
00158 double x0= 0.0,sigma =0.0, area=0.0, offset=0.0;
00159 int bad_fit;
00160 xsh_instrument *instrument = NULL;
00161
00162
00163 TESTS_INIT( MODULE_ID);
00164 cpl_msg_set_level( CPL_MSG_DEBUG);
00165 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM);
00166
00167 HandleOptions( argc, argv, &zchunk_hsize);
00168
00169
00170 if (argc-optind >= 1 ) {
00171 sci_name = argv[optind];
00172 if (argc-optind >= 2 ) {
00173 sky_name = argv[optind+1];
00174 }
00175 }
00176 else{
00177 Help();
00178 exit(0);
00179 }
00180
00181 TESTS_XSH_FRAME_CREATE( sci_frame, "CUBE_arm", sci_name);
00182 if ( sky_name != NULL){
00183 TESTS_XSH_FRAME_CREATE( sky_frame, "SKY_LINE_LIST_arm", sky_name);
00184 }
00185
00186
00187 xsh_msg("CUBE : %s",
00188 cpl_frame_get_filename( sci_frame));
00189 if ( sky_frame != NULL){
00190 xsh_msg("SKY LINE LIST : %s",
00191 cpl_frame_get_filename( sky_frame));
00192 }
00193 else{
00194 xsh_msg("Don't use sky line list!!");
00195 }
00196
00197 check( instrument = create_instrument( sci_name));
00198 xsh_instrument_set_mode( instrument, XSH_MODE_IFU);
00199
00200 check( cube = xsh_pre_3d_load( sci_frame));
00201 header = cube->data_header;
00202
00203 check( cube_data_img = xsh_pre_3d_get_data( cube));
00204 check( cube_x = xsh_image_3d_get_size_x( cube_data_img));
00205 check( cube_y = xsh_image_3d_get_size_y( cube_data_img));
00206 check( cube_z = xsh_image_3d_get_size_z( cube_data_img));
00207
00208 xsh_msg(" Cube SLITLET*SLIT*LAMBDAS (%dx%dx%d)", cube_x, cube_y, cube_z);
00209
00210 check( crpix1 = xsh_pfits_get_crpix1( header));
00211 check( crval1 = xsh_pfits_get_crval1( header));
00212 check( cdelt1 = xsh_pfits_get_cdelt1( header));
00213
00214 check( crpix2 = xsh_pfits_get_crpix2( header));
00215 check( crval2 = xsh_pfits_get_crval2( header));
00216 check( cdelt2 = xsh_pfits_get_cdelt2( header));
00217
00218 check( crpix3 = xsh_pfits_get_crpix3( header));
00219 check( crval3 = xsh_pfits_get_crval3( header));
00220 check( cdelt3 = xsh_pfits_get_cdelt3( header));
00221
00222 xsh_msg(" SLITLET pix %f ref %f with step %f", crpix1, crval1, cdelt1);
00223 xsh_msg(" SLIT pix %f ref %f with step %f", crpix2, crval2, cdelt2);
00224 xsh_msg(" LAMBDAS pix %f ref %f with step %f", crpix3, crval3, cdelt3);
00225
00226 check( cube_data = (float*)xsh_image_3d_get_data( cube_data_img));
00227
00228
00229 XSH_MALLOC( median, double, cube_z);
00230 check( med_vect = cpl_vector_wrap( cube_z, median));
00231 check( med_img = cpl_image_new( cube_x, cube_y, CPL_TYPE_DOUBLE));
00232 check( med_img_data = cpl_image_get_data_double( med_img));
00233
00234 for( y=0; y<cube_y; y++){
00235 for( x=0; x<cube_x; x++){
00236 for(z=0; z< cube_z; z++){
00237 median[z] = cube_data[cube_x*cube_y*z+cube_x*y+x];
00238 }
00239 check (med_z = cpl_vector_get_median( med_vect));
00240 med_img_data[y*cube_x+x] = med_z;
00241 }
00242 }
00243 check( cpl_image_save( med_img, "MEDIAN_CUBE.fits", CPL_BPP_IEEE_DOUBLE,
00244 NULL, CPL_IO_CREATE));
00245
00246 sprintf( filename, "median_profile_slitbin_%.3f.dat", cdelt2);
00247 profil_file = fopen( filename, "w");
00248 fprintf( profil_file, "#slit UP CEN LO\n");
00249
00250 for( y=0; y<cube_y; y++){
00251 fprintf( profil_file, "%f ", crval2+y*cdelt2);
00252 for( x=0; x<cube_x; x++){
00253 fprintf( profil_file, "%f ", med_img_data[y*cube_x+x]);
00254 }
00255 fprintf( profil_file, "\n");
00256 }
00257 fclose( profil_file);
00258
00259 XSH_MALLOC( coadd, double, cube_y);
00260 check( coadd_vect = cpl_vector_wrap( cube_y, coadd));
00261 check( positions = cpl_vector_new( cube_y));
00262
00263 for( y=0; y<cube_y; y++){
00264 cpl_vector_set( positions, y, y);
00265 cpl_vector_set( coadd_vect, y, med_img_data[y*cube_x+1]);
00266 }
00267 cpl_vector_fit_gaussian( positions, NULL, coadd_vect, NULL,CPL_FIT_ALL,&x0,&sigma,&area,&offset,NULL,NULL,NULL);
00268
00269 if (cpl_error_get_code() != CPL_ERROR_NONE){
00270 xsh_error_reset();
00271 }
00272 else{
00273 sprintf( filename, "gaussian_fit.dat");
00274 profil_file = fopen( filename, "w");
00275 fprintf( profil_file, "#slit cen gauss_cen\n");
00276
00277 for( y=0; y<cube_y; y++){
00278 double sval = 0.0;
00279 double cen_val = 0.0;
00280 double gauss_val = 0.0;
00281 double z;
00282
00283 z = (y-x0)/(sigma*XSH_MATH_SQRT_2);
00284 gauss_val = area / sqrt(2 *M_PI*sigma*sigma)*exp(-(z*z))+offset;
00285 sval = crval2+y*cdelt2;
00286 cen_val = cpl_vector_get( coadd_vect, y);
00287
00288 fprintf( profil_file, "%f %f %f\n", sval, cen_val, gauss_val);
00289 }
00290
00291 fclose( profil_file);
00292 xsh_msg(" Median cube central slitlet gaussian fit %f arcsec\n", crval2+x0*cdelt2);
00293 }
00294
00295
00296 sprintf( filename, "profile_lambda_%d.dat", zchunk_hsize);
00297
00298 profil_file = fopen( filename, "w");
00299
00300 median_size = 0;
00301 bad_fit = 0;
00302 xsh_unwrap_vector( &med_vect);
00303
00304 fprintf( profil_file, "#lref up cen low\n");
00305 for(z=zchunk_hsize; z< cube_z; z+=zchunk_hsize*2){
00306 double cen[3];
00307
00308 for( x=0; x<cube_x; x++){
00309
00310 for( y=0; y<cube_y; y++){
00311 coadd[y]=0;
00312 }
00313 for(zmed=z-zchunk_hsize; zmed< z+zchunk_hsize; zmed++){
00314 for( y=0; y<cube_y; y++){
00315 coadd[y] += cube_data[cube_x*cube_y*zmed+cube_x*y+x];
00316 }
00317 }
00318 cpl_vector_fit_gaussian( positions, NULL, coadd_vect, NULL,CPL_FIT_ALL,&x0,&sigma,&area,&offset,NULL,NULL,NULL);
00319 if (cpl_error_get_code() != CPL_ERROR_NONE){
00320 xsh_error_reset();
00321 cen[x]=-1;
00322 if (x == 1){
00323 bad_fit++;
00324 }
00325 }
00326 else{
00327 cen[x] = crval2+x0*cdelt2;
00328 if (x == 1){
00329 median[median_size] = cen[1];
00330 median_size++;
00331 }
00332 }
00333 }
00334 fprintf( profil_file, "%f %f %f %f\n", crval3+z*cdelt3, cen[0], cen[1], cen[2]);
00335 }
00336 fclose( profil_file);
00337 check( med_vect = cpl_vector_wrap( median_size, median));
00338 med_z = cpl_vector_get_median( med_vect);
00339 xsh_msg(" Statistics of gaussian fit bad %d lines, good %d lines",
00340 bad_fit, median_size);
00341 xsh_msg(" Median from gaussian fit %f arcsec\n", med_z);
00342
00343
00344 for( x=0; x< cube_x; x++){
00345 double init_par[6];
00346 double fit_errs[6];
00347 int status;
00348 float slitcen, fit_err;
00349
00350 sprintf( filename, "gsl_localize_pos%d.dat", x);
00351 profil_file = fopen( filename, "w");
00352 fprintf( profil_file, "#wavelength slit_fit fit_err\n");
00353
00354 for( z=0; z < cube_z; z++){
00355 for( y=0; y < cube_y; y++){
00356 cpl_vector_set( coadd_vect, y, cube_data[cube_x*cube_y*z+cube_x*y+x]);
00357 }
00358 xsh_gsl_init_gaussian_fit( positions, coadd_vect, init_par);
00359 xsh_gsl_fit_gaussian( positions, coadd_vect, 0, init_par, fit_errs, &status);
00360 slitcen = crval2+init_par[4]*cdelt2;
00361 fit_err = fit_errs[4]*cdelt2;
00362 fprintf( profil_file, "%f %f %f\n", crval3+z*cdelt3, slitcen, fit_err);
00363 }
00364 fclose( profil_file);
00365 }
00366
00367 check( xsh_center_cube( sci_frame, sky_frame, zchunk_hsize*2,
00368 instrument));
00369
00370 cleanup:
00371 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00372 xsh_error_dump(CPL_MSG_ERROR);
00373 ret = 1;
00374 }
00375 xsh_instrument_free( &instrument);
00376 xsh_free_frame( &sci_frame);
00377 xsh_pre_3d_free( &cube);
00378 XSH_FREE( median);
00379 XSH_FREE( coadd);
00380 xsh_unwrap_vector( &coadd_vect);
00381 xsh_unwrap_vector( &med_vect);
00382 xsh_free_image( &med_img);
00383 TEST_END();
00384 return ret;
00385 }
00386