Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ascii to netcdf #202

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions notebooks/create_netcdf_mesh_description_file.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create netCDF mesh description file"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook gives an example of how to:\n",
"a) read the ASCII files that make up a FESOM mesh \n",
"b) compute a number of mesh caracteristics. \n",
"c) save the mesh as a [CDO](https://code.mpimet.mpg.de/projects/cdo) readable netCDF file for further use."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import pyfesom2 as pf"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"griddir='/work/ab0246/a270092/input/fesom2/core2/'"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"reading node (grid point) coordinates and coast information ...\n",
"... done. grid contains 126858 nodes of which 9311.0 are coastal (according to info in nod2d.out).\n",
"... execution Time: 0.47 seconds\n",
"reading neighbourhood (triangular elements) information ...\n",
"... done. grid contains 244659 triangular elements.\n",
"... execution Time: 0.65 seconds\n",
"reordering clockwise triangular elements counterclockwise ...\n",
"... done. 5 of 244659 elements reordered.\n",
"... execution Time: 5.81 seconds\n",
"reading 3D information ...\n",
"... done. Grid over all levels contains 3705887 elements.\n",
"... execution Time: 0.21 seconds\n",
"searching all neighbors of each node based on the triangular elements ...\n",
"... done. number of neighbors ranges from 3 to 9 nodes and is 5.8592 on average.\n",
"... execution Time: 12.8 seconds\n",
"determining which elements include coastal nodes ...\n",
"... done. grid features 9365 elements that contain coastal nodes.\n",
"... execution Time: 1.04 seconds\n",
"computing barycenters (centroids) for all triangular elements ...\n",
"... done.\n",
"... execution Time: 7.19 seconds\n",
"generate 'stamp polygons' around each node ...\n",
"... done. number of 'stamp polygon' vertices per node ranges from 6 (before padding) to 18 and is 11.7184 on average (before padding).\n",
"... execution Time: 24.23 seconds\n",
"computing element and 'stamp polygon' areas ...\n",
"... done.\n",
"... execution Time: 57.43 seconds\n"
]
}
],
"source": [
"grid = pf.read_fesom_ascii_grid(griddir=griddir)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the grid has 3140 nodes (grid points) with up to 16 stamp polygon vertices per node.\n",
"the grid has 48 vertical levels.\n",
"Horizontal grid description file complete.\n",
"You can use this file to set the horizontal grid of a corresponding NetCDF file with 'cdo setgrid,/work/ab0246/a270092/input/fesom2/pi_mesh/mesh.nc ifile.nc ofile.nc'.\n"
]
}
],
"source": [
"pf.write_mesh_to_netcdf(grid, ofile=griddir+'mesh.nc',overwrite=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets check the grid we crated"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dimensions:\n",
"ncells: 3140\n",
"vertices: 16\n",
"nlinks_max: 8\n",
"ntriags: 5839\n",
"Three: 3\n",
"nlev: 48\n",
"nlev_bnds: 49\n",
"\n",
"Variables:\n",
"lon: (3140,) \n",
"lon_bnds: (16, 3140) \n",
"lat: (3140,) \n",
"lat_bnds: (16, 3140) \n",
"cell_area: (3140,) \n",
"node_node_links: (8, 3140) \n",
"triag_nodes: (3, 5839) \n",
"coast: (3140,) \n",
"depth: (48,) \n",
"depth_bnds: (49,) \n",
"depth_lev: (3140,) \n",
"\n",
"Global attributes:\n",
"Conventions: CF-1.4\n",
"History: 2023-08-03 15:20:22 GMT; Grid description file generated with pyfesom2 version :0.2.0.; Grid written with: writeCDO(grid, ofile='/work/ab0246/a270092/input/fesom2/pi_mesh/mesh.nc', netcdf=True, netcdf_prec='double', ascii_digits=inf, overwrite=True, verbose=True, cell_area=True, node_node_links=True, triag_nodes=True, coast=True, depth=True, ofile_ZAXIS=None, fesom2velocities=False, conventions='original')\n"
]
}
],
"source": [
"import netCDF4 as nc\n",
"\n",
"# Replace 'your_file.nc' with the path to your actual NetCDF file\n",
"file_path = griddir+'mesh.nc'\n",
"\n",
"# Open the NetCDF file in read mode\n",
"nc_file = nc.Dataset(file_path, 'r')\n",
"\n",
"# Get information about the dimensions, variables, and global attributes\n",
"print(\"Dimensions:\")\n",
"for dimname, dim in nc_file.dimensions.items():\n",
" print(f\"{dimname}: {len(dim)}\")\n",
"\n",
"print(\"\\nVariables:\")\n",
"for varname, var in nc_file.variables.items():\n",
" print(f\"{varname}: {var.shape} {var.units if 'units' in var.ncattrs() else ''}\")\n",
"\n",
"print(\"\\nGlobal attributes:\")\n",
"for attrname in nc_file.ncattrs():\n",
" print(f\"{attrname}: {getattr(nc_file, attrname)}\")\n",
"\n",
"# Close the NetCDF file after reading\n",
"nc_file.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 1 addition & 0 deletions pyfesom2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
from .datasets import open_dataset
from .accessor import FESOMDataArray as _FESOMDataArray
from .transport import cross_section_transport
from .ascii_to_netcdf import read_fesom_ascii_grid, write_mesh_to_netcdf
Loading
Loading