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
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 char *module_name = "ringfit_ssw";
00161 char *module_desc = "ring fitting using method of Haber and Hindman";
00162 char *version_id = "0.8";
00163
00164 #include <jsoc_main.h>
00165
00166 ModuleArgs_t module_args[] = {
00167 {ARG_DATASET, "in", "mdi.rdVpspec_dp[1988][180][180][0]", "input dataset"},
00168 {ARG_STRING, "out", "fit.out",
00169 "name of output data series (or file) containing fit coefficients"},
00170 {ARG_STRING, "guessfile", "/home/dhaber/coefhrtest.dat",
00171 "name of file containing parameterizations for each fitting parameter"},
00172 {ARG_STRING, "unwrap", "unwrap",
00173 "segment containing optional unwrapped power spectra"},
00174 {ARG_STRING, "filter", "filter",
00175 "segment containing optional spatial wavenumber filter"},
00176 {ARG_STRING, "filtps", "filtps",
00177 "segment containing filtered, subsampled, power spectra"},
00178 {ARG_INT, "nmin", "0", "lowest radial order to fit", "[0,)"},
00179 {ARG_INT, "nmax", "7", "highest radial order to fit", "[0,)"},
00180 {ARG_INT, "npar", "6", "number of parameters to fit", "[6,)"},
00181 {ARG_INT, "ntht", "400",
00182 "number of azimuth values for initial unwrapping of spectrum, usually = 2*pi*nk, should be divisible by 4"},
00183
00184 {ARG_INT, "nthts", "40",
00185 "number of azimuth values after subsampling, generally a factor of 10 less than ntht, should also be divisible by 4"},
00186
00187 {ARG_INT, "nrdtot", "16",
00188 "number of ridges in guess file. as long as there are 16 or less the default should work"},
00189 {ARG_FLOAT, "ux", "0.0", "initial guess for zonal flow speed [m/s]"},
00190 {ARG_FLOAT, "uy", "0.0", "initial guess for merridional flow speed [m/s]"},
00191 {ARG_INT, "smooth", "0", "amount of smoothing wanted [wavenumber bins]"},
00192 {ARG_INT, "mdata", "16",
00193 "number of azimuthal Fourier components to keep when subsampling the data"},
00194 {ARG_INT, "mfilt", "8",
00195 "highest azimuthal Fourier component to use in filtering the data"},
00196 {ARG_INTS, "kstart", "{11, 7, 6, 6, 6, 6, 7, 8}",
00197 "starting kbin values"},
00198 {ARG_INTS, "kstop", "{55, 55, 45, 44, 37, 27, 23, 20}",
00199 "ending kbin values"},
00200 {ARG_FLAG, "f", "0", "write spatial wavenumber filter"},
00201 {ARG_FLAG, "n", "0", "do not write out fits (diagnostics only)"},
00202 {ARG_FLAG, "U", "0", "write unwrapped power spectrum"},
00203 {ARG_FLAG, "v", "0", "verbose output"},
00204 {ARG_FLAG, "x", "0", "really verbose output"},
00205 {ARG_FLAG, "2", "0", "do not use second derivatives in parameter errors"},
00206 {ARG_FLOAT, "xtol", "1e-4", "termination tolerance on fitting parameters"},
00207 {ARG_FLOAT, "ftol", "1e-8", "termination tolerance on fitting function"},
00208 {ARG_FLOAT, "feps", "1e-16", "accuracy of function values from FUNC"},
00209 {ARG_FLOAT, "delta0", "0.0", "initial radius of the trust region"},
00210 {ARG_INT, "maxeval", "750", "maximum number of iterations for each fit"},
00211 {ARG_INT, "igrad", "1", "how to determine gradient, 1=user supplied DF1"},
00212 {ARG_INT, "iextrap", "1", "if iextrap is 1 then safeguarded quadratic/cubic interpolation and extrapolation is performed"},
00213 {ARG_INT, "idiag", "0", "if idiag is 0 then I is used as the initial approximation to the Hessian"},
00214 {ARG_INT, "iprint", "0", "if iprint=1 then information is printed after each iteration"},
00215 {ARG_END}
00216 };
00217
00218 extern void ringanalysis_ (float *powr, int *nkx, int *nky, int *nnu,
00219 int *ntht, int *nthts, int *nk, float *crpix1, float *crpix2, int *smooth,
00220 double *dk, double *dnu, float *lnbase, int *nmin, int *nmax,
00221 int *npar, int *ntot, double *ux_guess, double *uy_guess, float *ux_fit,
00222 float *d_ux, float *uy_fit, float *d_uy, float *amp, float *d_amp, float *bg,
00223 float *d_bg, float *fwhm, float *d_fwhm, float *nu, float *d_nu,
00224 float *delnu, int *nval, float *kval, int *kbin, int *kbstrt, int *kbend,
00225 int *kbmin, int *kbmax, int *nrdg, float *min_func, int *n_iter, int *fit_chk,
00226 float *chi, float *kmin, float *kmax, int *modecount, int *good, float *unwrapped_pspec,
00227 float *filter, float *filtered_pspec, double *fnu, double *width, double *ampg,
00228 double *bkg, int *verbose, int *mdata, int *mfilt, double *doptions, int *ioptions,
00229 int *iflg, int *rgn, int *nrdtot, int *status);
00230
00231
00232 char *propagate[] = {"CarrTime", "CarrRot", "CMLon", "LonHG", "LatHG", "LonCM",
00233 "MidTime", "Duration", "Cadence", "LonSpan",
00234 "T_START", "T_STOP", "Coverage", "Size", "Width", "Height",
00235 "ZonalTrk", "ZonalVel", "MeridTrk", "MeridVel",
00236 "MapScale", "MapProj", "Map_PA", "RSunRef", "PosAng", "MAI",
00237 "Apode_f", "APOD_MIN", "APOD_MAX"};
00238
00239 #include "keystuff.c"
00240 #include "read_guess.c"
00241
00242 #define RSUN_MM (696.0)
00243 #define NOFITS (0x80000000)
00244
00245 int DoIt (void) {
00246 CmdParams_t *params = &cmdparams;
00247 DRMS_RecordSet_t *ids;
00248 DRMS_Record_t *irec, *orec;
00249 DRMS_Segment_t *iseg, *oseg;
00250 DRMS_Array_t *pspec;
00251 DRMS_Array_t *filtps_array = NULL, *filter_array = NULL, *unwrap_array = NULL;
00252 FILE *unit24, *runlog;
00253 float *filtered_pspec;
00254 double doptions[4];
00255 double dk, dnu, dklast;
00256 double *fnu, *width, *ampg, *bkg;
00257 double *fnua, *fwa, *pa, *bga;
00258 float *spec;
00259 float *unwrapped_pspec, *filter;
00260 float *ux_fit, *d_ux, *uy_fit, *d_uy;
00261 float *amp, *d_amp, *bg, *d_bg, *fwhm, *d_fwhm;
00262 float *nu, *d_nu, *delnu, *chi, *min_func, *kval;
00263 float lnbase, crpix1, crpix2;
00264 float rsun, kmin, kmax, lmin, lmax, ell, nkc;
00265 unsigned int quality;
00266 int *nval, *n_iter, *fit_chk, *good, *nvct;
00267 int *kbstrt, *kbend, *kbin;
00268 int kbmin, kbmax;
00269 int ioptions[5], m[4];
00270 int modecount;
00271 int rgn, rgnct, segct, drms_output, dispose;
00272 int isegnum, osegnum, logsegnum, filtsegnum;
00273 int nmalloc, nloopmalloc;
00274 int unwrapsegnum, filtpowsegnum;
00275 int n, nkx, nky, nnu, nk, ntot, ma;
00276 int nrdg, i, j;
00277 int status;
00278 char logfile[DRMS_MAXPATHLEN], outfile[DRMS_MAXPATHLEN];
00279 char recid[DRMS_MAXQUERYLEN];
00280 char module_ident[64], key[64];
00281
00282 char *inds = strdup (params_get_str (params, "in"));
00283 char *outser = strdup (params_get_str (params, "out"));
00284 char *guessfile = strdup (params_get_str (params, "guessfile"));
00285 char *unwrap_segment = strdup (params_get_str (params, "unwrap"));
00286 char *filter_segment = strdup (params_get_str (params, "filter"));
00287 char *filtered_segment = strdup (params_get_str (params, "filtps"));
00288 int nmin = params_get_int (params, "nmin");
00289 int nmax = params_get_int (params, "nmax");
00290 int npar = params_get_int (params, "npar");
00291 int ntht = params_get_int (params, "ntht");
00292 int nthts = params_get_int (params, "nthts");
00293 int nrdtot = params_get_int (params, "nrdtot");
00294 double ux_guess = params_get_double (params, "ux");
00295 double uy_guess = params_get_double (params, "uy");
00296 int smooth = params_get_int (params, "smooth");
00297 int kstrtct = params_get_int (params, "kstart_nvals");
00298 int kstopct = params_get_int (params, "kstop_nvals");
00299
00300 int write_filter = params_isflagset (params, "f");
00301 int no_fits = params_isflagset (params, "n");
00302 int write_unwrap_pow = params_isflagset (params, "U");
00303 int write_filt_pow = params_isflagset (params, "u");
00304 int verbose = params_isflagset (params, "v");
00305 int Verbose = params_isflagset (params, "x");
00306
00307 int mdata = params_get_int (params, "mdata");
00308 int mfilt = params_get_int (params, "mfilt");
00309 double xtol = params_get_double (params, "xtol");
00310 double ftol = params_get_double (params, "ftol");
00311 double feps = params_get_double (params, "feps");
00312 double delta0 = params_get_double (params, "delta0");
00313 int maxeval = params_get_int (params, "maxeval");
00314 int igrad = params_get_int (params, "igrad");
00315 int iextrap = params_get_int (params, "iextrap");
00316 int idiag = params_get_int (params, "idiag");
00317 int iprint = params_get_int (params, "iprint");
00318 int iflg = params_isflagset (params, "2");
00319
00320 if (verbose)
00321 fprintf(stderr," guessfile = %s \n", guessfile);
00322 snprintf (module_ident, 64, "%s v%s", module_name, version_id);
00323 if (verbose) {
00324 printf ("%s version %s\n", module_name, version_id);
00325 printf ("fits of %s\n", inds);
00326 if (no_fits)
00327 printf ("(diagnostic run only, no records will be written to DRMS)\n");
00328 }
00329
00330 ioptions[0] = maxeval;
00331 ioptions[1] = igrad;
00332 ioptions[2] = iextrap;
00333 ioptions[3] = idiag;
00334 ioptions[4] = iprint;
00335 doptions[0] = xtol;
00336 doptions[1] = ftol;
00337 doptions[2] = feps;
00338 doptions[3] = delta0;
00339
00340 if (nmax + 1 > nrdtot) {
00341 fprintf (stderr, "Error: not enough ridges in guess file\n");
00342 return 0;
00343 }
00344 if (kstopct != kstrtct) {
00345 fprintf (stderr, "Warning: kstop vals (%d) != kstart vals (%d);", kstopct,
00346 kstrtct);
00347 if (kstrtct < kstopct) kstopct = kstrtct;
00348 else kstrtct = kstopct;
00349 fprintf (stderr, " reduced to %d\n", kstopct);
00350 }
00351 nrdg = kstopct;
00352 if (nrdg <= nmax) {
00353 fprintf (stderr,
00354 "Warning: insufficient kstart/kstop values for requested range\n");
00355 fprintf (stderr, " nmax reduced from %d to %d\n", nmax, nrdg - 1);
00356 nmax = nrdg - 1;
00357 if (nrdg <= nmax) {
00358 fprintf (stderr, "Error: still insufficient kstart/kstop values \n");
00359 return 0;
00360 }
00361 }
00362 if (nmin > nmax) {
00363 fprintf (stderr, "Warning: nmax %d < nmin %d)\n", nmax, nmin);
00364 fprintf (stderr, "setting nmax=nmin");
00365 nmax = nmin;
00366 }
00367 if (ntht < nthts) {
00368 fprintf (stderr, "Error: ntht %d < nthts %d\n", ntht, nthts);
00369 return 1;
00370 }
00371 if (npar != 6) {
00372 fprintf (stderr, "Error: only 6 fitting parameters possible at this time\n");
00373 fprintf (stderr, "You'll need to change subroutines and guess table\n");
00374 fprintf (stderr, "if you want to try other parameters or functional forms\n");
00375 return 0;
00376 }
00377
00378 nvct = (int *)malloc ((nmax - nmin + 1) * sizeof (int));
00379 kbstrt = (int *)malloc (nrdg * sizeof (int));
00380 kbend = (int *)malloc (nrdg * sizeof (int));
00381 for (n = 0; n < nrdg; n++) {
00382 snprintf (key, 64, "kstart_%d_value", n);
00383 kbstrt[n] = params_get_int (params, key);
00384 snprintf (key, 64, "kstop_%d_value", n);
00385 kbend[n] = params_get_int (params, key);
00386 }
00387
00388 ids = drms_open_records (drms_env, inds, &status);
00389 if (status) {
00390 fprintf (stderr, "Error: drms_open_records returned %d for dataset:\n", status);
00391 fprintf (stderr, " %s\n", inds);
00392 return 0;
00393 }
00394 rgnct = ids->n;
00395 if (rgnct < 1) {
00396 fprintf (stderr, "dataset %s contains no records\n", inds);
00397 drms_close_records (ids, DRMS_FREE_RECORD);
00398 return 0;
00399 }
00400
00401
00402
00403 orec = drms_create_record (drms_env, outser, DRMS_TRANSIENT, &status);
00404 if (orec) {
00405 segct = drms_record_numsegments (orec);
00406 if (segct < 1) {
00407 fprintf (stderr,
00408 "Error: no data segment in output series %s\n", outser);
00409 drms_close_records (ids, DRMS_FREE_RECORD);
00410 drms_close_record (orec, DRMS_FREE_RECORD);
00411 return 1;
00412 }
00413 oseg = drms_segment_lookup (orec, "fit.out");
00414 if (oseg && oseg->info->protocol == DRMS_GENERIC)
00415 osegnum = oseg->info->segnum;
00416 else {
00417
00418 for (n = 0; n < segct; n++) {
00419 oseg = drms_segment_lookupnum (orec, n);
00420 if (oseg->info->protocol != DRMS_GENERIC) continue;
00421 osegnum = n;
00422 break;
00423 }
00424 if (n == segct) {
00425 fprintf (stderr,
00426 "Error: no generic data segment in output series %s\n", outser);
00427 drms_close_records (ids, DRMS_FREE_RECORD);
00428 return 1;
00429 }
00430 fprintf (stderr, " writing to segment %s.\n", oseg->info->name);
00431 }
00432 logsegnum = -1;
00433 oseg = drms_segment_lookup (orec, "Log");
00434 if (oseg && oseg->info->protocol == DRMS_GENERIC)
00435 logsegnum = oseg->info->segnum;
00436
00437 if (write_unwrap_pow) {
00438 oseg = drms_segment_lookup (orec, unwrap_segment);
00439 if (oseg && oseg->info->protocol != DRMS_GENERIC && oseg->info->naxis == 3)
00440 unwrapsegnum = oseg->info->segnum;
00441 else {
00442 fprintf (stderr,
00443 "Warning: unwrapped spectrum requested, but no appropriate segment\n");
00444 fprintf (stderr, " %s in output series %s\n", unwrap_segment, outser);
00445 write_unwrap_pow = 0;
00446 }
00447 }
00448 filtsegnum = -1;
00449 if (write_filter) {
00450 oseg = drms_segment_lookup (orec, filter_segment);
00451 if (oseg && oseg->info->protocol != DRMS_GENERIC && oseg->info->naxis == 2)
00452 filtsegnum = oseg->info->segnum;
00453 else {
00454 fprintf (stderr,
00455 "Warning: filter file requested, but no appropriate segment\n");
00456 fprintf (stderr, " %s in output series %s\n", filter_segment, outser);
00457 write_filter = 0;
00458 }
00459 }
00460 if (write_filt_pow) {
00461 oseg = drms_segment_lookup (orec, filtered_segment);
00462 if (oseg && oseg->info->protocol != DRMS_GENERIC && oseg->info->naxis == 3)
00463 filtpowsegnum = oseg->info->segnum;
00464 else {
00465 fprintf (stderr,
00466 "Warning: filtered spectrum requested, but no appropriate segment\n");
00467 fprintf (stderr, " %s in output series %s\n", filtered_segment, outser);
00468 write_filt_pow = 0;
00469 }
00470 }
00471 drms_close_record (orec, DRMS_FREE_RECORD);
00472 drms_output = 1;
00473 if (verbose)
00474 printf("rgnct = %d \n",rgnct);
00475 if (verbose && (rgnct > 1)) printf ("%d records in input dataset %s\n",
00476 rgnct, inds);
00477 } else {
00478
00479 if (rgnct > 1) {
00480 fprintf (stderr,
00481 "Query produced %d matching records with output to single file;",
00482 rgnct);
00483 fprintf (stderr, " must limit to 1\n");
00484 drms_close_records (ids, DRMS_FREE_RECORD);
00485 return 1;
00486 }
00487 strcpy (outfile, outser);
00488 drms_output = 0;
00489 }
00490 dispose = (no_fits) ? DRMS_FREE_RECORD : DRMS_INSERT_RECORD;
00491
00492
00493 irec = ids->records[0];
00494 dk = drms_getkey_double (irec, "DELTA_K", &status);
00495 nkc = drms_getkey_float (irec, "CRPIX1", &status);
00496 nk = nkc - 0.5;
00497 status = read_guess (guessfile, nrdtot, m, &fnua, &fwa, &pa, &bga, &ma,
00498 verbose);
00499 if (status) {
00500 fprintf (stderr, "Error: problem reading guessfile %s\n", guessfile);
00501 fprintf (stderr, " read_guess() returned %d\n", status);
00502 drms_close_records (ids, DRMS_FREE_RECORD);
00503 return 1;
00504 }
00505
00506 fnu = (double *)calloc (nk * nrdtot , sizeof (double));
00507 width = (double *)calloc (nk * nrdtot , sizeof (double));
00508 ampg = (double *)calloc (nk * nrdtot , sizeof (double));
00509 bkg = (double *)calloc (nk * nrdtot , sizeof (double));
00510 ntot = nmax - nmin + 1;
00511 kbmin = min_arr(kbstrt, ntot);
00512 kbmax = max_arr(kbend, ntot);
00513 status = make_table (fnua, fwa, pa, bga, fnu, width, ampg, bkg, m, nk, dk,
00514 nrdtot, kbmin, kbmax, ma, verbose);
00515 if (status) {
00516 fprintf (stderr, "Error: problem making table of guesses\n");
00517 drms_close_records (ids, DRMS_FREE_RECORD);
00518 return 1;
00519 }
00520 dklast = dk;
00521
00522
00523 nmalloc = 0;
00524 for (rgn = 0; rgn < rgnct; rgn++) {
00525 irec = ids->records[rgn];
00526 drms_sprint_rec_query (recid, irec);
00527 if (verbose && (rgnct > 1)) {
00528 printf ("processing record %d:\n %s\n", rgn, recid);
00529 }
00530 dk = drms_getkey_double (irec, "DELTA_K", &status);
00531 dnu = drms_getkey_double (irec, "DELTA_NU", &status);
00532 lnbase = drms_getkey_float (irec, "LOG_BASE", &status);
00533 crpix1 = drms_getkey_float (irec, "CRPIX1", &status);
00534 crpix2 = drms_getkey_float (irec, "CRPIX2", &status);
00535 rsun = drms_getkey_float (irec, "RSunRef", &status);
00536 if (status || isnan (rsun)) {
00537 rsun = RSUN_MM;
00538 fprintf (stderr, "Warning: no valid value for RSunRef; ");
00539 fprintf (stderr, "using %f\n", rsun);
00540 }
00541 if (dk != dklast) {
00542 status = make_table(fnua, fwa, pa, bga, fnu, width,
00543 ampg, bkg, m, nk, dk, nrdtot, kbmin, kbmax, ma, verbose);
00544 if (status) {
00545 printf("Problem making table of guesses \n");
00546 return 1;
00547 }
00548 }
00549 dklast = dk;
00550 if (verbose) printf("dk = %lf dklast = %lf \n", dk, dklast);
00551
00552 segct = irec->segments.num_total;
00553
00554 if (segct != 1) {
00555 for (n = 0; n < segct; n++) {
00556 iseg = drms_segment_lookupnum (irec, n);
00557 if (iseg->info->naxis == 3) break;
00558 isegnum = n;
00559 }
00560 if (verbose) printf("number of segments %d\n",segct);
00561 if (n >= segct) {
00562 fprintf (stderr, "found no segmemt of rank 3 in input dataset\n");
00563 drms_close_records (ids, DRMS_FREE_RECORD);
00564 return 1;
00565 }
00566 } else {
00567 isegnum = 0;
00568 iseg = drms_segment_lookupnum (irec, isegnum);
00569 }
00570 if (!iseg) {
00571 fprintf (stderr, "Error, could not open data segment\n");
00572 drms_close_records (ids, DRMS_FREE_RECORD);
00573 return 1;
00574 }
00575 pspec = drms_segment_read (iseg, DRMS_TYPE_FLOAT, &status);
00576 if (status) {
00577 fprintf (stderr,
00578 "Error, could not read data segment from input record %d\n", rgn);
00579 drms_close_records (ids, DRMS_FREE_RECORD);
00580 return 1;
00581 }
00582
00583 nkx = pspec->axis[0];
00584 nky = pspec->axis[1];
00585 nnu = pspec->axis[2];
00586 spec = (float *)pspec->data;
00587 nk = nkx/2;
00588
00589 if (verbose) {
00590 printf("nkx = %d nky = %d nnu = %d nk = %d ntot = %d\n",nkx,nky,nnu,nk,ntot);
00591 }
00592 for (n = 0; n < nrdg; n++) {
00593 if (kbend[n] > nk) {
00594 fprintf(stderr, "Error: kstop value %d > nk = %d \n",kbend[n], nk);
00595 return 1;
00596 }
00597 }
00598 if (nmalloc == 0) {
00599 unwrapped_pspec = (float *)malloc (ntht * nk * nnu * sizeof (float));
00600 filter = (float *)malloc (nthts * nk * sizeof (float));
00601 ux_fit = (float *)malloc (nk * ntot * sizeof (float));
00602 d_ux = (float *)malloc (nk * ntot * sizeof (float));
00603 uy_fit = (float *)malloc (nk * ntot * sizeof (float));
00604 d_uy = (float *)malloc (nk * ntot * sizeof (float));
00605 amp = (float *)malloc (nk * ntot * sizeof (float));
00606 d_amp = (float *)malloc (nk * ntot * sizeof (float));
00607 bg = (float *)malloc (nk * ntot * sizeof (float));
00608 d_bg = (float *)malloc (nk * ntot * sizeof (float));
00609 fwhm = (float *)malloc (nk * ntot * sizeof (float));
00610 d_fwhm = (float *)malloc (nk * ntot * sizeof (float));
00611 nu = (float *)malloc (nk * ntot * sizeof (float));
00612 d_nu = (float *)malloc (nk * ntot * sizeof (float));
00613 delnu = (float *)malloc (nk * ntot * sizeof (float));
00614 nval = (int *)malloc (nk * ntot * sizeof (int));
00615 kval = (float *)malloc (nk * ntot * sizeof (float));
00616 kbin = (int *)malloc (nk * ntot * sizeof (int));
00617 good = (int *)malloc (nk * ntot * sizeof (int));
00618 min_func = (float *)malloc (nk * ntot * sizeof (float));
00619 n_iter = (int *)malloc (nk * ntot * sizeof (int));
00620 fit_chk = (int *)malloc (nk * ntot * sizeof (int));
00621 chi = (float *)malloc (nk * ntot * sizeof (float));
00622 filtered_pspec = (float*)malloc (nthts * nk * nnu * sizeof (float));
00623 nmalloc = ntht * nk * nnu;
00624 } else {
00625 nloopmalloc = ntht * nk * nnu;
00626 if (nloopmalloc != nmalloc) {
00627 fprintf (stderr, "Warning: bad record, go to next one");
00628 fprintf (stderr, " dimensions are not the same");
00629 continue;
00630 }
00631
00632 }
00633
00634 if (drms_output) {
00635 int kstat = 0;
00636 int keyct = sizeof (propagate) / sizeof (char *);
00637 char *key_str, *suffix;
00638 double key_dbl;
00639 int key_int, crcl_known = 1;
00640 TIME key_tim;
00641
00642 orec = drms_create_record (drms_env, outser, DRMS_PERMANENT, &status);
00643 oseg = drms_segment_lookupnum (orec, osegnum);
00644 drms_segment_filename (oseg, outfile);
00645
00646 suffix = strstr (outfile, ".generic");
00647 if (suffix) *suffix = '\0';
00648 if (logsegnum >= 0) {
00649 oseg = drms_segment_lookupnum (orec, logsegnum);
00650 drms_segment_filename (oseg, logfile);
00651
00652 suffix = strstr (logfile, ".generic");
00653 if (suffix) *suffix = '\0';
00654 }
00655
00656
00657 kstat += propagate_keys (orec, irec, propagate, keyct);
00658
00659 key_str = drms_getkey_string (irec, "CarrTime", &status);
00660 if (status) {
00661 key_int = drms_getkey_int (irec, "CarrRot", &status);
00662 if (status) crcl_known = 0;
00663 else {
00664 key_dbl = drms_getkey_double (irec, "CMLon", &status);
00665 if (status) crcl_known = 0;
00666 }
00667 if (crcl_known) {
00668 char CarrTime[9];
00669 snprintf (CarrTime, 9, "%d:%03.0f", key_int, key_dbl);
00670 kstat += check_and_set_key_str (orec, "CarrTime", CarrTime);
00671 }
00672 } else {
00673 key_int = drms_getkey_int (irec, "CarrRot", &status);
00674 if (status) {
00675 sscanf (key_str, "%d:%lf", &key_int, &key_dbl);
00676 kstat += check_and_set_key_int (orec, "CarrRot", key_int);
00677 kstat += check_and_set_key_double (orec, "CMLon", key_dbl);
00678 }
00679 }
00680 kstat += check_and_set_key_str (orec, "Module", module_ident);
00681 kstat += check_and_set_key_str (orec, "BLD_VERS", jsoc_version);
00682 kstat += check_and_set_key_str (orec, "Source", recid);
00683 kstat += check_and_set_key_time (orec, "Created", CURRENT_SYSTEM_TIME);
00684
00685 kstat += check_and_set_key_int (orec, "n_min", nmin);
00686 kstat += check_and_set_key_int (orec, "n_max", nmax);
00687 kstat += check_and_set_key_int (orec, "n_param", npar);
00688 kstat += check_and_set_key_int (orec, "n_ang", ntht);
00689 kstat += check_and_set_key_int (orec, "n_ang_subsmpl", nthts);
00690 kstat += check_and_set_key_str (orec, "GuessTable", guessfile);
00691 kstat += check_and_set_key_int (orec, "GuessRidges", nrdtot);
00692 kstat += check_and_set_key_float (orec, "Ux_guess", ux_guess);
00693 kstat += check_and_set_key_float (orec, "Uy_guess", uy_guess);
00694 kstat += check_and_set_key_int (orec, "Smoothing", smooth);
00695 kstat += check_and_set_key_int (orec, "m_data", mdata);
00696 kstat += check_and_set_key_int (orec, "m_max_filt", mfilt);
00697 kstat += check_and_set_key_float (orec, "FitParmTol", xtol);
00698 kstat += check_and_set_key_float (orec, "FitFuncTol", ftol);
00699 kstat += check_and_set_key_float (orec, "FitFuncPrec", feps);
00700 kstat += check_and_set_key_float (orec, "trust", delta0);
00701 kstat += check_and_set_key_int (orec, "iter_max", maxeval);
00702 kstat += check_and_set_key_int (orec, "GradFlag", igrad);
00703 kstat += check_and_set_key_int (orec, "IntrpFlag", iextrap);
00704 kstat += check_and_set_key_int (orec, "HessianFlag", idiag);
00705 if (kstat) {
00706 fprintf (stderr, "Error writing key value(s) to record %d in series %s\n",
00707 rgn, outser);
00708 fprintf (stderr, " series may not have appropriate structure\n");
00709 drms_close_records (ids, DRMS_FREE_RECORD);
00710 drms_close_record (orec, DRMS_FREE_RECORD);
00711 return 1;
00712 }
00713 }
00714
00715
00716 status = 0;
00717
00718 ioptions[0] = maxeval;
00719
00720 ringanalysis_ (spec, &nkx, &nky, &nnu, &ntht, &nthts, &nk, &crpix1, &crpix2,
00721 &smooth, &dk, &dnu, &lnbase, &nmin, &nmax, &npar, &ntot, &ux_guess,
00722 &uy_guess, ux_fit, d_ux, uy_fit, d_uy, amp, d_amp, bg, d_bg, fwhm,
00723 d_fwhm, nu, d_nu, delnu, nval, kval, kbin,
00724 kbstrt, kbend, &kbmin, &kbmax, &nrdg, min_func, n_iter, fit_chk,
00725 chi, &kmin, &kmax, &modecount, good, unwrapped_pspec, filter,
00726 filtered_pspec, fnu, width, ampg, bkg, &Verbose, &mdata, &mfilt,
00727 doptions, ioptions, &iflg, &rgn, &nrdtot, &status);
00728
00729 drms_free_array (pspec);
00730 if (status) {
00731 quality = NOFITS;
00732 if (status < 10) {
00733 fprintf (stderr, "Unknown problem: code %d", status);
00734 quality |= 0x40000000;
00735 } else if (status < 20) {
00736 fprintf (stderr, "Problem unwrapping power spectrum: code %d", status);
00737 fprintf (stderr, " (set in cart_to_polar subroutine)");
00738 quality |= 0x20000000;
00739 } else if (status < 30) {
00740 fprintf (stderr,
00741 "Problem subsampling and filtering spectrum: code %d", status);
00742 fprintf (stderr, " (set in fourier_filter subroutine)");
00743 quality |= 0x10000000;
00744 } else if (status == 350) {
00745 fprintf (stderr, "Problem code %d - no fits for power spectrum\n", status);
00746 fprintf (stderr, " %s\n", recid);
00747 quality |= 0x08000000;
00748 } else {
00749 fprintf (stderr, "Problem fitting power spectrum: code %d", status);
00750 fprintf (stderr, " (set in hmifit or subsidiary subroutine)");
00751 quality |= 0x04000000;
00752 }
00753 if (drms_output) {
00754 drms_setkey_int (orec, "Quality", quality);
00755 for (n = nmin; n < nmax; n++) {
00756 snprintf (key, 64, "n_%d_fits", n);
00757 drms_setkey_int (orec, key, 0);
00758 }
00759 drms_close_record (orec, dispose);
00760 }
00761 continue;
00762 }
00763 quality = 0;
00764 if (drms_output) {
00765 drms_setkey_int (orec, "Quality", quality);
00766 }
00767
00768 unit24 = fopen (outfile, "w");
00769 if (!unit24) {
00770 fprintf (stderr, "Error: could not open file %s for output\n", outfile);
00771 drms_close_records (ids, DRMS_FREE_RECORD);
00772 if (drms_output) drms_close_record (orec, DRMS_FREE_RECORD);
00773 return 1;
00774 }
00775 if (verbose) printf ("rgn = %d\n", rgn);
00776
00777 lmin = kmin * rsun;
00778 lmax = kmax * rsun;
00779
00780 fprintf (unit24, "# input record = %s\n", recid);
00781 if (drms_output) {
00782 drms_sprint_rec_query (recid, orec);
00783 fprintf (unit24, "# output record = %s\n", recid);
00784 } else fprintf (unit24, "# output file = %s\n", outfile);
00785 fprintf (unit24, "# program used = %s\n", module_ident);
00786
00787 fprintf (unit24, "# version %s\n", version_id);
00788 fprintf (unit24, "# guess file used = %s\n", guessfile);
00789 fprintf (unit24, "# nmin = %2d nmax = %2d \n", nmin, nmax);
00790 fprintf (unit24, "# lmin = %8.4f lmax = %8.4f kmin = %8.4f kmax = %8.4f \n", lmin, lmax, kmin, kmax);
00791 fprintf (unit24, "# delta_nu = %13.4e delta_k = %13.4e \n", dnu, dk);
00792 fprintf (unit24, "# number of modes fit = %d\n", modecount);
00793 fprintf (unit24, "#n l k nu d_nu ");
00794 fprintf (unit24, " ux d_ux uy d_uy fit amp");
00795 fprintf (unit24, " d_amp bg d_bg fwhm");
00796 fprintf (unit24, " d_fwhm delnu d_nu k_bin nfe");
00797 fprintf (unit24, " min_func rdchi\n");
00798
00799 for (n = nmin; n <= nmax; n++) nvct[n] = 0;
00800 for (n = 0; n < ntot*nk; n++) {
00801 if (good[n] == 1) {
00802 ell = kval[n] * rsun;
00803 fprintf (unit24, "%2d %8.3f %8.5f", nval[n], ell, kval[n]);
00804 fprintf (unit24, "%10.3f%12.4e", nu[n], d_nu[n]);
00805 fprintf (unit24, "%12.4e%12.4e", ux_fit[n], d_ux[n]);
00806 fprintf (unit24, "%12.4e%12.4e", uy_fit[n], d_uy[n]);
00807 fprintf (unit24, "%3d%12.4e%12.4e", fit_chk[n], amp[n], d_amp[n]);
00808 fprintf (unit24, "%12.4e%12.4e", bg[n], d_bg[n]);
00809 fprintf (unit24, "%12.4e%12.4e", fwhm[n], d_fwhm[n]);
00810 fprintf (unit24, "%12.4e%12.4e", delnu[n], d_nu[n]);
00811 fprintf (unit24, "%5d%6d%12.4e", kbin[n], n_iter[n], min_func[n]);
00812 fprintf (unit24, "%12.3e\n", chi[n]);
00813 nvct[nval[n] - nmin]++;
00814 }
00815 }
00816 fclose (unit24);
00817 if (drms_output) {
00818 for (n = nmin; n <= nmax; n++) {
00819 sprintf (key, "n_%d_fits", n);
00820 drms_setkey_int (orec, key, nvct[n]);
00821 }
00822 }
00823
00824 if (write_filter) {
00825 if (!filter_array) {
00826 int axes[] = {nthts, nk};
00827 filter_array = drms_array_create (DRMS_TYPE_FLOAT, 2, axes, filter,
00828 &status);
00829 if (status) {
00830 fprintf (stderr, "Error: couldn't create output array for filter\n");
00831 return 1;
00832 }
00833 }
00834 oseg = drms_segment_lookupnum (orec, filtsegnum);
00835 printf ("writing filter array to seg num %d\n", filtsegnum);
00836
00837 if (drms_segment_write (oseg, filter_array, 0)) {
00838 fprintf (stderr, "Error writing data to record in series %s\n",
00839 outser);
00840 fprintf (stderr,
00841 " series may not have appropriate structure\n");
00842 return 1;
00843 }
00844 }
00845 if (write_unwrap_pow) {
00846 if (!unwrap_array) {
00847 int axes[] = {ntht, nk, nnu};
00848 unwrap_array = drms_array_create (DRMS_TYPE_FLOAT, 3, axes, unwrapped_pspec,
00849 &status);
00850 if (status) {
00851 fprintf (stderr,
00852 "Error: couldn't create output array for unwrapped power spectrum\n");
00853 return 1;
00854 }
00855 }
00856 oseg = drms_segment_lookupnum (orec, unwrapsegnum);
00857 printf ("writing unwrapped power to seg num %d\n", unwrapsegnum);
00858 if (drms_segment_write (oseg, unwrap_array, 0)) {
00859 fprintf (stderr, "Error writing data to record in series %s\n",
00860 outser);
00861 fprintf (stderr,
00862 " series may not have appropriate structure\n");
00863 return 1;
00864 }
00865 printf ("succeeded\n");
00866 }
00867 if (write_filt_pow) {
00868 if (!filtps_array) {
00869 int axes[] = {nthts, nk, nnu};
00870 filtps_array = drms_array_create (DRMS_TYPE_FLOAT, 3, axes,
00871 filtered_pspec, &status);
00872 if (status) {
00873 fprintf (stderr,
00874 "Error: couldn't create output array for filtered power spectrum\n");
00875 return 1;
00876 }
00877 }
00878 oseg = drms_segment_lookupnum (orec, filtpowsegnum);
00879 printf ("writing filtered unwrapped power to seg num %d\n", filtpowsegnum);
00880 if (drms_segment_write (oseg, filtps_array, 0)) {
00881 fprintf (stderr, "Error writing data to record in series %s\n", outser);
00882 fprintf (stderr,
00883 " series may not have appropriate structure\n");
00884 return 1;
00885 }
00886 printf ("succeeded\n");
00887 }
00888
00889 if (drms_output) drms_close_record (orec, dispose);
00890 else if (write_filt_pow || write_unwrap_pow || write_filter) {
00891 fprintf (stderr,
00892 "Warning: output of unwrapped spectrum and/or filter requested,\n");
00893 fprintf (stderr, " but this is only supported in DRMS\n");
00894 }
00895 }
00896 drms_close_records (ids, DRMS_FREE_RECORD);
00897 return 0;
00898
00899 }
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932