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
00037
00040
00041
00042
00043
00044 #include <math.h>
00045 #include <xsh_drl.h>
00046
00047 #include <xsh_utils_table.h>
00048 #include <xsh_badpixelmap.h>
00049 #include <xsh_data_pre.h>
00050 #include <xsh_dfs.h>
00051 #include <xsh_pfits.h>
00052 #include <xsh_error.h>
00053 #include <xsh_msg.h>
00054 #include <xsh_fit.h>
00055 #include <xsh_data_instrument.h>
00056 #include <xsh_data_localization.h>
00057 #include <xsh_data_spectrum.h>
00058 #include <xsh_dfs.h>
00059 #include <cpl.h>
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00082
00083 void xsh_mark_tell( cpl_frame *s1d_frame, cpl_frame *tellmask_frame)
00084 {
00085 xsh_spectrum *s1d = NULL;
00086 int *qual = NULL;
00087 int size;
00088 cpl_vector *tellmask_vect = NULL;
00089 int tellmask_size, i;
00090 const char *s1d_name = NULL;
00091 const char *tag = NULL;
00092 cpl_frame *result = NULL;
00093
00094 XSH_ASSURE_NOT_NULL( s1d_frame);
00095
00096 check( tag = cpl_frame_get_tag( s1d_frame));
00097 check( s1d_name = cpl_frame_get_filename( s1d_frame));
00098
00099 check( s1d = xsh_spectrum_load( s1d_frame));
00100 check( qual = xsh_spectrum_get_qual( s1d));
00101 check( size = xsh_spectrum_get_size( s1d));
00102
00103 if ( tellmask_frame != NULL){
00104 const char* tellmask_name = NULL;
00105
00106 check( tellmask_name = cpl_frame_get_filename( tellmask_frame));
00107 xsh_msg("Use telluric mask %s", tellmask_name);
00108
00109 check( tellmask_vect = cpl_vector_load( tellmask_name, 0));
00110 check( tellmask_size = cpl_vector_get_size( tellmask_vect));
00111
00112 XSH_ASSURE_NOT_ILLEGAL( tellmask_size == size);
00113
00114 for( i=0; i< size; i++){
00115 double mask_val;
00116
00117 mask_val = cpl_vector_get( tellmask_vect, i);
00118
00119 if ( mask_val > 0){
00120 qual[i] |= QFLAG_TELLURIC_UNCORRECTED;
00121 }
00122 }
00123 check( result = xsh_spectrum_save( s1d, s1d_name, tag));
00124 }
00125 else{
00126 xsh_msg("No telluric mask");
00127 }
00128
00129 cleanup:
00130 xsh_free_frame( &result);
00131 xsh_spectrum_free( &s1d);
00132 xsh_free_vector( &tellmask_vect);
00133 }
00134
00135
00136
00152
00153 cpl_frame* xsh_compute_absorp( cpl_frame *s1d_frame, cpl_frame *telllist_frame,
00154 int filter_hsize, double threshold, xsh_instrument* instr)
00155 {
00156 cpl_frame *result = NULL;
00157 xsh_spectrum *s1d = NULL;
00158 double *flux_data = NULL;
00159 cpl_vector *flux_vect = NULL;
00160 cpl_vector *smooth_vect = NULL;
00161 double *diff_data = NULL;
00162 cpl_vector *diff_vect = NULL;
00163 cpl_vector *std_vect = NULL;
00164 cpl_vector *std_box_vect = NULL;
00165 cpl_vector *tellmask = NULL;
00166 int i, size = 0;
00167 double wmin=0, wstep=0;
00168
00169 char mask_name[256];
00170 char tag[16];
00171
00172 int *tellflag = NULL;
00173 const char* tellist_name = NULL;
00174 cpl_table *tellist_table = NULL;
00175
00176 XSH_ASSURE_NOT_NULL( s1d_frame);
00177 XSH_ASSURE_NOT_NULL( instr);
00178
00179 xsh_msg( "Compute absorp");
00180
00181 XSH_REGDEBUG("File %s list %s filter %d threshold %f", cpl_frame_get_filename( s1d_frame),
00182 cpl_frame_get_filename( telllist_frame), filter_hsize, threshold);
00183
00184 check( s1d = xsh_spectrum_load( s1d_frame));
00185
00186 wmin = s1d->lambda_min;
00187 wstep = s1d->lambda_step;
00188
00189 size = xsh_spectrum_get_size_lambda( s1d);
00190 check( flux_data = xsh_spectrum_get_flux( s1d));
00191 check( flux_vect = cpl_vector_wrap( size, flux_data));
00192
00193 check( smooth_vect = cpl_vector_filter_median_create( flux_vect,
00194 filter_hsize));
00195 check( diff_vect = cpl_vector_new( size));
00196
00197 for( i=0; i< size; i++){
00198 double smooth_val, dval;
00199
00200 smooth_val = cpl_vector_get( smooth_vect, i);
00201 dval = (flux_data[i]-smooth_val)/smooth_val;
00202 cpl_vector_set( diff_vect, i, dval);
00203 }
00204
00205
00206 check( std_vect = cpl_vector_new( size));
00207 check( diff_data = cpl_vector_get_data( diff_vect));
00208
00209 for(i=0; i< (size-2*filter_hsize-1); i++){
00210 double dval;
00211
00212 std_box_vect = cpl_vector_wrap( filter_hsize*2+1, diff_data+i-filter_hsize);
00213 dval = cpl_vector_get_stdev( std_box_vect);
00214 cpl_vector_set( std_vect, i, dval);
00215 xsh_unwrap_vector( &std_box_vect);
00216 }
00217
00218 tellmask = cpl_vector_new( size);
00219
00220 XSH_CALLOC( tellflag, int , size);
00221
00222
00223 if ( telllist_frame != NULL){
00224 int it=0, table_size;
00225 double slit=0.0;
00226 double r1, r2;
00227 double factor_min, factor_max;
00228
00229
00230 check( tellist_name = cpl_frame_get_filename( telllist_frame));
00231 XSH_TABLE_LOAD( tellist_table, tellist_name);
00232
00233 table_size = cpl_table_get_nrow( tellist_table);
00234
00235 check( xsh_sort_table_1( tellist_table, LSTART_COLUMN_NAME, CPL_FALSE));
00236
00237 if ( xsh_instrument_get_arm( instr) == XSH_ARM_UVB){
00238 r1 = GUESS_TELL_MASK_RESOLUTION_UVB;
00239 }
00240 else if ( xsh_instrument_get_arm( instr) == XSH_ARM_VIS){
00241 r1 = GUESS_TELL_MASK_RESOLUTION_VIS;
00242 }
00243 else{
00244 r1 = GUESS_TELL_MASK_RESOLUTION_NIR;
00245 }
00246 check( slit = xsh_pfits_get_slit_width( s1d->flux_header, instr));
00247 check ( r2 = xsh_resolution_get( instr, slit));
00248 factor_min = (r2-1)/(r1-1)*r1/r2;
00249 factor_max = (r2+1)/(r1+1)*r1/r2;
00250 i=0;
00251 while( i< size){
00252 double wave, wtellmin, wtellmax;
00253 int good;
00254
00255 tellflag[i] = 1;
00256 wave = wmin+i*wstep;
00257 check( wtellmin = cpl_table_get_float( tellist_table, LSTART_COLUMN_NAME,
00258 it, &good));
00259 check( wtellmax = cpl_table_get_float( tellist_table, LEND_COLUMN_NAME,
00260 it, &good));
00261
00262 wtellmin *= factor_min;
00263 wtellmax *= factor_max;
00264
00265
00266 if ( wave >= wtellmin){
00267 if ( wave <= wtellmax){
00268 tellflag[i] = 0;
00269 i++;
00270 }
00271 else{
00272 if ( it < table_size-1){
00273 it++;
00274 }
00275 else{
00276 i++;
00277 }
00278 }
00279 }
00280 else{
00281 i++;
00282 }
00283 }
00284 }
00285
00286
00287
00288 for(i=0; i< size; i++){
00289
00290 double noise;
00291 int flag;
00292
00293
00294 noise = cpl_vector_get( std_vect, i);
00295 flag = tellflag[i];
00296
00297
00298 if ( flag == 0 && noise > threshold){
00299 cpl_vector_set( tellmask, i,1.0);
00300 }
00301 else{
00302 cpl_vector_set( tellmask, i,0.0);
00303 }
00304 }
00305
00306
00307 {
00308 FILE* debug_file = NULL;
00309
00310 debug_file = fopen( "out.dat", "w+");
00311
00312 fprintf( debug_file, "#lambda flux smooth diff flag is_tell\n");
00313
00314 for(i=0; i< size; i++){
00315 double mask_val;
00316
00317 mask_val = cpl_vector_get( tellmask, i);
00318 fprintf( debug_file, "%f %f %f %f %f %d\n", wmin+wstep*i, flux_data[i],
00319 cpl_vector_get( smooth_vect, i), cpl_vector_get( std_vect, i),
00320 mask_val*100, tellflag[i]*100);
00321 }
00322 fclose(debug_file);
00323 }
00324
00325
00326
00327
00328 sprintf( mask_name, "TELL_MASK.fits");
00329 sprintf(tag, "%s_%s", XSH_TELL_MASK, xsh_instrument_arm_tostring( instr));
00330
00331 check( cpl_vector_save( tellmask, mask_name, XSH_PRE_QUAL_BPP,
00332 s1d->flux_header, CPL_IO_CREATE));
00333 result = cpl_frame_new ();
00334
00335 check( cpl_frame_set_filename ( result, mask_name));
00336 check( cpl_frame_set_tag( result,tag));
00337 check( cpl_frame_set_type( result, CPL_FRAME_TYPE_IMAGE));
00338 check( cpl_frame_set_group ( result, CPL_FRAME_GROUP_PRODUCT)) ;
00339
00340 check( cpl_frame_set_level( result, CPL_FRAME_LEVEL_TEMPORARY));
00341 xsh_add_temporary_file( mask_name);
00342
00343
00344 cleanup:
00345 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00346 xsh_free_frame( &result);
00347 }
00348 XSH_FREE( tellflag);
00349 xsh_unwrap_vector( &flux_vect);
00350 xsh_spectrum_free( &s1d);
00351 xsh_free_vector( &smooth_vect);
00352 xsh_free_vector( &diff_vect);
00353 xsh_free_vector( &std_vect);
00354 xsh_free_vector( &tellmask);
00355 XSH_TABLE_FREE( tellist_table);
00356 return result;
00357 }
00358