00001 #include "jsoc.h"
00002 #include "jsoc_main.h"
00003
00004 char *module_name = "hmi_fixCROTA2";
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 #define DIE(msg) {fflush(stdout);fprintf(stderr,"%s, status=%d\n",msg,status); return(status);}
00033
00034 #define NOTSPECIFIED "Not Specified"
00035
00036 ModuleArgs_t module_args[] =
00037 {
00038 {ARG_STRING, "ds", NOTSPECIFIED, "data series to process."},
00039 {ARG_TIME, "first", NOTSPECIFIED, "first time to process."},
00040 {ARG_TIME, "last", NOTSPECIFIED, "last time to process."},
00041 {ARG_END}
00042 };
00043
00044 #define CAM1_DELTA (0.0135 - 0.082596) // i.e. change 180.082596 to 180.0135 by adding CAM1_DELTA to INST_ROT and CROTAT2
00045 #define CAM2_DELTA (-0.0702) // i.e. change 180.0 to 179.9298 by adding CAM1_DELTA to INST_ROT and CROTAT2
00046 #define BLOCKSIZE (4000) // approx number of records to process in one call, will be rounded down to nice time.
00047 #define HOUR (3600)
00048 #define DAY (86400)
00049
00050 int DoIt(void)
00051 {
00052 int status = DRMS_SUCCESS;
00053 DRMS_Record_t *inTemplate;
00054 DRMS_RecordSet_t *inRS, *outRS;
00055 DRMS_Keyword_t *pkey;
00056 int irec, nrecs, npkeys;
00057 const char *dsSeries = params_get_str(&cmdparams, "ds");
00058 char dsQuery[DRMS_MAXQUERYLEN];
00059 char *timekeyname;
00060 TIME t_first = params_get_time(&cmdparams, "first");
00061 TIME t_last = params_get_time(&cmdparams, "last");
00062 TIME t_step, t_epoch, t_block, t_start, t_stop;
00063 int hasCALVER64;
00064 int hasCALVER32;
00065
00066 char history[4096];
00067
00068
00069 inTemplate = drms_template_record(drms_env, dsSeries, &status);
00070 if (status || !inTemplate) DIE("series not found");
00071 npkeys = inTemplate->seriesinfo->pidx_num;
00072 timekeyname = NULL;
00073 if (npkeys > 0)
00074 {
00075 int i;
00076 for (i=0; i<npkeys; i++)
00077 {
00078 pkey = inTemplate->seriesinfo->pidx_keywords[i];
00079 if (pkey->info->recscope > 1)
00080 pkey = drms_keyword_slotfromindex(pkey);
00081 if (pkey->info->type != DRMS_TYPE_TIME)
00082 continue;
00083 if(i > 0) DIE("Input series must have TIME keyword first, for now...");
00084 timekeyname = pkey->info->name;
00085 t_step = drms_keyword_getdouble(drms_keyword_stepfromslot(pkey), &status);
00086 if (status) DIE("problem getting t_step");
00087 t_epoch = drms_keyword_getdouble(drms_keyword_epochfromslot(pkey), &status);
00088 if (status) DIE("problem getting t_epoch");
00089 break;
00090 }
00091 }
00092 else
00093 DIE("Must have time prime key");
00094
00095 hasCALVER64 = drms_keyword_lookup(inTemplate, "CALVER64", 0) != NULL;
00096 hasCALVER32 = drms_keyword_lookup(inTemplate, "CALVER32", 0) != NULL;
00097
00098 fprintf(stdout,"Series %s hasCALVER32=%d hasCALVER64=%d\n",dsSeries, hasCALVER32, hasCALVER64);
00099 fflush(stdout);
00100
00101 if (strncmp(dsSeries, "hmi.lev1", 8) && !hasCALVER64)
00102 DIE("Must have non-linked CALVER64 keyword for products above lev1");
00103
00104 if (strncmp(dsSeries, "hmi.lev1", 8) == 0 && !hasCALVER32)
00105 DIE("Must have non-linked CALVER32 keyword for lev1 products");
00106
00107 t_block = t_step * BLOCKSIZE;
00108 if (t_block > 100*DAY)
00109 t_block = 100*DAY;
00110 else if (t_block > 10*DAY)
00111 t_block = 10*DAY;
00112 else if (t_block > DAY)
00113 t_block = DAY;
00114 else if (t_block > 6*HOUR)
00115 t_block = 6*3600;
00116
00117 for (t_start = t_first; t_start <= t_last; t_start += t_block)
00118 {
00119 char first[100], last[100];
00120 t_stop = t_start + t_block - t_step;
00121 if (t_stop > t_last)
00122 t_stop = t_last;
00123 sprint_time(first, t_start, pkey->info->unit, 0);
00124 sprint_time(last, t_stop, pkey->info->unit, 0);
00125
00126 sprintf(dsQuery, "%s[%s-%s]", dsSeries, first, last);
00127 fprintf(stdout,"Query %s ",dsQuery);
00128 fflush(stdout);
00129
00130 inRS = drms_open_records(drms_env, dsQuery, &status);
00131 nrecs = inRS->n;
00132 if (status || nrecs == 0)
00133 {
00134 fprintf(stdout, " status=%d, no records found, skip this block\n",status);
00135 fflush(stdout);
00136 continue;
00137 }
00138
00139 outRS = drms_clone_records_nosums(inRS, DRMS_PERMANENT, DRMS_SHARE_SEGMENTS, &status);
00140 nrecs = outRS->n;
00141 if (status || nrecs == 0)
00142 DIE("No records cloned");
00143 drms_close_records(inRS, DRMS_FREE_RECORD);
00144
00145 for (irec=0; irec<nrecs; irec++)
00146 {
00147 DRMS_Record_t *rec = outRS->records[irec];
00148 double inst_rot, crota2;
00149 unsigned long long calver64 = 0;
00150 unsigned int calver32 = 0;
00151 int quality, camera, instrot_status;
00152
00153 quality = drms_getkey_int(rec, "QUALITY", &status);
00154
00155
00156
00157 if (hasCALVER64)
00158 {
00159 calver64 = (unsigned long long)drms_getkey_longlong(rec, "CALVER64", NULL);
00160 if ((calver64 & 0xF0) != 0)
00161 {
00162 fprintf(stdout, "Record previously processed, skip, first=%s, irec=%d\n", first, irec);
00163 fflush(stdout);
00164 continue;
00165 }
00166 }
00167 else if (hasCALVER32)
00168 {
00169 calver32 = (unsigned int)drms_getkey_int(rec, "CALVER32", NULL);
00170 if ((calver32 & 0xF0) != 0)
00171 {
00172 fprintf(stdout, "Record previously processed, skip, first=%s, irec=%d\n", first, irec);
00173 fflush(stdout);
00174 continue;
00175 }
00176 }
00177
00178 inst_rot = drms_getkey_double(rec, "INST_ROT", &instrot_status);
00179 camera = drms_getkey_int(rec, "CAMERA", NULL);
00180 crota2 = drms_getkey_double(rec, "CROTA2", NULL);
00181
00182
00183 if (camera == 1)
00184 {
00185 crota2 += CAM1_DELTA;
00186 inst_rot += CAM1_DELTA;
00187 sprintf(history, "CROTA2 corrected by adding %6.4f degrees", CAM1_DELTA);
00188 }
00189 else
00190 {
00191 crota2 += CAM2_DELTA;
00192 inst_rot += CAM2_DELTA;
00193 sprintf(history, "CROTA2 corrected by adding %6.4f degrees", CAM2_DELTA);
00194 }
00195
00196 if (hasCALVER64)
00197 {
00198 calver64 &= 0xFFFFFFFFFFFFFF0F;
00199 calver64 |= 0x0000000000000010;
00200 drms_setkey_longlong(rec, "CALVER64", calver64);
00201 }
00202 if (hasCALVER32)
00203 {
00204 if (calver32 == 0x80000000)
00205 calver32 = 0xFFFFFFFF;
00206 calver32 &= 0xFFFFFF0F;
00207 calver32 |= 0x00000010;
00208 drms_setkey_int(rec, "CALVER32", calver32);
00209 }
00210
00211 if (!instrot_status)
00212 drms_setkey_double(rec, "INST_ROT", inst_rot);
00213 drms_setkey_double(rec, "CROTA2", crota2);
00214 drms_appendhistory(rec, history, 1);
00215 drms_setkey_time(rec, "DATE", CURRENT_SYSTEM_TIME);
00216 }
00217
00218 if (drms_close_records(outRS, DRMS_INSERT_RECORD))
00219 fprintf(stderr, "drms_close_records failure for first=%s\n", first);
00220
00221 fprintf(stdout, "%s CROTA2 fixed for %s through %s\n", dsSeries, first, last);
00222 fflush(stdout);
00223 }
00224
00225 return (DRMS_SUCCESS);
00226 }