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 <xsh_data_instrument.h>
00044 #include <xsh_pfits.h>
00045 #include <xsh_msg.h>
00046 #include <xsh_utils.h>
00047 #include <tests.h>
00048 #include <cpl.h>
00049 #include <math.h>
00050
00051 #include <getopt.h>
00052
00053
00054
00055 #define MODULE_ID "XSH_SUBTRACT_BACKGROUND"
00056
00057
00058 enum {
00059 NB_Y_OPT, RADIUS_X_OPT, RADIUS_Y_OPT, HELP_OPT, MAX_FRAC_GRID_OPT,
00060 METHOD_OPT, SMOOTH_X_OPT, SMOOTH_Y_OPT, MIN_FRAC_OPT
00061 };
00062
00063 static const char * Options = "" ;
00064
00065 static struct option long_options[] = {
00066 {"nb-y", required_argument, 0, NB_Y_OPT},
00067 {"radius-x", required_argument, 0, RADIUS_X_OPT},
00068 {"radius-y", required_argument, 0, RADIUS_Y_OPT},
00069 {"grid-frac", required_argument, 0, MAX_FRAC_GRID_OPT},
00070 {"method", required_argument, 0, METHOD_OPT},
00071 {"smooth-x", required_argument, 0, SMOOTH_X_OPT},
00072 {"smooth-y",required_argument, 0, SMOOTH_X_OPT},
00073 {"min-frac",required_argument, 0,MIN_FRAC_OPT},
00074 {"help", 0, 0, HELP_OPT},
00075 {0, 0, 0, 0}
00076 };
00077
00078
00079
00080 static void Help( void )
00081 {
00082 puts( "Unitary test of xsh_subtract_background" ) ;
00083 puts( "Usage: test_xsh_subtract_background [options] <input_files>" ) ;
00084 puts( "Options" ) ;
00085 puts( " --nb-y=<n> : Number of points of the grid in y direction" ) ;
00086 puts( " --radius-x=<n> : Half size of the subwindow in x direction" ) ;
00087 puts( " --radius-y=<n> : Half size of the subwindow in y direction" ) ;
00088 puts( " --grid-frac=<nn> : Maximum fraction of points rejected from the grid" ) ;
00089 puts( " --method=<name> : Method adopted for background estimation median |"\
00090 "minimum | poly | no" );
00091 puts( " --smooth-x=<n> : Smoothing radius' half size in x direction" );
00092 puts( " --smooth-y=<n> : Smoothing radius' half size in y direction" );
00093 puts( " --help : What you see" ) ;
00094 puts( "\nInput Files" ) ;
00095 puts( "The input files argument MUST be in this order:" ) ;
00096 puts( " 1. Science frame in PRE format" ) ;
00097 puts( " 2. SOF [ORDER_TAB_EDGES]\n" ) ;
00098 TEST_END();
00099 }
00100
00101 static void HandleOptions( int argc, char **argv,
00102 xsh_background_param *backg_par)
00103 {
00104 int opt ;
00105 int option_index = 0;
00106
00107
00108 while (( opt = getopt_long (argc, argv, Options,
00109 long_options, &option_index)) != EOF ){
00110 switch ( opt ) {
00111 case NB_Y_OPT:
00112 backg_par->sampley = atoi( optarg);
00113 break;
00114 case RADIUS_X_OPT:
00115 backg_par->radius_x = atoi( optarg);
00116 break;
00117 case RADIUS_Y_OPT:
00118 backg_par->radius_y = atoi( optarg);
00119 break;
00120 case MAX_FRAC_GRID_OPT:
00121 backg_par->max_frac_grid = atof( optarg);
00122 break;
00123 case METHOD_OPT:
00124 backg_par->method = optarg;
00125 break;
00126 case SMOOTH_X_OPT:
00127 backg_par->smooth_x = atoi( optarg);
00128 break;
00129 case SMOOTH_Y_OPT:
00130 backg_par->smooth_y = atoi( optarg);
00131 break;
00132 case MIN_FRAC_OPT:
00133 backg_par->min_frac_grid = atof( optarg);
00134 break;
00135 default: Help() ; exit( 0 ) ;
00136 }
00137 }
00138 }
00139
00140
00141
00142
00143
00150
00151
00152 int main( int argc, char **argv)
00153 {
00154 int ret = 0;
00155
00156 xsh_instrument* instrument = NULL;
00157
00158 const char *sof_name = NULL;
00159 cpl_frameset *set = NULL;
00160 const char * sci_name = NULL ;
00161
00162 cpl_frame* flat_frame = NULL;
00163 cpl_frame* flat_rmbckg = NULL;
00164 cpl_frame* guess_order_tab_frame = NULL;
00165
00166 xsh_background_param backg;
00167 cpl_frame* frame_grid=NULL;
00168 cpl_frame* frame_back=NULL;
00169
00170
00171 TESTS_INIT(MODULE_ID);
00172 cpl_msg_set_level(CPL_MSG_DEBUG);
00173 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00174
00175 backg.sampley = 800;
00176 backg.radius_x = 2;
00177 backg.radius_y = 2;
00178 backg.max_frac_grid = 0.7;
00179 backg.method = "median";
00180 backg.smooth_x = -1;
00181 backg.smooth_y = 100;
00182 backg.min_frac_grid = 0.3;
00183 backg.debug = CPL_TRUE;
00184
00185 HandleOptions( argc, argv, &backg);
00186
00187 if ( (argc - optind) >=2 ) {
00188 sci_name = argv[optind];
00189 sof_name = argv[optind+1];
00190 }
00191 else {
00192 Help();
00193 exit(0);
00194 }
00195
00196
00197 check( set = sof_to_frameset( sof_name));
00198
00199 check( instrument = xsh_dfs_set_groups( set));
00200
00201 check( guess_order_tab_frame = xsh_find_order_tab_edges( set, instrument));
00202 TESTS_XSH_FRAME_CREATE( flat_frame, "FLAT", sci_name);
00203
00204 xsh_msg("FRAME : %s",
00205 cpl_frame_get_filename( flat_frame));
00206 xsh_msg("ORDERLIST : %s",
00207 cpl_frame_get_filename( guess_order_tab_frame));
00208
00209 xsh_msg(" Parameters ");
00210 xsh_msg(" sample Y %d", backg.sampley);
00211 xsh_msg(" radius X %d", backg.radius_x);
00212 xsh_msg(" radius Y %d", backg.radius_y);
00213 xsh_msg(" max frac grid %f", backg.max_frac_grid);
00214 xsh_msg(" method %s", backg.method);
00215 xsh_msg(" smooth X %d", backg.smooth_x);
00216 xsh_msg(" smooth Y %d", backg.smooth_y);
00217 xsh_msg(" min frac %f", backg.min_frac_grid);
00218
00219 check( flat_rmbckg = xsh_subtract_background( flat_frame,
00220 guess_order_tab_frame, &backg, instrument, "",
00221 &frame_grid, &frame_back));
00222
00223 cleanup:
00224 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00225 xsh_error_dump(CPL_MSG_ERROR);
00226 ret = 1;
00227 }
00228 xsh_free_frameset( &set);
00229 xsh_instrument_free( &instrument);
00230 xsh_free_frame( &flat_frame);
00231 xsh_free_frame( &flat_rmbckg);
00232 xsh_free_frame( &frame_grid);
00233 xsh_free_frame( &frame_back);
00234
00235 TEST_END();
00236 return ret;
00237 }
00238