📄 summary.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h> #include "rdseed.h"#define SUBSTR(str,xch,ch) \ { char *c; \ while (c = strchr(str, xch)) \ *c = ch; \ }#define IS_BTW(x,a,b) \ ((timecmp(x, a) >= 0) && (timecmp(x, b) <= 0)) #define append_linklist_element(new, head, tail) \ new->next = NULL; \ if (head != NULL) tail->next = new; \ else head = new; \ tail = new;struct twindow { char time_start[23]; char time_end[23]; int already_used; char chn_list[1024]; char loc_list[1024]; struct twindow *next;};struct stn_tspan{ char net[5]; char stn[10]; char *this_event; struct twindow *ts_head; struct twindow *ts_tail; struct stn_tspan *next; } *stn_listhead, *stn_listtail;struct tspan_list{ char time_start[23]; char time_end[23]; int start_seq_num; int end_seq_num; int already_used; struct tspan_list *next;} *tspans_listhead;static char *this_event = "";char *add_time();void dump_stn_nodes();void dump_twindows();extern float WeedVersion;extern int output;read_summary_file(fname)char *fname;{ FILE *sumfile; char buffer[512]; char *this_ev, *ch; char token[30]; int bias_start; int bias_end; float weed_version; /* ### warning Must be big enough for indeterminate channel count */ char chn[3000]; char loc[3000]; char time_win_start[23]; char time_win_end[23]; char tmp[23]; struct stn_tspan *stn_ptr; struct twindow *twindow_ptr; weed_version = 1.0; strcpy(chn, ""); strcpy(loc, ""); sumfile = fopen(fname, "r"); if (sumfile == NULL) { fprintf(stderr, "Unable to open summary file: %s!\n", fname); perror("read_summary_file"); return(0); } /* read in the first token */ while (fscanf(sumfile, "%s", token) != EOF) { /* scan first tokens, WEED_ID, EVENT, STATION, or PHASE */ if (strcmp(token, "") == 0) continue; if (strstr(token, "Weed_version")) { fgets(buffer, 512, sumfile); weed_version = atof(buffer); } if (strstr(token, "EVENT")) { /* save Event line */ fgets(buffer, 512, sumfile); /* erase \n at end */ buffer[strlen(buffer) - 1] = 0; this_ev = malloc(strlen(buffer)); if (this_ev == NULL) { fprintf(stderr, "Error : unable to obtain memory for event from summary file!\n"); fprintf(stderr, "Quitting\n"); exit(-1); } /* save it for later */ strcpy(this_ev, buffer); continue; } if (strstr(token, "STATION")) { stn_ptr = (struct stn_tspan *) malloc(sizeof(struct stn_tspan)); if (stn_ptr == NULL) { fprintf(stderr, "Error : unable to obtain memory for station from summary file!\n"); fprintf(stderr, "Quitting\n"); exit(-1); } memset((char *)stn_ptr, 0, sizeof(struct stn_tspan)); fscanf(sumfile, "%s %s", stn_ptr->stn, stn_ptr->net); stn_ptr->this_event = this_ev; append_linklist_element(stn_ptr, stn_listhead, stn_listtail); add_stn(stn_ptr->stn); fgets(buffer, 512, sumfile); continue; } if (strstr(token, "PHASE")) { twindow_ptr = (struct twindow *) malloc(sizeof(struct twindow)); memset((char *)twindow_ptr, 0, sizeof(struct twindow)); if (weed_version >= 2.7) fscanf(sumfile, "%*s %d %*s %d %s %s %s %s %*s\n", &bias_start, &bias_end, chn, loc, time_win_start, time_win_end); else fscanf(sumfile, "%*s %d %*s %d %s %s %s %*s\n", &bias_start, &bias_end, chn, time_win_start, time_win_end); /* strip out the quotes in the channel list */ strncpy(twindow_ptr->chn_list, &chn[1], /* start quote */ strlen(chn) > 511 ? 511 : strlen(chn) - 2); /* now the ending quote */ twindow_ptr->chn_list[strlen(chn) > 511 ? 511 : strlen(chn) - 2] = 0; if (weed_version > 1.0) { /* strip out the quotes in the location list */ strncpy(twindow_ptr->loc_list, &loc[1], /* start quote */ strlen(loc) > 1023 ? 1023 : strlen(loc) - 2); /* now the ending quote */ twindow_ptr->loc_list[strlen(loc) > 1023 ? 1023 : strlen(loc) - 2] = 0; } else strcpy(twindow_ptr->loc_list, "*"); add_chn(twindow_ptr->chn_list); add_loc(twindow_ptr->loc_list); /* WEED version -1.5 and greater allows the user * to enter negative or positive values here. * If < 2.0 assume subtraction only, make into * negitive */ add_time(time_win_start, tmp, WeedVersion >= 1.5 ? bias_start : -bias_start); strcpy(twindow_ptr->time_start, tmp); add_time(time_win_end, tmp, WeedVersion >= 1.5 ? bias_end: -bias_end); strcpy(twindow_ptr->time_end, tmp); append_linklist_element(twindow_ptr, stn_ptr->ts_head, stn_ptr->ts_tail); continue; } } /* while fscanf */ fclose(sumfile); /* must parse channel_list as built in add_chn(), parse * into channel_point, all globals, used by parsers() */ channel_count = 0; ch = strtok(channel_list, ","); if (ch != NULL) do { channel_point[channel_count] = ch; channel_count++; } while ((ch = strtok(NULL, ",")) != NULL); location_count = 0; ch = strtok(location_list, ","); if (ch != NULL) do { location_point[location_count] = ch; location_count++; } while ((ch = strtok(NULL, ",")) != NULL); /* dump_stn_nodes(stn_listhead); */ return 1; }/* ------------------------------------------------------------------- */char *strptime();char *ddd2mmdd();/* --------------------------------------------------------------------- */void dump_stn_nodes(stn_node)struct stn_tspan *stn_node;{ while (stn_node) { printf("%s / %s:%s\n", stn_node->net, stn_node->stn, stn_node->this_event); dump_twindows(stn_node->ts_head); stn_node = stn_node->next; }}/* -------------------------------------------------------------------- */int add_stn(stn)char *stn;{ int i; int found = FALSE; for (i = 0; i < station_count; i++) if (strcmp(station_point[i], stn) == 0) found = TRUE; if (!found) { station_point[i] = stn; station_count++; } return;}/* -------------------------------------------------------------------- *//* add_chn() and add_loc() simply add the chn and locs to a global string * making sure that no duplicates exists. Then when read_summary_file() * exits it loads these lists into the global variables. */int add_chn(chn)char *chn;{ int i; int found = FALSE; char *ch; char *ch_ptr; char channels[512]; /* protect original string - also channel_list is global */ strncpy(channels, chn, strlen(chn)); channels[strlen(chn)] = 0; /* the time window channel string could be comma delimited */ ch = strtok(channels, ","); while(ch != NULL) { if (strstr(channel_list, ch) == 0) { strcat(channel_list, ch); strcat(channel_list, ","); } ch = strtok(NULL, ","); } return;}/* ------------------------------------------------------------------ */int add_loc(loc)char *loc;{ int i; int found = FALSE; char *ch; char *ch_ptr; char locations[1024]; /* protect original string - also location_list is global */ strncpy(locations, loc, strlen(loc)); locations[strlen(loc)] = 0; SUBSTR(locations, ':', ','); /* the time window channel string could be comma delimited */ ch = strtok(locations, ","); while(ch != NULL) { if (strstr(location_list, ch) == 0) { strcat(location_list, ch); strcat(location_list, ","); } ch = strtok(NULL, ","); } /* replace dashes with spaces */ SUBSTR(location_list, '-', ' '); return;}/* --------------------------------------------------------------------- */void dump_twindows(twin)struct twindow *twin;{ char time_start[23]; char time_end[23]; char chn_list[100]; while (twin) { printf("\t%s <> %s - %s\n", twin->time_start, twin->time_end, twin->chn_list); twin = twin->next; } return;}/* ------------------------------------------------------------------------- */int free_stn_nodes(){ free_stn(stn_listhead); stn_listhead = NULL; stn_listtail = NULL; return;}/* ------------------------------------------------------------------------- */int free_stn(node)struct stn_tspan *node;{ if (node == NULL) { return; } else { free_stn(node->next); free_tspans(node->ts_head); node->ts_head = NULL; node->ts_tail = NULL; free(node->this_event); free(node); node = NULL; } return;}/* ------------------------------------------------------------------------- */int free_tspans(ts_node)struct twindow *ts_node;{ if (ts_node == NULL) { return; } else { free_tspans(ts_node->next); free(ts_node); ts_node = NULL; } return;}/* ------------------------------------------------------------------------- */ char *add_time(time_asc, tmp, incr)char *time_asc; /* YYYY,DDD,HH:MM:SS.FFFF */char *tmp;int incr;{ time_t t; struct time tm, newtime; char YYYYDDDetc[30]; memset((char *)&tm, 0, sizeof(struct tm)); SUBSTR(time_asc, ',', ' '); SUBSTR(time_asc, ':', ' '); SUBSTR(time_asc, '.', ' '); sscanf(time_asc, "%d %d %d %d %d %d", &tm.year, &tm.day, &tm.hour, &tm.minute, &tm.second, &tm.fracsec); if (incr < 0) newtime = timesub(tm, (-incr) * 10000); else newtime = timeadd(tm, incr * 10000); sprintf(YYYYDDDetc, "%d,%03d,%02d:%02d:%02d.%d", newtime.year, newtime.day, newtime.hour, newtime.minute, newtime.second, newtime.fracsec); strcpy(tmp, YYYYDDDetc); return tmp; }/* ---------------------------------------------------------------------- */#define isaleap(year) ((((year)%100 != 0) && ((year)%4 == 0)) || ((year)%400 == 0))/* ----------------------------------------------------------------------- */static int days_in_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};char *ddd2mmdd(ddd, yyyy)int ddd;int yyyy;{ static char mmddyyyy[200]; int mon; for (mon = 1; mon < 13; mon++) { if (ddd <= days_in_month[mon]) break; ddd -= days_in_month[mon]; /* adjust for leap year */ if (isaleap(yyyy) && (mon == 2)) ddd--; } if (mon == 13) { fprintf(stderr, "Bad day number!\n"); return; } sprintf(mmddyyyy, "%d/%d/%d", mon, ddd, yyyy); return mmddyyyy;}/* ------------------------------------------------------------------------- */struct twindow *scan_summary_file(stn, chn, net, loc, blk_start, blk_end)char *stn, *chn, *net, *loc;struct time *blk_start;struct time *blk_end;{ char **c_ptr, **l_ptr; struct stn_tspan *stn_ptr; struct time req_start; struct time req_end; int found_chn; int i, n, nn; stn_ptr = stn_listhead; while (stn_ptr) { found_chn = FALSE; if ((strcmp(stn_ptr->stn, stn) != 0)) { stn_ptr = stn_ptr->next; continue; } if (strcmp(stn_ptr->net, "??") == 0 ? 0 : (strcmp(stn_ptr->net, net) != 0)) { stn_ptr = stn_ptr->next; continue; } /* check phase lines, comparing channel */ strupr(stn_ptr->ts_head->chn_list); n = split(stn_ptr->ts_head->chn_list, &c_ptr, ','); for (i = 0; i < n; i++) { if (wstrcmp(chn, c_ptr[i], 3) == 0) { nn = split(stn_ptr->ts_head->loc_list, &l_ptr, ','); /* nn and n should be equal */ if (nn != n) { fprintf(stderr, "Warning, unable to parse location codes in the summary file. \n"); found_chn = TRUE; } else { if (wstrcmp(loc, l_ptr[i], 2)) found_chn = TRUE; } fuse(&l_ptr, nn); found_chn = TRUE; } } fuse(&c_ptr, n); if (!found_chn) { stn_ptr = stn_ptr->next; continue; } timecvt(&req_start, stn_ptr->ts_head->time_start); timecvt(&req_end, stn_ptr->ts_head->time_end); /* at last - compare times */ if (!((IS_BTW(req_start, *blk_start, *blk_end)) ||
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -