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 <string.h>
00036 #include <math.h>
00037 #include <stdio.h>
00038
00039
00040 #include "omega_coadd.h"
00041 #include "omega_fits.h"
00042 #include "omega_dfs.h"
00043 #include "omega_utils.h"
00044 #include "omega_pfits.h"
00045
00075 cpl_image **omega_jitter_stack(cpl_frameset *jset, cpl_frameset *jset_mask, cpl_bivector *offsets, int refine,
00076 cpl_parameterlist *pars, int ext)
00077 {
00078
00079 int n = 0;
00080 int n_mask=0;
00081 int i=0;
00082 cpl_mask * mask=NULL;
00083 double psigmas[] = {10.0, 5.0, 2.0};
00084 const int nsigmas = (int)(sizeof(psigmas)/sizeof(double));
00085 int shx, shy, mhx, mhy, minrej, maxrej;
00086 const char *sval;
00087 cpl_geom_combine comb_meth;
00088 cpl_imagelist *ilist;
00089 cpl_imagelist *ilist_mask;
00090 cpl_vector *sigmas;
00091 cpl_parameter *par;
00092 cpl_image **combined = NULL;
00093 cpl_vector * offset_x = NULL;
00094 cpl_vector * offset_y = NULL;
00095
00096 if((jset == NULL) || (pars == NULL))
00097 return NULL;
00098
00099
00100 par = cpl_parameterlist_find(pars, "omega.omega_science.xcorr") ;
00101 sval = cpl_parameter_get_string(par);
00102
00103 if (sscanf(sval, "%d,%d,%d,%d",&shx,­,&mhx,&mhy)!=4)
00104 return NULL;
00105
00106 par = cpl_parameterlist_find(pars, "omega.omega_science.CombMode");
00107 sval = cpl_parameter_get_string(par);
00108 if (!strcmp(sval, "union"))
00109 comb_meth = CPL_GEOM_UNION;
00110 else if (!strcmp(sval, "intersect"))
00111 comb_meth = CPL_GEOM_INTERSECT;
00112 else if (!strcmp(sval, "first"))
00113 comb_meth = CPL_GEOM_FIRST;
00114 else
00115 return NULL;
00116
00117
00118
00119 offset_x=cpl_bivector_get_x(offsets);
00120 offset_y=cpl_bivector_get_y(offsets);
00121
00122 if (cpl_vector_get_max(offset_x)-cpl_vector_get_min(offset_x) > 3000 ||
00123 cpl_vector_get_max(offset_y)-cpl_vector_get_min(offset_y) > 6000)
00124 {
00125 cpl_msg_warning(cpl_func, "Dithering offsets are very large. "
00126 "Combination method changed to \"first\" ");
00127 comb_meth = CPL_GEOM_FIRST;
00128 }
00129
00130 par = cpl_parameterlist_find(pars, "omega.omega_science.StackRej");
00131 sval = cpl_parameter_get_string(par);
00132 if(sscanf(sval, "%d,%d",&minrej,&maxrej) !=2)
00133 return NULL;
00134
00135 n = cpl_frameset_get_size(jset);
00136 n_mask = cpl_frameset_get_size(jset_mask);
00137
00138 if(n != n_mask){
00139 cpl_vector_unwrap(sigmas);
00140 cpl_msg_warning(cpl_func,"Number of images and masks differ!");
00141 return NULL;
00142 }
00143
00144
00145
00146 sigmas = cpl_vector_wrap(nsigmas, psigmas);
00147
00148
00149 ilist = cpl_imagelist_load_frameset(jset, CPL_TYPE_FLOAT, 1, ext);
00150 if(ilist == NULL){
00151 cpl_vector_unwrap(sigmas);
00152 return NULL;
00153 }
00154
00155 ilist_mask = cpl_imagelist_load_frameset(jset_mask, CPL_TYPE_INT, 1, ext);
00156 if(ilist_mask == NULL){
00157 cpl_vector_unwrap(sigmas);
00158 return NULL;
00159 }
00160
00161
00162
00163 for (i=0; i<n; i++){
00164 mask = cpl_mask_threshold_image_create(cpl_imagelist_get(ilist_mask,i), 0.5, 1.5);
00165 cpl_image_reject_from_mask(cpl_imagelist_get(ilist,i), mask);
00166 freemask(mask);
00167 }
00168
00169 freeilist(ilist_mask);
00170
00171
00172
00173 combined = cpl_geom_img_offset_combine(ilist,offsets,refine,NULL,sigmas,NULL,
00174 shx,shy,mhx,mhy,minrej,maxrej,comb_meth);
00175
00176 if(combined == NULL){
00177 cpl_msg_debug(cpl_func,"Error in combination function. %s",cpl_error_get_message());
00178 cpl_vector_unwrap(sigmas);
00179 freeilist(ilist);
00180 return NULL;
00181 }
00182
00183
00184 cpl_vector_unwrap(sigmas);
00185 freeilist(ilist);
00186
00187 return combined;
00188 }
00189
00201 cpl_bivector *omega_tel_offsets(cpl_frameset *set, int mode)
00202 {
00203
00204 int i = 0;
00205 double *offsets_x, *offsets_y;
00206 double scale = 0.0;
00207 cpl_frame *frame;
00208 cpl_propertylist *plist;
00209 cpl_bivector *offsets;
00210
00211 if((set == NULL) || (mode <= 0) || (mode > 4))
00212 return NULL;
00213
00214
00215 frame = cpl_frameset_get_first(set);
00216 plist = cpl_propertylist_load(cpl_frame_get_filename(frame), 1);
00217
00218 if((scale = omega_pfits_get_cd22(plist)) != 0)
00219 scale = scale*3600.;
00220 else if((scale = omega_pfits_get_cdelt2(plist)) != 0)
00221 scale = scale*3600;
00222
00223 freeplist(plist);
00224
00225
00226 offsets = cpl_bivector_new(cpl_frameset_get_size(set));
00227 offsets_x = cpl_bivector_get_x_data(offsets);
00228 offsets_y = cpl_bivector_get_y_data(offsets);
00229
00230
00231
00232
00233
00234 offsets_x[0] = 0;
00235 offsets_y[0] = 0;
00236 frame = cpl_frameset_get_next(set);
00237
00238
00239
00240 for(i=1; i<cpl_frameset_get_size(set); i++){
00241 plist = cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
00242 if(mode == 1){
00243
00244 offsets_x[i] = offsets_y[i] = 0.0;
00245 }
00246 else if(mode == 2){
00247 offsets_x[i] = omega_pfits_get_jitter_offset(plist)/scale;
00248
00249 offsets_y[i] = offsets_x[i];
00250 }
00251 else if(mode == 3){
00252
00253 offsets_x[i] = omega_pfits_get_dither_offsetx(plist)/scale;
00254 offsets_y[i] = omega_pfits_get_dither_offsety(plist)/scale;
00255 }
00256 else if(mode == 4){
00257
00258 offsets_x[i] = omega_pfits_get_offset_alpha(plist)/scale;
00259 offsets_y[i] = omega_pfits_get_offset_delta(plist)/scale;
00260 }
00261 frame = cpl_frameset_get_next(set);
00262 freeplist(plist);
00263 cpl_msg_debug(cpl_func,"im=%d offset_x=%g offset_y=%g",i,offsets_x[i],offsets_y[i]);
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 return offsets;
00275 }
00276
00277
00288
00289 cpl_bivector *omega_get_offsets_from_wcs (
00290 const cpl_frameset * frameset,
00291 const cpl_frame * ref_frame,
00292 int ext)
00293 {
00294 cpl_bivector * offsets;
00295 double * offsets_x;
00296 double * offsets_y;
00297 const cpl_frame * this_frame;
00298 int nfiles;
00299 int i;
00300 cpl_propertylist *plist;
00301 cpl_wcs *ref_wcs;
00302
00303
00304 if (frameset == NULL){
00305 cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00306 return NULL;
00307 }
00308
00309
00310 nfiles = cpl_frameset_get_size(frameset);
00311 offsets = cpl_bivector_new(nfiles);
00312 offsets_x = cpl_bivector_get_x_data(offsets);
00313 offsets_y = cpl_bivector_get_y_data(offsets);
00314
00315
00316
00317
00318 plist = cpl_propertylist_load_regexp(
00319 cpl_frame_get_filename(ref_frame),
00320 ext, "PV", 1);
00321 ref_wcs = cpl_wcs_new_from_propertylist(plist);
00322
00323 if(cpl_error_get_code() == CPL_ERROR_NO_WCS)
00324 {
00325 cpl_msg_error(__func__,"Not compiled with WCS support.");
00326 cpl_propertylist_delete(plist);
00327 return NULL;
00328 }
00329
00330
00331 cpl_propertylist_delete(plist);
00332
00333
00334 if (ref_wcs == NULL){
00335 cpl_msg_error(cpl_func, "Could not read WCS from %s",
00336 cpl_frame_get_filename(ref_frame));
00337 cpl_bivector_delete(offsets);
00338 return NULL;
00339 }
00340
00341 for (i = 0; i < nfiles; i++){
00342 cpl_matrix * exp_pixel;
00343 cpl_matrix * exp_equat;
00344 cpl_matrix * ref_pixel;
00345 cpl_array * status =NULL;
00346 cpl_wcs * exp_wcs;
00347 cpl_error_code error = CPL_ERROR_NONE;
00348
00349
00350 exp_pixel = cpl_matrix_new(1, 2);
00351 cpl_matrix_set(exp_pixel, 0, 0, 1.);
00352 cpl_matrix_set(exp_pixel, 0, 1, 1.);
00353
00354
00355 this_frame = cpl_frameset_get_frame_const(frameset, i);
00356 plist = cpl_propertylist_load_regexp(
00357 cpl_frame_get_filename(this_frame),
00358 ext, "PV", 1);
00359 exp_wcs = cpl_wcs_new_from_propertylist(plist);
00360 cpl_propertylist_delete(plist);
00361
00362
00363 if (exp_wcs == NULL){
00364 cpl_msg_error(cpl_func, "Could not read WCS from %s",
00365 cpl_frame_get_filename(this_frame));
00366 cpl_matrix_delete(exp_pixel);
00367 cpl_bivector_delete(offsets);
00368 return NULL;
00369 }
00370
00371
00372
00373 status = NULL;
00374 if ((error = cpl_wcs_convert(exp_wcs, exp_pixel, &exp_equat, &status,
00375 CPL_WCS_PHYS2WORLD))
00376 != CPL_ERROR_NONE){
00377 cpl_msg_error(cpl_func, cpl_error_get_message_default(error));
00378 cpl_matrix_delete(exp_pixel);
00379 cpl_wcs_delete(exp_wcs);
00380 cpl_bivector_delete(offsets);
00381 cpl_array_delete(status);
00382 return NULL;
00383 }
00384 cpl_matrix_delete(exp_pixel);
00385 cpl_array_delete(status);
00386
00387 status = NULL;
00388 if ((error = cpl_wcs_convert(ref_wcs, exp_equat, &ref_pixel, &status,
00389 CPL_WCS_WORLD2PHYS))
00390 != CPL_ERROR_NONE){
00391 cpl_msg_error(cpl_func, cpl_error_get_message_default(error));
00392 cpl_matrix_delete(exp_equat);
00393 cpl_wcs_delete(exp_wcs);
00394 cpl_bivector_delete(offsets);
00395 cpl_array_delete(status);
00396 return NULL;
00397 }
00398 cpl_matrix_delete(exp_equat);
00399 cpl_wcs_delete(exp_wcs);
00400 cpl_array_delete(status);
00401
00402
00403 offsets_x[i] = cpl_matrix_get(ref_pixel, 0, 0);
00404 offsets_y[i] = cpl_matrix_get(ref_pixel, 0, 1);
00405
00406 cpl_matrix_delete(ref_pixel);
00407 }
00408
00409
00410 cpl_wcs_delete(ref_wcs);
00411 return offsets;
00412 }
00413
00414