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
00041
00042
00043 #define PI (M_PI)
00044 #define FOURK (4096)
00045 #define FOURK2 (16777216)
00046 #define RADSINDEG (PI/180.)
00047 #define RAD2ARCSEC (648000./M_PI)
00048
00049 #define ARRLENGTH(ARR) (sizeof(ARR) / sizeof(ARR[0]))
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 #define dpath "/home/jsoc/cvs/Development/JSOC"
00056
00057
00058
00059
00060
00061 #define PIX_X(wx,wy) ((((wx-crvalx)*cosa + (wy-crvaly)*sina)/cdelt)+crpix1)
00062 #define PIX_Y(wx,wy) ((((wy-crvaly)*cosa - (wx-crvalx)*sina)/cdelt)+crpix2)
00063 #define WX(pix_x,pix_y) (((pix_x-crpix1)*cosa - (pix_y-crpix2)*sina)*cdelt+crvalx)
00064 #define WY(pix_x,pix_y) (((pix_y-crpix2)*cosa + (pix_x-crpix1)*sina)*cdelt+crvaly)
00065
00066
00067
00068 const char *inSegNames[] = {"azimuth", "conf_disambig", "confid_map", "disambig",
00069 "field", "inclination", "vlos_mag"};
00070 const char *outSegNames[] = {"vlos_mag_corr", "pil_bl", "pil_br"};
00071
00072 const int nInSegs = (ARRLENGTH(inSegNames));
00073 const int nOutSegs = (ARRLENGTH(outSegNames));
00074
00075
00076
00077 struct ephemeris {
00078 double crlt_obs, crln_obs;
00079 double obs_vr, obs_vn, obs_vw;
00080 double rSun_obs, rSun_ref, crota2;
00081 double crpix1, crpix2, cdelt;
00082 TIME t_rec;
00083 };
00084
00085
00086
00087
00088 int getInput(DRMS_Record_t *inRec, double *data_in, struct ephemeris *ephem);
00089
00090
00091 int getEphemeris(DRMS_Record_t *inRec, struct ephemeris *ephem);
00092
00093
00094 int writeOutput(DRMS_Record_t *inRec, DRMS_Record_t *outRec, int writeImg,
00095 float *data_out, float doppcal_bias);
00096
00097
00098 extern void doppcal_estimate_ (int *naxis1, int *naxis2, int *naxis3i, int *naxis3o,
00099 double *data_in, float *data_out,
00100 double *crpix, double *obs_v, double *rsun_obs, double *rsun_ref, double *crlt_obs, double *crota2,
00101 float *doppcal_bias,
00102 double *max_ang_in, double *thresh_blos_in, double *pix_in,
00103 double *thresh_bmag_in, int *rad_los_sep_in);
00104
00105
00106
00107 char *module_name = "cgem_doppcal";
00108
00109 ModuleArgs_t module_args[] =
00110 {
00111 {ARG_STRING, "in", kNotSpecified, "Input data series."},
00112 {ARG_STRING, "out", kNotSpecified, "Output data series."},
00113 {ARG_FLAG, "w", "", "Set flag to write out images."},
00114 {ARG_END}
00115 };
00116
00117 int DoIt(void)
00118 {
00119
00120 int status = DRMS_SUCCESS;
00121
00122
00123
00124 char *inQuery = (char *) params_get_str(&cmdparams, "in");
00125 char *outQuery = (char *) params_get_str(&cmdparams, "out");
00126 int writeImg = params_isflagset(&cmdparams, "w");
00127
00128
00129
00130 DRMS_RecordSet_t *inRS = drms_open_records(drms_env, inQuery, &status);
00131 int nrecs = inRS->n;
00132 if (status || nrecs == 0 || !inRS) {
00133 DIE("Input data series error");
00134 }
00135
00136
00137
00138 DRMS_RecordSet_t *outRS = drms_create_records(drms_env, nrecs, outQuery, DRMS_PERMANENT, &status);
00139 if (status || !outRS) {
00140 drms_close_records(inRS, DRMS_FREE_RECORD);
00141 DIE("Error in output series");
00142 }
00143
00144 printf("==============\nStart processing. %d image(s) in total.\n", nrecs);
00145
00146
00147
00148 for (int irec = 0; irec < nrecs; irec++) {
00149
00150 printf("==============\nProcessing frame %d of %d...\n", irec + 1, nrecs);
00151
00152 status = 0;
00153 struct ephemeris ephem;
00154
00155
00156
00157 printf("Reading input... ");
00158 DRMS_Record_t *inRec = inRS->records[irec];
00159
00160 double *data_in = (double *) (malloc(FOURK2 * nInSegs * sizeof(double)));
00161
00162 status = getInput(inRec, data_in, &ephem);
00163 if (status) {
00164 SHOW("input read error, record skipped.\n");
00165 free(data_in);
00166 continue;
00167 }
00168
00169 printf("done.\n");
00170
00171
00172
00173 printf("Perform correction... "); fflush(stdout);
00174
00175
00176 int naxis1 = FOURK, naxis2 = FOURK, naxis3i = nInSegs, naxis3o = nOutSegs;
00177 double crpix[2] = {ephem.crpix1, ephem.crpix2};
00178 double obs_v[3] = {ephem.obs_vr, ephem.obs_vn, ephem.obs_vw};
00179 double rsun_obs = ephem.rSun_obs, rsun_ref = ephem.rSun_ref;
00180 double crlt_obs = ephem.crlt_obs, crota2 = ephem.crota2;
00181
00182
00183 double max_ang_in = 60., thresh_blos_in = 60., thresh_bmag_in = 250., pix_in = ephem.cdelt;
00184 int rad_los_sep_in = 2;
00185
00186
00187 float doppcal_bias = 0.;
00188 float *data_out = (float *) (malloc(FOURK2 * nOutSegs * sizeof(float)));
00189
00190
00191
00192 doppcal_estimate_ (&naxis1, &naxis2, &naxis3i, &naxis3o,
00193 data_in, data_out,
00194 crpix, obs_v, &rsun_obs, &rsun_ref, &crlt_obs, &crota2,
00195 &doppcal_bias,
00196 &max_ang_in, &thresh_blos_in, &pix_in,
00197 &thresh_bmag_in, &rad_los_sep_in);
00198
00199 printf("done.\n");
00200 printf("doppcal_bias = %f\n", doppcal_bias);
00201
00202
00203
00204 printf("Writing output... ");
00205
00206 DRMS_Record_t *outRec = outRS->records[irec];
00207
00208 status = writeOutput(inRec, outRec, writeImg, data_out, doppcal_bias);
00209
00210 free(data_in); free(data_out);
00211
00212 if (status) {
00213 SHOW("output error, record skipped.\n");
00214 continue;
00215 }
00216
00217 printf("done.\n");
00218
00219 }
00220
00221
00222
00223 drms_close_records(outRS, DRMS_INSERT_RECORD);
00224 drms_close_records(inRS, DRMS_FREE_RECORD);
00225
00226 return DRMS_SUCCESS;
00227
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 int getInput(DRMS_Record_t *inRec, double *data_in, struct ephemeris *ephem)
00240 {
00241
00242 int status = 0;
00243
00244
00245
00246 getEphemeris(inRec, ephem);
00247
00248
00249
00250 for (int iSeg = 0; iSeg < nInSegs; iSeg++) {
00251
00252 DRMS_Segment_t *inSeg = drms_segment_lookup(inRec, inSegNames[iSeg]);
00253 if (!inSeg) {
00254 printf("segment %s error... ", inSegNames[iSeg]);
00255 return -1;
00256 }
00257
00258 DRMS_Array_t *inArray = drms_segment_read(inSeg, DRMS_TYPE_DOUBLE, &status);
00259 if (status) {
00260 printf("array %s erro... ", inSegNames[iSeg]);
00261 return 1;
00262 }
00263
00264 int nx = inArray->axis[0], ny = inArray->axis[1];
00265 int nxny = nx * ny;
00266 if (nx != FOURK || ny != FOURK) {
00267 printf("array %s is not full disk... ", inSegNames[iSeg]);
00268 drms_free_array(inArray);
00269 return 2;
00270 }
00271
00272 double *inData = (double *) (inArray->data);
00273 double *data_in_lead = data_in + iSeg * nxny;
00274 memcpy(data_in_lead, inData, nxny * sizeof(double));
00275
00276 drms_free_array(inArray);
00277
00278 }
00279
00280 return 0;
00281
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 int getEphemeris(DRMS_Record_t *inRec, struct ephemeris *ephem)
00293 {
00294
00295 int status = 0;
00296
00297 ephem->crota2 = drms_getkey_double(inRec, "CROTA2", &status);
00298 double sina = sin(ephem->crota2 * RADSINDEG);
00299 double cosa = cos(ephem->crota2 * RADSINDEG);
00300
00301 ephem->crlt_obs = drms_getkey_double(inRec, "CRLT_OBS", &status);
00302 ephem->crln_obs = drms_getkey_double(inRec, "CRLN_OBS", &status);
00303
00304 double crvalx = drms_getkey_double(inRec, "CRVAL1", &status);
00305 double crvaly = drms_getkey_double(inRec, "CRVAl2", &status);
00306 double crpix1 = drms_getkey_double(inRec, "CRPIX1", &status);
00307 double crpix2 = drms_getkey_double(inRec, "CRPIX2", &status);
00308 double cdelt = drms_getkey_double(inRec, "CDELT1", &status);
00309 ephem->crpix1 = PIX_X(0.0,0.0);
00310 ephem->crpix2 = PIX_Y(0.0,0.0);
00311 ephem->cdelt = cdelt;
00312
00313 ephem->rSun_ref = drms_getkey_double(inRec, "RSUN_REF", &status);
00314 if (status) {
00315 ephem->rSun_ref = 6.96e8;
00316 }
00317 ephem->rSun_obs = drms_getkey_double(inRec, "RSUN_OBS", &status);
00318 if (status) {
00319 double dSun = drms_getkey_double(inRec, "DSUN_OBS", &status);
00320 ephem->rSun_obs = asin(ephem->rSun_ref / dSun) * RAD2ARCSEC;
00321 }
00322
00323 ephem->obs_vr = drms_getkey_double(inRec, "OBS_VR", &status);
00324 ephem->obs_vn = drms_getkey_double(inRec, "OBS_VN", &status);
00325 ephem->obs_vw = drms_getkey_double(inRec, "OBS_VW", &status);
00326
00327 ephem->t_rec = drms_getkey_time(inRec, "T_REC", &status);
00328
00329 return 0;
00330
00331 }
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 int writeOutput(DRMS_Record_t *inRec, DRMS_Record_t *outRec, int writeImg,
00342 float *data_out, float doppcal_bias)
00343 {
00344
00345 int status = 0;
00346
00347
00348
00349 if (writeImg) {
00350
00351 for (int iSeg = 0; iSeg < nOutSegs; iSeg++) {
00352
00353 DRMS_Segment_t *outSeg = drms_segment_lookup(outRec, outSegNames[iSeg]);
00354 if (!outSeg) {
00355 printf("segment %s error... ", outSegNames[iSeg]);
00356 return -1;
00357 }
00358
00359 int nx = FOURK, ny = FOURK, nxny = nx * ny;
00360 int dims[2] = {nx, ny};
00361
00362
00363
00364 float *outData = (float *) (malloc(nxny * sizeof(float)));
00365 float *data_out_lead = data_out + iSeg * nxny;
00366 memcpy(outData, data_out_lead, nxny * sizeof(float));
00367
00368 DRMS_Array_t *outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, dims, outData, &status);
00369 if (status) {
00370 printf("array %s create error... ", outSegNames[iSeg]);
00371 free(outData);
00372 return 1;
00373 }
00374
00375 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
00376 outArray->israw = 0;
00377 outArray->bzero = outSeg->bzero;
00378 outArray->bscale = outSeg->bscale;
00379
00380 status = drms_segment_write(outSeg, outArray, 0);
00381 if (status) {
00382 printf("array %s write error... ", outSegNames[iSeg]);
00383 free(outData);
00384 return 2;
00385 }
00386
00387 drms_free_array(outArray);
00388
00389 }
00390
00391 }
00392
00393
00394
00395 drms_copykeys(outRec, inRec, 0, kDRMS_KeyClass_Explicit);
00396
00397 drms_setkey_float(outRec, "DOPPBIAS", doppcal_bias);
00398 drms_setkey_string(outRec, "BUNIT_000", "cm/s");
00399 drms_setkey_string(outRec, "BUNIT_001", " ");
00400 drms_setkey_string(outRec, "BUNIT_002", " ");
00401
00402
00403 TIME val, tnow, UNIX_epoch = -220924792.000;
00404 tnow = (double)time(NULL);
00405 tnow += UNIX_epoch;
00406 drms_setkey_time(outRec, "DATE", tnow);
00407
00408
00409
00410 DRMS_Link_t *link = hcon_lookup_lower(&outRec->links, "BDATA");
00411 if (link) drms_link_set("BDATA", outRec, inRec);
00412
00413
00414
00415 return 0;
00416 }
00417