Skip to content

Commit

Permalink
Per #1954, switch flag from Is2Dim to IsLatLon to avoid confusion.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnHalleyGotway committed Aug 19, 2022
1 parent 95d9bdf commit ed85c8a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 71 deletions.
119 changes: 53 additions & 66 deletions src/libcode/vx_grid/semilatlon_grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,28 @@ if ( data.name ) Name = data.name;
Lats = data.lats;
Lons = data.lons;

if ( Lons.n() > 0 ) xDim = &Lons;
if ( Lats.n() > 0 ) yDim = &Lats;
if ( Lats.n() > 0 && Lons.n() > 0 ) {

if ( Lats.n() != Lons.n() ) {
mlog << Error << "\nSemiLatLonGrid::SemiLatLonGrid(const SemiLatLonData & data) -> "
<< "when both are specified, the number of lats (" << Lats.n()
<< ") and lons (" << Lons.n() << ") must match.\n\n";
exit ( 1 );
}

IsLatLon = true;
xDim = &Lons;
}
else if ( Lons.n() > 0 ) { xDim = &Lons; }
else if ( Lats.n() > 0 ) { yDim = &Lats; }
else {

mlog << Error << "\nSemiLatLonGrid::SemiLatLonGrid(const SemiLatLonData & data) -> "
<< "the number of lats (" << Lats.n() << ") and lons (" << Lons.n()
<< ") cannot both be zero.\n\n";
exit ( 1 );

}

// Process the other dimensions

Expand All @@ -85,41 +105,15 @@ add_dimension(data.times, Times);
if ( !xDim || !yDim ) {

mlog << Error << "\nSemiLatLonGrid::SemiLatLonGrid(const SemiLatLonData & data) -> "
<< "exactly two dimensions should have non-zero length: lats ("
<< Lats.n() << "), lons (" << Lons.n() << "), levels (" << Levels.n()
<< "), times (" << Times.n() << ")\n\n";
<< "the number of levels (" << Levels.n() << ") and times (" << Times.n()
<< ") cannot both be zero.\n\n";
exit ( 1 );

}

Nx = xDim->n();
Ny = yDim->n();

// 1-dimensional array of lat/lon locations

if ( Lats.n() > 0 && Lons.n() > 0 ) {

Is2Dim = false;

if ( Lats.n() != Lons.n() ) {

mlog << Error << "\nSemiLatLonGrid::SemiLatLonGrid(const SemiLatLonData & data) -> "
<< "for one dimensional arrays, the number lats and lons must match ("
<< Lats.n() << " != " << Lons.n() << ")!\n\n";
exit ( 1 );

}

}

// 2-dimensional data

else {

Is2Dim = true;

}

// Store the data

Data = data;
Expand Down Expand Up @@ -175,6 +169,7 @@ Times.clear();
xDim = (NumArray *) 0;
yDim = (NumArray *) 0;

IsLatLon = false;
Nx = 0;
Ny = 0;

Expand All @@ -188,7 +183,7 @@ return;
////////////////////////////////////////////////////////////////////////


void SemiLatLonGrid::latlon_to_xy(double lat, double lon, double & x, double & y) const
void SemiLatLonGrid::latlon_to_xy(double xdim_val, double ydim_val, double &x, double &y) const

{

Expand All @@ -198,42 +193,34 @@ if ( !xDim || !yDim ) {
exit ( 1 );
}

if ( IsLatLon ) {
mlog << Error << "\nSemiLatLonGrid::latlon_to_xy() -> "
<< "not supported when IsLatLon is true.\n\n";
exit ( 1 );
}

int i, x_int, y_int;
x_int = y_int = bad_data_int;

// Search dimensions separately for matches
if ( Is2Dim ) {

// Search the first dimension
for(i=0; i<xDim->n(); i++) {
if(is_eq(lat, xDim->buf()[i])) {
x_int = i;
break;
}
}

// Search the second dimension
for(i=0; i<yDim->n(); i++) {
if(is_eq(lat, yDim->buf()[i])) {
y_int = i;
break;
}
// Search the X dimension
for(i=0; i<xDim->n(); i++) {
if(is_eq(xdim_val, xDim->buf()[i])) {
x_int = i;
break;
}
}
else {

// Search both dimensions
for(i=0; i<xDim->n(); i++) {
if(is_eq(lat, xDim->buf()[i]) && is_eq(lon, yDim->buf()[i])) {
x_int = y_int = i;
break;
}
// Search the Y dimension
for(i=0; i<yDim->n(); i++) {
if(is_eq(ydim_val, yDim->buf()[i])) {
y_int = i;
break;
}
}

if ( is_bad_data(x_int) || is_bad_data(y_int) ) {
mlog << Error << "\nSemiLatLonGrid::latlon_to_xy() -> "
<< "no match found for (" << lat << ", " << lon << ").\n\n";
<< "no match found for (" << xdim_val << ", " << ydim_val << ").\n\n";
exit ( 1 );
}

Expand All @@ -248,7 +235,7 @@ return;
////////////////////////////////////////////////////////////////////////


void SemiLatLonGrid::xy_to_latlon(double x, double y, double & lat, double & lon) const
void SemiLatLonGrid::xy_to_latlon(double x, double y, double & xdim_val, double & ydim_val) const

{

Expand All @@ -269,8 +256,8 @@ if ( x_int < 0 || x_int >= Nx ||
exit ( 1 );
}

lat = xDim->buf()[x_int];
lon = yDim->buf()[y_int];
xdim_val = xDim->buf()[x_int];
ydim_val = yDim->buf()[y_int];

return;

Expand Down Expand Up @@ -358,9 +345,9 @@ Levels.dump(out, depth+1);
out << prefix << "Times:\n";
Times.dump(out, depth+1);

out << prefix << "Is2Dim = " << bool_to_string(Is2Dim) << "\n";
out << prefix << "Nx = " << Nx << "\n";
out << prefix << "Ny = " << Ny << "\n";
out << prefix << "IsLatLon = " << bool_to_string(IsLatLon) << "\n";
out << prefix << "Nx = " << Nx << "\n";
out << prefix << "Ny = " << Ny << "\n";

//
// done
Expand All @@ -387,10 +374,10 @@ a << "Projection: SemiLatLon" << sep;
a << "Nx: " << Nx << sep;
a << "Ny: " << Ny << sep;

a << "Lats: " << Lats.serialize() << sep;
a << "Lons: " << Lons.serialize() << sep;
a << "Levels: " << Levels.serialize() << sep;
a << "Times: " << Times.serialize() << sep;
a << "Lats: " << Lats.summarize() << sep;
a << "Lons: " << Lons.summarize() << sep;
a << "Levels: " << Levels.summarize() << sep;
a << "Times: " << Times.summarize() << sep;

//
// done
Expand Down
7 changes: 2 additions & 5 deletions src/libcode/vx_grid/semilatlon_grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SemiLatLonGrid : public GridRep {
ConcatString Name;

// Lat and/or Lon are non-empty and Levels or Times are non-empty
// When Lat and Lon are specified, they must be the same length
// When Lat and Lon are both specified, they must be the same length

NumArray Lats;
NumArray Lons;
Expand All @@ -54,10 +54,7 @@ class SemiLatLonGrid : public GridRep {
NumArray * xDim; // not allocated
NumArray * yDim; // not allocated

// TRUE: for lat or lon vs level or time with unique lat/lon values
// FALSE: for a 1D list of potentially non-unique lat/lon values

bool Is2Dim;
bool IsLatLon;
int Nx;
int Ny;

Expand Down

0 comments on commit ed85c8a

Please sign in to comment.