00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <time.h>
00026 #include <sys/time.h>
00027 #include <math.h>
00028 #include <string.h>
00029 #include "jsoc_main.h"
00030 #include "astro.h"
00031 #include "copy_me_keys.c"
00032
00033 #define ARRLENGTH(ARR) (sizeof(ARR) / sizeof(ARR[0]))
00034
00035 #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s, status=%d\n", msg, status); return(status);}
00036 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00037
00038 #define kNotSpecified "Not Specified"
00039
00040 #define PIXSIZE ((double)3.64e7)
00041
00042
00043 char *vectorSegs[] = {"Bp", "Bt", "Br"};
00044
00045
00046 float vectorMulti[] = {1.,-1.,1.};
00047
00048
00049 char *lorentzSegs[] = {"Fx", "Fy", "Fz"};
00050
00051
00052 char *maskSegs[] = {"bitmap", "conf_disambig"};
00053 int maskThresh[] = {32, 61};
00054
00055
00056 void setKeys(DRMS_Record_t *outRec, DRMS_Record_t *inRec);
00057
00058
00059
00060 char *module_name = "lorentz";
00061
00062 ModuleArgs_t module_args[] =
00063 {
00064 {ARG_STRING, "in", kNotSpecified, "Input sharp series."},
00065 {ARG_STRING, "out", kNotSpecified, "Output Lorentz force series."},
00066 {ARG_INT, "mask", "1", "Mask for weak field: 0 for bitmap, 1 for conf_disambig"},
00067 {ARG_END}
00068 };
00069
00070 int DoIt(void)
00071 {
00072
00073 int status = DRMS_SUCCESS;
00074 int nrecs, irec;
00075
00076 char *inQuery, *outQuery;
00077 DRMS_RecordSet_t *inRS = NULL, *outRS = NULL;
00078
00079
00080
00081 inQuery = (char *) params_get_str(&cmdparams, "in");
00082 outQuery = (char *) params_get_str(&cmdparams, "out");
00083 int mask_id = params_get_int(&cmdparams, "mask");
00084 mask_id = mask_id ? 1 : 0;
00085
00086 char *maskSegName = maskSegs[mask_id];
00087 int thresh = maskThresh[mask_id];
00088 printf("mask=%d, name=%s, thresh=%d\n", mask_id, maskSegName, thresh);
00089
00090
00091
00092 inRS = drms_open_records(drms_env, inQuery, &status);
00093 if (status || inRS->n == 0) DIE("No input data found");
00094 nrecs = inRS->n;
00095
00096
00097
00098 outRS = drms_create_records(drms_env, nrecs, outQuery, DRMS_PERMANENT, &status);
00099 if (status) {
00100 DIE("Output error");
00101 }
00102
00103
00104
00105 for (irec = 0; irec < nrecs; irec++) {
00106
00107
00108
00109 DRMS_Record_t *inRec = inRS->records[irec];
00110
00111 TIME t_rec = drms_getkey_time(inRec, "T_REC", &status);
00112 int harpnum = drms_getkey_time(inRec, "HARPNUM", &status);
00113
00114 char *inRecInfo = (char *) malloc(100 * sizeof(char));
00115 char *t_rec_str = (char *) malloc(30 * sizeof(char));
00116 sprint_time(t_rec_str, t_rec, "TAI", 0);
00117 sprintf(inRecInfo, "%s[%d][%s]\n", inRec->seriesinfo->seriesname, harpnum, t_rec_str);
00118
00119 printf("Record #%d of %d, %s\n", irec+1, nrecs, inRecInfo); fflush(stdout);
00120
00121
00122
00123 DRMS_Record_t *outRec = outRS->records[irec];
00124
00125
00126
00127 DRMS_Segment_t *maskSeg = drms_segment_lookup(inRec, maskSegName);
00128 DRMS_Array_t *maskArray = drms_segment_read(maskSeg, DRMS_TYPE_CHAR, &status);
00129 if (status) {
00130 SHOW("Error while reading mask, skip record\n");
00131 drms_free_array(maskArray);
00132 }
00133
00134 DRMS_Segment_t *bxSeg = drms_segment_lookup(inRec, vectorSegs[0]);
00135 DRMS_Array_t *bxArray = drms_segment_read(bxSeg, DRMS_TYPE_FLOAT, &status);
00136 if (status) {
00137 SHOW("Error while reading Bx, skip record\n");
00138 drms_free_array(maskArray);
00139 drms_free_array(bxArray);
00140 }
00141
00142 DRMS_Segment_t *bySeg = drms_segment_lookup(inRec, vectorSegs[1]);
00143 DRMS_Array_t *byArray = drms_segment_read(bySeg, DRMS_TYPE_FLOAT, &status);
00144 if (status) {
00145 SHOW("Error while reading By, skip record\n");
00146 drms_free_array(maskArray);
00147 drms_free_array(bxArray); drms_free_array(byArray);
00148 }
00149
00150 DRMS_Segment_t *bzSeg = drms_segment_lookup(inRec, vectorSegs[2]);
00151 DRMS_Array_t *bzArray = drms_segment_read(bzSeg, DRMS_TYPE_FLOAT, &status);
00152 if (status) {
00153 SHOW("Error while reading Bz, skip record\n");
00154 drms_free_array(maskArray);
00155 drms_free_array(bxArray); drms_free_array(byArray); drms_free_array(bzArray);
00156 }
00157
00158 int nx = bzArray->axis[0], ny = bzArray->axis[1];
00159 int nxny = nx * ny;
00160
00161 char *mask = (char *) maskArray->data;
00162 float *bx = (float *) bxArray->data;
00163 float *by = (float *) byArray->data;
00164 float *bz = (float *) bzArray->data;
00165
00166
00167
00168 int outDims[2] = {nx, ny};
00169
00170 DRMS_Array_t *fxArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, NULL, &status);
00171 if (status) {
00172 SHOW("Error while creating Fx, skip record\n");
00173 drms_free_array(fxArray);
00174 drms_free_array(maskArray);
00175 drms_free_array(bxArray); drms_free_array(byArray); drms_free_array(bzArray);
00176 }
00177
00178 DRMS_Array_t *fyArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, NULL, &status);
00179 if (status) {
00180 SHOW("Error while creating Fy, skip record\n");
00181 drms_free_array(fxArray); drms_free_array(fyArray);
00182 drms_free_array(maskArray);
00183 drms_free_array(bxArray); drms_free_array(byArray); drms_free_array(bzArray);
00184 }
00185
00186 DRMS_Array_t *fzArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, NULL, &status);
00187 if (status) {
00188 SHOW("Error while creating Fz, skip record\n");
00189 drms_free_array(fxArray); drms_free_array(fyArray); drms_free_array(fzArray);
00190 drms_free_array(maskArray);
00191 drms_free_array(bxArray); drms_free_array(byArray); drms_free_array(bzArray);
00192 }
00193
00194 float *fx = (float *) fxArray->data;
00195 float *fy = (float *) fyArray->data;
00196 float *fz = (float *) fzArray->data;
00197
00198
00199
00200 double totfx = 0, totfy = 0, totfz = 0;
00201 double totfx1 = 0, totfy1 = 0, totfz1 = 0;
00202 double bsq = 0, totbsq = 0, totbsq1 = 0;
00203 double epsx = 0, epsy = 0, epsz = 0;
00204 double epsx1 = 0, epsy1 = 0, epsz1 = 0;
00205
00206 double area = pow(PIXSIZE, 2.0);
00207 double k_h = -1.0 * area / (4. * M_PI) / 1.0e20;
00208 double k_z = area / (8. * M_PI) / 1.0e20;
00209
00210 for (int i = 0; i < nxny; i++) {
00211
00212 fx[i] = (bx[i] * vectorMulti[0]) * (bz[i] * vectorMulti[2]) * k_h;
00213 fy[i] = (by[i] * vectorMulti[1]) * (bz[i] * vectorMulti[2]) * k_h;
00214 fz[i] = (bx[i] * bx[i] + by[i] * by[i] - bz[i] * bz[i]) * k_z;
00215 bsq = bx[i] * bx[i] + by[i] * by[i] + bz[i] * bz[i];
00216
00217 totfx += fx[i]; totfy += fy[i]; totfz += fz[i];
00218 totbsq += bsq;
00219
00220 if (mask[i] > thresh) {
00221 totfx1 += fx[i]; totfy1 += fy[i]; totfz1 += fz[i];
00222 totbsq1 += bsq;
00223 }
00224
00225 }
00226
00227 epsx = (totfx / k_h) / totbsq;
00228 epsy = (totfy / k_h) / totbsq;
00229 epsz = (totfz / k_z) / totbsq;
00230 epsx1 = (totfx1 / k_h) / totbsq1;
00231 epsy1 = (totfy1 / k_h) / totbsq1;
00232 epsz1 = (totfz1 / k_z) / totbsq1;
00233
00234
00235
00236 DRMS_Segment_t *fxSeg = drms_segment_lookup(outRec, lorentzSegs[0]);
00237 DRMS_Segment_t *fySeg = drms_segment_lookup(outRec, lorentzSegs[1]);
00238 DRMS_Segment_t *fzSeg = drms_segment_lookup(outRec, lorentzSegs[2]);
00239
00240 fxSeg->axis[0] = outDims[0]; fxSeg->axis[1] = outDims[1];
00241 fySeg->axis[0] = outDims[0]; fySeg->axis[1] = outDims[1];
00242 fzSeg->axis[0] = outDims[0]; fzSeg->axis[1] = outDims[1];
00243
00244 fxArray->israw = 0;
00245 fyArray->israw = 0;
00246 fzArray->israw = 0;
00247
00248 fxArray->bzero = fxSeg->bzero; fxArray->bscale = fxSeg->bscale;
00249 fyArray->bzero = fySeg->bzero; fyArray->bscale = fySeg->bscale;
00250 fzArray->bzero = fzSeg->bzero; fzArray->bscale = fzSeg->bscale;
00251
00252 status = drms_segment_write(fxSeg, fxArray, 0);
00253 if (status) {
00254 SHOW("Error while writing Fx, skip record\n");
00255 drms_free_array(fxArray); drms_free_array(fyArray); drms_free_array(fzArray);
00256 drms_free_array(maskArray);
00257 drms_free_array(bxArray); drms_free_array(byArray); drms_free_array(bzArray);
00258 }
00259
00260 status = drms_segment_write(fySeg, fyArray, 0);
00261 if (status) {
00262 SHOW("Error while writing Fy, skip record\n");
00263 drms_free_array(fxArray); drms_free_array(fyArray); drms_free_array(fzArray);
00264 drms_free_array(maskArray);
00265 drms_free_array(bxArray); drms_free_array(byArray); drms_free_array(bzArray);
00266 }
00267
00268 status = drms_segment_write(fzSeg, fzArray, 0);
00269 if (status) {
00270 SHOW("Error while writing Fz, skip record\n");
00271 drms_free_array(fxArray); drms_free_array(fyArray); drms_free_array(fzArray);
00272 drms_free_array(maskArray);
00273 drms_free_array(bxArray); drms_free_array(byArray); drms_free_array(bzArray);
00274 }
00275
00276
00277
00278 DRMS_Link_t *sharpLink = hcon_lookup_lower(&outRec->links, "SHARP");
00279 if (sharpLink) drms_link_set("SHARP", outRec, inRec);
00280
00281
00282
00283 setKeys(outRec, inRec);
00284
00285 drms_copykey(outRec, inRec, "T_REC");
00286 drms_copykey(outRec, inRec, "HARPNUM");
00287
00288 drms_setkey_double(outRec, "TOTFX", totfx);
00289 drms_setkey_double(outRec, "TOTFY", totfy);
00290 drms_setkey_double(outRec, "TOTFZ", totfz);
00291 drms_setkey_double(outRec, "TOTBSQ", totbsq);
00292 drms_setkey_double(outRec, "TOTFX1", totfx1);
00293 drms_setkey_double(outRec, "TOTFY1", totfy1);
00294 drms_setkey_double(outRec, "TOTFZ1", totfz1);
00295 drms_setkey_double(outRec, "TOTBSQ1", totbsq1);
00296 drms_setkey_double(outRec, "EPSX", epsx);
00297 drms_setkey_double(outRec, "EPSY", epsy);
00298 drms_setkey_double(outRec, "EPSZ", epsz);
00299 drms_setkey_double(outRec, "EPSX1", epsx1);
00300 drms_setkey_double(outRec, "EPSY1", epsy1);
00301 drms_setkey_double(outRec, "EPSZ1", epsz1);
00302 drms_setkey_double(outRec, "AREA", area);
00303 drms_setkey_string(outRec, "MASKNAME", maskSegName);
00304 drms_setkey_int(outRec, "MASKTHRS", thresh);
00305
00306
00307
00308 drms_free_array(fxArray); drms_free_array(fyArray); drms_free_array(fzArray);
00309 drms_free_array(maskArray);
00310 drms_free_array(bxArray); drms_free_array(byArray); drms_free_array(bzArray);
00311 free(inRecInfo); free(t_rec_str);
00312
00313 }
00314
00315 drms_close_records(outRS, DRMS_INSERT_RECORD);
00316 drms_close_records(inRS, DRMS_FREE_RECORD);
00317
00318 return DRMS_SUCCESS;
00319
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329 void setKeys(DRMS_Record_t *outRec, DRMS_Record_t *inRec)
00330 {
00331
00332 copy_me_keys(inRec, outRec);
00333 copy_patch_keys(inRec, outRec);
00334 copy_geo_keys(inRec, outRec);
00335 copy_ambig_keys(inRec, outRec);
00336
00337 drms_setkey_string(outRec, "BUNIT_000", "1e20 dyne");
00338 drms_setkey_string(outRec, "BUNIT_001", "1e20 dyne");
00339 drms_setkey_string(outRec, "BUNIT_002", "1e20 dyne");
00340
00341 char timebuf[1024];
00342 float UNIX_epoch = -220924792.000;
00343 double val;
00344 int status = DRMS_SUCCESS;
00345
00346 sprint_time(timebuf, (double)time(NULL) + UNIX_epoch, "ISO", 0);
00347 drms_setkey_string(outRec, "DATE", timebuf);
00348
00349 char *cvsinfo = strdup("$Id: lorentz.c,v 1.3 2014/08/13 19:13:56 arta Exp $");
00350 drms_setkey_string(outRec, "LOR_VERS", cvsinfo);
00351
00352 };