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