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_96m_01d_lev1_8";
00031 char *series = "ds_mdi.fd_M_96m_01d_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_96m_01d_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,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 fprintf(stderr, "'series_num' must be specified. Abort\n");
00102 return(1);
00103 }
00104
00105 if((rsp = index(series_num, ','))) {
00106 sprintf(msgtxt, "No \",\" allowed in series_num=n-m\n");
00107 Mailit(msgtxt);
00108 return(1);
00109 }
00110 if((rsp = index(series_num, '-'))) {
00111 token = (char *)strtok(series_num, "-");
00112 s_start = atoi(token);
00113 token = (char *)strtok(NULL, "-");
00114 s_end = atoi(token);
00115 }
00116 else {
00117 s_start = atoi(series_num);
00118 s_end = s_start;
00119 }
00120 if(s_start == 0 || (s_start > s_end)) {
00121 sprintf(msgtxt, "series_num error\n");
00122 Mailit(msgtxt);
00123 return(1);
00124 }
00125 pid = getpid();
00126 sprintf(logname, "/tmp/peqlog_%d.log", pid);
00127
00128 for(i = s_start; i <= s_end; i++) {
00129 sprintf(cmd,"peq 'prog:mdi,level:lev1.8,series:fd_M_96m_01d[%d]' 1> %s 2>&1",
00130 i, logname);
00131
00132 if(system(cmd)) {
00133 sprintf(msgtxt, "Failed: %s\n", cmd);
00134 Mailit(msgtxt);
00135 return(1);
00136 }
00137 if((logfp=fopen(logname, "r")) == NULL) {
00138 sprintf(msgtxt, "Can't open the log file %s\n", logname);
00139 Mailit(msgtxt);
00140 return(1);
00141 }
00142 while(fgets(line, 128, logfp)) {
00143 token=(char *)strtok(line, " \t\n");
00144 if(strstr(line, "in_0_level_sn:")) {
00145 token=(char *)strtok(NULL, " \t\n");
00146 level_num=atoi((char *)strtok(NULL, " \t\n"));
00147 continue;
00148 }
00149 if(strstr(line, "in_0_ds_index:")) {
00150 token=(char *)strtok(NULL, " \t\n");
00151 ds_index=atoi((char *)strtok(NULL, " \t\n"));
00152 continue;
00153 }
00154 if(strstr(line, "in_0_creat_date:")) {
00155 token=(char *)strtok(NULL, " \t\n");
00156 rsp=(char *)strtok(NULL, " \t\n");
00157 create_date = strdup(rsp);
00158 continue;
00159 }
00160 if(strstr(line, "t_first:")) {
00161 token=(char *)strtok(NULL, " \t\n");
00162 rsp=(char *)strtok(NULL, " \t\n");
00163 t_begin = strdup(rsp);
00164 continue;
00165 }
00166 if(!strstr(line, "in_0_wd:")) continue;
00167 token=(char *)strtok(NULL, " \t\n");
00168 rsp=(char *)strtok(NULL, " \t\n");
00169 wd = strdup(rsp);
00170 if(!strcmp(wd, ".")) wd = NULL;
00171 }
00172 fclose(logfp);
00173 if(wd) {
00174
00175
00176
00177
00178 }
00179 else {
00180 printf("No wd for series# %d\n", i);
00181 continue;
00182 }
00183
00184 if (access(wd, R_OK) != 0)
00185 {
00186 sprintf(msgtxt, "The requested dir can not be accessed: %s\n", wd);
00187 Mailit(msgtxt);
00188 return(1);
00189 }
00190
00191 sprintf(fname, "%s/fd_M_96m_01d.%d.overview.fits", wd, i);
00192 if((ffp=fopen(fname, "r")) == NULL) {
00193 fprintf(stderr, "Can't open the file %s\n", fname);
00194 }
00195 else {
00196 while(fgets(line, 128, ffp)) {
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(ffp);
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("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("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; /bin/rm -f /mag/fd_M_96m_01d.%06d; ln -s %s/fd_M_96m_01d.%06d /mag/fd_M_96m_01d.%06d",
00288 wd, path, i, path, i, i);
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 return(status);
00304 }