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 ZSTEP_OPT, ZCHUNK_HSIZE_OPT, ZMETHOD_OPT
00069 };
00070
00071 static struct option long_options[] = {
00072 {"z-step", required_argument, 0, ZSTEP_OPT},
00073 {"z-chunk-hsize", required_argument, 0, ZCHUNK_HSIZE_OPT},
00074 {"z-method", required_argument, 0, ZMETHOD_OPT},
00075 {0, 0, 0, 0}
00076 };
00077
00078 static void Help( void )
00079 {
00080 puts( "Unitary test of a cube");
00081 puts( "Usage: test_xsh_data_spectrum_merge_3d [options] <input_files>");
00082
00083 puts( "Options" ) ;
00084 puts( " --z-step=<n>");
00085 puts( " --z-chunk-hsize=<n>");
00086 puts( " --z-method : MAX=0 | GAUSSIAN=1 [1]");
00087 puts( "\nInput Files" ) ;
00088 puts( "The input files argument MUST be in this order:" ) ;
00089 puts( " 1. Cube" ) ;
00090 TEST_END();
00091 }
00092
00093
00094 static void HandleOptions( int argc, char **argv,
00095 int *zstep, int *zchunk_hsize, int *zmethod)
00096 {
00097 int opt ;
00098 int option_index = 0;
00099
00100 while (( opt = getopt_long (argc, argv, Options,
00101 long_options, &option_index)) != EOF )
00102 switch ( opt ) {
00103 case ZSTEP_OPT:
00104 *zstep = atoi( optarg);
00105 break;
00106 case ZCHUNK_HSIZE_OPT:
00107 *zchunk_hsize = atoi( optarg);
00108 break;
00109 case ZMETHOD_OPT:
00110 *zmethod = 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 xsh_instrument* instrument = NULL;
00131 const char* sci_name = NULL;
00132 cpl_frame *sci_frame = NULL;
00133
00134 xsh_pre_3d *cube = NULL;
00135 xsh_image_3d *cube_data_img = NULL;
00136 int cube_x, cube_y, cube_z;
00137 float *cube_data = NULL;
00138 double *median = NULL;
00139 double *coadd =NULL;
00140 cpl_vector *med_vect = NULL;
00141 cpl_vector *coadd_vect = NULL;
00142 cpl_vector *positions = NULL;
00143 int x,y,z;
00144 double med_z;
00145 cpl_image *med_img = NULL;
00146 double *med_img_data = NULL;
00147 double *profil = NULL;
00148 cpl_vector *profil_vect = NULL;
00149 FILE *profil_file = NULL;
00150 char filename[256];
00151 int zmed;
00152 cpl_propertylist *header = NULL;
00153 double crval1, crval2, crval3;
00154 double crpix1, crpix2, crpix3;
00155 double cdelt1, cdelt2, cdelt3;
00156
00157
00158 TESTS_INIT( MODULE_ID);
00159 cpl_msg_set_level( CPL_MSG_DEBUG);
00160 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM);
00161
00162
00163 if (argc >= 2 ) {
00164 sci_name = argv[1];
00165 }
00166 else{
00167 Help();
00168 exit(0);
00169 }
00170
00171 TESTS_XSH_FRAME_CREATE( sci_frame, "CUBE_arm", sci_name);
00172
00173
00174 xsh_msg("CUBE : %s",
00175 cpl_frame_get_filename( sci_frame));
00176
00177 xsh_msg(" Parameters ");
00178
00179 check( cube = xsh_pre_3d_load( sci_frame));
00180 header = cube->data_header;
00181
00182 check( cube_data_img = xsh_pre_3d_get_data( cube));
00183 check( cube_x = xsh_image_3d_get_size_x( cube_data_img));
00184 check( cube_y = xsh_image_3d_get_size_y( cube_data_img));
00185 check( cube_z = xsh_image_3d_get_size_z( cube_data_img));
00186
00187 xsh_msg(" Cube SLITLET*SLIT*LAMBDAS (%dx%dx%d)", cube_x, cube_y, cube_z);
00188
00189 check( crpix1 = xsh_pfits_get_crpix1( header));
00190 check( crval1 = xsh_pfits_get_crval1( header));
00191 check( cdelt1 = xsh_pfits_get_cdelt1( header));
00192
00193 check( crpix2 = xsh_pfits_get_crpix2( header));
00194 check( crval2 = xsh_pfits_get_crval2( header));
00195 check( cdelt2 = xsh_pfits_get_cdelt2( header));
00196
00197 check( crpix3 = xsh_pfits_get_crpix3( header));
00198 check( crval3 = xsh_pfits_get_crval3( header));
00199 check( cdelt3 = xsh_pfits_get_cdelt3( header));
00200
00201 xsh_msg(" SLITLET pix %f ref %f with step %f", crpix1, crval1, cdelt1);
00202 xsh_msg(" SLIT pix %f ref %f with step %f", crpix2, crval2, cdelt2);
00203 xsh_msg(" LAMBDAS pix %f ref %f with step %f", crpix3, crval3, cdelt3);
00204
00205 check( cube_data = (float*)xsh_image_3d_get_data( cube_data_img));
00206
00207 XSH_MALLOC( median, double, cube_z);
00208 check( med_vect = cpl_vector_wrap( cube_z, median));
00209 check( med_img = cpl_image_new( cube_x, cube_y, CPL_TYPE_DOUBLE));
00210 check( med_img_data = cpl_image_get_data_double( med_img));
00211
00212 for( y=0; y<cube_y; y++){
00213 for( x=0; x<cube_x; x++){
00214 for(z=0; z< cube_z; z++){
00215 median[z] = cube_data[cube_x*cube_y*z+cube_x*y+x];
00216 }
00217 check (med_z = cpl_vector_get_median( med_vect));
00218 med_img_data[y*cube_x+x] = med_z;
00219 }
00220 }
00221 check( cpl_image_save( med_img, "MEDIAN_CUBE.fits", CPL_BPP_IEEE_DOUBLE,
00222 NULL, CPL_IO_CREATE));
00223
00224 sprintf( filename, "profil_%.3f.dat", cdelt2);
00225 profil_file = fopen( filename, "w");
00226 fprintf( profil_file, "#slit UP CEN LO\n");
00227
00228 for( y=0; y<cube_y; y++){
00229 fprintf( profil_file, "%f ", crval2+(y+0.5)*cdelt2);
00230 for( x=0; x<cube_x; x++){
00231 fprintf( profil_file, "%f ", med_img_data[y*cube_x+x]);
00232 }
00233 fprintf( profil_file, "\n");
00234 }
00235 fclose( profil_file);
00236
00237
00238 XSH_MALLOC( coadd, double, cube_y);
00239 check( coadd_vect = cpl_vector_wrap( cube_y, coadd));
00240 check( positions = cpl_vector_new( cube_y));
00241
00242 for( y=0; y<cube_y; y++){
00243 cpl_vector_set( positions, y, y);
00244 }
00245
00246 profil_file = fopen( "cube.dat", "w");
00247
00248
00249 fprintf( profil_file, "# z lref up cen low\n");
00250 for(z=10; z< cube_z; z+=100){
00251 double cen[3];
00252 double x0= 0.0,sigma =0.0, area=0.0, offset=0.0;
00253
00254 for( x=0; x<cube_x; x++){
00255
00256 for( y=0; y<cube_y; y++){
00257 coadd[y]=0;
00258 }
00259 for(zmed=z-10; zmed< z+10; zmed++){
00260 int max_pos = 0;
00261 double max =0;
00262
00263 for( y=0; y<cube_y; y++){
00264 coadd[y] += cube_data[cube_x*cube_y*zmed+cube_x*y+x];
00265 }
00266 }
00267 cpl_vector_fit_gaussian( positions, NULL, coadd_vect, NULL,CPL_FIT_ALL,&x0,&sigma,&area,&offset,NULL,NULL,NULL);
00268 if (cpl_error_get_code() != CPL_ERROR_NONE){
00269 xsh_error_reset();
00270 for( y=0; y<cube_y; y++){
00271 xsh_msg("NO FIT %f %f\n", cpl_vector_get( positions, y), cpl_vector_get( coadd_vect, y));
00272 }
00273 cen[x]=-1;
00274 }
00275 else{
00276 cen[x] = x0;
00277 }
00278 }
00279 fprintf( profil_file, "%d %f %f %f %f\n", z, crval3+z*cdelt3, cen[0], cen[1], cen[2]);
00280 }
00281 fclose( profil_file);
00282
00283 cleanup:
00284 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00285 xsh_error_dump(CPL_MSG_ERROR);
00286 ret = 1;
00287 }
00288 xsh_free_frame( &sci_frame);
00289 xsh_pre_3d_free( &cube);
00290 XSH_FREE( median);
00291 XSH_FREE( coadd);
00292 xsh_unwrap_vector( &coadd_vect);
00293 xsh_unwrap_vector( &med_vect);
00294 xsh_free_image( &med_img);
00295 TEST_END();
00296 return ret;
00297 }
00298