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