Daily_QC
get_zgage_grid.c
Go to the documentation of this file.
1 #define _POSIX_SOURCE
2 #include "prototypes_new.h"
3 
4 void get_zgage_grid(double maximum_latitude,double minimum_latitude,
5  double center_longitude)
6 
7 {
8 
9  extern char station_list_file[100];
10  extern char hrap_zgage_file[100];
11  extern struct station zstation[500];
12  extern struct station station[3000];
13  extern Widget scrollbar;
14  extern Widget top_level;
15  extern int posit;
16  extern int max_zstations;
17  extern int max_stations;
18  extern Widget s_text;
19  int i,k,kk,maxi,maxj,l,m,h;
20  struct HRAP hrap;
21  struct HRAP irap;
22  float cx,cy;
23  extern struct dval dval;
24  char dbuf[200];
25  extern struct hrap_grid *hrap_grid;
26  long r,s;
27  float conv=.0174,totlat,sorted[30];
28  float dist,dist1,dist2,distance,value;
29  float isohlat,isohlon;
30  int jj,latindex,lonindex;
31  int slider_size,scrollm,ivalue,increment,page_increment;
32  struct stat statbuf;
33  double omaximum_latitude,ominimum_latitude,ocenter_longitude;
34  int ier,newflag,mm,mer;
35  FILE *fz;
36  time_t ost_mtime;
37  char *p,kbuf[200];
38  struct tm *tm;
39 
40 newflag=0;
41 
42 if(hrap_zgage_file[0]==0)
43  strcpy(hrap_zgage_file,"map_zqc.grid");
44 
45 ier=stat(hrap_zgage_file,&statbuf);
46 
47 /* get creation time of station_list file */
48 
49 mer=stat(station_list_file,&statbuf);
50 
51 /* grid file does not exist */
52 
53 if(ier==-1)
54  newflag=2;
55 
56 else {
57 
58  fz=fopen(hrap_zgage_file,"r");
59 
60  p=fgets(dbuf,100,fz);
61 
62  ier=sscanf(dbuf,"%ld %lf %lf %lf",&ost_mtime,&omaximum_latitude,
63  &ominimum_latitude,&ocenter_longitude);
64 
65  tm=gmtime(&statbuf.st_mtime);
66 
67  if(ost_mtime != statbuf.st_mtime ||
68  omaximum_latitude != maximum_latitude ||
69  ominimum_latitude != minimum_latitude ||
70  ocenter_longitude != center_longitude)
71  newflag=1;
72 
73  fclose(fz);
74 
75  }
76 
77 if(newflag==0) {
78 
79  fz=fopen(hrap_zgage_file,"r");
80  p=fgets(dbuf,100,fz);
81 
82  }
83 
84 else if(newflag==1)
85  fz=fopen(hrap_zgage_file,"w+");
86 
87 else
88  fz=fopen(hrap_zgage_file,"w");
89 
90 if(newflag!=0) {
91 
92  strcpy(dbuf,"ZGage to grid recalculation\n");
93  posit=posit+strlen(dbuf);
94  XmTextInsert(s_text,posit,dbuf);
95 
96  XtVaGetValues(scrollbar,XmNmaximum,&scrollm,NULL);
97 
98  XmScrollBarGetValues(scrollbar,&ivalue,&slider_size,
99  &increment,&page_increment);
100 
101  XmScrollBarSetValues(scrollbar,scrollm-slider_size,
102  slider_size, increment,page_increment,True);
103 
104  XmUpdateDisplay(top_level);
105 
106  fprintf(fz,"%ld %f %f %f\n",statbuf.st_mtime,maximum_latitude,
107  minimum_latitude,center_longitude);
108 
109  }
110 
111 for (i=0; i < hrap_grid->maxi; i++){
112 
113 for (k=0; k < hrap_grid->maxj ;k++){
114 
115  irap.x=hrap_grid->hrap_minx+i;
116  irap.y=hrap_grid->hrap_miny+k;
117 
118  hrap=HrapToLatLong(irap);
119 
120  for(l=0;l<30;l++)
121  sorted[l]=9999999;
122 
123  if(newflag==0){
124 
125  m=0;
126  fread(kbuf,sizeof(char),25,fz);
127  kbuf[25]=0;
128  for(mm=0;mm<5;mm++) {
129 
130  hrap_grid->gage[i][k].zindex[mm]=atoi(&kbuf[m]);
131 
132  m=m+5;
133 
134 
135  }
136 
137 
138  }
139 
140  else {
141 
142  for(m=0;m<max_zstations;m++) {
143 
144  dist1=hrap.y-zstation[m].lat;
145  dist2=(hrap.x-zstation[m].lon)*cos((hrap.y+zstation[m].lat)/2*conv);
146 
147  dist=pow(dist1,2)+pow(dist2,2);
148 
149  for(l=0;l<5;l++) {
150 
151  if(dist < sorted[l]) {
152 
153  for(h=4; h > l; h--) {
154 
155  sorted[h]=sorted[h-1];
156 
157  hrap_grid->gage[i][k].zindex[h]=
158  hrap_grid->gage[i][k].zindex[h-1];
159 
160  }
161 
162  sorted[l]=dist;
163 
164  hrap_grid->gage[i][k].zindex[l]=m;
165 
166  break;
167 
168  }
169 
170 
171  }
172 
173  }
174 
175  for(l=0;l<5;l++)
176  fprintf(fz,"%04d ",hrap_grid->gage[i][k].zindex[l]);
177 
178  }
179 
180 
181  }
182 
183  }
184 
185 fclose(fz);
186 
187 for(i=0;i<max_stations;i++) {
188 
189  for(l=0;l<5;l++)
190  sorted[l]=9999999;
191 
192  for(m=0;m<max_zstations;m++) {
193 
194  if(i==m)
195  continue;
196 
197  dist1=station[i].lat-zstation[m].lat;
198  dist2=station[i].lon-zstation[m].lon;
199 
200  dist=pow(dist1,2)+pow(dist2,2);
201 
202  for(l=0;l<5;l++) {
203 
204  if(dist < sorted[l]) {
205 
206  for(h=4; h > l; h--) {
207 
208  sorted[h]=sorted[h-1];
209 
210  station[i].zindex[h]=
211  station[i].zindex[h-1];
212 
213  }
214 
215  sorted[l]=dist;
216 
217  station[i].zindex[l]=m;
218 
219  break;
220 
221  }
222 
223 
224  }
225 
226  }
227 
228  }
229 
230 return;
231 
232 }
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
int max_stations
Definition: daily_qc.c:199
char s
Definition: build_list.c:122
struct station zstation[500]
Definition: daily_qc.c:196
Widget top_level
Definition: daily_qc.c:214
char hrap_zgage_file[1000]
Definition: daily_qc.c:187
struct hrap_grid * hrap_grid
Definition: daily_qc.c:285
Widget scrollbar
Definition: daily_qc.c:215
int posit
Definition: daily_qc.c:255
int max_zstations
Definition: daily_qc.c:45
char station_list_file[1000]
Definition: daily_qc.c:173
Widget s_text
Definition: daily_qc.c:214
int maxj
int maxi
void get_zgage_grid(double maximum_latitude, double minimum_latitude, double center_longitude)
Definition: get_zgage_grid.c:4
struct HRAP HrapToLatLong(struct HRAP)
Definition: misc.h:407
float y
Definition: misc.h:410
float x
Definition: misc.h:409
Definition: misc.h:397
short int zindex[30]
Definition: misc_new.h:823
short int hrap_miny
Definition: misc.h:510
short int hrap_minx
Definition: misc.h:509
short int maxi
Definition: misc.h:507
short int maxj
Definition: misc.h:508
struct gage ** gage
Definition: misc.h:512
Definition: misc.h:232
float lat
Definition: misc.h:239
float lon
Definition: misc.h:240
short int * zindex
Definition: misc_new.h:473