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_fd_M_01h_lev1_8";
00031 char *series = "ds_mdi.fd_M_01h_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_fd_M_01h_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
00070 FILE *logfp, *ffp;
00071 char *series_num, *token, *rsp, *wd;
00072 char *create_date, *t_begin;
00073 char path[DRMS_MAXPATHLEN];
00074 char cmd[DRMS_MAXPATHLEN+1024];
00075 char fname[128], logname[128], line[128];
00076 DRMS_Record_t *rec, *template;
00077 int s_start, s_end, pid, i, level_num, ds_index;
00078
00079
00080 template = drms_template_record(drms_env, series, &status);
00081 if (template==NULL && status == DRMS_ERROR_UNKNOWNSERIES)
00082 {
00083 sprintf(msgtxt, "Series '%s' does not exist. Give up.\n", series);
00084 Mailit(msgtxt);
00085 return(1);
00086 }
00087 else
00088 if(status)
00089 {
00090 sprintf(msgtxt, "DRMS problem looking up series.\n");
00091 Mailit(msgtxt);
00092 return(status);
00093 }
00094
00095 if (nice_intro())
00096 return(0);
00097 series_num = cmdparams_get_str(&cmdparams, "series_num", NULL);
00098
00099 if (strcmp(series_num, NOTSPECIFIED) == 0)
00100 {
00101 sprintf(msgtxt, "'series_num' must be specified. Abort\n");
00102 Mailit(msgtxt);
00103 return(1);
00104 }
00105
00106 if((rsp = index(series_num, ','))) {
00107 sprintf(msgtxt, "No \",\" allowed in series_num=n-m\n");
00108 Mailit(msgtxt);
00109 return(1);
00110 }
00111 if((rsp = index(series_num, '-'))) {
00112 token = (char *)strtok(series_num, "-");
00113 s_start = atoi(token);
00114 token = (char *)strtok(NULL, "-");
00115 s_end = atoi(token);
00116 }
00117 else {
00118 s_start = atoi(series_num);
00119 s_end = s_start;
00120 }
00121 if(s_start == 0 || (s_start > s_end)) {
00122 sprintf(msgtxt, "series_num error\n");
00123 Mailit(msgtxt);
00124 return(1);
00125 }
00126 pid = getpid();
00127 sprintf(logname, "/tmp/peqlog_%d.log", pid);
00128
00129 for(i = s_start; i <= s_end; i++) {
00130 sprintf(cmd,"peq 'prog:mdi,level:lev1.8,series:fd_M_01h[%d]' 1> %s 2>&1",
00131 i, logname);
00132
00133 if(system(cmd)) {
00134 sprintf(msgtxt, "Failed: %s\n", cmd);
00135 Mailit(msgtxt);
00136 return(1);
00137 }
00138 if((logfp=fopen(logname, "r")) == NULL) {
00139 sprintf(msgtxt, "Can't open the log file %s\n", logname);
00140 Mailit(msgtxt);
00141 return(1);
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 sprintf(msgtxt, "The requested dir can not be accessed: %s\n", wd);
00188 Mailit(msgtxt);
00189 return(1);
00190 }
00191
00192 sprintf(fname, "%s/fd_M_01h.%d.overview.fits", wd, i);
00193 if((ffp=fopen(fname, "r")) == NULL) {
00194 fprintf(stderr, "Can't open the file %s\n", fname);
00195 }
00196 else {
00197 while(fgets(line, 128, ffp)) {
00198 token=(char *)strtok(line, " \t\n");
00199 if(strstr(line, "T_START")) {
00200 token=(char *)strtok(NULL, " \t\n");
00201 rsp=(char *)strtok(NULL, " \t\n");
00202 rsp++;
00203 *(rsp + strlen(rsp) -1) = NULL;
00204 t_begin = strdup(rsp);
00205 break;
00206 }
00207 }
00208 }
00209 fclose(ffp);
00210
00211
00212 rec = drms_create_record(drms_env, series, DRMS_PERMANENT, &status);
00213 if (!rec || status)
00214 {
00215 sprintf(msgtxt, "drms_create_record failed, series=%s, status=%d. Aborting.\n", series,status);
00216 Mailit(msgtxt);
00217 return(status);
00218 }
00219 if ((status = drms_setkey_string(rec, "T_BEGIN", t_begin)))
00220 {
00221 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00222 sprintf(msgtxt, "ERROR: series %s does not have a keyword named 'T_BEGIN'\n", series);
00223 Mailit(msgtxt);
00224 }
00225 else {
00226 sprintf(msgtxt, "ERROR: drms_setkey_string failed for 'T_BEGIN'\n");
00227 Mailit(msgtxt);
00228 }
00229 return(1);
00230 }
00231 if ((status = drms_setkey_string(rec, "create_date", create_date)))
00232 {
00233 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00234 sprintf(msgtxt, "ERROR: series %s does not have a keyword named 'create_date'\n", series);
00235 Mailit(msgtxt);
00236 }
00237 else {
00238 sprintf(msgtxt, "ERROR: drms_setkey_string failed for 'create_date'\n");
00239 Mailit(msgtxt);
00240 }
00241 return(1);
00242 }
00243 if ((status = drms_setkey_int(rec, "series_num", i)))
00244 {
00245 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00246 sprintf(msgtxt, "ERROR: series %s does not have a keyword named 'series_num'\n", series);
00247 Mailit(msgtxt);
00248 }
00249 else {
00250 sprintf(msgtxt, "ERROR: drms_setkey_string failed for 'series_num'\n");
00251 Mailit(msgtxt);
00252 }
00253 return(1);
00254 }
00255 if ((status = drms_setkey_int(rec, "level_num", level_num)))
00256 {
00257 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00258 sprintf(msgtxt, "WARNING: series %s does not have a keyword named 'level_num'\n", series);
00259 Mailit(msgtxt);
00260 }
00261 else {
00262 sprintf(msgtxt, "WARNING: drms_setkey_string failed for 'level_num'\n");
00263 Mailit(msgtxt);
00264 }
00265 return(1);
00266 }
00267 if ((status = drms_setkey_int(rec, "ds_index", ds_index)))
00268 {
00269 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00270 sprintf(msgtxt, "WARNING: series %s does not have a keyword named 'ds_index'\n", series);
00271 Mailit(msgtxt);
00272 }
00273 else {
00274 sprintf(msgtxt, "WARNING: drms_setkey_string failed for 'ds_index'\n");
00275 Mailit(msgtxt);
00276 }
00277 return(1);
00278 }
00279
00280
00281 drms_record_directory(rec, path, 1);
00282 if (! *path)
00283 {
00284 sprintf(msgtxt,"no path found\n");
00285 Mailit(msgtxt);
00286 return(1);
00287 }
00288 sprintf(cmd, "cp -r %s %s", wd, path);
00289 status = system(cmd);
00290 if (status)
00291 {
00292 sprintf(msgtxt, "Copy failed: %s\n", cmd);
00293 Mailit(msgtxt);
00294 return(status);
00295 }
00296 else
00297 printf("%s\n", cmd);
00298 if ((status = drms_close_record(rec, DRMS_INSERT_RECORD))) {
00299 sprintf(msgtxt, "drms_close_record failed!\n");
00300 Mailit(msgtxt);
00301 }
00302
00303 }
00304 return(status);
00305 }