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 #include "sharp_functions.c"
00027
00028 #define DIE(msg) {fflush(stdout); fprintf(stderr, "%s, status=%d\n", msg, status); return(status);}
00029 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00030 #define IN_FILES 3
00031 #define PI (3.141592653589793)
00032 #define CMPERPIX (0.504277*696000000.0*100.)/(943.)
00033
00034
00035
00036
00037
00038
00039
00040
00041 int computeAbsFlux(float *bz, int *dims, float *absFlux, float *mean_vf_ptr, int *mask);
00042 int computeBh(float *bpx, float *bpy, float *bz, float *bh, int *dims, float *mean_hf_ptr, int *mask);
00043 int computeGamma(float *bpx, float *bpy, float *bz, float *bh, int *dims, float *mean_gamma_ptr, int *mask);
00044 int readFits(char *filename, float **image, int *dims);
00045 int writeFits(char *filename, float *image, int *dims);
00046 int computeB_total(float *bpx, float *bpy, float *bz, float *bt, int *dims, int *mask);
00047 int computeBtotalderivative(float *bt, int *dims, float *mean_derivative_btotal_ptr, int *mask);
00048 int computeBhderivative(float *bh, int *dims, float *mean_derivative_bh_ptr, int *mask);
00049 int computeBzderivative(float *bz, int *dims, float *mean_derivative_bz_ptr, int *mask);
00050 void greenpot(float *bx, float *by, float *bz, int nnx, int nny);
00051
00052 char *module_name = "swharp_losB";
00053
00054 ModuleArgs_t module_args[] =
00055 {
00056 {ARG_STRING, "in", NULL, "Input vec mag recordset."},
00057 {ARG_STRING, "mask", NULL, "Input bitmap recordset."},
00058 {ARG_STRING, "out", NULL, "Output series."},
00059 {ARG_FLOAT, "dzvalue", NULL, "Monopole depth."},
00060 {ARG_END}
00061 };
00062
00063
00064 int DoIt(void)
00065 {
00066
00067 int status = DRMS_SUCCESS;
00068
00069 char *inQuery, *outQuery;
00070 char *maskQuery;
00071 DRMS_RecordSet_t *inRecSet, *outRecSet, *maskRecSet;
00072 DRMS_Record_t *inRec, *outRec, *maskRec;
00073 DRMS_Segment_t *inSegBx, *inSegBy, *inSegBz, *outSeg, *maskSeg;
00074 DRMS_Array_t *inArrayBx, *inArrayBy, *inArrayBz, *outArray, *maskArray;
00075 float *bx, *by, *bz, *outData, *bh, *bt, *jz, *bpx, *bpy, *bpz;
00076 int *mask;
00077 int dims[2], nxny, nx, ny;
00078 float mean_vf = 0.0;
00079 float absFlux = 0.0;
00080 float mean_hf = 0.0;
00081 float mean_gamma = 0.0;
00082 float mean_derivative_btotal = 0.0;
00083 float mean_derivative_bh = 0.0;
00084 float mean_derivative_bz = 0.0;
00085 float mean_jz = 0.0;
00086 float us_i = 0.0;
00087 float mean_alpha = 0.0;
00088 float mean_ih = 0.0;
00089 float total_us_ih = 0.0;
00090 float total_abs_ih = 0.0;
00091 float totaljz = 0.0;
00092 float totpot =0.0;
00093 float meanpot = 0.0;
00094 float area_w_shear_gt_45 = 0.0;
00095 float meanshear_angle = 0.0;
00096 float area_w_shear_gt_45h = 0.0;
00097 float meanshear_angleh = 0.0;
00098 int nrecs, irec, i;
00099
00100
00101
00102 inQuery = (char *) params_get_str(&cmdparams, "in");
00103 inRecSet = drms_open_records(drms_env, inQuery, &status);
00104 if (status || inRecSet->n == 0) DIE("No input data found");
00105 nrecs = inRecSet->n;
00106
00107
00108
00109 maskQuery = (char *) params_get_str(&cmdparams, "mask");
00110 maskRecSet = drms_open_records(drms_env, maskQuery, &status);
00111 if (status || maskRecSet->n == 0) DIE("No mask data found");
00112 if (maskRecSet->n != nrecs) DIE("Mask and Input series do not have a 1:1 match");
00113
00114
00115
00116 outQuery = (char *) params_get_str(&cmdparams, "out");
00117 outRecSet = drms_create_records(drms_env, nrecs, outQuery, DRMS_PERMANENT, &status);
00118 if (status) DIE("Output recordset not created");
00119
00120
00121
00122 for (irec = 0; irec < nrecs; irec++)
00123 {
00124
00125
00126
00127 inRec = inRecSet->records[irec];
00128 printf("Input Record #%d of #%d\n", irec+1, nrecs); fflush(stdout);
00129
00130 maskRec = maskRecSet->records[irec];
00131 printf("Mask Record #%d of #%d\n", irec+1, nrecs); fflush(stdout);
00132
00133 inSegBz = drms_segment_lookupnum(inRec, 0);
00134
00135 maskSeg = drms_segment_lookupnum(maskRec, 0);
00136
00137 inArrayBz = drms_segment_read(inSegBz, DRMS_TYPE_FLOAT, &status);
00138 if (status) DIE("No Bz data file found. \n");
00139
00140 maskArray = drms_segment_read(maskSeg, DRMS_TYPE_INT, &status);
00141 if (status) DIE("No mask data file found. \n");
00142
00143 bz = (float *)inArrayBz->data;
00144 mask = (int *)maskArray->data;
00145
00146 nx = dims[0] = inArrayBz->axis[0];
00147 ny = dims[1] = inArrayBz->axis[1];
00148 nxny = dims[0] * dims[1];
00149 if (maskArray->axis[0] != nx || maskArray->axis[1] != ny) DIE("Mask and Input series are not of the same size");
00150
00151
00152 int flag;
00153 char* value1;
00154 value1 = drms_getkey_string(inRec, "PROJECTION", &status);
00155 flag = strcmp("LambertCylindrical",value1);
00156 if (flag == 0)
00157 {
00158 int i, j;
00159 for (j = 0; j < ny; j++)
00160 {
00161 for (i = 0; i < nx; i++)
00162 {
00163 by[j * nx + i] = - by[j * nx + i];
00164 }
00165 }
00166 }
00167
00168
00169
00170 outRec = outRecSet->records[irec];
00171 drms_setlink_static(outRec, "SRCLINK", inRec->recnum);
00172
00173
00174
00175
00176 bh = (float *)malloc(nx*ny*sizeof(float));
00177 bt = (float *)malloc(nx*ny*sizeof(float));
00178 jz = (float *)malloc(nx*ny*sizeof(float));
00179 bpx = (float *)malloc(nx*ny*sizeof(float));
00180 bpy = (float *)malloc(nx*ny*sizeof(float));
00181 bpz = (float *)malloc(nx*ny*sizeof(float));
00182
00183
00184
00185
00186 if (computeAbsFlux(bz, dims, &absFlux, &mean_vf, mask))
00187 {
00188 absFlux = 0.0 / 0.0;
00189 mean_vf = 0.0 / 0.0;
00190 }
00191 drms_setkey_float(outRec, "USFLUX", mean_vf);
00192
00193 for (i=0 ;i<nxny; i++){bpz[i]=bz[i];}
00194 greenpot(bpx, bpy, bpz, nx, ny);
00195
00196 computeBh(bpx, bpy, bz, bh, dims, &mean_hf, mask);
00197
00198 if (computeGamma(bpx, bpy, bz, bh, dims, &mean_gamma, mask)) mean_gamma = 0.0 / 0.0;
00199 drms_setkey_float(outRec, "MEANGAM", mean_gamma);
00200
00201 computeB_total(bpx, bpy, bz, bt, dims, mask);
00202
00203 if (computeBtotalderivative(bt, dims, &mean_derivative_btotal, mask)) mean_derivative_btotal = 0.0 / 0.0;
00204 drms_setkey_float(outRec, "MEANGBT", mean_derivative_btotal);
00205
00206 if (computeBhderivative(bh, dims, &mean_derivative_bh, mask)) mean_derivative_bh = 0.0 / 0.0;
00207 drms_setkey_float(outRec, "MEANGBH", mean_derivative_bh);
00208
00209 if (computeBzderivative(bz, dims, &mean_derivative_bz, mask)) mean_derivative_bz = 0.0 / 0.0;
00210 drms_setkey_float(outRec, "MEANGBZ", mean_derivative_bz);
00211
00212
00213
00214
00215 drms_copykey(outRec, inRec, "T_REC");
00216 drms_copykey(outRec, inRec, "HARPNUM");
00217
00218
00219 drms_free_array(inArrayBz);
00220 drms_free_array(maskArray);
00221
00222 }
00223
00224 drms_close_records(inRecSet, DRMS_FREE_RECORD);
00225 drms_close_records(outRecSet, DRMS_INSERT_RECORD);
00226
00227 return 0;
00228
00229 }