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
00036 #include "sinfo_utl_cube_combine.h"
00037 #include "sinfo_utilities.h"
00038 #include "sinfo_utilities_scired.h"
00039 #include <stdio.h>
00040 #include "sinfo_key_names.h"
00041 #include "sinfo_error.h"
00042 #include "sinfo_utils_wrappers.h"
00043 #include "sinfo_pro_save.h"
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00066
00073
00074 int sinfo_utl_cube_combine(
00075 cpl_parameterlist * parlist,
00076 cpl_frameset * framelist)
00077 {
00078 cpl_parameter * param=NULL ;
00079 const char * name_o=NULL ;
00080 const char * name_i=NULL ;
00081 const char * name_m=NULL ;
00082
00083 cpl_propertylist * plist=NULL ;
00084 cpl_image * image=NULL ;
00085 cpl_frame * product_frame=NULL;
00086 cpl_frame * prod_frm=NULL;
00087 cpl_frame * frame=NULL;
00088
00089 int z=0;
00090 int z_min=0;
00091 int z_max=0;
00092 int z_siz=0;
00093 int z_stp=100;
00094 int xsize=0;
00095 int ysize=0;
00096 int scales_sky=0;
00097
00098 int i=0;
00099 int j=0;
00100 int n=0;
00101
00102 int size_x=0;
00103 int size_y=0;
00104 cpl_image* j_img=NULL;
00105 cpl_image* m_img=NULL;
00106
00107 cpl_imagelist ** cube_object=NULL;
00108 cpl_imagelist * cube_jitter=NULL;
00109 cpl_imagelist * cube_mask=NULL;
00110
00111 const char** files=NULL;
00112 int nframes=0;
00113
00114 double * times=NULL;
00115 float * offsetx=NULL;
00116 float * offsety=NULL;
00117 float ref_offx=0;
00118 float ref_offy=0;
00119 float tmpoffx=0;
00120 float tmpoffy=0;
00121 double kappa=2;
00122 int ks_clip=0;
00123 int min_size_x=9999;
00124 int min_size_y=9999;
00125 const char* name=NULL;
00126
00127
00128 FILE* file_list=NULL;
00129 int cnt=0;
00130 int onp=0;
00131
00132
00133
00134 check_nomsg(param = cpl_parameterlist_find(parlist,
00135 "sinfoni.sinfo_utl_cube_combine.ks_clip"));
00136 check_nomsg(ks_clip = cpl_parameter_get_bool(param));
00137
00138 check_nomsg(param = cpl_parameterlist_find(parlist,
00139 "sinfoni.sinfo_utl_cube_combine.scale_sky"));
00140 check_nomsg(scales_sky = cpl_parameter_get_bool(param));
00141
00142
00143 check_nomsg(param = cpl_parameterlist_find(parlist,
00144 "sinfoni.sinfo_utl_cube_combine.kappa"));
00145 check_nomsg(kappa = cpl_parameter_get_double(param));
00146
00147 check_nomsg(param = cpl_parameterlist_find(parlist,
00148 "sinfoni.sinfo_utl_cube_combine.name_i"));
00149 check_nomsg(name_i = cpl_parameter_get_string(param));
00150
00151 check_nomsg(param = cpl_parameterlist_find(parlist,
00152 "sinfoni.sinfo_utl_cube_combine.name_o"));
00153 check_nomsg(name_o = cpl_parameter_get_string(param));
00154
00155
00156
00157 check_nomsg(param = cpl_parameterlist_find(parlist,
00158 "sinfoni.sinfo_utl_cube_combine.xsize"));
00159 check_nomsg(xsize = cpl_parameter_get_int(param));
00160
00161 check_nomsg(param = cpl_parameterlist_find(parlist,
00162 "sinfoni.sinfo_utl_cube_combine.ysize"));
00163 check_nomsg(ysize = cpl_parameter_get_int(param));
00164
00165
00166
00167 check(sinfo_dfs_set_groups(framelist),
00168 "Cannot identify RAW and CALIB frames") ;
00169
00170
00171
00172 n=cpl_frameset_get_size(framelist);
00173 if(n<1) {
00174 sinfo_msg_error("Empty input frame list!");
00175 goto cleanup ;
00176 }
00177 for (i=0;i<n;i++) {
00178 frame=cpl_frameset_get_frame(framelist,i);
00179 cpl_frame_set_group(frame,CPL_FRAME_GROUP_RAW);
00180 }
00181
00182
00183
00184
00185
00186
00187 if ( NULL == (file_list = fopen (name_i, "r" ) ) )
00188 {
00189 sinfo_msg_error("cannot open %s\n", name_i) ;
00190 goto cleanup ;
00191 }
00192
00193 cnt = 0 ;
00194 while ( fscanf( file_list, "%f %f",&tmpoffx, &tmpoffy) != EOF )
00195 {
00196 cnt ++ ;
00197 }
00198 fclose(file_list);
00199
00200 nframes= cnt ;
00201 cknull(times = (double*) cpl_calloc (nframes, sizeof (double)),
00202 " could not allocate memory!") ;
00203
00204 cknull(offsetx = (float*) cpl_calloc (nframes, sizeof(float)),
00205 " could not allocate memory!") ;
00206
00207 cknull(offsety = (float*) cpl_calloc (nframes, sizeof(float)),
00208 " could not allocate memory!") ;
00209
00210 files = (const char**) cpl_calloc(MAX_NAME_SIZE,sizeof(const char*));
00211 if ( NULL == (file_list = fopen (name_i, "r" ) ) )
00212 {
00213 sinfo_msg_error("cannot open %s\n", name_i) ;
00214 return -1 ;
00215 }
00216
00217
00218 cnt=0;
00219 while ( fscanf( file_list, "%f %f",&tmpoffx,&tmpoffy ) != EOF ) {
00220 frame=cpl_frameset_get_frame(framelist,cnt);
00221 files[cnt]= cpl_frame_get_filename(frame);
00222 times[cnt]=sinfo_pfits_get_exptime(files[cnt]);
00223 offsetx[cnt]=tmpoffx;
00224 offsety[cnt]=tmpoffy;
00225 plist=cpl_propertylist_load(files[cnt],0);
00226 size_x=sinfo_pfits_get_naxis1(plist);
00227 size_y=sinfo_pfits_get_naxis2(plist);
00228 sinfo_free_propertylist(&plist);
00229 if(size_x < min_size_x) min_size_x=size_x;
00230 if(size_y < min_size_y) min_size_y=size_y;
00231 cnt ++ ;
00232 }
00233 sinfo_msg("Min input cube size x=%d y=%d",min_size_x,min_size_y);
00234
00235 nframes=cnt;
00236 fclose(file_list);
00237
00238 ck0(sinfo_auto_size_cube(offsetx,offsety,nframes,
00239 &ref_offx,&ref_offy,&size_x,&size_y),
00240 "Error resizing cube");
00241 sinfo_msg("output ima size=%dx%d",size_x,size_y);
00242 cknull(cube_object = cpl_calloc(nframes,sizeof(cpl_imagelist*)),
00243 "could not allocate memory") ;
00244
00245 for (i=0;i<nframes;i++) {
00246 frame=cpl_frameset_get_frame(framelist,i);
00247 name=cpl_frame_get_filename(frame);
00248 check_nomsg(cube_object[i]=cpl_imagelist_load(name,CPL_TYPE_FLOAT,0));
00249 ck0(sinfo_new_assign_offset2(i,name,offsetx,offsety,
00250 ref_offx,ref_offy),
00251 "Error assigning offsets");
00252 }
00253 onp = cpl_imagelist_get_size(cube_object[0]);
00254
00255
00256 check(cube_jitter = cpl_imagelist_new(),"allocating new data cube object");
00257 check(cube_mask = cpl_imagelist_new(),"allocating new data cube mask");
00258
00259 check(plist=cpl_propertylist_load(files[0],0),
00260 "Cannot read the FITS header") ;
00261
00262 if(scales_sky == 1) {
00263 sinfo_msg("Subtract spatial sinfo_median to each cube plane");
00264 for(n=0;n<nframes;n++) {
00265 sinfo_msg("process cube %d\n",n);
00266 sinfo_new_sinfoni_correct_median_it(&(cube_object[n]));
00267 }
00268 }
00269
00270 for(z=0;z<onp;z+=z_stp) {
00271 z_siz=(z_stp < (onp-z)) ? z_stp : (onp-z);
00272 z_min=z;
00273 z_max=z_min+z_siz;
00274 sinfo_msg("Coadding cubes: range [%4.4d,%4.4d) of 0-%d\n",
00275 z_min,z_max,onp);
00276
00277 for(j=z_min;j<z_max;j++) {
00278
00279 check_nomsg(j_img=cpl_image_new(size_x,size_y,CPL_TYPE_FLOAT));
00280 check_nomsg(cpl_imagelist_set(cube_jitter,j_img,j));
00281 check_nomsg(m_img = cpl_image_new(size_x,size_y,CPL_TYPE_FLOAT));
00282 check_nomsg(cpl_imagelist_set(cube_mask,m_img,j));
00283 }
00284 if(ks_clip == 1){
00285 sinfo_new_combine_jittered_cubes_thomas_range(cube_object,
00286 cube_jitter,
00287 cube_mask,
00288 nframes,
00289 offsetx,offsety,
00290 times,
00291 (char*) "tanh",
00292 z_min,
00293 z_max,
00294 kappa);
00295 } else {
00296 sinfo_new_combine_jittered_cubes_range(cube_object,
00297 cube_jitter,
00298 cube_mask,
00299 nframes,
00300 offsetx,
00301 offsety,
00302 times,
00303 (char*) "tanh",
00304 z_min,
00305 z_max) ;
00306 }
00307
00308 }
00309 sinfo_new_convert_0_to_ZERO_for_cubes(cube_jitter) ;
00310
00311 name_m="out_cube_mask.fits";
00312
00313
00314
00315 check_nomsg(product_frame = cpl_frame_new());
00316 check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00317 check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_CUBE_COMBINE_PROCUBE));
00318 check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00319 check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00320 check_nomsg(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL)) ;
00321
00322
00323 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
00324 check(cpl_dfs_setup_product_header(plist, product_frame,
00325 framelist, parlist,
00326 "sinfo_utl_cube_combine",
00327 "SINFONI", KEY_VALUE_HPRO_DID,NULL),
00328 "Problem in the product DFS-compliance") ;
00329 #else
00330 check(cpl_dfs_setup_product_header(plist, product_frame,
00331 framelist, parlist,
00332 "sinfo_utl_cube_combine",
00333 "SINFONI", KEY_VALUE_HPRO_DID),
00334 "Problem in the product DFS-compliance") ;
00335 #endif
00336 check(cpl_imagelist_save(cube_jitter, name_o, CPL_BPP_IEEE_FLOAT, plist,
00337 CPL_IO_DEFAULT),"Could not save product");
00338 check_nomsg(cpl_frameset_insert(framelist, product_frame));
00339
00340
00341 check_nomsg(prod_frm = cpl_frame_new());
00342 check_nomsg(cpl_frame_set_filename(prod_frm, name_m)) ;
00343 check_nomsg(cpl_frame_set_tag(prod_frm, SI_UTL_CUBE_COMBINE_PROMASK)) ;
00344 check_nomsg(cpl_frame_set_type(prod_frm, CPL_FRAME_TYPE_IMAGE)) ;
00345 check_nomsg(cpl_frame_set_group(prod_frm, CPL_FRAME_GROUP_PRODUCT)) ;
00346 check_nomsg(cpl_frame_set_level(prod_frm, CPL_FRAME_LEVEL_FINAL)) ;
00347
00348 check(cpl_imagelist_save(cube_mask, name_m, CPL_BPP_IEEE_FLOAT, plist,
00349 CPL_IO_DEFAULT),"Could not save product");
00350 check_nomsg(cpl_frameset_insert(framelist, prod_frm));
00351
00352 cleanup:
00353 sinfo_free_imagelist(&cube_jitter) ;
00354 sinfo_free_image(&image) ;
00355 sinfo_free_imagelist(&cube_mask) ;
00356
00357
00358 if(cube_object != NULL) {
00359 for (i=0;i< nframes;i++){
00360 sinfo_free_imagelist(&(cube_object[i]));
00361 }
00362 sinfo_free_array_imagelist(&cube_object);
00363 }
00364 sinfo_free_propertylist(&plist);
00365 if(files != NULL) {
00366 cpl_free(files);
00367 files=NULL;
00368 }
00369 sinfo_free_double(×);
00370 sinfo_free_float(&offsetx);
00371 sinfo_free_float(&offsety);
00372
00373
00374 if (cpl_error_get_code()) {
00375 return -1 ;
00376 }
00377 else {
00378 return 0 ;
00379 }
00380 }