00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <jsoc_main.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <math.h>
00026 float cdelt1_orig;
00027 float cdelt1;
00028 double rsun_ref;
00029 double dsun_obs;
00030 double rsun_obs;
00031 float imcrpix1;
00032 float imcrpix2;
00033 float crpix1;
00034 float crpix2;
00035
00036 #include "sharp_functions.c"
00037
00038 #define DIE(msg) {fflush(stdout); fprintf(stderr, "%s, status=%d\n", msg, status); return(status);}
00039 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00040 #define IN_FILES 3
00041 #define PI (3.141592653589793)
00042 #define MUNAUGHT (0.0000012566370614)
00043
00044
00045 int computeMu(float *bz, int *dims, float *mu, float *inverseMu);
00046 int computeAbsFlux(float *bz, int *dims, float *absFlux, float *mean_vf_ptr, int *mask, float *inverseMu);
00047 int computeBh(float *bx, float *by, float *bz, float *bh, int *dims, float *mean_hf_ptr, int *mask);
00048 int computeGamma(float *bx, float *by, float *bz, float *bh, int *dims, float *mean_gamma_ptr, int *mask);
00049 int readFits(char *filename, float **image, int *dims);
00050 int writeFits(char *filename, float *image, int *dims);
00051 int computeB_total(float *bx, float *by, float *bz, float *bt, int *dims, int *mask);
00052 int computeBtotalderivative(float *bt, int *dims, float *mean_derivative_btotal_ptr, int *mask);
00053 int computeBhderivative(float *bh, int *dims, float *mean_derivative_bh_ptr, int *mask);
00054 int computeBzderivative(float *bz, int *dims, float *mean_derivative_bz_ptr, int *mask);
00055 int computeJz(float *by, float *bx, int *dims, float *jz, float *mean_jz_ptr, float *us_i_ptr, int *mask);
00056 int computeAlpha(float *bz, int *dims, float *jz, float *mean_alpha_ptr, int *mask);
00057 int computeHelicity(float *bz, int *dims, float *jz, float *mean_ih_ptr, float *total_us_ih_ptr, float *total_abs_ih_ptr, int *mask);
00058 int computeSumAbsPerPolarity(float *bz, float *jz, int *dims, float *totaljzptr, int *mask);
00059 void greenpot(float *bx, float *by, float *bz, int nnx, int nny);
00060 int computeFreeEnergy(float *bx, float *by, float *bpx, float *bpy, int *dims, float *meanpotptr, float *totpotptr, int *mask);
00061 int computeShearAngle(float *bx, float *by, float *bz, float *bpx, float *bpy, float *bpz, int *dims, float *meanshear_angleptr, float *area_w_shear_gt_45ptr, float *meanshear_anglehptr, float *area_w_shear_gt_45hptr, int *mask);
00062
00063 char *module_name = "swharp_vectorB";
00064
00065 ModuleArgs_t module_args[] =
00066 {
00067 {ARG_STRING, "in", NULL, "Input vec mag recordset."},
00068 {ARG_STRING, "mask", NULL, "Input bitmap recordset."},
00069 {ARG_STRING, "out", NULL, "Output series."},
00070 {ARG_FLOAT, "dzvalue", NULL, "Monopole depth."},
00071 {ARG_END}
00072 };
00073
00074 int DoIt(void)
00075 {
00076
00077 int status = DRMS_SUCCESS;
00078
00079 char *inQuery, *outQuery;
00080 char *maskQuery;
00081 DRMS_RecordSet_t *inRecSet, *outRecSet, *maskRecSet;
00082 DRMS_Record_t *inRec, *outRec, *maskRec;
00083 DRMS_Segment_t *inSegBx, *inSegBy, *inSegBz, *outSeg, *maskSeg;
00084 DRMS_Array_t *inArrayBx, *inArrayBy, *inArrayBz, *outArray, *maskArray;
00085 float *inverseMu, *mu, *bx, *by, *bz, *outData, *bh, *bt, *jz, *bpx, *bpy, *bpz;
00086 int *mask;
00087 int dims[2], nxny, nx, ny;
00088 float mean_vf = 0.0;
00089 float absFlux = 0.0;
00090 float mean_hf = 0.0;
00091 float mean_gamma = 0.0;
00092 float mean_derivative_btotal = 0.0;
00093 float mean_derivative_bh = 0.0;
00094 float mean_derivative_bz = 0.0;
00095 float mean_jz = 0.0;
00096 float us_i = 0.0;
00097 float mean_alpha = 0.0;
00098 float mean_ih = 0.0;
00099 float total_us_ih = 0.0;
00100 float total_abs_ih = 0.0;
00101 float totaljz = 0.0;
00102 float totpot =0.0;
00103 float meanpot = 0.0;
00104 float area_w_shear_gt_45 = 0.0;
00105 float meanshear_angle = 0.0;
00106 float area_w_shear_gt_45h = 0.0;
00107 float meanshear_angleh = 0.0;
00108 int nrecs, irec, i;
00109
00110
00111
00112 inQuery = (char *) params_get_str(&cmdparams, "in");
00113 inRecSet = drms_open_records(drms_env, inQuery, &status);
00114 if (status || inRecSet->n == 0) DIE("No input data found");
00115 nrecs = inRecSet->n;
00116
00117
00118
00119 maskQuery = (char *) params_get_str(&cmdparams, "mask");
00120 maskRecSet = drms_open_records(drms_env, maskQuery, &status);
00121 if (status || maskRecSet->n == 0) DIE("No mask data found");
00122 if (maskRecSet->n != nrecs) DIE("Mask and Input series do not have a 1:1 match");
00123
00124
00125
00126 outQuery = (char *) params_get_str(&cmdparams, "out");
00127 outRecSet = drms_create_records(drms_env, nrecs, outQuery, DRMS_PERMANENT, &status);
00128 if (status) DIE("Output recordset not created");
00129
00130
00131
00132 for (irec = 0; irec < nrecs; irec++)
00133 {
00134
00135
00136 inRec = inRecSet->records[irec];
00137 printf("Input Record #%d of #%d\n", irec+1, nrecs); fflush(stdout);
00138
00139 maskRec = maskRecSet->records[irec];
00140 printf("Mask Record #%d of #%d\n", irec+1, nrecs); fflush(stdout);
00141
00142 inSegBx = drms_segment_lookupnum(inRec, 0);
00143 inSegBy = drms_segment_lookupnum(inRec, 1);
00144 inSegBz = drms_segment_lookupnum(inRec, 2);
00145
00146 maskSeg = drms_segment_lookupnum(maskRec, 0);
00147
00148 inArrayBx = drms_segment_read(inSegBx, DRMS_TYPE_FLOAT, &status);
00149 if (status) DIE("No Bx data file found. \n");
00150 inArrayBy = drms_segment_read(inSegBy, DRMS_TYPE_FLOAT, &status);
00151 if (status) DIE("No By data file found. \n");
00152 inArrayBz = drms_segment_read(inSegBz, DRMS_TYPE_FLOAT, &status);
00153 if (status) DIE("No Bz data file found. \n");
00154
00155 maskArray = drms_segment_read(maskSeg, DRMS_TYPE_INT, &status);
00156 if (status) DIE("No mask data file found. \n");
00157
00158 cdelt1_orig = drms_getkey_float(inRec, "CDELT1", &status);
00159 dsun_obs = drms_getkey_float(inRec, "DSUN_OBS", &status);
00160 rsun_ref = drms_getkey_double(inRec, "RSUN_REF", &status);
00161 rsun_obs = drms_getkey_double(inRec, "RSUN_OBS", &status);
00162 imcrpix1 = drms_getkey_float(inRec, "IMCRPIX1", &status);
00163 imcrpix2 = drms_getkey_float(inRec, "IMCRPIX2", &status);
00164 crpix1 = drms_getkey_float(inRec, "CRPIX1", &status);
00165 crpix2 = drms_getkey_float(inRec, "CRPIX2", &status);
00166
00167
00168 cdelt1=( (rsun_ref*cdelt1_orig*PI/180.) / (dsun_obs) )*(180./PI)*(3600.);
00169
00170 printf("cdelt1_orig=%f\n",cdelt1_orig);
00171 printf("cdelt1=%f\n",cdelt1);
00172 printf("rsun_obs/rsun_ref=%f\n",rsun_obs/rsun_ref);
00173 printf("rsun_ref/rsun_obs=%f\n",rsun_ref/rsun_obs);
00174 printf("test1=%f\n",((1/cdelt1_orig)*(rsun_obs/rsun_ref)*(1000000.)));
00175 printf("test2=%f\n",((cdelt1_orig)*(PI/180)*(rsun_ref)*(1/1000000.)));
00176
00177 bx = (float *)inArrayBx->data;
00178 by = (float *)inArrayBy->data;
00179 bz = (float *)inArrayBz->data;
00180 mask = (int *)maskArray->data;
00181
00182 nx = dims[0] = inArrayBz->axis[0];
00183 ny = dims[1] = inArrayBz->axis[1];
00184 nxny = dims[0] * dims[1];
00185 if (maskArray->axis[0] != nx || maskArray->axis[1] != ny) DIE("Mask and Input series are not of the same size");
00186
00187
00188 int flag;
00189 char* value1;
00190
00191 value1 = drms_getkey_string(inRec, "PROJECTION", &status);
00192 flag = strcmp("LambertCylindrical",value1);
00193 if (flag == 0)
00194 {
00195 int i, j;
00196 for (j = 0; j < ny; j++)
00197 {
00198 for (i = 0; i < nx; i++)
00199 {
00200 by[j * nx + i] = - by[j * nx + i];
00201 }
00202 }
00203 }
00204
00205
00206
00207 outRec = outRecSet->records[irec];
00208 drms_setlink_static(outRec, "SRCLINK", inRec->recnum);
00209
00210
00211
00212
00213 inverseMu = (float *)malloc(nx*ny*sizeof(float));
00214 mu = (float *)malloc(nx*ny*sizeof(float));
00215 bh = (float *)malloc(nx*ny*sizeof(float));
00216 bt = (float *)malloc(nx*ny*sizeof(float));
00217 jz = (float *)malloc(nx*ny*sizeof(float));
00218 bpx = (float *)malloc(nx*ny*sizeof(float));
00219 bpy = (float *)malloc(nx*ny*sizeof(float));
00220 bpz = (float *)malloc(nx*ny*sizeof(float));
00221
00222
00223
00224
00225 computeMu(bz, dims, mu, inverseMu);
00226
00227 if (computeAbsFlux(bz, dims, &absFlux, &mean_vf, mask, inverseMu))
00228 {
00229 absFlux = 0.0 / 0.0;
00230 mean_vf = 0.0 / 0.0;
00231 }
00232 drms_setkey_float(outRec, "USFLUX", mean_vf);
00233
00234 for (i=0 ;i<nxny; i++){bpz[i]=bz[i];}
00235 greenpot(bpx, bpy, bpz, nx, ny);
00236
00237 computeBh(bx, by, bz, bh, dims, &mean_hf, mask);
00238
00239 if (computeGamma(bx, by, bz, bh, dims, &mean_gamma, mask)) mean_gamma = 0.0 / 0.0;
00240 drms_setkey_float(outRec, "MEANGAM", mean_gamma);
00241
00242 computeB_total(bx, by, bz, bt, dims, mask);
00243
00244 if (computeBtotalderivative(bt, dims, &mean_derivative_btotal, mask)) mean_derivative_btotal = 0.0 / 0.0;
00245 drms_setkey_float(outRec, "MEANGBT", mean_derivative_btotal);
00246
00247 if (computeBhderivative(bh, dims, &mean_derivative_bh, mask)) mean_derivative_bh = 0.0 / 0.0;
00248 drms_setkey_float(outRec, "MEANGBH", mean_derivative_bh);
00249
00250 if (computeBzderivative(bz, dims, &mean_derivative_bz, mask)) mean_derivative_bz = 0.0 / 0.0;
00251 drms_setkey_float(outRec, "MEANGBZ", mean_derivative_bz);
00252
00253 if(computeJz(bx, by, dims, jz, &mean_jz, &us_i, mask))
00254 {
00255 mean_jz = 0.0 / 0.0;
00256 us_i = 0.0 /0.0;
00257 }
00258 drms_setkey_float(outRec, "MEANJZD", mean_jz);
00259 drms_setkey_float(outRec, "TOTUSJZ", us_i);
00260
00261 if (computeAlpha(bz, dims, jz, &mean_alpha, mask)) mean_alpha = 0.0 / 0.0;
00262 drms_setkey_float(outRec, "MEANALP", mean_alpha);
00263
00264 if (computeHelicity(bz, dims, jz, &mean_ih, &total_us_ih, &total_abs_ih, mask))
00265 {
00266 mean_ih = 0.0/0.0;
00267 total_us_ih = 0.0/0.0;
00268 total_abs_ih= 0.0/0.0;
00269 }
00270 drms_setkey_float(outRec, "MEANJZH", mean_ih);
00271 drms_setkey_float(outRec, "TOTUSJH", total_us_ih);
00272 drms_setkey_float(outRec, "ABSNJZH", total_abs_ih);
00273
00274 if (computeSumAbsPerPolarity(bz, jz, dims, &totaljz, mask)) totaljz = 0.0 / 0.0;
00275 drms_setkey_float(outRec, "SAVNCPP", totaljz);
00276
00277 if (computeFreeEnergy(bx, by, bpx, bpy, dims, &meanpot, &totpot, mask))
00278 {
00279 meanpot = 0.0 / 0.0;
00280 totpot = 0.0 / 0.0;
00281 }
00282 drms_setkey_float(outRec, "MEANPOT", meanpot);
00283 drms_setkey_float(outRec, "TOTPOT", totpot);
00284
00285 if (computeShearAngle(bx, by, bz, bpx, bpy, bpz, dims, &meanshear_angle, &area_w_shear_gt_45, &meanshear_angleh, &area_w_shear_gt_45h, mask))
00286 {
00287 meanshear_angle = 0.0 / 0.0;
00288 area_w_shear_gt_45 = 0.0/0.0;
00289 meanshear_angleh = 0.0 / 0.0;
00290 area_w_shear_gt_45h = 0.0/0.0;
00291 }
00292 printf("meanshear_angle=%f, area_w_shear_gt_45=%f, meanshear_angleh=%f, area_w_shear_gt_45h=%f\n",meanshear_angle,area_w_shear_gt_45,meanshear_angleh,area_w_shear_gt_45h);
00293 drms_setkey_float(outRec, "MEANSHR", meanshear_angle);
00294 drms_setkey_float(outRec, "SHRGT45", area_w_shear_gt_45);
00295
00296
00297
00298
00299
00300
00301
00302 drms_copykey(outRec, inRec, "T_REC");
00303 drms_copykey(outRec, inRec, "HARPNUM");
00304
00305
00306 drms_free_array(inArrayBz);
00307 drms_free_array(maskArray);
00308
00309 }
00310
00311 drms_close_records(inRecSet, DRMS_FREE_RECORD);
00312 drms_close_records(outRecSet, DRMS_INSERT_RECORD);
00313
00314 return 0;
00315
00316 }