00001
00063 #include "jsoc.h"
00064 #include "jsoc_main.h"
00065 #include <unistd.h>
00066
00067 static char x2c (char *what)
00068 {
00069 char digit;
00070 digit = (char)(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
00071 digit *= 16;
00072 digit = (char)(digit + (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0')));
00073 return (digit);
00074 }
00075
00076 static void CGI_unescape_url (char *url)
00077 {
00078 int x, y;
00079 for (x = 0, y = 0; url[y]; ++x, ++y)
00080 {
00081 if ((url[x] = url[y]) == '%')
00082 {
00083 url[x] = x2c (&url[y+1]);
00084 y += 2;
00085 }
00086 }
00087 url[x] = '\0';
00088 }
00089
00090 char *module_name = "jsoc_getJP2images";
00091
00092 #define DIE(msg) {printf("%s\n",(json ? "{\"status\":-1}" : ""));\
00093 fflush(stdout);\
00094 fprintf(stderr,"%s, status=%d\n",msg,status);\
00095 return(-1);}
00096
00097 ModuleArgs_t module_args[] =
00098 {
00099 {ARG_STRING, "times", "NOT SPECIFIED", "AIA times desired."},
00100 {ARG_INT, "wave", "0", "AIA wavelength in Angstroms"},
00101 {ARG_STRING, "cadence", "0", "AIA cadence spec, e.g. '15m' or '60s' style"},
00102 {ARG_STRING, "QUERY_STRING", "NOT SPECIFIED", "Params from web GET"},
00103 {ARG_FLAG, "j", "0", "Generate json formatted output, plain ascii otherwise"},
00104 {ARG_FLAG, "t", "0", "Generate HMI timetag nearest to each aia image found"},
00105 {ARG_END}
00106 };
00107
00108 int DoIt(void)
00109 {
00110 int status = DRMS_SUCCESS;
00111 DRMS_RecordSet_t *inRS;
00112 DRMS_Record_t *inRec;
00113 char date__obs[MAXSTR];
00114 char wave[MAXSTR];
00115 char HMItag[MAXSTR];
00116 char fname[MAXSTR];
00117 char timetag[MAXSTR];
00118 char path[MAXSTR];
00119 char testpath[MAXSTR];
00120 char showpath[MAXSTR];
00121 int year, month, day, hour, minute, sec, fsec;
00122 int irec, nrecs, ngood;
00123 int from_web;
00124 char *web_query;
00125 char *cadence, *p;
00126 char *in_filename = NULL;
00127 int json, wantHMItime;
00128 const char *inQuery;
00129 char Query[1024];
00130 int iwave;
00131 int step, jp2step;
00132 int mul;
00133 int rec_step;
00134
00135 web_query = strdup (cmdparams_get_str (&cmdparams, "QUERY_STRING", NULL));
00136 from_web = strcmp (web_query, "NOT SPECIFIED") != 0;
00137
00138 if (from_web)
00139 {
00140
00141 char *getstring, *p;
00142 CGI_unescape_url(web_query);
00143 getstring = strdup (web_query);
00144 for (p=strtok(getstring,"&"); p; p=strtok(NULL, "&"))
00145 {
00146 char *key=p, *val=index(p,'=');
00147 if (!val)
00148 {
00149 json = 1;
00150 DIE("Bad QUERY_STRING");
00151 }
00152 *val++ = '\0';
00153 cmdparams_set(&cmdparams, key, val);
00154 }
00155 free(getstring);
00156 }
00157
00158 iwave = params_get_int(&cmdparams, "wave");
00159 json = params_get_int(&cmdparams, "j");
00160 wantHMItime = params_get_int(&cmdparams, "t");
00161 inQuery = (char *)params_get_str(&cmdparams, "times");
00162 cadence = (char *)params_get_str(&cmdparams, "cadence");
00163 int n = strtol(cadence, &p, 10);
00164 if (*p == 's') mul = 1;
00165 else if (*p == 'm') mul = 60;
00166 else if (*p == 'h') mul = 3600;
00167 else if (*p == 'd') mul = 86400;
00168 n *= mul;
00169
00170
00171
00172
00173 step = (iwave == 1600 || iwave == 1700 ? 24: (iwave == 4500 ? 3600 : 12));
00174
00175 jp2step = (iwave == 1600 || iwave == 1700 ? 2: (iwave == 4500 ? 1 : 3));
00176 rec_step = n/(step);
00177
00178
00179 if (json)
00180 {
00181 printf("Content-type: application/json\n\n");
00182 }
00183 else
00184 {
00185 printf("Content-type: text/plain\n\n");
00186 }
00187
00188 char *at = index(inQuery, '@');
00189 if (at)
00190 DIE("Use cadence param instead of @ syntax");
00191 sprintf(Query,"aia.lev1_nrt2[%s][?WAVELNTH=%d?]", inQuery, iwave);
00192 fprintf(stderr,"query=%s\n",Query);
00193 inRS = drms_open_records(drms_env, Query, &status);
00194 if (status || inRS->n == 0)
00195 DIE("No input data found");
00196
00197 nrecs = inRS->n;
00198
00199 if (wave == 0)
00200 DIE("The wave parameter must be provided");
00201
00202 if (json)
00203 {
00204 printf("{\"images\":[\n");
00205 }
00206
00207 ngood = 0;
00208 irec = 0;
00209
00210 int trysteps[] = {0, -1, 1, -2, 2, -3, 3};
00211 int try;
00212 int found = 0;
00213 while (irec < nrecs)
00214 {
00215 for (try=0; try<7; try++)
00216 {
00217 int tryrec;
00218 tryrec = irec + trysteps[try];
00219 if (tryrec < 0) tryrec = 0;
00220 if (tryrec >= nrecs) tryrec = nrecs - 1;
00221 inRec = inRS->records[tryrec];
00222 strncpy(date__obs, drms_getkey_string(inRec, "DATE__OBS", NULL), MAXSTR);
00223 strncpy(wave, drms_getkey_string(inRec, "WAVELNTH", NULL), MAXSTR);
00224 sscanf(date__obs, "%4d-%2d-%2dT%2d:%2d:%2d.%2d", &year,&month,&day,&hour,&minute,&sec,&fsec);
00225 sprintf(fname, "%4d_%02d_%02d__%02d_%02d_%02d_%02d__SDO_AIA_AIA_%s.jp2",
00226 year,month,day,hour,minute,sec,fsec,wave);
00227 sprintf(path, "data/aia/images/%4d/%02d/%02d/%s/%s", year, month, day, wave, fname);
00228 sprintf(testpath, "/web/jsoc/htdocs/%s", path);
00229 if (access(testpath, R_OK) == 0)
00230 {
00231 found = 1;
00232 break;
00233 }
00234 }
00235 if (found)
00236 {
00237 if (wantHMItime)
00238 {
00239 TIME t_rec = drms_getkey_time(inRec, "T_REC", NULL);
00240 t_rec = (t_rec + 450.0) / 900.0;
00241 int slot = floor(t_rec);
00242 t_rec = 900.0 * slot;
00243 char HMIt_rec[MAXSTR];
00244 sprint_time(HMIt_rec, t_rec, "TAI", 0);
00245 int y,m,d,h,M,s;
00246 sscanf(HMIt_rec, "%4d.%02d.%02d_%02d:%02d:%02d",&y,&m,&d,&h,&M,&s);
00247 sprintf(HMItag, "%4d%02d%02d_%02d%02d%02d",y,m,d,h,M,s);
00248 }
00249 sprintf(timetag, "%4d%02d%02d_%02d%02d%02d", year,month,day,hour,minute,sec);
00250 sprintf(showpath, "http://jsoc.stanford.edu/%s", path);
00251 if (json)
00252 {
00253 if (wantHMItime)
00254 printf("%s{\"time\":\"%s\",\"wave\":\"%s\",\"url\":\"%s\",\"HMItag\":\"%s\"}\n",
00255 (ngood ? "," : " "), timetag, wave, showpath, HMItag);
00256 else
00257 printf("%s{\"time\":\"%s\",\"wave\":\"%s\",\"url\":\"%s\"}\n",
00258 (ngood ? "," : " "), timetag, wave, showpath);
00259 }
00260 else
00261 {
00262 printf("%s\n",testpath);
00263 }
00264 ngood++;
00265 }
00266 irec += rec_step;
00267 }
00268
00269 if (json)
00270 {
00271 printf("], \"count\":%d, \"status\":0}\n", ngood);
00272 }
00273 else
00274 {
00275 }
00276 drms_close_records(inRS, DRMS_FREE_RECORD);
00277 return (DRMS_SUCCESS);
00278 }
00279