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 #include <jsoc_main.h>
00057 #include "keystuff.c"
00058
00059 #define KERNTYP_RAY (1)
00060 #define KERNTYP_BORN (2)
00061
00062 char *module_name = "timed_invert";
00063 char *version_id = "0.81";
00064
00065 ModuleArgs_t module_args[] = {
00066 {ARG_STRING, "in", "", "input data record"},
00067 {ARG_STRING, "kernels", "", "input kernel"},
00068 {ARG_STRING, "out", "", "output series"},
00069 {ARG_NUME, "fitting", "both", "travel-time fitting method used",
00070 "Gabor, GizonBirch, both"},
00071 {ARG_STRING, "copy", "+",
00072 "comma separated list of keys to propagate forward"},
00073 {ARG_FLAG, "v", "0", "verbose output"},
00074 {}
00075 };
00076
00077
00078 char *propagate[] = {"CarrRot", "CMLon", "LonHG", "LatHG", "LonCM", "MidTime",
00079 "PxLocVer", "WCSAXES", "WCSNAME", "CTYPE1", "CTYPE2", "CRPIX1", "CRPIX2",
00080 "CDELT1", "CDELT2", "CUNIT1", "CUNIT2", "CRVAL1", "CRVAL2",
00081 "MapScale", "MapProj", "Map_PA"};
00082
00083 extern void inver_cs_sub_ (float *oi_mn, float *kern_cs, float *cs);
00084 extern void inver_v_sub_ (float *oi, float *we, float *ns, float *lonc,
00085 float *latc, float *kern_vx, float *kern_vy, float *kern_vz,
00086 float *vx, float *vy, float *vz, int *kerntyp);
00087
00088 enum fittypes {GABOR_WAVELET, GIZON_BIRCH, ALL_FITS};
00089
00090 int DoIt (void) {
00091 CmdParams_t *params = &cmdparams;
00092 DRMS_RecordSet_t *ids, *kds;
00093 DRMS_Record_t *ttrec, *krec, *orec;
00094 DRMS_Segment_t *ttseg, *seg_cs, *seg_vx, *seg_vy, *seg_vz;
00095 DRMS_Segment_t *kcs_seg, *kvx_seg, *kvy_seg, *kvz_seg;
00096 DRMS_Array_t *kcs_arr, *kvx_arr, *kvy_arr, *kvz_arr;
00097 DRMS_Array_t *ttdat, *cs_res, *vx_res, *vy_res, *vz_res;
00098 float *kcs, *kvx, *kvy, *kvz;
00099 float *input, *oi_mn, *oi, *we, *ns, *cs, *vx, *vy, *vz;
00100 float *inpmn, *inpoi, *inpwe, *inpns;
00101 int *axes;
00102 int col, cols, row, rows, pln, pln0, pln1, pln2, pln3;
00103 int i, j, k, n, ntot, rgn, rgnct, outok;
00104 int pxlocver, kerntyp;
00105 int kstat, key_n, propct, propstd, status;
00106 char **copykeylist;
00107 char *type_segname[] = {"gabor", "GB"};
00108 char *fitting_key[] = {"Gabor", "GizonBirch", "both"};
00109 char *sval;
00110 char recid[DRMS_MAXQUERYLEN], source[DRMS_MAXQUERYLEN];
00111 char module_ident[64];
00112
00113 int keyct = sizeof (propagate) / sizeof (char *);
00114 int docsinv = 1, dovinv = 1;
00115
00116 char *ttimds = strdup (params_get_str (params, "in"));
00117 char *kernds = strdup (params_get_str (params, "kernels"));
00118 char *outser = strdup (params_get_str (params, "out"));
00119 char *propagate_req = strdup (params_get_str (params, "copy"));
00120 int fitopt = params_get_int (params, "fitting");
00121 int verbose = params_isflagset (params, "v");
00122
00123 snprintf (module_ident, 64, "%s v %s", module_name, version_id);
00124 if (verbose) printf ("%s:\n", module_ident);
00125
00126 kds = drms_open_records (drms_env, kernds, &status);
00127 if (status) {
00128 fprintf (stderr, "Error: unable to open input record set %s\n", kernds);
00129 return 1;
00130 }
00131 if (kds->n != 1) {
00132 fprintf (stderr,
00133 "Error: kernel record set %s does not contain single record\n",
00134 kernds);
00135 drms_close_records (kds, DRMS_FREE_RECORD);
00136 return 1;
00137 }
00138 krec = kds->records[0];
00139 kcs_seg = drms_segment_lookup (krec, "cs");
00140 kvx_seg = drms_segment_lookup (krec, "vx");
00141 kvy_seg = drms_segment_lookup (krec, "vy");
00142 kvz_seg = drms_segment_lookup (krec, "vz");
00143 sval = drms_getkey_string (krec, "Approx", &status);
00144 if (status) {
00145 fprintf (stderr, "Warning: kernel approximation type not specified in\n");
00146 fprintf (stderr, " %s ; Ray path assumed\n", kernds);
00147 kerntyp = KERNTYP_RAY;
00148 } else {
00149 if (!strcasecmp (sval, "Raypath")) kerntyp = KERNTYP_RAY;
00150 else if (!strcasecmp (sval, "Born")) kerntyp = KERNTYP_BORN;
00151 else {
00152 fprintf (stderr, "Warning: kernel approximation type %s not recognized in\n",
00153 sval);
00154 fprintf (stderr, " %s ; Ray path assumed\n", kernds);
00155 kerntyp = KERNTYP_RAY;
00156 }
00157 }
00158
00159 if (kcs_seg) {
00160 kcs_arr = drms_segment_read (kcs_seg, DRMS_TYPE_FLOAT, &status);
00161 if (status) {
00162 fprintf (stderr, "Warning: unable to read \"cs\" segment in kernel record;\n");
00163 fprintf (stderr, " sound-speed inversions will not be attempted\n");
00164 docsinv = 0;
00165 } else
00166 kcs = (float *)kcs_arr->data;
00167 } else {
00168 fprintf (stderr, "Warning: no \"cs\" segment in kernel record;\n");
00169 fprintf (stderr, " sound-speed inversions will not be attempted\n");
00170 docsinv = 0;
00171 }
00172
00173 if (kvx_seg && kvy_seg && kvz_seg) {
00174 kvx_arr = drms_segment_read (kvx_seg, DRMS_TYPE_FLOAT, &status);
00175 if (status) dovinv = 0;
00176 kvy_arr = drms_segment_read (kvy_seg, DRMS_TYPE_FLOAT, &status);
00177 if (status) dovinv = 0;
00178 kvz_arr = drms_segment_read (kvz_seg, DRMS_TYPE_FLOAT, &status);
00179 if (status) dovinv = 0;
00180 if (!dovinv) {
00181 fprintf (stderr,
00182 "Warning: unable to read one or more v_i segments in kernel record;\n");
00183 fprintf (stderr, " velocity inversions will not be attempted.\n");
00184 if (!docsinv) {
00185 fprintf (stderr, " nothing to do!\n");
00186 drms_close_records (kds, DRMS_FREE_RECORD);
00187 return 1;
00188 }
00189 }
00190 kvx = (float *)kvx_arr->data;
00191 kvy = (float *)kvy_arr->data;
00192 kvz = (float *)kvz_arr->data;
00193 } else {
00194 fprintf (stderr,
00195 "Warning: one or more v_i segments missing in kernel record;\n");
00196 fprintf (stderr, " velocity inversions will not be attempted.\n");
00197 if (!docsinv) {
00198 fprintf (stderr, " nothing to do!\n");
00199 drms_close_records (kds, DRMS_FREE_RECORD);
00200 return 1;
00201 }
00202 dovinv = 0;
00203 }
00204
00205 ids = drms_open_records (drms_env, ttimds, &status);
00206 if (status) {
00207 fprintf (stderr, "Error: unable to open input record set %s\n", ttimds);
00208 drms_close_records (kds, DRMS_FREE_RECORD);
00209 return 1;
00210 fprintf (stderr, "input data set is open\n");
00211 }
00212 rgnct = ids->n;
00213 if (rgnct < 1) {
00214 fprintf (stderr, "Error: input dataset %s contains no records\n", ttimds);
00215 drms_close_records (kds, DRMS_FREE_RECORD);
00216 drms_close_records (ids, DRMS_FREE_RECORD);
00217 return 1;
00218 }
00219
00220 axes = (int *)malloc (3 * sizeof (int));
00221 cols = axes[0] = rows = axes[1] = 256;
00222 axes[2] = 11;
00223 ntot = 1;
00224 for (n = 0; n < 3; n++) ntot *= axes[n];
00225 oi_mn = (float *)malloc (ntot * sizeof (float));
00226 oi = (float *)malloc (ntot * sizeof (float));
00227 we = (float *)malloc (ntot * sizeof (float));
00228 ns = (float *)malloc (ntot * sizeof (float));
00229 vx = (float *)malloc (ntot * sizeof (float));
00230 vy = (float *)malloc (ntot * sizeof (float));
00231 vz = (float *)malloc (ntot * sizeof (float));
00232 if (docsinv) {
00233 cs = (float *)malloc (ntot * sizeof (float));
00234 cs_res = drms_array_create (DRMS_TYPE_FLOAT, 3, axes, cs, &status);
00235 if (status) {
00236 fprintf (stderr, "Error: unable to create required ouput array cs\n");
00237 return 1;
00238 }
00239 }
00240
00241 vx_res = drms_array_create (DRMS_TYPE_FLOAT, 3, axes, vx, &status);
00242 vy_res = drms_array_create (DRMS_TYPE_FLOAT, 3, axes, vy, &status);
00243 vz_res = drms_array_create (DRMS_TYPE_FLOAT, 3, axes, vz, &status);
00244
00245 outok = 0;
00246 rgn = 0;
00247
00248 for (rgn = 0; rgn < rgnct; rgn++) {
00249 if (verbose) printf ("processing record %d\n", rgn);
00250 ttrec = ids->records[rgn];
00251 drms_sprint_rec_query (source, ttrec);
00252
00253 pxlocver = drms_getkey_int (ttrec, "PxLocVer", &status);
00254 if (pxlocver < 1 && verbose) {
00255 printf ("Warning: Pixel locator file used for travel time map in record\n");
00256 printf (" %s has unknown version\n", source);
00257 }
00258 if (fitopt == ALL_FITS) {
00259 fprintf (stderr, "Warning: inversions from dual fits not supported\n");
00260 fprintf (stderr, " only Gabor wavelet fits will be used\n");
00261 fitopt = GABOR_WAVELET;
00262 }
00263 ttseg = drms_segment_lookup (ttrec, type_segname[fitopt]);
00264 if (!ttseg) {
00265 fprintf (stderr, "Error: no segment \"%s\" in record %s; skipped\n",
00266 type_segname[fitopt], source);
00267 continue;
00268 }
00269 ttdat = drms_segment_read (ttseg, DRMS_TYPE_FLOAT, &status);
00270 if (!ttdat) {
00271 fprintf (stderr, "Error: unable to read input data record-segment in %s\n",
00272 source);
00273 continue;
00274 }
00275 input = (float *)ttdat->data;
00276
00277 orec = drms_create_record (drms_env, outser, DRMS_PERMANENT, &status);
00278 if (!orec) {
00279 fprintf (stderr, "Error: unable to create record %d in series %s\n", rgn,
00280 outser);
00281 fprintf (stderr, " remainder of processing skipped\n");
00282 drms_close_records (ids, DRMS_FREE_RECORD);
00283 return 0;
00284 }
00285 seg_cs = drms_segment_lookup (orec, "cs");
00286 seg_vx = drms_segment_lookup (orec, "vx");
00287 seg_vy = drms_segment_lookup (orec, "vy");
00288 seg_vz = drms_segment_lookup (orec, "vz");
00289
00290 if (!outok) {
00291
00292 if (!seg_cs) {
00293 docsinv = 0;
00294 if (!dovinv) {
00295 fprintf (stderr, "Error: output data series %s\n", outser);
00296 fprintf (stderr,
00297 " lacks segment \"cs\", and no flow inversion kernels available\n");
00298 drms_close_records (ids, DRMS_FREE_RECORD);
00299 return 1;
00300 }
00301 fprintf (stderr,
00302 "Warning: cs segment missing in output record;\n");
00303 fprintf (stderr, " sound-speed inversions will not be attempted.\n");
00304 }
00305 if (!seg_vx) {
00306 dovinv = 0;
00307 if (!docsinv) {
00308 fprintf (stderr, "Error: output data series %s\n", outser);
00309 fprintf (stderr,
00310 " lacks one or more segments \"vx\", \"vy\", \"vz\"\n");
00311 fprintf (stderr,
00312 " and no sound-speed inversion kernels available\n");
00313 drms_close_records (ids, DRMS_FREE_RECORD);
00314 return 1;
00315 }
00316 fprintf (stderr,
00317 "Warning: one or more v_i segments missing in output record;\n");
00318 fprintf (stderr, " velocity inversions will not be attempted.\n");
00319 }
00320
00321 if (dovinv) {
00322 if (seg_vx->info->naxis != 3 || seg_vy->info->naxis != 3 ||
00323 seg_vz->info->naxis != 3) {
00324 fprintf (stderr, "Error: output data series %s\n", outser);
00325 fprintf (stderr,
00326 " has wrong rank for one or more segments \"vx\", \"vy\", \"vz\"\n");
00327 drms_close_records (ids, DRMS_FREE_RECORD);
00328 return 1;
00329 }
00330 if (seg_vx->info->scope != DRMS_VARDIM) {
00331 if (seg_vx->axis[0] != 256 || seg_vx->axis[1] != 256 ||
00332 seg_vx->axis[2] != 11) {
00333 fprintf (stderr, "Error: in output data series %s\n", outser);
00334 fprintf (stderr,
00335 " dimensions for segment \"vx\" differ from hard-coded values\n");
00336 drms_close_records (ids, DRMS_FREE_RECORD);
00337 return 1;
00338 }
00339 }
00340 if (seg_vy->info->scope != DRMS_VARDIM) {
00341 if (seg_vy->axis[0] != 256 || seg_vy->axis[1] != 256 ||
00342 seg_vy->axis[2] != 11) {
00343 fprintf (stderr, "Error: in output data series %s\n", outser);
00344 fprintf (stderr,
00345 " dimensions for segment \"vy\" differ from hard-coded values\n");
00346 drms_close_records (ids, DRMS_FREE_RECORD);
00347 return 1;
00348 }
00349 }
00350 if (seg_vz->info->scope != DRMS_VARDIM) {
00351 if (seg_vz->axis[0] != 256 || seg_vz->axis[1] != 256 ||
00352 seg_vz->axis[2] != 11) {
00353 fprintf (stderr, "Error: in output data series %s\n", outser);
00354 fprintf (stderr,
00355 " dimensions for segment \"vx\" differ from hard-coded values\n");
00356 drms_close_records (ids, DRMS_FREE_RECORD);
00357 return 1;
00358 }
00359 }
00360 }
00361 if (docsinv) {
00362 if (seg_cs->info->naxis != 3) {
00363 fprintf (stderr, "Error: output data series %s\n", outser);
00364 fprintf (stderr,
00365 " has wrong rank for segment \"cs\"\n");
00366 drms_close_records (ids, DRMS_FREE_RECORD);
00367 return 1;
00368 }
00369 if (seg_cs->info->scope != DRMS_VARDIM) {
00370 if (seg_cs->axis[0] != 256 || seg_cs->axis[1] != 256 ||
00371 seg_cs->axis[2] != 11) {
00372 fprintf (stderr, "Error: in output data series %s\n", outser);
00373 fprintf (stderr,
00374 " dimensions for segment \"cs\" differ from hard-coded values\n");
00375 drms_close_records (ids, DRMS_FREE_RECORD);
00376 return 1;
00377 }
00378 }
00379 }
00380 outok = 1;
00381 }
00382
00383
00384
00385 kstat = 0;
00386 propct = construct_stringlist (propagate_req, ',', ©keylist);
00387 propstd = 0;
00388
00389 for (key_n = 0; key_n < propct; key_n++) {
00390 if (strcmp (copykeylist[key_n], "+"))
00391 kstat += check_and_copy_key (orec, ttrec, copykeylist[key_n]);
00392 else propstd = 1;
00393 }
00394 if (propstd) {
00395
00396 kstat = propagate_keys (orec, ttrec, propagate, keyct);
00397
00398 kstat += copy_prime_keys (orec, ttrec);
00399 }
00400 kstat += check_and_set_key_str (orec, "Module", module_ident);
00401 kstat += check_and_set_key_str (orec, "BLD_VERS", jsoc_version);
00402 kstat += check_and_set_key_time (orec, "Created", CURRENT_SYSTEM_TIME);
00403 kstat += check_and_set_key_str (orec, "Source", source);
00404 kstat += check_and_set_key_str (orec, "Input", ttimds);
00405 kstat += check_and_set_key_str (orec, "Kernels", kernds);
00406 if (kerntyp == KERNTYP_RAY)
00407 kstat += check_and_set_key_str (orec, "KrnAppr", "Raypath");
00408 else if (kerntyp == KERNTYP_BORN)
00409 kstat += check_and_set_key_str (orec, "KrnAppr", "Born");
00410 kstat += check_and_set_key_str (orec, "Fitting", fitting_key[fitopt]);
00411 if (kstat) {
00412 fprintf (stderr, "Error writing key value(s) to record in series %s:\n",
00413 outser);
00414 fprintf (stderr, " series may not have appropriate structure;\n");
00415 fprintf (stderr, " record %d skipped\n", rgn);
00416 drms_close_record (orec, DRMS_FREE_RECORD);
00417 continue;
00418 }
00419
00420
00421 if (docsinv) {
00422 cs_res->bscale = seg_cs->bscale;
00423 cs_res->bzero = seg_cs->bzero;
00424 }
00425 if (dovinv) {
00426 vx_res->bscale = seg_vx->bscale;
00427 vx_res->bzero = seg_vx->bzero;
00428 vy_res->bscale = seg_vy->bscale;
00429 vy_res->bzero = seg_vy->bzero;
00430 vz_res->bscale = seg_vz->bscale;
00431 vz_res->bzero = seg_vz->bzero;
00432 }
00433
00434 for (k=0; k<11; k++) {
00435 pln = k * cols * rows;
00436 pln0 = 4 * k * cols * rows;
00437 pln1 = (4 * k + 1) * cols * rows;
00438 pln2 = (4 * k + 2) * cols * rows;
00439 pln3 = (4 * k + 3) * cols * rows;
00440 for (row = 0; row < rows; row++){
00441 pln += cols;
00442 pln0 += cols;
00443 pln1 += cols;
00444 pln2 += cols;
00445 pln3 += cols;
00446 for (col = 0; col < cols; col++){
00447 oi_mn[pln + col] = input[pln0 + col];
00448 oi[pln + col] = input[pln1 + col];
00449 we[pln + col] = input[pln2 + col];
00450 ns[pln + col] = input[pln3 + col];
00451 }
00452 }
00453 }
00454
00455 if (docsinv) inver_cs_sub_ (oi_mn, kcs, cs);
00456 if (dovinv) {
00457 float latc = drms_getkey_float (ttrec, "LatHG", &status);
00458 float lonc = drms_getkey_float (ttrec, "LonCM", &status);
00459 inver_v_sub_ (oi, we, ns, &lonc, &latc, kvx, kvy, kvz, vx, vy, vz,
00460 &kerntyp);
00461 }
00462
00463 status = 0;
00464 if (docsinv) status += drms_segment_write (seg_cs, cs_res, 0);
00465 if (dovinv) {
00466 status += drms_segment_write (seg_vx, vx_res, 0);
00467 status += drms_segment_write (seg_vy, vy_res, 0);
00468 status += drms_segment_write (seg_vz, vz_res, 0);
00469 }
00470 if (status) {
00471 drms_sprint_rec_query (recid, orec);
00472 fprintf (stderr, "Error writing data to record %s\n", recid);
00473 fprintf (stderr, " series may not have appropriate structure\n");
00474 fprintf (stderr, " record %d skipped\n", rgn);
00475 drms_close_record (orec, DRMS_FREE_RECORD);
00476 continue;
00477 }
00478
00479 drms_close_record (orec, DRMS_INSERT_RECORD);
00480
00481 }
00482 return 0;
00483 }
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516