Skip to content

Commit

Permalink
Bugfix 2936 point2grid gfs (#2964)
Browse files Browse the repository at this point in the history
* #2936 Support 1D lat/lon values

* #2936 Initial release

* #2936 Cast the data type to avoid a compile warning

* #2936 Added an unittest point2grid_gfs_1D_lat_lon

---------

Co-authored-by: Howard Soh <[email protected]>
  • Loading branch information
hsoh-u and Howard Soh authored Sep 5, 2024
1 parent f474e8a commit 2e14724
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
7 changes: 7 additions & 0 deletions internal/test_unit/config/Point2GridConfig_SST
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file_type = NETCDF_NCCF;

var_name_map = [
{ key = "lat_vname"; val = "yh"; },
{ key = "lon_vname"; val = "xh"; }
];

28 changes: 13 additions & 15 deletions internal/test_unit/xml/unit_point2grid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -284,40 +284,38 @@
</output>
</test>

<!--
<test name="point2grid_tripolar_gfs_ocean">
<test name="point2grid_tripolar_rtofs">
<exec>&MET_BIN;/point2grid</exec>
<env>
<pair><name>MET_TMP_DIR</name> <value>&OUTPUT_DIR;/point2grid</value></pair>
</env>
<param> \
&DATA_DIR_OBS;/point_obs/gfs.ocean.t00z.6hr_avg.f006_SST.nc \
"latlon 360 180 -90 -180 1.0 1.0" \
&OUTPUT_DIR;/point2grid/point2grid_gfs_ocean_SST.nc \
-field 'name="SST"; level="(0,*,*)";' \
-config &CONFIG_DIR;/Point2GridConfig_SST \
&DATA_DIR_OBS;/point_obs/rtofs_glo_2ds_f006_ice_coverage.nc \
"latlon 720 360 -80. -60. 0.5 0.5" \
&OUTPUT_DIR;/point2grid/point2grid_rtofs_ice_coverage.nc \
-field 'name="ice_coverage"; level="(0,*,*)";' \
-v 1
</param>
<output>
<grid_nc>&OUTPUT_DIR;/point2grid/point2grid_gfs_ocean_SST.nc</grid_nc>
<grid_nc>&OUTPUT_DIR;/point2grid/point2grid_rtofs_ice_coverage.nc</grid_nc>
</output>
</test>
-->

<test name="point2grid_tripolar_rtofs">
<test name="point2grid_gfs_1D_lat_lon">
<exec>&MET_BIN;/point2grid</exec>
<env>
<pair><name>MET_TMP_DIR</name> <value>&OUTPUT_DIR;/point2grid</value></pair>
</env>
<param> \
&DATA_DIR_OBS;/point_obs/rtofs_glo_2ds_f006_ice_coverage.nc \
"latlon 720 360 -80. -60. 0.5 0.5" \
&OUTPUT_DIR;/point2grid/point2grid_rtofs_ice_coverage.nc \
-field 'name="ice_coverage"; level="(0,*,*)";' \
&DATA_DIR_OBS;/point_obs/gfs.ocean.t00z.6hr_avg.f006_SST.nc \
G231 \
&OUTPUT_DIR;/point2grid/point2grid_gfs.ocean.SST.nc \
-config &CONFIG_DIR;/Point2GridConfig_SST \
-field 'name="SST"; level="(0,*,*)";' \
-v 1
</param>
<output>
<grid_nc>&OUTPUT_DIR;/point2grid/point2grid_rtofs_ice_coverage.nc</grid_nc>
<grid_nc>&OUTPUT_DIR;/point2grid/point2grid_gfs.ocean.SST.nc</grid_nc>
</output>
</test>

Expand Down
49 changes: 29 additions & 20 deletions src/tools/other/point2grid/point2grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ static void process_data_file() {
config.read_string(FieldSA[0].c_str());

// Note: The command line argument MUST processed before this
if (compress_level < 0) compress_level = config.nc_compression();
if (compress_level < 0) compress_level = conf_info.conf.nc_compression();

// Get the gridded file type from config string, if present
ftype = parse_conf_file_type(&conf_info.conf);
Expand Down Expand Up @@ -963,7 +963,7 @@ void process_point_met_data(MetPointData *met_point_obs, MetConfig &config, VarI

var_index_array.add(idx);
var_count++;
if (is_eq(obs_data->obs_vals[idx], 0.)) obs_count_zero_from++;
if (is_eq(obs_data->obs_vals[idx], (float)0.)) obs_count_zero_from++;
else obs_count_non_zero_from++;
}
}
Expand Down Expand Up @@ -2098,7 +2098,7 @@ static bool get_grid_mapping(const Grid &to_grid, IntArray *cellMapping,
static void get_grid_mapping_latlon(
DataPlane from_dp, DataPlane to_dp, Grid to_grid,
IntArray *cellMapping, float *latitudes, float *longitudes,
int from_lat_count, int from_lon_count, bool *skip_times, bool to_north) {
int from_lat_count, int from_lon_count, bool *skip_times, bool to_north, bool is_2d) {
double x;
double y;
double to_ll_lat;
Expand All @@ -2122,11 +2122,16 @@ static void get_grid_mapping_latlon(
//Count the number of cells to be mapped to TO_GRID
//Following the logic at DataPlane::two_to_one(int x, int y) n = y*Nx + x;
for (int yIdx=0; yIdx<from_lat_count; yIdx++) {
int lat_offset = yIdx;
for (int xIdx=0; xIdx<from_lon_count; xIdx++) {
int lon_offset = xIdx;
int coord_offset = from_dp.two_to_one(xIdx, yIdx, to_north);
if( skip_times != nullptr && skip_times[coord_offset] ) continue;
float lat = latitudes[coord_offset];
float lon = longitudes[coord_offset];
if (is_2d) {
lon_offset = lat_offset = coord_offset;
if( skip_times != 0 && skip_times[coord_offset] ) continue;
}
float lat = latitudes[lat_offset];
float lon = longitudes[lon_offset];
if( lat < MISSING_LATLON || lon < MISSING_LATLON ) continue;
to_grid.latlon_to_xy(lat, -1.0*rescale_lon(lon), x, y);
int idx_x = nint(x);
Expand Down Expand Up @@ -2239,15 +2244,17 @@ static bool get_grid_mapping(const Grid &fr_grid, const Grid &to_grid, IntArray
}
else if (data_size > 0) {
int last_idx = data_size - 1;
auto latitudes = new float[data_size];
auto longitudes = new float[data_size];
int lat_count = get_data_size(&var_lat);
int lon_count = get_data_size(&var_lon);
auto latitudes = new float[lat_count];
auto longitudes = new float[lon_count];
status = get_nc_data(&var_lat, latitudes);
if( status ) status = get_nc_data(&var_lon, longitudes);
if( status ) {
get_grid_mapping_latlon(from_dp, to_dp, to_grid, cellMapping,
latitudes, longitudes, from_lat_count,
from_lon_count, skip_times,
!fr_grid.get_swap_to_north());
!fr_grid.get_swap_to_north(), (lon_count==data_size));

if (is_eq(latitudes[0], latitudes[last_idx]) ||
is_eq(longitudes[0], longitudes[last_idx])) {
Expand Down Expand Up @@ -2367,6 +2374,8 @@ static void get_grid_mapping(const Grid &fr_grid, const Grid &to_grid, IntArray
to_dp.set_size(to_lon_count, to_lat_count);

if (data_size > 0) {
int lat_count = data_size;
int lon_count = data_size;
auto latitudes = (float *)nullptr;
auto longitudes = (float *)nullptr;
auto latitudes_buf = (float *)nullptr;
Expand All @@ -2388,6 +2397,8 @@ static void get_grid_mapping(const Grid &fr_grid, const Grid &to_grid, IntArray
NcVar var_lat = get_nc_var(coord_nc_in, var_name_lat);
NcVar var_lon = get_nc_var(coord_nc_in, var_name_lon);
if (IS_VALID_NC(var_lat) && IS_VALID_NC(var_lon)) {
lat_count = get_data_size(&var_lat);
lon_count = get_data_size(&var_lon);
get_nc_data(&var_lat, latitudes);
get_nc_data(&var_lon, longitudes);
}
Expand Down Expand Up @@ -2444,16 +2455,14 @@ static void get_grid_mapping(const Grid &fr_grid, const Grid &to_grid, IntArray
}
}
}
else {
if (fr_grid.info().gi) {
grid_data.copy(fr_grid.info().gi);
grid_data.compute_lat_lon();
latitudes = grid_data.lat_values;
longitudes = grid_data.lon_values;
if (!file_exists(geostationary_file.c_str())) {
save_geostationary_data(geostationary_file,
latitudes, longitudes, grid_data);
}
else if (fr_grid.info().gi) {
grid_data.copy(fr_grid.info().gi);
grid_data.compute_lat_lon();
latitudes = grid_data.lat_values;
longitudes = grid_data.lon_values;
if (!file_exists(geostationary_file.c_str())) {
save_geostationary_data(geostationary_file,
latitudes, longitudes, grid_data);
}
}
if (nullptr == latitudes) {
Expand All @@ -2468,7 +2477,7 @@ static void get_grid_mapping(const Grid &fr_grid, const Grid &to_grid, IntArray
check_lat_lon(data_size, latitudes, longitudes);
get_grid_mapping_latlon(from_dp, to_dp, to_grid, cellMapping, latitudes,
longitudes, from_lat_count, from_lon_count, nullptr,
!fr_grid.get_swap_to_north());
!fr_grid.get_swap_to_north(), (lon_count==data_size));
}

if (latitudes_buf) delete [] latitudes_buf;
Expand Down

0 comments on commit 2e14724

Please sign in to comment.