📄 find_segment.c
字号:
"[find_segment] err set to %d\n", err ); } else if (len == 0) { /* segment ends at eof */ *end_o = flen; err = 1; if( Debug >= D_MAX ) fprintf( D_OUT, "[find_segment] normal eof\n" ); } if (err < 2) { /* get info on time segment found */ *scan = save_scan; /* time of last sample in last rec before this one */ *endtime = save_endtime; *start_o = save_o; if (Debug >= D_MED) { fprintf (D_OUT, "[find_segment] End of segment at offset = %d\n",*end_o); *msg = '\0'; strcat( msg, input_time_to_asc(save_scan.time) ); strcat( msg, " to " ); strcat( msg, input_time_to_asc(save_endtime) ); fprintf (D_OUT, "[find_segment] %s\n", msg ); } save_endtime = rec_end; } if (err == 0) { /* another valid segment in file so save info */ save_scan = rec_scan; /* save scan info */ save_o = *end_o; /* save start offset pointer */ segcnt++; /* increment segment counter */ if( Debug >= D_MAX ) fprintf( D_OUT, "[find_segment] middle of segment\n" ); } else { /* no more data of interest in file so reset */ file_o = 0; /* reset file offset pointer */ save_o = -1; /* reset start offset pointer */ segcnt = 0; /* reset segment counter */ if( Debug >= D_MAX ) fprintf( D_OUT, "[find_segment] reset all\n" ); } if (Debug >= D_MIN) fprintf (D_OUT, "[find_segment] Finished.\n"); return (err);}/* ---------------------------------------------------------------------- */void set_msg_printed_flag(x)int x;{ msg_printed = x;}/* ------------------------------------------------------------------------ *//* ------------------------------------------------------------------------ */int find_Lrecl(fptr, f)FILE *fptr;char *f;{ int num_bytes; struct input_data_logrec *data_header; char stn_name[10]; char channel[10]; char loc[10]; char wrkstr[200]; /* grab the station/channel name from file name, use later */ /* grabs the basename */ if (strrchr(f, '/')) strcpy(wrkstr, strrchr(f, '/') + 1); else strcpy(wrkstr, f); /* since we are dealing with a station/day file, we assume that * the logical record size will not vary in the space of one * day, might not be the safest of assumptions. But it is the same * strategy used by the seed_pack routines which got us here. */ if (fptr == NULL) { sprintf(wrkstr, "NULL file pointer in find_Lrecl. File %s. Assuming 4096\n", f); error_handler(ERROR, wrkstr); perror("find_Lrecl"); return(4096); } if ((num_bytes = fread(buf, 1, sizeof(buf), fptr)) <= 0) { fclose(fptr); sprintf(wrkstr, "Unable to read data file %s to check the data record length. Assuming %d\n", f, Lrecl); error_handler(ERROR, wrkstr); perror("find_Lrecl"); return(Lrecl); } /* look for the case where this data file is exactly 1 data * record in length */ if (num_bytes == 512) { rewind(fptr); return 512; } if (num_bytes == 1024) { rewind(fptr); return 1024; } if (num_bytes == 2048) { rewind(fptr); return 2048; } data_header = (struct input_data_logrec *)buf; strncpy(stn_name, data_header->hdr.scan.station, sizeof(data_header->hdr.scan.station)); stn_name[sizeof(data_header->hdr.scan.station)] = 0; strncpy(channel, data_header->hdr.scan.channel, sizeof(data_header->hdr.scan.channel)); channel[sizeof(data_header->hdr.scan.channel)] = 0; strncpy(loc, data_header->hdr.scan.location, sizeof(data_header->hdr.scan.location)); loc[sizeof(data_header->hdr.scan.location)] = 0; data_header = (struct input_data_logrec *)&buf[512]; if (strncmp(data_header->hdr.scan.station, stn_name, strlen(stn_name)) == 0) if (strncmp(data_header->hdr.scan.channel, channel, strlen(channel)) == 0) if (strncmp(data_header->hdr.scan.location, loc, strlen(loc)) == 0) { rewind(fptr); return 512; } data_header = (struct input_data_logrec *)&buf[1024]; if (strncmp(data_header->hdr.scan.station, stn_name, strlen(stn_name)) == 0) if (strncmp(data_header->hdr.scan.channel, channel, strlen(channel)) == 0) if (strncmp(data_header->hdr.scan.location, loc, strlen(loc)) == 0) { rewind(fptr); return 1024; } /* 2k record */ data_header = (struct input_data_logrec *)&buf[2048]; if (strncmp(data_header->hdr.scan.station, stn_name, strlen(stn_name)) == 0) if (strncmp(data_header->hdr.scan.channel, channel, strlen(channel)) == 0) if (strncmp(data_header->hdr.scan.location, loc, strlen(loc)) == 0) { rewind(fptr); return 2048; } rewind(fptr); return 4096;}/* ------------------------------------------------------------------------- */int scan_for_seg_start(fname, fptr, start, end_tspan)char *fname;FILE *fptr;struct input_time *start, *end_tspan;{ struct input_time *rec_start; /* start time of data record */ int now_fptr, floor_fptr, ceiling_fptr; int filesize, cnter = 0; if (Debug >= D_MIN) { fprintf(stderr, "entering scan_for_seg_start()\n"); fprintf(stderr, "filename =%s \n", fname); fprintf(stderr, "Looking for segment %d,%d,%d:%d:%d\n", start->year, start->day, start->hour, start->minute, start->second); } filesize = get_fsize(fname); if (filesize / Lrecl < 50) { /* don't bother with scan */ return; } if (start->hour > 12) { /* starts in the middle */ now_fptr = filesize/2; } else { now_fptr = filesize / 8; } fseek(fptr, (now_fptr / Lrecl) * Lrecl, 0); ceiling_fptr = filesize; fread(buf, sizeof(buf), 1, fptr); rec_start = (struct input_time *)&buf[20]; fseek(fptr, -Lrecl, 1); if ((cmp_time (*start, rec_start, 100) < 0)) { /* bail */ rewind(fptr); return; } else if ((cmp_time (*start, rec_start, 100) == 0)) { return; } floor_fptr = ftell(fptr); now_fptr = (ceiling_fptr - floor_fptr) / 2; now_fptr = ((int)(now_fptr / Lrecl)) * Lrecl; fseek(fptr, now_fptr, 1); while (1) { fread(buf, sizeof(buf), 1, fptr); fseek(fptr, -Lrecl, 1); rec_start = (struct input_time *)&buf[20]; /* record is equal */ if (cmp_time (rec_start, *start, 100) == 0) return; /* record is less than request, * record becomes the floor, * try again. */ if (cmp_time (rec_start, *start, 100) < 0) { floor_fptr = ftell(fptr); if (((ceiling_fptr - floor_fptr) / Lrecl < 10) || cnter > 8) { /* close enough, bail using floor_fptr */ return; } now_fptr = (ceiling_fptr - floor_fptr) / 2; now_fptr = ((int)(now_fptr / Lrecl)) * Lrecl; fseek(fptr, now_fptr, 1); } /* record is later than request, * record becomes ceiling, try again */ else { ceiling_fptr = ftell(fptr); if (((ceiling_fptr - floor_fptr) / Lrecl < 10) || cnter > 8) { /* close enough, bail using floor_fptr */ fseek(fptr, -(ceiling_fptr - floor_fptr), 1); return; } now_fptr = (ceiling_fptr - floor_fptr) / 2; now_fptr = ((int)(now_fptr / Lrecl)) * Lrecl; fseek(fptr, -now_fptr, 1); } cnter++; } /* while cnter < 4 */ return;}/* ------------------------------------------------------------------------ */#include <sys/stat.h>int get_fsize(fname)char *fname;{ struct stat s; stat(fname, &s); return s.st_size; }/* ------------------------------------------------------------------------ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -