Mapper
make_nexrad.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  extern int errno;
17  char tarbuf[1000];
18  int irain;
19  struct nexrad nexrad;
20  int l;
21  int maxi,maxj;
22  int nrapx[131][131],nrapy[131][131];
23  char dbuf[200];
24  extern int errno;
25  FILE *afosfile;
26  FILE *fp;
27  char ch, prevchar,pprevchar;
28  int i,j,n,ihr,iminute,len,k,kk;
29  char clat[9],clon[9],height[7],year[3],month[3],day[3];
30  char hour[3],minute[3],radar[4],chour[3];
31  unsigned char ncharrows;
32  char param[7],t1[7],t2[7],t3[7];
33  char maxval[7],bias[7],error[7],jdate[7],jmin[7];
34  char outname[81],sjunk[50],lradar[4];
35  char cdate[7];
36  div_t time;
37  unsigned char data[300];
38  short nruns,nbytes;
39  short xrun,level;
40  unsigned char xchar;
41  double lat,lon;
42  struct HRAP hrap;
43  struct HRAP irap;
44  float cx,cy;
45  unsigned char value[131][131];
46  unsigned char cbytes1,cbytes2;
47  FILE *fpout;
48  short int maxvalue;
49  char fname[100];
50  char *p;
51  int imonth,iyear,ihour,iday;
52  char ibuf[100];
53  char dname[100];
54  float xlat,nlat,xlon,nlon;
55  float hmaxx,hmaxy,hminx,hminy;
56  int imaxx,iminx,imaxy,iminy;
57  char kbuf[120];
58  float val[500][500];
59  char *nexname[20];
60  FILE *fq;
61  float pw,pnexrad[255];
62  int ii,jj;
63  char name[100];
64  int ier;
65 
66 strcpy(nexrad_directory,"/usr/mapper/nexrad");
67 
68 afosfile = fopen(argv[1],"rb");
69 
70 if(afosfile==NULL) {
71 
72  printf("could not open %s\n",argv[1]);
73 
74  exit(1);
75 
76  }
77 
78 fread(data,sizeof(char),300,afosfile);
79 
80 for(i=0;i<250;i++) {
81 
82  n=memcmp(&data[i],"HDP",3);
83  if(n==0)
84  break;
85 
86  }
87 
88 if(n != 0) {
89 
90  printf("could not find HDP\n");
91  fclose(afosfile);
92  exit(1);
93 
94  }
95 
96 rewind(afosfile);
97 
98 data[i+6]=0;
99 
100 memcpy(radar,&data[i+3],4);
101 
102 printf("decoding of %s started\n",argv[1]);
103 
104 for (;;) {
105 
106  n = fscanf(afosfile,"%c",&ch);
107  if (n==EOF) {
108 
109  printf("Error 1 %s\n",fname);
110  fclose(afosfile);
111  exit(1);
112 
113  }
114 
115  if (ch == ' ' && prevchar=='1' && pprevchar == '8')
116  break;
117 
118  pprevchar=prevchar;
119  prevchar=ch;
120 
121  }
122 
123  /* read header block */
124 
125  n = fscanf(afosfile,"%7s%8s%6s%2s%2s%2s%2s%2s",clat,clon,height,year,month,
126  day,hour,minute);
127 
128  printf("%s %s %s %s ",radar,clat,clon,height);
129 
130  if (n==EOF) {
131 
132  printf("Error 2 %s\n",fname);
133  fclose(afosfile);
134  exit(1);
135 
136  }
137 
138  n = fscanf(afosfile,"%6s%6s%6s%6s",param,t1,t2,t3);
139  if (n==EOF) {
140 
141  printf("Error 3 %s\n",fname);
142  fclose(afosfile);
143  exit(1);
144 
145  }
146 
147  for (i=0;i<13;i++)
148  fscanf(afosfile,"%6s",param);
149 
150  n = fscanf(afosfile,"%6s%6s%6s%6s%6s%6s%6s",maxval,bias,error,
151  jdate,jmin,param,param);
152 
153  if (n==EOF){
154 
155  printf("Error 3 %s\n",fname);
156  fclose(afosfile);
157  exit(1);
158 
159  }
160 
161  strcpy(cdate,convertJulianDate((short)atoi(jdate)));
162 
163  /* determine product valid time, if not H+00 product quit */
164 
165  time = div(atoi(jmin),60);
166  ihr = time.quot;
167  iminute = time.rem;
168 
169  printf("%s %02d:%02d max=%6s bias=%6s err=%6s\n",
170  cdate, ihr, iminute, maxval,bias,error);
171 
172  if (iminute > 10) {
173 
174  printf("%s too old - aborting\n",argv[1]);
175 
176  fclose(afosfile);
177  exit(1);
178 
179  }
180 
181  sprintf(chour,"%02d",ihr);
182 
183  fread(sjunk,sizeof(char),11,afosfile);
184 
185  maxvalue=0;
186 
187  n = fread(&ncharrows, sizeof(char),1,afosfile);
188 
189  for (i=0; i<ncharrows; i++)
190 
191  {
192 
193  n = fread(&cbytes1, sizeof(char),1,afosfile);
194  n = fread(&cbytes2, sizeof(char),1,afosfile);
195  nbytes=cbytes2;
196 
197  n = fread(data, sizeof(unsigned char), nbytes-2,afosfile);
198 
199  if(n != nbytes-2)
200 
201  {
202  printf(" unexpected EOF encoutered -- ");
203  printf(" number of bytes expected = %d",nbytes-2);
204  printf(" number of bytes found =%d \n",n);
205 
206  fclose(afosfile);
207  exit(1);
208 
209  }
210 
211  nruns = (short)((float)(nbytes-2)/2.);
212  kk=0;
213 
214  for (j=0;j<nruns; j++)
215  {
216  xchar = (unsigned char)data[j*2];
217  xrun = (short) xchar;
218  level = (short) data[1+j*2];
219 
220  for(k=0;k<xrun;k++) {
221 
222  if(level > 0) {
223 
224  if(i > 130 || kk > 130) {
225 
226  printf("Error 3 %s\n",fname);
227  fclose(afosfile);
228  exit(1);
229 
230  }
231 
232  value[130-i][kk]=(char)level;
233 
234  if(value[130-i][kk] != 255 &&
235  value[130-i][kk] > maxvalue)
236  maxvalue=value[130-i][kk] ;
237 
238  }
239 
240  else
241  value[130-i][kk]=0;
242 
243  kk++;
244 
245  }
246 
247  }
248 
249  }
250 
251  fclose(afosfile);
252 
253  iyear=atoi(year);
254  imonth=atoi(month);
255  iday=atoi(day);
256  ihour=atoi(chour);
257 
258  for(i=0;i<3;i++)
259  lradar[i]=tolower(radar[i]);
260 
261  lradar[3]=0;
262 
263  sprintf(outname,"%s/raw/%s.%02d-%02d-%02d-%02d",nexrad_directory,
264  lradar,iyear,imonth,iday,ihour);
265 
266  fpout=fopen(outname,"wb");
267  if(fpout==NULL) {
268 
269  printf("could not open %s\n",outname);
270 
271  exit(1);
272 
273  }
274 
275  fwrite(value,sizeof(char),131*131,fpout);
276 
277  fclose(fpout);
278 
279  printf("max value is %d\n",maxvalue);
280 
281  /* get max min hrap bins */
282 
283  iyear=atoi(year);
284  imonth=atoi(month);
285  iday=atoi(day);
286  ihour=atoi(chour);
287 
288  sprintf(dname,"%s/%02d/%02d",nexrad_directory,iyear,imonth);
289 
290  errno=access(dname,R_OK);
291 
292  if(errno != 0) {
293 
294  sprintf(ibuf,"mkdir -p %s",dname);
295  system(ibuf);
296 
297  sprintf(ibuf,"chmod -R 777 /usr/mapper/nexrad");
298  system(ibuf);
299 
300  }
301 
302  sprintf(dbuf,"/usr/mapper/NEXRAD_radars");
303 
304  fp=fopen(dbuf,"r");
305 
306  if(fp==NULL) {
307 
308  printf("could not open /usr/mapper/NEXRAD_radars\n");
309  exit(1);
310 
311  }
312 
313  p=fgets(kbuf,80,fp);
314  if(p==NULL) {
315 
316  printf("error in format /usr/mapper/NEXRAD_radars\n");
317  exit(1);
318 
319  }
320 
321  p=pars_line(kbuf,"x_lat=",dbuf);
322 
323  if(p==NULL) {
324 
325  printf("no x_lat\n");
326  exit(1);
327 
328  }
329 
330  xlat=atof(dbuf);
331 
332  p=pars_line(kbuf,"n_lat=",dbuf);
333 
334  if(p==NULL) {
335 
336  printf("no n_lat\n");
337  exit(1);
338 
339  }
340 
341  nlat=atof(dbuf);
342 
343  p=pars_line(kbuf,"x_lon=",dbuf);
344 
345  if(p==NULL) {
346 
347  printf("no x_lon\n");
348  exit(1);
349 
350  }
351 
352  xlon=atof(dbuf);
353 
354  p=pars_line(kbuf,"n_lon=",dbuf);
355 
356  if(p==NULL) {
357 
358  printf("no n_lon\n");
359  exit(1);
360 
361  }
362 
363  nlon=atof(dbuf);
364 
365 
366  /* initialize val */
367 
368  for(i=0;i<500;i++) {
369  for(j=0;j<500;j++) {
370 
371  val[i][j]=-9999;
372 
373  }
374 
375  }
376 
377  /* initialize name */
378 
379  for(i=0;i<20;i++)
380  nexname[i]=calloc(20,1);
381 
382 /* initialize pnexrad */
383 
384  for(i=0;i<255;i++) {
385 
386  pw=(-6) +( (float)(i-1))*.125;
387  pw=pw/10;
388  pw=.0397 * pow(10,pw);
389 
390  if(i==0)
391  pnexrad[i]=0;
392 
393  else
394  pnexrad[i]=pw;
395 
396  }
397 
398  hmaxx=-1;hmaxy=-1;
399  hminx=9999;hminy=9999;
400 
402 
403  if(hrap.x > hmaxx)
404  hmaxx=hrap.x;
405 
406 
407  if(hrap.x < hminx)
408  hminx=hrap.x;
409 
410 
411  if(hrap.y > hmaxy)
412  hmaxy=hrap.y;
413 
414 
415  if(hrap.y < hminy)
416  hminy=hrap.y;
417 
418  hrap=LatLongToHrap(xlat,nlon);
419 
420  if(hrap.x > hmaxx)
421  hmaxx=hrap.x;
422 
423 
424  if(hrap.x < hminx)
425  hminx=hrap.x;
426 
427 
428  if(hrap.y > hmaxy)
429  hmaxy=hrap.y;
430 
431 
432  if(hrap.y < hminy)
433  hminy=hrap.y;
434 
435  hrap=LatLongToHrap(nlat,xlon);
436 
437  if(hrap.x > hmaxx)
438  hmaxx=hrap.x;
439 
440 
441  if(hrap.x < hminx)
442  hminx=hrap.x;
443 
444 
445  if(hrap.y > hmaxy)
446  hmaxy=hrap.y;
447 
448 
449  if(hrap.y < hminy)
450  hminy=hrap.y;
451 
452  hrap=LatLongToHrap(nlat,nlon);
453 
454  if(hrap.x > hmaxx)
455  hmaxx=hrap.x;
456 
457 
458  if(hrap.x < hminx)
459  hminx=hrap.x;
460 
461 
462  if(hrap.y > hmaxy)
463  hmaxy=hrap.y;
464 
465 
466  if(hrap.y < hminy)
467  hminy=hrap.y;
468 
469  imaxx=hmaxx+.5;
470  iminx=hminx+.5;
471 
472  imaxy=hmaxy+.5;
473  iminy=hminy+.5;
474 
475  maxi=imaxx-iminx;
476  maxj=imaxy-iminy;
477 
478  /* need times to get for each file */
479 
480  for(l=0;l<20;l++) {
481 
482  p=fgets(kbuf,80,fp);
483  if(p==NULL) {
484 
485  fclose(fp);
486  break;
487 
488  }
489 
490  p=pars_line(kbuf,"radar=",dbuf);
491 
492  if(p==NULL)
493  continue;
494 
495  strcpy(name,dbuf);
496 
497  for(i=0;i<3;i++)
498  name[i]=tolower(name[i]);
499 
500  name[3]=0;
501 
502  strcpy(nexname[l],name);
503 
504  p=pars_line(kbuf,"latitude=",dbuf);
505 
506  if(p==NULL)
507  continue;
508 
509  lat=atof(p);
510 
511  p=pars_line(kbuf,"longitude=",dbuf);
512 
513  if(p==NULL)
514  continue;
515 
516  lon=atof(p);
517 
518  if(lon > 0)
519  lon=-lon;
520 
521  /* get hrap coordinates of radar */
522 
524 
525  cx=(int)hrap.x+1;
526  cy=(int)hrap.y;
527 
528  for (i=0; i < 131; i++){
529  for (k=0; k < 131 ;k++){
530 
531 
532  irap.x=cx + (float)(k - 65);
533  irap.y=cy + (float)(i-65);
534 
535  nrapx[i][k]=irap.x;
536  nrapy[i][k]=irap.y;
537 
538  }
539 
540  }
541 
542  sprintf(fbuf,"/usr/mapper/nexrad/raw/%s.%02d-%02d-%02d-%02d",
543  name,iyear,imonth,iday,ihour);
544 
545  fq=fopen(fbuf,"rb");
546 
547  /* what if missing ?? */
548 
549  if(fq==NULL) {
550 
551  printf("could not find %s\n",fbuf);
552  continue;
553 
554  }
555 
556  fread(&nexrad,sizeof(char),131*131,fq);
557 
558  fclose(fq);
559 
560  for (i=0; i < 131; i++) {
561  for (j=0; j < 131 ;j++) {
562 
563  ii=nrapx[i][j]-iminx;
564  jj=nrapy[i][j]-iminy;
565 
566  if(ii < 0 || ii > maxi ||
567  jj < 0 || jj > maxj)
568  continue;
569 
570  if(nexrad.value[i][j] == 255)
571  continue;
572 
573  if(pnexrad[nexrad.value[i][j]] >
574  val[ii][jj])
575  val[ii][jj]=pnexrad[nexrad.value[i][j]];
576 
577  }
578 
579  }
580 
581  }
582 
583 
584 /* now add up all numbers and divide by total contributions */
585 
586 fclose(fp);
587 
588 sprintf(fbuf,"/usr/mapper/nexrad/%02d/%02d/ngrid.%02d-%02d-%02d-%02d",
589 iyear,imonth,iyear,imonth,iday,ihour);
590 
591 printf("fbuf is %s\n",fbuf);
592 
593 fp=fopen(fbuf,"w");
594 
595 fprintf(fp,"%d %d %d %d 1\n",iminx,iminy,maxi,maxj);
596 
597 for(i=0;i<maxi;i++) {
598 
599 for(j=0;j<maxj;j++) {
600 
601 
602  if(val[i][j] >=0)
603  irain=val[i][j]* 100;
604 
605  else
606  irain=-9999;
607 
608  fprintf(fp," %5d",irain);
609 
610  }
611 
612  }
613 
614 fclose(fp);
615 
616 sprintf(tarbuf,"/usr/local/bin/gzip -f %s",fbuf);
617 printf("tarbuf %s\n",tarbuf);
618 ier=system(tarbuf);
619 printf("ieris %d %d\n",ier,errno);
620 
621 }
622 
623 
624 char *convertJulianDate(jdate)
625  short jdate;
626 {
627  char cdate[7];
628  int days[]={31,28,31,30,31,30,31,31,30,31,30,31} ;
629  int year,month,ndays,date,total,nyear,leap_year;
630  total=0;
631  for (year=1970;year<2010;year++)
632  {
633  leap_year=FALSE;
634  if (year==1972 || year==1976 || year==1980 ||
635  year==1984 || year==1988 || year==1992 || year==1996 ||
636  year==2000 || year==2004 || year==2008)
637  leap_year=TRUE;
638  for (month=0;month<12;month++)
639  {
640  total = total+days[month];
641  if (month==1 && leap_year==TRUE)
642  total++;
643  if (total >= jdate)
644  {
645  ndays=days[month];
646  if (month==1 && leap_year==TRUE) ndays++;
647  date=ndays - (total-jdate);
648  month=month+1;
649  nyear=year-1900;
650  if (year > 2000) nyear=year-2000;
651  sprintf(cdate,"%02d%02d%02d",month,date,nyear);
652  return cdate;
653  }
654  }
655  }
656 }
657 
658 
659 
660 
661 
662 
663 
664 
665 
666 
667 
668 
669 
670 
671 
672 
673 
674 
675 
676 
677 
678 
679 
680 
681 
682 
683 
684 
685 
686 
687 
688 
689 
690 
691 
char name[35][30]
Definition: borshef.c:10
static int i
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
struct HRAP LatLongToHrap(float lat, float lon)
Definition: hrap.c:105
char * pars_line(char *buf, char *s, char *sbuf)
Definition: pars_line.c:3
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
system(tarbuf)
fprintf(fp,"%d %d %d %d 1\n", iminx, iminy, maxi, maxj)
char * convertJulianDate()
char fbuf[100]
Definition: make_nexrad.c:2
main(int argc, char **argv)
Definition: make_nexrad.c:13
char database_directory[100]
Definition: make_nexrad.c:4
char directory[100]
Definition: make_nexrad.c:3
#define TRUE
Definition: make_nexrad.c:8
#define FALSE
Definition: make_nexrad.c:9
char nexrad_directory[100]
Definition: make_nexrad.c:5
int j
Definition: mapp2h.h:48
double xlat
Definition: mapp2h.h:41
double lat
Definition: mapp2h.h:41
double xlon
Definition: mapp2h.h:41
double lon
Definition: mapp2h.h:41
int k
Definition: mapp2h.h:48
HRAP hrap
Definition: mapp2h.h:53
float value
Dimension height
char fname[100]
Definition: send_afos.c:6
Definition: mapp2h.h:25
float y
Definition: mapp2h.h:26
float x
Definition: mapp2h.h:26
Definition: misc.h:487
unsigned char value[131][131]
Definition: misc.h:489