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,
00236 "Unable to map data for image %" CPL_SIZE_FORMAT,
00237 (cpl_size)i);
00238 tidy();
00239 FATAL_ERROR
00240 }
00241 if (nconfs == 0) {
00242 icdata = NULL;
00243 } else {
00244 icdata = cpl_image_get_data_int(icimg);
00245 if (icdata == NULL) {
00246 cpl_msg_error(fctid,
00247 "Unable to map data for confidence map %" CPL_SIZE_FORMAT,
00248 (cpl_size)i);
00249 tidy();
00250 FATAL_ERROR
00251 }
00252 }
00253
00254
00255
00256 nx = (int)cpl_image_get_size_x(inimg);
00257 ny = (int)cpl_image_get_size_y(inimg);
00258 for (j = 0; j < ny; j++) {
00259 jindex = j*nx;
00260 joindex = nxo*((int)(((float)j + yoff[i])*fnm + 0.5));
00261 for (k = 0; k < nx; k++) {
00262 iindex = jindex + k;
00263 outindex = joindex + (int)(((float)k + xoff[i])*fnm + 0.5);
00264 if (outindex < 0 || outindex >= nxo*nyo) {
00265 cpl_msg_error(fctid,
00266 "Programming error -- output index out of bounds %" CPL_SIZE_FORMAT,
00267 (cpl_size)outindex);
00268 tidy();
00269 FATAL_ERROR
00270 }
00271 odata[outindex] = idata[iindex] - backmeds[i];
00272 if (ocdata != NULL)
00273 ocdata[outindex] = icdata[iindex];
00274 }
00275 }
00276 }
00277
00278
00279
00280
00281
00282
00283 fnm = 1.0/fnm;
00284 if (vircam_crpixshift(pp,fnm,offs) != VIR_OK) {
00285 tidy();
00286 FATAL_ERROR
00287 }
00288 if (vircam_rescalecd(pp,fnm) != VIR_OK) {
00289 tidy();
00290 FATAL_ERROR
00291 }
00292
00293
00294
00295 vircam_prov(pp,infiles,ninputs);
00296
00297
00298
00299 *outimage = cpl_image_wrap_float((cpl_size)nxo,(cpl_size)nyo,odata);
00300 *outconf = cpl_image_wrap_int((cpl_size)nxo,(cpl_size)nyo,ocdata);
00301
00302
00303
00304 vircam_timestamp(timestamp,25);
00305 p2 = vircam_fits_get_phu(infiles[0]);
00306 cpl_propertylist_update_string(p2,"VIR_TIME",timestamp);
00307 cpl_propertylist_set_comment(p2,"VIR_TIME",
00308 "Timestamp for matching to conf map");
00309
00310
00311
00312 *p = pp;
00313 outok = 1;
00314 tidy();
00315 GOOD_STATUS
00316 }
00317
00318 static void tidy(void) {
00319 if (! outok) {
00320 freespace(odata);
00321 freespace(ocdata);
00322 freepropertylist(pp)
00323 }
00324 freespace(xoff);
00325 freespace(yoff);
00326 freespace(backmeds);
00327 }
00328
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
00380
00381
00382
00383
00384
00385
00386
00387