00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "jsoc_main.h"
00016 #include "drms.h"
00017 #include "drms_names.h"
00018 #include <unistd.h>
00019 #include <strings.h>
00020
00021 #define NOTSPECIFIED "***NOTSPECIFIED***"
00022
00023 ModuleArgs_t module_args[] = {
00024 {ARG_STRING, "series_num", NOTSPECIFIED, "", ""},
00025 {ARG_FLAG, "h", "0", "", ""},
00026 {ARG_FLAG, "v", "0", "", ""},
00027 {ARG_END}
00028 };
00029
00030 char *module_name = "store_ds_vw_V_06h_lev1_8";
00031 char *series = "ds_mdi.vw_V_06h_lev1_8";
00032
00033
00034 int verbose = 0;
00035 char msgtxt[1024];
00036
00037
00038 int nice_intro()
00039 {
00040 int usage = cmdparams_get_int(&cmdparams, "h", NULL);
00041 verbose = cmdparams_get_int(&cmdparams, "v", NULL);
00042 if (usage)
00043 {
00044 printf("store_ds_vw_V_06h_lev1_8 {-h} {-v} series_num=n-m\n"
00045 " -h: print this message and exit\n"
00046 " -v: verbose\n"
00047 "series_num - series numbers to ingest\n");
00048 return(1);
00049 }
00050 if (verbose)
00051 cmdparams_printall(&cmdparams);
00052 return(0);
00053 }
00054
00055 void Mailit(char *str) {
00056 char mcmd[1096];
00057
00058 printf("%s", str);
00059 sprintf(mcmd, "echo \"%s\" | Mail -s \"store_ds_fd_V_01h_lev1_8\" jim,jeneen,thailand", str);
00060 if(system(mcmd)) {
00061 fprintf(stderr, "Failed: %s\n", mcmd);
00062 }
00063 }
00064
00065
00066 int DoIt(void)
00067 {
00068 int status = 0;
00069 int j = 0;
00070
00071 FILE *logfp;
00072 char *series_num, *token, *rsp, *wd;
00073 char *create_date, *t_begin;
00074 char path[DRMS_MAXPATHLEN];
00075 char cmd[DRMS_MAXPATHLEN+1024];
00076 char fname[128], logname[128], line[128];
00077 DRMS_Record_t *rec, *template;
00078 int s_start, s_end, pid, i, level_num, ds_index;
00079
00080
00081 template = drms_template_record(drms_env, series, &status);
00082 if (template==NULL && status == DRMS_ERROR_UNKNOWNSERIES)
00083 {
00084 sprintf(msgtxt, "Series '%s' does not exist. Give up.\n", series);
00085 Mailit(msgtxt);
00086 return(1);
00087 }
00088 else
00089 if(status)
00090 {
00091 sprintf(msgtxt, "DRMS problem looking up series.\n");
00092 Mailit(msgtxt);
00093 return(status);
00094 }
00095
00096 if (nice_intro())
00097 return(0);
00098 series_num = cmdparams_get_str(&cmdparams, "series_num", NULL);
00099
00100 if (strcmp(series_num, NOTSPECIFIED) == 0)
00101 {
00102 sprintf(msgtxt, "'series_num' must be specified. Abort\n");
00103 Mailit(msgtxt);
00104 return(1);
00105 }
00106
00107 if((rsp = index(series_num, ','))) {
00108 sprintf(msgtxt, "No \",\" allowed in series_num=n-m\n");
00109 Mailit(msgtxt);
00110 return(1);
00111 }
00112 if((rsp = index(series_num, '-'))) {
00113 token = (char *)strtok(series_num, "-");
00114 s_start = atoi(token);
00115 token = (char *)strtok(NULL, "-");
00116 s_end = atoi(token);
00117 }
00118 else {
00119 s_start = atoi(series_num);
00120 s_end = s_start;
00121 }
00122 if(s_start == 0 || (s_start > s_end)) {
00123 sprintf(msgtxt, "series_num error\n");
00124 Mailit(msgtxt);
00125 return(1);
00126 }
00127 pid = getpid();
00128 sprintf(logname, "/tmp/peqlog_%d.log", pid);
00129
00130 for(i = s_start; i <= s_end; i++) {
00131 j++;
00132 sprintf(cmd, "peq 'prog:mdi,level:lev1.8,series:vw_V_06h[%d]' 1> %s 2>&1",
00133 i, logname);
00134
00135 if(system(cmd)) {
00136 fprintf(stderr, "Failed: %s\n", cmd);
00137 return(0);
00138 }
00139 if((logfp=fopen(logname, "r")) == NULL) {
00140 fprintf(stderr, "Can't open the log file %s\n", logname);
00141 return(0);
00142 }
00143 while(fgets(line, 128, logfp)) {
00144 token=(char *)strtok(line, " \t\n");
00145 if(strstr(line, "in_0_level_sn:")) {
00146 token=(char *)strtok(NULL, " \t\n");
00147 level_num=atoi((char *)strtok(NULL, " \t\n"));
00148 continue;
00149 }
00150 if(strstr(line, "in_0_ds_index:")) {
00151 token=(char *)strtok(NULL, " \t\n");
00152 ds_index=atoi((char *)strtok(NULL, " \t\n"));
00153 continue;
00154 }
00155 if(strstr(line, "in_0_creat_date:")) {
00156 token=(char *)strtok(NULL, " \t\n");
00157 rsp=(char *)strtok(NULL, " \t\n");
00158 create_date = strdup(rsp);
00159 continue;
00160 }
00161 if(strstr(line, "t_first:")) {
00162 token=(char *)strtok(NULL, " \t\n");
00163 rsp=(char *)strtok(NULL, " \t\n");
00164 t_begin = strdup(rsp);
00165 continue;
00166 }
00167 if(!strstr(line, "in_0_wd:")) continue;
00168 token=(char *)strtok(NULL, " \t\n");
00169 rsp=(char *)strtok(NULL, " \t\n");
00170 wd = strdup(rsp);
00171 if(!strcmp(wd, ".")) wd = NULL;
00172 }
00173 fclose(logfp);
00174 if(wd) {
00175
00176
00177
00178
00179 }
00180 else {
00181 printf("No wd for series# %d\n", i);
00182 continue;
00183 }
00184
00185 if (access(wd, R_OK) != 0)
00186 {
00187 printf("The requested dir can not be accessed. %s\n", wd);
00188 return(0);
00189 }
00190
00191 sprintf(fname, "%s/vw_V_06h.%d.overview.fits", wd, i);
00192 if((logfp=fopen(fname, "r")) == NULL) {
00193 fprintf(stderr, "Can't open the file %s\n", fname);
00194 }
00195 else {
00196 while(fgets(line, 128, logfp)) {
00197 token=(char *)strtok(line, " \t\n");
00198 if(strstr(line, "T_START")) {
00199 token=(char *)strtok(NULL, " \t\n");
00200 rsp=(char *)strtok(NULL, " \t\n");
00201 rsp++;
00202 *(rsp + strlen(rsp) -1) = NULL;
00203 t_begin = strdup(rsp);
00204 break;
00205 }
00206 }
00207 }
00208 fclose(logfp);
00209
00210
00211 rec = drms_create_record(drms_env, series, DRMS_PERMANENT, &status);
00212 if (!rec || status)
00213 {
00214 sprintf(msgtxt, "drms_create_record failed, series=%s, status=%d. Aborting.\n", series,status);
00215 Mailit(msgtxt);
00216 return(status);
00217 }
00218 if ((status = drms_setkey_string(rec, "T_BEGIN", t_begin)))
00219 {
00220 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00221 sprintf(msgtxt, "ERROR: series %s does not have a keyword named 'T_BEGIN'\n", series);
00222 Mailit(msgtxt);
00223 }
00224 else {
00225 sprintf(msgtxt, "ERROR: drms_setkey_string failed for 'T_BEGIN'\n");
00226 Mailit(msgtxt);
00227 }
00228 return(1);
00229 }
00230 if ((status = drms_setkey_string(rec, "create_date", create_date)))
00231 {
00232 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00233 sprintf(msgtxt, "ERROR: series %s does not have a keyword named 'create_date'\n", series);
00234 Mailit(msgtxt);
00235 }
00236 else {
00237 sprintf(msgtxt, "ERROR: drms_setkey_string failed for 'create_date'\n");
00238 Mailit(msgtxt);
00239 }
00240 return(1);
00241 }
00242 if ((status = drms_setkey_int(rec, "series_num", i)))
00243 {
00244 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00245 sprintf(msgtxt, "ERROR: series %s does not have a keyword named 'series_num'\n", series);
00246 Mailit(msgtxt);
00247 }
00248 else {
00249 sprintf(msgtxt, "ERROR: drms_setkey_string failed for 'series_num'\n");
00250 Mailit(msgtxt);
00251 }
00252 return(1);
00253 }
00254 if ((status = drms_setkey_int(rec, "level_num", level_num)))
00255 {
00256 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00257 sprintf(msgtxt, "WARNING: series %s does not have a keyword named 'level_num'\n", series);
00258 Mailit(msgtxt);
00259 }
00260 else {
00261 sprintf(msgtxt, "WARNING: drms_setkey_string failed for 'level_num'\n");
00262 Mailit(msgtxt);
00263 }
00264 return(1);
00265 }
00266 if ((status = drms_setkey_int(rec, "ds_index", ds_index)))
00267 {
00268 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00269 sprintf(msgtxt, "WARNING: series %s does not have a keyword named 'ds_index'\n", series);
00270 Mailit(msgtxt);
00271 }
00272 else {
00273 sprintf(msgtxt, "WARNING: drms_setkey_string failed for 'ds_index'\n");
00274 Mailit(msgtxt);
00275 }
00276 return(1);
00277 }
00278
00279
00280 drms_record_directory(rec, path, 1);
00281 if (! *path)
00282 {
00283 sprintf(msgtxt,"no path found\n");
00284 Mailit(msgtxt);
00285 return(1);
00286 }
00287 sprintf(cmd, "cp -r %s %s", wd, path);
00288 status = system(cmd);
00289 if (status)
00290 {
00291 sprintf(msgtxt, "Copy failed: %s\n", cmd);
00292 Mailit(msgtxt);
00293 return(status);
00294 }
00295 else
00296 printf("%s\n", cmd);
00297
00298
00299
00300 if ((status = drms_close_record(rec, DRMS_INSERT_RECORD))) {
00301 sprintf(msgtxt, "drms_close_record failed!\n");
00302 Mailit(msgtxt);
00303 }
00304 }
00305 return(status);
00306 }