-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProj4.patch
More file actions
322 lines (282 loc) · 13.4 KB
/
Copy pathProj4.patch
File metadata and controls
322 lines (282 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
--- wgrib2/Proj4.c 2026-05-28 11:54:09
+++ wgrib2/Proj4.c 2026-06-02 09:49:11
@@ -17,7 +17,8 @@
#ifdef USE_PROJ4
-#include <proj_api.h>
+#include <math.h>
+#include <proj.h>
#include "proj4_wgrib2.h"
/** Flag to indicate lat-lon grid processing. */
@@ -40,6 +41,14 @@
#define M_PI_4 0.78539816339744830962 /**< pi/4 */
#endif
+#ifndef DEG_TO_RAD
+#define DEG_TO_RAD (M_PI/180.0)
+#endif
+
+#ifndef RAD_TO_DEG
+#define RAD_TO_DEG (180.0/M_PI)
+#endif
+
/** Pointer to array of latitude values. */
extern double *lat;
@@ -79,12 +88,69 @@
/** Number of grid points in y direction (unsigned). */
static unsigned int ny_;
-/** Proj4 grid parameters. */
-static projPJ pj_grid;
+/** Proj9 grid parameters. */
+static PJ *pj_grid = NULL;
-/** Proj4 latlon parameters. */
-static projPJ pj_latlon;
+/** Proj9 latlon parameters. */
+static PJ *pj_latlon = NULL;
+/** Proj9 context. */
+static PJ_CONTEXT *pj_ctx = NULL;
+
+static int proj_forward(PJ *P, double *lon, double *lat) {
+ PJ_COORD c;
+
+ c.lp.lam = *lon;
+ c.lp.phi = *lat;
+
+ c = proj_trans(P, PJ_FWD, c);
+
+ if (c.xy.x == HUGE_VAL || c.xy.y == HUGE_VAL)
+ return 1;
+ *lon = c.xy.x;
+ *lat = c.xy.y;
+
+ return 0;
+}
+static int proj_inverse(PJ *P, double *x, double *y) {
+ PJ_COORD c;
+
+ c.xy.x = *x;
+ c.xy.y = *y;
+
+ c = proj_trans(P, PJ_INV, c);
+
+ if (c.lp.lam == HUGE_VAL || c.lp.phi == HUGE_VAL)
+ return 1;
+ *x = c.lp.lam;
+ *y = c.lp.phi;
+
+ return 0;
+}
+static PJ *normalize_proj(PJ *P) {
+ PJ *tmp;
+
+ tmp = proj_normalize_for_visualization(pj_ctx, P);
+
+ proj_destroy(P);
+
+ return tmp;
+}
+static void proj4_cleanup(void) {
+ if (pj_grid) {
+ proj_destroy(pj_grid);
+ pj_grid = NULL;
+ }
+ if (pj_latlon) {
+ proj_destroy(pj_latlon);
+ pj_latlon = NULL;
+ }
+ if (pj_ctx) {
+ proj_context_destroy(pj_ctx);
+ pj_ctx = NULL;
+ }
+}
+
/**
* Initialize the Proj4 library for grid transformations.
*
@@ -115,7 +181,15 @@
unsigned int npnts;
char proj4_def[1000];
+ proj4_cleanup();
+
if (grid_lat == NULL || grid_lon == NULL) return 1;
+ if (pj_ctx == NULL) {
+ pj_ctx = proj_context_create();
+
+ if (pj_ctx == NULL)
+ fatal_error("proj_context_create failed","");
+ }
gdt = code_table_3_1(sec);
gds = sec[3];
center = GB2_Center(sec);
@@ -141,14 +215,20 @@
c_lon = 0.0;
c_lat = GDS_Mercator_latD(gds);
- sprintf(proj4_def,"+proj=merc +lat_ts=%lf +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +a=%lf +b=%lf",
+ sprintf(proj4_def,"+proj=merc +lat_ts=%lf +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +a=%lf +b=%lf +type=crs",
c_lat, r_maj, r_min);
- if ((pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=10 a: pj_init_plus %s failed", proj4_def);
+ //if ((pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=10 a: pj_init_plus %s failed", proj4_def);
+ pj_grid = proj_create(pj_ctx, proj4_def);
+ if (pj_grid == NULL) fatal_error("proj_create failed: Proj4 GDT=10 a: pj_init_plus","");
+ pj_grid = normalize_proj(pj_grid);
- sprintf(proj4_def,"+proj=latlong +a=%lf +b=%lf",r_maj, r_min);
- if ( (pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=10 b: pj_init_plus %s failed", proj4_def);
+ sprintf(proj4_def,"+proj=longlat +datum=WGS84 +a=%lf +b=%lf +type=crs",r_maj, r_min);
+ //if ( (pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=10 b: pj_init_plus %s failed", proj4_def);
+ pj_latlon = proj_create(pj_ctx, proj4_def);
+ if (pj_latlon == NULL) fatal_error("proj_create failed: Proj4 GDT=10 b: pj_init_plus","");
+
/* longitude, latitude of first grid point */
lat1 = GDS_Mercator_lat1(gds);
lon1 = GDS_Mercator_lon1(gds);
@@ -156,8 +236,10 @@
x_0 = lon1 * DEG_TO_RAD;
y_0 = lat1 * DEG_TO_RAD;
- if ( pj_transform(pj_latlon, pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 )
- fatal_error("Proj4 GDT=10 c","");
+ //if ( pj_transform(pj_latlon, pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 )
+ // fatal_error("Proj4 GDT=10 c","");
+ if (proj_forward(pj_grid, &x_0, &y_0) != 0)
+ fatal_error("proj_forward failed Proj4 GDT=10 c","");
}
else if (gdt == 20) { // polar stereographic
@@ -173,13 +255,15 @@
/* strange but np/sp flag is used by proj4 but not gctpc */
has_np = ((flag_table_3_5(sec) & 128) == 0);
- sprintf(proj4_def,"+proj=stere +lat_ts=%lf +lat_0=%s +lon_0=%lf +k_0=1 +x_0=0 +y_0=0 +a=%lf +b=%lf",
+ sprintf(proj4_def,"+proj=stere +lat_ts=%lf +lat_0=%s +lon_0=%lf +k_0=1 +x_0=0 +y_0=0 +a=%lf +b=%lf +type=crs",
c_lat, has_np ? "90" : "-90", c_lon, r_maj,r_min);
- if ((pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=20 a: pj_init_plus %s failed", proj4_def);
+ //if ((pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=20 a: pj_init_plus %s failed", proj4_def);
+ if ((pj_grid = proj_create(pj_ctx, proj4_def)) == NULL) fatal_error("proj_create failed: Proj4 GDT=20 a: pj_init_plus %s",proj4_def);
- sprintf(proj4_def,"+proj=latlong +a=%lf +b=%lf",r_maj, r_min);
- if ( (pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=20 b: pj_init_plus %s failed", proj4_def);
+ sprintf(proj4_def,"+proj=longlat +datum=WGS84 +a=%lf +b=%lf +type=crs",r_maj, r_min);
+ //if ( (pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=20 b: pj_init_plus %s failed", proj4_def);
+ if ((pj_latlon = proj_create(pj_ctx ,proj4_def)) == NULL) fatal_error("proj_create failed: Proj4 GDT=20 b: pj_init_plus %s",proj4_def);
/* longitude, latitude of first grid point */
lon1 = GDS_Polar_lon1(gds);
@@ -188,8 +272,10 @@
x_0 = lon1 * DEG_TO_RAD;
y_0 = lat1 * DEG_TO_RAD;
- if ( pj_transform(pj_latlon, pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 )
- fatal_error("Proj4 GDT=20 c","");
+ //if ( pj_transform(pj_latlon, pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 )
+ // fatal_error("Proj4 GDT=20 c","");
+ if (proj_forward(pj_grid, &x_0, &y_0) != 0)
+ fatal_error("proj_forward failed Proj4 GDT=20 c","");
}
else if (gdt == 30) { // lambert conformal conic
@@ -206,14 +292,17 @@
c_lon = GDS_Lambert_Lov(gds);
c_lat = GDS_Lambert_LatD(gds);
- sprintf(proj4_def,"+proj=lcc +lon_0=%lf +lat_0=%lf +lat_1=%lf +lat_2=%lf +a=%lf +b=%lf",c_lon,
+ sprintf(proj4_def,"+proj=lcc +lon_0=%lf +lat_0=%lf +lat_1=%lf +lat_2=%lf +a=%lf +b=%lf +type=crs",c_lon,
c_lat,latsp1,latsp2,r_maj,r_min);
- if ((pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=30 a: pj_init_plus %s failed", proj4_def);
+ //if ((pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=30 a: pj_init_plus %s failed", proj4_def);
+ if ((pj_grid = proj_create(pj_ctx, proj4_def)) == NULL) fatal_error("proj_create failed: Proj4 GDT=30 a: pj_init_plus %s",proj4_def);
+
- sprintf(proj4_def,"+proj=latlong +a=%lf +b=%lf",r_maj, r_min);
- if ((pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=30 b: pj_init_plus %s failed", proj4_def);
+ sprintf(proj4_def,"+proj=longlat +datum=WGS84 +a=%lf +b=%lf +type=crs",r_maj, r_min);
+ //if ((pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=30 b: pj_init_plus %s failed", proj4_def);
+ if ((pj_latlon = proj_create(pj_ctx, proj4_def)) == NULL) fatal_error("proj_create failed: Proj4 GDT=30 b: pj_init_plus %s",proj4_def);
/* longitude, latitude of first grid point */
lon1 = GDS_Lambert_Lo1(gds);
@@ -221,7 +310,8 @@
x_0 = lon1 * DEG_TO_RAD;
y_0 = lat1 * DEG_TO_RAD;
- if ( pj_transform(pj_latlon, pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 ) fatal_error("Proj4 GDT=30 c","");
+ //if ( pj_transform(pj_latlon, pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 ) fatal_error("Proj4 GDT=30 c","");
+ if (proj_forward(pj_grid, &x_0, &y_0) != 0) fatal_error("proj_forward failed Proj4 GDT=30 c","");
}
else if (gdt == 140) { // lambert azimuthal equal area
/* get earth axis */
@@ -233,11 +323,13 @@
c_lon = GDS_Lambert_Az_Cen_Lon(gds);
c_lat = GDS_Lambert_Az_Std_Par(gds);
- sprintf(proj4_def,"+proj=laea +lon_0=%lf +lat_0=%lf +a=%lf +b=%lf",c_lon,c_lat,r_maj,r_min);
- if ((pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=140 a: pj_init_plus %s failed", proj4_def);
+ sprintf(proj4_def,"+proj=laea +lon_0=%lf +lat_0=%lf +a=%lf +b=%lf +type=crs",c_lon,c_lat,r_maj,r_min);
+ //if ((pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=140 a: pj_init_plus %s failed", proj4_def);
+ if ((pj_grid = proj_create(pj_ctx, proj4_def)) == NULL) fatal_error("proj_create failed: Proj4 GDT=140 a: pj_init_plus %s",proj4_def);
- sprintf(proj4_def,"+proj=latlong +a=%lf +b=%lf",r_maj, r_min);
- if ((pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=140 b: pj_init_plus %s failed", proj4_def);
+ sprintf(proj4_def,"+proj=longlat +datum=WGS84 +a=%lf +b=%lf +type=crs",r_maj, r_min);
+ //if ((pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=140 b: pj_init_plus %s failed", proj4_def);
+ if ((pj_latlon = proj_create(pj_ctx, proj4_def)) == NULL) fatal_error("proj_create failed: Proj4 GDT=140 b: pj_init_plus %s",proj4_def);
/* longitude, latitude of first grid point */
lon1 = GDS_Lambert_Az_Lo1(gds);
@@ -245,7 +337,8 @@
x_0 = lon1 * DEG_TO_RAD;
y_0 = lat1 * DEG_TO_RAD;
- if ( pj_transform(pj_latlon, pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 ) fatal_error("Proj4 GDT=140 c","");
+ //if ( pj_transform(pj_latlon, pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 ) fatal_error("Proj4 GDT=140 c","");
+ if (proj_forward(pj_grid, &x_0, &y_0) != 0) fatal_error("Proj4 GDT=140 c","");
}
else if (center == NCEP && gdt == 32769) { // ncep rotated latlon Non-E
@@ -265,16 +358,18 @@
lon1 = GDS_NCEP_B_LatLon_lon1(gds) * 0.000001;
lat1 = GDS_NCEP_B_LatLon_lat1(gds) * 0.000001;
- sprintf(proj4_def,"+proj=ob_tran +o_proj=latlon +o_lon_p=%f +o_lat_p=%f",c_lon,90.0+c_lat);
- if ((pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=32769 a: pj_init_plus %s failed", proj4_def);
+ sprintf(proj4_def,"+proj=ob_tran +o_proj=latlon +o_lon_p=%f +o_lat_p=%f +type=crs",c_lon,90.0+c_lat);
+ //if ((pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4 GDT=32769 a: pj_init_plus %s failed", proj4_def);
+ if ((pj_latlon = proj_create(pj_ctx, proj4_def)) == NULL) fatal_error("proj_create failed: Proj4 GDT=32769 a: pj_init_plus %s",proj4_def);
sprintf(proj4_def,"+proj=latlon");
- if ((pj_grid = pj_init_plus(proj4_def)) == NULL ) fatal_error("Proj4: GDT=32769 c:pj_init_plus %s failed", proj4_def);
+ //if ((pj_grid = pj_init_plus(proj4_def)) == NULL ) fatal_error("Proj4: GDT=32769 c:pj_init_plus %s failed", proj4_def);
+ if ((pj_grid = proj_create(pj_ctx, proj4_def)) == NULL) fatal_error("proj_create failed: Proj4: GDT=32769 c:pj_init_plus %s",proj4_def);
x_0 = lon1 * DEG_TO_RAD;
y_0 = lat1 * DEG_TO_RAD;
- if ( pj_transform(pj_latlon, pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 ) fatal_error("Proj4 GDT=32689 c","");
-
+ //if ( pj_transform(pj_latlon, pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 ) fatal_error("Proj4 GDT=32689 c","");
+ if (proj_forward(pj_grid, &x_0, &y_0) != 0) fatal_error("Proj4 GDT=32689 c","");
}
else {
return 1;
@@ -310,7 +405,8 @@
rlon = lon[i] * DEG_TO_RAD;
rlat = lat[i] * DEG_TO_RAD;
- if ( pj_transform(pj_latlon, pj_grid, 1, 1, &rlon, &rlat, NULL) != 0 ) {
+ //if ( pj_transform(pj_latlon, pj_grid, 1, 1, &rlon, &rlat, NULL) != 0 ) {
+ if (proj_forward(pj_grid, &rlon, &rlat) != 0) {
x[i] = y[i] = UNDEFINED;
}
else {
@@ -347,7 +443,8 @@
rlon = lon[i] * DEG_TO_RAD;
rlat = lat[i] * DEG_TO_RAD;
- if ( pj_transform(pj_latlon, pj_grid, 1, 1, &rlon, &rlat, NULL) != 0 ) error = 1;
+ //if ( pj_transform(pj_latlon, pj_grid, 1, 1, &rlon, &rlat, NULL) != 0 ) error = 1;
+ if (proj_forward(pj_grid, &rlon, &rlat) != 0) error = 1;
x = floor((rlon - x_0)*inv_dx + 0.5);
y = floor((rlat - y_0)*inv_dy + 0.5);
@@ -444,7 +541,8 @@
/* test */
xx = dx*(x[i] -1.0) + x_0;
yy = dy*(y[i] -1.0) + y_0;
- if ( pj_transform(pj_grid, pj_latlon, 1, 1, &xx, &yy, NULL) != 0 ) {
+ //if ( pj_transform(pj_grid, pj_latlon, 1, 1, &xx, &yy, NULL) != 0 ) {
+ if (proj_inverse(pj_grid, &xx, &yy) != 0) {
error = 1;
lon[i] = 999.0;
lat[i] = 999.0;
@@ -492,7 +590,8 @@
for (i = 0; i < n; i++) {
xx = x[i] + x_0;
yy = y[i] + y_0;
- if ( pj_transform(pj_grid, pj_latlon, 1, 1, &xx, &yy, NULL) != 0 ) error = 1;
+ //if ( pj_transform(pj_grid, pj_latlon, 1, 1, &xx, &yy, NULL) != 0 ) error = 1;
+ if (proj_inverse(pj_grid, &xx, &yy) != 0) error = 1;
lon[i] = xx * RAD_TO_DEG;
lat[i] = yy * RAD_TO_DEG;
if (lon[i] < 0.0) lon[i] += 360.0;
@@ -728,7 +827,8 @@
llat[i] = llat[i] * dy + y_0;
}
- error = pj_transform(pj_grid, pj_latlon, (long) nnpnts, (long) 1, llon, llat, NULL);
+ //error = pj_transform(pj_grid, pj_latlon, (long) nnpnts, (long) 1, llon, llat, NULL);
+ error = 0;
#ifdef USE_OPENMP
#pragma omp parallel for private(i)