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
00034
00035
00036
00037 #include <jsoc_main.h>
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <math.h>
00041 #include <sys/time.h>
00042 #include <sys/resource.h>
00043
00044 #define DIE(msg) {fflush(stdout); fprintf(stderr, "%s, status=%d\n", msg, status); return(status);}
00045
00046 #define DTOR (M_PI/180.)
00047
00048
00049
00050
00051 #define PIX_X(wx,wy) ((((wx-crvalx)*cosa + (wy-crvaly)*sina)/cdelt)+crpix1)
00052 #define PIX_Y(wx,wy) ((((wy-crvaly)*cosa - (wx-crvalx)*sina)/cdelt)+crpix2)
00053 #define WX(pix_x,pix_y) (((pix_x-crpix1)*cosa - (pix_y-crpix2)*sina)*cdelt+crvalx)
00054 #define WY(pix_x,pix_y) (((pix_y-crpix2)*cosa + (pix_x-crpix1)*sina)*cdelt+crvaly)
00055
00056
00057
00058
00059 double getwalltime(void)
00060 {
00061 struct timeval tv;
00062 gettimeofday(&tv, NULL);
00063 return tv.tv_sec * 1000.0 + tv.tv_usec/1000.0;
00064 }
00065
00066 double getcputime(double *utime, double *stime)
00067 {
00068
00069 struct rusage ru;
00070 getrusage(RUSAGE_SELF, &ru);
00071 *utime = ru.ru_utime.tv_sec * 1000.0 + ru.ru_utime.tv_usec / 1000.0;
00072 *stime = ru.ru_stime.tv_sec * 1000.0 + ru.ru_stime.tv_usec / 1000.0;
00073 return *utime + *stime;
00074 }
00075
00076
00077
00078
00079
00080
00081 char *module_name = "patch_los2vec";
00082
00083
00084
00085 ModuleArgs_t module_args[] =
00086 {
00087 {ARG_STRING, "plos", NULL, "Input LoS patches."},
00088 {ARG_STRING, "vmag", NULL, "Input vector magnetograms."},
00089 {ARG_STRING, "pvec", NULL, "Output vector patches."},
00090 {ARG_INT, "buffx", "0", "Buffer around the identified patch, as needed."},
00091 {ARG_INT, "buffy", "0", "Buffer around the identified patch, as needed."},
00092 {ARG_INT, "VERB", "1", "Level of verbosity: 0=errors/warnings; 1=all messages"},
00093 {ARG_END}
00094 };
00095
00096
00097 int DoIt(void)
00098 {
00099 int status = DRMS_SUCCESS;
00100 char *plosQuery, *vmag, *vmagQuery, *pvecQuery;
00101 DRMS_RecordSet_t *plosRS, *vmagRS, *pvecRS;
00102 int irec, nrecs, imag, nmag;
00103 DRMS_Record_t *plosRec, *vmagRec, *pvecRec;
00104 DRMS_Segment_t *plosSeg, *pvecSeg;
00105 DRMS_Array_t *plosArray, *pvecArray;
00106 DRMS_Link_t *srcLink;
00107 char *plosBitmap, *pvecBitmap;
00108
00109 int i, j, verbflag, outDims[2], buffx, buffy;
00110 int n0, n1, mapsz;
00111 int pnum;
00112 TIME t_rec, dt;
00113 char *trec_str;
00114
00115
00116 double wt0, wt1, wt;
00117 double ut0, ut1, ut;
00118 double st0, st1, st;
00119 double ct0, ct1, ct;
00120 wt0 = getwalltime();
00121 ct0 = getcputime(&ut0, &st0);
00122
00123
00124 plosQuery = (char *)params_get_str(&cmdparams, "plos");
00125 vmag = (char *)params_get_str(&cmdparams, "vmag");
00126 pvecQuery = (char *)params_get_str(&cmdparams, "pvec");
00127 verbflag = params_get_int(&cmdparams, "VERB");
00128 buffx = params_get_int(&cmdparams, "buffx");
00129 buffy = params_get_int(&cmdparams, "buffy");
00130
00131
00132 plosRS = drms_open_records(drms_env, plosQuery, &status);
00133 if (status || plosRS->n == 0) DIE("No magnetic input data found");
00134 nrecs = plosRS->n;
00135
00136
00137 for (irec = 0; irec < nrecs; irec++)
00138 {
00139
00140 if (verbflag) {
00141 wt1 = getwalltime();
00142 ct1 = getcputime(&ut1, &st1);
00143 printf("processing record %d...\n", irec);
00144 }
00145
00146
00147 plosRec = plosRS->records[irec];
00148 t_rec = drms_getkey_time(plosRec, "T_REC", &status);
00149
00150
00151 vmagQuery = (char *)malloc(100 * sizeof(char));
00152 trec_str = (char *)malloc(30 * sizeof(char));
00153 sprint_time(trec_str, t_rec, "TAI", 0);
00154 sprintf(vmagQuery, "%s[%s]", vmag, trec_str); printf("%s\n", trec_str);
00155
00156 vmagRS = drms_open_records(drms_env, vmagQuery, &status);
00157 if (status || vmagRS->n != 1) {
00158 printf("No magnetic input data found\n"); fflush(stdout);
00159 free(trec_str); trec_str = NULL;
00160 free(vmagQuery); vmagQuery = NULL;
00161 if (vmagRS) drms_close_records(vmagRS, DRMS_FREE_RECORD);
00162 continue;
00163 }
00164 vmagRec = vmagRS->records[0];
00165
00166
00167
00168 plosSeg = drms_segment_lookup(plosRec, "bitmap");
00169 plosArray = drms_segment_read(plosSeg, DRMS_TYPE_CHAR, &status);
00170 if (status) {
00171 printf("No bitmap found for patch #%d. \n", irec); fflush(stdout);
00172 drms_free_array(plosArray);
00173 continue;
00174 }
00175 plosBitmap = (char *)plosArray->data;
00176 n0 = plosArray->axis[0]; n1 = plosArray->axis[1];
00177
00178 mapsz = n0 * n1;
00179
00180
00181 outDims[0] = n0 + 2 * buffx; outDims[1] = n1 + 2 * buffy;
00182 pvecArray = drms_array_create(DRMS_TYPE_CHAR, 2, outDims, NULL, &status);
00183 pvecBitmap = (char *)pvecArray->data;
00184 for (i = 0; i < n0; i++) {
00185 for (j = 0; j < n1; j++) {
00186 pvecBitmap[(j + buffy) * outDims[0] + i + buffx] = plosBitmap[j * n0 + i];
00187 }
00188 }
00189
00190
00191 pvecRec = drms_create_record(drms_env, pvecQuery, DRMS_PERMANENT, &status);
00192 if (status)
00193 DIE("Output record not created");
00194 pvecSeg = drms_segment_lookup(pvecRec, "bitmap");
00195 pvecSeg->axis[0] = pvecArray->axis[0];
00196 pvecSeg->axis[1] = pvecArray->axis[1];
00197 pvecArray->parent_segment = pvecSeg;
00198
00199
00200 srcLink = hcon_lookup_lower(&pvecRec->links, "MDATA");
00201 if (srcLink) {
00202 drms_setlink_static(pvecRec, "MDATA", vmagRec->recnum);
00203 }
00204
00205
00206
00207
00208
00209
00210 float crvalx = drms_getkey_float(vmagRec, "CRVAL1", &status);
00211 float crvaly = drms_getkey_float(vmagRec, "CRVAL2", &status);
00212 float cdelt = drms_getkey_float(vmagRec, "CDELT1", &status);
00213 float crpix1 = drms_getkey_float(vmagRec, "CRPIX1", &status);
00214 float crpix2 = drms_getkey_float(vmagRec, "CRPIX2", &status);
00215 float crota2 = drms_getkey_float(vmagRec, "CROTA2", &status);
00216 float sina = sin(crota2 * DTOR);
00217 float cosa = cos(crota2 * DTOR);
00218 float pcx = drms_getkey_float(plosRec, "CRPIX1", &status);
00219 float pcy = drms_getkey_float(plosRec, "CRPIX2", &status);
00220
00221
00222 drms_setkey_string(pvecRec, "BLD_VERS", jsoc_version);
00223 drms_setkey_time(pvecRec, "DATE", CURRENT_SYSTEM_TIME);
00224
00225 drms_copykey(pvecRec, vmagRec, "T_REC");
00226 drms_copykey(pvecRec, plosRec, "PNUM");
00227
00228
00229
00230 drms_setkey_int(pvecRec, "HWIDTH1", drms_getkey_int(plosRec, "HWIDTH1", &status) + buffx);
00231 drms_setkey_int(pvecRec, "HWIDTH2", drms_getkey_int(plosRec, "HWIDTH2", &status) + buffy);
00232 drms_copykey(pvecRec, plosRec, "CRPIX1");
00233 drms_copykey(pvecRec, plosRec, "CRPIX2");
00234 drms_copykey(pvecRec, vmagRec, "CDELT1");
00235 drms_copykey(pvecRec, vmagRec, "CDELT2");
00236 drms_setkey_float(pvecRec, "CRVAL1", WX(pcx,pcy));
00237 drms_setkey_float(pvecRec, "CRVAL2", WY(pcx,pcy));
00238
00239 drms_copykey(pvecRec, plosRec, "PATCHNUM");
00240 drms_copykey(pvecRec, plosRec, "MASK");
00241
00242 drms_copykey(pvecRec, plosRec, "TOTVALS");
00243 drms_copykey(pvecRec, plosRec, "DATAVALS");
00244 drms_copykey(pvecRec, plosRec, "MISSVALS");
00245
00246
00247 status = drms_segment_write(pvecSeg, pvecArray, 0);
00248 if (status) DIE("Problem writing file");
00249 drms_close_record(pvecRec, DRMS_INSERT_RECORD);
00250
00251
00252 drms_free_array(plosArray);
00253 drms_free_array(pvecArray);
00254 free(trec_str); trec_str = NULL;
00255 free(vmagQuery); vmagQuery = NULL;
00256 drms_close_records(vmagRS, DRMS_FREE_RECORD);
00257
00258
00259 if (verbflag) {
00260 wt = getwalltime();
00261 ct = getcputime(&ut, &st);
00262 printf("record %d done, %.2f ms wall time, %.2f ms cpu time\n",
00263 irec, wt - wt1, ct - ct1);
00264 }
00265
00266 }
00267
00268 drms_close_records(plosRS, DRMS_FREE_RECORD);
00269
00270 if (verbflag) {
00271 wt = getwalltime();
00272 ct = getcputime(&ut, &st);
00273 printf("total time spent: %.2f ms wall time, %.2f ms cpu time\n",
00274 wt - wt0, ct - ct0);
00275 }
00276
00277 return(DRMS_SUCCESS);
00278
00279 }