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
00027
00028 #ifdef HAVE_CONFIG_H
00029 # include <config.h>
00030 #endif
00031
00032
00033
00034
00035 #include "sinfo_new_cubes_coadd.h"
00036 #include "sinfo_pfits.h"
00037 #include "sinfo_baryvel.h"
00038 #include "sinfo_pro_save.h"
00039 #include "sinfo_objnod_ini_by_cpl.h"
00040 #include "sinfo_functions.h"
00041 #include "sinfo_utilities_scired.h"
00042 #include "sinfo_wave_calibration.h"
00043 #include "sinfo_cube_construct.h"
00044 #include "sinfo_error.h"
00045 #include "sinfo_utils_wrappers.h"
00046
00047 #include "sinfo_dfs.h"
00048
00049
00050
00051 #define PI_NUMB (3.1415926535897932384626433832795)
00052
00053
00054
00055
00056
00057
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 int
00078 sinfo_new_cubes_coadd (const char* plugin_id,cpl_parameterlist* config,
00079 cpl_frameset* sof, const char* procatg)
00080 {
00081
00082 object_config * cfg=NULL ;
00083 cpl_image * im=NULL ;
00084 cpl_image* jitter_image=NULL;
00085 cpl_imagelist ** cube_tmp=NULL ;
00086
00087 cpl_imagelist ** cubeobject=NULL ;
00088 cpl_imagelist * jittercube=NULL ;
00089 cpl_imagelist * maskcube=NULL ;
00090 cpl_propertylist* plist=NULL;
00091 int sky_cor=0;
00092 int pdensity=0;
00093
00094 int i=0;
00095 int n=0 ;
00096 int partind = 0 ;
00097 int centralpix=0 ;
00098 int z_siz=0;
00099 int z_min=0;
00100 int z_max=0;
00101 int z=0;
00102 int z_stp=100;
00103 int scales_sky=0;
00104 int ks_clip=0;
00105 double kappa=2.0;
00106
00107 float ref_offx=0;
00108 float ref_offy=0;
00109 float newcenter_x=0 ;
00110 float newcenter_y=0 ;
00111 float cd1_1=0;
00112 float cd1_2=0;
00113 float cd2_1=0;
00114 float cd2_2=0;
00115 double pixelscale=0;
00116 double angle=0;
00117 float radangle=0 ;
00118 double exptime=0 ;
00119
00120 double * times=NULL ;
00121 float * offsetx=NULL;
00122 float * offsety=NULL;
00123 float offx_min=1.e10;
00124 float offy_min=1.e10;
00125 float offx_max=-1.e10;
00126 float offy_max=-1.e10;
00127
00128 double dis=0 ;
00129 double centralLambda=0 ;
00130 double mjd_obs=0;
00131
00132 char name_jitter[MAX_NAME_SIZE] ;
00133 char pro_mjit[MAX_NAME_SIZE];
00134 char pro_obs[MAX_NAME_SIZE];
00135 char pro_med[MAX_NAME_SIZE];
00136
00137
00138 char * name=NULL ;
00139 char * partname=NULL;
00140 char * partname2=NULL ;
00141 char file_name[MAX_NAME_SIZE];
00142 int vllx=0;
00143 int vlly=0;
00144 int vurx=0;
00145 int vury=0;
00146
00147 int onp=0;
00148 int j=0;
00149 cpl_image* j_img=NULL;
00150 cpl_image* m_img=NULL;
00151 cpl_table* qclog_tbl=NULL;
00152 cpl_image* ill_cor=NULL;
00153 cpl_frame* frame=NULL;
00154 cpl_frameset* stk=NULL;
00155 cpl_parameter* p=NULL;
00156 int mosaic_max_size=0;
00157 double barycor=0;
00158 double helicor=0;
00159 cpl_table* qclog=NULL;
00160
00161 check_nomsg(p=cpl_parameterlist_find(config,
00162 "sinfoni.objnod.mosaic_max_size"));
00163 check_nomsg(mosaic_max_size=cpl_parameter_get_int(p));
00164 check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
00165 check_nomsg(pdensity=cpl_parameter_get_int(p));
00166
00167 if (strcmp(procatg,PRO_COADD_STD) == 0) {
00168 strcpy(pro_mjit,PRO_MASK_COADD_STD);
00169 strcpy(pro_obs,PRO_OBS_STD);
00170 strcpy(pro_med,PRO_MED_COADD_STD);
00171
00172 } else if (strcmp(procatg,PRO_COADD_PSF) == 0) {
00173 strcpy(pro_mjit,PRO_MASK_COADD_PSF);
00174 strcpy(pro_obs,PRO_OBS_PSF);
00175 strcpy(pro_med,PRO_MED_COADD_PSF);
00176 } else {
00177 strcpy(pro_mjit,PRO_MASK_COADD_OBJ);
00178 strcpy(pro_obs,PRO_OBS_OBJ);
00179 strcpy(pro_med,PRO_MED_COADD_OBJ);
00180 }
00181
00182
00183 check_nomsg(stk = cpl_frameset_new());
00184
00185 cknull(cfg = sinfo_parse_cpl_input_objnod(config,sof,&stk),
00186 "Error setting parameter configuration");
00187
00188 ck0(sinfo_check_input_data(cfg),"error checking input");
00189
00190 if ( cfg->jitterind == 1 )
00191 {
00192 cknull(times = (double*) cpl_calloc (cfg->nframes, sizeof (double)),
00193 " could not allocate memory!") ;
00194
00195 cknull(offsetx = (float*) cpl_calloc (cfg->nframes, sizeof(float)),
00196 " could not allocate memory!") ;
00197
00198 cknull(offsety = (float*) cpl_calloc (cfg->nframes, sizeof(float)),
00199 " could not allocate memory!") ;
00200 }
00201
00202 if (cfg->jitterind == 0)
00203 {
00204 if ( NULL != (partname = strtok(cfg->outName, ".")))
00205 {
00206 partname2 = strtok (NULL, ".") ;
00207 partind = 1 ;
00208 }
00209 }
00210
00211 ck0(sinfo_auto_size_cube5(cfg,&ref_offx,&ref_offy,
00212 &offx_min,&offy_min,
00213 &offx_max,&offy_max),
00214 "Error resizing cube");
00215
00216 if(NULL != cpl_frameset_find(sof,PRO_ILL_COR)) {
00217 frame = cpl_frameset_find(sof,PRO_ILL_COR);
00218 ill_cor=cpl_image_load(cpl_frame_get_filename(frame),CPL_TYPE_FLOAT,0,0);
00219 } else {
00220 sinfo_msg("Illumination correction image not provided");
00221 cpl_error_reset();
00222 }
00223
00224 for ( n = 0 ; n < cfg->nframes ; n++ )
00225 {
00226
00227 sinfo_msg_debug("Read FITS information");
00228 name = cfg->framelist[n] ;
00229 if (n == 0)
00230 {
00231 strcpy (name_jitter, name) ;
00232 }
00233 if( sinfo_is_fits_file(name) != 1) {
00234 sinfo_msg_error("Input file %s is not FITS",name);
00235 goto cleanup;
00236 }
00237
00238
00239
00240 plist=cpl_propertylist_load(name,0);
00241
00242 if (sinfo_propertylist_has(plist, KEY_NAME_MJD_OBS)) {
00243 mjd_obs=cpl_propertylist_get_double(plist, KEY_NAME_MJD_OBS);
00244 } else {
00245 sinfo_msg_error("keyword %s does not exist",KEY_NAME_MJD_OBS);
00246 cpl_propertylist_delete(plist) ;
00247 return -1;
00248 }
00249
00250
00251 pixelscale = sinfo_pfits_get_pixscale(plist) /2;
00252 angle = sinfo_pfits_get_posangle(plist) ;
00253
00254 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00255 cpl_error_reset();
00256 }
00257
00258 sinfo_free_propertylist(&plist);
00259
00260 radangle = angle * PI_NUMB / 180. ;
00261 cd1_1 = cos(radangle) ;
00262 cd1_2 = sin(radangle) ;
00263 cd2_1 = -sin(radangle) ;
00264 cd2_2 = cos(radangle) ;
00265
00266 sinfo_msg_debug("frame no.: %d, name: %s\n", n, name) ;
00267 cknull(im = cpl_image_load(name,CPL_TYPE_FLOAT,0,0),
00268 " could not load frame %s!",name) ;
00269
00270 if (cfg->jitterind == 1)
00271 {
00272 exptime = sinfo_pfits_get_ditndit(name) ;
00273
00274 if (exptime == FLAG)
00275 {
00276 sinfo_msg_error("could not read fits header keyword exptime!");
00277 goto cleanup;
00278 }
00279 times[n] = exptime ;
00280
00281 ck0(sinfo_new_assign_offset(n,name,offsetx,offsety,
00282 ref_offx,ref_offy),
00283 "Error assigning offsets");
00284
00285 }
00286 sinfo_free_image(&im);
00287
00288 }
00289
00290
00291
00292 if(cfg->jitterind == 0) {
00293 goto exit;
00294 }
00295
00296
00297 if( (cfg->size_x*cfg->size_y*cfg->nframes) > (100*mosaic_max_size) ) {
00298 sinfo_msg_warning("Coadd cube size:%d,%d. N frames: %d",
00299 cfg->size_x,cfg->size_y,cfg->nframes);
00300 sinfo_msg_warning("Max allowed should be such "
00301 "that sixeX*sixeY*Nframes < 100*%d",mosaic_max_size);
00302 goto exit;
00303 }
00304
00305 if ( cfg->jitterind == 1 )
00306 {
00307 check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.objnod.vllx"));
00308 check_nomsg(vllx = cpl_parameter_get_int(p));
00309 check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.objnod.vlly"));
00310 check_nomsg(vlly = cpl_parameter_get_int(p));
00311 check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.objnod.vurx"));
00312 check_nomsg(vurx = cpl_parameter_get_int(p));
00313 check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.objnod.vury"));
00314 check_nomsg(vury = cpl_parameter_get_int(p));
00315 cknull(cube_tmp = (cpl_imagelist**) cpl_calloc (cfg->nframes,
00316 sizeof (cpl_imagelist*)),
00317 "Could not allocate memory for cube_tmp");
00318
00319
00320 cknull(cubeobject = (cpl_imagelist**) cpl_calloc (cfg->nframes,
00321 sizeof (cpl_imagelist*)),
00322 "Could not allocate memory for cubeobject");
00323 check_nomsg(p=cpl_parameterlist_find(config,
00324 "sinfoni.objnod.sky_cor"));
00325 check_nomsg(sky_cor=cpl_parameter_get_bool(p));
00326
00327
00328 for ( n = 0 ; n < cfg->nframes ; n++ ) {
00329 if(sky_cor == 1 && (pdensity == 1 || pdensity == 3) &&
00330 strcmp(cfg->sky_dist,"no_sky")!=0) {
00331 sinfo_msg("load sky corrected cubes");
00332
00333 snprintf(file_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_cube_obj_cor",
00334 n,".fits");
00335
00336 } else {
00337 snprintf(file_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_cube_obj",n,
00338 ".fits");
00339 }
00340 check_nomsg(cube_tmp[n] = cpl_imagelist_load(file_name,
00341 CPL_TYPE_FLOAT,0));
00342
00343
00344 check_nomsg(cubeobject[n] = sinfo_new_cube_getvig(
00345 cube_tmp[n],
00346 1+vllx,1+vlly,
00347 64 - vurx, 64 - vury));
00348 check_nomsg(sinfo_free_imagelist(&cube_tmp[n]));
00349
00350 }
00351 sinfo_free_array_imagelist(&cube_tmp);
00352
00353
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363 if (cfg->jitterind == 1)
00364 {
00365 sinfo_msg("Jittering...");
00366
00367 sinfo_msg("Coadded cube size. x: %d y: %d",
00368 cfg->size_x,cfg->size_y);
00369 check_nomsg(jittercube = cpl_imagelist_new()) ;
00370
00371
00372
00373
00374
00375
00376
00377
00378 check_nomsg(p=cpl_parameterlist_find(config,
00379 "sinfoni.objnod.scales_sky"));
00380 check_nomsg(scales_sky=cpl_parameter_get_bool(p));
00381 check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.objnod.ks_clip"));
00382 check_nomsg(ks_clip = cpl_parameter_get_bool(p));
00383 check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.objnod.kappa"));
00384 check_nomsg(kappa = cpl_parameter_get_double(p));
00385
00386
00387 if(scales_sky == 1) {
00388 sinfo_msg("Subtract spatial sinfo_median to each cube plane");
00389 for(n=0;n<cfg->nframes;n++) {
00390 sinfo_msg("Process cube %d\n",n);
00391 sinfo_new_sinfoni_correct_median_it(&(cubeobject[n]));
00392 }
00393 }
00394
00395
00396
00397
00398 cknull(maskcube=cpl_imagelist_new(),"could not allocate cube!");
00399
00400
00401 if(ill_cor != NULL) {
00402 for(n=0;n<cfg->nframes;n++) {
00403 cpl_imagelist_divide_image(cubeobject[n],ill_cor);
00404 }
00405 }
00406 sinfo_free_image(&ill_cor);
00407
00408 sinfo_msg("Combine jittered cubes");
00409
00410
00411 if(ks_clip == 1) {
00412 sinfo_msg("Cube coaddition with kappa-sigma");
00413 }
00414 check_nomsg(onp=cpl_imagelist_get_size(cubeobject[0]));
00415 for(z=0;z<onp;z+=z_stp) {
00416 z_siz=(z_stp < (onp-z)) ? z_stp : (onp-z);
00417 z_min=z;
00418 z_max=z_min+z_siz;
00419 sinfo_msg("Coadding cubes: range [%4.4d,%4.4d) of 0-%d\n",
00420 z_min,z_max,onp);
00421
00422 for(j=z_min;j<z_max;j++) {
00423 check_nomsg(j_img=cpl_image_new(cfg->size_x,
00424 cfg->size_y,CPL_TYPE_FLOAT));
00425 check_nomsg(cpl_imagelist_set(jittercube,j_img,j));
00426 check_nomsg(m_img = cpl_image_new(cfg->size_x,
00427 cfg->size_y,CPL_TYPE_FLOAT));
00428 check_nomsg(cpl_imagelist_set(maskcube,m_img,j));
00429 }
00430 if(ks_clip == 1) {
00431 sinfo_new_combine_jittered_cubes_thomas_range(cubeobject,
00432 jittercube,
00433 maskcube,
00434 cfg->nframes,
00435 offsetx,offsety,
00436 times,
00437 cfg->kernel_type,
00438 z_min,
00439 z_max,
00440 kappa);
00441
00442 } else {
00443 sinfo_new_combine_jittered_cubes_range(cubeobject,
00444 jittercube,
00445 maskcube,
00446 cfg->nframes,
00447 offsetx,
00448 offsety,
00449 times,
00450 cfg->kernel_type,
00451 z_min,
00452 z_max) ;
00453 }
00454 }
00455 sinfo_new_convert_0_to_ZERO_for_cubes(jittercube) ;
00456
00457 if (jittercube == NULL)
00458 {
00459 sinfo_msg_error(" could not allocate new data cube!") ;
00460 goto cleanup;
00461 }
00462
00463 if (maskcube == NULL)
00464 {
00465 sinfo_msg_error(" could not merge the jittered data cubes\n") ;
00466 goto cleanup;
00467 }
00468
00469 for ( i = 0 ; i <cfg->nframes ; i++ ) {
00470 sinfo_free_imagelist(&cubeobject[i]);
00471
00472 }
00473 sinfo_free_array_imagelist(&cubeobject);
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496 if (mjd_obs > 53825. ) {
00497
00498 newcenter_x=cfg->size_x / 2. +2*ref_offx;
00499 newcenter_y=cfg->size_y / 2. +2*ref_offy;
00500 } else if ((mjd_obs > 53421.58210082 ) && (mjd_obs <= 53825.)){
00501
00502 newcenter_x=cfg->size_x / 2. -2*ref_offx;
00503 newcenter_y=cfg->size_y / 2. +2*ref_offy;
00504 } else {
00505
00506 newcenter_x=cfg->size_x / 2. +2*ref_offx;
00507 newcenter_y=cfg->size_y / 2. -2*ref_offy;
00508 }
00509
00510
00511
00512
00513
00514
00515 cknull_nomsg(qclog = sinfo_qclog_init());
00516 plist=cpl_propertylist_load(file_name,0);
00517 if(sinfo_baryvel(plist, &barycor,&helicor) != CPL_ERROR_NONE) {
00518 sinfo_msg_warning("Could not compute velocity corrections");
00519 cpl_error_reset();
00520 } else {
00521
00522 check_nomsg(sinfo_qclog_add_double(qclog,
00523 "QC VRAD BARYCOR",
00524 barycor,
00525 "Barycentric radial velocity correction ",
00526 "%13.6f"));
00527
00528 check_nomsg(sinfo_qclog_add_double(qclog,
00529 "QC VRAD HELICOR",
00530 helicor,
00531 "Heliocentric radial velocity correction ",
00532 "%13.6f"));
00533 sinfo_msg("Barycor=%g Helicor=%g",barycor,helicor);
00534
00535 sinfo_free_propertylist(&plist);
00536
00537 }
00538
00539 ck0(sinfo_pro_save_ims(jittercube,sof,sof,cfg->outName,
00540 procatg,qclog,plugin_id,config),
00541 "cannot save cube %s", cfg->outName);
00542
00543 sinfo_free_table(&qclog);
00544
00545
00546 plist=cpl_propertylist_load(file_name,0);
00547 dis=sinfo_pfits_get_cdelt3(plist);
00548 centralLambda=sinfo_pfits_get_crval3(plist);
00549 centralpix=sinfo_pfits_get_crpix3(plist);
00550 sinfo_free_propertylist(&plist);
00551 sinfo_new_set_wcs_cube(jittercube, cfg->outName,
00552 centralLambda, dis,
00553 centralpix, newcenter_x, newcenter_y);
00554
00555 cknull(jitter_image = sinfo_new_median_cube(jittercube),
00556 " could not do sinfo_medianCube()");
00557 cknull_nomsg(qclog = sinfo_qclog_init());
00558 update_bad_pixel_map(jitter_image);
00559 check_nomsg(sinfo_qclog_add_double(qclog,
00560 "QC FRMDIF MEANSTD",
00561 cpl_image_get_mean(jitter_image),
00562 "mean of the collapesd cube",
00563 "%13.6f"));
00564 check_nomsg(sinfo_qclog_add_double(qclog,
00565 "QC FRMDIF STDEV",
00566 cpl_image_get_stdev(jitter_image),
00567 "standard deviation of the collapesd cube",
00568 "%13.6f"));
00569 ck0(sinfo_pro_save_ima(jitter_image,sof,sof,cfg->med_cube_name,
00570 pro_med,qclog,plugin_id,config),
00571 "cannot save ima %s", cfg->outName);
00572 sinfo_free_table(&qclog);
00573
00574 sinfo_new_set_wcs_image(jitter_image, cfg->med_cube_name,
00575 newcenter_x,newcenter_y);
00576
00577 sinfo_free_image(&jitter_image);
00578
00579 ck0(sinfo_pro_save_ims(maskcube,sof,sof,cfg->maskname,
00580 pro_mjit,NULL,plugin_id,config),
00581 "cannot save cube %s", cfg->maskname);
00582
00583 sinfo_new_set_wcs_cube(maskcube, cfg->maskname,
00584 centralLambda, dis, centralpix,
00585 newcenter_x, newcenter_y);
00586
00587 sinfo_free_double(×) ;
00588 sinfo_free_float(&offsetx) ;
00589 sinfo_free_float(&offsety) ;
00590 sinfo_free_imagelist(&maskcube) ;
00591 sinfo_free_imagelist(&jittercube) ;
00592
00593 }
00594
00595 exit:
00596
00597
00598 sinfo_objnod_free(&cfg);
00599 sinfo_free_frameset(&stk);
00600 return 0;
00601
00602 cleanup:
00603
00604 sinfo_free_image(&jitter_image);
00605 sinfo_free_imagelist(&jittercube) ;
00606 sinfo_free_imagelist(&maskcube) ;
00607 sinfo_free_table(&qclog);
00608
00609 if(cfg != NULL) {
00610 if(cube_tmp != NULL) {
00611 for ( n = 0 ; n < cfg->nframes ; n++ ) {
00612 sinfo_free_imagelist(&(cube_tmp[n]));
00613 }
00614 sinfo_free_array_imagelist(&cube_tmp);
00615 }
00616 if(cubeobject != NULL) {
00617 for ( n = 0 ; n < cfg->nframes ; n++ ) {
00618 sinfo_free_imagelist(&(cubeobject[n]));
00619 }
00620 sinfo_free_array_imagelist(&cubeobject);
00621 }
00622
00623 }
00624 sinfo_free_table(&qclog_tbl);
00625 sinfo_free_image(&im);
00626 sinfo_free_image(&ill_cor);
00627 sinfo_free_float(&offsety);
00628 sinfo_free_float(&offsetx);
00629 sinfo_free_double(×);
00630 sinfo_objnod_free(&cfg);
00631 sinfo_free_frameset(&stk);
00632
00633 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00634 sinfo_check_rec_status(0);
00635 }
00636 return -1;
00637
00638
00639
00640 }
00641