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