Mapper
decode_HDP.c
Go to the documentation of this file.
1 #define _POSIX_SOURCE
2 char fbuf[100];
3 char directory[100];
5 char nexrad_directory[100];
6 
7 #include "prototypes.h"
8 #define TRUE 1
9 #define FALSE 0
10 
12 
13 main(int argc,char **argv)
14 
15 {
16 
17  extern int errno;
18  FILE *afosfile,*biasfile;
19  char ch, prevchar,pprevchar;
20  int i,j,n,ihr,iminute,len,len2,k,kk;
21  short imin,inc,nrows;
22  char clat[9],clon[9],height[7],year[3],month[3],day[3];
23  char hour[3],minute[3],radar[4],chour[3];
24  unsigned char ncharrows;
25  char param[7],junk[4],t1[7],t2[7],t3[7];
26  char maxval[7],bias[7],error[7],jdate[7],jmin[7];
27  char outname[81],biasname[81],sjunk[50],lradar[4];
28  char cdate[7];
29  div_t time;
30  unsigned char data[300];
31  short nruns,nbytes;
32  short xrun,level;
33  unsigned char xchar;
34  double lat,lon;
35  char zchar,ichar;
36  struct HRAP hrap;
37  struct HRAP irap;
38  float cx,cy ;
39  double pw;
40  unsigned char value[131][131];
41  char temp[40];
42  char st1dir[80];
43  unsigned char cbytes1,cbytes2;
44  FILE *fpout;
45  short int maxvalue;
46  DIR *dirp;
47  struct dirent *de;
48  char fname[100];
49  char *p;
50  int imonth,iyear,ihour,iday;
51  int ilen,slen;
52  char ibuf[100];
53  char dname[100];
54 
55 strcpy(nexrad_directory,"/awips/hydroapps/rfc/local/apps/mapper/nexrad");
56 
57 afosfile = fopen(argv[1],"rb");
58 
59 if(afosfile==NULL) {
60 
61  printf("could not open %s\n",argv[1]);
62 
63  exit(1);
64 
65  }
66 
67 fread(data,sizeof(char),300,afosfile);
68 
69 for(i=0;i<250;i++) {
70 
71  n=memcmp(&data[i],"HDP",3);
72  if(n==0)
73  break;
74 
75  }
76 
77 if(n != 0) {
78 
79  printf("could not find HDP\n");
80  fclose(afosfile);
81  exit(1);
82 
83  }
84 
85 rewind(afosfile);
86 
87 data[i+6]=0;
88 
89 memcpy(radar,&data[i+3],4);
90 
91 printf("decoding of %s started\n",argv[1]);
92 
93 for (;;) {
94 
95  n = fscanf(afosfile,"%c",&ch);
96  if (n==EOF) {
97 
98  printf("Error 1 %s\n",fname);
99  fclose(afosfile);
100  exit(1);
101 
102  }
103 
104  if (ch == ' ' && prevchar=='1' && pprevchar == '8')
105  break;
106 
107  pprevchar=prevchar;
108  prevchar=ch;
109 
110  }
111 
112  /* read header block */
113 
114  n = fscanf(afosfile,"%7s%8s%6s%2s%2s%2s%2s%2s",clat,clon,height,year,month,
115  day,hour,minute);
116 
117  printf("%s %s %s %s ",radar,clat,clon,height);
118 
119  if (n==EOF) {
120 
121  printf("Error 2 %s\n",fname);
122  fclose(afosfile);
123  exit(1);
124 
125  }
126 
127  n = fscanf(afosfile,"%6s%6s%6s%6s",param,t1,t2,t3);
128  if (n==EOF) {
129 
130  printf("Error 3 %s\n",fname);
131  fclose(afosfile);
132  exit(1);
133 
134  }
135 
136  for (i=0;i<13;i++)
137  fscanf(afosfile,"%6s",param);
138 
139  n = fscanf(afosfile,"%6s%6s%6s%6s%6s%6s%6s",maxval,bias,error,
140  jdate,jmin,param,param);
141 
142  if (n==EOF){
143 
144  printf("Error 3 %s\n",fname);
145  fclose(afosfile);
146  exit(1);
147 
148  }
149 
150  strcpy(cdate,convertJulianDate((short)atoi(jdate)));
151 
152  /* determine product valid time, if not H+00 product quit */
153 
154  time = div(atoi(jmin),60);
155  ihr = time.quot;
156  iminute = time.rem;
157 
158  printf("%s %02d:%02d max=%6s bias=%6s err=%6s\n",
159  cdate, ihr, iminute, maxval,bias,error);
160 
161  if (iminute > 10) {
162 
163  printf("%s too old - aborting\n",argv[1]);
164 
165  fclose(afosfile);
166  exit(1);
167 
168  }
169 
170  sprintf(chour,"%02d",ihr);
171 
172  fread(sjunk,sizeof(char),11,afosfile);
173 
174  maxvalue=0;
175 
176  n = fread(&ncharrows, sizeof(char),1,afosfile);
177 printf("ncharrows %d\n",ncharrows);
178 
179  for (i=0; i<ncharrows; i++)
180 
181  {
182 
183  n = fread(&cbytes1, sizeof(char),1,afosfile);
184  n = fread(&cbytes2, sizeof(char),1,afosfile);
185  nbytes=cbytes2;
186 
187  n = fread(data, sizeof(unsigned char), nbytes-2,afosfile);
188 
189  if(n != nbytes-2)
190 
191  {
192  printf(" unexpected EOF encoutered -- ");
193  printf(" number of bytes expected = %d",nbytes-2);
194  printf(" number of bytes found =%d \n",n);
195 
196  fclose(afosfile);
197  exit(1);
198 
199  }
200 
201  nruns = (short)((float)(nbytes-2)/2.);
202  kk=0;
203 
204 printf("nruns %d\n",nruns);
205 
206  for (j=0;j<nruns; j++)
207  {
208  xchar = (unsigned char)data[j*2];
209  xrun = (short) xchar;
210  level = (short) data[1+j*2];
211 
212 
213 printf("xrun %d %d %d\n",xrun,xchar,level);
214 
215  for(k=0;k<xrun;k++) {
216 
217  if(level > 0) {
218 
219  if(i > 130 || kk > 130) {
220 
221  printf("Error 3 %s\n",fname);
222  fclose(afosfile);
223  exit(1);
224 
225  }
226 
227  value[130-i][kk]=(char)level;
228 /*
229 printf("level is %d\n",(char)level);
230  */
231  if(value[130-i][kk] != 255 &&
232  value[130-i][kk] > maxvalue)
233  maxvalue=value[130-i][kk] ;
234 
235  }
236 
237  else
238  value[130-i][kk]=0;
239 
240  kk++;
241 
242  }
243 
244  }
245 
246  }
247 
248  fclose(afosfile);
249 
250  len = strlen("st2_st1");
251 
252  for(i=0;i<3;i++)
253  lradar[i]=tolower(radar[i]);
254 
255  iyear=atoi(year);
256  imonth=atoi(month);
257  iday=atoi(day);
258  ihour=atoi(chour);
259 
260  sprintf(dname,"%s/%02d/%02d",nexrad_directory,iyear,imonth);
261 
262 
263  errno=access(dname,R_OK);
264 
265  if(errno != 0) {
266 
267  sprintf(ibuf,"mkdir -p %s",dname);
268  system(ibuf);
269 
270  sprintf(ibuf,"chmod -R 777 /awips/hydroapps/rfc/local/apps/mapper/nexrad");
271  system(ibuf);
272 
273  }
274 
275  sprintf(outname,"%s/%s.%02d-%02d-%02d-%02d",dname,
276  lradar,iyear,imonth,iday,ihour);
277 
278  fpout=fopen(outname,"wb");
279  if(fpout==NULL) {
280 
281  printf("could not open %s\n",outname);
282 
283  exit(1);
284 
285  }
286 
287  fwrite(value,sizeof(char),131*131,fpout);
288 
289  fclose(fpout);
290 
291  sprintf(ibuf,"gzip -f %s",outname);
292 
293  system(ibuf);
294 
295  printf("file %s written and zipped\n",outname);
296 
297  printf("max value is %d\n",maxvalue);
298 
299 }
300 
301 char *convertJulianDate(jdate)
302  short jdate;
303 {
304  char cdate[7];
305  int days[]={31,28,31,30,31,30,31,31,30,31,30,31} ;
306  int year,month,ndays,date,total,nyear,leap_year;
307  total=0;
308  for (year=1970;year<2010;year++)
309  {
310  leap_year=FALSE;
311  if (year==1972 || year==1976 || year==1980 ||
312  year==1984 || year==1988 || year==1992 || year==1996 ||
313  year==2000 || year==2004 || year==2008)
314  leap_year=TRUE;
315  for (month=0;month<12;month++)
316  {
317  total = total+days[month];
318  if (month==1 && leap_year==TRUE)
319  total++;
320  if (total >= jdate)
321  {
322  ndays=days[month];
323  if (month==1 && leap_year==TRUE) ndays++;
324  date=ndays - (total-jdate);
325  month=month+1;
326  nyear=year-1900;
327  if (year > 2000) nyear=year-2000;
328  sprintf(cdate,"%02d%02d%02d",month,date,nyear);
329  return cdate;
330  }
331  }
332  }
333 }
334 
335 
336 
337 
338 
339 
340 
341 
342 
343 
344 
345 
346 
347 
348 
349 
350 
351 
352 
353 
354 
355 
356 
357 
static int i
char * convertJulianDate()
char fbuf[100]
Definition: decode_HDP.c:2
main(int argc, char **argv)
Definition: decode_HDP.c:13
char database_directory[100]
Definition: decode_HDP.c:4
char directory[100]
Definition: decode_HDP.c:3
#define TRUE
Definition: decode_HDP.c:8
#define FALSE
Definition: decode_HDP.c:9
char nexrad_directory[100]
Definition: decode_HDP.c:5
int hour
Definition: display_data.c:26
int day
Definition: display_data.c:26
int days
Definition: display_data.c:26
int year
Definition: display_data.c:26
int month
Definition: display_data.c:26
fclose(fp)
sprintf(fbuf,"/usr/mapper/nexrad/ngrid.%02d-%02d-%02d-%02d", year, month, day, hour)
printf("fbuf is %s\n", fbuf)
system(tarbuf)
int j
Definition: mapp2h.h:48
double lat
Definition: mapp2h.h:41
char temp[9]
Definition: mapp2h.h:66
double lon
Definition: mapp2h.h:41
int k
Definition: mapp2h.h:48
HRAP hrap
Definition: mapp2h.h:53
int len2
Definition: mapp2h.h:48
float value
Dimension height
char fname[100]
Definition: send_afos.c:6
struct dirent * de
Definition: shef_structs.h:266
DIR * dirp
Definition: shef_structs.h:265
Definition: mapp2h.h:25