00001
00007
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <time.h>
00011 #include "jsoc_main.h"
00012 #include "fstats.h"
00013
00014 char *module_name = "mdidailysynframe";
00015
00016 #define DIE(msg) {fflush(stdout); fprintf(stderr, "%s, status = %d \n", msg, status); return(status);}
00017 #define PARAMETER_ERROR(PNAME)
00018 #define PI 4.0 * atan(1.0)
00019
00020 ModuleArgs_t module_args[] =
00021 {
00022 {ARG_STRING, "in", "NOTSPECIFIED", "in"},
00023 {ARG_STRING, "out", "NOTSPECIFIED", "out"},
00024 {ARG_STRING, "synoptic", "NOTSPECIFIED", "synoptic"},
00025 {ARG_STRING, "drmethod", "NOCORRDIFFROT", "drmethod"},
00026 {ARG_INT, "magresoln", "NOTSPECIFIED", "magresoln"},
00027 {ARG_INT, "synresoln", "NOTSPECIFIED", "synresoln"},
00028 {ARG_STRING, "qualmask", "0x00000200", ""},
00029 {ARG_INT, "xx1", "-1", "xx1"},
00030 {ARG_INT, "yy1", "-1", "yy1"},
00031 {ARG_END}
00032 };
00033
00034 float zgrid(int jph, int ith, int cmp, int rup, int dbl,
00035 float phd[], float thd[], float lad[], float cth[],
00036 float sth[], float csc[], float scs[]);
00037
00038 int DoIt(void)
00039 {
00040 DRMS_RecordSet_t *inRS, *inRSfinal;
00041 DRMS_Record_t *inRec, *inRecfinal, *outRec = NULL;
00042 DRMS_Segment_t *inSeg, *inSegfinal, *outSeg;
00043 DRMS_Array_t *inArray, *inArrayfinal;
00044 TIME t_rec, t_rec0;
00045 TIME halfw = 43200.0;
00046 char *t_window = "1440m";
00047 char *inQueryfinal, *trec_str = NULL;
00048 float crlt, crln;
00049
00050 double clog0;
00051 float aa, bb, cc, dd, ee;
00052 int status = DRMS_SUCCESS, nrecs, irec;
00053 int i, j, crn;
00054 int xdim_syn, ydim_syn, xmg, ymg;
00055 char sdatestime[100], sdatetmp[100], timetmp[100];
00056 unsigned int quality, qualvld, qualmask, qualmask1, qualmask2, qualmask3;
00057
00058 memset(sdatetmp, 0, sizeof(sdatetmp));
00059 memset(timetmp, 0, sizeof(timetmp));
00060 memset(sdatestime, 0, sizeof(sdatestime));
00061
00062 char *sdate, *stime, *inQuery, *outQuery, *synQuery;
00063 char *drmethod;
00064 int magresoln = params_get_int(&cmdparams, "magresoln");
00065 int synresoln = params_get_int(&cmdparams, "synresoln");
00066 int xx1 = params_get_int(&cmdparams, "xx1");
00067 int yy1 = params_get_int(&cmdparams, "yy1");
00068
00069 inQuery = (char *)params_get_str(&cmdparams, "in");
00070 outQuery = (char *)params_get_str(&cmdparams, "out");
00071 synQuery = (char *)params_get_str(&cmdparams, "synoptic");
00072 drmethod = (char *)params_get_str(&cmdparams, "drmethod");
00073 qualmask = strtoul(cmdparams_get_str(&cmdparams, "qualmask", &status), (char **) 0, 0);
00074 qualvld = strtoul("0x00000201", (char **) 0, 0);
00075
00076 qualmask1 = strtoul("0x00000000", (char **) 0, 0);
00077 qualmask3 = strtoul("0x00000200", (char **) 0, 0);
00078 qualmask2 = strtoul("0x00000201", (char **) 0, 0);
00079
00080
00081
00082 aa = 13.1988; bb = 0.0; cc = 0.0;
00083 if (strcmp(drmethod, "Meunier") == 0)
00084 {
00085 aa = 13.562; bb = -2.04; cc = -1.4875;
00086 }
00087
00088 if (strcmp(drmethod, "Phil") == 0)
00089 {
00090 aa = 2.917; bb = -0.40; cc = -0.40;
00091 dd = 0.202006;
00092 aa = aa/dd; bb = bb/dd; cc=cc/dd;
00093 ee = 0.930505;
00094 aa = aa*ee; bb = bb*ee; cc = cc*ee;
00095 }
00096
00097 if (strcmp(drmethod, "Pevtsov") == 0)
00098 {
00099 aa = 13.51; bb = -1.72; cc = -2.31;
00100 }
00101
00102 if (strcmp(drmethod, "Snodgrass") == 0)
00103 {
00104 aa = 2.897; bb = -0.339; cc = -0.485;
00105 dd = 0.202006;
00106 aa = aa/dd; bb=bb/dd; cc=cc/dd;
00107 ee = 0.930505;
00108 aa = aa*ee; bb = bb*ee; cc = cc*ee;
00109 }
00110
00111 outRec = drms_create_record(drms_env, outQuery, DRMS_PERMANENT, &status);
00112 if (status) DIE("Output recordset not created");
00113
00114 inRS = drms_open_records(drms_env, inQuery, &status);
00115 if (status || inRS->n == 0) DIE("No input data found");
00116 inRec = inRS->records[0];
00117 t_rec = drms_getkey_time(inRec, "T_REC", &status);
00118 t_rec0 = t_rec - halfw;
00119
00120 inQueryfinal = (char *)malloc(100 * sizeof(char));
00121 trec_str = (char *)malloc(30 * sizeof(char));
00122 sprint_time(trec_str, t_rec0, "TAI", 0);
00123 sprintf(inQueryfinal, "%s[%s/%s]", inRec->seriesinfo->seriesname, trec_str, t_window);
00124 printf("%s\n", inQueryfinal);
00125 drms_close_records(inRS, DRMS_FREE_RECORD);
00126 free(trec_str);
00127
00128 inRSfinal = drms_open_records(drms_env, inQueryfinal, &status);
00129 if (status || inRS->n == 0) DIE("No input data found");
00130 nrecs = inRSfinal->n;
00131
00132
00133
00134 int count = 0;
00135 int *recp;
00136 int nref, rec_cen;
00137 recp = (int *)malloc(nrecs * sizeof(int));
00138 for (i = 0; i < nrecs; i++)
00139 {
00140 inRecfinal = inRSfinal->records[i];
00141 inSeg = drms_segment_lookupnum(inRecfinal, 0);
00142 inArray = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00143
00144 if (status)
00145 {
00146 printf(" No data file found, status=%d\n", status);
00147 drms_free_array(inArray);
00148 continue;
00149 }
00150
00151 quality = drms_getkey_int(inRecfinal, "QUALITY", &status);
00152 if (drms_ismissing_int(quality))
00153 {
00154 printf(" Can't find QUALITY in log file; quality check disabled\n");
00155 continue;
00156 }
00157 else if ((quality != qualmask) & (quality != qualvld) & (quality != qualmask1))
00158 {
00159 printf(" Bad QUALITY = 0x%08x; rejected\n", quality);
00160 continue;
00161 }
00162 recp[count] = i;
00163 count += 1;
00164 drms_free_array(inArray);
00165 }
00166
00167 if (count==0) DIE("No input remapped data found");
00168 rec_cen = (int)(count/2);
00169 nref = recp[rec_cen];
00170 printf("middle data id=%d\n", nref);
00171 inRecfinal = inRSfinal->records[nref];
00172 inSeg = drms_segment_lookupnum(inRecfinal, 0);
00173 inArray = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00174 int naxis = inArray->naxis;
00175 xmg = inArray->axis[0]; ymg = inArray->axis[1];
00176 t_rec = drms_getkey_time(inRecfinal, "T_REC", &status);
00177 t_rec0 = drms_getkey_time(inRecfinal, "T_OBS", &status);
00178 crn = drms_getkey_int(inRecfinal, "CAR_ROT", &status);
00179 crlt = drms_getkey_float(inRecfinal, "CRLT_OBS", &status);
00180 crln = drms_getkey_float(inRecfinal, "CRLN_OBS", &status);
00181 clog0 = drms_getkey_double(inRecfinal, "CRVAL1", &status);
00182
00183
00184 drms_copykey(outRec, inRecfinal, "DATASIGN");
00185 drms_copykey(outRec, inRecfinal, "DSUN_OBS");
00186 drms_copykey(outRec, inRecfinal, "OBS_VR");
00187 drms_copykey(outRec, inRecfinal, "OBS_VW");
00188 drms_copykey(outRec, inRecfinal, "OBS_VN");
00189 drms_copykey(outRec, inRecfinal, "QUALITY");
00190 drms_copykey(outRec, inRecfinal, "MAPMMAX");
00191 drms_copykey(outRec, inRecfinal, "MAPLGMAX");
00192 drms_copykey(outRec, inRecfinal, "MAPLGMIN");
00193 drms_copykey(outRec, inRecfinal, "SINBDIVS");
00194 drms_copykey(outRec, inRecfinal, "MAPBMAX");
00195 drms_copykey(outRec, inRecfinal, "MAPRMAX");
00196 drms_copykey(outRec, inRecfinal, "LGSHIFT");
00197 drms_copykey(outRec, inRecfinal, "INTERPO");
00198 drms_copykey(outRec, inRecfinal, "MCORLEV");
00199 drms_copykey(outRec, inRecfinal, "MOFFSET");
00200 drms_copykey(outRec, inRecfinal, "CARSTRCH");
00201 drms_copykey(outRec, inRecfinal, "DIFROT_A");
00202 drms_copykey(outRec, inRecfinal, "DIFROT_B");
00203 drms_copykey(outRec, inRecfinal, "DIFROT_C");
00204 drms_copykey(outRec, inRecfinal, "INSTRUME");
00205 drms_copykey(outRec, inRecfinal, "BLD_VERS");
00206
00207 float *aveData;
00208 xmg = inArray->axis[0]; ymg = inArray->axis[1];
00209 xdim_syn = 3600; ydim_syn = 1440;
00210 int inDims[2] = {xmg, ymg};
00211 int dxsz = 2 * inDims[0];
00212 int ith = inDims[1];
00213 int ppd = xdim_syn/360;
00214 int xbeg = 30;
00215 if (xx1 == -1) xx1 = 50;
00216 if (yy1 == -1) yy1 = 0;
00217 int hwd = xx1;
00218 TIME tobs_total = 0.0, tobs_ave;
00219 xx1 *= ppd;
00220 xbeg *= ppd;
00221 aveData = (float *)malloc(xmg * ymg * sizeof(float));
00222 int ii, jj;
00223
00224 for (i = 0; i < count; i++)
00225 {
00226 inRecfinal = inRSfinal->records[recp[i]];
00227 inSeg = drms_segment_lookupnum(inRecfinal, 0);
00228 inArray = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00229 float *inData = (float *)inArray->data;
00230 int crnn = drms_getkey_int(inRecfinal, "CAR_ROT", &status);
00231 float clogn = drms_getkey_float(inRecfinal, "CRVAL1", &status);
00232 int xshift = (int)(ppd * ((clogn - clog0) - 360.0 * (crnn - crn)));
00233 TIME Tobs = drms_getkey_time(inRecfinal, "T_OBS", &status);
00234 quality = drms_getkey_int(inRecfinal, "QUALITY", &status);
00235 tobs_total += Tobs;
00236
00237 for (jj = 0; jj < ymg; jj++)
00238 {
00239 for (ii = xbeg; ii < xmg - xbeg; ii++)
00240 {
00241 aveData[jj * xmg + ii] += inData[jj * xmg + ii - xshift];
00242 }
00243 }
00244 drms_free_array(inArray);
00245 }
00246
00247 tobs_ave = tobs_total/count;
00248 for (jj = 0; jj < ymg; jj++)
00249 for (ii = 0; ii < xmg; ii++)
00250 aveData[jj * xmg + ii] /= count;
00251
00252 drms_close_records(inRSfinal, DRMS_FREE_RECORD);
00253 float thd[ith], csc[ith], phd[dxsz];
00254 float lad[ith], sth[ith], cth[ith], scs[dxsz];
00255 double dtor = PI/180.;
00256 float constrate = 13.1988;
00257 float ratelow, sinlat;
00258 DRMS_RecordSet_t *synRS = NULL;
00259 DRMS_Record_t *synRec;
00260 DRMS_Segment_t *synSeg;
00261 DRMS_Array_t *synArray, *supsynArray, *outArray;
00262 float *synData, *supsynData, *outData;
00263 int i1, j1;
00264 int synleftst = ppd * hwd, supleftst = ppd * clog0;
00265
00266 zgrid(dxsz, ith, 0, 0, 0, phd, thd, lad, cth, sth, csc, scs);
00267
00268 snprintf(timetmp, sizeof(timetmp), "%s[%d/3]", synQuery, crn-2);
00269 synQuery = timetmp;
00270 printf("inputname= %s\n", synQuery);
00271 synRS = drms_open_records(drms_env, synQuery, &status);
00272 if (status || synRS->n == 0) DIE("No input data found");
00273
00274 int nds = synRS->n;
00275 int supxDim = nds * xdim_syn;
00276 int supsynDim[2] = {supxDim, ydim_syn};
00277 int synDim[2] = {xdim_syn, ydim_syn};
00278 supsynArray = drms_array_create(DRMS_TYPE_FLOAT, 2, supsynDim, NULL, &status);
00279 supsynData = supsynArray->data;
00280 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, synDim, NULL, &status);
00281 outData = outArray->data;
00282
00283 for (i = 0; i < nds; i++)
00284 {
00285 synRec = synRS->records[i];
00286 synSeg = drms_segment_lookup(synRec, "synopmag");
00287 synArray = drms_segment_read(synSeg, DRMS_TYPE_FLOAT, &status);
00288 synData = synArray->data;
00289 int ii = (nds - 1 - i) * xdim_syn;
00290 for (j1 = 0; j1 < ydim_syn; j1++)
00291 for (i1 = 0; i1 < xdim_syn; i1++)
00292 {
00293 supsynData[supxDim * j1 + ii + i1] = synData[xdim_syn * j1 + i1];
00294 }
00295 }
00296
00297
00298
00299
00300 for (j = 0; j < ydim_syn/2; j++)
00301 {
00302 sinlat = sin(lad[j]*dtor);
00303 ratelow = aa + bb * sinlat * sinlat + cc * pow(sinlat, 4);
00304
00305 for (i = synleftst; i < xdim_syn; i++)
00306 {
00307 float lon = (i - synleftst) * constrate/ratelow;
00308 float delta_x = lon - (int)lon;
00309 int lonpixel = (int)(supleftst + lon);
00310 float x1 = supsynData[j * supxDim + lonpixel];
00311 float x2 = supsynData[j * supxDim + lonpixel + 1];
00312 outData[j*xdim_syn + i] = (1.0 - delta_x) * x1 + delta_x * x2;
00313 jj = ydim_syn - 1 - j;
00314 x1 = supsynData[jj * supxDim + lonpixel];
00315 x2 = supsynData[jj * supxDim + lonpixel + 1];
00316 outData[jj*xdim_syn + i] = (1.0 - delta_x) * x1 + delta_x * x2;
00317 }
00318 }
00319
00320 int magleft = ppd * (90 - hwd);
00321 for (j = 0; j < ydim_syn; j++)
00322 for (i = 0; i < 2*synleftst; i++)
00323 {
00324 outData[j * xdim_syn + i] = aveData[j * inDims[0] + (int)magleft + i];
00325 }
00326
00327 double statMin, statMax, statMedn, statMean, statSig, statSkew, statKurt;
00328 int statNgood;
00329 if (fstats(xdim_syn*ydim_syn, outData, &statMin, &statMax, &statMedn, &statMean, &statSig,
00330 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00331
00332 drms_setkey_time(outRec, "T_REC", t_rec);
00333 trec_str = (char *)malloc(30 * sizeof(char));
00334 sprint_time(trec_str, tobs_ave, "TAI", 0);
00335 drms_setkey_time(outRec, "T_OBS", tobs_ave);
00336 drms_setkey_int(outRec, "CAR_ROT", crn);
00337 drms_setkey_float(outRec, "CRLT_OBS", crlt);
00338 drms_setkey_float(outRec, "CRLN_OBS", crln);
00339 double loncen = xdim_syn/2 + 0.0;
00340 drms_setkey_double(outRec, "CRPIX1", loncen);
00341
00342 double latcen = ydim_syn/2 + 0.0;
00343 drms_setkey_double(outRec, "CRPIX2", latcen);
00344
00345 double lonstep = -360.0/xdim_syn;
00346 drms_setkey_double(outRec, "CDELT1", lonstep);
00347 double latstep = 2.0/ydim_syn;
00348 drms_setkey_double(outRec, "CDELT2", latstep);
00349 double lonfirst = 360.0 * (crn - 1) - clog0 + hwd;
00350 drms_setkey_double(outRec, "LON_FRST", lonfirst);
00351 double lonlast = 360.0 * crn - clog0 + hwd - 360.0/xdim_syn;
00352 drms_setkey_double(outRec, "LON_LAST", lonlast);
00353 double loncenter = lonfirst + 180.0 - 0.5 * 360.0/xdim_syn;
00354 drms_setkey_double(outRec, "CRVAL1", loncenter);
00355 drms_setkey_double(outRec, "CARRTIME", loncenter);
00356 drms_setkey_double(outRec, "LON_STEP", lonstep);
00357
00358
00359 float hwnwidth = drms_getkey_float(synRec, "HWNWIDTH", &status);
00360 drms_setkey_float(outRec, "HWNWIDTH", hwnwidth);
00361 float eqpoints = drms_getkey_float(synRec, "EQPOINTS", &status);
00362 drms_setkey_float(outRec, "EQPOINTS", eqpoints);
00363 float syndro_a = drms_getkey_float(synRec, "DIFROT_A", &status);
00364 drms_setkey_float(outRec, "OSYNDR_A", syndro_a);
00365 float syndro_b = drms_getkey_float(synRec, "DIFROT_B", &status);
00366 drms_setkey_float(outRec, "OSYNDR_B", syndro_b);
00367 float syndro_c = drms_getkey_float(synRec, "DIFROT_C", &status);
00368 drms_setkey_float(outRec, "OSYNDR_C", syndro_c);
00369
00370 i = xdim_syn*ydim_syn;
00371 drms_setkey_int(outRec, "TOTVALS", i);
00372 drms_setkey_int(outRec, "DATAVALS", statNgood);
00373 i = xdim_syn*ydim_syn-statNgood;
00374 drms_setkey_int(outRec, "MISSVALS", i);
00375 drms_setkey_double(outRec, "DATAMIN", statMin);
00376 drms_setkey_double(outRec, "DATAMAX", statMax);
00377 drms_setkey_double(outRec, "DATAMEDN", statMedn);
00378 drms_setkey_double(outRec, "DATAMEAN", statMean);
00379 drms_setkey_double(outRec, "DATARMS", statSig);
00380 drms_setkey_double(outRec, "DATASKEW", statSkew);
00381 drms_setkey_double(outRec, "DATAKURT", statKurt);
00382
00383 drms_setkey_string(outRec, "FRTIMWDN", t_window);
00384 drms_setkey_string(outRec, "SYNDRORA", drmethod);
00385 drms_setkey_int(outRec, "FRAVEPNT", count);
00386 drms_setkey_float(outRec, "FRWINDOW", 2.0 * hwd);
00387 drms_setkey_float(outRec, "SYNDRO_A", aa);
00388 drms_setkey_float(outRec, "SYNDRO_B", bb);
00389 drms_setkey_float(outRec, "SYNDRO_C", cc);
00390
00391 drms_keyword_setdate(outRec);
00392 printf(" WRITING OUTPUT\n");
00393 outSeg = drms_segment_lookupnum(outRec, 0);
00394 outArray->parent_segment = outSeg;
00395 status = drms_segment_write(outSeg, outArray, 0);
00396 if (status) DIE("problem writing file");
00397
00398 free(recp); free(aveData); free(trec_str);
00399 drms_free_array(outArray);
00400 drms_free_array(supsynArray);
00401 drms_free_array(synArray);
00402
00403 drms_close_record(outRec, DRMS_INSERT_RECORD);
00404 drms_close_records(synRS, DRMS_FREE_RECORD);
00405 return 0;
00406 }
00407
00408
00409
00410
00411
00412 float zgrid(int jph, int ith, int cmp, int rup, int dbl,
00413 float phd[], float thd[], float lad[], float cth[],
00414 float sth[], float csc[], float scs[])
00415 {
00416
00417 double dtor = PI/180.;
00418 int i, j;
00419
00420 float dcth = 2.0/ith;
00421 float dph = 360./jph;
00422 float cpc[jph], thr, dphr;
00423 float lthd[ith], lphd[jph], lscs[jph];
00424 float llad[ith], lsth[ith], lcth[ith];
00425
00426 #if 0
00427 if (dbl == 1)
00428 {
00429 double thd[ith], phd[jph], csc[ith], cpc[jph];
00430 double dph = 360./jph;
00431 double dcth = 2.0/ith;
00432 double lad[ith], sth[ith], cth[ith], thr;
00433 double lthd[ith], lphd[jph], lscs[jph];
00434 double llad[ith], lsth[ith], lcth[ith];
00435 double dphr;
00436 float scs[jph];
00437 }
00438 #endif
00439
00440 for (i =0; i < ith; i++)
00441 {
00442 cth[i] = (i + 0.5) * dcth - 1.0;
00443 thr = acos(cth[i]);
00444 sth[i] = sin(thr);
00445 thd[i] = thr/dtor;
00446 lad[i] = 90-thd[i];
00447 }
00448
00449 for (j = 0; j < jph; j++)
00450 phd[j] = (j + 0.5) * dph;
00451
00452 if (cmp == 1)
00453 {
00454 for (i = 0; i < ith; i++)
00455 csc[i] = 1/sth[i];
00456 for (j = 0; j < jph; j++)
00457 {
00458 dphr = (phd[j] - cmp) * dtor;
00459 cpc[j] = cos(dphr);
00460 scs[j] = 1/cpc[j];
00461 }
00462 }
00463
00464 if (rup == 1)
00465 {
00466 for (i = 0; i < ith; i++)
00467 {
00468 thd[i] = lthd[ith - i];
00469 lad[i] = llad[ith - 1];
00470 cth[i] = lcth[ith - i];
00471 sth[i] = lsth[ith - 1];
00472 }
00473 for (j = 0; j < jph; j++)
00474 {
00475 phd[j] = lphd[jph-j];
00476 scs[j] = lscs[jph-j];
00477 }
00478 }
00479 return 0;
00480 }
00481