ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/JSOC/proj/dsdsmigr/apps/store_ds_fd_M_01h_lev1_8.c
Revision: 1.1
Committed: Tue Oct 16 22:48:16 2007 UTC (15 years, 11 months ago) by arta
Content type: text/plain
Branch: MAIN
CVS Tags: Ver_4-0, NewTree01_cp07_JSOC, NewTree01_cp05_JSOC, NewTree01_cp06_JSOC
Log Message:
Move JSOC/src/base to JSOC/base and JSOC/src/proj to JSOC/proj.  86 JSOC/src.

File Contents

# User Rev Content
1 arta 1.1 /* store_ds_fd_M_01h_lev1_8.c
2     * stores mdi fd_M_01h_lev1_8 ds in DRMS/SUMS.
3     */
4     /* Usage: store_ds_fd_M_01h_lev1_8 series_num=n-m
5     *
6     * Store MDI datasets into DRMS/SUMS.
7     *
8     * Queries the mdi dsds to get the location of the given series numbers
9     * and copies the dirs into the SUMS and makes the DRMS keywords according
10     * to the su_ds.fd_M_01h_lev1_8.jsd where:
11     * Index: series_num
12     *
13     */
14    
15     #include "jsoc_main.h"
16     #include "drms.h"
17     #include "drms_names.h"
18     #include <unistd.h>
19     #include <strings.h>
20    
21     #define NOTSPECIFIED "***NOTSPECIFIED***"
22     /* List of default parameter values */
23     ModuleArgs_t module_args[] = {
24     {ARG_STRING, "series_num", NOTSPECIFIED, "", ""}, /* series #s to store */
25     {ARG_FLAG, "h", "0", "", ""}, /* print usage message and quit */
26     {ARG_FLAG, "v", "0", "", ""}, /* verbose flag, normally do not use */
27     {ARG_END}
28     };
29     /* Module name presented to DRMS */
30     char *module_name = "store_ds_fd_M_01h_lev1_8";
31     char *series = "ds_mdi.fd_M_01h_lev1_8"; /* our series name */
32    
33     /* Some global variables for this module. */
34     int verbose = 0;
35    
36     /* Check DRMS session status and calling arguments and set verbose variable */
37     int nice_intro()
38     {
39     int usage = cmdparams_get_int(&cmdparams, "h", NULL);
40     verbose = cmdparams_get_int(&cmdparams, "v", NULL);
41     if (usage)
42     {
43     printf("store_ds_fd_M_01h_lev1_8 {-h} {-v} series_num=n-m\n"
44     " -h: print this message and exit\n"
45     " -v: verbose\n"
46     "series_num - series numbers to ingest\n");
47     return(1);
48     }
49     if (verbose)
50     cmdparams_printall(&cmdparams);
51     return(0);
52     }
53    
54     /* Module main function. */
55     int DoIt(void)
56     {
57     int status = 0;
58    
59     FILE *logfp, *ffp;
60     char *series_num, *token, *rsp, *wd;
61     char *create_date, *t_begin;
62     char path[DRMS_MAXPATHLEN];
63     char cmd[DRMS_MAXPATHLEN+1024];
64     char fname[128], logname[128], line[128];
65     DRMS_Record_t *rec, *template;
66     int s_start, s_end, pid, i, level_num, ds_index;
67    
68     /* Series must already exist */
69     template = drms_template_record(drms_env, series, &status);
70     if (template==NULL && status == DRMS_ERROR_UNKNOWNSERIES)
71     {
72     printf("Series '%s' does not exist. Give up.\n", series);
73     return(1);
74     }
75     else
76     if(status)
77     {
78     fprintf(stderr, "DRMS problem looking up series.\n");
79     return(status);
80     }
81    
82     if (nice_intro())
83     return(0);
84     series_num = cmdparams_get_str(&cmdparams, "series_num", NULL);
85    
86     if (strcmp(series_num, NOTSPECIFIED) == 0)
87     {
88     fprintf(stderr, "'series_num' must be specified. Abort\n");
89     return(1);
90     }
91    
92     if((rsp = index(series_num, ','))) {
93     fprintf(stderr, "No \",\" allowed in series_num=n-m\n");
94     return(1);
95     }
96     if((rsp = index(series_num, '-'))) {
97     token = (char *)strtok(series_num, "-");
98     s_start = atoi(token);
99     token = (char *)strtok(NULL, "-");
100     s_end = atoi(token);
101     }
102     else {
103     s_start = atoi(series_num);
104     s_end = s_start;
105     }
106     if(s_start == 0 || (s_start > s_end)) {
107     fprintf(stderr, "series_num error\n");
108     return(1);
109     }
110     pid = getpid();
111     sprintf(logname, "/tmp/peqlog_%d.log", pid);
112    
113     for(i = s_start; i <= s_end; i++) {
114     sprintf(cmd,"peq 'prog:mdi,level:lev1.8,series:fd_M_01h[%d]' 1> %s 2>&1",
115     i, logname);
116     /*printf("About to execute:\n %s\n", cmd);*/
117     if(system(cmd)) {
118     fprintf(stderr, "Failed: %s\n", cmd);
119     return(1);
120     }
121     if((logfp=fopen(logname, "r")) == NULL) {
122     fprintf(stderr, "Can't open the log file %s\n", logname);
123     return(1);
124     }
125     while(fgets(line, 128, logfp)) { /* get a log file line */
126     token=(char *)strtok(line, " \t\n");
127     if(strstr(line, "in_0_level_sn:")) {
128     token=(char *)strtok(NULL, " \t\n");
129     level_num=atoi((char *)strtok(NULL, " \t\n"));
130     continue;
131     }
132     if(strstr(line, "in_0_ds_index:")) {
133     token=(char *)strtok(NULL, " \t\n");
134     ds_index=atoi((char *)strtok(NULL, " \t\n"));
135     continue;
136     }
137     if(strstr(line, "in_0_creat_date:")) {
138     token=(char *)strtok(NULL, " \t\n");
139     rsp=(char *)strtok(NULL, " \t\n");
140     create_date = strdup(rsp);
141     continue;
142     }
143     if(strstr(line, "t_first:")) { /* use t_first as t_begin */
144     token=(char *)strtok(NULL, " \t\n");
145     rsp=(char *)strtok(NULL, " \t\n");
146     t_begin = strdup(rsp);
147     continue;
148     }
149     if(!strstr(line, "in_0_wd:")) continue;
150     token=(char *)strtok(NULL, " \t\n");
151     rsp=(char *)strtok(NULL, " \t\n"); /* this is the wd */
152     wd = strdup(rsp);
153     if(!strcmp(wd, ".")) wd = NULL;
154     }
155     fclose(logfp);
156     if(wd) {
157     /*printf("wd for series# %d: %s\n", i, wd); /* !!TEMP */
158     /*printf("level_num=%d ds_index=%d series_num=%d create_date=%s\n",
159     level_num, ds_index, i, create_date);
160     */
161     }
162     else {
163     printf("No wd for series# %d\n", i);
164     continue;
165     }
166     /* First, check to see that the dir exists */
167     if (access(wd, R_OK) != 0)
168     {
169     printf("The requested dir can not be accessed: %s\n", wd);
170     return(1);
171     }
172     /* See if any overview.fits file to get T_START from */
173     sprintf(fname, "%s/fd_M_01h.%d.overview.fits", wd, i);
174     if((ffp=fopen(fname, "r")) == NULL) {
175     fprintf(stderr, "Can't open the file %s\n", fname);
176     }
177     else {
178     while(fgets(line, 128, ffp)) { /* get an overview.fits line */
179     token=(char *)strtok(line, " \t\n");
180     if(strstr(line, "T_START")) { /* use t_start as t_begin */
181     token=(char *)strtok(NULL, " \t\n");
182     rsp=(char *)strtok(NULL, " \t\n");
183     rsp++; /* elim start tick mark */
184     *(rsp + strlen(rsp) -1) = NULL; /* elim end tick */
185     t_begin = strdup(rsp);
186     break;
187     }
188     }
189     }
190     fclose(ffp);
191    
192     /* Now ready to make a new record and set keywords */
193     rec = drms_create_record(drms_env, series, DRMS_PERMANENT, &status);
194     if (!rec || status)
195     {
196     printf("drms_create_record failed, series=%s, status=%d. Aborting.\n", series,status);
197     return(status);
198     }
199     if ((status = drms_setkey_string(rec, "T_BEGIN", t_begin)))
200     {
201     if (status == DRMS_ERROR_UNKNOWNKEYWORD)
202     printf("ERROR: series %s does not have a keyword named 'T_BEGIN'\n", series);
203     else
204     printf("ERROR: drms_setkey_string failed for 'T_BEGIN'\n");
205     return(1);
206     }
207     if ((status = drms_setkey_string(rec, "create_date", create_date)))
208     {
209     if (status == DRMS_ERROR_UNKNOWNKEYWORD)
210     printf("ERROR: series %s does not have a keyword named 'create_date'\n", series);
211     else
212     printf("ERROR: drms_setkey_string failed for 'create_date'\n");
213     return(1);
214     }
215     if ((status = drms_setkey_int(rec, "series_num", i)))
216     {
217     if (status == DRMS_ERROR_UNKNOWNKEYWORD)
218     printf("ERROR: series %s does not have a keyword named 'series_num'\n", series);
219     else
220     printf("ERROR: drms_setkey_string failed for 'series_num'\n");
221     return(1);
222     }
223     if ((status = drms_setkey_int(rec, "level_num", level_num)))
224     {
225     if (status == DRMS_ERROR_UNKNOWNKEYWORD)
226     printf("WARNING: series %s does not have a keyword named 'level_num'\n", series);
227     else
228     printf("WARNING: drms_setkey_string failed for 'level_num'\n");
229     return(1);
230     }
231     if ((status = drms_setkey_int(rec, "ds_index", ds_index)))
232     {
233     if (status == DRMS_ERROR_UNKNOWNKEYWORD)
234     printf("WARNING: series %s does not have a keyword named 'ds_index'\n", series);
235     else
236     printf("WARNING: drms_setkey_string failed for 'ds_index'\n");
237     return(1);
238     }
239    
240     /* a SU should have been allocated since there is a segment in the series defn */
241     drms_record_directory(rec, path, 1);
242     if (! *path)
243     {
244     fprintf(stderr,"no path found\n");
245     return(1);
246     }
247     sprintf(cmd, "cp -r %s %s", wd, path);
248     status = system(cmd);
249     if (status)
250     {
251     printf("Copy failed: %s\n", cmd);
252     return(status);
253     }
254     else
255     printf("%s\n", cmd);
256     if ((status = drms_close_record(rec, DRMS_INSERT_RECORD)))
257     printf("drms_close_record failed!\n");
258    
259     }
260     return(status);
261     }