Mapper
get_valid_gages.c
Go to the documentation of this file.
1 #define _POSIX_SOURCE
2 
3 #include "prototypes_new.h"
4 
5 void get_valid_gages(double maximum_latitude,double minimum_latitude,
6  double center_longitude)
7 
8 {
9 
10  extern char station_list_file[100];
11  extern char forecast_station_list_file[100];
12  extern char hrap_gage_file[100];
13  extern int wfo_orig;
14  extern int max_fstations;
15  extern struct fstation fstation[500];
16  extern struct station station[3000];
17  int i,k,kk,maxi,maxj,l,m,h;
18  struct HRAP hrap;
19  struct HRAP irap;
20  char dbuf[200];
21  extern struct hrap_grid *hrap_grid;
22  float conv=.0174,totlat,sorted[20];
23  float dist,dist1,dist2,distance,value;
24  float isohlat,isohlon;
25  int jj,latindex,lonindex;
26  int mini,minj;
27  int mm,hh;
28  struct stat statbuf;
29  int newflag;
30  time_t ltime1,ltime2;
31  time_t oltime1,oltime2;
32  int ier,mer;
33  FILE *fz;
34  char *p;
35  double omaximum_latitude,ominimum_latitude,ocenter_longitude;
36  char kbuf[200];
37 
38  mini=hrap_grid->hrap_minx;
39  minj=hrap_grid->hrap_miny;
40 
41  newflag=0;
42 
43  if(hrap_gage_file[0]==0)
44  newflag=1;
45 
46  else {
47 
48  ier=stat(hrap_gage_file,&statbuf);
49  if(ier==-1)
50  newflag=1;
51 
52  }
53 
54 
55  mer=stat(station_list_file,&statbuf);
56 
57  if(mer==-1)
58  ltime1=-1;
59 
60  else
61  ltime1=statbuf.st_mtime;
62 
63  mer=stat(forecast_station_list_file,&statbuf);
64 
65  if(mer==-1)
66  ltime2=-1;
67 
68  else
69  ltime2=statbuf.st_mtime;
70 
71 
72  if(newflag==0) {
73 
74  fz=fopen(hrap_gage_file,"r");
75 
76  p=fgets(dbuf,100,fz);
77 
78  ier=sscanf(dbuf,"%ld %ld %lf %lf %lf",&oltime1,&oltime2,&omaximum_latitude,
79  &ominimum_latitude,&ocenter_longitude);
80 
81 
82  if(oltime1 != ltime1 || oltime2 != ltime2 ||
83  omaximum_latitude != maximum_latitude ||
84  ominimum_latitude != minimum_latitude ||
85  ocenter_longitude != center_longitude)
86  newflag=1;
87 
88  fclose(fz);
89 
90 
91  }
92 
93 if(newflag==0) {
94 
95  fz=fopen(hrap_gage_file,"r");
96  p=fgets(dbuf,100,fz);
97 
98  }
99 
100 else if(newflag==1) {
101 
102  fz=fopen(hrap_gage_file,"w+");
103 
104  fprintf(fz,"%ld %ld %f %f %f\n",ltime1,ltime2,maximum_latitude,
105  minimum_latitude,center_longitude);
106 
107  }
108 
109 
110 
111  for (i=0; i < hrap_grid->maxi; i++)
112  {
113  for (k=0; k < hrap_grid->maxj ;k++)
114  {
115 
116  irap.x=mini+i;
117  irap.y=minj+k;
118 
119  hrap=HrapToLatLong(irap);
120  /* we have lat long of point we need seasonal isohyet */
121 
122  for(l=0;l<5;l++)
123  sorted[l]=9999999;
124 
125  if(newflag==0){
126 
127  m=0;
128  fread(kbuf,sizeof(char),50,fz);
129  kbuf[50]=0;
130  for(mm=0;mm<10;mm++) {
131 
132  hrap_grid->gage[i][k].index[mm]=atoi(&kbuf[m]);
133 
134  m=m+5;
135 
136 
137  }
138 
139 
140  }
141 
142  else {
143 
144  for(mm=0;mm<max_fstations;mm++) {
145 
146  if(fstation[mm].owner != wfo_orig)
147  continue;
148 
149  if(fstation[mm].qpf==-1)
150  continue;
151 
152  m=fstation[mm].pindex;
153 
154  dist1=hrap.y-station[m].lat;
155  dist2=hrap.x-station[m].lon;
156 
157  dist=pow(dist1,2)+pow(dist2,2);
158 
159  for(l=0;l<5;l++) {
160 
161  if(dist < sorted[l]) {
162 
163  for(hh=4; hh > l; hh--) {
164 
165  sorted[hh]=sorted[hh-1];
166 
167 
168  hrap_grid->gage[i][k].index[hh]=
169  hrap_grid->gage[i][k].index[hh-1];
170 
171  }
172 
173  sorted[l]=dist;
174 
175  hrap_grid->gage[i][k].index[l]=m;
176 
177  break;
178 
179  }
180 
181 
182  }
183 
184 
185  }
186 
187  for(l=5;l<10;l++)
188  sorted[l]=9999999;
189 
190 
191  for(mm=0;mm<max_fstations;mm++) {
192 
193  if(fstation[mm].qpf==-1)
194  continue;
195 
196  m=fstation[mm].pindex;
197 
198  dist1=hrap.y-station[m].lat;
199  dist2=hrap.x-station[m].lon;
200 
201  dist=pow(dist1,2)+pow(dist2,2);
202 
203  for(l=0;l<5;l++) {
204 
205  if(dist < sorted[l+5]) {
206 
207  for(hh=9; hh > l+5; hh--) {
208 
209  sorted[hh]=sorted[hh-1];
210 
211  hrap_grid->gage[i][k].index[hh]=
212  hrap_grid->gage[i][k].index[hh-1];
213 
214 
215  }
216 
217  sorted[l+5]=dist;
218 
219  hrap_grid->gage[i][k].index[l+5]=m;
220 
221  break;
222 
223  }
224 
225 
226  }
227 
228 
229  }
230 
231  for(l=0;l<10;l++)
232  fprintf(fz,"%04d ",hrap_grid->gage[i][k].index[l]);
233 
234  }
235 
236  }
237 
238 }
239 
240 fclose(fz);
241 
242 return;
243 }
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
static int i
void get_valid_gages(double maximum_latitude, double minimum_latitude, double center_longitude)
struct HRAP HrapToLatLong(struct HRAP hrap)
Definition: hrap.c:40
fclose(fp)
fprintf(fp,"%d %d %d %d 1\n", iminx, iminy, maxi, maxj)
int k
Definition: mapp2h.h:48
HRAP hrap
Definition: mapp2h.h:53
char station_list_file[256]
Definition: mapper.c:18
float value
struct hrap_grid * hrap_grid
Definition: mapper.c:142
Definition: mapp2h.h:25
float y
Definition: mapp2h.h:26
float x
Definition: mapp2h.h:26
Definition: misc.h:30
int pindex
Definition: misc_new.h:94
short int index[20]
Definition: misc.h:592
short int hrap_miny
Definition: misc.h:583
short int hrap_minx
Definition: misc.h:582
short int maxi
Definition: misc.h:580
short int maxj
Definition: misc.h:581
struct gage ** gage
Definition: misc.h:585
Definition: misc.h:296
float lat
Definition: misc.h:303
float lon
Definition: misc.h:304