From d90d5bfc3ba323231b40c05a18cb7188bae74029 Mon Sep 17 00:00:00 2001 From: bekozi Date: Fri, 17 May 2019 11:02:59 -0600 Subject: [PATCH] FIX: Fix unnecessary coordinate reduction for SCRIP in chunked-rwg #497 - Also added some more example scripts - Moved CTSM examples into own directory --- .../convert_to_shapefile.py | 24 +++++++++++ .../do-chunked-rwg-ctsm-scrip.sh | 42 +++++++++++++++++++ .../do-chunked-rwg-with-ss-esmf-unstruct.sh | 0 .../get_grid_spatial_resolution.py | 20 +++++++++ src/ocgis/spatial/grid_chunker.py | 5 ++- 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 examples/CTSM-MapData-Workflow/convert_to_shapefile.py create mode 100644 examples/CTSM-MapData-Workflow/do-chunked-rwg-ctsm-scrip.sh rename examples/{ => CTSM-MapData-Workflow}/do-chunked-rwg-with-ss-esmf-unstruct.sh (100%) create mode 100644 examples/CTSM-MapData-Workflow/get_grid_spatial_resolution.py diff --git a/examples/CTSM-MapData-Workflow/convert_to_shapefile.py b/examples/CTSM-MapData-Workflow/convert_to_shapefile.py new file mode 100644 index 000000000..039c56af3 --- /dev/null +++ b/examples/CTSM-MapData-Workflow/convert_to_shapefile.py @@ -0,0 +1,24 @@ +import ocgis + +# Path to the file of interest +PATH = "/home/benkoziol/Dropbox/dtmp/ctsm-grids/SCRIPgrid_4x5_nomask_c110308.nc" +# The metadata/IO driver for the data file. +DRIVER="netcdf-scrip" + +# ---------------------------------------------------------------------------------------------------------------------- + +# The request dataset provides metadata for a field. +rd = ocgis.RequestDataset(PATH, driver=DRIVER) +# The field is similar to ESMF Field and provides access to grids, etc. It can also hold multiple data variables. +field = rd.create_field() +# Convert the coordinates into Shapely polygon objects. +print("This is the geometry abstraction that will be used: {}".format(field.grid.abstraction)) +field.set_abstraction_geom() +print("Sending polygons to a shapefile...") +field.geom.write_vector("/tmp/shp_polygon.shp") + +# Can also write the points in another way... +field.grid.abstraction = "point" +points = field.grid.get_abstraction_geometry() +points.write_vector("/tmp/shp_points.shp") + diff --git a/examples/CTSM-MapData-Workflow/do-chunked-rwg-ctsm-scrip.sh b/examples/CTSM-MapData-Workflow/do-chunked-rwg-ctsm-scrip.sh new file mode 100644 index 000000000..b5379083c --- /dev/null +++ b/examples/CTSM-MapData-Workflow/do-chunked-rwg-ctsm-scrip.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +# The base or working directory. +WD=~/scratch/ + +# Directory to write all the chunked files to. +CHUNKDIR=${WD}/chunking + +# Path to the source field/grid netCDF file. +SRC="/home/ubuntu/Dropbox/dtmp/ctsm-grids/SCRIPgrid_0.5x0.5_AVHRR_c110228.nc" +SRCTYPE="SCRIP" +SRC_MAXSPATIALRES=0.5 + +# Path to the destination field/grid netCDF file. +DST="/home/ubuntu/Dropbox/dtmp/ctsm-grids/SCRIPgrid_4x5_nomask_c110308.nc" +DSTTYPE="SCRIP" +DST_MAXSPATIALRES=0.5 + +# Chunking composition for the destination grid (n_chunks_y,n_chunks_x). +NCHUNKS_DST=10 + +# Path to the merged output weight file. +WEIGHTS=${WD}/weights.nc + +# The ocli python file or executable target with the subcommand for chunked +# regrid weight generation. +CRWG="ocli chunked-rwg " +#CRWG="python /home/ubuntu/Dropbox/NESII/project/ocg/git/ocgis/src/ocgis/ocli.py chunked-rwg " + +# An optional execution prefix. +EXEC_PREFIX="" +#EXEC_PREFIX="mpirun -n `nproc`" + +############################################################################### + +# Generate the weights using the spatially subset source field and destination +# field. + +${EXEC_PREFIX} ${CRWG} --source ${SRC} --destination ${DST} \ + --esmf_regrid_method CONSERVE --nchunks_dst ${NCHUNKS_DST} --wd ${CHUNKDIR} \ + --weight ${WEIGHTS} --persist --esmf_src_type ${SRCTYPE} --esmf_dst_type ${DSTTYPE} \ + --src_resolution ${SRC_MAXSPATIALRES} --dst_resolution ${DST_MAXSPATIALRES} --verbose diff --git a/examples/do-chunked-rwg-with-ss-esmf-unstruct.sh b/examples/CTSM-MapData-Workflow/do-chunked-rwg-with-ss-esmf-unstruct.sh similarity index 100% rename from examples/do-chunked-rwg-with-ss-esmf-unstruct.sh rename to examples/CTSM-MapData-Workflow/do-chunked-rwg-with-ss-esmf-unstruct.sh diff --git a/examples/CTSM-MapData-Workflow/get_grid_spatial_resolution.py b/examples/CTSM-MapData-Workflow/get_grid_spatial_resolution.py new file mode 100644 index 000000000..916294b80 --- /dev/null +++ b/examples/CTSM-MapData-Workflow/get_grid_spatial_resolution.py @@ -0,0 +1,20 @@ +import ocgis + +# Path to the file of interest +PATH = "/home/benkoziol/Dropbox/dtmp/ctsm-grids/SCRIPgrid_4x5_nomask_c110308.nc" +# The metadata/IO driver for the data file. +DRIVER="netcdf-scrip" + +# The request dataset provides metadata for a field. +rd = ocgis.RequestDataset(PATH, driver=DRIVER) +# The field is similar to ESMF Field and provides access to grids, etc. It can also hold multiple data variables. +field = rd.create_field() +print("Average spatial resolution: {}".format(field.grid.resolution)) +print("Maximum spatial resolution: {}".format(field.grid.resolution_max)) + +#----------------------------------------------------------------------------------------------------------------------- + +print("\nDo a simple data file inspection...") +rd.inspect() +print("\nThis is the OCGIS interpretation of the metadata (can be customized)...") +rd.dimension_map.pprint(as_dict=True) \ No newline at end of file diff --git a/src/ocgis/spatial/grid_chunker.py b/src/ocgis/spatial/grid_chunker.py index edf94c8fb..6448fb7bc 100644 --- a/src/ocgis/spatial/grid_chunker.py +++ b/src/ocgis/spatial/grid_chunker.py @@ -505,8 +505,9 @@ def iter_src_grid_subsets(self, yield_dst=False, yield_idx=None): else: target_grid = dst_grid_subset - # Try to reduce the coordinates in the case of unstructured grid data. - if hasattr(target_grid, 'reduce_global') and Topology.POLYGON in target_grid.abstractions_available: + # Try to reduce the coordinates in the case of unstructured grid data. Ensure the data also has a + # coordinate index. SCRIP grid files, for example, do not have a coordinate index like UGRID. + if hasattr(target_grid, 'reduce_global') and Topology.POLYGON in target_grid.abstractions_available and target_grid.cindex is not None: ocgis_lh(logger='grid_chunker', msg='starting reduce_global for dst_grid_subset', level=logging.DEBUG) target_grid = target_grid.reduce_global()