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_V_01h_lev1_8";
00031 char *series = "ds_mdi.fd_V_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_V_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_V_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_V_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,
00223 "ERROR: series %s does not have a keyword named 'T_BEGIN'\n", series);
00224 Mailit(msgtxt);
00225 }
00226 else {
00227 sprintf(msgtxt, "ERROR: drms_setkey_string failed for 'T_BEGIN'\n");
00228 Mailit(msgtxt);
00229 }
00230 return(1);
00231 }
00232 if ((status = drms_setkey_string(rec, "create_date", create_date)))
00233 {
00234 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00235 sprintf(msgtxt, "ERROR: series %s does not have a keyword named 'create_date'\n", series);
00236 Mailit(msgtxt);
00237 }
00238 else {
00239 sprintf(msgtxt, "ERROR: drms_setkey_string failed for 'create_date'\n");
00240 Mailit(msgtxt);
00241 }
00242 return(1);
00243 }
00244 if ((status = drms_setkey_int(rec, "series_num", i)))
00245 {
00246 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00247 sprintf(msgtxt, "ERROR: series %s does not have a keyword named 'series_num'\n", series);
00248 Mailit(msgtxt);
00249 }
00250 else {
00251 sprintf(msgtxt, "ERROR: drms_setkey_string failed for 'series_num'\n");
00252 Mailit(msgtxt);
00253 }
00254 return(1);
00255 }
00256 if ((status = drms_setkey_int(rec, "level_num", level_num)))
00257 {
00258 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00259 sprintf(msgtxt, "WARNING: series %s does not have a keyword named 'level_num'\n", series);
00260 Mailit(msgtxt);
00261 }
00262 else {
00263 sprintf(msgtxt, "WARNING: drms_setkey_string failed for 'level_num'\n");
00264 Mailit(msgtxt);
00265 }
00266 return(1);
00267 }
00268 if ((status = drms_setkey_int(rec, "ds_index", ds_index)))
00269 {
00270 if (status == DRMS_ERROR_UNKNOWNKEYWORD) {
00271 sprintf(msgtxt, "WARNING: series %s does not have a keyword named 'ds_index'\n", series);
00272 Mailit(msgtxt);
00273 }
00274 else {
00275 sprintf(msgtxt, "WARNING: drms_setkey_string failed for 'ds_index'\n");
00276 Mailit(msgtxt);
00277 }
00278 return(1);
00279 }
00280
00281
00282 drms_record_directory(rec, path, 1);
00283 if (! *path)
00284 {
00285 sprintf(msgtxt, "no path found\n");
00286 Mailit(msgtxt);
00287 return(1);
00288 }
00289 sprintf(cmd, "cp -r %s %s", wd, path);
00290 status = system(cmd);
00291 if (status)
00292 {
00293 sprintf(msgtxt, "Copy failed: %s\n", cmd);
00294 Mailit(msgtxt);
00295 return(status);
00296 }
00297 else
00298 printf("%s\n", cmd);
00299 if ((status = drms_close_record(rec, DRMS_INSERT_RECORD))) {
00300 sprintf(msgtxt, "drms_close_record failed!\n");
00301 Mailit(msgtxt);
00302 }
00303
00304 }
00305 return(status);
00306 }