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
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_dfs.h>
00049 #include <xsh_pfits.h>
00050 #include <tests.h>
00051 #include <cpl.h>
00052 #include <math.h>
00053 #include <getopt.h>
00054
00055
00056
00057
00058 #define MODULE_ID "XSH_SUBTRACT_BIAS"
00059
00060 enum {
00061 DEBUG_OPT, HELP_OPT
00062 };
00063
00064 static struct option long_options[] = {
00065 {"debug", required_argument, 0, DEBUG_OPT},
00066 {"help", 0, 0, HELP_OPT},
00067 {0, 0, 0, 0}
00068 };
00069
00070
00071 static void Help( void )
00072 {
00073 puts( "Unitary test of xsh_subtract_bias");
00074 puts( "Usage: test_xsh_subtract_bias [options] <input_files>");
00075
00076 puts( "Options" ) ;
00077 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
00078 puts( " --help : What you see" ) ;
00079 puts( "\nInput Files" ) ;
00080 puts( "The input files argument MUST be in this order:" ) ;
00081 puts( " 1. Science frame in PRE format" ) ;
00082 puts( " 2. SOF [MASTER_BIAS]");
00083 TEST_END();
00084 exit(0);
00085 }
00086
00087
00088
00089
00090 static void HandleOptions( int argc, char **argv)
00091 {
00092 int opt ;
00093 int option_index = 0;
00094 while (( opt = getopt_long (argc, argv, "debug:help",
00095 long_options, &option_index)) != EOF ){
00096
00097 switch ( opt ) {
00098 case DEBUG_OPT:
00099 if ( strcmp( optarg, "LOW")==0){
00100 xsh_debug_level_set( XSH_DEBUG_LEVEL_LOW);
00101 }
00102 else if ( strcmp( optarg, "HIGH")==0){
00103 xsh_debug_level_set( XSH_DEBUG_LEVEL_HIGH);
00104 }
00105 break;
00106 case HELP_OPT:
00107 Help();
00108 break;
00109 default:
00110 break;
00111 }
00112 }
00113 return;
00114 }
00115
00116
00117
00118
00119
00126
00127
00128 int main( int argc, char **argv)
00129 {
00130 xsh_instrument * instrument = NULL ;
00131 cpl_frameset* set = NULL;
00132 cpl_frame* frame = NULL;
00133 cpl_frame* bias = NULL;
00134 char* sci_name = NULL;
00135 char* sof_name = NULL;
00136 cpl_frame* temp = NULL;
00137 int i = 0;
00138 xsh_pre* pre1 = NULL;
00139 xsh_pre* pre2 = NULL;
00140 xsh_pre* pre3 = NULL;
00141 float *data1 = NULL,*errs1 = NULL;
00142 int * qual1 = NULL;
00143 float *data2 = NULL,*errs2 = NULL;
00144 int * qual2 = NULL;
00145 float *data3 = NULL,*errs3 = NULL;
00146 int * qual3 = NULL;
00147 int ret=0;
00148 int pre_overscan_corr=0;
00149
00150
00151 TESTS_INIT(MODULE_ID);
00152 cpl_msg_set_level( CPL_MSG_DEBUG);
00153 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM);
00154
00155 HandleOptions( argc, argv);
00156
00157 if ( (argc-optind) >= 2 ) {
00158 sci_name = argv[optind];
00159 sof_name = argv[optind+1];
00160
00161
00162 check( set = sof_to_frameset( sof_name));
00163
00164
00165 check( instrument = xsh_dfs_set_groups( set));
00166 check( bias = xsh_find_master_bias( set, instrument));
00167 TESTS_XSH_FRAME_CREATE( frame, "OBJECT_SLIT_STARE_arm", sci_name);
00168 }
00169 else{
00170 xsh_msg("-------------------------------------------");
00171 xsh_msg("Execute default test : do --help for option");
00172 xsh_msg("-------------------------------------------");
00173
00174
00175 instrument = xsh_instrument_new() ;
00176 xsh_instrument_set_mode( instrument, XSH_MODE_IFU ) ;
00177 xsh_instrument_set_arm( instrument, XSH_ARM_UVB ) ;
00178 xsh_instrument_set_lamp( instrument, XSH_LAMP_QTH ) ;
00179
00180
00181 set = cpl_frameset_new();
00182 for(i=0;i<2;i++){
00183 char framename[256];
00184
00185 sprintf(framename,"frame%d.fits",i);
00186 frame = xsh_test_create_frame(framename,10,10,
00187 XSH_LINEARITY_UVB,CPL_FRAME_GROUP_RAW, instrument);
00188 cpl_frameset_insert(set,frame);
00189 }
00190
00191
00192 check(xsh_prepare(set, NULL, NULL,"PRE",instrument,0));
00193
00194
00195 check( frame = cpl_frame_duplicate(cpl_frameset_get_frame(set,0)));
00196 check( bias = cpl_frameset_get_frame(set,1));
00197 }
00198
00199
00200 xsh_msg("SCI : %s",
00201 cpl_frame_get_filename( frame));
00202 xsh_msg("BIAS : %s",
00203 cpl_frame_get_filename( bias));
00204
00205 check( temp = xsh_subtract_bias(frame,bias,instrument,"TEST_BIAS_",pre_overscan_corr));
00206
00207
00208 check(pre1 = xsh_pre_load(temp,instrument));
00209 check(pre2 = xsh_pre_load(frame,instrument));
00210 check(pre3 = xsh_pre_load(bias,instrument));
00211
00212 data1 = cpl_image_get_data_float(pre1->data);
00213 data2 = cpl_image_get_data_float(pre2->data);
00214 data3 = cpl_image_get_data_float(pre3->data);
00215 errs1 = cpl_image_get_data_float(pre1->errs);
00216 errs2 = cpl_image_get_data_float(pre2->errs);
00217 errs3 = cpl_image_get_data_float(pre3->errs);
00218 qual1 = cpl_image_get_data_int(pre1->qual);
00219 qual2 = cpl_image_get_data_int(pre2->qual);
00220 qual3 = cpl_image_get_data_int(pre3->qual);
00221
00222 for(i=0;i<100;i++){
00223 assure(data1[i] -(data2[i] -data3[i]) < XSH_FLOAT_PRECISION,
00224 CPL_ERROR_ILLEGAL_OUTPUT,"Wrong data part");
00225 assure(errs1[i] - (sqrt(errs2[i]*errs2[i]+errs3[i]*errs3[i])) <
00226 XSH_FLOAT_PRECISION,CPL_ERROR_ILLEGAL_OUTPUT,"Wrong errs part");
00227 assure(qual1[i] == (qual2[i] + qual3[i]),CPL_ERROR_ILLEGAL_OUTPUT,
00228 "Wrong qual part");
00229 }
00230 xsh_msg("subtract bias ok");
00231
00232 cleanup:
00233 xsh_free_frame( &frame);
00234 xsh_pre_free(&pre1);
00235 xsh_pre_free(&pre2);
00236 xsh_pre_free(&pre3);
00237 xsh_instrument_free(&instrument);
00238 xsh_free_frameset(&set);
00239 xsh_free_frame(&temp);
00240
00241 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00242 xsh_error_dump(CPL_MSG_ERROR);
00243 ret=1;
00244 }
00245 TEST_END();
00246 return ret;
00247 }
00248