00001
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <time.h>
00005 #include <math.h>
00006 #include <sys/time.h>
00007 #include <sys/resource.h>
00008 #include <limits.h>
00009 #include <sys/types.h>
00010 #include <sys/stat.h>
00011 #include "jsoc_main.h"
00012 #include "astro.h"
00013 #include "drms_dsdsapi.h"
00014 #include "fstats.h"
00015
00016
00017 #include "/home/wso/src/libastro.d/solephem.c"
00018
00019 #define C1 0.0757644
00020 #define C2 92353.9357
00021 #define RADSINDEG (PI/180)
00022
00023
00024 #define kRecSetIn "in"
00025 #define kSeriesOut "out"
00026 #define kSegIn "segin"
00027 #define kSegOut "segout"
00028 #define kNOTSPECIFIED "not specified"
00029
00030 #define EQPOINTS "EQPOINTS"
00031 #define HALFWIN "HALFWIN"
00032 #define LOSFLAG "l"
00033 #define FORCEFLAG "f"
00034 #define DLOGFLAG "d"
00035 #define NOISESIGMA "NOISESIGMA"
00036 #define MAXNOISEADJ "MAXNOISEADJ"
00037
00038
00039 #define MINOUTLIERPTS "MINOUTLIERPTS"
00040
00041
00042
00043 #define DPC_OBSR "DPC_OBSR"
00044 #define FD_Magnetogram_Sum "FD_Magnetogram_Sum"
00045 #define FD_Magnetogram "FD_Magnetogram"
00046 #define HWNWIDTH "HWNWIDTH"
00047
00048
00049 #define kCARSTRCH "CARSTRCH"
00050 #define kDIFROT_A "DIFROT_A"
00051 #define kDIFROT_B "DIFROT_B"
00052 #define kDIFROT_C "DIFROT_C"
00053 #define CR "CR"
00054 #define NBIN "NBIN"
00055 #define QUAL_CHECK (0xfffefb00)
00056
00057 const float kNOISE_EQ = 10.0;
00058 char *module_name = "mrmlossynoptic";
00059
00060 #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s, status=%d\n", msg, status); return(status);}
00061
00062
00063
00064
00065
00066
00067
00068 typedef struct MagCol_struct
00069 {
00070 float dist;
00071 float *datacol;
00072 float equivPts;
00073 int ds;
00074
00075 int col;
00076 char valid;
00077 } MagCol_t;
00078
00079 int CalcSynCols(int start,
00080 int end,
00081 int incr,
00082 MagCol_t **smc,
00083 float *synop,
00084 int *wt,
00085 char *ww,
00086 float *epts,
00087 float *losSynop,
00088 int *len,
00089 float center,
00090 float nEquivPtsReq,
00091 int los,
00092 int radialFound,
00093 float sensAdj,
00094 float noiseS,
00095 float nsig,
00096 float maxNoiseAdj,
00097 int minOutPts,
00098 int dlog);
00099
00100 int magStats(float **val, int npts, double sum, int outThreshold, double *avg, float *med);
00101 void SortMagCols(MagCol_t *mc, int nelem);
00102 void FreeMagColsData(int start, int end, int incr, int *n, MagCol_t **mc);
00103 void m_sort(MagCol_t *mc, MagCol_t *mc_cpy, int sz);
00104 void frebinbox(float *image_in, float *image_out, int nx, int ny, int nbinx, int nbiny);
00105 TIME CarringtonTime(int crot, double L);
00106
00107 ModuleArgs_t module_args[] =
00108 {
00109 {ARG_STRING, kRecSetIn, "", "Input data records."},
00110 {ARG_STRING, kSeriesOut, "", "Output data series."},
00111 {ARG_STRING, kSegIn, kNOTSPECIFIED, ""},
00112 {ARG_STRING, kSegOut, kNOTSPECIFIED, ""},
00113 {ARG_STRING, "outMl", "NOTSPECIFIED", ""},
00114 {ARG_STRING, "smallMr", "NOTSPECIFIED", ""},
00115 {ARG_STRING, "smallMl", "NOTSPECIFIED", ""},
00116 {ARG_INT, CR, "", "", ""},
00117 {ARG_FLOAT, "nsig", "3.0", "", ""},
00118 {ARG_INT, "MAPMMAX", "1800", "", ""},
00119 {ARG_INT, "SINBDIVS", "720", "", ""},
00120 {ARG_FLOAT, "MAPLGMAX", "90.0", "", ""},
00121 {ARG_FLOAT, "MAPLGMIN", "-90.0", "", ""},
00122 {ARG_FLOAT, EQPOINTS, "20.0", "", ""},
00123 {ARG_FLOAT, HALFWIN, "15.0", "", ""},
00124 {ARG_FLOAT, "center", "0.0", "", ""},
00125 {ARG_INT, "checkqual", "0", "", ""},
00126 {ARG_STRING, "qualmask", "0x0", ""},
00127 {ARG_FLAG, LOSFLAG, "0", "-1", "1"},
00128 {ARG_FLAG, FORCEFLAG, "0", "-1", "1"},
00129 {ARG_FLAG, DLOGFLAG, "0", "-1", "1"},
00130 {ARG_FLOAT, NOISESIGMA, "3.0", "", ""},
00131 {ARG_FLOAT, MAXNOISEADJ, "3.0", "", ""},
00132 {ARG_INT, MINOUTLIERPTS, "4", "", ""},
00133 {ARG_INT, NBIN, "5", "", ""},
00134 {ARG_END}
00135 };
00136
00137 int DoIt(void)
00138 {
00139
00140 int newstat = 0;
00141 int status = DRMS_SUCCESS;
00142 float *synopbr, *synopblos;
00143 int *wt;
00144 char *ww;
00145 float *epts = NULL;
00146 float *losSynop = NULL;
00147 float *ps, *pm;
00148 char *odir;
00149 struct stat stbuf;
00150 float nsig;
00151 int cr, mapmmax, sinbdivs;
00152 int checkqual;
00153 unsigned int quality, qualvld, qualmask;
00154 float lgmax, lgmin;
00155 float center;
00156 float halfWindow;
00157 int len[2];
00158 int ngood, idx;
00159 int started, ended;
00160 int firstidx, lastidx;
00161 int mapmidcol, synmidcol;
00162 int cols2right, cols2left;
00163 int mrc, mlc, src, slc;
00164 int mapcols, col, row, syncol;
00165 double maprightct, mapleftct;
00166 double synleftct, synrightct;
00167 double synstart;
00168 double synend;
00169 double synstep;
00170 double subSynopStart, subSynopEnd, synRange;
00171 int SyncolStart, SyncolEnd, nsynop;
00172 char tstr[64];
00173 int mag_num = 0;
00174 TIME tstart, tstop, trot, tearth;
00175 TIME tdiff, magtime;
00176 double r,b,l,vr,vn,vw;
00177 int y,m,d;
00178 TIME tmod, delta_T = 0.0;
00179 double bearth;
00180 double carrtime;
00181 char dsname[50400], key[50400], logfn[50400], synfn[50400], wtfn[50400], losFn[50400];
00182 char eptsfn[50400];
00183 int ds, nds, noutRec;
00184 int fsn, lsn;
00185 int i,j;
00186 double statMin, statMax, statMedn, statMean, statSig, statSkew, statKurt;
00187 double smallstatMin, smallstatMax, smallstatMedn, smallstatMean, smallstatSig, smallstatSkew, smallstatKurt;
00188 int statNgood;
00189 int smallstatNgood;
00190 long long calVer;
00191 double eph[30];
00192 char historyofthemodule[2048];
00193 char *cvsinfo = strdup("$Id: mrmlossynoptic.c,v 1.3 2016/11/16 21:58:08 yliu Exp $");
00194 cvsinfo = (char *)malloc(2048 * sizeof(char));
00195 sprintf(historyofthemodule,"Carrington-Time conversion corrected; o2helio.c bug corrected -- July 2013");
00196
00197 struct {
00198 int recno;
00199 double mapct;
00200 double mapCM;
00201 float mapdev;
00202 int ds;
00203 float tmin;
00204 float tmax;
00205 TIME tobs;
00206 } imrec[50400];
00207
00208 int nStackMags;
00209 float nEquivPtsReq;
00210 MagCol_t **sortedMagCol = NULL;
00211
00212 long long modVers = soi_vers_num;
00213 long long imgVers = 0;
00214 int radialFound = 0;
00215 int losFound = 0;
00216 int carrStretch = 1;
00217 float diffrotA = DRMS_MISSING_FLOAT;
00218 float diffrotB = DRMS_MISSING_FLOAT;
00219 float diffrotC = DRMS_MISSING_FLOAT;
00220
00221 const char *synopFileName = NULL;
00222 const char *wtFileName = NULL;
00223 const char *eptsFileName = NULL;
00224
00225 int los;
00226 int force;
00227 int dlog;
00228 float noiseS = cmdparams_get_float(&cmdparams, NOISESIGMA, &status);
00229 float maxNoiseAdj = cmdparams_get_float(&cmdparams, MAXNOISEADJ, &status);
00230
00231
00232 int minOutPts = cmdparams_get_int(&cmdparams, MINOUTLIERPTS, &status);
00233
00234
00235
00236 int sensAdjDone = -1;
00237 float sensAdj;
00238
00239 int nxtSyncol = -1;
00240 int calcsynret = 0;
00241 int nbin = 5;
00242 delta_T = sscan_time("1977.01.01_00:00:00_TAI") - sscan_time("1601.01.01_00:00:00_UT");
00243
00244
00245
00246
00247
00248 cr = cmdparams_get_int(&cmdparams, CR, &status);
00249 nsig = cmdparams_get_float(&cmdparams, "nsig", &status);
00250 mapmmax = cmdparams_get_int(&cmdparams, "MAPMMAX", &status);
00251 sinbdivs = cmdparams_get_int(&cmdparams, "SINBDIVS", &status);
00252 checkqual = cmdparams_get_int(&cmdparams, "checkqual", &status);
00253 nbin = cmdparams_get_int(&cmdparams, "NBIN", &status);
00254 qualmask = strtoul(cmdparams_get_str(&cmdparams, "qualmask", &status), (char **) 0, 0);
00255 center = cmdparams_get_float(&cmdparams, "center", &status);
00256 halfWindow = cmdparams_get_float(&cmdparams, HALFWIN, &status);
00257 lgmax = cmdparams_get_float(&cmdparams, "MAPLGMAX", &status);
00258 lgmin = cmdparams_get_float(&cmdparams, "MAPLGMIN", &status);
00259 firstidx = 0;
00260
00261 if (lgmax <= lgmin)
00262 {
00263 printf("MAPLGMAX must be greater than MAPLGMIN\n");
00264 DIE("MAKES_NO_SENSE");
00265 }
00266
00267 nEquivPtsReq = cmdparams_get_float(&cmdparams, EQPOINTS, &status);
00268
00269
00270 len[0] = rint(360.0 / (lgmax - lgmin) * mapmmax);
00271 len[1] = 2 * sinbdivs;
00272
00273 printf("%d, %d\n", len[0], len[1]);
00274
00275 synstep = 360.0 / len[0];
00276 synstart = (cr - 1) * 360.0 + 0.0 * synstep;
00277 synend = cr * 360.0 - 1.0 * synstep;
00278
00279 nStackMags = rint(2 * halfWindow * 0.5);
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 tstart = CarringtonTime(cr, 360.0);
00296 tstop = CarringtonTime(cr, 0.0);
00297 trot = CarringtonTime(cr, 180.0);
00298
00299
00300
00301
00302
00303
00304
00305 los = (cmdparams_isflagset(&cmdparams, LOSFLAG) != 0);
00306 force = (cmdparams_isflagset(&cmdparams, FORCEFLAG) != 0);
00307 dlog = (cmdparams_isflagset(&cmdparams, DLOGFLAG) != 0);
00308
00309 printf("\n######### FIRST PASS #################\n");
00310
00311
00312
00313
00314 if (modVers < 0) modVers = -modVers;
00315 char *inRecQuery, *outRecQuery, *outMlRecQuery, *smallMrRecQuery, *smallMlRecQuery;
00316 inRecQuery = (char *)cmdparams_get_str(&cmdparams, kRecSetIn, &status);
00317 outRecQuery = (char *)cmdparams_get_str(&cmdparams, kSeriesOut, &status);
00318 outMlRecQuery = (char *)cmdparams_get_str(&cmdparams, "outMl", &status);
00319 smallMrRecQuery = (char *)cmdparams_get_str(&cmdparams, "smallMr", &status);
00320 smallMlRecQuery = (char *)cmdparams_get_str(&cmdparams, "smallMl", &status);
00321
00322 DRMS_RecordSet_t *inRD, *outRD, *outMlRD, *smallMrRD, *smallMlRD;
00323 DRMS_Record_t *outRec, *outMlRec, *smallMrRec, *smallMlRec;
00324
00325 inRD = drms_open_records(drms_env, inRecQuery, &status);
00326 if (status || inRD->n == 0)
00327 DIE("No input data found");
00328 nds = inRD->n;
00329 idx = 0;
00330 for (ds=0; ds < nds; ds++)
00331 {
00332 double clong;
00333 double cmLong;
00334 double mapct = 0.0;
00335 double mapctnew;
00336 double mapCM = 0.0;
00337 double mapCMnew;
00338 double remapLgmax = 0.0;
00339 double remapLgmin = 0.0;
00340 int orot;
00341 char t_obs[64];
00342 char *imgVersStr = NULL;
00343 int rkey = 0;
00344 int csKey = 0;
00345 float csCoeffKey;
00346 int sensAdjKey;
00347 TIME trec;
00348 char trecstr[100];
00349 DRMS_Record_t *inRec;
00350 inRec = inRD->records[ds];
00351
00352 calVer = drms_getkey_longlong(inRec, "CALVER64", &status);
00353 trec = drms_getkey_time(inRec, "T_REC", &status);
00354 sprint_time(trecstr, trec, "TAI", 0);
00355
00356
00357
00358 quality = drms_getkey_int(inRec, "QUALITY", &status);
00359
00360 if (quality & QUAL_CHECK)
00361 {
00362 printf("SKIP: Bad QUALITY = 0x%08x; T_REC=%s, rejected iRec = %d\n", quality, trecstr, ds);
00363 continue;
00364 }
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 rkey = drms_getkey_int(inRec, "FDRADIAL", &status);
00425 if (rkey > 0)
00426 {
00427 printf(" Found radial keyword %d, ds=%d.\n", rkey, ds);
00428 if (losFound)
00429 {
00430 printf(" Attempt to use a mixture of radial and line-of-sight images.\n");
00431 printf(" Rejecting ds=%d.\n", ds);
00432 continue;
00433 }
00434 else
00435 {
00436 radialFound = 1;
00437 }
00438 }
00439 else
00440 {
00441 if (radialFound)
00442 {
00443 printf(" Attempt to use a mixture of radial and line-of-sight images.\n");
00444 printf(" Rejecting ds=%d.\n", ds);
00445 continue;
00446 }
00447 else
00448 {
00449 losFound = 1;
00450 }
00451 }
00452
00453 csKey = drms_getkey_int(inRec, kCARSTRCH, &status);
00454 csKey = (drms_ismissing_int(csKey)) ? 0 : csKey;
00455
00456 if (idx == 0)
00457 {
00458 carrStretch = csKey;
00459 }
00460 else
00461 {
00462 if (csKey != carrStretch)
00463 {
00464 printf(" Attempt to mix carr-streched and non-carr-stretched images.\n");
00465 printf(" Rejecting ds=%d.\n", ds);
00466 continue;
00467 }
00468 }
00469
00470 if (carrStretch > 0)
00471 {
00472 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_A, &status);
00473 if (idx == 0)
00474 {
00475 diffrotA = (float)csCoeffKey;
00476 }
00477 else
00478 {
00479 if (fabsf(csCoeffKey - diffrotA) > 0.001)
00480 {
00481 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00482 printf(" Rejecting ds=%d.\n", ds);
00483 continue;
00484 }
00485 }
00486
00487 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_B, &status);
00488 if (idx == 0)
00489 {
00490 diffrotB = (float)csCoeffKey;
00491 }
00492 else
00493 {
00494 if (fabsf(csCoeffKey - diffrotB) > 0.001)
00495 {
00496 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00497 printf(" Rejecting ds=%d.\n", ds);
00498 continue;
00499 }
00500 }
00501
00502 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_C, &status);
00503 if (idx == 0)
00504 {
00505 diffrotC = (float)csCoeffKey;
00506 }
00507 else
00508 {
00509 if (fabsf(csCoeffKey - diffrotC) > 0.001)
00510 {
00511 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00512 printf(" Rejecting ds=%d.\n", ds);
00513 continue;
00514 }
00515 }
00516 }
00517
00518 clong = drms_getkey_double(inRec, "CRVAL1", &status);
00519 cmLong = drms_getkey_double(inRec, "CRLN_OBS", &status);
00520 orot = drms_getkey_int(inRec, "CAR_ROT", &status);
00521 if (drms_ismissing_float(clong) || drms_ismissing_int(orot))
00522 {
00523 printf(" Bad MAP_L0 or OBS_CR in header; skipped");
00524 continue;
00525 }
00526
00527 mapctnew = (double)(orot) * 360.0 - clong - center;
00528 mapCMnew = (double)(orot) * 360.0 - cmLong - center;
00529 if (mapCMnew < mapCM)
00530 {
00531 printf(" Source image (ds=%d) has a smaller CT than previous img; skipped.\n", ds);
00532 continue;
00533 }
00534 else
00535 {
00536 mapct = mapctnew;
00537 mapCM = mapCMnew;
00538 }
00539
00540 remapLgmax = drms_getkey_double(inRec, "MAPLGMAX", &status);
00541 remapLgmin = drms_getkey_double(inRec, "MAPLGMIN", &status);
00542 imrec[idx].mapdev = (remapLgmax - remapLgmin) / 2.0;
00543 imrec[idx].recno = drms_getkey_int(inRec, "I_DREC", &status);
00544 imrec[idx].mapct = mapct;
00545 imrec[idx].mapCM = mapCM;
00546 imrec[idx].ds = ds;
00547 strcpy(t_obs, drms_getkey_string(inRec, "T_REC", &status));
00548 imrec[idx].tobs = sscan_time(t_obs);
00549 imrec[idx].tmin = MAX(mapCM - halfWindow, mapct + center - imrec[idx].mapdev);
00550 imrec[idx].tmax = MIN(mapCM + halfWindow, mapct + center + imrec[idx].mapdev);
00551 ++idx;
00552
00553 }
00554 ngood = idx;
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569 printf("\n######### SECOND PASS #################\n");
00570
00571 if (!(wt = (int *) calloc(len[0],4)))
00572 DIE("MALLOC_FAILED");
00573 if (!(ww = (char *) malloc(len[0]*len[1]*2)))
00574 DIE("MALLOC_FAILED");
00575 if (!(epts = (float *) malloc(len[0] * len[1] * sizeof(float))))
00576 DIE("MALLOC_FAILED");
00577 if (!(synopbr = (float *) malloc(len[0]*len[1]*4)))
00578 DIE("MALLOC_FAILED");
00579 if (!(synopblos = (float *) malloc(len[0]*len[1]*4)))
00580 DIE("MALLOC_FAILED");
00581 if (los && radialFound && !(losSynop = (float *)malloc(len[0] * len[1] * sizeof(float))))
00582 DIE("MALLOC_FAILED");
00583 sortedMagCol = (MagCol_t **)malloc(sizeof(MagCol_t *) * len[0]);
00584 if (!sortedMagCol)
00585 DIE("MALLOC_FAILED");
00586 memset(sortedMagCol, 0, sizeof(MagCol_t *) * len[0]);
00587
00588
00589 started = 0;
00590 ended = 0;
00591 nxtSyncol = len[0] - 1;
00592 synRange = 60.0 ;
00593
00594
00595 nsynop = rint(360.0/synRange);
00596
00597
00598
00599
00600
00601 for (ds = 0; ds < nsynop; ds++)
00602 {
00603 subSynopStart = synstart + ds * synRange;
00604 subSynopEnd = synstart + (ds + 1) * synRange - synstep;
00605 SyncolStart = rint((synend - subSynopStart) / synstep);
00606 SyncolEnd = rint((synend - subSynopEnd) / synstep);
00607 printf("ds=%d\n", ds);
00608
00609 for (idx = 0; idx < ngood; idx++)
00610 {
00611 int recno;
00612 int col;
00613 float magtype = 0.0;
00614 char *mType = NULL;
00615 float equivPts = 0.0;
00616 char t_mag[64];
00617 TIME tmag;
00618 DRMS_Record_t *inRec;
00619 DRMS_Segment_t *inSeg = NULL;
00620 DRMS_Array_t *inArray;
00621 inRec = inRD->records[imrec[idx].ds];
00622
00623 if (imrec[idx].tmax <= subSynopStart || imrec[idx].tmin >= subSynopEnd) continue;
00624
00625
00626
00627
00628 maprightct = MAX(subSynopStart, imrec[idx].tmin);
00629 mapleftct = MIN(subSynopEnd, imrec[idx].tmax);
00630 mapcols = drms_getkey_int(inRec, "MAPMMAX", &status) + 1;
00631 mapmidcol = rint((mapcols - 1.0) / 2 + center / synstep);
00632 cols2right = rint((imrec[idx].mapct - maprightct) / synstep);
00633 cols2left = rint((imrec[idx].mapct - mapleftct) / synstep);
00634 synmidcol = rint((synend - imrec[idx].mapct) / synstep);
00635
00636
00637
00638
00639
00640 mrc = mapmidcol + cols2right;
00641 mlc = mapmidcol + cols2left;
00642
00643
00644 src = synmidcol + cols2right;
00645 slc = synmidcol + cols2left;
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658 equivPts = 1.0;
00659 inSeg = drms_segment_lookupnum(inRec, 0);
00660 inArray = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00661
00662 if (status)
00663 {
00664 printf(" No data file found, status=%d\n", status);
00665 drms_free_array(inArray);
00666 continue;
00667 }
00668
00669 for (col = mlc; col <= mrc; ++col)
00670 {
00671 MagCol_t mMagCol;
00672
00673 mMagCol.dist = ((col - mapmidcol) * synstep + imrec[idx].mapct) - imrec[idx].mapCM;
00674 mMagCol.datacol = (float *)malloc(sizeof(float) * len[1]);
00675 mMagCol.equivPts = equivPts;
00676 mMagCol.ds = imrec[idx].ds;
00677 mMagCol.col = col;
00678 mMagCol.valid = 1;
00679
00680 pm = (float *)inArray->data + col;
00681 ps = mMagCol.datacol;
00682
00683 for (row = 0; row < len[1]; ++row)
00684 {
00685 *ps = *pm;
00686 pm += mapcols;
00687 ps++;
00688 }
00689
00690 syncol = col + slc - mlc;
00691
00692 if (!sortedMagCol[syncol])
00693 {
00694 sortedMagCol[syncol] = (MagCol_t *)malloc(sizeof(MagCol_t) * nStackMags);
00695 if (!sortedMagCol[syncol]) DIE("MALLOC_FAILED");
00696 memset(sortedMagCol[syncol], 0, sizeof(MagCol_t) * nStackMags);
00697 }
00698
00699 if (wt[syncol] > 0 && (wt[syncol] % nStackMags == 0))
00700 {
00701 int frames = 1 + (wt[syncol] / nStackMags);
00702 MagCol_t *mc = (MagCol_t *)malloc(sizeof(MagCol_t) * frames * nStackMags);
00703 memcpy(mc,
00704 sortedMagCol[syncol],
00705 sizeof(MagCol_t) * (frames - 1) * nStackMags);
00706 free(sortedMagCol[syncol]);
00707 sortedMagCol[syncol] = mc;
00708 }
00709
00710 sortedMagCol[syncol][wt[syncol]] = mMagCol;
00711 wt[syncol]++;
00712
00713 }
00714 drms_free_array(inArray);
00715 }
00716
00717 calcsynret = CalcSynCols(SyncolStart,
00718 SyncolEnd,
00719 -1,
00720 sortedMagCol,
00721 synopbr,
00722 wt,
00723 ww,
00724 epts,
00725 losSynop,
00726 len,
00727 center,
00728 nEquivPtsReq,
00729 los,
00730 radialFound,
00731 sensAdj,
00732 noiseS,
00733 nsig,
00734 maxNoiseAdj,
00735 minOutPts,
00736 dlog);
00737
00738 printf("ds=%d\n", ds);
00739 FreeMagColsData(SyncolStart, SyncolEnd, -1, wt, sortedMagCol);
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753 if (calcsynret) DIE(" Cannot be finished up!");
00754 }
00755
00756 if (!ended) lastidx = ngood-1;
00757 magtime = imrec[(int)(ngood/2)].tobs;
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773 float *smallSynopbr = NULL, *smallSynopblos = NULL, *smallEpts = NULL;
00774 int smallDims[2], xout, yout;
00775
00776 xout = len[0]/nbin; yout = len[1]/(nbin-1);
00777 if (!(smallSynopbr = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00778 if (!(smallSynopblos = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00779 if (!(smallEpts = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00780 smallDims[0] = xout; smallDims[1] = yout;
00781 frebinbox(synopbr, smallSynopbr, len[0], len[1], nbin, nbin-1);
00782 frebinbox(epts, smallEpts, len[0], len[1], nbin, nbin-1);
00783
00784
00785 int icol, jrow, rowoffset;
00786 float sinB, sinB2, cosB, sinBbase;
00787 double sinBdelta;
00788
00789
00790 sinBbase = 0.5 - len[1]/2.0;
00791 sinBdelta = 2.0/len[1];
00792
00793 for (jrow = 0; jrow < len[1]; jrow++) {
00794 rowoffset = jrow * len[0];
00795 sinB = sinBdelta * (sinBbase + jrow);
00796 sinB2 = sinB * sinB;
00797 cosB = sqrt(1.0 - sinB2);
00798
00799 for (icol = 0; icol < len[0]; icol++) {
00800 if (isnan(synopbr[rowoffset + icol]))
00801 {
00802 synopblos[rowoffset + icol] = DRMS_MISSING_FLOAT;
00803 continue;
00804 }
00805 synopblos[rowoffset + icol] = synopbr[rowoffset + icol] * cosB;
00806 }
00807 }
00808
00809
00810
00811 sinBbase = 0.5 - yout/2.0;
00812 sinBdelta = 2.0/yout;
00813
00814 for (jrow = 0; jrow < yout; jrow++) {
00815 rowoffset = jrow * xout;
00816 sinB = sinBdelta * (sinBbase + jrow);
00817 sinB2 = sinB * sinB;
00818 cosB = sqrt(1.0 - sinB2);
00819
00820 for (icol = 0; icol < xout; icol++) {
00821 if (isnan(smallSynopbr[rowoffset + icol]))
00822 {
00823 smallSynopblos[rowoffset + icol] = DRMS_MISSING_FLOAT;
00824 continue;
00825 }
00826 smallSynopblos[rowoffset + icol] = smallSynopbr[rowoffset + icol] * cosB;
00827 }
00828 }
00829
00830
00831
00832 outRD = drms_create_records(drms_env, 1, outRecQuery, DRMS_PERMANENT, &status);
00833 if (status)
00834 DIE("Output record not created");
00835 outMlRD = drms_create_records(drms_env, 1, outMlRecQuery, DRMS_PERMANENT, &status);
00836 if (status)
00837 DIE("Output record not created");
00838 smallMrRD = drms_create_records(drms_env, 1, smallMrRecQuery, DRMS_PERMANENT, &status);
00839 if (status)
00840 DIE("Output record not created");
00841 smallMlRD = drms_create_records(drms_env, 1, smallMlRecQuery, DRMS_PERMANENT, &status);
00842 if (status)
00843 DIE("Output record not created");
00844
00845 DRMS_Segment_t *outSeg, *outMlSeg, *smallMrSeg, *smallMlSeg;
00846 DRMS_Array_t *outArray, *smallArray;
00847 int outDims[2] = {len[0],len[1]};
00848 outRec = outRD->records[0];
00849 outMlRec = outMlRD->records[0];
00850 smallMrRec = smallMrRD->records[0];
00851 smallMlRec = smallMlRD->records[0];
00852
00853
00854 outSeg = drms_segment_lookupnum(outRec, 0);
00855 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, synopbr, &status);
00856 status = drms_segment_write(outSeg, outArray, 0);
00857 if (status) DIE("problem writing file");
00858
00859 if (fstats(len[0]*len[1], synopbr, &statMin, &statMax, &statMedn, &statMean, &statSig,
00860 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00861
00862 drms_setkey_int(outRec, "TOTVALS", len[0]*len[1]);
00863 drms_setkey_int(outRec, "DATAVALS", statNgood);
00864 drms_setkey_int(outRec, "MISSVALS", len[0]*len[1]-statNgood);
00865 drms_setkey_double(outRec, "DATAMIN", statMin);
00866 drms_setkey_double(outRec, "DATAMAX", statMax);
00867 drms_setkey_double(outRec, "DATAMEDN", statMedn);
00868 drms_setkey_double(outRec, "DATAMEAN", statMean);
00869 drms_setkey_double(outRec, "DATARMS", statSig);
00870 drms_setkey_double(outRec, "DATASKEW", statSkew);
00871 drms_setkey_double(outRec, "DATAKURT", statKurt);
00872 drms_free_array(outArray);
00873
00874
00875 outMlSeg = drms_segment_lookupnum(outMlRec, 0);
00876 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, synopblos, &status);
00877 status = drms_segment_write(outMlSeg, outArray, 0);
00878 if (status) DIE("problem writing file");
00879
00880
00881 if (fstats(len[0]*len[1], synopblos, &statMin, &statMax, &statMedn, &statMean, &statSig,
00882 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00883
00884 drms_setkey_int(outMlRec, "TOTVALS", len[0]*len[1]);
00885 drms_setkey_int(outMlRec, "DATAVALS", statNgood);
00886 drms_setkey_int(outMlRec, "MISSVALS", len[0]*len[1]-statNgood);
00887 drms_setkey_double(outMlRec, "DATAMIN", statMin);
00888 drms_setkey_double(outMlRec, "DATAMAX", statMax);
00889 drms_setkey_double(outMlRec, "DATAMEDN", statMedn);
00890 drms_setkey_double(outMlRec, "DATAMEAN", statMean);
00891 drms_setkey_double(outMlRec, "DATARMS", statSig);
00892 drms_setkey_double(outMlRec, "DATASKEW", statSkew);
00893 drms_setkey_double(outMlRec, "DATAKURT", statKurt);
00894 drms_free_array(outArray);
00895
00896
00897 outSeg = drms_segment_lookupnum(outRec, 1);
00898 outMlSeg = drms_segment_lookupnum(outMlRec, 1);
00899 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, epts, &status);
00900 status = drms_segment_write(outSeg, outArray, 0);
00901 if (status) DIE("problem writing file");
00902 status = drms_segment_write(outMlSeg, outArray, 0);
00903 if (status) DIE("problem writing file");
00904 drms_free_array(outArray);
00905
00906
00907 smallMrSeg = drms_segment_lookupnum(smallMrRec, 0);
00908 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallSynopbr, &status);
00909 status = drms_segment_write(smallMrSeg, smallArray, 0);
00910 if (status) DIE("problem writing file");
00911
00912 if (fstats(xout*yout, smallSynopbr, &smallstatMin, &smallstatMax, &smallstatMedn, &smallstatMean, &smallstatSig,
00913 &smallstatSkew, &smallstatKurt, &smallstatNgood)) printf("\n Statistics computation failed\n");
00914
00915 drms_setkey_int(smallMrRec, "TOTVALS", xout*yout);
00916 drms_setkey_int(smallMrRec, "DATAVALS", smallstatNgood);
00917 drms_setkey_int(smallMrRec, "MISSVALS", xout*yout-smallstatNgood);
00918 drms_setkey_double(smallMrRec, "DATAMIN", smallstatMin);
00919 drms_setkey_double(smallMrRec, "DATAMAX", smallstatMax);
00920 drms_setkey_double(smallMrRec, "DATAMEDN", smallstatMedn);
00921 drms_setkey_double(smallMrRec, "DATAMEAN", smallstatMean);
00922 drms_setkey_double(smallMrRec, "DATARMS", smallstatSig);
00923 drms_setkey_double(smallMrRec, "DATASKEW", smallstatSkew);
00924 drms_setkey_double(smallMrRec, "DATAKURT", smallstatKurt);
00925 drms_free_array(smallArray);
00926
00927
00928 smallMlSeg = drms_segment_lookupnum(smallMlRec, 0);
00929 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallSynopblos, &status);
00930 status = drms_segment_write(smallMlSeg, smallArray, 0);
00931 if (status) DIE("problem writing file");
00932
00933 if (fstats(xout*yout, smallSynopblos, &smallstatMin, &smallstatMax, &smallstatMedn, &smallstatMean, &smallstatSig,
00934 &smallstatSkew, &smallstatKurt, &smallstatNgood)) printf("\n Statistics computation failed\n");
00935
00936 drms_setkey_int(smallMlRec, "TOTVALS", xout*yout);
00937 drms_setkey_int(smallMlRec, "DATAVALS", smallstatNgood);
00938 drms_setkey_int(smallMlRec, "MISSVALS", xout*yout-smallstatNgood);
00939 drms_setkey_double(smallMlRec, "DATAMIN", smallstatMin);
00940 drms_setkey_double(smallMlRec, "DATAMAX", smallstatMax);
00941 drms_setkey_double(smallMlRec, "DATAMEDN", smallstatMedn);
00942 drms_setkey_double(smallMlRec, "DATAMEAN", smallstatMean);
00943 drms_setkey_double(smallMlRec, "DATARMS", smallstatSig);
00944 drms_setkey_double(smallMlRec, "DATASKEW", smallstatSkew);
00945 drms_setkey_double(smallMlRec, "DATAKURT", smallstatKurt);
00946 drms_free_array(smallArray);
00947
00948
00949 smallMrSeg = drms_segment_lookupnum(smallMrRec, 1);
00950 smallMlSeg = drms_segment_lookupnum(smallMlRec, 1);
00951 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallEpts, &status);
00952 status = drms_segment_write(smallMrSeg, smallArray, 0);
00953 if (status) DIE("problem writing file");
00954 status = drms_segment_write(smallMlSeg, smallArray, 0);
00955 if (status) DIE("problem writing file");
00956 drms_free_array(smallArray);
00957
00958
00959
00960
00961
00962 sprint_ut(tstr, CURRENT_SYSTEM_TIME);
00963 sscanf(tstr, "%d.%d.%d", &y, &m, &d);
00964 sprintf(tstr, "%04d-%02d-%02d", y, m, d);
00965 drms_setkey_string(outRec, "DATE", tstr);
00966 drms_setkey_string(outMlRec, "DATE", tstr);
00967
00968
00969
00970
00971 drms_setkey_string(outRec, "HISTORY", historyofthemodule);
00972 drms_setkey_string(outMlRec, "HISTORY", historyofthemodule);
00973 drms_setkey_string(outRec, "CTYPE1", "CRLN-CEA");
00974 drms_setkey_string(outMlRec, "CTYPE1", "CRLN-CEA");
00975 drms_setkey_string(outRec, "CTYPE2", "CRLT-CEA");
00976 drms_setkey_string(outMlRec, "CTYPE2", "CRLT-CEA");
00977 drms_setkey_string(outRec, "BLD_VERS", jsoc_version);
00978 drms_setkey_string(outMlRec, "BLD_VERS", jsoc_version);
00979 status = drms_setkey_string(outRec, "CODEVER", cvsinfo);
00980 status = drms_setkey_string(outMlRec, "CODEVER", cvsinfo);
00981
00982
00983 i=len[0]-(len[0]/2+1.0)+1.0;
00984
00985
00986
00987
00988
00989 drms_setkey_double(outRec, "CRPIX1", i);
00990 drms_setkey_double(outMlRec, "CRPIX1", i);
00991
00992 l=(len[1]+1.0)/2;
00993 drms_setkey_double(outRec, "CRPIX2", l);
00994 drms_setkey_double(outMlRec, "CRPIX2", l);
00995
00996
00997 carrtime = (cr-1)*360.0 + 180.0;
00998 drms_setkey_double(outRec, "CRVAL1", carrtime);
00999 drms_setkey_double(outMlRec, "CRVAL1", carrtime);
01000 i=0.0;
01001 drms_setkey_double(outRec, "CRVAL2", i);
01002 drms_setkey_double(outMlRec, "CRVAL2", i);
01003 l=-360.0/len[0];
01004 drms_setkey_double(outRec, "CDELT1", l);
01005 drms_setkey_double(outMlRec, "CDELT1", l);
01006 l=2.0/len[1];
01007 drms_setkey_double(outRec, "CDELT2", l);
01008 drms_setkey_double(outMlRec, "CDELT2", l);
01009 drms_setkey_string(outRec, "CUNIT1", "degree");
01010 drms_setkey_string(outMlRec, "CUNIT1", "degree");
01011 drms_setkey_string(outRec, "CUNIT2", "Sine Latitude");
01012 drms_setkey_string(outMlRec, "CUNIT2", "Sine Latitude");
01013 drms_setkey_string(outRec, "WCSNAME", "Carrington Heliographic");
01014 drms_setkey_string(outMlRec, "WCSNAME", "Carrington Heliographic");
01015
01016
01017 sprint_at(tstr, trot - delta_T);
01018
01019 drms_setkey_float(outRec, "CADENCE", 360.0);
01020 drms_setkey_float(outMlRec, "CADENCE", 360.0);
01021 drms_setkey_float(outRec, "T_REC_step", 27.2753*24.*60.*60.);
01022 drms_setkey_float(outMlRec, "T_REC_step", 27.2753*24.*60.*60.);
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058 drms_setkey_string(outRec, "T_OBS", tstr);
01059 drms_setkey_string(outRec, "T_ROT", tstr);
01060 drms_setkey_string(outMlRec, "T_OBS", tstr);
01061 drms_setkey_string(outMlRec, "T_ROT", tstr);
01062 sprint_at(tstr, tstart - delta_T);
01063 drms_setkey_string(outRec, "T_START", tstr);
01064 drms_setkey_string(outMlRec, "T_START", tstr);
01065 sprint_at(tstr, tstop - delta_T);
01066 drms_setkey_string(outRec, "T_STOP", tstr);
01067 drms_setkey_string(outMlRec, "T_STOP", tstr);
01068
01069
01070 drms_setkey_int(outRec, "CAR_ROT", cr);
01071 drms_setkey_double(outRec, "CARRTIME", carrtime);
01072 drms_setkey_int(outMlRec, "CAR_ROT", cr);
01073 drms_setkey_double(outMlRec, "CARRTIME", carrtime);
01074 solephem(trot, eph);
01075
01076 drms_setkey_double(outRec, "B0_ROT", eph[9]/RADSINDEG);
01077 drms_setkey_double(outMlRec, "B0_ROT", eph[9]/RADSINDEG);
01078 solephem(tstart, eph);
01079
01080 drms_setkey_double(outRec, "B0_FRST", eph[9]/RADSINDEG);
01081 drms_setkey_double(outMlRec, "B0_FRST", eph[9]/RADSINDEG);
01082 solephem(tstop, eph);
01083
01084 drms_setkey_double(outRec, "B0_LAST", eph[9]/RADSINDEG);
01085 drms_setkey_double(outMlRec, "B0_LAST", eph[9]/RADSINDEG);
01086
01087 l=(cr-1)*360.0;
01088 drms_setkey_double(outRec, "LON_FRST", l);
01089 drms_setkey_double(outMlRec, "LON_FRST", l);
01090 l=cr*360.0-360.0/len[0];
01091 drms_setkey_double(outRec, "LON_LAST", l);
01092 drms_setkey_double(outMlRec, "LON_LAST", l);
01093 l=-360.0/len[0];
01094 drms_setkey_double(outRec, "LON_STEP", l);
01095 drms_setkey_double(outMlRec, "LON_STEP", l);
01096 l=center;
01097 drms_setkey_double(outRec, "W_OFFSET", l);
01098 drms_setkey_string(outRec, "W_WEIGHT", "Even");
01099 drms_setkey_double(outMlRec, "W_OFFSET", l);
01100 drms_setkey_string(outMlRec, "W_WEIGHT", "Even");
01101 i=lastidx-firstidx+1;
01102 drms_setkey_int(outRec, "IMG_NUM", i);
01103 drms_setkey_int(outMlRec, "IMG_NUM", i);
01104 sprint_at(tstr, imrec[firstidx].tobs);
01105 drms_setkey_string(outRec, "IMG_FRST", tstr);
01106 drms_setkey_string(outMlRec, "IMG_FRST", tstr);
01107 sprint_at(tstr, imrec[lastidx].tobs);
01108 drms_setkey_string(outRec, "IMG_LAST", tstr);
01109 drms_setkey_string(outMlRec, "IMG_LAST", tstr);
01110 sprint_at(tstr, magtime);
01111 drms_setkey_string(outRec, "IMG_ROT", tstr);
01112 drms_setkey_string(outMlRec, "IMG_ROT", tstr);
01113 drms_setkey_float(outRec, HWNWIDTH, halfWindow);
01114 drms_setkey_float(outRec, EQPOINTS, nEquivPtsReq);
01115 drms_setkey_float(outRec, "NSIGMA", nsig);
01116 drms_setkey_longlong(outRec, "CALVER64", calVer);
01117 drms_setkey_float(outMlRec, HWNWIDTH, halfWindow);
01118 drms_setkey_float(outMlRec, EQPOINTS, nEquivPtsReq);
01119 drms_setkey_float(outMlRec, "NSIGMA", nsig);
01120 drms_setkey_longlong(outMlRec, "CALVER64", calVer);
01121
01122
01123 if (carrStretch > 0)
01124 {
01125 drms_setkey_int(outRec, kCARSTRCH, carrStretch);
01126 drms_setkey_float(outRec, kDIFROT_A, diffrotA);
01127 drms_setkey_float(outRec, kDIFROT_B, diffrotB);
01128 drms_setkey_float(outRec, kDIFROT_C, diffrotC);
01129 drms_setkey_int(outMlRec, kCARSTRCH, carrStretch);
01130 drms_setkey_float(outMlRec, kDIFROT_A, diffrotA);
01131 drms_setkey_float(outMlRec, kDIFROT_B, diffrotB);
01132 drms_setkey_float(outMlRec, kDIFROT_C, diffrotC);
01133 }
01134
01135
01136
01137
01138
01139
01140 sprint_ut(tstr, CURRENT_SYSTEM_TIME);
01141 sscanf(tstr, "%d.%d.%d", &y, &m, &d);
01142 sprintf(tstr, "%04d-%02d-%02d", y, m, d);
01143 drms_setkey_string(smallMrRec, "DATE", tstr);
01144 drms_setkey_string(smallMlRec, "DATE", tstr);
01145
01146
01147
01148
01149 drms_setkey_string(smallMrRec, "HISTORY", historyofthemodule);
01150 drms_setkey_string(smallMrRec, "CTYPE1", "CRLN-CEA");
01151 drms_setkey_string(smallMrRec, "CTYPE2", "CRLT-CEA");
01152 drms_setkey_string(smallMrRec, "BLD_VERS", jsoc_version);
01153 status = drms_setkey_string(smallMrRec, "CODEVER", cvsinfo);
01154 drms_setkey_string(smallMlRec, "HISTORY", historyofthemodule);
01155 drms_setkey_string(smallMlRec, "CTYPE1", "CRLN-CEA");
01156 drms_setkey_string(smallMlRec, "CTYPE2", "CRLT-CEA");
01157 drms_setkey_string(smallMlRec, "BLD_VERS", jsoc_version);
01158 status = drms_setkey_string(smallMlRec, "CODEVER", cvsinfo);
01159
01160
01161 double tmp;
01162 tmp=xout - ((180.0-((nbin+1)/2.0-1.0)*(360.0/len[0]))/(360.0/xout)+1.0) + 1.0;
01163
01164
01165
01166 drms_setkey_double(smallMrRec, "CRPIX1", tmp);
01167 drms_setkey_double(smallMlRec, "CRPIX1", tmp);
01168 tmp=(yout+1.0)/2;
01169 drms_setkey_double(smallMrRec, "CRPIX2", tmp);
01170 drms_setkey_double(smallMlRec, "CRPIX2", tmp);
01171
01172 carrtime = (cr-1)*360.0 + 180.0;
01173 drms_setkey_double(smallMrRec, "CRVAL1", carrtime);
01174 drms_setkey_double(smallMlRec, "CRVAL1", carrtime);
01175 tmp=0.0;
01176 drms_setkey_double(smallMrRec, "CRVAL2", tmp);
01177 drms_setkey_double(smallMlRec, "CRVAL2", tmp);
01178 l=-360.0/xout;
01179 drms_setkey_double(smallMrRec, "CDELT1", l);
01180 l = 2.0/yout;
01181 drms_setkey_double(smallMrRec, "CDELT2", l);
01182 drms_setkey_string(smallMrRec, "CUNIT1", "Degree");
01183 drms_setkey_string(smallMrRec, "CUNIT2", "Sine Latitude");
01184 drms_setkey_string(smallMrRec, "WCSNAME", "Carrington Heliographic");
01185 l=-360.0/xout;
01186 drms_setkey_double(smallMlRec, "CDELT1", l);
01187 l = 2.0/yout;
01188 drms_setkey_double(smallMlRec, "CDELT2", l);
01189 drms_setkey_string(smallMlRec, "CUNIT1", "Degree");
01190 drms_setkey_string(smallMlRec, "CUNIT2", "Sine Latitude");
01191 drms_setkey_string(smallMlRec, "WCSNAME", "Carrington Heliographic");
01192
01193
01194 sprint_at(tstr, trot - delta_T);
01195
01196 drms_setkey_float(smallMrRec, "CADENCE", 360.0);
01197 drms_setkey_float(smallMlRec, "CADENCE", 360.0);
01198 drms_setkey_float(smallMrRec, "T_REC_step", 27.2753*24.*60.*60.);
01199 drms_setkey_float(smallMlRec, "T_REC_step", 27.2753*24.*60.*60.);
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232 drms_setkey_string(smallMrRec, "T_OBS", tstr);
01233 drms_setkey_string(smallMrRec, "T_ROT", tstr);
01234 drms_setkey_string(smallMlRec, "T_OBS", tstr);
01235 drms_setkey_string(smallMlRec, "T_ROT", tstr);
01236 sprint_at(tstr, tstart - delta_T);
01237 drms_setkey_string(smallMrRec, "T_START", tstr);
01238 drms_setkey_string(smallMlRec, "T_START", tstr);
01239 sprint_at(tstr, tstop - delta_T);
01240 drms_setkey_string(smallMrRec, "T_STOP", tstr);
01241 drms_setkey_string(smallMlRec, "T_STOP", tstr);
01242
01243
01244 drms_setkey_int(smallMrRec, "CAR_ROT", cr);
01245 drms_setkey_double(smallMrRec, "CARRTIME", carrtime);
01246 drms_setkey_int(smallMlRec, "CAR_ROT", cr);
01247 drms_setkey_double(smallMlRec, "CARRTIME", carrtime);
01248 solephem(trot, eph);
01249
01250 drms_setkey_double(smallMrRec, "B0_ROT", eph[9]/RADSINDEG);
01251 drms_setkey_double(smallMlRec, "B0_ROT", eph[9]/RADSINDEG);
01252
01253 solephem(tstart, eph);
01254 drms_setkey_double(smallMrRec, "B0_FRST", eph[9]/RADSINDEG);
01255 drms_setkey_double(smallMlRec, "B0_FRST", eph[9]/RADSINDEG);
01256
01257 solephem(tstop, eph);
01258 drms_setkey_double(smallMrRec, "B0_LAST", eph[9]/RADSINDEG);
01259 drms_setkey_double(smallMlRec, "B0_LAST", eph[9]/RADSINDEG);
01260
01261 l=(cr-1)*360.0+((nbin+1)/2.0-1.0)*(360.0/len[0]);
01262 drms_setkey_double(smallMrRec, "LON_FRST", l);
01263 drms_setkey_double(smallMlRec, "LON_FRST", l);
01264 l=cr*360.0-360.0/len[0]-((nbin+1)/2.0-1.0)*(360.0/len[0]);
01265 drms_setkey_double(smallMrRec, "LON_LAST", l);
01266 drms_setkey_double(smallMlRec, "LON_LAST", l);
01267 l=-360.0/xout;
01268 drms_setkey_double(smallMrRec, "LON_STEP", l);
01269 drms_setkey_double(smallMlRec, "LON_STEP", l);
01270 l=center;
01271 drms_setkey_double(smallMrRec, "W_OFFSET", l);
01272 drms_setkey_string(smallMrRec, "W_WEIGHT", "Even");
01273 drms_setkey_double(smallMlRec, "W_OFFSET", l);
01274 drms_setkey_string(smallMlRec, "W_WEIGHT", "Even");
01275 i=lastidx-firstidx+1;
01276 drms_setkey_int(smallMrRec, "IMG_NUM", i);
01277 drms_setkey_int(smallMlRec, "IMG_NUM", i);
01278 sprint_at(tstr, imrec[firstidx].tobs);
01279 drms_setkey_string(smallMrRec, "IMG_FRST", tstr);
01280 drms_setkey_string(smallMlRec, "IMG_FRST", tstr);
01281 sprint_at(tstr, imrec[lastidx].tobs);
01282 drms_setkey_string(smallMrRec, "IMG_LAST", tstr);
01283 drms_setkey_string(smallMlRec, "IMG_LAST", tstr);
01284 sprint_at(tstr, magtime);
01285 drms_setkey_string(smallMrRec, "IMG_ROT", tstr);
01286 drms_setkey_float(smallMrRec, HWNWIDTH, halfWindow);
01287 drms_setkey_float(smallMrRec, EQPOINTS, nEquivPtsReq);
01288 drms_setkey_float(smallMrRec, "NSIGMA", nsig);
01289 drms_setkey_longlong(smallMrRec, "CALVER64", calVer);
01290 drms_setkey_string(smallMlRec, "IMG_ROT", tstr);
01291 drms_setkey_float(smallMlRec, HWNWIDTH, halfWindow);
01292 drms_setkey_float(smallMlRec, EQPOINTS, nEquivPtsReq);
01293 drms_setkey_float(smallMlRec, "NSIGMA", nsig);
01294 drms_setkey_longlong(smallMlRec, "CALVER64", calVer);
01295
01296
01297 if (carrStretch > 0)
01298 {
01299 drms_setkey_int(smallMrRec, kCARSTRCH, carrStretch);
01300 drms_setkey_float(smallMrRec, kDIFROT_A, diffrotA);
01301 drms_setkey_float(smallMrRec, kDIFROT_B, diffrotB);
01302 drms_setkey_float(smallMrRec, kDIFROT_C, diffrotC);
01303 drms_setkey_int(smallMlRec, kCARSTRCH, carrStretch);
01304 drms_setkey_float(smallMlRec, kDIFROT_A, diffrotA);
01305 drms_setkey_float(smallMlRec, kDIFROT_B, diffrotB);
01306 drms_setkey_float(smallMlRec, kDIFROT_C, diffrotC);
01307 }
01308
01309 if (sortedMagCol)
01310 {
01311 for (i = 0; i < len[0]; i++)
01312 {
01313 if (sortedMagCol[i])
01314 {
01315 free(sortedMagCol[i]);
01316 }
01317 }
01318 free(sortedMagCol);
01319 }
01320
01321 drms_close_records(inRD, DRMS_FREE_RECORD);
01322 drms_close_records(outRD, DRMS_INSERT_RECORD);
01323 drms_close_records(outMlRD, DRMS_INSERT_RECORD);
01324 drms_close_records(smallMrRD, DRMS_INSERT_RECORD);
01325 drms_close_records(smallMlRD, DRMS_INSERT_RECORD);
01326 return 0;
01327 }
01328
01329 int CalcSynCols(int start,
01330 int end,
01331 int incr,
01332 MagCol_t **smc,
01333 float *synopbr,
01334 int *wt,
01335 char *ww,
01336 float *epts,
01337 float *losSynop,
01338 int *len,
01339 float center,
01340 float nEquivPtsReq,
01341 int los,
01342 int radialFound,
01343 float sensAdj,
01344 float noiseS,
01345 float nsig,
01346 float maxNoiseAdj,
01347 int minOutPts,
01348 int dlog)
01349 {
01350 float midSynRow;
01351 float cosrho;
01352
01353
01354
01355
01356
01357 float cosCenter;
01358 char *rejDescFormat;
01359 int col;
01360 int row;
01361 int i;
01362 int j;
01363 static int nrej = 0;
01364 int marked;
01365 float nEquivPts;
01366
01367 midSynRow = (float)(len[1] - 1) / 2.0;
01368 cosCenter = cos(center * M_PI / 180);
01369 rejDescFormat = " magnetogram: ds=%d, sn=%d, magcol=%d, magrow=%d\n";
01370 if (minOutPts < 2) minOutPts = 2;
01371
01372
01373 for (col = start; incr > 0 ? col <= end : col >= end; col += incr)
01374 {
01375 float *val = (float *)malloc(sizeof(float) * wt[col]);
01376 float *ept = (float *)malloc(sizeof(float) * wt[col]);
01377 MagCol_t *pcols = NULL;
01378 marked = 0;
01379
01380 if (dlog)
01381 {
01382 pcols = (MagCol_t *)malloc(sizeof(MagCol_t) * wt[col]);
01383 memset(pcols, 0, sizeof(MagCol_t) * wt[col]);
01384 }
01385
01386 if (!val || !ept) printf("MALLOC FAILED");
01387
01388
01389 SortMagCols(smc[col], wt[col]);
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407 for (row = 0; row < len[1]; ++row)
01408 {
01409 float sinLat = (row - midSynRow) / midSynRow;
01410 float cosLat = sqrt(1 - sinLat * sinLat);
01411 float sum;
01412 int npts;
01413 float sumfinal = 0.0;
01414 int nptsfinal = 0;
01415 int Maxnepts = nEquivPtsReq + 10;
01416
01417 if (radialFound) cosrho = cosLat * cosCenter;
01418
01419 sum = 0.0;
01420 npts = 0;
01421 j = 0;
01422 nEquivPts = 0.0;
01423
01424
01425
01426
01427
01428 for (i = 0; i < wt[col] && nEquivPts < Maxnepts; i++)
01429 {
01430 float magVal = *(smc[col][i].datacol + row);
01431 if (!drms_ismissing_float(magVal))
01432 {
01433 nEquivPts += smc[col][i].equivPts;
01434 sum += magVal;
01435 val[j] = magVal;
01436 ept[j] = smc[col][i].equivPts;
01437
01438 if (dlog && pcols) pcols[j] = smc[col][i];
01439
01440 j++;
01441 npts++;
01442 }
01443 }
01444
01445
01446 if (npts > 1 && nsig > 0.0) {
01447 double avg = 0.0;
01448 double ssqr = 0.0;
01449 double sig;
01450 float med;
01451 int nStatPts = 0;
01452 float *statVals;
01453 float noiseLevel;
01454 float *outlier = NULL;
01455 int statsDone = 0;
01456 int sIdx = 0;
01457 float dev = 0.0;
01458 int nPtsB4Rej = npts;
01459
01460
01461
01462 noiseLevel = kNOISE_EQ * noiseS * sensAdj;
01463 if (radialFound) noiseLevel = noiseLevel * MIN(1 / cosrho, maxNoiseAdj);
01464
01465
01466 statVals = (float *)malloc(sizeof(float) * npts);
01467 if (!statVals) printf("MALLOC_FAILED");
01468
01469 memcpy(statVals, val, sizeof(float) * npts);
01470 nStatPts = magStats(&statVals, npts, sum, minOutPts, &avg, &med);
01471
01472 for (j = 0; j < nPtsB4Rej; ++j)
01473 {
01474
01475
01476 if (!statsDone)
01477 {
01478 for (sIdx = 0; sIdx < nStatPts; sIdx++)
01479 {
01480 ssqr += statVals[sIdx] * statVals[sIdx];
01481 }
01482
01483 sig = sqrt((ssqr - nStatPts * avg * avg) / (nStatPts - 1));
01484 statsDone = 1;
01485 }
01486
01487 dev = fabs(val[j] - med);
01488 if (nptsfinal >= nEquivPtsReq) break;
01489 if (dev < nsig * sig)
01490 {
01491 sumfinal += val[j];
01492 nptsfinal += 1;
01493
01494 --npts;
01495 ++nrej;
01496 if (dlog)
01497 {
01498 char rejDescBuf[128];
01499
01500 snprintf(rejDescBuf,
01501 sizeof(rejDescBuf),
01502 rejDescFormat,
01503 pcols[j].ds,
01504 pcols[j].col,
01505 row);
01506
01507 }
01508
01509 }
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532 }
01533
01534 free(statVals);
01535 }
01536
01537
01538
01539
01540
01541 if (nptsfinal)
01542 {
01543
01544 float synVal = sumfinal/nptsfinal;
01545 float minVal = (SHRT_MIN + 1);
01546 float maxVal = (SHRT_MAX - 1);
01547
01548
01549
01550
01551 if (synVal < minVal)
01552 {
01553 synopbr[row * len[0] + col] = minVal;
01554 }
01555 else if (synVal > maxVal)
01556 {
01557 synopbr[row * len[0] + col] = maxVal;
01558 }
01559 else
01560 {
01561 synopbr[row * len[0] + col] = synVal;
01562 }
01563 }
01564 else
01565 {
01566 synopbr[row * len[0] + col] = DRMS_MISSING_FLOAT;
01567 }
01568
01569 ww[row * len[0] + col] = npts;
01570
01571 epts[row * len[0] + col] = nptsfinal;
01572 if (los && radialFound)
01573 {
01574 int offset = row * len[0] + col;
01575 if (drms_ismissing_float(synopbr[offset]))
01576 {
01577 losSynop[offset] = DRMS_MISSING_FLOAT;
01578 }
01579 else
01580 {
01581 losSynop[offset] = synopbr[offset] * cosLat;
01582 }
01583 }
01584 }
01585
01586 free(val);
01587 val = NULL;
01588 free(ept);
01589 ept = NULL;
01590
01591 if (pcols)
01592 {
01593 free(pcols);
01594 pcols = NULL;
01595 }
01596 }
01597 return 0;
01598 }
01599
01600 int cmp(const void *a, const void *b)
01601 {
01602 float aa = *(float *)a;
01603 float bb = *(float *)b;
01604 return (aa<bb) ? -1 : (aa==bb) ? 0 : 1;
01605 }
01606
01607
01608 int magStats(float **val, int npts, double sum, int outThreshold, double *avg, float *med)
01609 {
01610 int actPts = npts;
01611 float *out = NULL;
01612 float *in = *val;
01613 int idx = 0;
01614 int iOut = 0;
01615 float medianInt = 0.0;
01616 float first;
01617 float last;
01618
01619 qsort((void *)in, npts, sizeof(float), cmp);
01620
01621 if (npts > outThreshold && npts > 1)
01622 {
01623 actPts = npts - 1;
01624 out = malloc(sizeof(float) * actPts);
01625
01626 medianInt = in[npts/2];
01627
01628 first = in[0];
01629 last = in[npts - 1];
01630
01631 if (fabs(first - medianInt) <= fabs(last - medianInt))
01632 {
01633 idx = 0;
01634 sum -= last;
01635 }
01636 else
01637 {
01638 idx = 1;
01639 sum -= first;
01640 }
01641
01642 for (iOut = 0; idx < npts && iOut < actPts; idx++, iOut++)
01643 {
01644 out[iOut] = in[idx];
01645 }
01646
01647 free(in);
01648 *val = out;
01649 in = *val;
01650 }
01651
01652 *avg = sum / actPts;
01653 *med = in[actPts / 2];
01654
01655 return actPts;
01656 }
01657
01658 int CmpMagCol(const void *a, const void *b)
01659 {
01660 MagCol_t *aa = (MagCol_t *)a;
01661 MagCol_t *bb = (MagCol_t *)b;
01662
01663 if (aa->valid && bb->valid)
01664 {
01665 return (fabsf(aa->dist) < fabsf(bb->dist)) ? -1 :
01666 ((fabsf(aa->dist) == fabsf(bb->dist)) ? 0 : 1);
01667 }
01668 else if (aa->valid)
01669 {
01670 return -1;
01671 }
01672 else if (bb->valid)
01673 {
01674 return 1;
01675 }
01676 else
01677 {
01678 return 0;
01679 }
01680 }
01681
01682 void SortMagCols(MagCol_t *mc, int nelem)
01683 {
01684
01685 MagCol_t *mc_cpy = NULL;
01686 int i;
01687 mc_cpy = (MagCol_t *)malloc(sizeof(MagCol_t) * nelem);
01688 for (i = 0; i < nelem; i++)
01689 mc_cpy[i] = mc[i];
01690 m_sort(mc, mc_cpy, nelem);
01691 free(mc_cpy);
01692 }
01693
01694 void m_sort(MagCol_t *mc, MagCol_t *mc_cpy, int sz)
01695 {
01696 int n, i, j, mid;
01697 mid = sz/2;
01698 if (mid > 1) m_sort(mc_cpy, mc, mid);
01699 if (sz-mid > 1) m_sort(mc_cpy + mid, mc + mid, sz - mid);
01700
01701
01702 MagCol_t *mc_cpy2 = mc_cpy + mid;
01703 for (n=0, i=0, j=0; i<mid && j<sz-mid; n++)
01704 mc[n] = (fabs(mc_cpy[i].dist) < fabs(mc_cpy2[j].dist)) ? mc_cpy[i++] : mc_cpy2[j++];
01705 while (i<mid)
01706 mc[n++] = mc_cpy[i++];
01707 while (j<sz-mid)
01708 mc[n++] = mc_cpy2[j++];
01709 }
01710
01711 void frebinbox(float *image_in, float *image_out, int nx, int ny, int nbinx, int nbiny)
01712 {
01713 int nxout, nyout;
01714 int ii, jj, i, j;
01715 nxout = nx / nbinx; nyout = ny / nbiny;
01716 for (j = 0; j < nyout; j++) {
01717 int yOff, jy;
01718 jy = j * nbiny;
01719 for (i = 0; i < nxout; i++) {
01720 int ix;
01721 ix = i * nbinx;
01722 float aveval = 0.0;
01723 int number = 0;
01724 for (jj = 0; jj < nbiny; jj++) {
01725 yOff = (jy + jj) * nx;
01726 for (ii = 0; ii < nbinx; ii++) {
01727 int iData = yOff + ix + ii;
01728 if (!(isnan(image_in[iData]))) {
01729 aveval += image_in[iData];
01730 number += 1;
01731 }
01732 }
01733 }
01734 image_out[j*nxout+i] = aveval/number;
01735 }
01736 }
01737 }
01738
01739
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749
01750
01751
01752
01753
01754
01755
01756
01757
01758
01759
01760
01761
01762
01763
01764
01765
01766
01767
01768
01769
01770
01771
01772
01773
01774
01775
01776
01777
01778
01779
01780
01781
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792
01793
01794
01795
01796
01797
01798
01799
01800
01801
01802
01803
01804
01805
01806
01807
01808
01809
01810
01811
01812
01813
01814
01815
01816
01817
01818
01819
01820
01821
01822
01823
01824
01825
01826
01827
01828
01829
01830 void FreeMagColsData(int start, int end, int incr, int *n, MagCol_t **mc)
01831 {
01832 if (mc)
01833 {
01834 float *data = NULL;
01835 int cIdx;
01836 int idx;
01837
01838 for (cIdx = start; incr > 0 ? cIdx <= end : cIdx >= end; cIdx += incr)
01839 {
01840 for (idx = 0; idx < n[cIdx]; idx++)
01841 {
01842 data = mc[cIdx][idx].datacol;
01843 if (data)
01844 {
01845 free(data);
01846 mc[cIdx][idx].datacol = NULL;
01847 mc[cIdx][idx].valid = 0;
01848 }
01849 }
01850 }
01851 }
01852 }
01853
01854 TIME CarringtonTime(int crot, double L)
01855 {
01856 double eph[30];
01857 double CT, clong=0.0;
01858 double err, CTp50m, CTm50m;
01859 TIME t;
01860 char *stime;
01861 char tstr[64];
01862 CT = 0.0;
01863 static TIME T1853 = 0.0, delta_T = 0.0;
01864 T1853 = sscan_time("1853.11.09_22:27:24_UTC");
01865
01866 delta_T = sscan_time("1977.01.01_00:00:00_TAI") - sscan_time("1601.01.01_00:00:00_UT");
01867
01868 CT = 360.0 * crot - L;
01869 t = (C2 + CT * C1) * SID;
01870
01871
01872 solephem(t, eph);
01873 err = CT - eph[8];
01874 solephem(t+6*3600.0, eph);
01875 CTp50m = eph[8];
01876 solephem(t-6*3600.0, eph);
01877 CTm50m = eph[8];
01878
01879 t += 12*3600 * (err/(CTp50m - CTm50m));
01880 solephem(t, eph);
01881 err = CT - eph[8];
01882 solephem(t+300.0, eph);
01883 CTp50m = eph[8];
01884 solephem(t-300.0, eph);
01885 CTm50m = eph[8];
01886
01887 t += 600 * (err/(CTp50m - CTm50m));
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901 return(t);
01902 }
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924
01925
01926
01927
01928
01929
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940
01941
01942
01943
01944
01945
01946
01947
01948
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967
01968
01969
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991
01992
01993
01994
01995
01996
01997
01998
01999
02000
02001
02002
02003
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014
02015
02016
02017
02018
02019
02020
02021
02022
02023
02024
02025
02026
02027
02028
02029
02030
02031
02032
02033
02034