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