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 #include <stdio.h>
00056 #include <string.h>
00057 #include <math.h>
00058 #include <jsoc_main.h>
00059 #include "keystuff.c"
00060 #include "rdutil.c"
00061
00062 extern void h1h2_(int *n, int *l, double *om, double *dom,
00063 double *err, double *w, int *nmode, int *no, int *nw,
00064 int *nsol, char *filcsq, char *filsw, char *filout,
00065 int *qverb, int csqlen, int swlen, int outlen);
00066 int get_comp_filepath(double lat, double lon, char *comp, char *filepath, char *compKeyWord);
00067
00068 char *module_name = "rdsinv";
00069 char *version_id = "0.7";
00070
00071 ModuleArgs_t module_args[] = {
00072 {ARG_STRING, "in", "su_rsb.rdfits", "input data set (or file)"},
00073 {ARG_STRING, "seg", "fit.out", "data segment name"},
00074 {ARG_INT, "cr", "Not Specified", "Carrington rotation for all regions"},
00075 {ARG_FLOAT, "clon", "Not Specified", "Carrington longitude of CM for all regions"},
00076 {ARG_FLOAT, "lon", "Not Specified", "Carrington longitude of all regions"},
00077 {ARG_FLOAT, "lat", "Not Specified", "Carrington latitude of all regions"},
00078 {ARG_INT, "no", "6", "number of knots in frequency"},
00079 {ARG_INT, "nw", "10", "number of knots in w (accoustic depth)"},
00080 {ARG_INT, "nsol", "50", "number of solution points"},
00081
00082 {ARG_STRING, "comp", "yale_cb.rdVfitsc_avg", "Comparison frequencies"},
00083 {ARG_STRING, "out", "/tmp", "Output series name or directory name"},
00084 {ARG_FLAG, "v", "", "run in verbose mode"},
00085 {ARG_END}
00086 };
00087
00088
00089 char *propagate[] = {"CarrTime", "CarrRot", "CMLon", "LonHG", "LatHG", "LonCM",
00090 "MidTime", "Duration", "MapProj", "MapScale", "Map_PA", "Width", "Height",
00091 "Size", "Cadence", "ZonalTrk", "ZonalVel", "MeridTrk", "MeridVel", "MAI"};
00092
00093 int get_comp_filepath(double lat, double lon, char *comp, char *filepath, char *compKeyWord) {
00094 int i, j, status, numrec, car, userec;
00095 DRMS_RecordSet_t *recordSet = NULL;
00096 DRMS_Record_t *record;
00097 DRMS_Segment_t *segment;
00098 double distance, latC, lonC, minDistance = 1.0e20;
00099 char *carTime;
00100
00101 FILE *test = fopen (comp, "r");
00102 if (test) {
00103 fclose (test);
00104 sprintf (filepath, "%s", comp);
00105 return 0;
00106 }
00107
00108 recordSet = drms_open_records (drms_env, comp, &status);
00109 if (status) {
00110 fprintf (stderr, "Error: unable to open %s\n", comp);
00111 fprintf (stderr, " as either a data set or a file\n");
00112 return 1;
00113 }
00114
00115 numrec = recordSet->n;
00116
00117 for (i=0; i<numrec; i++) {
00118 record = recordSet->records[i];
00119 latC = drms_getkey_double(record, "LatHG", &status);
00120 if (status) {
00121 fprintf(stderr, "Keyword LatHG not found in comparison series record\n");
00122 return 1;
00123 }
00124 lonC = drms_getkey_double(record, "LonCM", &status);
00125 if (status) {
00126 lonC = drms_getkey_double(record, "LonHG", &status);
00127 if (status) {
00128 fprintf (stderr, "Error: no keyword for longitude found in comparison series record\n");
00129 return 1;
00130 }
00131 }
00132
00133 car = drms_getkey_int(record, "CarrRot", &status);
00134 if (status) {
00135 carTime = drms_getkey_string(record, "CarrTime", &status);
00136 if(status) {
00137 fprintf (stderr, "Keyword CarrTime not found in comparison series record\n");
00138 return 1;
00139 }
00140 sscanf(carTime, "%*i:%i", &car);
00141 }
00142
00143
00144 distance = (lon-lonC)*(lon-lonC)+(lat-latC)*(lat-latC);
00145 if(distance < minDistance) {
00146 minDistance = distance;
00147 userec = i;
00148 sprintf(compKeyWord, "%s[%4.1lf][%4.1lf][%4i]", comp, latC, lonC, (int) car);
00149 }
00150 }
00151 record = recordSet->records[userec];
00152 if (record->sunum != -1LL && record->su == NULL) {
00153 record->su = drms_getunit (record->env, record->seriesinfo->seriesname,
00154 record->sunum, 1, &status);
00155 }
00156 segment = drms_segment_lookup (record, "fit.out");
00157 sprintf (filepath, "%s/S%05i/%s", segment->record->su->sudir, segment->record->slotnum, "fit.out");
00158 fprintf (stderr, "Using comparison region %s\n", compKeyWord);
00159
00160 return 0;
00161 }
00162
00163 int key_is_prime (DRMS_Record_t *rec, const char *key) {
00164 DRMS_Keyword_t *pkey, *keywd;
00165 int n, npct = rec->seriesinfo->pidx_num;
00166
00167 if (!(keywd = drms_keyword_lookup (rec, key, 1))) return 0;
00168 if (npct) {
00169 for (n = 0; n < npct; n++) {
00170 pkey = rec->seriesinfo->pidx_keywords[n];
00171 if (pkey->info->recscope > 1) pkey = drms_keyword_slotfromindex (pkey);
00172 if (strcmp (key, pkey->info->name)) continue;
00173 return 1;
00174 }
00175 }
00176 return 0;
00177 }
00178
00179 int unique_values (const char *dsqry, const char *key, DRMS_Record_t *rec) {
00180 int status, values;
00181 DRMS_Array_t *value;
00182 DRMS_Keyword_t *pkey, *keywd;
00183
00184 if (!(keywd = drms_keyword_lookup (rec, key, 1))) return 0;
00185 value = drms_record_getvector (drms_env, dsqry, key,
00186 DRMS_TYPE_FLOAT, 1, &status);
00187 if (!value) return 0;
00188 values = value->axis[1];
00189 drms_free_array (value);
00190 return values;
00191 }
00192
00193 char *printcrcl_loc (int cr, double cl, double lon, double lat) {
00194 static char fnam[32];
00195 double dlon;
00196
00197 while (lon < 0) lon += 360;
00198 while (lon > 360) lon -= 360;
00199 if (cr > 0) {
00200 while (cl < 0) {
00201 cl += 360;
00202 cr++;
00203 } while (cl > 360) {
00204 cl -= 360;
00205 cr--;
00206 }
00207 dlon = lon - cl;
00208 while (dlon > 180) dlon -= 360;
00209 while (dlon < -180) dlon += 360;
00210 if (dlon >= 0) {
00211 if (lat >= 0) sprintf (fnam, "%04d:%05.1f_%04.1fW%04.1fN", cr, cl, dlon, lat);
00212 else sprintf (fnam, "%04d:%05.1f_%04.1fW%04.1fS", cr, cl, dlon, -lat);
00213 } else {
00214 if (lat >= 0) sprintf (fnam, "%04d:%05.1f_%04.1fE%04.1fN", cr, cl, -dlon, lat);
00215 else sprintf (fnam, "%04d:%05.1f_%04.1fE%04.1fS", cr, cl, -dlon, -lat);
00216 }
00217 } else sprintf (fnam, "%05.1f%+04.1f", lon, lat);
00218 return fnam;
00219 }
00220
00221 int DoIt(void) {
00222 CmdParams_t *params = &cmdparams;
00223 int status = 0;
00224 DRMS_RecordSet_t *recordSet = NULL;
00225 DRMS_Record_t *irec, *orec;
00226 DRMS_Segment_t *segment, *osegment;
00227 FILE *fpt;
00228
00229 char *filcsq = "/home/baldner/src/rdsinv/fort.33";
00230 char *filsw = "/home/baldner/src/rdsinv/fw.out";
00231 int outlen;
00232 int i, j, k, rec_i, drms_input, drms_output;
00233 int *n_in, *n, *n_i, *l, *l_i, *mask;
00234 int npts_in, npts_comp, npts, nrec, seg_ct;
00235 double *l_in, *om_in, *err_in, *om_i, *err_i, *om, *dom, *err, *w;
00236 char *carstr, *suffix;
00237 double latc, lonc, loncCar;
00238 char filename[DRMS_MAXPATHLEN], compfile[DRMS_MAXPATHLEN];
00239 char odir[DRMS_MAXPATHLEN], ofilename[DRMS_MAXPATHLEN];
00240 char compKeyWord[DRMS_MAXPATHLEN];
00241 char rec_query[DRMS_MAXQUERYLEN], source[DRMS_MAXQUERYLEN];
00242 char module_ident[64];
00243
00244 char *in = strdup (params_get_str (params, "in"));
00245 char *seg = strdup (params_get_str (params, "seg"));
00246 char *out = strdup (params_get_str (params, "out"));
00247 char *comp = strdup (params_get_str (params, "comp"));
00248 int cr = params_get_int (params, "cr");
00249 float cl = params_get_float (params, "clon");
00250 float lon = params_get_float (params, "lon");
00251 float lat = params_get_float (params, "lat");
00252 int no = params_get_int (params, "no");
00253 int nw = params_get_int (params, "nw");
00254 int nsol = params_get_int (params, "nsol");
00255 int verbose = params_isflagset (params, "v");
00256
00257 snprintf (module_ident, 64, "%s v %s", module_name, version_id);
00258 if (verbose) printf ("%s:\n", module_ident);
00259
00260 int csqlen = strlen(filcsq);
00261 int swlen = strlen(filsw);
00262
00263 int anycr = (cr <= 0);
00264 int anycl = isnan (cl);
00265 int anylon = isnan (lon);
00266 int anylat = isnan (lat);
00267
00268 drms_input = 1;
00269 if (anycr && anycl && anylon && anylat) {
00270
00271
00272 recordSet = drms_open_records (drms_env, in, &status);
00273 if (status) {
00274 fpt = fopen (in, "r");
00275 if (!fpt) {
00276 fprintf (stderr, "Error: in specification \"%s\"\n", in);
00277 fprintf (stderr, " does not specify a readable data set nor file\n");
00278 return 1;
00279 }
00280 drms_input = 0;
00281 nrec = 1;
00282 } else nrec = recordSet->n;
00283 if (nrec < 1) {
00284 fprintf (stderr, "No data records in set %s\n", in);
00285 return 1;
00286 }
00287 } else {
00288 strncpy (rec_query, in, DRMS_MAXQUERYLEN);
00289 if (!anycr) {
00290 snprintf (source, DRMS_MAXQUERYLEN, "[?CarrRot=%d?]", cr);
00291 strncat (rec_query, source, DRMS_MAXQUERYLEN);
00292 }
00293 if (!anycl) {
00294 float clmin = cl - 0.001;
00295 float clmax = cl + 0.001;
00296
00297 snprintf (source, DRMS_MAXQUERYLEN, "[?CMLon>%g and CMLon<%g?]",
00298 clmin, clmax);
00299 strncat (rec_query, source, DRMS_MAXQUERYLEN);
00300 }
00301 if (!anylon) {
00302 float lonmin = lon - 0.01;
00303 float lonmax = lon + 0.01;
00304
00305 snprintf (source, DRMS_MAXQUERYLEN, "[?LonHG>%g and LonHG<%g?]",
00306 lonmin, lonmax);
00307 strncat (rec_query, source, DRMS_MAXQUERYLEN);
00308 }
00309 if (!anylat) {
00310 float latmin = lat - 0.01;
00311 float latmax = lat + 0.01;
00312 snprintf (source, DRMS_MAXQUERYLEN, "[?LatHG>%g and LatHG<%g?]",
00313 latmin, latmax);
00314 strncat (rec_query, source, DRMS_MAXQUERYLEN);
00315 }
00316 if (!(recordSet = drms_open_records (drms_env, rec_query, &status))) {
00317 fprintf (stderr, "Error: unable to open input data set %s\n", rec_query);
00318 fprintf (stderr, " status = %d\n", status);
00319 return 1;
00320 }
00321 nrec = recordSet->n;
00322 }
00323 printf ("Processing %d records\n", nrec);
00324
00325 if (drms_input) {
00326
00327 irec = recordSet->records[0];
00328 if (anycr) anycr = unique_values (in, "CarrRot", irec) - 1;
00329 if (anycl) anycl = unique_values (in, "CMLon", irec) - 1;
00330 if (anylon) anylon = unique_values (in, "LonHG", irec) - 1;
00331 if (anylat) anylat = unique_values (in, "LatHG", irec) - 1;
00332 }
00333
00334 drms_output = 1;
00335 orec = drms_create_record (drms_env, out, DRMS_PERMANENT, &status);
00336 if (status) {
00337 drms_output = 0;
00338 fprintf (stderr,
00339 "Warning: drms_create_record() returned %d for data series:\n", status);
00340 fprintf (stderr, " %s; will be interpreted as directory name\n", out);
00341 strncpy (odir, out, DRMS_MAXPATHLEN);
00342 } else if (drms_record_directory (orec, odir, 1)) {
00343 fprintf (stderr,
00344 "Error: drms_record_directory() failure: SUMS may not be available\n");
00345 fprintf (stderr,
00346 " or series %s may contain no segments\n", out);
00347 return 1;
00348 }
00349
00350 if (drms_output) {
00351 int kstat = 0;
00352 int keyct = sizeof (propagate) / sizeof (char *);
00353
00354
00355
00356
00357 kstat += check_and_set_key_str (orec, "Module", module_ident);
00358
00359
00360
00361
00362 kstat += check_and_set_key_time (orec, "Created", CURRENT_SYSTEM_TIME);
00363
00364 kstat += check_and_set_key_int (orec, "NumO", no);
00365 kstat += check_and_set_key_int (orec, "NumW", nw);
00366 kstat += check_and_set_key_int (orec, "NumSol", nsol);
00367
00368 if (anycr) {
00369 if (key_is_prime (orec, "CarrRot")) {
00370 fprintf (stderr, "Error: CarrRot is prime key for output series %s\n", out);
00371 fprintf (stderr, " but input data set contains multiple values\n");
00372 drms_close_record (orec, DRMS_FREE_RECORD);
00373
00374 if (drms_input) drms_close_records (recordSet, DRMS_FREE_RECORD);
00375 return 1;
00376 }
00377 } else kstat += check_and_set_key_int (orec, "CarrRot", cr);
00378 if (anycl) {
00379 if (key_is_prime (orec, "CMLon")) {
00380 fprintf (stderr, "Error: CMLon is prime key for output series %s\n", out);
00381 fprintf (stderr, " but input data set contains multiple values\n");
00382 drms_close_record (orec, DRMS_FREE_RECORD);
00383 if (drms_input) drms_close_records (recordSet, DRMS_FREE_RECORD);
00384 return 1;
00385 }
00386 } else kstat += check_and_set_key_double (orec, "CMLon", cl);
00387 if (anycr || anycl) {
00388 if (key_is_prime (orec, "CarrTime")) {
00389 fprintf (stderr, "Error: CarrTime is prime key for output series %s\n", out);
00390 fprintf (stderr, " but input data set contains multiple values\n");
00391 drms_close_record (orec, DRMS_FREE_RECORD);
00392 if (drms_input) drms_close_records (recordSet, DRMS_FREE_RECORD);
00393 return 1;
00394 }
00395 } else {
00396 char crcl[32];
00397 sprintf (crcl, "%d:%03.0f", cr, cl);
00398 kstat += check_and_set_key_str (orec, "CarrTime", crcl);
00399 }
00400 if (anylon) {
00401 if (key_is_prime (orec, "LonHG")) {
00402 fprintf (stderr, "Error: LonHG is prime key for output series %s\n", out);
00403 fprintf (stderr, " but input data set contains multiple values\n");
00404 drms_close_record (orec, DRMS_FREE_RECORD);
00405 if (drms_input) drms_close_records (recordSet, DRMS_FREE_RECORD);
00406 return 1;
00407 }
00408 } else kstat += check_and_set_key_double (orec, "LonHG", lon);
00409 if (anylat) {
00410 if (key_is_prime (orec, "LatHG")) {
00411 fprintf (stderr, "Error: LatHG is prime key for output series %s\n", out);
00412 fprintf (stderr, " but input data set contains multiple values\n");
00413 drms_close_record (orec, DRMS_FREE_RECORD);
00414 if (drms_input) drms_close_records (recordSet, DRMS_FREE_RECORD);
00415 return 1;
00416 }
00417 } else kstat += check_and_set_key_double (orec, "LatHG", lat);
00418
00419 if (kstat) {
00420 fprintf (stderr, "Error writing key value(s) to record in series %s:\n",
00421 out);
00422 fprintf (stderr, " series may not have appropriate structure;\n");
00423 drms_close_record (orec, DRMS_FREE_RECORD);
00424 if (drms_input) drms_close_records (recordSet, DRMS_FREE_RECORD);
00425 return 1;
00426 }
00427 }
00428
00429 for (rec_i=0; rec_i<nrec; rec_i++) {
00430 if (verbose) printf (" Processing record %i\n", rec_i);
00431 if (drms_input) {
00432 irec = recordSet->records[rec_i];
00433 if (irec->sunum != -1LL && irec->su == NULL) {
00434 irec->su = drms_getunit (irec->env, irec->seriesinfo->seriesname,
00435 irec->sunum, 1, &status);
00436 }
00437 segment = drms_segment_lookup(irec, seg);
00438 sprintf (filename, "%s/S%05i/%s", segment->record->su->sudir,
00439 segment->record->slotnum, seg);
00440 fpt = fopen (filename, "r");
00441 drms_sprint_rec_query (source, irec);
00442 }
00443 else {
00444 fpt = fopen (in, "r");
00445 strcpy (source, in);
00446 }
00447 if (!fpt) {
00448 fprintf (stderr, "Error: could not open SUMS file for record\n");
00449 fprintf (stderr, " %s\n", source);
00450 drms_close_records (recordSet, DRMS_FREE_RECORD);
00451 if (drms_output) drms_close_record (orec, DRMS_FREE_RECORD);
00452 return 1;
00453 }
00454 if (verbose) printf (" %s\n", source);
00455 read_fit (fpt, &npts_in, &n_in, &l_in, &om_in, &err_in);
00456 fclose (fpt);
00457 n_i = (int*) malloc(npts_in*sizeof(int));
00458 l_i = (int*) malloc(npts_in*sizeof(int));
00459 om_i = (double*) malloc(npts_in*sizeof(double));
00460 err_i = (double*) malloc(npts_in*sizeof(double));
00461 n = (int*) malloc(npts_in*sizeof(int));
00462 l = (int*) malloc(npts_in*sizeof(int));
00463 om = (double*) malloc(npts_in*sizeof(double));
00464 dom = (double*) malloc(npts_in*sizeof(double));
00465 err = (double*) malloc(npts_in*sizeof(double));
00466 w = (double*) malloc(npts_in*sizeof(double));
00467 interp_freq (n_in, l_in, om_in, err_in, npts_in, n_i, l_i, om_i, err_i,
00468 &npts_in);
00469 free(n_in);
00470 free(l_in);
00471 free(om_in);
00472 free(err_in);
00473 if (drms_input) {
00474 latc = drms_getkey_double (irec, "LatHG", &status);
00475 lonc = drms_getkey_double (irec, "LonHG", &status);
00476 carstr = drms_getkey_string (irec, "CarrTime", &status);
00477 if (status) {
00478 cr = drms_getkey_int (irec, "CarrRot", &status);
00479 if (status) cr = -1;
00480 loncCar = drms_getkey_double (irec, "CMLon", &status);
00481 if (status) loncCar = 2 * lonc;
00482 } else sscanf (carstr, "%i:%lf", &cr, &loncCar);
00483 lonc = loncCar - lonc;
00484 } else latc = lonc = 0.0;
00485
00486 status = get_comp_filepath (latc, lonc, comp, compfile, compKeyWord);
00487
00488
00489 fpt = fopen(compfile, "r");
00490 read_fit(fpt, &npts_comp, &n_in, &l_in, &om_in, &err_in);
00491 fclose(fpt);
00492 freqdif(n_i, n_in, l_i, l_in, om_i, om_in, err_i, err_in, npts_in, npts_comp,
00493 n, l, om, dom, err, &npts);
00494 mask = (int*) malloc(npts*sizeof(int));
00495 for(i=0; i<npts; i++) mask[i] = 0;
00496 autoweed(l, n, om, dom, err, mask, npts);
00497 j = 0;
00498
00499 for(i=0; i<npts; i++) {
00500 if(mask[i]) {
00501 l[j] = l[i];
00502 n[j] = n[i];
00503 om[j] = om[i];
00504 dom[j] = dom[i];
00505 err[j] = err[i];
00506 w[j] = log10(om[i]/(l[i]+0.5)) - 3.0;
00507 j++;
00508 }
00509 }
00510 npts = j;
00511
00512 sprintf (ofilename, "%s/%s", odir, printcrcl_loc (cr, loncCar, lonc + loncCar, latc));
00513 if (verbose) printf ( " Writing solution to %s\n", ofilename);
00514 outlen = strlen (ofilename);
00515
00516 h1h2_(n, l, om, dom, err, w, &npts, &no, &nw, &nsol, filcsq, filsw, ofilename,
00517 &verbose, csqlen, swlen, outlen);
00518
00519 free(n_in);
00520 free(l_in);
00521 free(om_in);
00522 free(err_in);
00523 free(n_i);
00524 free(l_i);
00525 free(om_i);
00526 free(err_i);
00527 free(n);
00528 free(l);
00529 free(om);
00530 free(dom);
00531 free(err);
00532 free(w);
00533 free(mask);
00534 if (verbose) printf (" Closeout record %d...\n", rec_i);
00535 }
00536 if (drms_input) drms_close_records (recordSet, DRMS_FREE_RECORD);
00537 if (drms_output) drms_close_record (orec, DRMS_INSERT_RECORD);
00538 return 0;
00539 }
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556