00001 #include "jsoc.h"
00002 #include "jsoc_main.h"
00003
00004 char *module_name = "aia_fixCROTA2";
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #define DIE(msg) {fflush(stdout);fprintf(stderr,"%s, status=%d\n",msg,status); return(status);}
00018
00019 #define NOTSPECIFIED "Not Specified"
00020 #define MP_SERIES "sdo.master_pointing"
00021 #define GMP_MAX_MPO_REC_SIZE 200
00022
00023 ModuleArgs_t module_args[] =
00024 {
00025 {ARG_STRING, "ds", NOTSPECIFIED, "data series to process."},
00026 {ARG_END}
00027 };
00028
00029 static char mp_query[512], respq[512];
00030
00031 int DoIt(void)
00032 {
00033 int status = DRMS_SUCCESS;
00034 DRMS_Record_t *rmp, *rresp;
00035 DRMS_RecordSet_t *inRS, *outRS, *rsmp=NULL, *rs_resp=NULL;;
00036 int irec, nrecs, nresp;
00037 const char *dsSeries = params_get_str(&cmdparams, "ds");
00038 TIME t_obs, mpt_end=0.0;
00039
00040 inRS = drms_open_records(drms_env, dsSeries, &status);
00041 nrecs = inRS->n;
00042 if (status || nrecs == 0)
00043 {
00044 fprintf(stdout, " status=%d, no records found, skip this block\n",status);
00045 fflush(stdout);
00046 return (DRMS_SUCCESS);
00047 }
00048
00049 outRS = drms_clone_records_nosums(inRS, DRMS_PERMANENT, DRMS_SHARE_SEGMENTS, &status);
00050 nrecs = outRS->n;
00051 if (status || nrecs == 0)
00052 DIE("No records cloned");
00053 drms_close_records(inRS, DRMS_FREE_RECORD);
00054
00055 for (irec=0; irec<nrecs; irec++)
00056 {
00057 DRMS_Record_t *rec = outRS->records[irec];
00058 float arot[10], ascl[10];
00059 float crota2, sat_rot;
00060 int i, quality, instrot_status, wl;
00061 char mpo_rec[GMP_MAX_MPO_REC_SIZE];
00062 char *rotkeys[] = { "A_094_INSTROT", "A_131_INSTROT", "A_171_INSTROT",
00063 "A_193_INSTROT", "A_211_INSTROT", "A_304_INSTROT", "A_335_INSTROT",
00064 "A_1600_INSTROT", "A_1700_INSTROT", "A_4500_INSTROT"};
00065 char *sclkeys[] = { "A_094_IMSCALE", "A_131_IMSCALE", "A_171_IMSCALE",
00066 "A_193_IMSCALE", "A_211_IMSCALE", "A_304_IMSCALE", "A_335_IMSCALE",
00067 "A_1600_IMSCALE", "A_1700_IMSCALE", "A_4500_IMSCALE"};
00068 char *dsresp = "aia.response";
00069 char *wavstr = drms_getkey_string(rec, "WAVE_STR", &status);
00070
00071 quality = drms_getkey_int(rec, "QUALITY", &status);
00072 if (!status && quality < 0)
00073 continue;
00074
00075 t_obs = drms_getkey_time(rec, "T_OBS", &status);
00076 sat_rot = drms_getkey_float(rec, "SAT_ROT", NULL);
00077 if (drms_ismissing_float(sat_rot)) sat_rot = 0.0;
00078 wl = drms_getkey_int(rec, "WAVELNTH", NULL);
00079
00080
00081 if (t_obs > mpt_end) {
00082 int iw, nrmp;
00083 sprintf(mp_query, "%s[? T_START <= %f and %f < T_STOP ?]",
00084 MP_SERIES, t_obs, t_obs);
00085 rsmp = drms_open_records(drms_env, mp_query, &status);
00086 nrmp = rsmp->n;
00087 if (status || nrmp != 1) DIE("No Master Pointing Record");
00088 rmp = rsmp->records[0];
00089 sprintf(mpo_rec, "%s[:#%lld]", MP_SERIES, rmp->recnum);
00090 mpt_end = drms_getkey_time(rmp, "T_STOP", &status);
00091 if (status) DIE("No T_STOP for Master Pointing Record");
00092 for (iw=0; iw<10; iw++) {
00093 arot[iw] = drms_getkey_float(rmp, rotkeys[iw], &instrot_status);
00094 if (instrot_status) DIE("Value for inst rotation not found");
00095 ascl[iw] = drms_getkey_float(rmp, sclkeys[iw], &status);
00096 if (status) DIE("Value for inst scale not found");
00097 }
00098 drms_close_records(rsmp, DRMS_FREE_RECORD);
00099 }
00100 switch (wl) {
00101 case 94: i = 0; break;
00102 case 131: i = 1; break;
00103 case 171: i = 2; break;
00104 case 193: i = 4; break;
00105 case 211: i = 4; break;
00106 case 304: i = 5; break;
00107 case 335: i = 6; break;
00108 case 1600: i = 7; break;
00109 case 1700: i = 8; break;
00110 case 4500: i = 9; break;
00111 default: DIE("Bad wavelength in AIA level 1 record");
00112 }
00113 drms_setkey_float(rec, "INST_ROT", arot[i]);
00114 drms_setkey_float(rec, "CDELT1", ascl[i]);
00115 drms_setkey_float(rec, "CDELT2", ascl[i]);
00116 drms_setkey_float(rec, "IMSCL_MP", ascl[i]);
00117 crota2 = sat_rot + arot[i];
00118 drms_setkey_float(rec, "CROTA2", crota2);
00119 drms_setkey_time(rec, "DATE", CURRENT_SYSTEM_TIME);
00120 drms_setkey_string(rec, "MPO_REC", mpo_rec);
00121 sprintf(respq, "%s[][%s][? t_start <= %10.5f and t_stop > %10.5f ?]",
00122 dsresp, wavstr, t_obs, t_obs);
00123 rresp = NULL;
00124 rs_resp = drms_open_records(drms_env, respq, &status);
00125 if (status) { DIE("Can not open aia.response series.\n");
00126 } else {
00127 nresp = rs_resp->n;
00128 if (nresp>0) rresp = rs_resp->records[0];
00129 }
00130 if(rresp) {
00131 float eperdn = drms_getkey_float(rresp, "EPERDN", &status);
00132 drms_setkey_float(rec, "DN_GAIN", eperdn);
00133 float dnperpht = drms_getkey_float(rresp, "DNPERPHT", &status);
00134 drms_setkey_float(rec, "DNPERPHT", dnperpht);
00135 float eff_wl = drms_getkey_float(rresp, "EFF_WVLN", &status);
00136 drms_setkey_float(rec, "EFF_WVLN", eff_wl);
00137 float p1 = drms_getkey_float(rresp, "EFFA_P1", &status);
00138 float p2 = drms_getkey_float(rresp, "EFFA_P2", &status);
00139 float p3 = drms_getkey_float(rresp, "EFFA_P3", &status);
00140 TIME t_start = drms_getkey_float(rresp, "T_START", &status);
00141 float dt = (float) (t_obs - t_start)/86400.0;
00142 float factor = ((p3*dt + p2)*dt + p1)*dt + 1.0;
00143 float eff_area = drms_getkey_float(rresp, "EFF_AREA", &status);
00144 eff_area = eff_area*factor;
00145 drms_setkey_float(rec, "EFF_AREA", eff_area);
00146 int ver_num = drms_getkey_int(rresp, "VER_NUM", &status);
00147 drms_setkey_int(rec, "DN_GN_V", ver_num);
00148 drms_setkey_int(rec, "EFF_AR_V", ver_num);
00149 }
00150 }
00151 if (drms_close_records(outRS, DRMS_INSERT_RECORD))
00152 fprintf(stderr, "drms_close_records failure for %s\n", dsSeries);
00153
00154 fprintf(stdout, "%s CROTA2 fixed\n", dsSeries);
00155 fflush(stdout);
00156
00157 return (DRMS_SUCCESS);
00158 }