Created
December 11, 2018 09:11
-
-
Save lkluft/a218acbf237254a7e4fe9a292a3b1042 to your computer and use it in GitHub Desktop.
Copy a single group from one netCDF to another.
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": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import netCDF4\n\n\ndef copy_netcdf_group(source, destination, group):\n with netCDF4.Dataset(source) as src, netCDF4.Dataset(destination, 'w') as dst:\n dst.setncatts(src.__dict__)\n\n for name, dimension in src.dimensions.items():\n dst.createDimension(name, (len(dimension) if not dimension.isunlimited() else None))\n\n dst.createGroup(group)\n for name, variable in src[group].variables.items():\n x = dst[group].createVariable(name, variable.datatype, variable.dimensions)\n dst[group][name][:] = src[group][name][:]\n # copy variable attributes all at once via dictionary\n dst[group][name].setncatts(src[group][name].__dict__)\n \n \ncopy_netcdf_group('reference.nc', 'out.nc', 'atmosphere')", | |
"execution_count": 16, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.7.1", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "Copy a single group from one netCDF to another.", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment