Debian Codegraph @

gctp/src/isinusinv.c

raw
1 /******************************************************************************
2 NAME ISINUSINV.C
3
4 PURPOSE: Integerized Sinusoidal Library Functions - library routines to
5 perform mapping to and from the Integerized Sinusoidal. These
6 functions perform the mapping from projection coordinates (x/y)
7 to the geographic coordinates (longitude/latitude).
8
9 PROOGRAMMER DATE REASON
10 ---------- ---- ------
11 Robert Wolfe (STX) 1-2-97 Initial version.
12 Raj Gejjagaraguppe (ARC) 1-24-97 Modified and added code to make
13 this work with GCTP software.
14
15
16 ! Usage Notes:
17 1. The following functions are available:
18
19 isinusinvinit - Initialize integerized sinusoidal transformations
20 isinusinv - Inverse mapping; converts map projection
21 coordinates (x/y) to geographic coordinates
22 (longitude/latitude)
23
24 2. Since there are discontinuities at the top and bottom of each zone
25 within the integerized sinusoidal grid care should be taken when
26 mapping points near the edges of the zones. Also, care should be taken
27 near the discontinuity at the most eastward and westward portions of
28 the projection (180 degrees from the central meridian).
29
30 3. Latitudes are expected to in the range [-'HALFPI' to 'HALFPI'].
31
32 4. Longitudes are expected to be in the range [-'TWOPI' to 'TWOPI'].
33
34 5. The justify flag is used to indicate what to do with zones with an
35 odd number of columns. If it has a value of 0 or 1 it indicates the
36 extra column is on the right (zero) or left (one) of the projection
37 y axis. If the flag is set to 2 the columns are calculated so there
38 are always an even number of column in each zone.
39
40 6. The origin is located at the equator which is at the bottom of the first
41 zone above the equator and the top of the first zone below the equator.
42
43 7. These routines were designed as an GCTP (General Cartographic
44 Transformation Package) interface to the 'isin' library.
45
46 !END****************************************************************************
47 */
48
49 /* #define NO_OUTPUT */ /* if defined, error messages are not written */
50 /* #define CHECK_EDGE */ /* if defined, statistics are gathered on how
51 * close the column calculations are to being
52 * dependent on the machine precision */
53
54
55 #include<stdio.h>
56 #include <stdlib.h>
57 #include <limits.h>
58 #include <math.h>
59
60 #ifndef NO_OUTPUT
61 #include <stdio.h>
62 #endif
63
64 #include "isin.h"
65
66 #ifndef M_PI
67 #define PI 3.141592653589793 /* Circular constant */
68 #else
69 #define PI M_PI
70 #endif
71 #define TWOPI (2.0 * PI)
72 #define HALFPI (PI / 2.0)
73 #define TWOPI_INV (1.0 / (2.0 * PI))
74
75 #define NROW_MAX (360 * 3600) /* Maximum number of rows (zones) */
76 #define NZONE_MAX (360 * 3600) /* Maximum number of longitudinal zones */
77 #define EPS_SPHERE (1.0e-10) /* Minimum sphere radius */
78 #define EPS_CNVT 0.01 /* Doubles must be within this of an integer to be
79 * valid */
80 #define ISIN_KEY 212589603 /* Key to verify correct data structure */
81
82 /* Local error handling routine */
83
84 static void error(const char *routine, const char *text) {
85 #ifndef NO_OUTPUT
86 fprintf(stderr, " error (isinusinv.c/%s) : %s\n", routine, text);
87 #endif
88 /* exit(EXIT_FAILURE); */
89 }
90
91 /* Error Messages */
92
93 static error_t ISIN_BADALLOC = { -3, "memory allocation"};
94 static error_t ISIN_BADPARAM = { -4, "invalid parameter"};
95 static error_t ISIN_BADHANDLE = { -5, "invalid handle"};
96 static error_t ISIN_BADKEY = { -6, "invalid key"};
97
98
99 /* Local data structure for 'Isin' library */
100
101 static Isin_t *isin = NULL;
102
103 /* Functions */
104
105
106 int isinusinvinit(double sphere, double lon_cen_mer, double false_east,
107 double false_north, double dzone, double djustify)
108 /*
109 !C******************************************************************************
110 !Description: isinusinvinit (initialize mapping) initializes the integerized
111 sinusoidal transformations.
112
113 !Input Parameters:
114 sphere radius (meters)
115 longitude of central meridian (radians)
116 easting at projection origin (meters)
117 northing at projection origin (meters)
118 number of longitudinal zones
119 justify flag; flag to indicate what to do with
120 rows with an odd number of columns:
121 0.0 - indicates the extra column is on the right
122 of the projection y axis;
123 1.0 - indicates the extra column is on the left
124 of the projection y axis;
125 2.0 - calculate an even number of columns
126
127 !Output Parameters:
128 (none)
129
130 !Team Unique Header:
131
132 ! Usage Notes:
133 1. The sphere radius must not be smaller than 'EPS_SPHERE'.
134 2. The longitude must be in the range [-'TWOPI' to 'TWOPI'].
135 3. The number of longitudinal zones must be a positive multiple of two
136 and no more than 'NZONE_MAX'.
137 4. The number of longitudinal zones and the justify flag must be within
138 'EPS_CNVT' of an integer.
139
140 !END****************************************************************************
141 */
142 {
143 long nzone; /* Number of longitudinal zones */
144 int ijustify; /* Justify flag (see above) */
145 int istat; /* Status returned from 'Isin' functions */
146
147 /* Check to see if this data set was already initialized; if it was,
148 * free the data structure so it can be re-used */
149
150 if (isin != NULL) {
151 istat = Isin_inv_free(isin);
152 if (istat != ISIN_SUCCESS) {
153 error("isinusinvinit", "bad return from Isin_inv_free");
154 return ISIN_ERROR;
155 }
156 }
157
158 /* Check the input parameters */
159
160 if (sphere <= 0.0) {
161 error("isinusinvinit", "bad parameter; sphere radius invalid");
162 return ISIN_ERROR;
163 }
164
165 if (lon_cen_mer < -TWOPI || lon_cen_mer > TWOPI) {
166 error("isinusinvinit", "bad parameter; longitude of central meridian invalid");
167 return ISIN_ERROR;
168 }
169
170 if (dzone < (2.0 - EPS_CNVT) ||
171 dzone > ((double) NZONE_MAX + EPS_CNVT)) {
172 error("isinusinvinit", "bad parameter; nzone out of range");
173 return ISIN_ERROR;
174 }
175
176 nzone = dzone + EPS_CNVT;
177 if (fabs(dzone - nzone) > EPS_CNVT) {
178 error("isinusinvinit", "bad parameter; nzone not near an integer value");
179 return ISIN_ERROR;
180 }
181
182 if ((nzone % 2) != 0) {
183 error("isinusinvinit", "bad parameter; nzone not multiple of two");
184 return ISIN_ERROR;
185 }
186
187 if (djustify < -EPS_CNVT ||
188 djustify > (2.0 + EPS_CNVT)) {
189 error("isinusinvinit", "bad parameter; ijustify out of range");
190 return ISIN_ERROR;
191 }
192
193 ijustify = djustify + EPS_CNVT;
194 if (fabs(djustify - ijustify) > EPS_CNVT) {
195 error("isinusinvinit", "bad parameter; ijustify not near an integer value");
196 return ISIN_ERROR;
197 }
198
199 /* Initialize the projection */
200
201 isin = Isin_inv_init(sphere, lon_cen_mer, false_east, false_north,
202 nzone, ijustify);
203 if (isin == NULL) {
204 error("isinusinvinit", "bad return from Isin_inv_init");
205 return ISIN_ERROR;
206 }
207
208 return ISIN_SUCCESS;
209 }
210
211
212
213 Isin_t *Isin_inv_init(double sphere,
214 double lon_cen_mer,
215 double false_east, double false_north,
216 long nrow, int ijustify)
217 /*
218 !C******************************************************************************
219 !Description: Isin_inv_init (initialize mapping) initializes the integerized
220 sinusoidal transformations by calculating constants and a short-cut
221 lookup table.
222
223 !Input Parameters:
224 sphere sphere radius (meters)
225 lon_cen_mer longitude of central meridian (radians)
226 false_east easting at projection origin (meters)
227 false_north northing at projection origin (meters)
228 nrow number of rows (longitudinal zones)
229 ijustify justify flag; flag to indicate what to do with rows with an
230 odd number of columns;
231 0 = indicates the extra column is on the right
232 of the projection y axis;
233 1 = indicates the extra column is on the left
234 of the projection y axis;
235 2 = calculate an even number of columns
236
237 !Output Parameters:
238 (returns) a handle for this instance of the integerized sinusoidal
239 projection or NULL for error
240
241 !Team Unique Header:
242
243 ! Usage Notes:
244 1. The sphere radius must not be smaller than 'EPS_SPHERE'.
245 2. The longitude must be in the range [-'TWOPI' to 'TWOPI'].
246 3. The number of rows must be a multiple of two and no more than 'NROW_MAX'.
247
248 !END****************************************************************************
249 */
250 {
251 Isin_t *thisIsin; /* 'isin' data structure */
252 Isin_row_t *row; /* current row data structure */
253 long irow; /* row (zone) index */
254 double clat; /* central latitude of the row */
255 long ncol_cen; /* number of columns in the central row of the grid
256 * (at the equator) */
257
258 #ifdef CHECK_EDGE
259 double dcol; /* delta column (normalized by number of columns) */
260 double dcol_min, /* minimum delta column */
261 double log2_dcol_min; /* log base 2 of minimum delta column */
262
263 dcol_min = 1.0;
264 #endif
265
266 /* Check input parameters */
267
268 if (sphere < EPS_SPHERE)
269 {Isin_error(&ISIN_BADPARAM, "Isin_inv_init"); return NULL;}
270
271 if (lon_cen_mer < -TWOPI || lon_cen_mer > TWOPI)
272 {Isin_error(&ISIN_BADPARAM, "Isin_inv_init"); return NULL;}
273 if (lon_cen_mer < PI) lon_cen_mer += TWOPI;
274 if (lon_cen_mer >= PI) lon_cen_mer -= TWOPI;
275
276 if (nrow < 2 || nrow > NROW_MAX)
277 {Isin_error(&ISIN_BADPARAM, "Isin_inv_init"); return NULL;}
278 if ((nrow % 2) != 0)
279 {Isin_error(&ISIN_BADPARAM, "Isin_inv_init"); return NULL;}
280
281 if (ijustify < 0 || ijustify > 2)
282 {Isin_error(&ISIN_BADPARAM, "Isin_inv_init"); return NULL;}
283
284 /* Allocate 'isin' data structure */
285
286 thisIsin = (Isin_t *) malloc(sizeof(Isin_t));
287 if (thisIsin == NULL)
288 {Isin_error(&ISIN_BADALLOC, "Isin_inv_init"); return NULL;}
289
290 /* Initialize data structure */
291
292 thisIsin->key = (long) NULL;
293 thisIsin->false_east = false_east;
294 thisIsin->false_north = false_north;
295 thisIsin->sphere = sphere;
296 thisIsin->sphere_inv = 1.0 / sphere;
297 thisIsin->ang_size_inv = ((double) nrow) / PI;
298 thisIsin->nrow = nrow;
299 thisIsin->nrow_half = nrow / 2;
300 thisIsin->lon_cen_mer = lon_cen_mer;
301 thisIsin->ref_lon = lon_cen_mer - PI;
302 if (thisIsin->ref_lon < -PI) thisIsin->ref_lon += TWOPI;
303 thisIsin->ijustify = ijustify;
304
305 /* Allocate space for information about each row */
306
307 thisIsin->row = (Isin_row_t *) malloc(thisIsin->nrow_half * sizeof(Isin_row_t));
308 if (thisIsin->row == NULL) {
309 free(thisIsin);
310 Isin_error(&ISIN_BADALLOC, "Isin_inv_init");
311 return NULL;
312 }
313
314 /* Do calculations for each row; calculations are only done for half
315 * the rows because of the symmetry between the rows above the
316 * equator and the ones below */
317
318 row = thisIsin->row;
319 for (irow = 0; irow < thisIsin->nrow_half; irow++, row++) {
320
321 /* Calculate latitude at center of row */
322
323 clat = HALFPI * (1.0 - ((double) irow + 0.5) / thisIsin->nrow_half);
324
325 /* Calculate number of columns per row */
326
327 if (ijustify < 2)
328 row->ncol = (2.0 * cos(clat) * nrow) + 0.5;
329 else { /* make the number of columns even */
330 row->ncol = (cos(clat) * nrow) + 0.5;
331 row->ncol *= 2;
332 }
333
334 #ifdef CHECK_EDGE
335 /* Check to be sure the are no less then three columns per row and that
336 * there are exactly three columns at the poles */
337 if (ijustify < 2) {
338 if (row->ncol < 3 || (irow == 0 && row->ncol != 3))
339 printf(" irow = %d ncol = %d\n", irow, row->ncol);
340 } else {
341 if (row->ncol < 6 || (irow == 0 && row->ncol != 6))
342 printf(" irow = %d ncol = %d\n", irow, row->ncol);
343 }
344 #endif
345
346 /* Must have at least one column */
347
348 if (row->ncol < 1) row->ncol = 1;
349
350 #ifdef CHECK_EDGE
351
352 /* Calculate the minimum delta column (normalized by the number of
353 * columns in the row) */
354
355 if (ijustify < 2)
356 dcol = fabs((2.0 * cos(clat) * nrow) + 0.5 - row->ncol);
357 else
358 dcol = 2.0 * fabs((cos(clat) * nrow) + 0.5 - (row->ncol / 2));
359 dcol = dcol / row->ncol;
360 if (dcol < dcol_min) dcol_min = dcol;
361
362 if (ijustify < 2) {
363 dcol = fabs((2.0 * cos(clat) * nrow) + 0.5 - (row->ncol + 1));
364 dcol = dcol / (row->ncol + 1);
365 } else {
366 dcol = 2.0 * fabs((cos(clat) * nrow) + 0.5 - ((row->ncol / 2) + 1));
367 dcol = dcol / (row->ncol + 2);
368 }
369 if (dcol < dcol_min) dcol_min = dcol;
370 #endif
371
372 /* Save the inverse of the number of columns */
373
374 row->ncol_inv = 1.0 / ((double) row->ncol);
375
376 /* Calculate the column number of the column whose left edge touches the
377 * central meridian */
378 if (ijustify == 1)
379 row->icol_cen = (row->ncol + 1) / 2;
380 else
381 row->icol_cen = row->ncol / 2;
382
383 } /* for (irow... */
384
385 /* Get the number of columns at the equator */
386 ncol_cen = thisIsin->row[thisIsin->nrow_half - 1].ncol;
387
388 #ifdef CHECK_EDGE
389
390 /* Print the minimum delta column and its base 2 log */
391
392 log2_dcol_min = log(dcol_min) / log(2.0);
393 printf(" dcol_min = %g log2_dcol_min = %g\n", dcol_min, log2_dcol_min);
394
395 /* Check to be sure the number of columns at the equator is twice the
396 * number of rows */
397 if (ncol_cen != nrow * 2)
398 printf(" ncol_cen = %d nrow = %d\n", ncol_cen, nrow);
399 #endif
400
401 /* Calculate the distance at the equator between
402 * the centers of two columns (and the inverse) */
403
404 thisIsin->col_dist = (TWOPI * sphere) / ncol_cen;
405 thisIsin->col_dist_inv = ncol_cen / (TWOPI * sphere);
406
407 /* Give the data structure a valid key */
408
409 thisIsin->key = ISIN_KEY;
410
411 /* All done */
412 return thisIsin;
413 }
414
415
416
417 int isinusinv(double x, double y, double *lon, double *lat)
418 /*
419 !C******************************************************************************
420 !Description: isinusinv (inverse mapping) maps from map projection coordinates
421 ('x', 'y') to geographic coordinates ('lon', 'lat').
422
423 !Input Parameters:
424 x easting in map projection (same units as 'sphere')
425 y northing in map projection (same units as 'sphere')
426
427 !Output Parameters:
428 lon longitude (radians)
429 lat latitude (radians)
430
431 !Team Unique Header:
432
433 ! Usage Notes:
434 1. 'isinus_init' must have been previously called for the handle.
435 2. The longitude returned is in the range [-'PI' to 'PI').
436 3. If the input point is in the fill area of the map projection
437 a status of ISIN_ERANGE is returned.
438
439 !END****************************************************************************
440 */
441 {
442 int istat; /* Status returned from 'Isin_inv' function */
443
444 /* Call 'Isin_inv' */
445
446 istat = Isin_inv(isin, x, y, lon, lat);
447 if (istat != ISIN_SUCCESS) {
448 error("isinusinv", "bad return from Isin_inv");
449 return ISIN_ERROR;
450 }
451
452 return ISIN_SUCCESS;
453 }
454
455
456
457 int Isin_inv(const Isin_t *thisIsin,
458 double x, double y,
459 double *lon, double *lat)
460 /*
461 !C******************************************************************************
462 !Description: Isin_inv (inverse mapping) maps from map projection coordinates
463 ('x', 'y') to geographic coordinates ('lon', 'lat').
464
465 !Input Parameters:
466 thisIsin handle for this instance of the integerized sinusoidal
467 projection
468 x easting in map projection (same units as 'sphere')
469 y northing in map projection (same units as 'sphere')
470
471 !Output Parameters:
472 lon longitude (radians)
473 lat latitude (radians)
474 (returns) status:
475 ISIN_SUCCESS - normal return
476 ISIN_ERANGE - point not in map projection
477 ISIN_ERROR - error return
478
479 !Team Unique Header:
480
481 ! Usage Notes:
482 1. 'Isin_inv_init' must have been previously called for the handle.
483 2. The longitude returned is in the range [-'PI' to 'PI').
484 3. If the input point is in the fill area of the map projection
485 a status of ISIN_ERANGE is returned.
486
487 !END****************************************************************************
488 */
489 {
490 double row, col; /* Row (zone) and column; column is relative to
491 * central; 0.5 is the center of a row or column */
492 double flon; /* Fractional longitude (multiples of PI) */
493 long irow; /* Integer row (zone) number */
494
495 /* Check the input parameters */
496
497 *lon = 0.0;
498 *lat = 0.0;
499
500 if (thisIsin == NULL) return Isin_error(&ISIN_BADHANDLE, "Isin_inv");
501 if (thisIsin->key != ISIN_KEY) return Isin_error(&ISIN_BADKEY, "Isin_inv");
502
503 /* Latitude */
504
505 *lat = (y - thisIsin->false_north) * thisIsin->sphere_inv;
506 if (*lat < -HALFPI || *lat > HALFPI)
507 {*lat = 0.0; return ISIN_ERANGE;}
508
509 /* Integer row number */
510
511 row = (HALFPI - *lat) * thisIsin->ang_size_inv;
512 irow = row;
513 if (irow >= thisIsin->nrow_half) irow = (thisIsin->nrow - 1) - irow;
514 if (irow < 0) irow = 0;
515
516 /* Column number (relative to center) */
517
518 col = (x - thisIsin->false_east) * thisIsin->col_dist_inv;
519
520 /* Fractional longitude (between 0 and 1) */
521
522 flon = (col + thisIsin->row[irow].icol_cen) * thisIsin->row[irow].ncol_inv;
523 if (flon < 0.0 || flon > 1.0)
524 {*lat = 0.0; return ISIN_ERANGE;}
525
526 /* Actual longitude */
527
528 *lon = thisIsin->ref_lon + (flon * TWOPI);
529 if (*lon >= PI) *lon -= TWOPI;
530 if (*lon < -PI) *lon += TWOPI;
531
532 return ISIN_SUCCESS;
533 }
534
535
536 int Isin_inv_free(Isin_t *thisIsin)
537 /*
538 !C******************************************************************************
539 !Description: Isin_inv_free (free) deallocates the 'isin' data structure and
540 array memory.
541
542 !Input Parameters:
543 thisIsin handle for this instance of the integerized sinusoidal
544 projection
545 !Output Parameters:
546 (returns) status:
547 ISIN_SUCCESS - normal return
548 ISIN_ERROR - error return
549
550 !Team Unique Header:
551
552 ! Usage Notes:
553 1. 'Isin_inv_init' must have been previously called for the handle.
554
555 !END****************************************************************************
556 */
557 {
558 if (thisIsin == NULL) return Isin_error(&ISIN_BADHANDLE, "Isin_inv_free");
559 if (thisIsin->key != ISIN_KEY) return Isin_error(&ISIN_BADKEY, "Isin_inv_free");
560
561 /* Set the key to NULL */
562 thisIsin->key = (long) NULL;
563
564 /* Free the memory */
565 free(thisIsin->row);
566 thisIsin->row = NULL;
567 free(thisIsin);
568 thisIsin = NULL;
569
570 return ISIN_SUCCESS;
571 }
572
573
574 static int Isin_error(const error_t *err, const char *routine)
575 /*
576 !C******************************************************************************
577
578 !Description: Private function to handle errors.
579
580 !Input Parameters:
581 err Error structure
582 routine String containing name of routine where error occurred
583
584 !Output Parameters:
585 (returns) status:
586 ISIN_ERROR - normal return
587
588 !Team Unique Header:
589
590 !Usage Notes: (none)
591
592 !END****************************************************************************
593 */
594 {
595 #ifndef NO_OUTPUT
596 fprintf(stderr, " error (isinusinv.c/%s) : (%i) %s\n", routine, err->num,
597 err->str);
598 #endif
599
600 return ISIN_ERROR;
601 }