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 #include <jsoc_main.h>
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <math.h>
00038 #include <sys/time.h>
00039 #include <sys/resource.h>
00040 #include "copy_me_keys.c"
00041
00042 #define DIE(msg) {fflush(stdout); fprintf(stderr, "%s, status=%d\n", msg, status); return(status);}
00043 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00044
00045 #define DTOR (M_PI/180.)
00046
00047 #ifndef MIN
00048 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
00049 #endif
00050 #ifndef MAX
00051 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
00052 #endif
00053
00054
00055
00056
00057 #define PIX_X(wx,wy) ((((wx-crvalx)*cosa + (wy-crvaly)*sina)/cdelt)+crpix1)
00058 #define PIX_Y(wx,wy) ((((wy-crvaly)*cosa - (wx-crvalx)*sina)/cdelt)+crpix2)
00059 #define WX(pix_x,pix_y) (((pix_x-crpix1)*cosa - (pix_y-crpix2)*sina)*cdelt+crvalx)
00060 #define WY(pix_x,pix_y) (((pix_y-crpix2)*cosa + (pix_x-crpix1)*sina)*cdelt+crvaly)
00061
00062
00063
00064
00065
00066 char *module_name = "m2meharp";
00067
00068
00069
00070 ModuleArgs_t module_args[] =
00071 {
00072 {ARG_STRING, "mharp", NULL, "Input LoS HARP series."},
00073 {ARG_STRING, "me", NULL, "Input ME inversion series, series name only."},
00074 {ARG_STRING, "meharp", NULL, "Output ME HARP series."},
00075 {ARG_INT, "buffx", "0", "Buffer around the identified patch, as needed."},
00076 {ARG_INT, "buffy", "0", "Buffer around the identified patch, as needed."},
00077 {ARG_END}
00078 };
00079
00080 int DoIt(void)
00081 {
00082 int status = DRMS_SUCCESS;
00083
00084 char *mHarpQuery, *meQuery, *meHarpQuery, *meSeriesName;
00085
00086 DRMS_RecordSet_t *mHarpRS=NULL, *meRS=NULL, *meHarpRS=NULL;
00087 DRMS_Record_t *mHarpRec=NULL, *meRec=NULL, *meHarpRec=NULL;
00088 DRMS_Segment_t *mHarpSeg=NULL, *meHarpSeg=NULL;
00089 DRMS_Array_t *mHarpArray=NULL, *meHarpArray=NULL;
00090 DRMS_Link_t *meLink=NULL;
00091
00092 char *mBitmap=NULL, *meBitmap=NULL;
00093
00094 int irec, nrecs, imag, nmag;
00095 int nx, ny, mapsz;
00096 int outDims[2], buffx, buffy;
00097
00098 TIME t_rec;
00099 char *t_rec_str=NULL;
00100
00101
00102
00103 mHarpQuery = (char *) params_get_str(&cmdparams, "mharp");
00104 meSeriesName = (char *) params_get_str(&cmdparams, "me");
00105 meHarpQuery = (char *) params_get_str(&cmdparams, "meharp");
00106
00107 buffx = params_get_int(&cmdparams, "buffx"); buffx = MAX(buffx,0);
00108 buffy = params_get_int(&cmdparams, "buffy"); buffy = MAX(buffy,0);
00109
00110
00111
00112 mHarpRS = drms_open_records(drms_env, mHarpQuery, &status);
00113 if (status || mHarpRS->n == 0) DIE("No Mharp input data found");
00114 nrecs = mHarpRS->n;
00115
00116
00117
00118 for (irec = 0; irec < nrecs; irec++) {
00119
00120
00121
00122 mHarpRec = mHarpRS->records[irec];
00123 t_rec = drms_getkey_time(mHarpRec, "T_REC", &status);
00124 printf("Record #%d of #%d\n", irec+1, nrecs); fflush(stdout);
00125
00126
00127
00128 meQuery = (char *) malloc(100 * sizeof(char));
00129 t_rec_str = (char *) malloc(30 * sizeof(char));
00130
00131 sprint_time(t_rec_str, t_rec, "TAI", 0);
00132 sprintf(meQuery, "%s[%s]", meSeriesName, t_rec_str);
00133 meRS = drms_open_records(drms_env, meQuery, &status);
00134 if (status || meRS->n < 1) {
00135
00136 free(t_rec_str); free(meQuery);
00137 if (meRS) drms_close_records(meRS, DRMS_FREE_RECORD);
00138 t_rec_str = NULL; meQuery = NULL;
00139 meRS = NULL;
00140 DIE("No ME input data found, skip current record\n");
00141 continue;
00142 }
00143
00144 for (int ii = 0; ii < meRS->n; ii++) {
00145 meRec = meRS->records[ii];
00146
00147
00148
00149 mHarpSeg = drms_segment_lookup(mHarpRec, "bitmap");
00150 mHarpArray = drms_segment_read(mHarpSeg, DRMS_TYPE_CHAR, &status);
00151 if (status) {
00152 SHOW("No bitmap found, skip current record\n");
00153 drms_free_array(mHarpArray);
00154 continue;
00155 }
00156 mBitmap = (char *) mHarpArray->data;
00157 nx = mHarpArray->axis[0]; ny = mHarpArray->axis[1];
00158 mapsz = nx * ny;
00159
00160
00161
00162 outDims[0] = nx + 2 * buffx; outDims[1] = ny + 2 * buffy;
00163 meHarpArray = drms_array_create(DRMS_TYPE_CHAR, 2, outDims, NULL, &status);
00164 meBitmap = (char *) meHarpArray->data;
00165
00166 for (int j = 0; j < ny; j++) {
00167 for (int i = 0; i < nx; i++) {
00168 meBitmap[(j + buffy) * outDims[0] + (i + buffx)] = mBitmap[j * nx + i];
00169 }
00170 }
00171
00172
00173
00174 meHarpRec = drms_create_record(drms_env, meHarpQuery, DRMS_PERMANENT, &status);
00175 if (status) {
00176 SHOW("Output error, skip current record\n");
00177 drms_free_array(mHarpArray); drms_free_array(meHarpArray);
00178 continue;
00179 }
00180 meHarpSeg = drms_segment_lookup(meHarpRec, "bitmap");
00181 meHarpSeg->axis[0] = outDims[0];
00182 meHarpSeg->axis[1] = outDims[1];
00183 meHarpArray->parent_segment = meHarpSeg;
00184
00185
00186
00187 meLink = hcon_lookup_lower(&meHarpRec->links, "MDATA");
00188 if (meLink) {
00189 drms_setlink_static(meHarpRec, "MDATA", meRec->recnum);
00190 }
00191
00192
00193
00194
00195 drms_copykey(meHarpRec, meRec, "T_REC");
00196 drms_copykey(meHarpRec, mHarpRec, "HARPNUM");
00197 drms_copykey(meHarpRec, meRec, "RUNNUM");
00198
00199 copy_me_keys(meRec, meHarpRec);
00200 copy_patch_keys(mHarpRec, meHarpRec);
00201 copy_geo_keys(mHarpRec, meHarpRec);
00202
00203 drms_setkey_string(meHarpRec, "BLD_VERS", jsoc_version);
00204 char timebuf[1024];
00205 float UNIX_epoch = -220924792.000;
00206 sprint_time(timebuf, (double)time(NULL) + UNIX_epoch, "ISO", 0);
00207 drms_setkey_string(meHarpRec, "DATE", timebuf);
00208
00209 drms_setkey_string(meHarpRec, "BUNIT_025", " ");
00210 drms_setkey_float(meHarpRec, "CRPIX1",
00211 drms_getkey_float(mHarpRec, "CRPIX1", &status)-buffx);
00212 drms_setkey_float(meHarpRec, "CRPIX2",
00213 drms_getkey_float(mHarpRec, "CRPIX2", &status)-buffy);
00214
00215
00216 status = drms_segment_write(meHarpSeg, meHarpArray, 0);
00217 if (status) {
00218 SHOW("Output error, skip current record\n");
00219 drms_free_array(mHarpArray); drms_free_array(meHarpArray);
00220 continue;
00221 }
00222
00223 drms_free_array(meHarpArray);
00224 drms_close_record(meHarpRec, DRMS_INSERT_RECORD);
00225
00226 }
00227
00228
00229 drms_free_array(mHarpArray); meHarpArray = NULL;
00230 free(t_rec_str); free(meQuery); t_rec_str = NULL; meQuery = NULL;
00231 drms_close_records(meRS, DRMS_FREE_RECORD); meRS = NULL;
00232
00233 }
00234
00235 drms_close_records(mHarpRS, DRMS_FREE_RECORD);
00236
00237 return 0;
00238
00239 }