00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 #include <jsoc_main.h>
00097 #include "keystuff.c"
00098 #include "earth_ephem.c"
00099
00100 char *module_name = "travel_times_loop_wGB";
00101 char *version_id = "0.9.2";
00102
00103 ModuleArgs_t module_args[] = {
00104 {ARG_STRING, "in",
00105 "hmi_test.tdVtrack_synop[2010.07.08_04:00:00_TAI][][0.0][0.0]",
00106 "input record set"},
00107 {ARG_STRING, "pxloc", "hmi.tdpixlist[]", "pixel locater record set"},
00108 {ARG_STRING, "out", "hmi_test.tdVtimes_synop", "output series"},
00109 {ARG_STRING, "copy", "+",
00110 "comma separated list of keys to propagate forward"},
00111 {ARG_FLAG, "v", "0", "verbose output"},
00112 {}
00113 };
00114
00115
00116 char *propagate[] = {"CarrRot", "CMLon", "LonHG", "LatHG", "LonCM", "MidTime",
00117 "CRVAL1", "CRVAL2", "CTYPE1", "CTYPE2", "CUNIT1", "CUNIT2",
00118 "T_START", "T_STOP", "LonSpan", "Coverage", "Duration", "MapProj", "MapPA",
00119 "Zonal Trk", "ZonalVel"};
00120
00121 #define NUM_ANNULI_MAX (20)
00122
00123 extern void mainsub1_ (float *velo, float *wid, int *num_annuli, float *time,
00124 float *coef, int *n_start, int *n_th, float *input,
00125 char *pix_locat_file, int *len_ploc_file, float *out1, float *out2);
00126 extern void mainsub2_ (float *velo, float *wid, int *num_annuli, float *time,
00127 float *coef, int *n_start, int *n_th, float *input,
00128 char *pix_locat_file, int *len_ploc_file, float *out1, float *out2);
00129
00130 int DoIt (void) {
00131 CmdParams_t *params = &cmdparams;
00132 DRMS_RecordSet_t *plds, *inds;
00133 DRMS_Record_t *ploc, *trck, *orec;
00134 DRMS_Segment_t *pseg, *dseg, *seg_gabor, *seg_gb;
00135 DRMS_Array_t *data_array, *res_gabor, *res_gb;
00136 FILE *timesfile, *fout;
00137 double mapscale_factor;
00138 float **ttimes;
00139 float *data, *input, *input2, *input3, *output, *output2, *out1, *out2;
00140 float *annmin, *annmax, *velo, *wid, *coef0, *coef1, *coef2, *coef3, *coef4;
00141 float coef[5];
00142 float newctr, newscale;
00143 long long n, ntot;
00144 int *num_annuli, *n_start;
00145 int trck_axis[2];
00146 int ann, i, j, k, *axes;
00147 int len_ploc_file, outok, ploc_version, status;
00148 int kstat, key_n, propct, propstd;
00149 int rec, rgnct, smpl, smpls;
00150 int need_ephem;
00151 char **pix_locat_file, **copykeylist;
00152 char *genstr, *errmsg;
00153 char plocfile[DRMS_MAXQUERYLEN];
00154 char source[DRMS_MAXQUERYLEN], recid[DRMS_MAXQUERYLEN];
00155 char module_ident[64], keyname[32];
00156
00157 int keyct = sizeof (propagate) / sizeof (char *);
00158
00159 char *plocds = strdup (params_get_str (params, "pxloc"));
00160 char *trckds = strdup (params_get_str (params, "in"));
00161 char *outser = strdup (params_get_str (params, "out"));
00162 char *propagate_req = strdup (params_get_str (params, "copy"));
00163 int verbose = params_isflagset (params, "v");
00164
00165 snprintf (module_ident, 64, "%s v %s", module_name, version_id);
00166 if (verbose) printf ("%s:\n", module_ident);
00167
00168 plds = drms_open_records (drms_env, plocds, &status);
00169 if (status) {
00170 fprintf (stderr, "Error: unable to open pixel target record set %s\n",
00171 plocds);
00172 return 1;
00173 }
00174 smpls = plds->n;
00175 if (verbose)
00176 printf (" calculating travel time maps for %d annulus sets\n", smpls);
00177 if (smpls != 11)
00178 fprintf (stderr, "Warning: untested case of %d annulus sets\n", smpls);
00179 num_annuli = (int *)malloc (smpls * sizeof (int));
00180 annmin = (float *)malloc (smpls * sizeof (float));
00181 annmax = (float *)malloc (smpls * sizeof (float));
00182 velo = (float *)malloc (smpls * sizeof (float));
00183 wid = (float *)malloc (smpls * sizeof (float));
00184 n_start = (int *)malloc (smpls * sizeof (int));
00185 coef0 = (float *)malloc (smpls * sizeof (float));
00186 coef1 = (float *)malloc (smpls * sizeof (float));
00187 coef2 = (float *)malloc (smpls * sizeof (float));
00188 coef3 = (float *)malloc (smpls * sizeof (float));
00189 coef4 = (float *)malloc (smpls * sizeof (float));
00190 ttimes = (float **)malloc (smpls * sizeof (float *));
00191 pix_locat_file = (char **)malloc (smpls * sizeof (char **));
00192 for (smpl = 0; smpl < smpls; smpl++) {
00193 ploc = plds->records[smpl];
00194 drms_sprint_rec_query (source, ploc);
00195
00196 if (smpl) {
00197 if (ploc_version != drms_getkey_int (ploc, "Version", &status)) {
00198 if (ploc_version) {
00199 fprintf (stderr,
00200 "Warning: inconsistent versions of pixel locator records\n");
00201 fprintf (stderr,
00202 " Version number will be set to 0 in output\n");
00203 }
00204 ploc_version = 0;
00205 }
00206 } else {
00207 ploc_version = drms_getkey_int (ploc, "Version", &status);
00208 if (status) {
00209 fprintf (stderr,
00210 "Warning: unknown version of pixel locator record(s)\n");
00211 ploc_version = 0;
00212 }
00213 }
00214
00215 num_annuli[smpl] = drms_getkey_int (ploc, "Annuli", &status);
00216 if (num_annuli[smpl] > NUM_ANNULI_MAX) {
00217 fprintf (stderr,
00218 "Warning: num_annuli exceeds fixed mem alloc in Fortran code\n");
00219 fprintf (stderr, " reduced to %d\n", NUM_ANNULI_MAX);
00220 num_annuli[smpl] = NUM_ANNULI_MAX;
00221 }
00222 annmin[smpl] = drms_getkey_float (ploc, "RadInner", &status);
00223 annmax[smpl] = drms_getkey_float (ploc, "RadOuter", &status);
00224 velo[smpl] = drms_getkey_float (ploc, "PhaseVel", &status);
00225 wid[smpl] = drms_getkey_float (ploc, "FiltWid", &status);
00226 coef0[smpl] = drms_getkey_float (ploc, "AmplInit", &status);
00227 coef1[smpl] = drms_getkey_float (ploc, "DFreqIni", &status);
00228 coef2[smpl] = drms_getkey_float (ploc, "GrpTTIni", &status);
00229 coef3[smpl] = drms_getkey_float (ploc, "FreqInit", &status);
00230 coef4[smpl] = drms_getkey_float (ploc, "PhsTTIni", &status);
00231 n_start[smpl] = drms_getkey_float (ploc, "NStart", &status);
00232 ttimes[smpl] = (float *)calloc (NUM_ANNULI_MAX, sizeof (float));
00233 pseg = drms_segment_lookup (ploc, "times");
00234 if (!pseg) {
00235 fprintf (stderr,
00236 "Error: record %s\n does not contain required segment \"times\"\n",
00237 source);
00238 drms_close_records (plds, DRMS_FREE_RECORD);
00239 return 1;
00240 }
00241 drms_segment_filename (pseg, plocfile);
00242 timesfile = fopen (plocfile, "r");
00243 for (n = 0; n < num_annuli[smpl]; n++)
00244 fscanf (timesfile, "%f", &(ttimes[smpl][n]));
00245
00246 for (; n < NUM_ANNULI_MAX; n++) ttimes[smpl][n] = 0.0;
00247 fclose (timesfile);
00248
00249 pseg = drms_segment_lookup (ploc, "pxloc.fortran");
00250 if (!pseg) {
00251 fprintf (stderr,
00252 "Error: record %s\n does not contain required segment \"pxloc.fortran\"\n",
00253 source);
00254 drms_close_records (plds, DRMS_FREE_RECORD);
00255 return 1;
00256 }
00257 drms_segment_filename (pseg, plocfile);
00258 pix_locat_file[smpl]= (char *)malloc (strlen (plocfile) + 1);
00259 strncpy (pix_locat_file[smpl], plocfile, strlen (plocfile));
00260 pix_locat_file[smpl][strlen (plocfile)] = '\0';
00261 }
00262
00263 axes = (int *)malloc (4 * sizeof (int));
00264 axes[0] = axes[1] = 256;
00265 axes[2] = 4;
00266 axes[3] = smpls;
00267 output = (float *)malloc (smpls * 4 * 256 * 256 * sizeof (float));
00268 output2 = (float *)malloc (smpls * 4 * 256 * 256 * sizeof (float));
00269 out1 = (float *)malloc (4 * 256 * 256 *sizeof(float));
00270 out2 = (float *)malloc (4 * 256 * 256 *sizeof(float));
00271 res_gabor = drms_array_create (DRMS_TYPE_FLOAT, 4, axes, output, &status);
00272 res_gb = drms_array_create (DRMS_TYPE_FLOAT, 4, axes, output2, &status);
00273 drms_close_records (plds, DRMS_FREE_RECORD);
00274
00275 input2 = (float *)malloc (256 * 256 * 640 * sizeof(float));
00276 input3 = (float *)malloc (256 * 256 * 640 * sizeof(float));
00277
00278 inds = drms_open_records (drms_env, trckds, &status);
00279 if (status) {
00280 fprintf (stderr, "Error: unable to open input record set %s\n", trckds);
00281 drms_free_array (res_gabor);
00282 drms_free_array (res_gb);
00283 return 1;
00284 }
00285 rgnct = inds->n;
00286 if (verbose)
00287 printf (" in each of %d regions\n", rgnct);
00288
00289 outok = 0;
00290 for (rec = 0; rec < rgnct; rec++) {
00291 trck = inds->records[rec];
00292 dseg = drms_segment_lookup (trck, "V");
00293 if (!dseg) {
00294 fprintf (stderr, "Error: no segment \"V\" in record %d; skipped\n", rec);
00295 continue;
00296 }
00297 for (i = 0; i < 2; i++) {
00298 trck_axis[i] = dseg->axis[i];
00299 if (i) {
00300 if (trck_axis[i] - trck_axis[i-1]) {
00301 fprintf (stderr,
00302 "Error: code as implemented requires square tracked regions\n");
00303 drms_free_array (res_gabor);
00304 drms_free_array (res_gb);
00305 drms_close_records (inds, DRMS_FREE_RECORD);
00306 return 1;
00307 }
00308 }
00309 }
00310 mapscale_factor = 256.0 / (double)trck_axis[0];
00311
00312 orec = drms_create_record (drms_env, outser, DRMS_PERMANENT, &status);
00313 if (!orec) {
00314 fprintf (stderr, "Error: unable to create record %d in series %s\n", rec,
00315 outser);
00316 fprintf (stderr, " remainder of processing skipped\n");
00317 drms_close_records (inds, DRMS_FREE_RECORD);
00318 return 0;
00319 }
00320
00321 seg_gabor = drms_segment_lookup (orec, "gabor");
00322 seg_gb = drms_segment_lookup (orec, "GB");
00323 if (!outok) {
00324
00325 if (!seg_gabor || !seg_gb) {
00326 fprintf (stderr, "Error: output data series %s\n", outser);
00327 fprintf (stderr, " lacks segment \"GB\" and/or \"gabor\"\n");
00328 drms_close_records (inds, DRMS_FREE_RECORD);
00329 return 1;
00330
00331 }
00332
00333 outok = 1;
00334 }
00335 res_gabor->bscale = res_gb->bscale = 1.0;
00336 res_gabor->bzero = res_gb->bzero = 0.0;
00337
00338 propct = construct_stringlist (propagate_req, ',', ©keylist);
00339 kstat = 0;
00340 propstd = 0;
00341
00342 for (key_n = 0; key_n < propct; key_n++) {
00343 if (strcmp (copykeylist[key_n], "+"))
00344 kstat += check_and_copy_key (orec, trck, copykeylist[key_n]);
00345 else propstd = 1;
00346 }
00347 if (propstd) {
00348
00349 kstat += propagate_keys (orec, trck, propagate, keyct);
00350
00351 kstat += copy_prime_keys (orec, trck);
00352 }
00353 if (kstat) {
00354 fprintf (stderr, "Error writing key value(s) to record in series %s:\n",
00355 outser);
00356 fprintf (stderr, " series may not have appropriate structure;\n");
00357 fprintf (stderr, " record %d skipped\n", rec);
00358 drms_close_record (orec, DRMS_FREE_RECORD);
00359 continue;
00360 }
00361 drms_sprint_rec_query (source, trck);
00362
00363 need_ephem = (drms_keyword_lookup (orec, "CRLT_Mid", 1) != NULL);
00364 if (need_ephem)
00365 need_ephem = (drms_keyword_lookup (orec, "MidTime", 1) != NULL);
00366 if (need_ephem) {
00367 double rsun, clat = 0.0 / 0.0, clon, vr, vn, vw;
00368 TIME tmid = drms_getkey_time (orec, "MidTime", &status);
00369 earth_ephemeris (tmid, &rsun, &clat, &clon, &vr, &vn, &vw);
00370 kstat += check_and_set_key_float (orec, "CRLT_Mid", clat);
00371 }
00372
00373 kstat += check_and_set_key_int (orec, "WCSAXES", 2);
00374 newctr = drms_getkey_float (trck, "CRPIX1", &status);
00375 newctr = mapscale_factor * (newctr - 0.5) + 0.5;
00376 kstat += check_and_set_key_float (orec, "CRPIX1", newctr);
00377 newctr = drms_getkey_float (trck, "CRPIX2", &status);
00378 newctr = mapscale_factor * (newctr - 0.5) + 0.5;
00379 kstat += check_and_set_key_float (orec, "CRPIX2", newctr);
00380 newscale = drms_getkey_float (trck, "CDELT1", &status);
00381 newscale /= mapscale_factor;
00382 kstat += check_and_set_key_float (orec, "CDELT1", newscale);
00383 newscale = drms_getkey_float (trck, "CDELT2", &status);
00384 newscale /= mapscale_factor;
00385 kstat += check_and_set_key_float (orec, "CDELT2", newscale);
00386 genstr = drms_getkey_string (trck, "WCSNAME", &status);
00387 if (!status) {
00388
00389 strtok (genstr, "/");
00390 kstat += check_and_set_key_str (orec, "WCSNAME", genstr);
00391 }
00392
00393 kstat += check_and_set_key_str (orec, "TTDiff_1",
00394 "mean (outgoing,ingoing)");
00395 kstat += check_and_set_key_str (orec, "TTDiff_2", "outgoing-ingoing");
00396 kstat += check_and_set_key_str (orec, "TTDiff_3", "west-east");
00397 kstat += check_and_set_key_str (orec, "TTDiff_4", "north-south");
00398
00399 newscale = drms_getkey_float (trck, "MapScale", &status);
00400 if (!status) {
00401 newscale /= mapscale_factor;
00402 kstat += check_and_set_key_float (orec, "MapScale", newscale);
00403 }
00404
00405 for (smpl = 0; smpl < smpls; smpl++) {
00406 sprintf (keyname, "AnnMin%02d", smpl + 1);
00407 kstat += check_and_set_key_float (orec, keyname, annmin[smpl]);
00408 sprintf (keyname, "AnnMax%02d", smpl + 1);
00409 kstat += check_and_set_key_float (orec, keyname, annmax[smpl]);
00410 sprintf (keyname, "PhsVel%02d", smpl + 1);
00411 kstat += check_and_set_key_float (orec, keyname, velo[smpl]);
00412 sprintf (keyname, "FltWid%02d", smpl + 1);
00413 kstat += check_and_set_key_float (orec, keyname, wid[smpl]);
00414 }
00415 if (kstat) {
00416 fprintf (stderr, "Error writing key value(s) to record in series %s:\n",
00417 outser);
00418 fprintf (stderr, " series may not have appropriate structure;\n");
00419 fprintf (stderr, " record %d skipped\n", rec);
00420 drms_close_record (orec, DRMS_FREE_RECORD);
00421 continue;
00422 }
00423
00424 data_array = drms_segment_read (dseg, DRMS_TYPE_FLOAT, &status);
00425 input = (float *)data_array->data;
00426 if (!input) {
00427 fprintf (stderr, "Error: unable to read input data record-segment %d\n",
00428 rec);
00429 drms_close_record (orec, DRMS_FREE_RECORD);
00430 continue;
00431 }
00432 for (i=0; i<640; i++)
00433 for (j=0; j<256; j++)
00434 for (k=0; k<256; k++)
00435 input2[i*256*256 + j*256 + k] =
00436 0.25*(input[i*512*512 + (2*j*512) + (2*k)] +
00437 input[i*512*512 + (2*j)*512 + (2*k+1)] +
00438 input[i*512*512 + (2*j+1)*512 + (2*k)] +
00439 input[i*512*512 + (2*j+1)*512 + (2*k+1)]);
00440
00441 ntot = 1;
00442 for (i = 0; i < data_array->naxis; i++) ntot *= data_array->axis[i];
00443 for (n = 0; n < ntot; n++) if (isnan (input[n])) input[n] = 0.0;
00444
00445 for (ann=0; ann<3; ann++) {
00446 int nth = ann + 1;
00447 if (verbose) printf ("begin annulus %d\n", ann);
00448
00449 len_ploc_file = strlen (pix_locat_file[ann]);
00450
00451 coef[0] = coef0[ann];
00452 coef[1] = coef1[ann];
00453 coef[2] = coef2[ann];
00454 coef[3] = coef3[ann];
00455 coef[4] = coef4[ann];
00456 mainsub1_ (&velo[ann], &wid[ann], &num_annuli[ann], ttimes[ann], coef,
00457 &n_start[ann], &nth, input, pix_locat_file[ann],
00458 &len_ploc_file, out1, out2);
00459
00460 for (j=0; j<4*256*256; j++) {
00461 output[ann*4*256*256+j] = out1[j];
00462 output2[ann*4*256*256+j] = out2[j];
00463 }
00464
00465 drms_free_array (data_array);
00466 data_array = drms_segment_read (dseg, DRMS_TYPE_FLOAT, &status);
00467 input = (float *)data_array->data;
00468 if (!input) {
00469 fprintf (stderr, "Error: unable to read input data record-segment %d\n",
00470 rec);
00471 drms_close_record (orec, DRMS_FREE_RECORD);
00472 ann = 11;
00473 continue;
00474 }
00475 for (n = 0; n < ntot; n++) if (isnan (input[n])) input[n] = 0.0;
00476 }
00477
00478 ntot = 256 * 256 * 640;
00479 for (n = 0; n < ntot; n++) if (isnan (input2[n])) input2[n] = 0.0;
00480 for (; ann<11; ann++) {
00481 int nth = ann + 1;
00482 if (verbose) printf ("begin annulus %d\n", ann);
00483 for (n = 0; n < ntot; n++) input3[n] = input2[n];
00484
00485 len_ploc_file = strlen (pix_locat_file[ann]);
00486 coef[0] = coef0[ann];
00487 coef[1] = coef1[ann];
00488 coef[2] = coef2[ann];
00489 coef[3] = coef3[ann];
00490 coef[4] = coef4[ann];
00491 mainsub2_ (&velo[ann], &wid[ann], &num_annuli[ann], ttimes[ann], coef,
00492 &n_start[ann], &nth, input3, pix_locat_file[ann],
00493 &len_ploc_file, out1, out2);
00494 for (j=0; j<4*256*256; j++) {
00495 output[ann*4*256*256 + j] = out1[j];
00496 output2[ann*4*256*256 + j] = out2[j];
00497 }
00498 }
00499 status = drms_segment_write (seg_gabor, res_gabor, 0);
00500 if (status) {
00501 drms_sprint_rec_query (recid, orec);
00502 fprintf (stderr, "Error writing data to record %s\n", recid);
00503 fprintf (stderr, " drms_segment_write returned %d\n", status);
00504 fprintf (stderr, " series may not have appropriate structure\n");
00505 fprintf (stderr, " record %d skipped\n", rec);
00506 drms_close_record (orec, DRMS_FREE_RECORD);
00507 continue;
00508 }
00509 status = drms_segment_write (seg_gb, res_gb, 0);
00510 if (status) {
00511
00512 drms_sprint_rec_query (recid, orec);
00513 fprintf (stderr, "Error writing data to record %s\n", recid);
00514 fprintf (stderr, " series may not have appropriate structure\n");
00515 fprintf (stderr, " record %d skipped\n", rec);
00516 drms_close_record (orec, DRMS_FREE_RECORD);
00517 continue;
00518 }
00519
00520 kstat += check_and_set_key_str (orec, "Module", module_ident);
00521 kstat += check_and_set_key_str (orec, "BLD_VERS", jsoc_version);
00522 kstat += check_and_set_key_str (orec, "Source", source);
00523 kstat += check_and_set_key_str (orec, "Input", trckds);
00524 kstat += check_and_set_key_str (orec, "PixLocate", plocds);
00525 kstat += check_and_set_key_int (orec, "PxLocVer", ploc_version);
00526 kstat += check_and_set_key_time (orec, "Created", CURRENT_SYSTEM_TIME);
00527 if (kstat) {
00528 fprintf (stderr, "Error writing key value(s) to record in series %s:\n",
00529 outser);
00530 fprintf (stderr, " series may not have appropriate structure;\n");
00531 fprintf (stderr, " record %d skipped\n", rec);
00532 drms_close_record (orec, DRMS_FREE_RECORD);
00533 continue;
00534 }
00535
00536 drms_close_record (orec, DRMS_INSERT_RECORD);
00537 }
00538 drms_close_records (inds, DRMS_FREE_RECORD);
00539 return 0;
00540 }
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586