00001
00002
00003
00004
00005
00006 #include <math.h>
00007 #include <sys/time.h>
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <time.h>
00011 #include "jsoc_main.h"
00012 #include "fitimage_float.c"
00013
00014 #include <mkl_blas.h>
00015 #include <mkl_service.h>
00016 #include <mkl_lapack.h>
00017 #include <mkl_vml_functions.h>
00018 #include <omp.h>
00019 #include "fresize.c"
00020
00021 #define PI (M_PI)
00022 #define RADSINDEG (PI/180)
00023
00024 #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s, status=%d\n", msg, status); return(status);}
00025
00026
00027 #define kRecSetIn "in"
00028 #define kSeriesOut "out"
00029 #define kSegIn "segin"
00030 #define kSegOut "segout"
00031 #define kNOTSPECIFIED "not specified"
00032
00033 #define RADSINDEG (PI/180)
00034 #define RAD2ARCSEC (648000. / M_PI)
00035
00036
00037
00038 void frebin(float *image_in, float *image_out, int nx_in, int ny_in, int nx_out, int ny_out, int nbin)
00039 {
00040 struct fresize_struct fresizes;
00041
00042 int nlead_in = nx_in, nlead_out = nx_out;
00043
00044
00045 init_fresize_gaussian2(&fresizes, nbin, (3 * nbin)/2, (3 * nbin)/2, nbin);
00046
00047
00048
00049 fresize(&fresizes, image_in, image_out, nx_in, ny_in, nlead_in, nx_out, ny_out, nlead_out, 1, 1, DRMS_MISSING_FLOAT);
00050
00051
00052
00053
00054
00055 free_fresize(&fresizes);
00056
00057 }
00058
00059 char *module_name = "noisemaskcoef4twindow";
00060 float median(int n, float *x);
00061
00062 ModuleArgs_t module_args[] =
00063 {
00064 {ARG_STRING, kRecSetIn, "", "Input data records."},
00065 {ARG_STRING, kSeriesOut, "", "Output data series."},
00066 {ARG_STRING, kSegIn, kNOTSPECIFIED, ""},
00067 {ARG_STRING, kSegOut, kNOTSPECIFIED, ""},
00068 {ARG_INT, "ORDER", "15", "", ""},
00069 {ARG_INT, "MASKID", "1", "", ""},
00070 {ARG_FLOAT, "VR_START", "-3600.0", "", ""},
00071 {ARG_FLOAT, "VR_STOP", "3600.0", "", ""},
00072 {ARG_END}
00073 };
00074
00075 int DoIt(void)
00076 {
00077
00078 int status = DRMS_SUCCESS, cstat, newchunk, ds, nds, nnds;
00079 float vr, vrstart, vrstop, vrstep = 50.0, vr_center;
00080 char *inRecQuery, *outRecQuery;
00081 char *phasemapid_str, phaseid[64];
00082 int phasemapid;
00083 char tstr[64];
00084 int y,m,d,maskid;
00085 int imrec[36000];
00086 int ngood, ndMax = 50;
00087 float vr_left, vr_right, dSun, rSun_ref, cdelt, asd, rSun, xCenter, yCenter, bCutoff = 300.0;
00088 int xDim = 4096, yDim = 4096;
00089 long long imgData;
00090 double xDist = 0.0, yDist = 0.0, xDist2 = 0.0, yDist2 = 0.0, xDist2PlusyDist2 = 0.0, dToDiskCenter = 0.0;
00091 int mm, nn, order, yOff = 0, iData = 0;
00092 DRMS_RecordSet_t *inRD, *outRD;
00093 DRMS_Record_t *inRec;
00094 DRMS_Record_t *outRec;
00095 DRMS_Segment_t *inSeg = NULL, *outSeg;
00096 DRMS_Array_t *inArray, *outArray;
00097 int outDims[2] = {xDim, yDim};
00098 TIME t_rec;
00099
00100 phasemapid_str = (char *)malloc(100 * sizeof(char));
00101 inRecQuery = (char *)params_get_str(&cmdparams, kRecSetIn);
00102 outRecQuery = (char *)params_get_str(&cmdparams, kSeriesOut);
00103 maskid = cmdparams_get_int(&cmdparams, "MASKID", &status);
00104 vrstart = cmdparams_get_float(&cmdparams, "VR_START", &status);
00105 vrstop = cmdparams_get_float(&cmdparams, "VR_STOP", &status);
00106 order = cmdparams_get_int(&cmdparams, "ORDER", &status);
00107
00108 nds = (int)((vrstop - vrstart)/vrstep) + 1;
00109
00110
00111
00112 for (ds = 0; ds < nds; ds++)
00113 {
00114 float *bPixel, *datacube, *bField, *bMask;
00115 double *coef;
00116 int idx = 0;
00117 float crpix1, crpix2, crval1, crval2, cdelt1, cdelt2, crota2;
00118
00119 vr_center = vrstart + ds * vrstep;
00120 vr_left = vr_center - vrstep;
00121 vr_right = vr_center + vrstep;
00122
00123 datacube = (float *) malloc(xDim * yDim * ndMax * sizeof(float));
00124 bMask = (float *) malloc(xDim * yDim * sizeof(float));
00125
00126 nnds = drms_count_records(drms_env, inRecQuery, &status);
00127 inRD = drms_open_recordset(drms_env, inRecQuery, &status);
00128 printf("n=%d\n", nnds);
00129 while ((inRec = drms_recordset_fetchnext(drms_env, inRD, &status, &cstat, &newchunk)) != NULL)
00130 {
00131 vr = drms_getkey_double(inRec, "OBS_VR", &status);
00132 if (vr > vr_left && vr < vr_right)
00133 {
00134 ++idx;
00135 if (idx == 1)
00136 {
00137 dSun = drms_getkey_double(inRec, "DSUN_OBS", &status);
00138 rSun_ref = drms_getkey_double(inRec, "RSUN_REF", &status);
00139 if (status) rSun_ref = 6.96e8;
00140 cdelt = drms_getkey_float(inRec, "CDELT1", &status);
00141 asd = asin(rSun_ref/dSun);
00142 rSun = asin(rSun_ref / dSun) * RAD2ARCSEC / cdelt;
00143 xCenter = drms_getkey_float(inRec, "CRPIX1", &status) - 1.0;
00144 yCenter = drms_getkey_float(inRec, "CRPIX2", &status) - 1.0;
00145 phasemapid = drms_getkey_int(inRec, "INVPHMAP", &status);
00146
00147 sprintf(phasemapid_str, "%d", phasemapid);
00148 printf("phase ID = %s\n", phasemapid_str);
00149
00150 t_rec = drms_getkey_time(inRec, "T_REC", &status);
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 crpix1 = drms_getkey_float(inRec, "CRPIX1", &status);
00162
00163 crpix2 = drms_getkey_float(inRec, "CRPIX2", &status);
00164
00165 crval1 = drms_getkey_float(inRec, "CRVAL1", &status);
00166
00167 crval2 = drms_getkey_float(inRec, "CRVAL2", &status);
00168
00169 cdelt1 = drms_getkey_float(inRec, "CDELT1", &status);
00170
00171 cdelt2 = drms_getkey_float(inRec, "CDELT2", &status);
00172
00173 crota2 = drms_getkey_float(inRec, "CROTA2", &status);
00174
00175
00176
00177
00178 }
00179
00180 t_rec = drms_getkey_time(inRec, "T_REC", &status);
00181 vr = drms_getkey_double(inRec, "OBS_VR", &status);
00182 sprint_at(tstr, t_rec);
00183 printf("idx=%d, Vr=%f, T_REC = %s\n", idx, vr, tstr);
00184 inSeg = drms_segment_lookup(inRec, "field");
00185 inArray = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00186 if (status)
00187 {
00188 printf("Bad data, skip! \n");
00189 --idx; continue;
00190 }
00191 bField = (float *)inArray->data;
00192 imgData = (idx - 1) * xDim * yDim;
00193 int jy = 0;
00194 for (jy = 0; jy < yDim; jy++)
00195 {
00196 int ix = 0;
00197 yOff = jy * xDim;
00198 for (ix = 0; ix < xDim; ix++)
00199 {
00200 iData = yOff + ix;
00201 datacube[imgData + iData] = bField[iData];
00202 }
00203 }
00204 drms_free_array(inArray);
00205 }
00206 if (idx > ndMax) break;
00207 }
00208 ngood = idx;
00209 printf("ngood=%d\n", ngood);
00210 if (ngood < 10) {free(datacube); continue;}
00211 else
00212 {
00213 bPixel = (float *) malloc(ngood * sizeof(float));
00214
00215 int jy = 0;
00216 for (jy = 0; jy < yDim; jy++)
00217 {
00218 int ix = 0;
00219 yDist = (double)jy - yCenter;
00220 yDist2 = yDist * yDist;
00221 yOff = jy * xDim;
00222
00223 for (ix = 0; ix < xDim; ix++)
00224 {
00225 iData = yOff + ix;
00226 xDist = (double)ix - xCenter;
00227 xDist2 = xDist * xDist;
00228 xDist2PlusyDist2 = xDist2 + yDist2;
00229 dToDiskCenter = sqrt(xDist2PlusyDist2);
00230 if (dToDiskCenter >= rSun) bMask[iData] = DRMS_MISSING_FLOAT;
00231 else
00232 {
00233 int countPixel = 0, iimg = 0;
00234 imgData = 0;
00235 for (iimg = 0; iimg < ngood; iimg++)
00236 {
00237 imgData = iimg * xDim * yDim + iData;
00238 if (datacube[imgData] < bCutoff)
00239 {
00240 bPixel[countPixel] = datacube[imgData];
00241 ++countPixel;
00242 }
00243 }
00244 if (countPixel == 0) bMask[iData] = DRMS_MISSING_FLOAT;
00245 else if (countPixel == 1) bMask[iData] = bPixel[0];
00246 else
00247 {
00248 bMask[iData] = median(countPixel, bPixel);
00249 }
00250 }
00251 }
00252 }
00253
00254
00255 int nbin = 8, xout, yout, xleft, yleft, jData;
00256 float *outData, *outResize, xcResize, ycResize, rResize;
00257 double *mask;
00258
00259 xout = xDim/nbin; yout = yDim/nbin;
00260 outData = (float *) malloc(xout * yout * sizeof(float));
00261 frebin(bMask, outData, xDim, yDim, xout, yout, nbin);
00262
00263 xcResize = xCenter/nbin;
00264 ycResize = yCenter/nbin;
00265 rResize = rSun/nbin;
00266 xleft = (int)(xcResize - rResize) - 1;
00267 yleft = (int)(ycResize - rResize) - 1;
00268 outDims[0] = (int)(2*rResize) + 3; outDims[1] = (int)(2*rResize) + 3;
00269 outResize = (float *) malloc(outDims[0] * outDims[1] * sizeof(float));
00270
00271 for (jy = 0; jy < outDims[1]; jy++)
00272 {
00273 int ix = 0;
00274 jData = (jy + yleft) * xout;
00275 for (ix = 0; ix < outDims[0]; ix++)
00276 {
00277 imgData = jData + ix + xleft;
00278 outResize[jy * outDims[0] + ix] = outData[imgData];
00279 }
00280 }
00281
00282 mm = outDims[0]; nn = outDims[1];
00283 coef = (double *)malloc(order * order * sizeof(double));
00284 fitimage_float(outResize, mm, nn, order, 1, coef);
00285
00286
00287
00288 outRD = drms_create_records(drms_env, 1, outRecQuery, DRMS_PERMANENT, &status);
00289 if (status) DIE("Output recordset not created");
00290 outRec = outRD->records[0];
00291 drms_setkey_time(outRec, "T_REC_O", t_rec);
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301 drms_setkey_float(outRec, "CRPIX1_O", crpix1);
00302 drms_setkey_float(outRec, "CRPIX2_O", crpix2);
00303 drms_setkey_float(outRec, "CRVAL1_O", crval1);
00304 drms_setkey_float(outRec, "CRVAL2_O", crval2);
00305 drms_setkey_float(outRec, "CDELT1_O", cdelt1);
00306 drms_setkey_float(outRec, "CDELT2_O", cdelt2);
00307 drms_setkey_float(outRec, "CROTA2_O", crota2);
00308 drms_setkey_double(outRec, "DSUN_ORI", dSun);
00309 drms_setkey_double(outRec, "RSUN_ORI", rSun);
00310 drms_setkey_int(outRec, "VRCENT", (int)(vr_center));
00311
00312 outDims[0] = order; outDims[1] = order;
00313 outSeg = drms_segment_lookupnum(outRec, 0);
00314 outArray = drms_array_create(DRMS_TYPE_DOUBLE, 2, outDims, coef, &status);
00315 status = drms_segment_write(outSeg, outArray, 0);
00316 if (status) printf("problem writing file, skip! \n");
00317 drms_setkey_int(outRec, "NGOOD", ngood);
00318 drms_setkey_int(outRec, "POLYDEGR", order);
00319 drms_setkey_int(outRec, "MASKID", maskid);
00320 drms_setkey_float(outRec, "BCUTOFF", bCutoff);
00321 drms_setkey_float(outRec, "DELTAVR", vrstep);
00322 sprint_ut(tstr, CURRENT_SYSTEM_TIME);
00323 sscanf(tstr, "%d.%d.%d", &y, &m, &d);
00324 sprintf(tstr, "%04d-%02d-%02d", y, m, d);
00325 drms_setkey_string(outRec, "DATE", tstr);
00326 drms_free_array(outArray);
00327 free(outResize); free(outData); free(bPixel); free(datacube);
00328 drms_close_records(outRD, DRMS_INSERT_RECORD);
00329 }
00330 drms_close_records(inRD, DRMS_FREE_RECORD);
00331 }
00332
00333 return 0;
00334 }
00335
00336 float median(int n, float x[]) {
00337 float temp;
00338 int i, j;
00339
00340 for(i=0; i<n-1; i++) {
00341 for(j=i+1; j<n; j++) {
00342 if(x[j] < x[i]) {
00343
00344 temp = x[i];
00345 x[i] = x[j];
00346 x[j] = temp;
00347 }
00348 }
00349 }
00350
00351 if(n%2==0) {
00352
00353 return((x[n/2] + x[n/2 - 1]) / 2.0);
00354 } else {
00355
00356 return x[n/2];
00357 }
00358 }
00359
00360
00361
00362
00363
00364