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
00029
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033
00034 #include <cpl.h>
00035 #include "vircam_utils.h"
00036 #include "vircam_wcsutils.h"
00037 #include "vircam_fits.h"
00038 #include "vircam_mods.h"
00039
00040 static float *odata = NULL;
00041 static int *ocdata = NULL;
00042 static cpl_propertylist *pp = NULL;
00043 static int outok = 0;
00044 static float *xoff = NULL;
00045 static float *yoff = NULL;
00046 static float *backmeds = NULL;
00047
00048 static void tidy(void);
00049
00052
00115
00116
00117 extern int vircam_interleave(vir_fits **infiles, int ninputs,
00118 vir_fits **inconfs, int nconfs, int nsteps,
00119 cpl_propertylist **p, cpl_image **outimage,
00120 cpl_image **outconf, int *status) {
00121 const char *fctid = "vircam_interleave";
00122 char timestamp[25];
00123 int i,j,k,nxo,nyo,itx,nx,ny,jindex,joindex;
00124 int iindex,outindex,*icdata,ival;
00125 float minoff,minzero,*idata,fval;
00126 double offs[2],fnm;
00127 cpl_image *inimg,*icimg;
00128 cpl_propertylist *ehu,*p2;
00129
00130
00131
00132 *outimage = NULL;
00133 *outconf = NULL;
00134 *p = NULL;
00135 if (*status != VIR_OK)
00136 return(*status);
00137
00138
00139
00140 if (ninputs == 0) {
00141 cpl_msg_error(fctid,"No input files to interleave");
00142 tidy();
00143 FATAL_ERROR
00144 }
00145
00146
00147
00148
00149
00150
00151 if (inconfs == NULL) {
00152 nconfs = 0;
00153 } else {
00154 if (nconfs != ninputs)
00155 nconfs = 1;
00156 }
00157
00158
00159
00160
00161 pp = cpl_propertylist_duplicate(vircam_fits_get_ehu(infiles[0]));
00162
00163
00164
00165
00166 xoff = cpl_malloc(ninputs*sizeof(float));
00167 yoff = cpl_malloc(ninputs*sizeof(float));
00168 backmeds = cpl_malloc(ninputs*sizeof(float));
00169 fnm = (double)nsteps;
00170 for (i = 0; i < ninputs; i++) {
00171 ehu = vircam_fits_get_ehu(infiles[i]);
00172 xoff[i] = (float)cpl_propertylist_get_double(ehu,"ESO DRS XOFFMICRO");
00173 fval = xoff[i] - (int)xoff[i];
00174 ival = vircam_nint(fval*fnm);
00175 xoff[i] = (int)xoff[i] + (float)ival/fnm;
00176 yoff[i] = (float)cpl_propertylist_get_double(ehu,"ESO DRS YOFFMICRO");
00177 fval = yoff[i] - (int)yoff[i];
00178 ival = vircam_nint(fval*fnm);
00179 yoff[i] = (int)yoff[i] + (float)ival/fnm;
00180 backmeds[i] = cpl_propertylist_get_float(ehu,"ESO DRS BACKMED");
00181 }
00182
00183
00184
00185 minoff = xoff[0];
00186 for (i = 1; i < ninputs; i++)
00187 minoff = min(minoff,xoff[i]);
00188 for (i = 0; i < ninputs; i++)
00189 xoff[i] -= minoff;
00190 offs[0] = (double)minoff;
00191 minoff = yoff[0];
00192 for (i = 1; i < ninputs; i++)
00193 minoff = min(minoff,yoff[i]);
00194 for (i = 0; i < ninputs; i++)
00195 yoff[i] -= minoff;
00196 offs[1] = (double)minoff;
00197
00198
00199
00200 minzero = backmeds[0];
00201 for (i = 1; i < ninputs; i++)
00202 minzero = min(minzero,backmeds[i]);
00203 for (i = 0; i < ninputs; i++)
00204 backmeds[i] -= minzero;
00205
00206
00207
00208 nxo = 0;
00209 nyo = 0;
00210 for (i = 0; i < ninputs; i++) {
00211 itx = (int)((float)nsteps*((float)cpl_image_get_size_x(vircam_fits_get_image(infiles[i])) + xoff[i]) + 0.5) - 1;
00212 nxo = max(nxo,itx);
00213 itx = (int)((float)nsteps*((float)cpl_image_get_size_y(vircam_fits_get_image(infiles[i])) + yoff[i]) + 0.5) - 1;
00214 nyo = max(nyo,itx);
00215 }
00216
00217
00218
00219 odata = cpl_calloc(nxo*nyo,sizeof(*odata));
00220 if (nconfs > 0)
00221 ocdata = cpl_calloc(nxo*nyo,sizeof(*ocdata));
00222
00223
00224
00225
00226
00227 for (i = 0; i < ninputs; i++) {
00228 inimg = vircam_fits_get_image(infiles[i]);
00229 if (nconfs == 1)
00230 icimg = vircam_fits_get_image(inconfs[0]);
00231 else
00232 icimg = vircam_fits_get_image(inconfs[i]);
00233 idata = cpl_image_get_data_float(inimg);
00234 if (idata == NULL) {
00235 cpl_msg_error(fctid,"Unable to map data for image %d",i);
00236 tidy();
00237 FATAL_ERROR
00238 }
00239 if (nconfs == 0) {
00240 icdata = NULL;
00241 } else {
00242 icdata = cpl_image_get_data_int(icimg);
00243 if (icdata == NULL) {
00244 cpl_msg_error(fctid,
00245 "Unable to map data for confidence map %d",i);
00246 tidy();
00247 FATAL_ERROR
00248 }
00249 }
00250
00251
00252
00253 nx = cpl_image_get_size_x(inimg);
00254 ny = cpl_image_get_size_y(inimg);
00255 for (j = 0; j < ny; j++) {
00256 jindex = j*nx;
00257 joindex = nxo*((int)(((float)j + yoff[i])*fnm + 0.5));
00258 for (k = 0; k < nx; k++) {
00259 iindex = jindex + k;
00260 outindex = joindex + (int)(((float)k + xoff[i])*fnm + 0.5);
00261 if (outindex < 0 || outindex >= nxo*nyo) {
00262 cpl_msg_error(fctid,"Programming error -- output index out of bounds %d\n",outindex);
00263 tidy();
00264 FATAL_ERROR
00265 }
00266 odata[outindex] = idata[iindex] - backmeds[i];
00267 if (ocdata != NULL)
00268 ocdata[outindex] = icdata[iindex];
00269 }
00270 }
00271 }
00272
00273
00274
00275
00276
00277
00278 fnm = 1.0/fnm;
00279 if (vircam_crpixshift(pp,fnm,offs) != VIR_OK) {
00280 tidy();
00281 FATAL_ERROR
00282 }
00283 if (vircam_rescalecd(pp,fnm) != VIR_OK) {
00284 tidy();
00285 FATAL_ERROR
00286 }
00287
00288
00289
00290 vircam_prov(pp,infiles,ninputs);
00291
00292
00293
00294 *outimage = cpl_image_wrap_float(nxo,nyo,odata);
00295 *outconf = cpl_image_wrap_int(nxo,nyo,ocdata);
00296
00297
00298
00299 vircam_timestamp(timestamp,25);
00300 p2 = vircam_fits_get_phu(infiles[0]);
00301 cpl_propertylist_update_string(p2,"VIR_TIME",timestamp);
00302 cpl_propertylist_set_comment(p2,"VIR_TIME",
00303 "Timestamp for matching to conf map");
00304
00305
00306
00307 *p = pp;
00308 outok = 1;
00309 tidy();
00310 GOOD_STATUS
00311 }
00312
00313 static void tidy(void) {
00314 if (! outok) {
00315 freespace(odata);
00316 freespace(ocdata);
00317 freepropertylist(pp)
00318 }
00319 freespace(xoff);
00320 freespace(yoff);
00321 freespace(backmeds);
00322 }
00323
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379