Skip to content

Commit

Permalink
Per #2152, add new -type poly_xy masking option to Gen-Vx-Mask.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnHalleyGotway committed May 5, 2022
1 parent 5ecbf56 commit ec72f55
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 16 deletions.
86 changes: 71 additions & 15 deletions met/src/tools/other/gen_vx_mask/gen_vx_mask.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// 009 06/01/21 Seth Linden Change -type from optional to required.
// 010 08/30/21 Halley Gotway MET #1891 Fix input and mask fields.
// 011 12/13/21 Halley Gotway MET #1993 Fix -type grid.
// 012 05/05/22 Halley Gotway MET #2152 Add -type poly_xy.
//
////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -77,10 +78,11 @@ int main(int argc, char *argv[]) {
process_mask_file(dp_mask);

// Apply combination logic if the current mask is binary
if(mask_type == MaskType_Poly ||
mask_type == MaskType_Shape ||
mask_type == MaskType_Box ||
mask_type == MaskType_Grid ||
if(mask_type == MaskType_Poly ||
mask_type == MaskType_Poly_XY ||
mask_type == MaskType_Shape ||
mask_type == MaskType_Box ||
mask_type == MaskType_Grid ||
thresh.get_type() != thresh_na) {
dp_out = combine(dp_data, dp_mask, set_logic);
}
Expand Down Expand Up @@ -211,9 +213,10 @@ void process_mask_file(DataPlane &dp) {
solar_ut = (unixtime) 0;

// Process the mask file as a lat/lon polyline file
if(mask_type == MaskType_Poly ||
mask_type == MaskType_Box ||
mask_type == MaskType_Circle ||
if(mask_type == MaskType_Poly ||
mask_type == MaskType_Poly_XY ||
mask_type == MaskType_Box ||
mask_type == MaskType_Circle ||
mask_type == MaskType_Track) {

poly_mask.clear();
Expand Down Expand Up @@ -339,6 +342,10 @@ void process_mask_file(DataPlane &dp) {
apply_poly_mask(dp);
break;

case MaskType_Poly_XY:
apply_poly_xy_mask(dp);
break;

case MaskType_Box:
apply_box_mask(dp);
break;
Expand Down Expand Up @@ -549,7 +556,7 @@ void apply_poly_mask(DataPlane & dp) {
bool inside;
double lat, lon;

// Check each grid point being inside the polyline
// Check the Lat/Lon of grid point being inside the polyline
for(x=0,n_in=0; x<grid.nx(); x++) {
for(y=0; y<grid.ny(); y++) {

Expand Down Expand Up @@ -586,6 +593,52 @@ void apply_poly_mask(DataPlane & dp) {

////////////////////////////////////////////////////////////////////////

void apply_poly_xy_mask(DataPlane & dp) {
int i, x, y, n_in;
bool inside;
double x_dbl, y_dbl;
GridClosedPoly poly_xy;

// Convert MaskPoly Lat/Lon coordinates to Grid X/Y
for(i=0; i<poly_mask.n_points(); i++) {
grid.latlon_to_xy(poly_mask.lat(i), poly_mask.lon(i), x_dbl, y_dbl);
poly_xy.add_point(x_dbl, y_dbl);
}

// Check the X/Y of each grid point being inside the polyline
for(x=0,n_in=0; x<grid.nx(); x++) {
for(y=0; y<grid.ny(); y++) {

// Check current grid point inside polyline
inside = (poly_xy.is_inside((double) x, (double) y) != 0);

// Check the complement
if(complement) inside = !inside;

// Increment count
if(inside) n_in++;

// Store the current mask value
dp.set((inside ? 1.0 : 0.0), x, y);

} // end for y
} // end for x

if(complement) {
mlog << Debug(3)
<< "Applying complement of polyline XY mask.\n";
}

// List number of points inside the mask
mlog << Debug(3)
<< "Polyline XY Masking:\t" << n_in << " of " << grid.nx() * grid.ny()
<< " points inside\n";

return;
}

////////////////////////////////////////////////////////////////////////

void apply_box_mask(DataPlane &dp) {
int i, x_ll, y_ll, x, y, n_in;
double cen_x, cen_y;
Expand Down Expand Up @@ -1217,8 +1270,9 @@ void write_netcdf(const DataPlane &dp) {

// Set the mask_name, if not already set
if(mask_name.length() == 0) {
if(mask_type == MaskType_Poly ||
mask_type == MaskType_Circle ||
if(mask_type == MaskType_Poly ||
mask_type == MaskType_Poly_XY ||
mask_type == MaskType_Circle ||
mask_type == MaskType_Track) {
mask_name = poly_mask.name();
}
Expand Down Expand Up @@ -1294,6 +1348,7 @@ MaskType string_to_masktype(const char *s) {
MaskType t = MaskType_None;

if(strcasecmp(s, "poly") == 0) t = MaskType_Poly;
else if(strcasecmp(s, "poly_xy") == 0) t = MaskType_Poly_XY;
else if(strcasecmp(s, "box") == 0) t = MaskType_Box;
else if(strcasecmp(s, "circle") == 0) t = MaskType_Circle;
else if(strcasecmp(s, "track") == 0) t = MaskType_Track;
Expand All @@ -1320,6 +1375,7 @@ const char * masktype_to_string(const MaskType t) {

switch(t) {
case MaskType_Poly: s = "poly"; break;
case MaskType_Poly_XY: s = "poly_xy"; break;
case MaskType_Box: s = "box"; break;
case MaskType_Circle: s = "circle"; break;
case MaskType_Track: s = "track"; break;
Expand Down Expand Up @@ -1371,8 +1427,8 @@ void usage() {

<< "\t\t\"mask_file\" defines the masking information "
<< "(required).\n"
<< "\t\t For \"poly\", \"box\", \"circle\", and \"track\" "
<< "masking, specify an ASCII Lat/Lon file.\n"
<< "\t\t For \"poly\", \"poly_xy\", \"box\", \"circle\", "
<< "and \"track\" masking, specify an ASCII Lat/Lon file.\n"
<< "\t\t For \"grid\" masking, specify a named grid, the "
<< "path to a gridded data file, or an explicit grid "
<< "specification.\n"
Expand All @@ -1390,9 +1446,9 @@ void usage() {

<< "\t\t\"-type string\" specify the masking type "
<< "(required).\n"
<< "\t\t \"poly\", \"box\", \"circle\", \"track\", \"grid\", "
<< "\"data\", \"solar_alt\", \"solar_azi\", \"lat\", \"lon\" "
<< "or \"shape\"\n"
<< "\t\t \"poly\", \"poly_xy\", \"box\", \"circle\", \"track\", "
<< "\"grid\", \"data\", \"solar_alt\", \"solar_azi\", \"lat\", "
<< "\"lon\" or \"shape\"\n"

<< "\t\t\"-input_field string\" reads existing mask data from "
<< "the \"input_grid\" gridded data file (optional).\n"
Expand Down
6 changes: 5 additions & 1 deletion met/src/tools/other/gen_vx_mask/gen_vx_mask.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// 002 11/15/16 Halley Gotway Add solar masking types.
// 003 06/03/21 Seth Linden Changed default mask type to MaskType_None.
// 004 08/30/21 Halley Gotway MET #1891 fix input and mask fields.
// 005 05/05/22 Halley Gotway MET #2152 Add -type poly_xy.
//
////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -48,7 +49,9 @@ static const char *program_name = "gen_vx_mask";

enum MaskType {

MaskType_Poly, // Polyline masking region
MaskType_Poly, // Polyline masking in lat/lon space
MaskType_Poly_XY, // Polyline masking in grid x/y space

MaskType_Box, // Box masking type
MaskType_Circle, // Circle masking region

Expand Down Expand Up @@ -129,6 +132,7 @@ static bool get_gen_vx_mask_config_str(MetNcMetDataFile *,
ConcatString &);
static void get_shapefile_outline(ShpPolyRecord &shape);
static void apply_poly_mask(DataPlane &dp);
static void apply_poly_xy_mask(DataPlane &dp);
static void apply_shape_mask(DataPlane &dp);
static void apply_box_mask(DataPlane &dp);
static void apply_circle_mask(DataPlane &dp);
Expand Down

0 comments on commit ec72f55

Please sign in to comment.