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
00015
00016 #include "/home/wso/src/libastro.d/solephem.c"
00017 #include "fstats.h"
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 = "hmisynoptic";
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, "smallsyn", "NOTSPECIFIED", ""},
00114 {ARG_INT, CR, "", "", ""},
00115 {ARG_FLOAT, "nsig", "3.0", "", ""},
00116 {ARG_INT, "MAPMMAX", "1800", "", ""},
00117 {ARG_INT, "SINBDIVS", "720", "", ""},
00118 {ARG_FLOAT, "MAPLGMAX", "90.0", "", ""},
00119 {ARG_FLOAT, "MAPLGMIN", "-90.0", "", ""},
00120 {ARG_FLOAT, EQPOINTS, "20.0", "", ""},
00121 {ARG_FLOAT, HALFWIN, "15.0", "", ""},
00122 {ARG_FLOAT, "center", "0.0", "", ""},
00123 {ARG_INT, "checkqual", "0", "", ""},
00124 {ARG_STRING, "qualmask", "0x0", ""},
00125 {ARG_FLAG, LOSFLAG, "0", "-1", "1"},
00126 {ARG_FLAG, FORCEFLAG, "0", "-1", "1"},
00127 {ARG_FLAG, DLOGFLAG, "0", "-1", "1"},
00128 {ARG_FLOAT, NOISESIGMA, "3.0", "", ""},
00129 {ARG_FLOAT, MAXNOISEADJ, "3.0", "", ""},
00130 {ARG_INT, MINOUTLIERPTS, "4", "", ""},
00131 {ARG_INT, NBIN, "5", "", ""},
00132 {ARG_END}
00133 };
00134
00135 int DoIt(void)
00136 {
00137
00138 int newstat = 0;
00139 int status = DRMS_SUCCESS;
00140 float *synop;
00141 int *wt;
00142 char *ww;
00143 float *epts = NULL;
00144 float *losSynop = NULL;
00145 float *ps, *pm;
00146 char *odir;
00147 struct stat stbuf;
00148 float nsig;
00149 int cr, mapmmax, sinbdivs;
00150 int checkqual;
00151 unsigned int quality, qualvld, qualmask;
00152 float lgmax, lgmin;
00153 float center;
00154 float halfWindow;
00155 int len[2];
00156 int ngood, idx;
00157 int started, ended;
00158 int firstidx, lastidx;
00159 int mapmidcol, synmidcol;
00160 int cols2right, cols2left;
00161 int mrc, mlc, src, slc;
00162 int mapcols, col, row, syncol;
00163 double maprightct, mapleftct;
00164 double synleftct, synrightct;
00165 double synstart;
00166 double synend;
00167 double synstep;
00168 double subSynopStart, subSynopEnd, synRange;
00169 int SyncolStart, SyncolEnd, nsynop;
00170 char tstr[64];
00171 int mag_num = 0;
00172 TIME tstart, tstop, trot, tearth;
00173 TIME tdiff, magtime;
00174 double r,b,l,vr,vn,vw;
00175 int y,m,d;
00176 TIME tmod, delta_T = 0.0;
00177 double bearth;
00178 double carrtime;
00179 char dsname[50400], key[50400], logfn[50400], synfn[50400], wtfn[50400], losFn[50400];
00180 char eptsfn[50400];
00181 int ds, nds, noutRec;
00182 int fsn, lsn;
00183 int i,j;
00184 double statMin, statMax, statMedn, statMean, statSig, statSkew, statKurt;
00185 double smallstatMin, smallstatMax, smallstatMedn, smallstatMean, smallstatSig, smallstatSkew, smallstatKurt;
00186 int statNgood;
00187 int smallstatNgood;
00188 long long calVer;
00189 double eph[30];
00190 char historyofthemodule[2048];
00191
00192 char *cvsinfo = strdup("$Id: hmisynoptic.c,v 1.15 2014/03/05 15:30:20 yliu Exp $");
00193
00194
00195 sprintf(historyofthemodule,"Module version added -- Feb 2014; Carrington-Time conversion corrected; o2helio.c bug corrected; CRPIX, CRVAL corrected -- Jan. 2014");
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, *smallRecQuery;
00316 inRecQuery = (char *)cmdparams_get_str(&cmdparams, kRecSetIn, &status);
00317 outRecQuery = (char *)cmdparams_get_str(&cmdparams, kSeriesOut, &status);
00318 smallRecQuery = (char *)cmdparams_get_str(&cmdparams, "smallsyn", &status);
00319
00320 DRMS_RecordSet_t *inRD, *outRD, *smallRD;
00321 DRMS_Record_t *outRec, *smallRec;
00322
00323 inRD = drms_open_records(drms_env, inRecQuery, &status);
00324 if (status || inRD->n == 0)
00325 DIE("No input data found");
00326 nds = inRD->n;
00327 idx = 0;
00328 for (ds=0; ds < nds; ds++)
00329 {
00330 double clong;
00331 double cmLong;
00332 double mapct = 0.0;
00333 double mapctnew;
00334 double mapCM = 0.0;
00335 double mapCMnew;
00336 double remapLgmax = 0.0;
00337 double remapLgmin = 0.0;
00338 int orot;
00339 char t_obs[64];
00340 char *imgVersStr = NULL;
00341 int rkey = 0;
00342 int csKey = 0;
00343 float csCoeffKey;
00344 int sensAdjKey;
00345 TIME trec;
00346 char trecstr[100];
00347 DRMS_Record_t *inRec;
00348 inRec = inRD->records[ds];
00349
00350 calVer = drms_getkey_longlong(inRec, "CALVER64", &status);
00351 trec = drms_getkey_time(inRec, "T_REC", &status);
00352 sprint_time(trecstr, trec, "TAI", 0);
00353
00354
00355
00356 quality = drms_getkey_int(inRec, "QUALITY", &status);
00357
00358 if (quality & QUAL_CHECK)
00359 {
00360 printf("SKIP: Bad QUALITY = 0x%08x; T_REC=%s, rejected iRec = %d\n", quality, trecstr, ds);
00361 continue;
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
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 rkey = drms_getkey_int(inRec, "FDRADIAL", &status);
00423 if (rkey > 0)
00424 {
00425 printf(" Found radial keyword %d, ds=%d.\n", rkey, ds);
00426 if (losFound)
00427 {
00428 printf(" Attempt to use a mixture of radial and line-of-sight images.\n");
00429 printf(" Rejecting ds=%d.\n", ds);
00430 continue;
00431 }
00432 else
00433 {
00434 radialFound = 1;
00435 }
00436 }
00437 else
00438 {
00439 if (radialFound)
00440 {
00441 printf(" Attempt to use a mixture of radial and line-of-sight images.\n");
00442 printf(" Rejecting ds=%d.\n", ds);
00443 continue;
00444 }
00445 else
00446 {
00447 losFound = 1;
00448 }
00449 }
00450
00451 csKey = drms_getkey_int(inRec, kCARSTRCH, &status);
00452 csKey = (drms_ismissing_int(csKey)) ? 0 : csKey;
00453
00454 if (idx == 0)
00455 {
00456 carrStretch = csKey;
00457 }
00458 else
00459 {
00460 if (csKey != carrStretch)
00461 {
00462 printf(" Attempt to mix carr-streched and non-carr-stretched images.\n");
00463 printf(" Rejecting ds=%d.\n", ds);
00464 continue;
00465 }
00466 }
00467
00468 if (carrStretch > 0)
00469 {
00470 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_A, &status);
00471 if (idx == 0)
00472 {
00473 diffrotA = (float)csCoeffKey;
00474 }
00475 else
00476 {
00477 if (fabsf(csCoeffKey - diffrotA) > 0.001)
00478 {
00479 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00480 printf(" Rejecting ds=%d.\n", ds);
00481 continue;
00482 }
00483 }
00484
00485 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_B, &status);
00486 if (idx == 0)
00487 {
00488 diffrotB = (float)csCoeffKey;
00489 }
00490 else
00491 {
00492 if (fabsf(csCoeffKey - diffrotB) > 0.001)
00493 {
00494 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00495 printf(" Rejecting ds=%d.\n", ds);
00496 continue;
00497 }
00498 }
00499
00500 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_C, &status);
00501 if (idx == 0)
00502 {
00503 diffrotC = (float)csCoeffKey;
00504 }
00505 else
00506 {
00507 if (fabsf(csCoeffKey - diffrotC) > 0.001)
00508 {
00509 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00510 printf(" Rejecting ds=%d.\n", ds);
00511 continue;
00512 }
00513 }
00514 }
00515
00516 clong = drms_getkey_double(inRec, "CRVAL1", &status);
00517 cmLong = drms_getkey_double(inRec, "CRLN_OBS", &status);
00518 orot = drms_getkey_int(inRec, "CAR_ROT", &status);
00519 if (drms_ismissing_float(clong) || drms_ismissing_int(orot))
00520 {
00521 printf(" Bad MAP_L0 or OBS_CR in header; skipped");
00522 continue;
00523 }
00524
00525 mapctnew = (double)(orot) * 360.0 - clong - center;
00526 mapCMnew = (double)(orot) * 360.0 - cmLong - center;
00527 if (mapCMnew < mapCM)
00528 {
00529 printf(" Source image (ds=%d) has a smaller CT than previous img; skipped.\n", ds);
00530 continue;
00531 }
00532 else
00533 {
00534 mapct = mapctnew;
00535 mapCM = mapCMnew;
00536 }
00537
00538 remapLgmax = drms_getkey_double(inRec, "MAPLGMAX", &status);
00539 remapLgmin = drms_getkey_double(inRec, "MAPLGMIN", &status);
00540 imrec[idx].mapdev = (remapLgmax - remapLgmin) / 2.0;
00541 imrec[idx].recno = drms_getkey_int(inRec, "I_DREC", &status);
00542 imrec[idx].mapct = mapct;
00543 imrec[idx].mapCM = mapCM;
00544 imrec[idx].ds = ds;
00545 strcpy(t_obs, drms_getkey_string(inRec, "T_REC", &status));
00546 imrec[idx].tobs = sscan_time(t_obs);
00547 imrec[idx].tmin = MAX(mapCM - halfWindow, mapct + center - imrec[idx].mapdev);
00548 imrec[idx].tmax = MIN(mapCM + halfWindow, mapct + center + imrec[idx].mapdev);
00549 ++idx;
00550
00551 }
00552 ngood = idx;
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567 printf("\n######### SECOND PASS #################\n");
00568
00569 if (!(wt = (int *) calloc(len[0],4)))
00570 DIE("MALLOC_FAILED");
00571 if (!(ww = (char *) malloc(len[0]*len[1]*2)))
00572 DIE("MALLOC_FAILED");
00573 if (!(epts = (float *) malloc(len[0] * len[1] * sizeof(float))))
00574 DIE("MALLOC_FAILED");
00575 if (!(synop = (float *) malloc(len[0]*len[1]*4)))
00576 DIE("MALLOC_FAILED");
00577 if (los && radialFound && !(losSynop = (float *)malloc(len[0] * len[1] * sizeof(float))))
00578 DIE("MALLOC_FAILED");
00579 sortedMagCol = (MagCol_t **)malloc(sizeof(MagCol_t *) * len[0]);
00580 if (!sortedMagCol)
00581 DIE("MALLOC_FAILED");
00582 memset(sortedMagCol, 0, sizeof(MagCol_t *) * len[0]);
00583
00584
00585 started = 0;
00586 ended = 0;
00587 nxtSyncol = len[0] - 1;
00588 synRange = 60.0 ;
00589
00590
00591 nsynop = rint(360.0/synRange);
00592
00593
00594
00595
00596
00597 for (ds = 0; ds < nsynop; ds++)
00598 {
00599 subSynopStart = synstart + ds * synRange;
00600 subSynopEnd = synstart + (ds + 1) * synRange - synstep;
00601 SyncolStart = rint((synend - subSynopStart) / synstep);
00602 SyncolEnd = rint((synend - subSynopEnd) / synstep);
00603 printf("ds=%d\n", ds);
00604
00605 for (idx = 0; idx < ngood; idx++)
00606 {
00607 int recno;
00608 int col;
00609 float magtype = 0.0;
00610 char *mType = NULL;
00611 float equivPts = 0.0;
00612 char t_mag[64];
00613 TIME tmag;
00614 DRMS_Record_t *inRec;
00615 DRMS_Segment_t *inSeg = NULL;
00616 DRMS_Array_t *inArray;
00617 inRec = inRD->records[imrec[idx].ds];
00618
00619 if (imrec[idx].tmax <= subSynopStart || imrec[idx].tmin >= subSynopEnd) continue;
00620
00621
00622
00623
00624 maprightct = MAX(subSynopStart, imrec[idx].tmin);
00625 mapleftct = MIN(subSynopEnd, imrec[idx].tmax);
00626 mapcols = drms_getkey_int(inRec, "MAPMMAX", &status) + 1;
00627 mapmidcol = rint((mapcols - 1.0) / 2 + center / synstep);
00628 cols2right = rint((imrec[idx].mapct - maprightct) / synstep);
00629 cols2left = rint((imrec[idx].mapct - mapleftct) / synstep);
00630 synmidcol = rint((synend - imrec[idx].mapct) / synstep);
00631
00632
00633
00634
00635
00636 mrc = mapmidcol + cols2right;
00637 mlc = mapmidcol + cols2left;
00638
00639
00640 src = synmidcol + cols2right;
00641 slc = synmidcol + cols2left;
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654 equivPts = 1.0;
00655 inSeg = drms_segment_lookupnum(inRec, 0);
00656 inArray = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00657
00658 if (status)
00659 {
00660 printf(" No data file found, status=%d\n", status);
00661 drms_free_array(inArray);
00662 continue;
00663 }
00664
00665 for (col = mlc; col <= mrc; ++col)
00666 {
00667 MagCol_t mMagCol;
00668
00669 mMagCol.dist = ((col - mapmidcol) * synstep + imrec[idx].mapct) - imrec[idx].mapCM;
00670 mMagCol.datacol = (float *)malloc(sizeof(float) * len[1]);
00671 mMagCol.equivPts = equivPts;
00672 mMagCol.ds = imrec[idx].ds;
00673 mMagCol.col = col;
00674 mMagCol.valid = 1;
00675
00676 pm = (float *)inArray->data + col;
00677 ps = mMagCol.datacol;
00678
00679 for (row = 0; row < len[1]; ++row)
00680 {
00681 *ps = *pm;
00682 pm += mapcols;
00683 ps++;
00684 }
00685
00686 syncol = col + slc - mlc;
00687
00688 if (!sortedMagCol[syncol])
00689 {
00690 sortedMagCol[syncol] = (MagCol_t *)malloc(sizeof(MagCol_t) * nStackMags);
00691 if (!sortedMagCol[syncol]) DIE("MALLOC_FAILED");
00692 memset(sortedMagCol[syncol], 0, sizeof(MagCol_t) * nStackMags);
00693 }
00694
00695 if (wt[syncol] > 0 && (wt[syncol] % nStackMags == 0))
00696 {
00697 int frames = 1 + (wt[syncol] / nStackMags);
00698 MagCol_t *mc = (MagCol_t *)malloc(sizeof(MagCol_t) * frames * nStackMags);
00699 memcpy(mc,
00700 sortedMagCol[syncol],
00701 sizeof(MagCol_t) * (frames - 1) * nStackMags);
00702 free(sortedMagCol[syncol]);
00703 sortedMagCol[syncol] = mc;
00704 }
00705
00706 sortedMagCol[syncol][wt[syncol]] = mMagCol;
00707 wt[syncol]++;
00708
00709 }
00710 drms_free_array(inArray);
00711 }
00712
00713 calcsynret = CalcSynCols(SyncolStart,
00714 SyncolEnd,
00715 -1,
00716 sortedMagCol,
00717 synop,
00718 wt,
00719 ww,
00720 epts,
00721 losSynop,
00722 len,
00723 center,
00724 nEquivPtsReq,
00725 los,
00726 radialFound,
00727 sensAdj,
00728 noiseS,
00729 nsig,
00730 maxNoiseAdj,
00731 minOutPts,
00732 dlog);
00733
00734 printf("ds=%d\n", ds);
00735 FreeMagColsData(SyncolStart, SyncolEnd, -1, wt, sortedMagCol);
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749 if (calcsynret) DIE(" Cannot be finished up!");
00750 }
00751
00752 if (!ended) lastidx = ngood-1;
00753 magtime = imrec[(int)(ngood/2)].tobs;
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769 float *smallSynop = NULL, *smallEpts = NULL;
00770 int smallDims[2], xout, yout;
00771
00772 xout = len[0]/nbin; yout = len[1]/(nbin-1);
00773 if (!(smallSynop = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00774 if (!(smallEpts = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00775 smallDims[0] = xout; smallDims[1] = yout;
00776
00777
00778
00779
00780
00781 frebinbox(synop, smallSynop, len[0], len[1], nbin, nbin-1);
00782 frebinbox(epts, smallEpts, len[0], len[1], nbin, nbin-1);
00783
00784
00785
00786 if (fstats(len[0]*len[1], synop, &statMin, &statMax, &statMedn, &statMean, &statSig,
00787 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00788
00789 if (fstats(xout*yout, smallSynop, &smallstatMin, &smallstatMax, &smallstatMedn, &smallstatMean, &smallstatSig,
00790 &smallstatSkew, &smallstatKurt, &smallstatNgood)) printf("\n Statistics computation failed\n");
00791
00792
00793
00794 outRD = drms_create_records(drms_env, 1, outRecQuery, DRMS_PERMANENT, &status);
00795 if (status)
00796 DIE("Output record not created");
00797 smallRD = drms_create_records(drms_env, 1, smallRecQuery, DRMS_PERMANENT, &status);
00798 if (status)
00799 DIE("Output record not created");
00800
00801 DRMS_Segment_t *outSeg, *smallSeg;
00802 DRMS_Array_t *outArray, *smallArray;
00803 int outDims[2] = {len[0],len[1]};
00804 outRec = outRD->records[0];
00805 smallRec = smallRD->records[0];
00806
00807
00808 outSeg = drms_segment_lookupnum(outRec, 0);
00809 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, synop, &status);
00810 status = drms_segment_write(outSeg, outArray, 0);
00811 if (status)
00812 DIE("problem writing file");
00813 drms_free_array(outArray);
00814
00815
00816 outSeg = drms_segment_lookupnum(outRec, 1);
00817 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, epts, &status);
00818 status = drms_segment_write(outSeg, outArray, 0);
00819 if (status)
00820 DIE("problem writing file");
00821 drms_free_array(outArray);
00822
00823
00824 smallSeg = drms_segment_lookupnum(smallRec, 0);
00825 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallSynop, &status);
00826 status = drms_segment_write(smallSeg, smallArray, 0);
00827 if (status)
00828 DIE("problem writing file");
00829 drms_free_array(smallArray);
00830
00831
00832
00833 smallSeg = drms_segment_lookupnum(smallRec, 1);
00834 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallEpts, &status);
00835 status = drms_segment_write(smallSeg, smallArray, 0);
00836 if (status)
00837 DIE("problem writing file");
00838 drms_free_array(smallArray);
00839
00840
00841
00842
00843
00844 sprint_ut(tstr, CURRENT_SYSTEM_TIME);
00845 sscanf(tstr, "%d.%d.%d", &y, &m, &d);
00846 sprintf(tstr, "%04d-%02d-%02d", y, m, d);
00847 drms_setkey_string(outRec, "DATE", tstr);
00848
00849
00850
00851
00852 drms_setkey_string(outRec, "HISTORY", historyofthemodule);
00853 drms_setkey_string(outRec, "BLD_VERS", jsoc_version);
00854 status = drms_setkey_string(outRec, "CODEVER", cvsinfo);
00855 drms_setkey_string(outRec, "CTYPE1", "CRLN-CEA");
00856 drms_setkey_string(outRec, "CTYPE2", "CRLT-CEA");
00857
00858 i=len[0]-(len[0]/2+1.0)+1.0;
00859
00860
00861
00862
00863
00864 drms_setkey_double(outRec, "CRPIX1", i);
00865
00866 l=(len[1]+1.0)/2;
00867 drms_setkey_double(outRec, "CRPIX2", l);
00868
00869
00870 carrtime = (cr-1)*360.0 + 180.0;
00871 drms_setkey_double(outRec, "CRVAL1", carrtime);
00872 i=0.0;
00873 drms_setkey_double(outRec, "CRVAL2", i);
00874 l=-360.0/len[0];
00875 drms_setkey_double(outRec, "CDELT1", l);
00876 drms_setkey_double(outRec, "CDELT2", 1.0/sinbdivs);
00877 drms_setkey_string(outRec, "CUNIT1", "degree");
00878 drms_setkey_string(outRec, "CUNIT2", "Sine Latitude");
00879 drms_setkey_string(outRec, "WCSNAME", "Carrington Heliographic");
00880
00881
00882 sprint_at(tstr, trot - delta_T);
00883
00884
00885
00886
00887
00888
00889 i = len[0]*len[1];
00890 drms_setkey_int(outRec, "TOTVALS", i);
00891 drms_setkey_int(outRec, "DATAVALS", statNgood);
00892 i = len[0]*len[1]-statNgood;
00893 drms_setkey_int(outRec, "MISSVALS", i);
00894 drms_setkey_double(outRec, "DATAMIN", statMin);
00895 drms_setkey_double(outRec, "DATAMAX", statMax);
00896 drms_setkey_double(outRec, "DATAMEDN", statMedn);
00897 drms_setkey_double(outRec, "DATAMEAN", statMean);
00898 drms_setkey_double(outRec, "DATARMS", statSig);
00899 drms_setkey_double(outRec, "DATASKEW", statSkew);
00900 drms_setkey_double(outRec, "DATAKURT", statKurt);
00901
00902
00903 drms_setkey_string(outRec, "T_OBS", tstr);
00904 drms_setkey_string(outRec, "T_ROT", tstr);
00905 sprint_at(tstr, tstart - delta_T);
00906 drms_setkey_string(outRec, "T_START", tstr);
00907 sprint_at(tstr, tstop - delta_T);
00908 drms_setkey_string(outRec, "T_STOP", tstr);
00909
00910
00911 drms_setkey_int(outRec, "CAR_ROT", cr);
00912 drms_setkey_double(outRec, "CARRTIME", carrtime);
00913 solephem(trot, eph);
00914
00915 drms_setkey_double(outRec, "B0_ROT", eph[9]/RADSINDEG);
00916 solephem(tstart, eph);
00917
00918 drms_setkey_double(outRec, "B0_FRST", eph[9]/RADSINDEG);
00919 solephem(tstop, eph);
00920
00921 drms_setkey_double(outRec, "B0_LAST", eph[9]/RADSINDEG);
00922
00923 l=(cr-1)*360.0;
00924 drms_setkey_double(outRec, "LON_FRST", l);
00925 l=cr*360.0-360.0/len[0];
00926 drms_setkey_double(outRec, "LON_LAST", l);
00927 l=-360.0/len[0];
00928 drms_setkey_double(outRec, "LON_STEP", l);
00929 l=center;
00930 drms_setkey_double(outRec, "W_OFFSET", l);
00931 drms_setkey_string(outRec, "W_WEIGHT", "Even");
00932 i=lastidx-firstidx+1;
00933 drms_setkey_int(outRec, "IMG_NUM", i);
00934 sprint_at(tstr, imrec[firstidx].tobs);
00935 drms_setkey_string(outRec, "IMG_FRST", tstr);
00936 sprint_at(tstr, imrec[lastidx].tobs);
00937 drms_setkey_string(outRec, "IMG_LAST", tstr);
00938 sprint_at(tstr, magtime);
00939 drms_setkey_string(outRec, "IMG_ROT", tstr);
00940 drms_setkey_float(outRec, HWNWIDTH, halfWindow);
00941 drms_setkey_float(outRec, EQPOINTS, nEquivPtsReq);
00942 drms_setkey_float(outRec, "NSIGMA", nsig);
00943 drms_setkey_longlong(outRec, "CALVER64", calVer);
00944
00945
00946
00947
00948 if (carrStretch > 0)
00949 {
00950 drms_setkey_int(outRec, kCARSTRCH, carrStretch);
00951 drms_setkey_float(outRec, kDIFROT_A, diffrotA);
00952 drms_setkey_float(outRec, kDIFROT_B, diffrotB);
00953 drms_setkey_float(outRec, kDIFROT_C, diffrotC);
00954 }
00955
00956
00957
00958
00959
00960
00961 sprint_ut(tstr, CURRENT_SYSTEM_TIME);
00962 sscanf(tstr, "%d.%d.%d", &y, &m, &d);
00963 sprintf(tstr, "%04d-%02d-%02d", y, m, d);
00964 drms_setkey_string(smallRec, "DATE", tstr);
00965
00966
00967
00968
00969 drms_setkey_string(smallRec, "HISTORY", historyofthemodule);
00970 drms_setkey_string(smallRec, "BLD_VERS", jsoc_version);
00971 status = drms_setkey_string(smallRec, "CODEVER", cvsinfo);
00972 drms_setkey_string(smallRec, "CTYPE1", "CRLN-CEA");
00973 drms_setkey_string(smallRec, "CTYPE2", "CRLT-CEA");
00974
00975 double tmp;
00976 tmp=xout - ((180.0-((nbin+1)/2.0-1.0)*(360.0/len[0]))/(360.0/xout)+1.0) + 1.0;
00977
00978
00979
00980 drms_setkey_double(smallRec, "CRPIX1", tmp);
00981 tmp=(yout+1.0)/2;
00982 drms_setkey_double(smallRec, "CRPIX2", tmp);
00983
00984 carrtime = (cr-1)*360.0 + 180.0;
00985 drms_setkey_double(smallRec, "CRVAL1", carrtime);
00986 tmp=0.0;
00987 drms_setkey_double(smallRec, "CRVAL2", tmp);
00988 l=-360.0/xout;
00989 drms_setkey_double(smallRec, "CDELT1", l);
00990 drms_setkey_double(smallRec, "CDELT2", nbin*1.0/sinbdivs);
00991 drms_setkey_string(smallRec, "CUNIT1", "degree");
00992 drms_setkey_string(smallRec, "CUNIT2", "Sine Latitude");
00993 drms_setkey_string(smallRec, "WCSNAME", "Carrington Heliographic");
00994
00995
00996 sprint_at(tstr, trot - delta_T);
00997
00998
00999
01000
01001
01002
01003 i = xout*yout;
01004 drms_setkey_int(smallRec, "TOTVALS", i);
01005 drms_setkey_int(smallRec, "DATAVALS", smallstatNgood);
01006 i = xout*yout-smallstatNgood;
01007 drms_setkey_int(smallRec, "MISSVALS", i);
01008 drms_setkey_double(smallRec, "DATAMIN", smallstatMin);
01009 drms_setkey_double(smallRec, "DATAMAX", smallstatMax);
01010 drms_setkey_double(smallRec, "DATAMEDN", smallstatMedn);
01011 drms_setkey_double(smallRec, "DATAMEAN", smallstatMean);
01012 drms_setkey_double(smallRec, "DATARMS", smallstatSig);
01013 drms_setkey_double(smallRec, "DATASKEW", smallstatSkew);
01014 drms_setkey_double(smallRec, "DATAKURT", smallstatKurt);
01015
01016
01017
01018 drms_setkey_string(smallRec, "T_OBS", tstr);
01019 drms_setkey_string(smallRec, "T_ROT", tstr);
01020 sprint_at(tstr, tstart - delta_T);
01021 drms_setkey_string(smallRec, "T_START", tstr);
01022 sprint_at(tstr, tstop - delta_T);
01023 drms_setkey_string(smallRec, "T_STOP", tstr);
01024
01025
01026 drms_setkey_int(smallRec, "CAR_ROT", cr);
01027 drms_setkey_double(smallRec, "CARRTIME", carrtime);
01028 solephem(trot, eph);
01029
01030 drms_setkey_double(smallRec, "B0_ROT", eph[9]/RADSINDEG);
01031
01032 solephem(tstart, eph);
01033 drms_setkey_double(smallRec, "B0_FRST", eph[9]/RADSINDEG);
01034
01035 solephem(tstop, eph);
01036 drms_setkey_double(smallRec, "B0_LAST", eph[9]/RADSINDEG);
01037
01038 l=(cr-1)*360.0+((nbin+1)/2.0-1.0)*(360.0/len[0]);
01039 drms_setkey_double(smallRec, "LON_FRST", l);
01040 l=cr*360.0-360.0/len[0]-((nbin+1)/2.0-1.0)*(360.0/len[0]);
01041 drms_setkey_double(smallRec, "LON_LAST", l);
01042 l=-360.0/xout;
01043 drms_setkey_double(smallRec, "LON_STEP", l);
01044 l=center;
01045 drms_setkey_double(smallRec, "W_OFFSET", l);
01046 drms_setkey_string(smallRec, "W_WEIGHT", "Even");
01047 i=lastidx-firstidx+1;
01048 drms_setkey_int(smallRec, "IMG_NUM", i);
01049 sprint_at(tstr, imrec[firstidx].tobs);
01050 drms_setkey_string(smallRec, "IMG_FRST", tstr);
01051 sprint_at(tstr, imrec[lastidx].tobs);
01052 drms_setkey_string(smallRec, "IMG_LAST", tstr);
01053 sprint_at(tstr, magtime);
01054 drms_setkey_string(smallRec, "IMG_ROT", tstr);
01055 drms_setkey_float(smallRec, HWNWIDTH, halfWindow);
01056 drms_setkey_float(smallRec, EQPOINTS, nEquivPtsReq);
01057 drms_setkey_float(smallRec, "NSIGMA", nsig);
01058 drms_setkey_longlong(smallRec, "CALVER64", calVer);
01059
01060
01061 if (carrStretch > 0)
01062 {
01063 drms_setkey_int(smallRec, kCARSTRCH, carrStretch);
01064 drms_setkey_float(smallRec, kDIFROT_A, diffrotA);
01065 drms_setkey_float(smallRec, kDIFROT_B, diffrotB);
01066 drms_setkey_float(smallRec, kDIFROT_C, diffrotC);
01067 }
01068
01069 if (sortedMagCol)
01070 {
01071 for (i = 0; i < len[0]; i++)
01072 {
01073 if (sortedMagCol[i])
01074 {
01075 free(sortedMagCol[i]);
01076 }
01077 }
01078 free(sortedMagCol);
01079 }
01080
01081 drms_close_records(inRD, DRMS_FREE_RECORD);
01082 drms_close_records(outRD, DRMS_INSERT_RECORD);
01083 drms_close_records(smallRD, DRMS_INSERT_RECORD);
01084 return 0;
01085 }
01086
01087 int CalcSynCols(int start,
01088 int end,
01089 int incr,
01090 MagCol_t **smc,
01091 float *synop,
01092 int *wt,
01093 char *ww,
01094 float *epts,
01095 float *losSynop,
01096 int *len,
01097 float center,
01098 float nEquivPtsReq,
01099 int los,
01100 int radialFound,
01101 float sensAdj,
01102 float noiseS,
01103 float nsig,
01104 float maxNoiseAdj,
01105 int minOutPts,
01106 int dlog)
01107 {
01108 float midSynRow;
01109 float cosrho;
01110
01111
01112
01113
01114
01115 float cosCenter;
01116 char *rejDescFormat;
01117 int col;
01118 int row;
01119 int i;
01120 int j;
01121 static int nrej = 0;
01122 int marked;
01123 float nEquivPts;
01124
01125 midSynRow = (float)(len[1] - 1) / 2.0;
01126 cosCenter = cos(center * M_PI / 180);
01127 rejDescFormat = " magnetogram: ds=%d, sn=%d, magcol=%d, magrow=%d\n";
01128 if (minOutPts < 2) minOutPts = 2;
01129
01130
01131 for (col = start; incr > 0 ? col <= end : col >= end; col += incr)
01132 {
01133 float *val = (float *)malloc(sizeof(float) * wt[col]);
01134 float *ept = (float *)malloc(sizeof(float) * wt[col]);
01135 MagCol_t *pcols = NULL;
01136 marked = 0;
01137
01138 if (dlog)
01139 {
01140 pcols = (MagCol_t *)malloc(sizeof(MagCol_t) * wt[col]);
01141 memset(pcols, 0, sizeof(MagCol_t) * wt[col]);
01142 }
01143
01144 if (!val || !ept) printf("MALLOC FAILED");
01145
01146
01147 SortMagCols(smc[col], wt[col]);
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165 for (row = 0; row < len[1]; ++row)
01166 {
01167 float sinLat = (row - midSynRow) / midSynRow;
01168 float cosLat = sqrt(1 - sinLat * sinLat);
01169 float sum;
01170 int npts;
01171 float sumfinal = 0.0;
01172 int nptsfinal = 0;
01173 int Maxnepts = nEquivPtsReq + 10;
01174
01175 if (radialFound) cosrho = cosLat * cosCenter;
01176
01177 sum = 0.0;
01178 npts = 0;
01179 j = 0;
01180 nEquivPts = 0.0;
01181
01182
01183
01184
01185
01186 for (i = 0; i < wt[col] && nEquivPts < Maxnepts; i++)
01187 {
01188 float magVal = *(smc[col][i].datacol + row);
01189 if (!drms_ismissing_float(magVal))
01190 {
01191 nEquivPts += smc[col][i].equivPts;
01192 sum += magVal;
01193 val[j] = magVal;
01194 ept[j] = smc[col][i].equivPts;
01195
01196 if (dlog && pcols) pcols[j] = smc[col][i];
01197
01198 j++;
01199 npts++;
01200 }
01201 }
01202
01203
01204 if (npts > 1 && nsig > 0.0) {
01205 double avg = 0.0;
01206 double ssqr = 0.0;
01207 double sig;
01208 float med;
01209 int nStatPts = 0;
01210 float *statVals;
01211 float noiseLevel;
01212 float *outlier = NULL;
01213 int statsDone = 0;
01214 int sIdx = 0;
01215 float dev = 0.0;
01216 int nPtsB4Rej = npts;
01217
01218
01219
01220 noiseLevel = kNOISE_EQ * noiseS * sensAdj;
01221 if (radialFound) noiseLevel = noiseLevel * MIN(1 / cosrho, maxNoiseAdj);
01222
01223
01224 statVals = (float *)malloc(sizeof(float) * npts);
01225 if (!statVals) printf("MALLOC_FAILED");
01226
01227 memcpy(statVals, val, sizeof(float) * npts);
01228 nStatPts = magStats(&statVals, npts, sum, minOutPts, &avg, &med);
01229
01230 for (j = 0; j < nPtsB4Rej; ++j)
01231 {
01232
01233
01234 if (!statsDone)
01235 {
01236 for (sIdx = 0; sIdx < nStatPts; sIdx++)
01237 {
01238 ssqr += statVals[sIdx] * statVals[sIdx];
01239 }
01240
01241 sig = sqrt((ssqr - nStatPts * avg * avg) / (nStatPts - 1));
01242 statsDone = 1;
01243 }
01244
01245 dev = fabs(val[j] - med);
01246 if (nptsfinal >= nEquivPtsReq) break;
01247 if (dev < nsig * sig)
01248 {
01249 sumfinal += val[j];
01250 nptsfinal += 1;
01251
01252 --npts;
01253 ++nrej;
01254 if (dlog)
01255 {
01256 char rejDescBuf[128];
01257
01258 snprintf(rejDescBuf,
01259 sizeof(rejDescBuf),
01260 rejDescFormat,
01261 pcols[j].ds,
01262 pcols[j].col,
01263 row);
01264
01265 }
01266
01267 }
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290 }
01291
01292 free(statVals);
01293 }
01294
01295
01296
01297
01298
01299 if (nptsfinal)
01300 {
01301
01302 float synVal = sumfinal/nptsfinal;
01303 float minVal = (SHRT_MIN + 1);
01304 float maxVal = (SHRT_MAX - 1);
01305
01306
01307
01308
01309 if (synVal < minVal)
01310 {
01311 synop[row * len[0] + col] = minVal;
01312 }
01313 else if (synVal > maxVal)
01314 {
01315 synop[row * len[0] + col] = maxVal;
01316 }
01317 else
01318 {
01319 synop[row * len[0] + col] = synVal;
01320 }
01321 }
01322 else
01323 {
01324 synop[row * len[0] + col] = DRMS_MISSING_FLOAT;
01325 }
01326
01327 ww[row * len[0] + col] = npts;
01328
01329 epts[row * len[0] + col] = nptsfinal;
01330 if (los && radialFound)
01331 {
01332 int offset = row * len[0] + col;
01333 if (drms_ismissing_float(synop[offset]))
01334 {
01335 losSynop[offset] = DRMS_MISSING_FLOAT;
01336 }
01337 else
01338 {
01339 losSynop[offset] = synop[offset] * cosLat;
01340 }
01341 }
01342 }
01343
01344 free(val);
01345 val = NULL;
01346 free(ept);
01347 ept = NULL;
01348
01349 if (pcols)
01350 {
01351 free(pcols);
01352 pcols = NULL;
01353 }
01354 }
01355 return 0;
01356 }
01357
01358 int cmp(const void *a, const void *b)
01359 {
01360 float aa = *(float *)a;
01361 float bb = *(float *)b;
01362 return (aa<bb) ? -1 : (aa==bb) ? 0 : 1;
01363 }
01364
01365
01366 int magStats(float **val, int npts, double sum, int outThreshold, double *avg, float *med)
01367 {
01368 int actPts = npts;
01369 float *out = NULL;
01370 float *in = *val;
01371 int idx = 0;
01372 int iOut = 0;
01373 float medianInt = 0.0;
01374 float first;
01375 float last;
01376
01377 qsort((void *)in, npts, sizeof(float), cmp);
01378
01379 if (npts > outThreshold && npts > 1)
01380 {
01381 actPts = npts - 1;
01382 out = malloc(sizeof(float) * actPts);
01383
01384 medianInt = in[npts/2];
01385
01386 first = in[0];
01387 last = in[npts - 1];
01388
01389 if (fabs(first - medianInt) <= fabs(last - medianInt))
01390 {
01391 idx = 0;
01392 sum -= last;
01393 }
01394 else
01395 {
01396 idx = 1;
01397 sum -= first;
01398 }
01399
01400 for (iOut = 0; idx < npts && iOut < actPts; idx++, iOut++)
01401 {
01402 out[iOut] = in[idx];
01403 }
01404
01405 free(in);
01406 *val = out;
01407 in = *val;
01408 }
01409
01410 *avg = sum / actPts;
01411 *med = in[actPts / 2];
01412
01413 return actPts;
01414 }
01415
01416 int CmpMagCol(const void *a, const void *b)
01417 {
01418 MagCol_t *aa = (MagCol_t *)a;
01419 MagCol_t *bb = (MagCol_t *)b;
01420
01421 if (aa->valid && bb->valid)
01422 {
01423 return (fabsf(aa->dist) < fabsf(bb->dist)) ? -1 :
01424 ((fabsf(aa->dist) == fabsf(bb->dist)) ? 0 : 1);
01425 }
01426 else if (aa->valid)
01427 {
01428 return -1;
01429 }
01430 else if (bb->valid)
01431 {
01432 return 1;
01433 }
01434 else
01435 {
01436 return 0;
01437 }
01438 }
01439
01440 void SortMagCols(MagCol_t *mc, int nelem)
01441 {
01442
01443 MagCol_t *mc_cpy = NULL;
01444 int i;
01445 mc_cpy = (MagCol_t *)malloc(sizeof(MagCol_t) * nelem);
01446 for (i = 0; i < nelem; i++)
01447 mc_cpy[i] = mc[i];
01448 m_sort(mc, mc_cpy, nelem);
01449 free(mc_cpy);
01450 }
01451
01452 void m_sort(MagCol_t *mc, MagCol_t *mc_cpy, int sz)
01453 {
01454 int n, i, j, mid;
01455 mid = sz/2;
01456 if (mid > 1) m_sort(mc_cpy, mc, mid);
01457 if (sz-mid > 1) m_sort(mc_cpy + mid, mc + mid, sz - mid);
01458
01459
01460 MagCol_t *mc_cpy2 = mc_cpy + mid;
01461 for (n=0, i=0, j=0; i<mid && j<sz-mid; n++)
01462 mc[n] = (fabs(mc_cpy[i].dist) < fabs(mc_cpy2[j].dist)) ? mc_cpy[i++] : mc_cpy2[j++];
01463 while (i<mid)
01464 mc[n++] = mc_cpy[i++];
01465 while (j<sz-mid)
01466 mc[n++] = mc_cpy2[j++];
01467 }
01468
01469 void frebinbox(float *image_in, float *image_out, int nx, int ny, int nbinx, int nbiny)
01470 {
01471 int nxout, nyout;
01472 int ii, jj, i, j;
01473 nxout = nx / nbinx; nyout = ny / nbiny;
01474 for (j = 0; j < nyout; j++) {
01475 int yOff, jy;
01476 jy = j * nbiny;
01477 for (i = 0; i < nxout; i++) {
01478 int ix;
01479 ix = i * nbinx;
01480 float aveval = 0.0;
01481 int number = 0;
01482 for (jj = 0; jj < nbiny; jj++) {
01483 yOff = (jy + jj) * nx;
01484 for (ii = 0; ii < nbinx; ii++) {
01485 int iData = yOff + ix + ii;
01486 if (!(isnan(image_in[iData]))) {
01487 aveval += image_in[iData];
01488 number += 1;
01489 }
01490 }
01491 }
01492 image_out[j*nxout+i] = aveval/number;
01493 }
01494 }
01495 }
01496
01497
01498
01499
01500
01501
01502
01503
01504
01505
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
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557
01558
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588 void FreeMagColsData(int start, int end, int incr, int *n, MagCol_t **mc)
01589 {
01590 if (mc)
01591 {
01592 float *data = NULL;
01593 int cIdx;
01594 int idx;
01595
01596 for (cIdx = start; incr > 0 ? cIdx <= end : cIdx >= end; cIdx += incr)
01597 {
01598 for (idx = 0; idx < n[cIdx]; idx++)
01599 {
01600 data = mc[cIdx][idx].datacol;
01601 if (data)
01602 {
01603 free(data);
01604 mc[cIdx][idx].datacol = NULL;
01605 mc[cIdx][idx].valid = 0;
01606 }
01607 }
01608 }
01609 }
01610 }
01611
01612 TIME CarringtonTime(int crot, double L)
01613 {
01614 double eph[30];
01615 double CT, clong=0.0;
01616 double err, CTp50m, CTm50m;
01617 TIME t;
01618 char *stime;
01619 char tstr[64];
01620 CT = 0.0;
01621 static TIME T1853 = 0.0, delta_T = 0.0;
01622 T1853 = sscan_time("1853.11.09_22:27:24_UTC");
01623
01624 delta_T = sscan_time("1977.01.01_00:00:00_TAI") - sscan_time("1601.01.01_00:00:00_UT");
01625
01626 CT = 360.0 * crot - L;
01627 t = (C2 + CT * C1) * SID;
01628
01629
01630 solephem(t, eph);
01631 err = CT - eph[8];
01632 solephem(t+6*3600.0, eph);
01633 CTp50m = eph[8];
01634 solephem(t-6*3600.0, eph);
01635 CTm50m = eph[8];
01636
01637 t += 12*3600 * (err/(CTp50m - CTm50m));
01638 solephem(t, eph);
01639 err = CT - eph[8];
01640 solephem(t+300.0, eph);
01641 CTp50m = eph[8];
01642 solephem(t-300.0, eph);
01643 CTm50m = eph[8];
01644
01645 t += 600 * (err/(CTp50m - CTm50m));
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659 return(t);
01660 }
01661
01662
01663
01664
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680
01681
01682
01683
01684
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694
01695
01696
01697
01698
01699
01700
01701
01702
01703
01704
01705
01706
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
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