Skip to content

Instantly share code, notes, and snippets.

@rsignell-usgs
Created August 13, 2021 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsignell-usgs/4ea6caac48319e0f39cd2fd1ecaee027 to your computer and use it in GitHub Desktop.
Save rsignell-usgs/4ea6caac48319e0f39cd2fd1ecaee027 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "63feb1dd-3cce-4d10-baf3-eef39665dfb2",
"metadata": {},
"source": [
"#### Encoding for streamflow from single NetCDF file from S3"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f7e649d6-f4db-452d-a1bc-11ee19cfeaba",
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr\n",
"import fsspec"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "94dbcce5-1dfe-4ce7-a7bc-2adadde2149a",
"metadata": {},
"outputs": [],
"source": [
"url = 's3://noaa-nwm-retrospective-2-1-pds/model_output/2020/202001011100.CHRTOUT_DOMAIN1.comp'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b2e7adcc-f5bd-4472-b768-a34f1e099794",
"metadata": {},
"outputs": [],
"source": [
"fs = fsspec.filesystem('s3', anon=True)\n",
"ds = xr.open_dataset(fs.open(url), drop_variables='reference_time', chunks={})"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e8f1a977-b166-4194-a055-e614ee837e62",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'chunksizes': (2776738,),\n",
" 'fletcher32': False,\n",
" 'shuffle': False,\n",
" 'zlib': True,\n",
" 'complevel': 2,\n",
" 'source': '<File-like object S3FileSystem, noaa-nwm-retrospective-2-1-pds/model_output/2020/202001011100.CHRTOUT_DOMAIN1.comp>',\n",
" 'original_shape': (2776738,),\n",
" 'dtype': dtype('int32'),\n",
" 'missing_value': array([-999900], dtype=int32),\n",
" '_FillValue': array([-999900], dtype=int32),\n",
" 'scale_factor': array([0.01], dtype=float32),\n",
" 'add_offset': array([0.], dtype=float32),\n",
" 'coordinates': 'latitude longitude'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds.streamflow.encoding"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "87a58f92-796b-404e-958c-4b6015d9421d",
"metadata": {},
"outputs": [],
"source": [
"ds.to_netcdf('roundtrip.nc', mode='w')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a2975878-a7d7-4ec5-a68d-09aa8804aaf1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'zlib': True,\n",
" 'shuffle': False,\n",
" 'complevel': 2,\n",
" 'fletcher32': False,\n",
" 'contiguous': False,\n",
" 'chunksizes': (2776738,),\n",
" 'source': '/home/shared/users/rsignell/notebooks/NWM/roundtrip.nc',\n",
" 'original_shape': (2776738,),\n",
" 'dtype': dtype('int32'),\n",
" 'missing_value': -999900,\n",
" '_FillValue': -999900,\n",
" 'scale_factor': 0.01,\n",
" 'add_offset': 0.0,\n",
" 'coordinates': 'latitude longitude'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xr.open_dataset('roundtrip.nc').streamflow.encoding"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:pangeo]",
"language": "python",
"name": "conda-env-pangeo-py"
},
"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.8.10"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment