Mapper
esp_age.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <math.h>
6 #include <sys/time.h>
7 
8 /* returns the age of an esp .CS file in hours */
9 
10 int esp_age (char *directory, char *file, int debug)
11 {
12 FILE *fp;
13 FILE *pf, *popen();
14 char fmt[80];
15 char filename[256];
16 char line[256];
17 char info[256];
18 char cmd[256];
19 int age;
20 int time;
21 int year,month,day,hour,minute;
22 int yr,mo,dy,hr,mn,sd;
23 int off_set;
24 int approach=2;
25 long t_file,t_system;
26 unsigned long fileLen;
27 float *buffer;
28 
29  strcpy(filename,directory);
30  strcat(filename,"/");
31  strcat(filename,file);
32 
33  if(debug)
34  {
35  printf("dir: <%s>\n",directory);
36  printf("file: <%s>\n",file);
37  printf("both: <%s>\n",filename);
38  }
39 
40  if(approach==1)
41  {
42  sprintf(cmd,"/awips/hydroapps/lx/rfc/nwsrfs/ens/scripts/run_espdata h %s",filename);
43  if(debug)
44  printf("cmd: <%s>\n",line);
45 
46  year=2000;month=1;day=1;hour=12;
47 
48  pf=popen(cmd,"r");
49  while(fgets(line,sizeof(line),pf))
50  {
51  if(debug)
52  printf("%s\n",line);
53  if(!strncmp(line,"_now[0]",7))
54  sscanf(line+13,"%d",&month);
55  if(!strncmp(line,"_now[1]",7))
56  sscanf(line+13,"%d",&day);
57  if(!strncmp(line,"_now[2]",7))
58  sscanf(line+13,"%d",&year);
59  if(!strncmp(line,"_now[3]",7))
60  {
61  sscanf(line+13,"%d",&time);
62  hour=time/100;
63  minute=time%100;
64  }
65  }
66  pclose(pf);
67  t_file=ouptime(year,month,day,hour,0,0);
68  if(debug)
69  {
70  printf("esp_file = %02d/%02d/%04d @ %02d%02d",month,day,year,hour,minute);
71  printf("esp_time = %d\n",t_file);
72  }
73  }
74  else
75  {
76  if((fp=fopen(filename,"rb")))
77  {
78  fseek(fp,0,SEEK_END);
79  fileLen=ftell(fp);
80  fseek(fp,0,SEEK_SET);
81  buffer=(float *)malloc(fileLen+1);
82 
83  if(!buffer)
84  {
85  if(debug)
86  printf("memmory error on buffer\n");
87  fclose(fp);
88  return(-1);
89  }
90 
91  fread(buffer,fileLen,4,fp);
92  if(debug)
93  printf("%s read into buffer!\n",filename);
94  fclose(fp);
95 
96  if(debug)
97  printf("%s (fileLen=%d bytes)\n",filename,fileLen*4);
98 
99  month=(int)buffer[9];
100  day=(int)buffer[10];
101  year=(int)buffer[11];
102  time=(int)buffer[12];
103  hour=time/100;
104  minute=time%100;
105 
106  off_set=-1*(int)buffer[24];
107  adj_time(off_set,year,month,day,hour,&yr,&mo,&dy,&hr);
108  year=yr;month=mo;day=dy;hour=hr;
109  t_file=ouptime(year,month,day,hour,0,0);
110 
111  if(debug)
112  {
113  printf("esp_file = %02d/%02d/%04d @ %02d%02d ZULU\n",month,day,year,hour,minute);
114  printf("esp_time = %d\n",t_file);
115  }
116  }
117  else
118  {
119  if(debug)
120  printf("cannot open .CS file: %s\n",filename);
121  return(-1);
122  }
123  }
124 
125  strncpy(fmt,"'+%Y %m %d %H %M %S'",20);
126  *(fmt+20)='\0';
127  sprintf(line,"date -u %s",fmt);
128  if(debug)
129  printf("cmd: <%s>\n",line);
130  pf=popen(line,"r");
131  fgets(info,sizeof(info),pf);
132  if(debug)
133  printf("info: <%s>\n",info);
134  pclose(pf);
135 
136  sscanf(info,"%d %d %d %d %d %d",&yr,&mo,&dy,&hr,&mn,&sd);
137  t_system=ouptime(yr,mo,dy,hr,mn,sd);
138  if(debug)
139  printf("system_time: %d\n",t_system);
140 
141  age=(int)((t_system - t_file)/3600);
142 
143  if(debug)
144  printf("esp_age is %d hrs.\n",age);
145 
146  return(age);
147 }
148 
int adj_time(int adj, int y, int m, int d, int h, int *adj_y, int *adj_m, int *adj_d, int *adj_h)
Definition: adj_time.c:10
char directory[100]
Definition: decode_HDP.c:3
int hour
Definition: display_data.c:26
int day
Definition: display_data.c:26
int year
Definition: display_data.c:26
int month
Definition: display_data.c:26
int esp_age(char *directory, char *file, int debug)
Definition: esp_age.c:10
int debug
Definition: hydro_data.h:53
char filename[200]
Definition: is_file_closed.c:5
long ouptime(int year, int month, int day, int hour, int minute, int second)
Definition: ouptime.c:10
fclose(fp)
sprintf(fbuf,"/usr/mapper/nexrad/ngrid.%02d-%02d-%02d-%02d", year, month, day, hour)
printf("fbuf is %s\n", fbuf)
fp
Definition: make_NEXRAD.c:339
int dy
char cmd[100]
Definition: send_afos.c:7