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_badpixelmap.h>
00048 #include <xsh_data_pre.h>
00049 #include <xsh_data_order.h>
00050 #include <xsh_data_wavemap.h>
00051 #include <xsh_data_localization.h>
00052 #include <xsh_data_rec.h>
00053 #include <xsh_dfs.h>
00054 #include <xsh_pfits.h>
00055 #include <xsh_error.h>
00056 #include <xsh_msg.h>
00057 #include <xsh_fit.h>
00058 #include <xsh_badpixelmap.h>
00059
00060 #include <cpl.h>
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 static void xsh_compute_slit_index( float slit_min, float slit_step,
00072 xsh_rec_list **from, int *slit_index_tab, int size)
00073 {
00074 int i;
00075
00076 XSH_ASSURE_NOT_NULL( from);
00077 XSH_ASSURE_NOT_NULL( slit_index_tab);
00078
00079 for( i=0; i< size; i++){
00080 xsh_rec_list *list = NULL;
00081 float* nod_slit = NULL;
00082
00083 list = from[i];
00084 check( nod_slit = xsh_rec_list_get_slit( list, 0));
00085 slit_index_tab[i] = (int)xsh_round_double((nod_slit[0]-slit_min)/slit_step);
00086 }
00087 cleanup:
00088 return;
00089 }
00090
00091 static void xsh_rec_list_add( xsh_rec_list *dest, xsh_rec_list **from,
00092 int *slit_index, int nb_frames, int no, int method)
00093 {
00094 int nslit, from_slit, nlambda;
00095 float *dest_data1 = NULL, *dest_errs1 = NULL;
00096 int *dest_qual1 = NULL ;
00097 float *from_data1 = NULL, *from_errs1 = NULL;
00098 int *from_qual1 = NULL;
00099 int ns, nl, nf;
00100 double *flux_tab = NULL;
00101 cpl_vector *flux_vect = NULL;
00102 double *err_tab = NULL;
00103 cpl_vector *err_vect = NULL;
00104
00105 XSH_ASSURE_NOT_NULL( dest);
00106 XSH_ASSURE_NOT_NULL( from);
00107 XSH_ASSURE_NOT_NULL( slit_index);
00108
00109 xsh_msg_dbg_medium( "xsh_rec_list_add: nb frames: %d, order: %d",
00110 nb_frames, no);
00111
00112 check( nslit = xsh_rec_list_get_nslit( dest, no));
00113 check( from_slit = xsh_rec_list_get_nslit( from[0], no));
00114 check( nlambda = xsh_rec_list_get_nlambda( dest, no));
00115
00116 check( dest_data1 = xsh_rec_list_get_data1( dest, no));
00117 check( dest_errs1 = xsh_rec_list_get_errs1( dest, no));
00118 check( dest_qual1 = xsh_rec_list_get_qual1( dest, no));
00119
00120 XSH_MALLOC( flux_tab, double , nb_frames);
00121 XSH_MALLOC( err_tab, double , nb_frames);
00122
00123 for( nf = 0 ; nf < nb_frames ; nf++ ) {
00124 xsh_msg_dbg_high("slit index: max %d min=%d",
00125 slit_index[nf],slit_index[nf]+from_slit);
00126 }
00127
00128 for( ns = 0; ns < nslit; ns++){
00129 for( nl = 0; nl < nlambda; nl++){
00130 int dest_idx;
00131 int good = 0, bad = 0;
00132
00133
00134 unsigned int qflag = QFLAG_GOOD_PIXEL;
00135 unsigned int badflag = QFLAG_GOOD_PIXEL;
00136
00137 dest_idx = nl+ns*nlambda;
00138
00139 for( nf = 0 ; nf < nb_frames ; nf++ ) {
00140 int from_idx;
00141
00142 if ( (ns < slit_index[nf]) || (ns >= (slit_index[nf]+from_slit))){
00143 continue;
00144 }
00145 from_idx = nl+(ns-slit_index[nf])*nlambda;
00146 check( from_data1 = xsh_rec_list_get_data1( from[nf], no));
00147 check( from_errs1 = xsh_rec_list_get_errs1( from[nf], no));
00148 check( from_qual1 = xsh_rec_list_get_qual1( from[nf], no));
00149
00150 if ( from_qual1[from_idx] <= XSH_GOOD_PIXEL_LEVEL){
00151 qflag |= from_qual1[from_idx];
00152 flux_tab[good] = from_data1[from_idx];
00153 err_tab[good] = from_errs1[from_idx];
00154 good++;
00155 }
00156 else{
00157 flux_tab[good+bad] = from_data1[from_idx];
00158 err_tab[good+bad] = from_errs1[from_idx];
00159 badflag |= from_qual1[from_idx];
00160 bad++;
00161 }
00162 }
00163
00164 if ( good == 0 ) {
00165 check( flux_vect = cpl_vector_wrap( bad, flux_tab));
00166 check( err_vect = cpl_vector_wrap( bad, err_tab));
00167
00168 dest_qual1[dest_idx] = badflag;
00169 }
00170 else {
00171 dest_qual1[dest_idx] = qflag;
00172 check( flux_vect = cpl_vector_wrap( good, flux_tab));
00173 check( err_vect = cpl_vector_wrap( good, err_tab));
00174 }
00175 if ( method == COMBINE_MEAN_METHOD){
00176 check( dest_data1[dest_idx] = cpl_vector_get_mean( flux_vect));
00177 check( dest_errs1[dest_idx] = xsh_vector_get_err_mean( err_vect));
00178 }
00179 else{
00180 check( dest_data1[dest_idx] = cpl_vector_get_median( flux_vect));
00181 check( dest_errs1[dest_idx] = xsh_vector_get_err_median( err_vect));
00182 }
00183 xsh_unwrap_vector( &flux_vect);
00184 xsh_unwrap_vector( &err_vect);
00185 }
00186 }
00187
00188 cleanup:
00189 if ( cpl_error_get_code() != CPL_ERROR_NONE){
00190 xsh_unwrap_vector( &flux_vect);
00191 xsh_unwrap_vector( &err_vect);
00192 }
00193 XSH_FREE( flux_tab);
00194 XSH_FREE( err_tab);
00195 return ;
00196 }
00197
00198
00210
00211 cpl_frame* xsh_combine_nod( cpl_frameset *nod_frames,
00212 xsh_combine_nod_param * nod_par,
00213 const char * tag,
00214 xsh_instrument *instrument,
00215 cpl_frame** res_frame_ext)
00216 {
00217 xsh_rec_list *result_list = NULL ;
00218 cpl_frame *result = NULL ;
00219 int nb_frames = 0 ;
00220 int i, no, nb_orders;
00221 float slit_min=999, slit_max=-999;
00222 double slit_step =0.0;
00223 double lambda_min, lambda_max, lambda_step;
00224 xsh_rec_list **rec_input_list = NULL ;
00225 xsh_rec_list *rec_first = NULL;
00226 float *rec_first_slit = NULL;
00227 float *result_slit = NULL;
00228 int nslit;
00229 char *fname = NULL ;
00230 char *tag_drl = NULL ;
00231 char *fname_drl = NULL ;
00232 int *slit_index = NULL;
00233 int slit_ext_min=-9999;
00234 int slit_ext_max=9999;
00235 cpl_propertylist *header = NULL;
00236
00237 XSH_ASSURE_NOT_NULL( nod_frames);
00238 XSH_ASSURE_NOT_NULL( nod_par);
00239 XSH_ASSURE_NOT_NULL( instrument);
00240 XSH_ASSURE_NOT_NULL( tag);
00241 XSH_ASSURE_NOT_NULL( res_frame_ext);
00242
00243
00244 check( nb_frames = cpl_frameset_get_size( nod_frames));
00245 xsh_msg( "---xsh_combine_nod (method %s) - Nb frames = %d",
00246 COMBINE_METHOD_PRINT( nod_par->method), nb_frames);
00247
00248
00249 XSH_CALLOC( rec_input_list, xsh_rec_list *, nb_frames);
00250
00251
00252 for ( i = 0 ; i < nb_frames ; i++ ) {
00253 cpl_frame * nod_frame = NULL ;
00254 xsh_rec_list * list = NULL;
00255
00256 check( nod_frame = cpl_frameset_get_frame( nod_frames, i));
00257 check( list = xsh_rec_list_load( nod_frame, instrument));
00258 rec_input_list[i] = list;
00259 }
00260
00261
00262 for ( i = 0 ; i < nb_frames ; i++ ) {
00263 float nod_slit_min, nod_slit_max;
00264 xsh_rec_list * list = NULL;
00265 nslit=0;
00266 float* nod_slit = NULL;
00267
00268 list = rec_input_list[i];
00269 check( nslit = xsh_rec_list_get_nslit( list, 0));
00270 check( nod_slit = xsh_rec_list_get_slit( list, 0));
00271 nod_slit_min = nod_slit[0];
00272 nod_slit_max = nod_slit[nslit-1];
00273
00274 if ( nod_slit_min < slit_min){
00275 slit_min = nod_slit_min;
00276 }
00277 if (nod_slit_max > slit_max){
00278 slit_max = nod_slit_max;
00279 }
00280 }
00281 rec_first = rec_input_list[0];
00282 check( rec_first_slit = xsh_rec_list_get_slit( rec_first, 0));
00283 check( header = xsh_rec_list_get_header( rec_first));
00284 check( slit_step = xsh_pfits_get_rectify_bin_space( header));
00285 nslit = (slit_max-slit_min)/slit_step+1;
00286
00287
00288 XSH_CALLOC( result_slit, float, nslit);
00289 for ( i = 0 ; i < nslit; i++ ) {
00290 result_slit[i] = slit_min+i*slit_step;
00291 }
00292
00293 xsh_msg("Combine nod slit : (%f,%f) step %f nslit %d", slit_min, slit_max,
00294 slit_step, nslit);
00295
00296 nb_orders = rec_first->size;
00297 check( result_list = xsh_rec_list_create_with_size( nb_orders,
00298 instrument));
00299
00300 for ( no = 0 ; no < nb_orders ; no++ ) {
00301 int absorder, nlambda;
00302 double *dnew = NULL;
00303 double *dold = NULL;
00304 float *dslit = NULL;
00305
00306 absorder = xsh_rec_list_get_order( rec_first, no);
00307 nlambda = xsh_rec_list_get_nlambda( rec_first, no);
00308 check( xsh_rec_list_set_data_size( result_list, no, absorder, nlambda,
00309 nslit));
00310
00311 dold = xsh_rec_list_get_lambda( rec_first, no);
00312 dnew = xsh_rec_list_get_lambda( result_list, no);
00313 memcpy( dnew, dold, nlambda*sizeof( double));
00314
00315 dslit = xsh_rec_list_get_slit( result_list, no);
00316 memcpy( dslit, result_slit, nslit*sizeof( float));
00317 }
00318
00319
00320 XSH_CALLOC( slit_index, int, nb_frames);
00321 check( xsh_compute_slit_index( slit_min, slit_step, rec_input_list,
00322 slit_index, nb_frames));
00323
00324
00325 for ( no = 0; no < nb_orders; no++) {
00326 check( xsh_rec_list_add( result_list, rec_input_list, slit_index,
00327 nb_frames, no, nod_par->method) ) ;
00328 }
00329
00330
00331 fname = xsh_stringcat_any( tag, ".fits", NULL);
00332 tag_drl = xsh_stringcat_any( tag, "_DRL", NULL);
00333 fname_drl = xsh_stringcat_any( "DRL_", fname, NULL);
00334
00335
00336 check( cpl_propertylist_append( result_list->header,
00337 rec_first->header));
00338
00339
00340 check( lambda_step = xsh_pfits_get_rectify_bin_lambda(
00341 result_list->header));
00342 check( lambda_min = xsh_pfits_get_rectify_lambda_min(
00343 result_list->header));
00344 check( lambda_max = xsh_pfits_get_rectify_lambda_max(
00345 result_list->header));
00346 check( xsh_pfits_set_rectify_bin_lambda( result_list->header,
00347 lambda_step));
00348 check( xsh_pfits_set_rectify_bin_space( result_list->header,
00349 slit_step));
00350
00351
00352
00353
00354 check( xsh_pfits_set_rectify_lambda_min( result_list->header,
00355 lambda_min)) ;
00356 check( xsh_pfits_set_rectify_lambda_max( result_list->header,
00357 lambda_max));
00358
00359
00360
00361
00362
00363
00364 if(1) {
00365 int nf=0;
00366 int from_slit=0;
00367 for ( no = 0; no < nb_orders; no++) {
00368 check( from_slit = xsh_rec_list_get_nslit( rec_input_list[0], no));
00369
00370 for( nf = 0 ; nf < nb_frames ; nf++ ) {
00371 slit_ext_min=(slit_index[nf]>slit_ext_min) ? slit_index[nf]: slit_ext_min;
00372 slit_ext_max=(slit_index[nf]+from_slit<slit_ext_max) ? slit_index[nf]+from_slit: slit_ext_max;
00373
00374 }
00375 }
00376 }
00377
00378 xsh_msg("slit index: min %f max=%f",
00379 slit_min,slit_max);
00380
00381
00382
00383 result_list->slit_min = slit_min;
00384 result_list->slit_max = slit_max;
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 check( xsh_pfits_set_rectify_space_min( result_list->header,
00395 result_list->slit_min));
00396 check( xsh_pfits_set_rectify_space_max( result_list->header,
00397 result_list->slit_max));
00398 check( xsh_pfits_set_pcatg( result_list->header, tag));
00399
00400 xsh_pfits_set_extract_slit_min(result_list->header,slit_ext_min);
00401 xsh_pfits_set_extract_slit_max(result_list->header,slit_ext_max);
00402 check( *res_frame_ext=xsh_rec_list_save2( result_list, fname, tag));
00403 check( result = xsh_rec_list_save( result_list, fname_drl, tag_drl, 1));
00404
00405 cleanup:
00406 if (cpl_error_get_code() != CPL_ERROR_NONE){
00407 xsh_free_frame( &result);
00408 xsh_free_frame( res_frame_ext);
00409 }
00410 xsh_rec_list_free( &result_list);
00411 if ( rec_input_list != NULL){
00412 for ( i = 0 ; i < nb_frames ; i++ ) {
00413 xsh_rec_list_free( &rec_input_list[i]);
00414 }
00415 }
00416 XSH_FREE( result_slit);
00417 XSH_FREE( slit_index);
00418 XSH_FREE( tag_drl);
00419 XSH_FREE( fname_drl);
00420 XSH_FREE( fname);
00421 XSH_FREE( rec_input_list);
00422 return result ;
00423 }
00424
00425