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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 #include <stdio.h>
00090 #include <math.h>
00091 #include <stdlib.h>
00092 #include <time.h>
00093 #include <sys/time.h>
00094 #include <sys/resource.h>
00095 #include <fftw3.h>
00096 #include "jsoc_main.h"
00097 #include "fstats.h"
00098 #include "drms_dsdsapi.h"
00099 #include "errlog.h"
00100 #include "projection.h"
00101 #include "atoinc.h"
00102
00103
00104
00105
00106
00107 #define ARRLENGTH(ARR) (sizeof(ARR)/sizeof(ARR[0]))
00108 #define PI (M_PI)
00109
00110 #define absval(x) (((x) < 0) ? (-(x)) : (x))
00111 #define minval(x,y) (((x) < (y)) ? (x) : (y))
00112 #define maxval(x,y) (((x) < (y)) ? (y) : (x))
00113 #define very_small (1.0e-30)
00114 #define is_very_small(x) (absval(x) < very_small)
00115 #define is_even(x) (!((x)%2))
00116 #define is_odd(x) ((x)%2)
00117
00118 #define RADSINDEG (PI/180)
00119 #define ARCSECSINRAD (3600*180/PI)
00120 #define DAYSINYEAR (365.2425)
00121 #define SECSINDAY (86400)
00122 #define TAU_A (499.004783806) // light travel time in seconds, = 1 AU/c
00123
00124 #define TCARR (25.38) // days
00125 #define RTRUE (6.96000000e8) // meters
00126 #define AU (149597870691) // meters/au
00127
00128
00129 #define QUAL_NODATA (0x80000000)
00130 #define QUAL_MIXEDCALVER (0x00000001)
00131 #define kNOTSPECIFIED "not specified"
00132
00133 #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s, status=%d\n", msg, status); return(status);}
00134
00135
00136 extern int img2helioVector (double bxImg, double byImg, double bzImg, double *bxHelio, double *byHelio, double *bzHelio, double lon, double lat, double lonc, double latc, double pAng);
00137
00138
00139 extern int synop_plane2sphere (double x, double y, double latc, double lonc, double *lat, double *lon, int projection);
00140 extern int synop_img2sphere(double x, double y, double ang_r, double latc, double lonc, double pa, double *rho, double *lat, double *lon, double *sinlat, double *coslat, double *sig, double *mu, double *chi);
00141 extern int synop_sphere2img(double lat, double lon, double latc, double lonc, double *x, double *y, double xcenter, double ycenter, double rsun, double peff, double ecc, double chi, int xinvrt, int yinvrt);
00142 extern int synop_sphere2plane(double lat, double lon, double latc, double lonc, double *x, double *y, int projection);
00143
00144
00145 extern double getwalltime(void);
00146 extern double getcputime(double *utime, double *stime);
00147
00148
00149 extern long long set_history(DRMS_Link_t *histlink);
00150
00151
00152 extern unsigned long long getbits(unsigned long long x, int p, int n);
00153 extern unsigned long long setbits(unsigned long long x, int p, int n, unsigned long long y);
00154 unsigned long long fixcalver64(unsigned long long x);
00155
00156
00157 extern char *savestr;
00158 extern int savestrlen;
00159 extern int savestrmax;
00160 extern const char *cmdparams_save_str(CmdParams_t *parms, char *name, int *status);
00161 extern float cmdparams_save_float (CmdParams_t *parms, char *name, int *status);
00162 extern double cmdparams_save_double (CmdParams_t *parms, char *name, int *status);
00163 extern int cmdparams_save_int (CmdParams_t *parms, char *name, int *status);
00164 extern double cmdparams_save_time (CmdParams_t *parms, char *name, int *status);
00165 extern int cmdparams_save_flag (CmdParams_t *parms, char *name, int *status);
00166 extern const char *cmdparams_save_arg (CmdParams_t *parms, int num, int *status);
00167 extern void cpsave_decode_error(int status);
00168
00169 char *module_name = "vectmag2helio3comp_random";
00170
00171
00172 ModuleArgs_t module_args[] =
00173 {
00174
00175 {ARG_STRING, "in", NULL, "input data records"},
00176 {ARG_STRING, "tsout", kNOTSPECIFIED, "output data series"},
00177 {ARG_STRING, "segin", kNOTSPECIFIED, "input data segment"},
00178 {ARG_STRING, "segout", kNOTSPECIFIED, "output data segment"},
00179 {ARG_STRING, "histlink", "HISTORY", "name of link to ancillary dataseries for processing metadata. specify \"none\" to suppress warning messages."},
00180 {ARG_STRING, "srclink", "SRCDATA", "name of link to source data"},
00181 {ARG_STRING, "v2hout", kNOTSPECIFIED, "output data series for jv2helio"},
00182 {ARG_STRING, "h2mout", kNOTSPECIFIED, "output data series for jhelio2mlat"},
00183 {ARG_INT, "PERM", "1", "set to 0 for transient records, nonzero for permanent records"},
00184 {ARG_INT, "VERB", "1", "option for level of verbosity: 0 gives only error and warning messages; >0 prints messages outside of loop; >1 prints messages inside loop; >2 for debugging output"},
00185 {ARG_INT, "FORCEOUTPUT", "0", "option to specify behavior on missing inputs; 0 gives an error on missing or duplicate inputs, >0 makes outputs regardless"},
00186 {ARG_STRING, "TAG", "none", "this parameter sets a keyword of the same name to the same value, usually used as an additional primekey"},
00187 {ARG_STRING, "VERSION", kNOTSPECIFIED, "this parameter sets a keyword of the same name to the same value, useful for selecting obsolete records"},
00188 {ARG_INT, "CALVERKEY", "2", "short integer bit mask determining which 4-bit fields of CALVER64 are permitted to change on input. set the bit to disallow change of the corresponding nybble."},
00189
00190 {ARG_INT, "LMIN", "0", "minimum l in output"},
00191
00192 {ARG_INT, "LCHUNK", "0", "increment in l on output, default is lmax+1"},
00193 {ARG_INT, "NORM", "1", "set to nonzero to use cnorm=sinbdelta*sqrt(2) in sgemm call, otherwise use cnorm=1"},
00194 {ARG_TIME, "TSTART", NULL, "start time of first output record"},
00195 {ARG_STRING, "TTOTAL", NULL, "total length of time in output"},
00196 {ARG_STRING, "TCHUNK", kNOTSPECIFIED, "length of output timeseries"},
00197
00198 {ARG_INT, "LMAX", "300", "maximum l (maximum m) in the output, cannot be greater than MAPMMAX", NULL},
00199 {ARG_INT, "SUBMEAN", "0", "nonzero subtracts mean of input image", NULL},
00200 {ARG_INT, "NORMLIZE", "0", "nonzero multiplies by sqrt((fraction nonmissing)/2) for each row", NULL},
00201 {ARG_INT, "CENTLONG", "1", "nonzero centers the longitude Fourier transform on the center of the remapped image", NULL},
00202 {ARG_INT, "ZEROMISS", "0", "zero sets any row containing a NaN to DRMS_MISSING", NULL},
00203 {ARG_INT, "LGAPOD", "0", "nonzero apodizes in longitude", NULL},
00204 {ARG_DOUBLE, "LGAPMIN", "60.0", "start of longitude apodization, degrees", NULL},
00205 {ARG_DOUBLE, "LGAPWIDT", "10.0", "width of longitude apodization, degrees", NULL},
00206
00207 {ARG_INT, "MAPMMAX", "5402", "determines mapcols", ""},
00208 {ARG_INT, "SINBDIVS", "2160", "number of increments in sin latitude from 0 to 1", ""},
00209 {ARG_FLOAT, "MAPRMAX", "0.998", "maximum image radius", ""},
00210 {ARG_INT, "NAN_BEYOND_RMAX", "0", "set to nonzero to set output to DRMS_MISSING outside MAPRMAX, otherwise uses 0.0 outside MAPRMAX"},
00211 {ARG_FLOAT, "MAPLGMAX", "90.0", "longitude maximum, degrees", ""},
00212 {ARG_FLOAT, "MAPLGMIN", "-90.0", "longitude minimum, degrees", ""},
00213 {ARG_FLOAT, "MAPBMAX", "90.0", "latitude maximum, degrees, also used for minimum", ""},
00214 {ARG_INT, "LGSHIFT", "3", "option for longitude shift: 0=none; 1=fixed rate; 2=nearest degree; 3=nearest tenth of a degree", ""},
00215 {ARG_TIME, "REF_T0", "1987.01.03_17:31:12_TAI", "reference time for computing fixed rate longitude shift", ""},
00216 {ARG_FLOAT, "REF_L0", "0.0", "reference longitude for computing fixed rate longitude shift ", ""},
00217 {ARG_FLOAT, "SOLAR_P", "999.0", "P-angle; if unset, taken from keywords", ""},
00218 {ARG_FLOAT, "PSIGN", "1.0", "sign of SOLAR_P", ""},
00219 {ARG_FLOAT, "PERR", "0.0", "fixed P-angle error, likely -0.22", ""},
00220 {ARG_FLOAT, "IERR", "0.0", "error in Carrington inclination, likely -0.10", ""},
00221 {ARG_TIME, "REF_TB0", "2001.06.06_06:57:22_TAI", "reference time for computing correction to P and B angles, roughly when B0=0", ""},
00222 {ARG_INT, "INTERPO", "1", "option for interpolation: 0=bilinear; 1=cubic convolution", ""},
00223 {ARG_INT, "APODIZE", "0", "option for apodization: 0=none; 1=use true solar coordinates; 2=use ideal solar coordinates (b0=0)", ""},
00224 {ARG_FLOAT, "APINNER", "0.90", "start of apodization in fractional image radius", ""},
00225 {ARG_FLOAT, "APWIDTH", "0.05", "width of apodization in fractional image radius", ""},
00226 {ARG_INT, "APEL", "0", "set to nonzero for elliptical apodization described by APX and APY", ""},
00227 {ARG_FLOAT, "APX", "1.00", "divide the x position by this before applying apodization", ""},
00228 {ARG_FLOAT, "APY", "1.00", "divide the y position by this before applying apodization", ""},
00229 {ARG_INT, "VCORLEV", "0", "option for velocity correction: 0=none; 1=subtract a model of differential rotation; 2=also divide by line of sight projection factor for purely radial velocities", ""},
00230 {ARG_INT, "MCORLEV", "0", "option for magnetic correction: 0=none; 1=line of sight; 2=radial", ""},
00231 {ARG_INT, "MOFFSETFLAG", "0", "set to nonzero to get BFITZERO from input record and subtract from data before interpolating", ""},
00232 {ARG_FLOAT, "OUTSCALE", "1.0", "bscale to use for output", ""},
00233 {ARG_FLOAT, "OUTBIAS", "0.0", "bzero to use for output", ""},
00234 {ARG_INT, "DISTORT", "0", "option for distortion correction: 0=none; 1=full disk(fd) data; 2=vector-weighted(vw) data", ""},
00235 {ARG_FLOAT, "CUBIC", "7.06E-9", "cubic distortion in fd units", ""},
00236 {ARG_FLOAT, "TILTALPHA", "2.59", "tilt of CCD, degrees", ""},
00237 {ARG_FLOAT, "TILTBETA", "56.0", "direction of CCD tilt, degrees", ""},
00238 {ARG_FLOAT, "TILTFEFF", "12972.629", "effective focal length", ""},
00239 {ARG_INT, "OFLAG", "0", "set to nonzero to force reading orientation from keyword, otherwise \"SESW\" is assumed)", ""},
00240 {ARG_INT, "DATASIGN", "0", "value to multiply data; set to 0 to take DATASIGN from keyword, or 1.0 if not found", ""},
00241 {ARG_INT, "MAXMISSVALS", "0", "if >0, this becomes threshold on MISSVALS from keyword", ""},
00242 {ARG_INT, "CARRSTRETCH", "1", "set to nonzero to correct for differential rotation according to DIFROT_[ABC]", ""},
00243 {ARG_FLOAT, "DIFROT_A", "13.562", "A coefficient in differential rotation adjustment (offset)", ""},
00244 {ARG_FLOAT, "DIFROT_B", "-2.04", "B coefficient (to sin(lat) ^ 2)", ""},
00245 {ARG_FLOAT, "DIFROT_C", "-1.4875", "C coefficient (to sin(lat) ^ 4)", ""},
00246 {ARG_FLOAT, "RESCALE", "0.333333", "Scale factor."},
00247
00248 {ARG_END}
00249 };
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 void scopy_(int *, float *, int *, float *, int *);
00274 void setplm_(int *, int *, int *, int *, int *, double *, int *, double *);
00275 void sgemm_();
00276 void do_boxcar(float *image_in, float *image_out, int in_nx, int in_ny, float fscale);
00277
00278 typedef enum
00279 {
00280 V2HStatus_Success,
00281 V2HStatus_MissingParameter,
00282 V2HStatus_IllegalTimeFormat,
00283 V2HStatus_TimeConvFailed,
00284 V2HStatus_Unimplemented,
00285 V2HStatus_IllegalOrientation
00286 } V2HStatus_t;
00287
00288 static void CheckO(const char *orientation, V2HStatus_t *stat)
00289 {
00290
00291 static char o[16];
00292 char *c = o;
00293
00294 if(!orientation)
00295 {
00296 *stat = V2HStatus_MissingParameter;
00297 }
00298 else if (4 != sscanf(orientation, "%[NS]%[EW]%[NS]%[EW]", c++, c++, c++, c++))
00299 {
00300 *stat = V2HStatus_IllegalOrientation;
00301 }
00302 else if ((o[0] == o[2]) && (o[1] == o[3]))
00303 {
00304 *stat = V2HStatus_IllegalOrientation;
00305 }
00306 else if ((o[0] !=o [2]) && (o[1] != o[3]))
00307 {
00308 *stat = V2HStatus_IllegalOrientation;
00309 }
00310 else
00311 {
00312 *stat = V2HStatus_Success;
00313 }
00314 }
00315
00316 static inline void ParameterDef(int status,
00317 char *pname,
00318 double defaultp,
00319 double *p,
00320 long long recnum,
00321 int verbflag)
00322 {
00323
00324 if (status != 0)
00325 {
00326 *p = defaultp;
00327 if (verbflag)
00328 fprintf(stderr, "WARNING: default value %g used for %s, status = %d, recnum = %lld\n", defaultp, pname, status, recnum);
00329 }
00330 }
00331
00332 #define PARAMETER_ERROR(PNAME) \
00333 if (status != DRMS_SUCCESS) \
00334 { \
00335 fprintf(stderr, "ERROR: problem with required keyword %s, status = %d, T_REC = %s, recnum = %lld, histrecnum = %lld\n", PNAME, status, trecstr, inrec->recnum, histrecnum); \
00336 drms_free_array(inarrBtotal); \
00337 drms_free_array(inarrBincl); \
00338 drms_free_array(inarrBazim); \
00339 drms_free_array(inarrBdisamb); \
00340 return 0; \
00341 }
00342
00343
00344 int DoIt(void)
00345 {
00346 int newstat=0;
00347 DRMS_RecChunking_t chunkstat = kRecChunking_None;
00348 int fetchstat = DRMS_SUCCESS;
00349 int status = DRMS_SUCCESS;
00350 char *inrecquery = NULL;
00351 char *outseries = NULL;
00352 char *segnamein = NULL;
00353 char *segnameout = NULL;
00354 DRMS_RecordSet_t *inrecset = NULL;
00355 DRMS_Record_t *inrec = NULL;
00356 DRMS_Record_t *outrec = NULL;
00357 DRMS_Segment_t *segin = NULL;
00358 DRMS_Segment_t *segout = NULL;
00359 DRMS_Array_t *inarrBdisamb = NULL, *inarrBtotal = NULL, *inarrBincl = NULL, *inarrBazim = NULL;
00360 DRMS_Array_t *outarr = NULL;
00361 DRMS_Type_t usetype = DRMS_TYPE_FLOAT;
00362 DRMS_RecLifetime_t lifetime;
00363 long long histrecnum=-1;
00364 int length[2];
00365
00366 double wt0, wt1, wt2, wt3, wt;
00367 double ut0, ut1, ut2, ut3, ut;
00368 double st0, st1, st2, st3, st;
00369 double ct0, ct1, ct2, ct3, ct;
00370
00371 TIME trec, tnow, UNIX_epoch = -220924792.000;
00372 char trecstr[100], tstartstr[100];
00373
00374 int quality;
00375 float *bTotal, *bIncl, *bAzim, *disamb;
00376 int xDim, yDim, iData, ix, jy, yOff;
00377
00378
00379 float *h2mptr;
00380 float *v2hbr, *v2hbt, *v2hbp;
00381
00382
00383 float *oddpart, *evenpart, *inptr, *workptr, *mptr, *outptr;
00384 float *folded, *masks, *real4evenl, *real4oddl, *imag4evenl, *imag4oddl, *outx;
00385
00386
00387 double *plm, *saveplm, *latgrid;
00388 int *indx;
00389
00390 double sinBdelta;
00391 double *plmptr;
00392 float *maskptr;
00393
00394 int nsn, fournsn, snx, maxnsn;
00395 int lchunksize, lchunkfirst, lchunklast, lchunk, l;
00396 int lmin, lmax;
00397 int msize, mx, foldedsize;
00398 int maprows, nlat, imagesize, latx, poslatx, neglatx, moffset, nlatx;
00399 int i, m, modd, meven;
00400 int lfirst, llast, ifirst, ilast, lstart, ldone;
00401 int lfirsteven, llasteven, nevenl, lfirstodd, llastodd, noddl;
00402 int fromoffset, tooffset, imageoffset;
00403
00404 int increment1 = 1;
00405 int increment2 = 2;
00406
00407
00408 char transpose[] = "t";
00409 char normal[] = "n";
00410 float one = 1.0;
00411 float zero = 0.0;
00412 int normflag;
00413 float cnorm;
00414
00415 double tstart, tepoch, tstep, tround, cadence, nseconds, chunksecs, cadence0;
00416 char *ttotal, *tchunk;
00417 int nrecs, irec, trecind, bc, iset, ntimechunks;
00418 int *bad;
00419 int ndt;
00420
00421
00422 double mean, norm=1.0, normx;
00423 int subtract_mean, normalize, cent_long, zero_miss, lgapod;
00424 double lgapmin, lgapwidt, lgapmax, lon;
00425
00426 int row, col;
00427 int lfft, mapped_lmax;
00428 int mapcols2;
00429 int nfft, nmean, nok, nout;
00430
00431 float *buf, *bp, *ip, *inp, *op, *outp, *weight, *wp;
00432 float *wbuf;
00433 fftwf_plan fftwp;
00434
00435
00436 V2HStatus_t vstat = V2HStatus_Success;
00437 const char *orientationdef = "SESW ";
00438 char *orientation = NULL;
00439 int paramsign;
00440 int longitude_shift, velocity_correction, interpolation, apodization;
00441 int mag_correction;
00442 int mag_offset;
00443 int sinb_divisions, mapcols, nmax, nmin;
00444 int carrStretch = 0;
00445 float diffrotA = 0.0;
00446 float diffrotB = 0.0;
00447 float diffrotC = 0.0;
00448 double tobs, tmearth, tref, trefb0;
00449 double smajor, sminor, sangle;
00450 double xscale, yscale, imagescale;
00451 int xpixels, ypixels, pixels;
00452 double obs_vr, obs_vw, obs_vn;
00453 double b0, bmax, bmin;
00454 double longmax, longmin, longmax_adjusted, longmin_adjusted, longinterval;
00455 double p0, p, rmax;
00456 double ierr, perr, psign;
00457 double x0, y0;
00458 double obsdist, longshift, obsl0, refl0, mapl0, longrate, rtrue, rsun, S;
00459 double rsunDef, rsunobs;
00460 int obsCR;
00461 int apel;
00462 double apinner, apwidth, apx, apy;
00463 double scale, bias;
00464 double colsperdeg;
00465
00466 double satrot, instrot;
00467 double dsignout, vsign;
00468 int distsave;
00469 double cubsave, tiltasave, tiltbsave, tiltfsave;
00470 LIBPROJECTION_Dist_t distP;
00471
00472 int errbufstat=setvbuf(stderr, NULL, _IONBF, BUFSIZ);
00473 int outbufstat=setvbuf(stdout, NULL, _IONBF, BUFSIZ);
00474
00475 wt0=getwalltime();
00476 ct0=getcputime(&ut0, &st0);
00477
00478 inrecquery = (char *)cmdparams_save_str(&cmdparams, "in", &newstat);
00479 outseries = (char *)cmdparams_save_str(&cmdparams, "tsout", &newstat);
00480 int tsflag = strcmp(kNOTSPECIFIED, outseries);
00481 segnamein = (char *)cmdparams_save_str(&cmdparams, "segin", &newstat);
00482 segnameout = (char *)cmdparams_save_str(&cmdparams, "segout", &newstat);
00483 int seginflag = strcmp(kNOTSPECIFIED, segnamein);
00484 int segoutflag = strcmp(kNOTSPECIFIED, segnameout);
00485 int verbflag = cmdparams_save_int(&cmdparams, "VERB", &newstat);
00486 int permflag = cmdparams_save_int(&cmdparams, "PERM", &newstat);
00487 if (permflag)
00488 lifetime = DRMS_PERMANENT;
00489 else
00490 lifetime = DRMS_TRANSIENT;
00491 int forceoutput = cmdparams_save_int(&cmdparams, "FORCEOUTPUT", &newstat);
00492 char *tag = (char *)cmdparams_save_str(&cmdparams, "TAG", &newstat);
00493 char *version = (char *)cmdparams_save_str(&cmdparams, "VERSION", &newstat);
00494 int verflag = strcmp(kNOTSPECIFIED, version);
00495 unsigned short calverkey = (unsigned short)cmdparams_save_int(&cmdparams, "CALVERKEY", &newstat);
00496
00497 char *histlinkname = (char *)cmdparams_save_str(&cmdparams, "histlink", &newstat);
00498 char *srclinkname = (char *)cmdparams_save_str(&cmdparams, "srclink", &newstat);
00499
00500 char *v2hout = (char *)cmdparams_save_str(&cmdparams, "v2hout", &newstat);
00501 char *h2mout = (char *)cmdparams_save_str(&cmdparams, "h2mout", &newstat);
00502 int v2hflag = strcmp(kNOTSPECIFIED, v2hout);
00503 int h2mflag = strcmp(kNOTSPECIFIED, h2mout);
00504 int histflag = strncasecmp("none", histlinkname, 4);
00505
00506 if (!v2hflag && !h2mflag && !tsflag)
00507 {
00508 fprintf(stderr, "ERROR: no outputs specified.\n");
00509 return 1;
00510 }
00511
00512 lmin=cmdparams_save_int(&cmdparams, "LMIN", &newstat);
00513 lmax=cmdparams_save_int(&cmdparams, "LMAX", &newstat);
00514 lchunksize=cmdparams_save_int(&cmdparams, "LCHUNK", &newstat);
00515 normflag=cmdparams_save_int(&cmdparams, "NORM", &newstat);
00516
00517 tstart=cmdparams_save_time(&cmdparams, "TSTART", &newstat);
00518 sprint_time(tstartstr, tstart, "TAI", 0);
00519 ttotal=(char *)cmdparams_save_str(&cmdparams, "TTOTAL", &newstat);
00520 nseconds=atoinc(ttotal);
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538 tchunk=(char *)cmdparams_save_str(&cmdparams, "TCHUNK", &newstat);
00539 if (strcmp(kNOTSPECIFIED, tchunk))
00540 {
00541 chunksecs=atoinc(tchunk);
00542
00543
00544
00545
00546
00547 }
00548 else if (!tsflag)
00549 {
00550 fprintf(stderr, "ERROR: TCHUNK must be specified if no tsout is given.\n");
00551 return 1;
00552 }
00553 else
00554 chunksecs=0.0;
00555
00556
00557 subtract_mean = cmdparams_save_int(&cmdparams, "SUBMEAN", &newstat);
00558 normalize = cmdparams_save_int(&cmdparams, "NORMLIZE", &newstat);
00559
00560
00561 cent_long = cmdparams_save_int(&cmdparams, "CENTLONG", &newstat);
00562
00563
00564 zero_miss = cmdparams_save_int(&cmdparams, "ZEROMISS", &newstat);
00565 lgapod = cmdparams_save_int(&cmdparams, "LGAPOD", &newstat);
00566 lgapmin = cmdparams_save_double(&cmdparams, "LGAPMIN", &newstat);
00567 lgapwidt = cmdparams_save_double(&cmdparams, "LGAPWIDT", &newstat);
00568 lgapmax = lgapmin+lgapwidt;
00569
00570 int checko = cmdparams_save_int(&cmdparams, "OFLAG", &newstat);
00571 int NaN_beyond_rmax = cmdparams_save_int(&cmdparams, "NAN_BEYOND_RMAX", &newstat);
00572 int maxmissvals = cmdparams_save_int(&cmdparams, "MAXMISSVALS", &newstat);
00573 float rescale = params_get_float(&cmdparams, "RESCALE");
00574
00575
00576
00577 carrStretch = cmdparams_save_int(&cmdparams, "CARRSTRETCH", &newstat);
00578 diffrotA = cmdparams_save_float(&cmdparams, "DIFROT_A", &newstat);
00579 diffrotB = cmdparams_save_float(&cmdparams, "DIFROT_B", &newstat);
00580 diffrotC = cmdparams_save_float(&cmdparams, "DIFROT_C", &newstat);
00581
00582 longrate = 360.0 / TCARR - 360.0 / DAYSINYEAR;
00583 longrate /= SECSINDAY;
00584
00585 apodization = cmdparams_save_int(&cmdparams, "APODIZE", &newstat);
00586 apinner = cmdparams_save_double(&cmdparams, "APINNER", &newstat);
00587 apwidth = cmdparams_save_double(&cmdparams, "APWIDTH", &newstat);
00588 apel = cmdparams_save_int(&cmdparams, "APEL", &newstat);
00589 apx = cmdparams_save_double(&cmdparams, "APX", &newstat);
00590 apy = cmdparams_save_double(&cmdparams, "APY", &newstat);
00591 longitude_shift = cmdparams_save_int(&cmdparams, "LGSHIFT", &newstat);
00592 mag_correction = cmdparams_save_int(&cmdparams, "MCORLEV", &newstat);
00593 mag_offset = cmdparams_save_int(&cmdparams, "MOFFSETFLAG", &newstat);
00594 velocity_correction = cmdparams_save_int(&cmdparams, "VCORLEV", &newstat);
00595 interpolation = cmdparams_save_int(&cmdparams, "INTERPO", &newstat);
00596 paramsign = cmdparams_save_int(&cmdparams, "DATASIGN", &newstat);
00597 rmax = cmdparams_save_double(&cmdparams, "MAPRMAX", &newstat);
00598 refl0 = cmdparams_save_double(&cmdparams, "REF_L0", &newstat);
00599
00600 distsave = cmdparams_save_int(&cmdparams, "DISTORT", &newstat);
00601 cubsave = cmdparams_save_double(&cmdparams, "CUBIC", &newstat);
00602 tiltasave = cmdparams_save_double(&cmdparams, "TILTALPHA", &newstat);
00603 tiltbsave = cmdparams_save_double(&cmdparams, "TILTBETA", &newstat);
00604 tiltfsave = cmdparams_save_double(&cmdparams, "TILTFEFF", &newstat);
00605
00606 scale = cmdparams_save_double(&cmdparams, "OUTSCALE", &newstat);
00607 bias = cmdparams_save_double(&cmdparams, "OUTBIAS", &newstat);
00608 p0 = cmdparams_save_double(&cmdparams, "SOLAR_P", &newstat);
00609 psign = cmdparams_save_double(&cmdparams, "PSIGN", &newstat);
00610 perr = cmdparams_save_double(&cmdparams, "PERR", &newstat);
00611 ierr = cmdparams_save_double(&cmdparams, "IERR", &newstat);
00612 trefb0 = cmdparams_save_time(&cmdparams, "REF_TB0", &newstat);
00613
00614 SetDistort(distsave, cubsave, tiltasave, tiltbsave, tiltfsave, &distP);
00615
00616 tref = cmdparams_save_time(&cmdparams, "REF_T0", &newstat);
00617
00618
00619 mapped_lmax = cmdparams_save_int(&cmdparams, "MAPMMAX", &newstat);
00620 longmax = cmdparams_save_double(&cmdparams, "MAPLGMAX", &newstat);
00621 longmin = cmdparams_save_double(&cmdparams, "MAPLGMIN", &newstat);
00622 longinterval = (180.0) / mapped_lmax;
00623
00624
00625
00626
00627
00628 nmin = (int)(longmin / longinterval);
00629 nmax = (int)(longmax / longinterval);
00630 colsperdeg = mapped_lmax / 180.0;
00631 nmin = (int)(longmin * colsperdeg);
00632 nmax = (int)(longmax * colsperdeg);
00633 mapcols = nmax - nmin + 1;
00634 longmin_adjusted = nmin * longinterval;
00635 longmax_adjusted = nmax * longinterval;
00636
00637
00638 sinb_divisions = cmdparams_save_int(&cmdparams, "SINBDIVS", &newstat);
00639 sinBdelta = 1.0/sinb_divisions;
00640 bmax = cmdparams_save_double(&cmdparams, "MAPBMAX", &newstat);
00641 bmin = -bmax;
00642 nmax = (int)(sin(RADSINDEG*bmax)*sinb_divisions);
00643 maprows = 2*nmax;
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653 if (normflag == 0)
00654 cnorm=1.0;
00655 else
00656 cnorm = sqrt(2.)*sinBdelta;
00657
00658 if (lmax > mapped_lmax || lmin > lmax)
00659 {
00660 fprintf(stderr, "ERROR: must have MAPMMAX >= LMAX >= LMIN, MAPMMAX = %d, LMAX= %d, LMIN = %d\n", mapped_lmax, lmax, lmin);
00661 return 1;
00662 }
00663
00664 if (newstat)
00665 {
00666 fprintf(stderr, "ERROR: problem with input arguments, status = %d, diagnosis follows\n", newstat);
00667 cpsave_decode_error(newstat);
00668 return 1;
00669 }
00670 else if (savestrlen != strlen(savestr))
00671 {
00672 fprintf(stderr, "ERROR: problem with savestr, savestrlen = %d, strlen(savestr) = %d\n", savestrlen, (int)strlen(savestr));
00673 return 1;
00674 }
00675
00676 DRMS_Record_t *tempoutrec;
00677 DRMS_Link_t *histlink;
00678 int itest;
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688 if (tsflag)
00689 {
00690 tempoutrec = drms_create_record(drms_env, outseries, DRMS_TRANSIENT, &status);
00691 if (status != DRMS_SUCCESS)
00692 {
00693 fprintf(stderr,"ERROR: couldn't open a record in output dataseries %s, status = %d\n", outseries, status);
00694 return 1;
00695 }
00696
00697 DRMS_Keyword_t *outkeytest = hcon_lookup_lower(&tempoutrec->keywords, "MAPMMAX");
00698 if (outkeytest != NULL && outkeytest->info->recscope == 1)
00699 {
00700 int mapmmaxout=drms_getkey_int(tempoutrec, "MAPMMAX", &status);
00701 if (mapmmaxout != mapped_lmax)
00702 {
00703 fprintf(stderr,"ERROR: output MAPMMAX=%d does not match input parameter MAPMMAX=%d, status = %d\n", mapmmaxout, mapped_lmax, status);
00704 return 1;
00705 }
00706 }
00707
00708 outkeytest = hcon_lookup_lower(&tempoutrec->keywords, "SINBDIVS");
00709 if (outkeytest != NULL && outkeytest->info->recscope == 1)
00710 {
00711 int sinbdivsout=drms_getkey_int(tempoutrec, "SINBDIVS", &status);
00712 if (sinbdivsout != sinb_divisions)
00713 {
00714 fprintf(stderr,"ERROR: output SINBDIVS=%d does not match input parameter SINBDIVS=%d, status = %d\n", sinbdivsout, sinb_divisions, status);
00715 return 1;
00716 }
00717 }
00718
00719
00720 if (histflag)
00721 {
00722 histlink = hcon_lookup_lower(&tempoutrec->links, histlinkname);
00723 if (histlink != NULL)
00724 {
00725 histrecnum=set_history(histlink);
00726 if (histrecnum < 0)
00727 {
00728 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00729 return 1;
00730 }
00731 }
00732 else
00733 {
00734 fprintf(stderr,"WARNING: could not find history link in output dataseries\n");
00735 }
00736 }
00737
00738
00739 char *outchecklist[] = {"T_START", "QUALITY", "LMIN", "LMAX", "NDT"};
00740 for (itest=0; itest < ARRLENGTH(outchecklist); itest++)
00741 {
00742 DRMS_Keyword_t *outkeytest = hcon_lookup_lower(&tempoutrec->keywords, outchecklist[itest]);
00743 if (outkeytest == NULL || outkeytest->info->islink || outkeytest->info->recscope == 1)
00744 {
00745 fprintf(stderr, "ERROR: output keyword %s is either missing, constant, or a link\n", outchecklist[itest]);
00746 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00747 return 1;
00748 }
00749 }
00750
00751 cadence0=drms_getkey_float(tempoutrec, "T_STEP", &status);
00752 tepoch=drms_getkey_time(tempoutrec, "T_START_epoch", &status);
00753 tstep=drms_getkey_float(tempoutrec, "T_START_step", &status);
00754 tround=drms_getkey_float(tempoutrec, "T_START_round", &status);
00755 if (fmod(tstart-tepoch,tstep) > tround/2)
00756 {
00757 sprint_time(trecstr, tepoch, "TAI", 0);
00758 fprintf(stderr, "ERROR: output dataseries seems incompatible with input parameters (tstep must divide tstart-tepoch): TSTART = %s, T_START_epoch = %s, tstep = %f\n",
00759 tstartstr, trecstr, tstep);
00760 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00761 return 1;
00762 }
00763 if (chunksecs == 0.0)
00764 chunksecs = tstep;
00765 else if (fmod(chunksecs,tstep))
00766 {
00767 fprintf(stderr, "ERROR: output dataseries seems incompatible with input parameters (tstep must divide chunksecs): chunksecs = %f, tstep = %f\n", chunksecs, tstep);
00768 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00769 return 1;
00770 }
00771
00772 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00773 }
00774
00775 if (v2hflag)
00776 {
00777 tempoutrec = drms_create_record(drms_env, v2hout, DRMS_TRANSIENT, &status);
00778 if (status != DRMS_SUCCESS)
00779 {
00780 fprintf(stderr,"ERROR: couldn't open a record in output dataseries %s, status = %d\n", v2hout, status);
00781 return 1;
00782 }
00783
00784
00785 if (histflag && histrecnum < 0)
00786 {
00787 histlink = hcon_lookup_lower(&tempoutrec->links, histlinkname);
00788 if (histlink != NULL)
00789 {
00790 histrecnum=set_history(histlink);
00791 if (histrecnum < 0)
00792 {
00793 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00794 return 1;
00795 }
00796 }
00797 else
00798 {
00799 fprintf(stderr,"WARNING: could not find history link in output dataseries\n");
00800 }
00801 }
00802
00803
00804 char *outchecklist[] = {"T_REC", "QUALITY", "CRPIX1", "CRVAL1", "CDELT1", "CRPIX2", "CROTA2", "CDELT2" };
00805 for (itest=0; itest < ARRLENGTH(outchecklist); itest++)
00806 {
00807 DRMS_Keyword_t *outkeytest = hcon_lookup_lower(&tempoutrec->keywords, outchecklist[itest]);
00808 if (outkeytest == NULL || outkeytest->info->islink || outkeytest->info->recscope == 1)
00809 {
00810 fprintf(stderr, "ERROR: output keyword %s is either missing, constant, or a link\n", outchecklist[itest]);
00811 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00812 return 1;
00813 }
00814 }
00815
00816 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00817 }
00818
00819 if (h2mflag)
00820 {
00821 tempoutrec = drms_create_record(drms_env, h2mout, DRMS_TRANSIENT, &status);
00822 if (status != DRMS_SUCCESS)
00823 {
00824 fprintf(stderr,"ERROR: couldn't open a record in output dataseries %s, status = %d\n", h2mout, status);
00825 return 1;
00826 }
00827
00828
00829 if (histflag && histrecnum < 0)
00830 {
00831 histlink = hcon_lookup_lower(&tempoutrec->links, histlinkname);
00832 if (histlink != NULL)
00833 {
00834 histrecnum=set_history(histlink);
00835 if (histrecnum < 0)
00836 {
00837 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00838 return 1;
00839 }
00840 }
00841 else
00842 {
00843 fprintf(stderr,"WARNING: could not find history link in output dataseries\n");
00844 }
00845 }
00846
00847
00848 char *outchecklist[] = {"T_REC", "QUALITY", "CRPIX1", "CDELT1", "CRPIX2", "CDELT2" };
00849 for (itest=0; itest < ARRLENGTH(outchecklist); itest++)
00850 {
00851 DRMS_Keyword_t *outkeytest = hcon_lookup_lower(&tempoutrec->keywords, outchecklist[itest]);
00852 if (outkeytest == NULL || outkeytest->info->islink || outkeytest->info->recscope == 1)
00853 {
00854 fprintf(stderr, "ERROR: output keyword %s is either missing, constant, or a link\n", outchecklist[itest]);
00855 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00856 return 1;
00857 }
00858 }
00859
00860 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00861 }
00862
00863
00864 if (fmod(nseconds,chunksecs) != 0.0)
00865 {
00866 fprintf(stderr, "ERROR: input parameters seem incompatible (chunksecs must divide totalsecs): totalsecs = %f, chunksecs = %f\n", nseconds, chunksecs);
00867 return 1;
00868 }
00869 ntimechunks=nseconds/chunksecs;
00870
00871 inrecset = drms_open_recordset(drms_env, inrecquery, &status);
00872 if (status != DRMS_SUCCESS || inrecset == NULL)
00873 {
00874 fprintf(stderr, "ERROR: problem opening input recordset: status = %d\n", status);
00875 return 1;
00876 }
00877
00878 int nrecsin = drms_count_records(drms_env, inrecquery, &status);
00879 if (status != DRMS_SUCCESS)
00880 {
00881 fprintf(stderr, "ERROR: problem counting input records: status = %d, nrecs = %d\n", status, nrecsin);
00882 drms_close_records(inrecset, DRMS_FREE_RECORD);
00883 return 1;
00884 }
00885 if (nrecsin == 0)
00886 {
00887 fprintf(stderr, "ERROR: input recordset contains no records. if such was intended use jretile instead.\n");
00888 drms_close_records(inrecset, DRMS_FREE_RECORD);
00889 return 1;
00890 }
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902 if (verbflag)
00903 printf("input recordset opened, nrecs = %d\n", nrecsin);
00904
00905 inrec = drms_recordset_fetchnext(drms_env, inrecset, &fetchstat, &chunkstat, NULL);
00906
00907
00908 char *inchecklist[] = {"T_REC", "QUALITY", "T_OBS", "CRLT_OBS", "CRLN_OBS", "CADENCE",
00909
00910 "CDELT1", "CDELT2"};
00911
00912 DRMS_Keyword_t *inkeytest;
00913 for (itest=0; itest < ARRLENGTH(inchecklist); itest++)
00914 {
00915 inkeytest = hcon_lookup_lower(&inrec->keywords, inchecklist[itest]);
00916 if (inkeytest == NULL)
00917 {
00918 fprintf(stderr, "ERROR: required input keyword %s is missing\n", inchecklist[itest]);
00919 drms_close_records(inrecset, DRMS_FREE_RECORD);
00920 return 1;
00921 }
00922 }
00923
00924 int readrsunref=0;
00925 double rsunref;
00926 inkeytest = hcon_lookup_lower(&inrec->keywords, "RSUN_REF");
00927 if (inkeytest == NULL)
00928 rtrue = RTRUE/AU;
00929 else if (inkeytest->info->recscope == 1)
00930 {
00931 rsunref = drms_getkey_double(inrec, "RSUN_REF", &status);
00932 ParameterDef(status, "RSUN_REF", RTRUE, &rsunref, inrec->recnum, 1);
00933 rtrue=rsunref/AU;
00934 }
00935 else
00936 readrsunref=1;
00937
00938 trec = drms_getkey_time(inrec, "T_REC", &status);
00939 if (status != DRMS_SUCCESS)
00940 {
00941 fprintf(stderr, "ERROR: problem with required parameter T_REC: status = %d, recnum = %lld\n", status, inrec->recnum);
00942 drms_close_records(inrecset, DRMS_FREE_RECORD);
00943 return 1;
00944 }
00945 sprint_time(trecstr, trec, "TAI", 0);
00946
00947 cadence=drms_getkey_float(inrec, "CADENCE", &status);
00948 if (status != DRMS_SUCCESS)
00949 {
00950 fprintf(stderr, "ERROR: problem with required parameter CADENCE: status = %d, T_REC = %s, recnum = %lld\n", status, trecstr, inrec->recnum);
00951 drms_close_records(inrecset, DRMS_FREE_RECORD);
00952 return 1;
00953 }
00954
00955 if (!forceoutput)
00956 {
00957 if (nrecsin != nseconds/cadence)
00958 {
00959 fprintf(stderr, "ERROR: input recordset does not contain a record for every slot.\n");
00960 drms_close_records(inrecset, DRMS_FREE_RECORD);
00961 return 1;
00962 }
00963 }
00964
00965 if (tsflag && cadence != cadence0)
00966 {
00967 fprintf(stderr, "ERROR: input CADENCE does not match output T_STEP: status = %d, T_REC = %s, recnum = %lld\n", status, trecstr, inrec->recnum);
00968 drms_close_records(inrecset, DRMS_FREE_RECORD);
00969 return 1;
00970 }
00971
00972 nrecs=chunksecs/cadence;
00973 maxnsn=nsn=nrecs;
00974 fournsn=4*nsn;
00975
00976 if (verbflag)
00977 printf("ntimechunks = %d, recs per timechunk = %d\n", ntimechunks, nrecs);
00978
00979
00980 if (trec >= tstart + nseconds)
00981 {
00982 fprintf(stderr, "ERROR: no records processed: first input record is after last output record: T_REC = %s\n", trecstr);
00983 drms_close_records(inrecset, DRMS_FREE_RECORD);
00984 return 1;
00985 }
00986
00987 while (trec < tstart && chunkstat != kRecChunking_NoMoreRecs)
00988 {
00989 fprintf(stderr, "WARNING: input record will not be included in output: T_REC = %s, TSTART = %s \n", trecstr, tstartstr);
00990 inrec = drms_recordset_fetchnext(drms_env, inrecset, &fetchstat, &chunkstat, NULL);
00991 if (inrec != NULL)
00992 {
00993 trec = drms_getkey_time(inrec, "T_REC", &status);
00994 sprint_time(trecstr, trec, "TAI", 0);
00995 }
00996 }
00997 if (chunkstat == kRecChunking_NoMoreRecs)
00998 {
00999 fprintf(stderr,"ERROR: no records processed: last input record is before first output record: T_REC = %s\n", trecstr);
01000 drms_close_records(inrecset, DRMS_FREE_RECORD);
01001 return 1;
01002 }
01003
01004 msize = lmax+1;
01005 if (lchunksize == 0) lchunksize = msize;
01006
01007 nlat = maprows/2;
01008 imagesize = maprows*2*msize;
01009
01010 nout = 2 * (lmax + 1);
01011
01012 lfft = 2 * mapped_lmax;
01013 nfft = lfft + 2;
01014 mapcols2 = mapcols/2;
01015
01016
01017
01018
01019
01020
01021
01022 if (nlat % 4)
01023 nlatx=4*(nlat/4+1);
01024 else
01025 nlatx=nlat;
01026
01027
01028 DRMS_RecordSet_t *outrecset, *v2hrecset, *h2mrecset;
01029 if (tsflag)
01030 {
01031 real4evenl = (float *)(malloc (nlatx*maxnsn*sizeof(float)));
01032 real4oddl = (float *)(malloc (nlatx*maxnsn*sizeof(float)));
01033 imag4evenl = (float *)(malloc (nlatx*maxnsn*sizeof(float)));
01034 imag4oddl = (float *)(malloc (nlatx*maxnsn*sizeof(float)));
01035 outx = (float *)(malloc (maxnsn*2*lchunksize*sizeof(float)));
01036
01037 plm = (double *)(malloc ((lmax+1)*nlat*sizeof(double)));
01038 saveplm = (double *)(malloc ((lmax+1)*nlat*2*sizeof(double)));
01039 latgrid = (double *)(malloc (nlat*sizeof(double)));
01040 for (i=0; i<nlat; i++) latgrid[i] = (i+0.5)*sinBdelta;
01041
01042 indx = (int *)(malloc ((lmax+1)*sizeof(int)));
01043 for (l=0; l<=lmax; l++) indx[l]=l;
01044
01045 masks = (float *)(malloc (nlat*lchunksize*sizeof(float)));
01046 foldedsize = 4*nlat*(lmax+1)*maxnsn;
01047 folded = (float *)(malloc (foldedsize*sizeof(float)));
01048 oddpart = (float *)(malloc (nlat*sizeof(float)));
01049 evenpart = (float *)(malloc (nlat*sizeof(float)));
01050
01051 lchunkfirst = lmin/lchunksize;
01052 lchunklast = lmax/lchunksize;
01053 int nlchunks = (lchunklast - lchunkfirst) + 1;
01054 int nrecsout = nlchunks*ntimechunks;
01055 outrecset = drms_create_records(drms_env, nrecsout, outseries, lifetime, &status);
01056 if (status != DRMS_SUCCESS || outrecset == NULL)
01057 {
01058 fprintf(stderr,"ERROR: unable to create records in output dataseries %s, status = %d\n", outseries, status);
01059 drms_close_records(inrecset, DRMS_FREE_RECORD);
01060 return 1;
01061 }
01062
01063 }
01064
01065 if (v2hflag)
01066 {
01067 v2hrecset = drms_create_records(drms_env, nrecsin, v2hout, lifetime, &status);
01068 if (status != DRMS_SUCCESS || v2hrecset == NULL)
01069 {
01070 fprintf(stderr,"ERROR: unable to create records in output dataseries %s, status = %d\n", v2hout, status);
01071 drms_close_records(inrecset, DRMS_FREE_RECORD);
01072 return 1;
01073 }
01074 }
01075
01076 if (h2mflag)
01077 {
01078 h2mrecset = drms_create_records(drms_env, nrecsin, h2mout, lifetime, &status);
01079 if (status != DRMS_SUCCESS || h2mrecset == NULL)
01080 {
01081 fprintf(stderr,"ERROR: unable to create records in output dataseries %s, status = %d\n", h2mout, status);
01082 drms_close_records(inrecset, DRMS_FREE_RECORD);
01083 return 1;
01084 }
01085 }
01086
01087
01088 bad = (int *)(malloc (nrecs*sizeof(int)));
01089 v2hbr = (float *)(malloc(maprows*mapcols*sizeof(float)));
01090 v2hbt = (float *)(malloc(maprows*mapcols*sizeof(float)));
01091 v2hbp = (float *)(malloc(maprows*mapcols*sizeof(float)));
01092 h2mptr = (float *)(malloc(maprows*nout*sizeof(float)));
01093
01094
01095 buf = (float *)malloc(nfft * sizeof(float));
01096
01097 wbuf = (float *)malloc(nfft * sizeof(float));
01098 fftwp = fftwf_plan_r2r_1d(lfft, buf, wbuf, FFTW_R2HC, FFTW_ESTIMATE);
01099
01100
01101 weight = (float *)malloc(nfft * sizeof(float));
01102
01103 wp = weight;
01104 for (col=0; col<mapcols; col++)
01105 {
01106 if (lgapod)
01107 {
01108 lon=abs(col-mapcols2)*360.0/(2*mapped_lmax);
01109 if (lon < lgapmin)
01110 *wp++=1.0;
01111 else if (lon < lgapmax)
01112 *wp++=0.5+0.5*cos(PI*(lon-lgapmin)/lgapwidt);
01113 else
01114 *wp++=0.0;
01115 }
01116 else
01117 *wp++ = 1.0;
01118 }
01119
01120 status=drms_stage_records(inrecset, 1, 0);
01121 if (status != DRMS_SUCCESS)
01122 {
01123 fprintf(stderr, "ERROR: drms_stage_records returned status = %d\n", status);
01124 return 1;
01125 }
01126
01127 unsigned long long calversout, calvers;
01128 int calversunset=1;
01129 unsigned int nybblearrout[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
01130 int fixflagarr[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
01131 for (i=0;i<16;i++)
01132 {
01133 if (getbits(calverkey,i,1))
01134 fixflagarr[i]=1;
01135 }
01136
01137 int mixflag=0;
01138
01139 int nrecords=0;
01140 int nsegments=0;
01141 int error=0;
01142 int nodata=0;
01143 int irecout=0;
01144 int iv2hrec=0;
01145 int ih2mrec=0;
01146 for (iset=0; iset < ntimechunks && chunkstat != kRecChunking_NoMoreRecs; iset++)
01147 {
01148 sprint_time(tstartstr, tstart, "TAI", 0);
01149 if (verbflag)
01150 {
01151 wt1=getwalltime();
01152 ct1=getcputime(&ut1, &st1);
01153 printf("processing timechunk %d, tstart = %s\n", iset, tstartstr);
01154 }
01155
01156 if (trec >= tstart+chunksecs)
01157 {
01158 if (forceoutput)
01159 {
01160 nodata=1;
01161 goto skip_norecs;
01162 }
01163 else
01164 {
01165 fprintf(stderr, "ERROR: no data for timechunk beginning at %s\n", tstartstr);
01166 error++;
01167 tstart+=chunksecs;
01168 continue;
01169 }
01170 }
01171
01172 while (trec < tstart && chunkstat != kRecChunking_NoMoreRecs)
01173 {
01174 inrec = drms_recordset_fetchnext(drms_env, inrecset, &fetchstat, &chunkstat, NULL);
01175 if (inrec != NULL)
01176 {
01177 trec = drms_getkey_time(inrec, "T_REC", &status);
01178 sprint_time(trecstr, trec, "TAI", 0);
01179 }
01180 }
01181
01182 bc=0;
01183 nodata=0;
01184 mixflag=0;
01185 calversunset=1;
01186 trecind=(trec-tstart+cadence/2)/cadence;
01187
01188 for (irec=0; irec < nrecs && chunkstat != kRecChunking_NoMoreRecs; irec++)
01189 {
01190
01191 if (trecind > irec)
01192
01193 {
01194 if (forceoutput)
01195 {
01196 bad[bc++]=irec;
01197 continue;
01198 }
01199 else
01200 {
01201 fprintf(stderr, "ERROR: some input records missing, T_START = %s, T_REC = %s, irec = %d\n", tstartstr, trecstr, irec);
01202 error++;
01203 goto continue_outer_loop;
01204 }
01205 }
01206
01207 while (trecind < irec)
01208
01209 {
01210 if (forceoutput)
01211 {
01212 inrec = drms_recordset_fetchnext(drms_env, inrecset, &fetchstat, &chunkstat, NULL);
01213 if (inrec != NULL)
01214 {
01215 trec = drms_getkey_time(inrec, "T_REC", &status);
01216 sprint_time(trecstr, trec, "TAI", 0);
01217 trecind=(trec-tstart+cadence/2)/cadence;
01218 }
01219 }
01220 else
01221 {
01222 fprintf(stderr, "ERROR: some input records have duplicate T_REC, T_START = %s, T_REC = %s, irec = %d\n", tstartstr, trecstr, irec);
01223 error++;
01224 goto continue_outer_loop;
01225 }
01226 }
01227
01228 if (verbflag > 1)
01229 {
01230 wt2=getwalltime();
01231 ct2=getcputime(&ut2, &st2);
01232 printf(" processing record %d\n", irec);
01233 }
01234
01235 quality=drms_getkey_int(inrec, "QUALITY", &status);
01236 if (status != DRMS_SUCCESS || (quality & QUAL_NODATA))
01237 {
01238 bad[bc++]=irec;
01239 if (verbflag > 2)
01240 fprintf(stderr, "SKIP: image rejected based on quality: T_REC = %s, quality = %08x\n", trecstr, quality);
01241 goto skip;
01242 }
01243
01244 if (tsflag)
01245 {
01246 if (calversunset)
01247 {
01248 calversout=drms_getkey_longlong(inrec, "CALVER64", &status);
01249 if (status != DRMS_SUCCESS)
01250 calversout = 0;
01251 else
01252 calversout = fixcalver64(calversout);
01253 calversunset=0;
01254
01255 for (i=0;i<16;i++)
01256 nybblearrout[i]=getbits(calversout,4*i+3,4);
01257
01258 }
01259
01260 calvers=drms_getkey_longlong(inrec, "CALVER64", &status);
01261 if (status != DRMS_SUCCESS)
01262 calvers = 0;
01263 else
01264 calvers = fixcalver64(calvers);
01265
01266 for (i=0;i<16;i++)
01267 {
01268 int nybble=getbits(calvers,4*i+3,4);
01269 if (fixflagarr[i])
01270 {
01271 if (nybble != nybblearrout[i])
01272 {
01273 fprintf(stderr, "ERROR: input data has mixed values for field %d of CALVER64: %d and %d, recnum = %lld, histrecnum = %lld\n", i, nybblearrout[i], nybble, inrec->recnum, histrecnum);
01274 error++;
01275 goto continue_outer_loop;
01276 }
01277 }
01278 else
01279 {
01280 if (nybble < nybblearrout[i])
01281 nybblearrout[i]=nybble;
01282 }
01283 }
01284
01285 if (!mixflag && calvers != calversout)
01286 mixflag=1;
01287 }
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298 segin = drms_segment_lookup(inrec, "disambig");
01299 if (segin != NULL)
01300 inarrBdisamb = drms_segment_read(segin, usetype, &status);
01301
01302 if (segin == NULL || inarrBdisamb == NULL || status != DRMS_SUCCESS)
01303 {
01304 fprintf(stderr, "ERROR: problem with input segment or array: status = %d, T_REC = %s, recnum = %lld, histrecnum = %lld\n", status, trecstr, inrec->recnum, histrecnum);
01305 return 0;
01306 }
01307 disamb = (float *)inarrBdisamb->data;
01308
01309 segin = drms_segment_lookup(inrec, "field");
01310 if (segin != NULL)
01311 inarrBtotal = drms_segment_read(segin, usetype, &status);
01312
01313 if (segin == NULL || inarrBtotal == NULL || status != DRMS_SUCCESS)
01314 {
01315 fprintf(stderr, "ERROR: problem with input segment or array: status = %d, T_REC = %s, recnum = %lld, histrecnum = %lld\n", status, trecstr, inrec->recnum, histrecnum);
01316 drms_free_array(inarrBdisamb);
01317 return 0;
01318 }
01319 bTotal = (float *)inarrBtotal->data;
01320
01321 segin = drms_segment_lookup(inrec, "inclination");
01322 if (segin != NULL)
01323 inarrBincl = drms_segment_read(segin, usetype, &status);
01324
01325 if (segin == NULL || inarrBincl == NULL || status != DRMS_SUCCESS)
01326 {
01327 fprintf(stderr, "ERROR: problem with input segment or array: status = %d, T_REC = %s, recnum = %lld, histrecnum = %lld\n", status, trecstr, inrec->recnum, histrecnum);
01328 drms_free_array(inarrBdisamb);
01329 drms_free_array(inarrBtotal);
01330 return 0;
01331 }
01332 bIncl = (float *)inarrBincl->data;
01333
01334 segin = drms_segment_lookup(inrec, "azimuth");
01335 if (segin != NULL)
01336 inarrBazim = drms_segment_read(segin, usetype, &status);
01337
01338 if (segin == NULL || inarrBazim == NULL || status != DRMS_SUCCESS)
01339 {
01340 fprintf(stderr, "ERROR: problem with input segment or array: status = %d, T_REC = %s, recnum = %lld, histrecnum = %lld\n", status, trecstr, inrec->recnum, histrecnum);
01341 return 0;
01342 drms_free_array(inarrBdisamb);
01343 drms_free_array(inarrBtotal);
01344 drms_free_array(inarrBincl);
01345 }
01346 bAzim = (float *)inarrBazim->data;
01347
01348
01349
01350 if (maxmissvals > 0)
01351 {
01352 int missvals = drms_getkey_int(inrec, "MISSVALS", &status);
01353 PARAMETER_ERROR("MISSVALS")
01354 if (missvals > maxmissvals)
01355 {
01356 bad[bc++]=irec;
01357 if (verbflag > 1)
01358 fprintf(stderr, "SKIP: %d pixels MISSING, max allowed is %d: T_REC = %s, recnum = %lld\n", missvals, maxmissvals, trecstr, inrec->recnum);
01359 drms_free_array(inarrBazim);
01360 drms_free_array(inarrBdisamb);
01361 drms_free_array(inarrBtotal);
01362 drms_free_array(inarrBincl);
01363 goto skip;
01364 }
01365 }
01366
01367 tobs = drms_getkey_time(inrec, "T_OBS", &status);
01368 PARAMETER_ERROR("T_OBS")
01369
01370
01371 b0 = drms_getkey_double(inrec, "CRLT_OBS", &status);
01372 PARAMETER_ERROR("CRLT_OBS")
01373
01374
01375 obsl0 = drms_getkey_double(inrec, "CRLN_OBS", &status);
01376 PARAMETER_ERROR("CRLN_OBS")
01377
01378 if (p0 == 999.0)
01379 {
01380
01381
01382
01383
01384
01385
01386
01387
01388 double crota = drms_getkey_double(inrec, "CROTA2", &status);
01389 PARAMETER_ERROR("CROTA2")
01390 p=-crota;
01391 }
01392 else
01393 {
01394 p = p0;
01395 }
01396
01397 p = psign * p ;
01398 b0 = b0 + ierr * sin((tobs - trefb0) / 31557600. * 2 * PI);
01399 p = p + perr - ierr * cos((tobs - trefb0) / 31557600. * 2 * PI);
01400
01401
01402 smajor = drms_getkey_double(inrec, "S_MAJOR", &status);
01403 ParameterDef(status, "S_MAJOR", 1.0, &smajor, inrec->recnum, 0);
01404
01405 sminor = drms_getkey_double(inrec, "S_MINOR", &status);
01406 ParameterDef(status, "S_MINOR", 1.0, &sminor, inrec->recnum, 0);
01407
01408 sangle = drms_getkey_double(inrec, "S_ANGLE", &status);
01409 ParameterDef(status, "S_ANGLE", 0.0, &sangle, inrec->recnum, 0);
01410
01411
01412
01413
01414
01415
01416 xscale = drms_getkey_double(inrec, "CDELT1", &status);
01417 PARAMETER_ERROR("CDELT1")
01418 yscale = drms_getkey_double(inrec, "CDELT2", &status);
01419 PARAMETER_ERROR("CDELT2")
01420
01421
01422 if (xscale != yscale)
01423 {
01424 fprintf(stderr, "ERROR: CDELT1 != CDELT2 not supported, CDELT1 = %f, CDELT2 = %f: T_REC = %s, recnum = %lld \n", xscale, yscale, trecstr, inrec->recnum);
01425 drms_free_array(inarrBazim);
01426 drms_free_array(inarrBdisamb);
01427 drms_free_array(inarrBtotal);
01428 drms_free_array(inarrBincl);
01429 error++;
01430 goto continue_outer_loop;
01431 }
01432 imagescale=xscale;
01433 xscale=1.0;
01434 yscale=1.0;
01435
01436
01437
01438
01439
01440
01441 if (paramsign != 0)
01442 {
01443 vsign = paramsign;
01444 }
01445 else
01446 {
01447 vsign = drms_getkey_double(inrec, "DATASIGN", &status);
01448 ParameterDef(status, "DATASIGN", 1.0, &vsign, inrec->recnum, 1);
01449 }
01450
01451 if (velocity_correction)
01452 {
01453 obs_vr = drms_getkey_double(inrec, "OBS_VR", &status);
01454 ParameterDef(status, "OBS_VR", 0.0, &obs_vr, inrec->recnum, 1);
01455
01456 obs_vw = drms_getkey_double(inrec, "OBS_VW", &status);
01457 ParameterDef(status, "OBS_VW", 0.0, &obs_vw, inrec->recnum, 1);
01458
01459 obs_vn = drms_getkey_double(inrec, "OBS_VN", &status);
01460 ParameterDef(status, "OBS_VN", 0.0, &obs_vn, inrec->recnum, 1);
01461 }
01462
01463
01464 obsdist = drms_getkey_double(inrec, "DSUN_OBS", &status) / AU;
01465
01466 ParameterDef(status, "OBS_DIST", 1.0, &obsdist, inrec->recnum, 1);
01467 if (readrsunref)
01468 {
01469 rsunref = drms_getkey_double(inrec, "RSUN_REF", &status);
01470 ParameterDef(status, "RSUN_REF", RTRUE, &rsunref, inrec->recnum, 1);
01471 rtrue=rsunref/AU;
01472 }
01473 S = rtrue / obsdist;
01474
01475 rsunobs = drms_getkey_double(inrec, "RSUN_OBS", &status);
01476 if (status == DRMS_SUCCESS)
01477 rsun = rsunobs/imagescale;
01478 else
01479 {
01480 rsun = drms_getkey_double(inrec, "R_SUN", &status);
01481 if (status != DRMS_SUCCESS)
01482 rsun = ARCSECSINRAD * S / sqrt(1.0 - S * S) / imagescale;
01483 }
01484
01485 if (longitude_shift == 1)
01486 {
01487 tmearth = tobs+TAU_A*(1.0-obsdist);
01488 longshift = (obsl0-refl0)+longrate*(tmearth-tref);
01489 while (longshift > 180.0) longshift-=360.0;
01490 while (longshift < -180.0) longshift+=360.0;
01491 }
01492 else if (longitude_shift == 2)
01493 {
01494 longshift = obsl0 - (int)(obsl0);
01495 if (longshift > 0.5) longshift -= 1.0;
01496 }
01497 else if (longitude_shift == 3)
01498 {
01499 longshift = (obsl0 * 10 - (int)(obsl0 * 10)) / 10;
01500 if (longshift > 0.5) longshift -= 1.0;
01501 }
01502 else
01503 {
01504 longshift = 0.0;
01505 }
01506
01507 mapl0 = obsl0 - longshift;
01508
01509 xpixels = inarrBtotal->axis[0];
01510 ypixels = inarrBtotal->axis[1];
01511 pixels = xpixels * ypixels;
01512
01513
01514 x0 = drms_getkey_double(inrec, "CRPIX1", &status);
01515 ParameterDef(status, "CRPIX1", xpixels / 2, &x0, inrec->recnum, 1);
01516 x0 -= 1.0;
01517
01518
01519 y0 = drms_getkey_double(inrec, "CRPIX2", &status);
01520 ParameterDef(status, "CRPIX2", ypixels / 2, &y0, inrec->recnum, 1);
01521 y0 -= 1.0;
01522
01523 if (mag_offset)
01524 {
01525 float *dat = (float *)inarrBtotal->data;
01526 double bfitzero = drms_getkey_double(inrec, "BFITZERO", &status);
01527 PARAMETER_ERROR("BFITZERO")
01528 int i;
01529
01530 if (!isnan(bfitzero))
01531 {
01532 for (i = 0; i < pixels; ++i)
01533 {
01534 dat[i] -= (float)bfitzero;
01535 }
01536
01537 }
01538 }
01539
01540 if (checko)
01541 {
01542 orientation = drms_getkey_string(inrec, "ORIENT", &status);
01543 PARAMETER_ERROR("ORIENT")
01544 CheckO(orientation, &vstat);
01545 if (vstat != V2HStatus_Success)
01546 {
01547 fprintf(stderr,"ERROR: illegal ORIENT: T_REC = %s, recnum = %lld\n", trecstr, inrec->recnum);
01548 drms_free_array(inarrBazim);
01549 drms_free_array(inarrBdisamb);
01550 drms_free_array(inarrBtotal);
01551 drms_free_array(inarrBincl);
01552 free(orientation);
01553 error++;
01554 goto continue_outer_loop;
01555 }
01556 }
01557 else
01558 {
01559 orientation = strdup(orientationdef);
01560 }
01561
01562
01563
01564
01565 xDim = inarrBtotal->axis[0];
01566 yDim = inarrBtotal->axis[1];
01567 double bxHel, byHel, bzHel;
01568 float *bRadial, *bTheta, *bPhi;
01569 bRadial = (float *)malloc(xDim * yDim * sizeof(float));
01570 bTheta = (float *)malloc(xDim * yDim * sizeof(float));
01571 bPhi = (float *)malloc(xDim * yDim * sizeof(float));
01572
01573 double xx, yy, lon, lat, coslat, sinlat;
01574 double mu, rho, sig, chi;
01575 jy = 0; iData = 0; yOff = 0;
01576 for (jy = 0; jy < yDim; jy++)
01577 {
01578 ix = 0;
01579 yy = (double)jy - y0;
01580 yy /= rsun;
01581 yOff = jy * xDim;
01582
01583 for (ix = 0; ix < xDim; ix++)
01584 {
01585 iData = yOff + ix;
01586 xx = (double)ix - x0;
01587 xx /= rsun;
01588 if (isnan(bTotal[iData]))
01589 {
01590 bRadial[iData] = DRMS_MISSING_FLOAT;
01591 bTheta[iData] = DRMS_MISSING_FLOAT;
01592 bPhi[iData] = DRMS_MISSING_FLOAT;
01593 continue;
01594 }
01595
01596 synop_img2sphere (xx, yy, asin(S), b0 * RADSINDEG, obsl0 * RADSINDEG, p * RADSINDEG, &rho, &lat, &lon,
01597 &sinlat, &coslat, &sig, &mu, &chi);
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608 double bx = 0.0, by = 0.0, bz = 0.0;
01609
01610 if ((int)(disamb[iData]/2)%2 == 1) bAzim[iData] += 180.0;
01611
01612 bx = -bTotal[iData] * sin(bIncl[iData] * RADSINDEG)
01613 * sin(bAzim[iData] * RADSINDEG);
01614 by = bTotal[iData] * sin(bIncl[iData] * RADSINDEG)
01615 * cos(bAzim[iData] * RADSINDEG);
01616 bz = bTotal[iData] * cos(bIncl[iData] * RADSINDEG);
01617
01618
01619
01620 img2helioVector (bx, by, bz, &bxHel, &byHel, &bzHel, lon, lat, obsl0 * RADSINDEG, b0 * RADSINDEG, p * RADSINDEG);
01621 bPhi[iData] = bxHel;
01622 bTheta[iData] = -byHel;
01623 bRadial[iData] = bzHel;
01624 }
01625 }
01626
01627
01628 if (status = obs2helio(bRadial,
01629 v2hbr,
01630 xpixels,
01631 ypixels,
01632 x0,
01633 y0,
01634 b0 * RADSINDEG,
01635 p * RADSINDEG,
01636 S,
01637 rsun,
01638 rmax,
01639 interpolation,
01640 mapcols,
01641 maprows,
01642 longmin_adjusted * RADSINDEG,
01643 longinterval * RADSINDEG,
01644 longshift * RADSINDEG,
01645 sinBdelta,
01646 smajor,
01647 sminor,
01648 sangle * RADSINDEG,
01649 xscale,
01650 yscale,
01651 orientation,
01652 mag_correction,
01653 velocity_correction,
01654 obs_vr,
01655 obs_vw,
01656 obs_vn,
01657 vsign,
01658 NaN_beyond_rmax,
01659 carrStretch,
01660 &distP,
01661 diffrotA,
01662 diffrotB,
01663 diffrotC,
01664 NULL,
01665 0))
01666 {
01667 fprintf(stderr, "ERROR: failure in obs2helio: status = %d, T_REC = %s, recnum = %lld\n", status, trecstr, inrec->recnum);
01668 drms_free_array(inarrBazim);
01669 drms_free_array(inarrBdisamb);
01670 drms_free_array(inarrBtotal);
01671 drms_free_array(inarrBincl);
01672 free(bPhi); free(bTheta); free(bRadial);
01673 free(orientation);
01674 error++;
01675 goto continue_outer_loop;
01676 }
01677
01678 if (status = apodize(v2hbr,
01679 b0 * RADSINDEG,
01680 mapcols,
01681 maprows,
01682 longmin_adjusted * RADSINDEG,
01683 longinterval * RADSINDEG,
01684 sinBdelta,
01685 apodization,
01686 apinner,
01687 apwidth,
01688 apel,
01689 apx,
01690 apy))
01691 {
01692 fprintf(stderr, "ERROR: failure in apodize: status = %d, T_REC = %s, recnum = %lld\n", status, trecstr, inrec->recnum);
01693 error++;
01694 goto continue_outer_loop;
01695 }
01696
01697
01698 if (status = obs2helio(bTheta,
01699 v2hbt,
01700 xpixels,
01701 ypixels,
01702 x0,
01703 y0,
01704 b0 * RADSINDEG,
01705 p * RADSINDEG,
01706 S,
01707 rsun,
01708 rmax,
01709 interpolation,
01710 mapcols,
01711 maprows,
01712 longmin_adjusted * RADSINDEG,
01713 longinterval * RADSINDEG,
01714 longshift * RADSINDEG,
01715 sinBdelta,
01716 smajor,
01717 sminor,
01718 sangle * RADSINDEG,
01719 xscale,
01720 yscale,
01721 orientation,
01722 mag_correction,
01723 velocity_correction,
01724 obs_vr,
01725 obs_vw,
01726 obs_vn,
01727 vsign,
01728 NaN_beyond_rmax,
01729 carrStretch,
01730 &distP,
01731 diffrotA,
01732 diffrotB,
01733 diffrotC,
01734 NULL,
01735 0))
01736 {
01737 fprintf(stderr, "ERROR: failure in obs2helio: status = %d, T_REC = %s, recnum = %lld\n", status, trecstr, inrec->recnum);
01738 drms_free_array(inarrBazim);
01739 drms_free_array(inarrBdisamb);
01740 drms_free_array(inarrBtotal);
01741 drms_free_array(inarrBincl);
01742 free(bPhi); free(bTheta); free(bRadial);
01743 free(orientation);
01744 error++;
01745 goto continue_outer_loop;
01746 }
01747
01748 if (status = apodize(v2hbt,
01749 b0 * RADSINDEG,
01750 mapcols,
01751 maprows,
01752 longmin_adjusted * RADSINDEG,
01753 longinterval * RADSINDEG,
01754 sinBdelta,
01755 apodization,
01756 apinner,
01757 apwidth,
01758 apel,
01759 apx,
01760 apy))
01761 {
01762 fprintf(stderr, "ERROR: failure in apodize: status = %d, T_REC = %s, recnum = %lld\n", status, trecstr, inrec->recnum);
01763 error++;
01764 goto continue_outer_loop;
01765 }
01766
01767 if (status = obs2helio(bPhi,
01768 v2hbp,
01769 xpixels,
01770 ypixels,
01771 x0,
01772 y0,
01773 b0 * RADSINDEG,
01774 p * RADSINDEG,
01775 S,
01776 rsun,
01777 rmax,
01778 interpolation,
01779 mapcols,
01780 maprows,
01781 longmin_adjusted * RADSINDEG,
01782 longinterval * RADSINDEG,
01783 longshift * RADSINDEG,
01784 sinBdelta,
01785 smajor,
01786 sminor,
01787 sangle * RADSINDEG,
01788 xscale,
01789 yscale,
01790 orientation,
01791 mag_correction,
01792 velocity_correction,
01793 obs_vr,
01794 obs_vw,
01795 obs_vn,
01796 vsign,
01797 NaN_beyond_rmax,
01798 carrStretch,
01799 &distP,
01800 diffrotA,
01801 diffrotB,
01802 diffrotC,
01803 NULL,
01804 0))
01805 {
01806 fprintf(stderr, "ERROR: failure in obs2helio: status = %d, T_REC = %s, recnum = %lld\n", status, trecstr, inrec->recnum);
01807 drms_free_array(inarrBazim);
01808 drms_free_array(inarrBdisamb);
01809 drms_free_array(inarrBtotal);
01810 drms_free_array(inarrBincl);
01811 free(bPhi); free(bTheta); free(bRadial);
01812 free(orientation);
01813 error++;
01814 goto continue_outer_loop;
01815 }
01816
01817 drms_free_array(inarrBazim);
01818 drms_free_array(inarrBdisamb);
01819 drms_free_array(inarrBtotal);
01820 drms_free_array(inarrBincl);
01821 free(bPhi); free(bTheta); free(bRadial);
01822 free(orientation);
01823
01824 if (status = apodize(v2hbp,
01825 b0 * RADSINDEG,
01826 mapcols,
01827 maprows,
01828 longmin_adjusted * RADSINDEG,
01829 longinterval * RADSINDEG,
01830 sinBdelta,
01831 apodization,
01832 apinner,
01833 apwidth,
01834 apel,
01835 apx,
01836 apy))
01837 {
01838 fprintf(stderr, "ERROR: failure in apodize: status = %d, T_REC = %s, recnum = %lld\n", status, trecstr, inrec->recnum);
01839 error++;
01840 goto continue_outer_loop;
01841 }
01842
01843 if (v2hflag)
01844 {
01845
01846 outrec=v2hrecset->records[iv2hrec++];
01847 drms_copykeys(outrec, inrec, 0, kDRMS_KeyClass_Explicit);
01848 DRMS_Link_t *histlink = hcon_lookup_lower(&outrec->links, histlinkname);
01849 DRMS_Link_t *srclink = hcon_lookup_lower(&outrec->links, srclinkname);
01850 if (histlink != NULL)
01851 drms_setlink_static(outrec, histlinkname, histrecnum);
01852 if (srclink != NULL)
01853 drms_setlink_static(outrec, srclinkname, inrec->recnum);
01854 if (segoutflag)
01855 segout = drms_segment_lookup(outrec, segnameout);
01856 else
01857 segout = drms_segment_lookupnum(outrec, 0);
01858
01859
01860 int mapcols_out = mapcols * rescale + 0.5;
01861 int maprows_out = maprows * rescale + 0.5;
01862 float *br_out, *bt_out, *bp_out;
01863
01864 if (!(br_out = (float *) malloc(mapcols_out*maprows_out*4))) DIE("MALLOC_FAILED");
01865 if (!(bt_out = (float *) malloc(mapcols_out*maprows_out*4))) DIE("MALLOC_FAILED");
01866 if (!(bp_out = (float *) malloc(mapcols_out*maprows_out*4))) DIE("MALLOC_FAILED");
01867
01868 do_boxcar(v2hbr, br_out, mapcols, maprows, rescale);
01869 do_boxcar(v2hbt, bt_out, mapcols, maprows, rescale);
01870 do_boxcar(v2hbp, bp_out, mapcols, maprows, rescale);
01871
01872 length[0]=mapcols_out;
01873 length[1]=maprows_out;
01874
01875
01876 outarr = drms_array_create(usetype, 2, length, br_out, &status);
01877
01878 drms_setkey_int(outrec, "TOTVALS", maprows_out*mapcols_out);
01879 set_statistics(segout, outarr, 1);
01880 outarr->bzero=segout->bzero;
01881 outarr->bscale=segout->bscale;
01882 status=drms_segment_write(segout, outarr, 0);
01883 free(outarr);
01884
01885 segout = drms_segment_lookupnum(outrec, 1);
01886
01887 outarr = drms_array_create(usetype, 2, length, bt_out, &status);
01888 outarr->bzero=segout->bzero;
01889 outarr->bscale=segout->bscale;
01890 status=drms_segment_write(segout, outarr, 0);
01891 free(outarr);
01892
01893 segout = drms_segment_lookupnum(outrec, 2);
01894
01895 outarr = drms_array_create(usetype, 2, length, bp_out, &status);
01896 outarr->bzero=segout->bzero;
01897 outarr->bscale=segout->bscale;
01898 status=drms_segment_write(segout, outarr, 0);
01899 free(outarr);
01900
01901 if (status != DRMS_SUCCESS)
01902 {
01903 fprintf(stderr, "ERROR: problem writing output segment: status = %d, T_REC = %s, input recnum = %lld, histrecnum = %lld\n", status, trecstr, inrec->recnum, histrecnum);
01904 return 0;
01905 }
01906
01907
01908 drms_setkey_int(outrec, "QUALITY", quality);
01909 drms_setkey_int(outrec, "MAPMMAX", (mapped_lmax - 0.5) * rescale + 0.5);
01910 drms_setkey_int(outrec, "SINBDIVS", (sinb_divisions-0.5) * rescale + 0.5);
01911 drms_setkey_float(outrec, "CRPIX1", mapcols_out/2.0+ 0.5);
01912 drms_setkey_float(outrec, "CRVAL1", mapl0);
01913 drms_setkey_float(outrec, "CROTA1", 0.0);
01914 drms_setkey_float(outrec, "CDELT1", longinterval/rescale);
01915 drms_setkey_float(outrec, "CRPIX2", maprows_out/2.0 + 0.5);
01916 drms_setkey_float(outrec, "CRVAL2", 0.0);
01917 drms_setkey_float(outrec, "CROTA2", 0.0);
01918 drms_setkey_float(outrec, "CDELT2", sinBdelta/rescale);
01919 drms_setkey_string(outrec, "CTYPE1", "CRLN_CEA");
01920 drms_setkey_string(outrec, "CTYPE2", "CRLT_CEA");
01921 drms_setkey_string(outrec, "CUNIT1", "deg");
01922 drms_setkey_string(outrec, "CUNIT2", "sinlat");
01923
01924
01925 drms_setkey_float(outrec, "MAPRMAX", rmax);
01926 drms_setkey_float(outrec, "MAPBMAX", bmax);
01927 drms_setkey_float(outrec, "MAPLGMAX", longmax_adjusted);
01928 drms_setkey_float(outrec, "MAPLGMIN", longmin_adjusted);
01929 drms_setkey_int(outrec, "INTERPO", interpolation);
01930 drms_setkey_int(outrec, "LGSHIFT", longitude_shift);
01931 drms_setkey_int(outrec, "MCORLEV", mag_correction);
01932 drms_setkey_int(outrec, "MOFFSET", mag_offset);
01933 drms_setkey_int(outrec, "CARSTRCH", carrStretch);
01934 drms_setkey_float(outrec, "DIFROT_A", diffrotA);
01935 drms_setkey_float(outrec, "DIFROT_B", diffrotB);
01936 drms_setkey_float(outrec, "DIFROT_C", diffrotC);
01937
01938 dsignout=vsign*drms_getkey_double(inrec, "DATASIGN", &status);
01939 if (status != DRMS_SUCCESS)
01940 dsignout=vsign;
01941 dsignout/=fabs(dsignout);
01942 drms_setkey_int(outrec, "DATASIGN", (int)dsignout);
01943
01944 tnow = (double)time(NULL);
01945 tnow += UNIX_epoch;
01946 drms_setkey_time(outrec, "DATE", tnow);
01947
01948
01949 }
01950
01951 if (verbflag > 1)
01952 {
01953 wt=getwalltime();
01954 ct=getcputime(&ut, &st);
01955 fprintf(stdout,
01956 " remap done, %.2f ms wall time, %.2f ms cpu time\n",
01957 wt-wt2,
01958 ct-ct2);
01959 }
01960
01961 if (!h2mflag && !tsflag)
01962 goto skip;
01963
01964 inp=v2hbr;
01965 outp=h2mptr;
01966
01967 mean = 0.0;
01968 if (subtract_mean)
01969 {
01970 nmean = 0;
01971 ip = inp;
01972 for (row=0; row<maprows; row++)
01973 {
01974 for (col=0; col<mapcols; col++)
01975 {
01976 if (!isnan(*ip))
01977 {
01978 nmean++;
01979 mean += *ip;
01980 }
01981 ip++;
01982 }
01983 }
01984 mean /= (nmean ? nmean : 1);
01985 }
01986
01987 for (row=0; row<maprows; row++)
01988 {
01989
01990 if (cent_long == 0)
01991 {
01992
01993
01994
01995 nok = 0;
01996 bp = buf;
01997 wp = weight;
01998 ip = inp + mapcols * row;
01999 for (col=0; col<mapcols; col++)
02000 {
02001 if (!isnan(*ip))
02002 {
02003 *bp++ = *wp * (*ip - mean);
02004 nok += 1;
02005 }
02006 else
02007 *bp++ = 0.0;
02008 ip++;
02009 wp++;
02010 }
02011
02012
02013 for (i=col; i<nfft; i++)
02014 *bp++ = 0.0;
02015 }
02016 else
02017 {
02018
02019
02020
02021
02022 nok = 0;
02023
02024
02025
02026 bp = buf;
02027 ip = inp + mapcols * row+mapcols2;
02028 wp = weight + mapcols2;
02029 if (subtract_mean)
02030 {
02031 for (col=0; col<=mapcols2; col++)
02032 {
02033 if (!isnan(*ip))
02034 {
02035 *bp++ = *wp * (*ip - mean);
02036 nok += 1;
02037 }
02038 else
02039 *bp++ = 0.0;
02040 ip++;
02041 wp++;
02042 }
02043 }
02044 else
02045 {
02046 for (col=0; col<=mapcols2; col++)
02047 {
02048 if (!isnan(*ip))
02049 {
02050 *bp++ = *wp * *ip;
02051 nok += 1;
02052 }
02053 else
02054 *bp++ = 0.0;
02055 ip++;
02056 wp++;
02057 }
02058 }
02059
02060
02061 bp = buf+lfft-mapcols2;
02062 ip = inp + mapcols * row;
02063 wp = weight;
02064 if (subtract_mean)
02065 {
02066 for (col=0; col<mapcols2; col++)
02067 {
02068 if (!isnan(*ip))
02069 {
02070 *bp++ = *wp * (*ip - mean);
02071 nok += 1;
02072 }
02073 else
02074 *bp++ = 0.0;
02075 ip++;
02076 wp++;
02077 }
02078 }
02079 else
02080 {
02081 for (col=0; col<mapcols2; col++)
02082 {
02083 if (!isnan(*ip))
02084 {
02085 *bp++ = *wp * *ip;
02086 nok += 1;
02087 }
02088 else
02089 *bp++ = 0.0;
02090 ip++;
02091 wp++;
02092 }
02093 }
02094
02095
02096 bp = buf+mapcols2+1;
02097 for (i=0; i<lfft-mapcols; i++)
02098 *bp++ = 0.0;
02099
02100 }
02101
02102 if ((zero_miss == 0) && (nok != mapcols))
02103 {
02104
02105
02106 for (i=0; i<nout; i++)
02107 {
02108 op = outp + i * maprows + row;
02109 *op = DRMS_MISSING_FLOAT;
02110 }
02111 }
02112 else
02113 {
02114 if (normalize)
02115 norm = 1./sqrt(2*(double)nfft/nok)/lfft;
02116 else
02117 norm = 1./lfft;
02118
02119
02120 fftwf_execute(fftwp);
02121
02122
02123
02124 for (i=0; i<nout/2; i++)
02125 {
02126 op = outp + 2*i * maprows + row;
02127 *op = wbuf[i]*norm;
02128 }
02129
02130
02131 *(outp+row+maprows)=0.0;
02132
02133 normx=-norm;
02134 for (i=1; i<nout/2; i++)
02135 {
02136 op = outp + 2*i * maprows + row + maprows;
02137 *op = wbuf[lfft-i]*normx;
02138 }
02139
02140 }
02141 }
02142
02143 if (h2mflag)
02144 {
02145
02146 outrec=h2mrecset->records[ih2mrec++];
02147 drms_copykeys(outrec, inrec, 0, kDRMS_KeyClass_Explicit);
02148 DRMS_Link_t *histlink = hcon_lookup_lower(&outrec->links, histlinkname);
02149 DRMS_Link_t *srclink = hcon_lookup_lower(&outrec->links, srclinkname);
02150 if (histlink != NULL)
02151 drms_setlink_static(outrec, histlinkname, histrecnum);
02152 if (srclink != NULL)
02153 drms_setlink_static(outrec, srclinkname, inrec->recnum);
02154 if (segoutflag)
02155 segout = drms_segment_lookup(outrec, segnameout);
02156 else
02157 segout = drms_segment_lookupnum(outrec, 0);
02158 length[0]=maprows;
02159 length[1]=nout;
02160 outarr = drms_array_create(usetype, 2, length, h2mptr, &status);
02161 drms_setkey_int(outrec, "TOTVALS", maprows*nout);
02162 set_statistics(segout, outarr, 1);
02163 outarr->bzero=segout->bzero;
02164 outarr->bscale=segout->bscale;
02165 status=drms_segment_write(segout, outarr, 0);
02166 free(outarr);
02167
02168 if (status != DRMS_SUCCESS)
02169 {
02170 fprintf(stderr, "ERROR: problem writing output segment: status = %d, T_REC = %s, input recnum = %lld, histrecnum = %lld\n", status, trecstr, inrec->recnum, histrecnum);
02171 return 0;
02172 }
02173
02174
02175 drms_setkey_int(outrec, "QUALITY", quality);
02176 drms_setkey_int(outrec, "MAPMMAX", mapped_lmax);
02177 drms_setkey_int(outrec, "SINBDIVS", sinb_divisions);
02178 drms_setkey_int(outrec, "LMAX", lmax);
02179 drms_setkey_double(outrec, "CRPIX1", maprows/2.0 + 0.5);
02180 drms_setkey_double(outrec, "CRVAL1", 0.0);
02181 drms_setkey_double(outrec, "CROTA1", 0.0);
02182 drms_setkey_double(outrec, "CDELT1", sinBdelta);
02183 drms_setkey_double(outrec, "CRPIX2", 1.0);
02184 drms_setkey_double(outrec, "CRVAL2", 0.0);
02185 drms_setkey_double(outrec, "CROTA2", 0.0);
02186 drms_setkey_double(outrec, "CDELT2", 1.0);
02187 drms_setkey_string(outrec, "CTYPE1", "CRLT_CEA");
02188 drms_setkey_string(outrec, "CTYPE2", "CRLN_FFT");
02189 drms_setkey_string(outrec, "CUNIT1", "rad");
02190 drms_setkey_string(outrec, "CUNIT2", "m");
02191
02192 tnow = (double)time(NULL);
02193 tnow += UNIX_epoch;
02194 drms_setkey_time(outrec, "DATE", tnow);
02195
02196 }
02197
02198 if (verbflag > 1)
02199 {
02200 wt2=getwalltime();
02201 ct2=getcputime(&ut2, &st2);
02202 fprintf(stdout,
02203 " fft and transpose done, %.2f ms wall time, %.2f ms cpu time\n",
02204 wt2-wt,
02205 ct2-ct);
02206 }
02207
02208 if (!tsflag)
02209 goto skip;
02210
02211 inptr=h2mptr;
02212 imageoffset = imagesize * irec;
02213 for (mx = 0; mx < 2*msize; mx++)
02214 {
02215 moffset = mx * maprows;
02216 mptr = inptr + moffset;
02217 for (latx = 0; latx < nlat; latx++)
02218 {
02219 poslatx = nlat + latx; neglatx = nlat - 1 - latx;
02220 evenpart[latx] = mptr[poslatx] + mptr[neglatx];
02221 oddpart[latx] = mptr[poslatx] - mptr[neglatx];
02222 }
02223 workptr = folded + imageoffset + moffset;
02224 scopy_ (&nlat, evenpart, &increment1, workptr, &increment1);
02225 workptr += nlat;
02226 scopy_ (&nlat, oddpart, &increment1, workptr, &increment1);
02227 }
02228
02229 skip:
02230 inrec = drms_recordset_fetchnext(drms_env, inrecset, &fetchstat, &chunkstat, NULL);
02231
02232 if (inrec != NULL)
02233 {
02234 trec = drms_getkey_time(inrec, "T_REC", &status);
02235 PARAMETER_ERROR("T_REC")
02236 trecind=(trec-tstart+cadence/2)/cadence;
02237 sprint_time(trecstr, trec, "TAI", 0);
02238 }
02239
02240 }
02241
02242 if (verbflag)
02243 printf(" number of bad images = %d\n", bc);
02244
02245 if (!tsflag)
02246 goto continue_outer_loop;
02247
02248
02249 while (irec < nrecs)
02250 {
02251 bad[bc++]=irec;
02252 irec++;
02253 }
02254
02255 if (bc == nrecs)
02256 {
02257 nodata=1;
02258 }
02259 else
02260 {
02261 while (bc > 0)
02262 {
02263 imageoffset=imagesize*bad[--bc];
02264 for (i=0;i<imagesize;i++) folded[imageoffset+i]=0.0;
02265 }
02266 }
02267
02268 if (verbflag)
02269 {
02270 wt2=getwalltime();
02271 ct2=getcputime(&ut2, &st2);
02272 fprintf(stdout,
02273 " images processed, %.2f ms wall time, %.2f ms cpu time\n",
02274 wt2-wt1,
02275 ct2-ct1);
02276 }
02277
02278
02279 skip_norecs:
02280
02281
02282
02283
02284
02285 ldone=-1;
02286
02287
02288 for (lchunk = lchunkfirst; lchunk <= lchunklast; lchunk++)
02289 {
02290 lfirst = lchunk * lchunksize;
02291 llast = lfirst + lchunksize - 1;
02292 lfirst = maxval(lfirst,lmin);
02293 llast = minval(llast,lmax);
02294
02295 ifirst = lfirst*(lfirst+1)/2;
02296 ilast = llast*(llast+1)/2+llast;
02297
02298 if (verbflag > 1)
02299 {
02300 wt3=getwalltime();
02301 ct3=getcputime(&ut3, &st3);
02302 printf(" processing lchunk %d, lmin = %d, lmax = %d\n", lchunk, lfirst, llast);
02303 }
02304
02305
02306 outrec=outrecset->records[irecout++];
02307
02308
02309
02310
02311
02312
02313
02314 if (histlink != NULL)
02315 drms_setlink_static(outrec, histlinkname, histrecnum);
02316
02317 if (nodata)
02318 goto skip_nodata;
02319
02320
02321 length[0]=2*nrecs;
02322 length[1]=(ilast-ifirst+1);
02323
02324 outarr = drms_array_create(usetype, 2, length, NULL, &status);
02325
02326 if (segoutflag)
02327 segout = drms_segment_lookup(outrec, segnameout);
02328 else
02329 segout = drms_segment_lookupnum(outrec, 0);
02330
02331 if (segout == NULL || outarr == NULL || status != DRMS_SUCCESS)
02332 {
02333 fprintf(stderr,"ERROR: problem with output segment or data array: lfirst = %d, llast = %d, length = [%d, %d], status = %d, iset = %d, T_START= %s, histrecnum = %lld",
02334 lfirst, llast, length[0], length[1], status, iset, tstartstr, histrecnum);
02335 return 0;
02336 }
02337
02338 outptr = (float *)(outarr->data);
02339
02340
02341 for (m = 0; m <= llast; m++)
02342 {
02343
02344 modd = is_odd(m);
02345 meven = !modd;
02346 lstart = maxval(lfirst,m);
02347
02348
02349
02350 if ((lstart-1) == ldone)
02351 {
02352
02353 if ((lstart - 2) >= m)
02354 for (latx = 0; latx < nlat; latx++)
02355 plm[(lstart-2)*nlat + latx] = saveplm[m*nlat + latx];
02356 if ((lstart - 1) >= m)
02357 for (latx = 0; latx < nlat; latx++)
02358 plm[(lstart-1)*nlat + latx] = saveplm[msize*nlat + m*nlat + latx];
02359
02360
02361
02362 setplm2(lstart, llast, m, nlat, indx, latgrid, nlat, plm, NULL);
02363
02364 }
02365 else
02366 {
02367
02368
02369 setplm2(m, llast, m, nlat, indx, latgrid, nlat, plm, NULL);
02370 }
02371
02372
02373 if ((llast-1) >= m)
02374 for (latx = 0; latx < nlat; latx++)
02375 saveplm[m*nlat + latx] = plm[(llast - 1)*nlat + latx];
02376 for (latx = 0; latx < nlat; latx++)
02377 saveplm[msize*nlat + m*nlat + latx] = plm[llast*nlat + latx];
02378 ldone=llast;
02379
02380
02381
02382
02383
02384
02385
02386
02387
02388
02389
02390
02391
02392
02393
02394
02395
02396
02397
02398 plmptr=plm+nlat*lstart;
02399 maskptr=masks;
02400 latx=nlat*(llast-lstart+1);
02401
02402 int ilatx;
02403 for (ilatx=0;ilatx<latx;ilatx++)
02404 maskptr[ilatx]=plmptr[ilatx];
02405
02406
02407
02408 for (snx=0; snx<nrecs; snx++)
02409 {
02410
02411
02412
02413
02414
02415 scopy_ (&nlat,
02416 folded + nlat*(4*m+modd) + snx*imagesize,
02417 &increment1,
02418 real4evenl + snx*nlatx,
02419 &increment1);
02420 scopy_ (&nlat,
02421 folded + nlat*(4*m+meven) + snx*imagesize,
02422 &increment1,
02423 real4oddl + snx*nlatx,
02424 &increment1);
02425 scopy_ (&nlat,
02426 folded + nlat*(4*m+2+modd) + snx*imagesize,
02427 &increment1,
02428 imag4evenl + snx*nlatx,
02429 &increment1);
02430 scopy_ (&nlat,
02431 folded + nlat*(4*m+2+meven) + snx*imagesize,
02432 &increment1,
02433 imag4oddl + snx*nlatx,
02434 &increment1);
02435 }
02436
02437
02438
02439 lfirsteven = is_even(lstart) ? lstart : lstart+1;
02440 llasteven = is_even(llast) ? llast : llast-1;
02441 nevenl = (llasteven-lfirsteven)/2 + 1;
02442
02443
02444 sgemm_ (transpose,
02445 normal,
02446 &nsn,
02447 &nevenl,
02448 &nlat,
02449 &cnorm,
02450 real4evenl,
02451 &nlatx,
02452 masks + nlat*(lfirsteven-lstart),
02453 &maprows,
02454 &zero,
02455 outx + nsn*2*(lfirsteven-lstart),
02456 &fournsn,
02457 1,
02458 1);
02459
02460 sgemm_ (transpose,
02461 normal,
02462 &nsn,
02463 &nevenl,
02464 &nlat,
02465 &cnorm,
02466 imag4evenl,
02467 &nlatx,
02468 masks + nlat*(lfirsteven-lstart),
02469 &maprows,
02470 &zero,
02471 outx + nsn*(2*(lfirsteven-lstart)+1),
02472 &fournsn,
02473 1,
02474 1);
02475
02476
02477 lfirstodd = is_odd(lstart) ? lstart : lstart+1;
02478 llastodd = is_odd(llast) ? llast : llast-1;
02479 noddl = (llastodd-lfirstodd)/2 + 1;
02480
02481 sgemm_ (transpose,
02482 normal,
02483 &nsn,
02484 &noddl,
02485 &nlat,
02486 &cnorm ,
02487 real4oddl,
02488 &nlatx,
02489 masks + nlat*(lfirstodd-lstart),
02490 &maprows,
02491 &zero,
02492 outx + nsn*2*(lfirstodd-lstart),
02493 &fournsn,
02494 1,
02495 1);
02496
02497 sgemm_ (transpose,
02498 normal,
02499 &nsn,
02500 &noddl,
02501 &nlat,
02502 &cnorm,
02503 imag4oddl,
02504 &nlatx,
02505 masks + nlat*(lfirstodd-lstart),
02506 &maprows,
02507 &zero,
02508 outx + nsn*(2*(lfirstodd-lstart)+1),
02509 &fournsn,
02510 1,
02511 1);
02512
02513
02514
02515 for (l = lstart; l <= llast; l++)
02516 {
02517 fromoffset = 2*nsn*(l-lstart);
02518 tooffset = 2*nsn*(l*(l+1)/2 + m -ifirst);
02519 scopy_ (&nsn,
02520 outx+fromoffset,
02521 &increment1,
02522 outptr+tooffset,
02523 &increment2);
02524 scopy_ (&nsn,
02525 outx+fromoffset+nsn,
02526 &increment1,
02527 outptr+tooffset+1,
02528 &increment2);
02529 }
02530
02531
02532 }
02533
02534
02535 outarr->bzero=segout->bzero;
02536 outarr->bscale=segout->bscale;
02537 status=drms_segment_write(segout, outarr, 0);
02538 drms_free_array(outarr);
02539 nsegments++;
02540
02541 if (status != DRMS_SUCCESS)
02542 {
02543 fprintf(stderr, "ERROR: problem writing output segment: status = %d, T_START = %s, LMIN = %d, LMAX = %d, histrecnum = %lld\n", status, tstartstr, lfirst, llast, histrecnum);
02544 return 0;
02545 }
02546
02547 skip_nodata:
02548
02549 drms_setkey_int(outrec, "LMIN", lfirst);
02550 drms_setkey_int(outrec, "LMAX", llast);
02551 drms_setkey_time(outrec, "T_START", tstart);
02552 drms_setkey_time(outrec, "T_STOP", tstart+chunksecs);
02553 drms_setkey_time(outrec, "T_OBS", tstart+chunksecs/2);
02554 drms_setkey_time(outrec, "DATE__OBS", tstart);
02555 drms_setkey_string(outrec, "TAG", tag);
02556 if (verflag)
02557 drms_setkey_string(outrec, "VERSION", version);
02558
02559 for (i=0;i<16;i++)
02560 setbits(calversout,4*i+3,4,nybblearrout[i]);
02561 drms_setkey_longlong(outrec, "CALVER64", calversout);
02562
02563 if (nodata)
02564 drms_setkey_int(outrec, "QUALITY", QUAL_NODATA);
02565 else if (mixflag)
02566 drms_setkey_int(outrec, "QUALITY", QUAL_MIXEDCALVER);
02567 else
02568 drms_setkey_int(outrec, "QUALITY", 0);
02569
02570
02571 drms_setkey_int(outrec, "MAPMMAX", mapped_lmax);
02572 drms_setkey_int(outrec, "SINBDIVS", sinb_divisions);
02573 drms_setkey_float(outrec, "T_STEP", cadence);
02574
02575 ndt=chunksecs/cadence;
02576 drms_setkey_int(outrec, "NDT", ndt);
02577
02578 tnow = (double)time(NULL);
02579 tnow += UNIX_epoch;
02580 drms_setkey_time(outrec, "DATE", tnow);
02581
02582
02583 nrecords++;
02584
02585 if (verbflag > 1)
02586 {
02587 wt=getwalltime();
02588 ct=getcputime(&ut, &st);
02589 fprintf(stdout,
02590 " %.2f ms wall time, %.2f ms cpu time\n",
02591 wt-wt3,
02592 ct-ct3);
02593 }
02594
02595
02596 }
02597
02598 if (verbflag)
02599 {
02600 wt1=getwalltime();
02601 ct1=getcputime(&ut1, &st1);
02602 fprintf(stdout, "SHT of timechunk %d complete: %.2f ms wall time, %.2f ms cpu time\n", iset,
02603 wt1-wt2, ct1-ct2);
02604 }
02605
02606
02607 continue_outer_loop:
02608 tstart+=chunksecs;
02609 }
02610
02611
02612 if (chunkstat != kRecChunking_LastInRS && chunkstat != kRecChunking_NoMoreRecs)
02613 fprintf(stderr, "WARNING: input records remain after last output record: chunkstat = %d\n", (int)chunkstat);
02614
02615 drms_close_records(inrecset, DRMS_FREE_RECORD);
02616 if (tsflag)
02617 drms_close_records(outrecset, DRMS_INSERT_RECORD);
02618 if (v2hflag)
02619 drms_close_records(v2hrecset, DRMS_INSERT_RECORD);
02620 if (h2mflag)
02621 drms_close_records(h2mrecset, DRMS_INSERT_RECORD);
02622
02623
02624 wt=getwalltime();
02625 ct=getcputime(&ut, &st);
02626 if (verbflag && tsflag)
02627 {
02628 printf("number of records created = %d\n", nrecords);
02629 printf("number of segments created = %d\n", nsegments);
02630 }
02631 if (verbflag)
02632 {
02633 fprintf(stdout, "total time spent: %.2f ms wall time, %.2f ms cpu time\n",
02634 wt-wt0, ct-ct0);
02635 }
02636
02637 if (!error)
02638 printf("module %s successful completion\n", cmdparams.argv[0]);
02639 else
02640 printf("module %s failed to produce %d timechunks: histrecnum = %lld\n", cmdparams.argv[0], error, histrecnum);
02641
02642 return 0;
02643 }
02644
02645 void do_boxcar(float *image_in, float *image_out, int in_nx, int in_ny, float fscale)
02646 {
02647 int iscale, nvector, vec_half;
02648 int inx, iny, outx, outy, i, j;
02649 float val;
02650
02651 iscale = 1.0/fscale + 0.5;
02652 nvector = iscale;
02653 vec_half = nvector/2;
02654
02655 int in_go = (iscale-1)/2.0 + 0.5;
02656 int out_nx = in_nx * fscale + 0.5;
02657 int out_ny = in_ny * fscale + 0.5;
02658
02659 for (outy = 0; outy < out_ny; outy += 1)
02660 for (outx = 0; outx < out_nx; outx += 1)
02661 {
02662 double total = 0.0;
02663 double weight = 0.0;
02664 int nn = 0;
02665 for (j = 0; j < nvector; j += 1)
02666 {
02667 iny = outy*iscale + in_go + j - vec_half;
02668 for (i = 0; i < nvector; i += 1)
02669 {
02670 inx = outx*iscale + in_go + i - vec_half;
02671 if (inx >= 0 && inx < in_nx && iny >=0 && iny < in_ny)
02672 {
02673 val = image_in[in_nx*(iny) + inx];
02674 if (!drms_ismissing_float(val))
02675 {
02676 double w = 1.0;
02677 total += w*val;
02678 weight += w;
02679 nn++;
02680 }
02681 }
02682 }
02683 }
02684 image_out[out_nx*outy + outx] = (nn > 0 ? total/weight : DRMS_MISSING_FLOAT);
02685 }
02686
02687 }
02688