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