00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "jsoc.h"
00010 #include "jsoc_main.h"
00011 #include "atoinc.h"
00012
00013 char *module_name = "count_unslotted";
00014
00015 #define DIE(msg) {fprintf(stderr,"%s Status=%d\n",msg, status); return(status?status:1);}
00016 #define DIE_get_recset(msg) {fprintf(stderr,"$$$$ %s: %s\n", module_name, msg); return NULL;}
00017
00018 ModuleArgs_t module_args[] =
00019 {
00020 {ARG_STRING, "in", "NOTSPECIFIED", "input series. e.g 'mdi.fd_M_96m_lev18'"},
00021 {ARG_TIME, "epoch", "1993.01.01_TAI", "Reference epoch for '@' slots" },
00022 {ARG_END}
00023 };
00024
00025 char *get_input_recset(DRMS_Env_t *drms_env, char *inQuery);
00026
00027 int DoIt(void)
00028 {
00029 CmdParams_t *params = &cmdparams;
00030 DRMS_RecordSet_t *inRS = NULL;
00031 int status = DRMS_SUCCESS, nrecs;
00032 char inQuery[DRMS_MAXQUERYLEN];
00033 char in[DRMS_MAXQUERYLEN];
00034 const char *ingiven = params_get_str(params, "in");
00035 char *new_in, *in_filename;
00036 strcpy(in, ingiven);
00037 new_in = get_input_recset(drms_env, in);
00038 if (new_in != in)
00039 {
00040 strcpy(in, new_in);
00041 in_filename = new_in+1;
00042 }
00043
00044 inRS = drms_open_records(drms_env, in, &status);
00045 if (status)
00046 {
00047 fprintf(stderr,"Query is: %s\n",in);
00048 DIE("No input data found");
00049 }
00050 nrecs = inRS->n;
00051 drms_close_records(inRS, DRMS_FREE_RECORD);
00052 if (in_filename)
00053 {
00054 unlink(in_filename);
00055 }
00056
00057 printf("%d\n", nrecs);
00058 return DRMS_SUCCESS;
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 char *get_input_recset(DRMS_Env_t *drms_env, char *inQuery)
00075 {
00076 static char newInQuery[DRMS_MAXSERIESNAMELEN+2];
00077 int epoch_given = cmdparams_exists(&cmdparams, "epoch");
00078 TIME epoch, t_epoch;
00079 DRMS_Array_t *data;
00080 DRMS_Record_t *inTemplate;
00081 TIME t_start, t_stop, t_now, t_want, t_diff, this_t_diff;
00082 int status = 1;
00083 int nrecs, irec;
00084 int nslots, islot;
00085 long long *recnums;
00086 TIME *t_this, half;
00087 TIME cadence;
00088 double *drecnum, *dquality;
00089 int quality;
00090 long long recnum;
00091 char keylist[DRMS_MAXQUERYLEN];
00092 char filename[DRMS_MAXSERIESNAMELEN];
00093 char *tmpdir;
00094 FILE *tmpfile;
00095 char newIn[DRMS_MAXQUERYLEN];
00096 char seriesname[DRMS_MAXQUERYLEN];
00097 char *lbracket;
00098 char *at = index(inQuery, '@');
00099 int npkeys;
00100 char *timekeyname;
00101 double t_step;
00102
00103 strcpy(seriesname, inQuery);
00104 lbracket = index(seriesname,'[');
00105 if (lbracket) *lbracket = '\0';
00106 inTemplate = drms_template_record(drms_env, seriesname, &status);
00107 if (status || !inTemplate) DIE_get_recset("Input series can not be found");
00108
00109
00110 npkeys = inTemplate->seriesinfo->pidx_num;
00111 timekeyname = NULL;
00112 if (npkeys > 0)
00113 {
00114 int i;
00115 for (i=0; i<npkeys; i++)
00116 {
00117 DRMS_Keyword_t *pkey, *skey;
00118 pkey = inTemplate->seriesinfo->pidx_keywords[i];
00119 if (pkey->info->recscope > 1)
00120 pkey = drms_keyword_slotfromindex(pkey);
00121 if (pkey->info->type != DRMS_TYPE_TIME)
00122 continue;
00123 if(i > 0) DIE_get_recset("Input series must have TIME keyword first, for now...");
00124 timekeyname = pkey->info->name;
00125 t_step = drms_keyword_getdouble(drms_keyword_stepfromslot(pkey), &status);
00126 if (status) DIE_get_recset("problem getting t_step");
00127 t_epoch = drms_keyword_getdouble(drms_keyword_epochfromslot(pkey), &status);
00128 if (status) DIE_get_recset("problem getting t_epoch");
00129 }
00130 }
00131 else
00132 DIE_get_recset("Must have time prime key");
00133 epoch = epoch_given ? params_get_time(&cmdparams, "epoch") : t_epoch;
00134
00135 if (at && *at && ((strncmp(inQuery,"aia.lev1[", 9)==0 ||
00136 strncmp(inQuery,"hmi.lev1[", 9)==0 ||
00137 strncmp(inQuery,"aia.lev1_nrt2[",14)==0 ||
00138 strncmp(inQuery,"hmi.lev1_nrt[", 13)==0 ) ||
00139 epoch_given))
00140 {
00141 char *ip=(char *)inQuery, *op=newIn, *p;
00142 long n, mul;
00143 while ( *ip && ip<at )
00144 *op++ = *ip++;
00145 ip++;
00146 n = strtol(ip, &p, 10);
00147 if (*p == 's') mul = 1;
00148 else if (*p == 'm') mul = 60;
00149 else if (*p == 'h') mul = 3600;
00150 else if (*p == 'd') mul = 86400;
00151 else
00152 DIE_get_recset("cant make sense of @xx cadence spec");
00153 cadence = n * mul;
00154 ip = ++p;
00155 while ( *ip )
00156 *op++ = *ip++;
00157 *op = '\0';
00158 half = cadence/2.0;
00159 sprintf(keylist, "%s,QUALITY,recnum", timekeyname);
00160 data = drms_record_getvector(drms_env, newIn, keylist, DRMS_TYPE_DOUBLE, 0, &status);
00161 if (!data || status)
00162 {
00163 fprintf(stderr, "status=%d\n", status);
00164 DIE_get_recset("getkey_vector failed\n");
00165 }
00166 nrecs = data->axis[1];
00167 irec = 0;
00168 t_this = (TIME *)data->data;
00169 dquality = (double *)data->data + 1*nrecs;
00170 drecnum = (double *)data->data + 2*nrecs;
00171 if (epoch_given)
00172 {
00173 int s0 = (t_this[0] - epoch)/cadence;
00174 TIME t0 = s0*cadence + epoch;
00175 t_start = (t0 < t_this[0] ? t0 + cadence : t0);
00176 }
00177 else
00178 t_start = t_this[0];
00179 t_stop = t_this[nrecs-1];
00180 nslots = (t_stop - t_start + cadence/2)/cadence;
00181 recnums = (long long *)malloc(nslots*sizeof(long long));
00182 for (islot=0; islot<nslots; islot++)
00183 recnums[islot] = 0;
00184 islot = 0;
00185 t_want = t_start;
00186 t_diff = 1.0e9;
00187 for (irec = 0; irec<nrecs; irec++)
00188 {
00189 t_now = t_this[irec];
00190 quality = (int)dquality[irec] & 0xFFFFFFFF;
00191 recnum = (long long)drecnum[irec];
00192 this_t_diff = fabs(t_now - t_want);
00193 if (quality < 0)
00194 continue;
00195 if (t_now <= (t_want-half))
00196 continue;
00197 while (t_now > (t_want+half))
00198 {
00199 islot++;
00200 if (islot >= nslots)
00201 break;
00202 t_want = t_start + cadence * islot;
00203 this_t_diff = fabs(t_now - t_want);
00204 t_diff = 1.0e8;
00205 }
00206 if (islot < nslots && this_t_diff <= t_diff)
00207 recnums[islot] = recnum;
00208 t_diff = fabs(t_now - t_want);
00209 }
00210 if (islot+1 < nslots)
00211 nslots = islot+1;
00212 tmpdir = getenv("TMPDIR");
00213 if (!tmpdir) tmpdir = "/tmp";
00214 sprintf(filename, "%s/%sXXXXXX", tmpdir, module_name);
00215 mkstemp(filename);
00216 tmpfile = fopen(filename,"w");
00217 for (islot=0; islot<nslots; islot++)
00218 if (recnums[islot])
00219 fprintf(tmpfile, "%s[:#%lld]\n", seriesname, recnums[islot]);
00220 fclose(tmpfile);
00221 free(recnums);
00222 drms_free_array(data);
00223 sprintf(newInQuery,"@%s", filename);
00224 return(newInQuery);
00225 }
00226 else
00227 return(inQuery);
00228 }