Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vgridshift: handle longitude wrap-around for grids with 360deg longitude extent #1429

Merged
merged 1 commit into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions src/apply_vgridshift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#define PJ_LIB__

#include <assert.h>
#include <stdio.h>
#include <string.h>

Expand Down Expand Up @@ -62,9 +63,19 @@ static double read_vgrid_value( PJ *defn, PJ_LP input, double vmultiplier, int *

ct = gi->ct;

/* skip tables that don't match our point at all. */
if( ct->ll.phi > input.phi || ct->ll.lam > input.lam
|| ct->ll.phi + (ct->lim.phi-1) * ct->del.phi < input.phi
/* skip tables that don't match our point at all (latitude check). */
if( ct->ll.phi > input.phi
|| ct->ll.phi + (ct->lim.phi-1) * ct->del.phi < input.phi )
continue;

bool fullWorldLongExtent = false;
if( fabs(ct->lim.lam * ct->del.lam - 2 * M_PI) < 1e-10 )
{
fullWorldLongExtent = true;
}

/* skip tables that don't match our point at all (longitude check). */
else if( ct->ll.lam > input.lam
|| ct->ll.lam + (ct->lim.lam-1) * ct->del.lam < input.lam )
continue;

Expand All @@ -77,8 +88,17 @@ static double read_vgrid_value( PJ *defn, PJ_LP input, double vmultiplier, int *
{
struct CTABLE *ct1 = child->ct;

if( ct1->ll.phi > input.phi || ct1->ll.lam > input.lam
|| ct1->ll.phi+(ct1->lim.phi-1)*ct1->del.phi < input.phi
fullWorldLongExtent = false;

if( ct1->ll.phi > input.phi
|| ct1->ll.phi+(ct1->lim.phi-1)*ct1->del.phi < input.phi)
continue;

if( fabs(ct1->lim.lam * ct1->del.lam - 2 * M_PI) < 1e-10 )
{
fullWorldLongExtent = true;
}
else if( ct1->ll.lam > input.lam
|| ct1->ll.lam+(ct1->lim.lam-1)*ct1->del.lam < input.lam)
continue;

Expand Down Expand Up @@ -109,15 +129,29 @@ static double read_vgrid_value( PJ *defn, PJ_LP input, double vmultiplier, int *

/* Interpolation a location within the grid */
grid_x = (input.lam - ct->ll.lam) / ct->del.lam;
if( fullWorldLongExtent ) {
// The first fmod goes to ]-lim, lim[ range
// So we add lim again to be in ]0, 2*lim[ and fmod again
grid_x = fmod(
fmod(grid_x + ct->lim.lam, ct->lim.lam) + ct->lim.lam,
ct->lim.lam);
}
grid_y = (input.phi - ct->ll.phi) / ct->del.phi;
grid_ix = lround(floor(grid_x));
assert(grid_ix >= 0 && grid_ix < ct->lim.lam);
grid_iy = lround(floor(grid_y));
assert(grid_iy >= 0 && grid_iy < ct->lim.phi);
grid_x -= grid_ix;
grid_y -= grid_iy;

grid_ix2 = grid_ix + 1;
if( grid_ix2 >= ct->lim.lam )
grid_ix2 = ct->lim.lam - 1;
if( grid_ix2 >= ct->lim.lam ) {
if( fullWorldLongExtent ) {
grid_ix2 = 0;
} else {
grid_ix2 = ct->lim.lam - 1;
}
}
grid_iy2 = grid_iy + 1;
if( grid_iy2 >= ct->lim.phi )
grid_iy2 = ct->lim.phi - 1;
Expand Down
18 changes: 18 additions & 0 deletions test/gie/more_builtins.gie
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,24 @@ ignore pjd_err_failed_to_load_grid
accept 12.5 55.5 0 0
expect 12.5 55.5 -36.021305084228516 0

accept -180.1 0 0
expect -180.1 0 -21.2423

accept 179.9 0 0
expect 179.9 0 -21.2423

accept 180 0 0
expect 180 0 -21.1533

accept 540 0 0
expect 540 0 -21.1533

accept -180 0 0
expect -180 0 -21.1533

accept -540 0 0
expect -540 0 -21.1533

roundtrip 100 1 nm
-------------------------------------------------------------------------------
Fail on purpose: +grids parameter is mandatory
Expand Down