Mapper
get_coord.c
Go to the documentation of this file.
1 #include "prototypes.h"
2 
3 void get_coord(double maximum_latitude,double minimum_latitude,double center_longitude)
4 
5 {
6 
7  int i,k,maxi,maxj;
8  struct HRAP hrap;
9  struct HRAP irap;
10  float cx,cy;
11  extern struct h_grid *h_grid;
12  extern struct top *topo;
13  int mini,minj,ix,iy,elevation,kk,jj;
14  float lat,lon,distance,value,dist1,dist2,dist;
15 
16  h_grid=(struct h_grid *) calloc(1,sizeof(struct h_grid));
17  if(h_grid==NULL) {
18 
19  printf("could not allocate h_grid space\n");
20  exit(1);
21 
22  }
23 
24  maxi=-1;maxj=-1;mini=999999;minj=99999;
25 
26  hrap=LatLongToHrap((float)maximum_latitude,(float)center_longitude-6.0);
27 
28  cx=(int)hrap.x+1;
29  cy=(int)hrap.y;
30 
31  if(cx > maxi)
32  maxi=cx;
33 
34  if(cy > maxj)
35  maxj=cy;
36 
37  if(cx < mini)
38  mini=cx;
39 
40  if(cy < minj)
41  minj=cy;
42 
43  hrap=LatLongToHrap((float)maximum_latitude,(float)center_longitude+6.0);
44 
45  cx=(int)hrap.x+1;
46  cy=(int)hrap.y;
47 
48  if(cx > maxi)
49  maxi=cx;
50 
51  if(cy > maxj)
52  maxj=cy;
53 
54  if(cx < mini)
55  mini=cx;
56 
57  if(cy < minj)
58  minj=cy;
59 
60  hrap=LatLongToHrap((float)minimum_latitude,(float)center_longitude-6.0);
61 
62  cx=(int)hrap.x+1;
63  cy=(int)hrap.y;
64 
65  if(cx > maxi)
66  maxi=cx;
67 
68  if(cy > maxj)
69  maxj=cy;
70 
71  if(cx < mini)
72  mini=cx;
73 
74  if(cy < minj)
75  minj=cy;
76 
77  hrap=LatLongToHrap((float)minimum_latitude,(float)center_longitude+6.0);
78 
79  cx=(int)hrap.x+1;
80  cy=(int)hrap.y;
81 
82  if(cx > maxi)
83  maxi=cx;
84 
85  if(cy > maxj)
86  maxj=cy;
87 
88  if(cx < mini)
89  mini=cx;
90 
91  if(cy < minj)
92  minj=cy;
93 
94 /*allocate necessary space */
95 
96 h_grid->maxi=maxi-mini;
97 h_grid->maxj=maxj-minj;
98 
100 maxj=h_grid->maxj;
101 
102 h_grid->box=(struct coord **) calloc(maxi,sizeof(struct coord *));
103 h_grid->elev=(short int**) calloc(maxi,sizeof(short int *));
104 h_grid->latlon=(struct HRAP **) calloc(maxi,sizeof(struct HRAP *));
105 
106 if(h_grid->box==NULL || h_grid->elev==NULL || h_grid->latlon==NULL) {
107 
108  printf("no memory for hrap array\n");
109  exit(1);
110 
111  }
112 
113 for(i=0;i<maxi;i++) {
114 
115  h_grid->box[i]=(struct coord *) calloc(maxj,sizeof(struct coord));
116  h_grid->elev[i]=(short int *) calloc(maxj,sizeof(short int));
117  h_grid->latlon[i]=(struct HRAP *) calloc(maxj,sizeof(struct HRAP));
118 
119  if(h_grid->box[i]==NULL ||
120  h_grid->elev[i]==NULL || h_grid->latlon[i]==NULL) {
121 
122  printf("no memory for hrap array\n");
123  exit(1);
124 
125  }
126 
127  }
128 
129 for (i=0; i < h_grid->maxi-1; i++)
130  {
131 for (k=0; k < h_grid->maxj-1 ;k++)
132  {
133 
134  irap.x=mini+i;
135  irap.y=minj+k;
136 
137  h_grid->box[i][k].x=(short int)irap.x;
138  h_grid->box[i][k].y=(short int)irap.y;
139 
140  hrap=HrapToLatLong(irap);
141 
142  h_grid->latlon[i][k].x=hrap.x;
143  h_grid->latlon[i][k].y=hrap.y;
144 /*
145 printf("%d %d %f %f\n",(short int)irap.x,(short int)irap.y,hrap.x,hrap.y);
146 */
147 
148  iy=(topo->max_lat-hrap.y)/topo->delta_lat;
149  ix=(topo->max_lon-hrap.x)/topo->delta_lon;
150 
151  if(ix <= 0 || iy <= 0 || ix >= topo->maxi || iy>= topo->maxj) {
152 
153  h_grid->elev[i][k]=-1;
154  continue;
155 
156  }
157 
158  distance=0.0;
159  value=0.0;
160 
161  for(kk=ix;kk<=ix+1;kk++) {
162 
163  for(jj=iy;jj<=iy+1;jj++) {
164 
165  lat=topo->max_lat-(float)jj * topo->delta_lat;
166  lon=topo->max_lon-(float)kk * topo->delta_lon;
167 
168  dist1=irap.y-lat;
169  dist2=irap.x-lon;
170 
171  dist=pow(dist1,2)+pow(dist2,2);
172 
173  if(dist==0.0)
174  dist=1.0;
175 
176  dist=1/dist;
177 
178  value=value+dist*(float)topo->value[kk][jj];
179  distance=distance+dist;
180 
181  }
182  }
183 
184  elevation=value/distance;
185  h_grid->elev[i][k]=elevation;
186 
187 if(elevation > 500)
188  printf("bad elevation %d %d %d %f %f\n",i,k,elevation,hrap.x,hrap.y);
189 
190 
191 
192  }
193 
194  }
195 
196 return;
197 
198 }
199 
200 
201 
struct top * topo
Definition: build_hrap.c:3
struct h_grid * h_grid
Definition: build_hrap.c:4
static int i
void get_coord(double maximum_latitude, double minimum_latitude, double center_longitude)
Definition: get_coord.c:3
struct HRAP LatLongToHrap(float lat, float lon)
Definition: hrap.c:105
struct HRAP HrapToLatLong(struct HRAP hrap)
Definition: hrap.c:40
printf("fbuf is %s\n", fbuf)
double lat
Definition: mapp2h.h:41
double lon
Definition: mapp2h.h:41
int k
Definition: mapp2h.h:48
HRAP hrap
Definition: mapp2h.h:53
float value
Definition: mapp2h.h:25
float y
Definition: mapp2h.h:26
float x
Definition: mapp2h.h:26
Definition: misc.h:495
short int y
Definition: misc.h:498
short int x
Definition: misc.h:497
Definition: misc.h:554
short int maxj
Definition: misc.h:557
struct HRAP ** latlon
Definition: misc.h:559
struct coord ** box
Definition: misc.h:558
short int ** elev
Definition: misc.h:560
short int maxi
Definition: misc.h:556
Definition: misc.h:509
Definition: misc.h:523
float delta_lat
Definition: misc.h:533
float max_lat
Definition: misc.h:529
int maxi
Definition: misc.h:527
float delta_lon
Definition: misc.h:534
short int ** value
Definition: misc.h:526
float max_lon
Definition: misc.h:530
int maxj
Definition: misc.h:528