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 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <time.h>
00036 #include <sys/time.h>
00037 #include <math.h>
00038 #include <string.h>
00039 #include "jsoc_main.h"
00040 #include <omp.h>
00041 #include "radius.h"
00042
00043 #define PI (M_PI)
00044 #define TWOPI (2.*M_PI)
00045 #define RADSINDEG (PI/180.)
00046 #define RAD2ARCSEC (648000./M_PI)
00047 #define SECINDAY (86400.)
00048 #define FOURK (4096)
00049 #define FOURK2 (16777216)
00050
00051 #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s, status=%d\n", msg, status); return(status);}
00052 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00053
00054 #define kNotSpecified "Not Specified"
00055
00056 #define ARRLENGTH(ARR) (sizeof(ARR) / sizeof(ARR[0]))
00057 #define NR_OUT (ARRLENGTH(r_out))
00058
00059
00060
00061 extern void mapfl_wrapper_(double *bp, double *bt, double *br,
00062 double *p, double *t, double *r,
00063 int *np, int *nt, int *nr,
00064 double *p_q, double *t_q, double *r_q,
00065 int *np_q, int *nt_q, double *r_ch,
00066 int *do_chmap, int *vb, int *do_cubic,
00067 double *qmap, double *chmap);
00068
00069
00070
00071
00072
00073 char *module_name = "qmap4pfss";
00074
00075 ModuleArgs_t module_args[] =
00076 {
00077 {ARG_STRING, "in", kNotSpecified, "Input PFSS series."},
00078 {ARG_STRING, "out", kNotSpecified, "Output Q-map series."},
00079 {ARG_FLAG, "v", "", "Verbose mode for Fortran."},
00080 {ARG_INT, "np", "1441", "Output map gridpoints in longitude."},
00081 {ARG_INT, "nt", "721", "Output map gridpoints in longitude."},
00082 {ARG_FLAG, "c", "", "Use cubic interpolation."},
00083 {ARG_END}
00084 };
00085
00086 int DoIt(void)
00087 {
00088
00089 int status = DRMS_SUCCESS;
00090
00091
00092
00093 char *inQuery = (char *) params_get_str(&cmdparams, "in");
00094 char *outQuery = (char *) params_get_str(&cmdparams, "out");
00095 int vb = params_isflagset(&cmdparams, "v");
00096 int do_cubic = params_isflagset(&cmdparams, "c");
00097 int np_q = params_get_int(&cmdparams, "np");
00098 int nt_q = params_get_int(&cmdparams, "nt");
00099
00100
00101
00102 DRMS_RecordSet_t *inRS = drms_open_records(drms_env, inQuery, &status);
00103 int nrecs = inRS->n;
00104 if (status || nrecs < 1) {
00105 DIE("Input data series error");
00106 }
00107
00108
00109
00110 DRMS_RecordSet_t *outRS = drms_create_records(drms_env, nrecs, outQuery, DRMS_PERMANENT, &status);
00111 if (status) {
00112 DIE("Output data series error");
00113 }
00114
00115
00116
00117 for (int irec = 0; irec < nrecs; irec++) {
00118
00119
00120
00121 DRMS_Record_t *inRec = inRS->records[irec];
00122 DRMS_Record_t *outRec = outRS->records[irec];
00123
00124
00125
00126 DRMS_Segment_t *inSeg_bp = drms_segment_lookup(inRec, "Bp");
00127 DRMS_Segment_t *inSeg_bt = drms_segment_lookup(inRec, "Bt");
00128 DRMS_Segment_t *inSeg_br = drms_segment_lookup(inRec, "Br");
00129
00130 DRMS_Array_t *inArray_bp = drms_segment_read(inSeg_bp, DRMS_TYPE_DOUBLE, &status);
00131 if (status) { SHOW("input error"); continue; }
00132 DRMS_Array_t *inArray_bt = drms_segment_read(inSeg_bt, DRMS_TYPE_DOUBLE, &status);
00133 if (status) { SHOW("input error"); continue; }
00134 DRMS_Array_t *inArray_br = drms_segment_read(inSeg_br, DRMS_TYPE_DOUBLE, &status);
00135 if (status) { SHOW("input error"); continue; }
00136
00137 int np = inArray_br->axis[0], nt = inArray_br->axis[1], nr = inArray_br->axis[2];
00138 printf("np=%d, nt=%d, nr=%d\n", np, nt, nr);
00139 int npix = np * nt * nr;
00140
00141 double *bp_jsoc = (double *) inArray_bp->data;
00142 double *bt_jsoc = (double *) inArray_bt->data;
00143 double *br_jsoc = (double *) inArray_br->data;
00144
00145 double *bp = (double *) (malloc(npix * sizeof(double)));
00146 double *bt = (double *) (malloc(npix * sizeof(double)));
00147 double *br = (double *) (malloc(npix * sizeof(double)));
00148
00149
00150 int idx = 0;
00151 int npnt = np * nt;
00152 for (int idx_p = 0; idx_p < np; idx_p++) {
00153 for (int idx_t = 0; idx_t < nt; idx_t++) {
00154 for (int idx_r = 0; idx_r < nr; idx_r++) {
00155 bp[idx] = bp_jsoc[idx_r*npnt+(nt-1-idx_t)*np+idx_p];
00156 bt[idx] = bt_jsoc[idx_r*npnt+(nt-1-idx_t)*np+idx_p];
00157 br[idx++] = br_jsoc[idx_r*npnt+(nt-1-idx_t)*np+idx_p];
00158 }
00159 }
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 double *p = (double *) (malloc(np * sizeof(double)));
00172 double *t = (double *) (malloc(nt * sizeof(double)));
00173 double *r = (double *) (malloc(nr * sizeof(double)));
00174 for (idx = 0; idx < np; idx++) { p[idx] = idx * TWOPI / (np - 1); }
00175 for (idx = 0; idx < nt; idx++) { t[idx] = idx * PI / (nt - 1); }
00176 for (idx = 0; idx < nr; idx++) { r[idx] = r_pfss[idx]; }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 int nr_q = NR_OUT;
00187 int npnt_q = np_q * nt_q;
00188 printf("np_q=%d, nt_q=%d, nr_q=%d, npnt_q=%d\n", np_q, nt_q, nr_q, npnt_q);
00189 double *p_q = (double *) (malloc(npnt_q * sizeof(double)));
00190 double *t_q = (double *) (malloc(npnt_q * sizeof(double)));
00191 double *r_q = (double *) (malloc(npnt_q * sizeof(double)));
00192 for (int idx_t = 0; idx_t < nt_q; idx_t++) {
00193 for (int idx_p = 0; idx_p < np_q; idx_p++) {
00194 idx = idx_t * np_q + idx_p;
00195 p_q[idx] = idx_p * TWOPI / (np_q - 1);
00196 t_q[idx] = idx_t * PI / (nt_q - 1);
00197 }
00198 }
00199
00200
00201
00202 double *qmap_cube = (double *) (malloc(npnt_q * nr_q * sizeof(double)));
00203 double *chmap = (double *) (malloc(npnt_q * sizeof(double)));
00204
00205
00206
00207 for (int ir = 0; ir < nr_q; ir++) {
00208
00209
00210 for (idx = 0; idx < npnt_q; idx++) {
00211 r_q[idx] = r_out[ir];
00212 }
00213
00214
00215
00216
00217 double r_ch = r_out[ir];
00218 double *qmap = qmap_cube + ir * npnt_q;
00219
00220 int do_chmap = (ir == 0) ? 1 : 0;
00221
00222 if (ir == 0) {
00223 for (idx = 0; idx < npnt_q; idx++) {
00224 chmap[idx] = 1;
00225 }
00226 }
00227
00228
00229
00230
00231 mapfl_wrapper_(bp, bt, br, p, t, r, &np, &nt, &nr,
00232 p_q, t_q, r_q, &np_q, &nt_q, &r_ch,
00233 &do_chmap, &vb, &do_cubic,
00234 qmap, chmap);
00235
00236
00237
00238
00239
00240 }
00241
00242
00243
00244 int npix_q = np_q * nt_q * nr_q;
00245
00246
00247
00248
00249
00250
00251 DRMS_Segment_t *inSeg_brq = drms_segment_lookup(inRec, "Brq");
00252 DRMS_Array_t *inArray_brq = drms_segment_read(inSeg_brq, DRMS_TYPE_DOUBLE, &status);
00253 if (status || inSeg_brq->axis[0] != np_q ||
00254 inSeg_brq->axis[1] != nt_q || inSeg_brq->axis[2] != nr_q) {
00255 SHOW("input brq error"); continue;
00256 }
00257 double *brq = (double *) inArray_brq->data;
00258
00259 double slogq, sign_br;
00260 for (int idx = 0; idx < npix_q; idx++) {
00261 slogq = qmap_cube[idx];
00262 sign_br = (brq[idx] < 0.) ? (-1.) : 1.;
00263 if (isnan(slogq) || slogq < 2.) {
00264 qmap_cube[idx] = 0.;
00265 } else {
00266 qmap_cube[idx] = sign_br * log10(slogq / 2. + sqrt(slogq * slogq / 4. - 1.));
00267 }
00268 }
00269
00270 drms_free_array(inArray_brq);
00271
00272
00273
00274 DRMS_Segment_t *outSeg_q = drms_segment_lookup(outRec, "slogQ");
00275 int dims_q[3] = {np_q, nt_q, nr_q};
00276 DRMS_Array_t *outArray_q = drms_array_create(DRMS_TYPE_DOUBLE, 3, dims_q, qmap_cube, &status);
00277 for (idx = 0; idx < 3; idx++) {
00278 outSeg_q->axis[idx] = outArray_q->axis[idx];
00279 }
00280 outArray_q->israw = 0;
00281 outArray_q->bzero = outSeg_q->bzero;
00282 outArray_q->bscale = outSeg_q->bscale;
00283 status = drms_segment_write(outSeg_q, outArray_q, 0);
00284
00285 DRMS_Segment_t *outSeg_ch = drms_segment_lookup(outRec, "chmap");
00286 int dims_ch[2] = {np_q, nt_q};
00287 DRMS_Array_t *outArray_ch = drms_array_create(DRMS_TYPE_DOUBLE, 2, dims_ch, chmap, &status);
00288 for (idx = 0; idx < 2; idx++) {
00289 outSeg_ch->axis[idx] = outArray_ch->axis[idx];
00290 }
00291 outArray_ch->israw = 0;
00292 outArray_ch->bzero = outSeg_ch->bzero;
00293 outArray_ch->bscale = outSeg_ch->bscale;
00294 status = drms_segment_write(outSeg_ch, outArray_ch, 0);
00295
00296
00297
00298 int car_rot = drms_getkey_int(inRec, "CAR_ROT", &status);
00299 double crval1 = drms_getkey_double(inRec, "CRVAL1", &status);
00300 double crpix1 = drms_getkey_double(inRec, "CRPIX1", &status);
00301 double cdelt1 = drms_getkey_double(inRec, "CDELT1", &status);
00302
00303 drms_copykeys(outRec, inRec, 0, kDRMS_KeyClass_Explicit);
00304
00305 drms_setkey_int(outRec, "CAR_ROT", car_rot);
00306
00307 drms_setkey_int(outRec, "CUBIC", do_cubic);
00308 drms_setkey_double(outRec, "R_CHMAP", r_out[0]);
00309
00310 drms_setkey_double(outRec, "CRPIX1", 1.);
00311
00312 drms_setkey_double(outRec, "CRVAL1",
00313 crval1 + (1. - crpix1) * cdelt1);
00314 drms_setkey_double(outRec, "CDELT1", (-1.) * 360. / (np_q - 1.));
00315 drms_setkey_string(outRec, "CUNIT1", "degree");
00316 drms_setkey_string(outRec, "CTYPE1", "CRLN-CAR");
00317
00318 drms_setkey_double(outRec, "CRPIX2", (1. + nt_q) / 2.);
00319 drms_setkey_double(outRec, "CRVAL2", 0.0);
00320 drms_setkey_double(outRec, "CDELT2", 180. / (nt_q - 1.));
00321 drms_setkey_string(outRec, "CUNIT2", "degree");
00322 drms_setkey_string(outRec, "CTYPE2", "CRLT-CAR");
00323
00324 TIME val, trec, tnow, UNIX_epoch = -220924792.000;
00325 tnow = (double)time(NULL);
00326 tnow += UNIX_epoch;
00327 drms_setkey_time(outRec, "DATE", tnow);
00328
00329
00330
00331 DRMS_Link_t *link = NULL;
00332 link = hcon_lookup_lower(&outRec->links, "PFSS");
00333 if (link) drms_link_set("PFSS", outRec, inRec);
00334
00335
00336 drms_free_array(inArray_bp); drms_free_array(inArray_bt); drms_free_array(inArray_br);
00337 drms_free_array(outArray_q); drms_free_array(outArray_ch);
00338 free(bp); free(bt); free(br);
00339 free(p); free(t); free(r);
00340 free(p_q); free(t_q); free(r_q);
00341
00342 }
00343
00344 drms_close_records(inRS, DRMS_FREE_RECORD);
00345 drms_close_records(outRS, DRMS_INSERT_RECORD);
00346
00347
00348
00349 return DRMS_SUCCESS;
00350
00351 }