📄 podjustdoit.c
字号:
/* ================================================================*//* DMC Interim out | podjustdoit.c | Main *//*=================================================================*//*Name: podjustdoit.c Purpose:to find the next continuous series of data records each of which lies in whole or in part within the given time spanUsage: int podjustdoit()Inputs: none.Output: struct input_scan *scan ptr->data hdr of found time segment int *start_o ptr->fseek offset to start of data int *end_o ptr->fseek offset to end of data + 1 int find_segment ()=0 time segment found =1 last time segment found =2 eof detected before time segment found =3 read errorMessages: Warnings: Errors: Fatals: Called by: /*=====================================*//*=================| |=================*/ /*=====================================*/#include <string.h>#include <stdio.h>#include <time.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/param.h>#include <dirent.h>#include "output.h"#define BOOL int#define DIM(thing) (sizeof(thing)/sizeof(thing[0]))#define ELEMENT_DIM(thing) (sizeof(thing)/DIM(thing))#define TRIM_STR(s)\{\ char *ch_ptr;\ while((ch_ptr = strchr(s, ' ')) != NULL)\ *ch_ptr = 0;\}/* ------------------------------------------------------------------ */extern struct path_list *Data_path;extern struct DMC_request request; /* current request */extern int Write_Data; static struct input_scan Scan; /* hdr info for current time segment */static struct input_time Start_volume;static struct input_time End;int podjustdoit(ofile, volume_break, tspan_break)FILE *ofile;struct input_time volume_break;struct input_time tspan_break;{ struct input_time end_volume; struct input_time end_tspan; struct path_list *next_p; int err, nlvol; nlvol = 0; Start_volume.year = 9999; Start_volume.day = 999; Start_volume.hour = 0; Start_volume.minute = 0; Start_volume.second = 0; Start_volume.fracsec = 0; memset(&Scan, 0, sizeof(struct input_time)); Scan.time.year = 9999; Scan.time.day = 366; Scan.time.hour = 23; memset(&End, 0, sizeof(struct input_time)); switch (Write_Data) { case 1 : next_p = Data_path; do { nlvol += scan_dir(next_p->path); next_p = next_p->next; } while (next_p != Data_path); break; case 0 : /* DATALESS */ nlvol += scan_header_dir(Header_path); Tspan_head->start=Lvol_head; Tspan_head->count=1; break; } /* all cnvt does is make sure * year is 4 digits */ Start_volume.year = cnvt_year(Start_volume.year); end_volume = add_inputtime(Start_volume, volume_break); end_tspan = add_inputtime(Start_volume, tspan_break); err = flush_output_volume(ofile, nlvol, 1, volume_break, tspan_break, Start_volume, end_volume);}/* --------------------------------------------------------------------- */int scan_header_dir(char *path){ DIR *dirp; struct dirent *direntp; char full_name[MAXPATHLEN]; int n, count, nlvol; dirp = opendir(path); nlvol = 0; if (dirp == NULL) { return 0; } while ((direntp = readdir(dirp)) != NULL) { /* filter out the "." and the ".."'s */ if ((strcmp(direntp->d_name, ".") == 0) || (strcmp(direntp->d_name, "..") == 0)) continue; sprintf(full_name, "%s/%s", path, direntp->d_name); /* check for sub directory */ if (is_dir(path, direntp->d_name)) { nlvol += scan_header_dir(full_name); continue; } /* build a dummy request entry based on * info as found in the B050 and B052 file. */ if (strcmp(direntp->d_name, "B050") == 0) { if (scan_B050(full_name) == -1) /* error cond */ { fprintf(stderr, "Path: %s\n", path); } } if (strcmp(direntp->d_name, "B052") == 0) { if (scan_B052(full_name) == -1) { fprintf(stderr, "Path: %s\n", path); continue; } if (put_lvol(&Lvol_head, &Lvol_tail, &Scan, "", &End, 0, 0) == 0) { nlvol += 1; } } } closedir(dirp); return nlvol;}/* --------------------------------------------------------------------- */int scan_B052(char *b052_name){ FILE *fptr; char buffer[9999]; /* max buff size for 4 chars */ fptr = fopen(b052_name, "r"); if (fptr == NULL) { fprintf(stderr, "Unable to open B052 file\n"); perror("scan_B052"); return -1; } while (fgets(buffer, sizeof(buffer), fptr)) { buffer[strlen(buffer) - 1] = 0; /* eliminate the \n */ if (strncmp(&buffer[0], "052", 3) != 0) continue; memcpy(Scan.location, &buffer[7], 2); memcpy(Scan.channel, &buffer[9], 3); fclose(fptr); return 1; /* we are finished */ } fprintf(stderr, "Warning: failed to scan blk 52 from B052 file!!\n"); fclose(fptr); return -1;}/* --------------------------------------------------------------------- */int scan_B050(char *b050_name){ FILE *fptr; char buffer[9999]; /* max buff size for 4 chars */ int n; char **parts; struct input_time t; fptr = fopen(b050_name, "r"); if (fptr == NULL) { fprintf(stderr, "Unable to open B050 file\n"); perror("scan_B050"); return -1; } while (fgets(buffer, sizeof(buffer), fptr)) { buffer[strlen(buffer) - 1] = 0; /* eliminate the \n */ if (strncmp(&buffer[0], "050", 3) != 0) continue; if (strncmp(&buffer[7], Scan.station, 5) != 0) { memset(&Scan, 0, sizeof(struct input_time)); Scan.time.year = 9999; Scan.time.day = 366; Scan.time.hour = 23; memset(&End, 0, sizeof(struct input_time)); } n = split(buffer, &parts, '~'); memcpy(Scan.station, &parts[0][7], sizeof(Scan.station)); memcpy(Scan.network, &parts[3][1], sizeof(Scan.network)); /* parse the start time */ t = asc_to_input_time(&parts[1][9]); if (cmp_time(t, Scan.time, 0) < 0) { Scan.time = t; if (cmp_time(Scan.time, Start_volume, 0) < 0) Start_volume = Scan.time; } /* parse the end time */ t = asc_to_input_time(&parts[2][0]); /* check for year == 0 indicating no endtime */ if (t.year == 0) t.year = 9999; if (cmp_time(t, End, 0) > 0) { End = t; } fuse(&parts, n); } fclose(fptr); return 1;}/* --------------------------------------------------------------------- */int scan_dir(path)char *path;{ DIR *dirp; struct dirent *direntp; struct input_time end_tspan; char full_name[MAXPATHLEN]; char **array; int year, day; struct lvol *first; int n, count, nlvol; end_tspan.year = 9999; end_tspan.day = 365; end_tspan.hour = 0; end_tspan.minute = 0; end_tspan.second = 0; end_tspan.fracsec = 0; dirp = opendir(path); nlvol = 0; if (dirp == NULL) { return 0; } while ((direntp = readdir(dirp)) != NULL) { /* filter out the "." and the ".."'s */ if ((strcmp(direntp->d_name, ".") == 0) || (strcmp(direntp->d_name, "..") == 0)) continue; sprintf(full_name, "%s/%s", path, direntp->d_name); /* check for sub directory */ if (is_dir(path, direntp->d_name)) { nlvol += scan_dir(full_name); continue; } /* build a dummy request entry based on file name * info. Make request for the whole day */ /* 1st determine if location code is in file name */ n = split(direntp->d_name, &array, '.'); /* name should be in this format: * MDJ.CD.VHZ.94.121 * which should yield at least 5 parts. */ if (n < 5) /* not a station day file */ continue; strcpy(request.entry.statn, array[0]); strcpy(request.entry.network, array[1]); strcpy(request.entry.chn, array[3]); strcpy(request.entry.location, (strlen(array[2]) == 0) ? " " : array[2]); year = atoi(array[4]); year = cnvt_year(year); day = atoi(array[5]); sprintf(request.entry.start, "%d,%03d", year, day); sprintf(request.entry.end, "%d,%03d,23:59:59.9999", year, day); strcpy(request.entry.file, full_name); strcpy(request.entry.superfile, Header_path); strcpy(request.reqstart, request.entry.start); strcpy(request.reqend, request.entry.end); find_data(&request, end_tspan, &Lvol_head, &Lvol_tail, &Data_path, &count, &first ); nlvol += count; if( Tspan_tail->count == 0 ) Tspan_tail->start = first; Tspan_tail->count += count; /* Find the start of volume */ if (((int)Start_volume.year) > year) { Start_volume.year = year; Start_volume.day = day; } if (((int)Start_volume.year) == year) { if ((int)Start_volume.day > day) Start_volume.day = day; } fuse(&array, n); } closedir(dirp); return nlvol;}/* ------------------------------------------------------------------ */int cnvt_year(y){ if (y < 40) /* good until the year 2040, * should be retired by then */ y += 2000; else if (y < 100) y += 1900; return y;}/* ------------------------------------------------------------------ */int get_file_info(fname, scan, endtime, size, end_tspan)char *fname;struct input_scan *scan;struct input_time *endtime;int *size;struct input_time *end_tspan;{ char buf[4096]; /* max lrecl for now */ struct input_scan rec_scan; struct input_scan sav_rec_scan; struct input_time rec_start; /* ptr->window start time */ struct input_time rec_end; /* ptr->window end time */ int interval, num_bytes; struct stat st; FILE *fptr; num_bytes = stat(fname, &st); if (num_bytes < 0) { perror("bad stat"); return 1; } *size = st.st_size; fptr = fopen(fname, "r"); if (fptr == NULL) { fprintf(stderr, "ERROR: get_file_info(): Unable to open: %s for reading\n", fname); perror("get_file_info"); fclose(fptr); return 1; /* error condition */ } /* obtain lrecl info */ Lrecl = find_Lrecl(fptr, fname); num_bytes = fread(buf, 1, Lrecl, fptr); if (num_bytes < 0) { fprintf(stderr, "ERROR: get_file_info(): Unable to read: %s\n", fname); perror("get_file_info"); fclose(fptr); return 1; /* error condition */ } get_scaninfo(buf, *end_tspan, &rec_scan, &rec_start, &rec_end, &interval); sav_rec_scan = rec_scan; /* check out the last record, assumes file times are in order */ if (fseek(fptr, -Lrecl, 2) == -1) { fprintf(stderr, "ERROR: get_file_info(): Unable to read: %s\n", fname); perror("get_file_info"); fclose(fptr); return 1; /* error condition */ } num_bytes = fread(buf, 1, Lrecl, fptr); if (num_bytes < 0) { fprintf(stderr, "ERROR: get_file_info(): Unable to read: %s\n",fname); perror("get_file_info"); fclose(fptr); return 1; /* error condition */ } get_scaninfo(buf, *end_tspan, &rec_scan, &rec_start, &rec_end, &interval); /* reset rec_start to the saved value */ *scan = sav_rec_scan; *endtime = rec_end; fclose(fptr); return 0; /* no error */ }/* ----------------------------------------------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -