Last active
March 14, 2018 23:10
-
-
Save jhamman/b804a64a4ecbf1f26fd17f6494c5cbe8 to your computer and use it in GitHub Desktop.
ESMF Unstructured Mesh Regridding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Create unstructured grid file\n", | |
"\n", | |
"Convert a polygon shapefile to ESMF Unstructured Format. This script could be run in\n", | |
"parallel using MPI to increase performance." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import ocgis\n", | |
"from ocgis.constants import DriverKey" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2.1.0rc1\n", | |
"/home/jovyan/src/ocgis/src/ocgis/__init__.py\n" | |
] | |
} | |
], | |
"source": [ | |
"print(ocgis.__version__)\n", | |
"print(ocgis.__file__)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Path to the input ESRI Shapefile.\n", | |
"PATH_SHP = '/gcs/camels/basin_set_full_res/HCDN_nhru_final_671.shp'\n", | |
"# Path to the output ESMF unstructured NetCDF file.\n", | |
"MESH_NC = './camels.nc'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Create the request dataset and field.\n", | |
"rd = ocgis.RequestDataset(PATH_SHP, driver=DriverKey.VECTOR)\n", | |
"field = rd.get()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Convert the field geometry to an unstructured grid format based on the UGRID spec.\n", | |
"gc = field.geom.convert_to(use_geometry_iterator=True, pack=False, node_threshold=5000, split_interiors=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"/home/jovyan/src/ocgis/src/ocgis/variable/base.py:2435: FutureWarning: Conversion of the second argument of issubdtype from `str` to `str` is deprecated. In future, it will be treated as `np.str_ == np.dtype(str).type`.\n", | |
" or np.issubdtype(dtype, np.unicode) or dtype == str:\n" | |
] | |
} | |
], | |
"source": [ | |
"# When writing the data to file, convert to ESMF unstructured format.\n", | |
"gc.parent.write(MESH_NC, driver=DriverKey.NETCDF_ESMF_UNSTRUCT)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"netcdf camels {\n", | |
"dimensions:\n", | |
"\tconnectionCount = 4322780 ;\n", | |
"\telementCount = 671 ;\n", | |
"\tnodeCount = 4321040 ;\n", | |
"\tcoordDim = 2 ;\n", | |
"variables:\n", | |
"\tchar ocgis_coordinate_system ;\n", | |
"\t\tocgis_coordinate_system:proj4 = \"+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs \" ;\n", | |
"\t\tocgis_coordinate_system:ocgis_role = \"coordinate_system\" ;\n", | |
"\tint elementConn(connectionCount) ;\n", | |
"\t\telementConn:long_name = \"Node indices that define the element connectivity.\" ;\n", | |
"\t\telementConn:start_index = 0LL ;\n", | |
"\tint numElementConn(elementCount) ;\n", | |
"\t\tnumElementConn:long_name = \"Number of nodes per element.\" ;\n", | |
"\tdouble nodeCoords(nodeCount, coordDim) ;\n", | |
"\t\tnodeCoords:units = \"degrees\" ;\n", | |
"\t\tnodeCoords:long_name = \"Node coordinate values indexed by element connectivity.\" ;\n", | |
"\t\tnodeCoords:coordinates = \"x y\" ;\n", | |
"\n", | |
"// global attributes:\n", | |
"\t\t:gridType = \"unstructured\" ;\n", | |
"\t\t:version = \"0.9\" ;\n", | |
"}\n" | |
] | |
} | |
], | |
"source": [ | |
"!ncdump -h camels.nc" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Remap a piece of the GMET Ensemble" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import ESMF" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# this step never completes, why not?\n", | |
"mesh = ESMF.Mesh(filename=MESH_NC, filetype=ESMF.FileFormat.ESMFMESH)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"print(mesh)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python (camels)", | |
"language": "python", | |
"name": "camels" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment