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
00036
00039
00040
00041
00042
00043 #include <tests.h>
00044 #include <xsh_data_pre.h>
00045 #include <xsh_error.h>
00046 #include <xsh_msg.h>
00047 #include <xsh_data_instrument.h>
00048 #include <xsh_data_rec.h>
00049 #include <xsh_data_spectrum.h>
00050 #include <xsh_drl.h>
00051 #include <xsh_pfits.h>
00052 #include <xsh_badpixelmap.h>
00053 #include <cpl.h>
00054 #include <math.h>
00055 #include <getopt.h>
00056 #include <xsh_ifu_defs.h>
00057
00058
00059
00060
00061 #define MODULE_ID "XSH_MERGE_ORD"
00062
00063 static void Help( void )
00064 {
00065 puts( "Unitary test of xsh_merge_ord" ) ;
00066 puts( "Usage: test_xsh_merge_ord [options] <input_files>" ) ;
00067 puts( "Options" );
00068 puts( " --method : WEIGHT or MEAN [MEAN]");
00069 puts( " --slitlet=n> : Num of the slitlet" ) ;
00070 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
00071 puts( "\nInput Files" ) ;
00072 puts( " REC_ORDER_nD_*_arm_DRL frame" ) ;
00073 TEST_END();
00074 }
00075
00076 enum {
00077 SLITLET_OPT, METHOD_OPT, DEBUG_OPT
00078 } ;
00079
00080
00081 static struct option long_options[] = {
00082 {"slitlet", required_argument, 0, SLITLET_OPT},
00083 {"method", required_argument, 0, METHOD_OPT},
00084 {"debug", required_argument, 0, DEBUG_OPT},
00085 {0,0,0,0}
00086 };
00087
00088 static void HandleOptions( int argc, char **argv, xsh_merge_param *merge_par,
00089 int *slitlet)
00090 {
00091 int opt ;
00092 int option_index = 0;
00093
00094 while (( opt = getopt_long (argc, argv, "slitlet:method",
00095 long_options, &option_index)) != EOF ){
00096 switch ( opt) {
00097 case SLITLET_OPT:
00098 sscanf( optarg, "%d", slitlet);
00099 break ;
00100 case METHOD_OPT:
00101 if ( strcmp(optarg, MERGE_METHOD_PRINT( MEAN_MERGE_METHOD)) == 0){
00102 merge_par->method = MEAN_MERGE_METHOD;
00103 }
00104 else if ( strcmp(optarg, MERGE_METHOD_PRINT( WEIGHTED_MERGE_METHOD)) == 0){
00105 merge_par->method = WEIGHTED_MERGE_METHOD;
00106 }
00107 else{
00108 xsh_msg("WRONG method %s", optarg);
00109 exit(-1);
00110 }
00111 break;
00112 case DEBUG_OPT:
00113 if ( strcmp( optarg, "LOW")==0){
00114 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW);
00115 }
00116 else if ( strcmp( optarg, "HIGH")==0){
00117 xsh_debug_level_set( XSH_DEBUG_LEVEL_HIGH);
00118 }
00119 break;
00120 default: xsh_msg("Not valid option %d",opt);Help() ; exit( 0 ) ;
00121 }
00122 }
00123 }
00124
00125
00126 static void analyse_merge_ord( cpl_frame* spectrum_frame, xsh_instrument* instr);
00127
00128
00129
00130
00131 static void analyse_merge_ord( cpl_frame* spectrum_frame, xsh_instrument* instr)
00132 {
00133 const char* spectrum_name = NULL;
00134 xsh_spectrum *spectrum = NULL;
00135 int i;
00136 FILE* fulldatfile = NULL;
00137 double* flux = NULL;
00138
00139 XSH_ASSURE_NOT_NULL( spectrum_frame);
00140 XSH_ASSURE_NOT_NULL( instr);
00141
00142 check (spectrum_name = cpl_frame_get_filename( spectrum_frame));
00143
00144 xsh_msg("Spectrum frame : %s", spectrum_name);
00145
00146 check( spectrum = xsh_spectrum_load( spectrum_frame, instr));
00147 check( flux = xsh_spectrum_get_flux( spectrum));
00148
00149 fulldatfile = fopen("merge_ord.dat","w");
00150 for(i=0; i< spectrum->size; i++){
00151 fprintf( fulldatfile, "%f %f\n", spectrum->lambda_min+i*spectrum->lambda_step, flux[i]);
00152 }
00153 xsh_msg("Save file merge_ord.dat");
00154 fclose( fulldatfile);
00155
00156 cleanup:
00157 xsh_spectrum_free( &spectrum);
00158 return;
00159 }
00160
00168 int main( int argc, char **argv)
00169 {
00170
00171 int ret = 0 ;
00172 xsh_instrument* instrument = NULL;
00173 XSH_ARM arm = XSH_ARM_UNDEFINED;
00174 xsh_merge_param merge_par = { MEAN_MERGE_METHOD} ;
00175
00176 cpl_frame* result = NULL;
00177 char* rec_name = NULL;
00178 cpl_frame* rec_frame = NULL;
00179 cpl_propertylist *plist = NULL;
00180 int slitlet = 0;
00181
00182
00183 TESTS_INIT(MODULE_ID);
00184 cpl_msg_set_level(CPL_MSG_DEBUG);
00185 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00186
00187 HandleOptions( argc, argv, &merge_par, &slitlet);
00188
00189 if ( (argc - optind) >=1 ) {
00190 rec_name = argv[optind];
00191 }
00192 else{
00193 Help();
00194 return 0;
00195 }
00196
00197
00198 TESTS_XSH_FRAME_CREATE( rec_frame, "REC_arm", rec_name);
00199
00200 check( plist = cpl_propertylist_load( rec_name, 0));
00201
00202
00203 check( arm = xsh_pfits_get_arm( plist));
00204
00205 instrument = xsh_instrument_new();
00206
00207 if ( arm == XSH_ARM_UVB){
00208 xsh_instrument_set_arm( instrument, XSH_ARM_UVB);
00209 cpl_frame_set_tag( rec_frame, "REC_UVB");
00210 }
00211 else if ( arm == XSH_ARM_VIS){
00212 xsh_instrument_set_arm( instrument, XSH_ARM_VIS);
00213 cpl_frame_set_tag( rec_frame, "REC_VIS");
00214 }
00215 else if ( arm == XSH_ARM_NIR){
00216 xsh_instrument_set_arm( instrument, XSH_ARM_NIR);
00217 cpl_frame_set_tag( rec_frame, "REC_NIR");
00218 }
00219 else{
00220 xsh_msg( "invalid arm of %s",
00221 xsh_instrument_arm_tostring( instrument));
00222 }
00223
00224 if (slitlet == 0){
00225 xsh_instrument_set_mode( instrument, XSH_MODE_SLIT);
00226 }
00227 else{
00228 xsh_instrument_set_mode( instrument, XSH_MODE_IFU);
00229 }
00230
00231 xsh_msg("---Input Frames");
00232 xsh_msg("Rec frame : %s", rec_name);
00233 xsh_msg("---Parameters");
00234 xsh_msg(" method %s", MERGE_METHOD_PRINT(merge_par.method));
00235
00236 check( result = xsh_merge_ord_slitlet( rec_frame, instrument, &merge_par, slitlet,"test"));
00237
00238 if (result != NULL){
00239 check( analyse_merge_ord( result, instrument));
00240 }
00241
00242 cleanup:
00243 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00244 xsh_error_dump(CPL_MSG_ERROR);
00245 ret = 1;
00246 }
00247 xsh_instrument_free( &instrument);
00248 xsh_free_propertylist( &plist);
00249 xsh_free_frame( &rec_frame);
00250 xsh_free_frame( &result);
00251
00252 TEST_END();
00253 return ret;
00254 }
00255