Skip to content

Instantly share code, notes, and snippets.

@rsignell-usgs
Created August 7, 2023 20:49
Show Gist options
  • Save rsignell-usgs/55e7ae0b8be503fb87a26ba050abf266 to your computer and use it in GitHub Desktop.
Save rsignell-usgs/55e7ae0b8be503fb87a26ba050abf266 to your computer and use it in GitHub Desktop.
gene.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "89fd975b-7e94-4c56-a567-a4911a934dd2",
"metadata": {},
"outputs": [],
"source": [
"import fsspec\n",
"import xarray as xr"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1322d8b7-9fd8-4790-a260-8465f8d8c45b",
"metadata": {},
"outputs": [],
"source": [
"fs = fsspec.filesystem('file')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "bccb6150-6517-4ac0-9ce6-c3d58222190a",
"metadata": {},
"outputs": [],
"source": [
"flist = fs.glob('/caldera/hytest_scratch/scratch/gzt5142/LOCA_hist/*historical.nc')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c22bad0f-fc1d-43de-a362-b121bf562fed",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/rsignell/miniconda3/envs/pangeo/lib/python3.9/site-packages/xarray/backends/plugins.py:71: RuntimeWarning: Engine 'rasterio' loading failed:\n",
"libLerc.so.4: cannot open shared object file: No such file or directory\n",
" warnings.warn(f\"Engine {name!r} loading failed:\\n{ex}\", RuntimeWarning)\n"
]
}
],
"source": [
"ds = xr.open_dataset(fs.open(flist[0]))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b403477e-62c7-4121-b68c-61348b9a0a9a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<xarray.Dataset>\n",
"Dimensions: (lat: 490, bnds: 2, lon: 960,\n",
" time: 20454)\n",
"Coordinates:\n",
" * lat (lat) float64 23.41 23.47 ... 53.97\n",
" * lon (lon) float64 234.0 234.1 ... 294.0\n",
" * time (time) datetime64[ns] 1950-01-01T12:0...\n",
"Dimensions without coordinates: bnds\n",
"Data variables:\n",
" lat_bnds (lat, bnds) float64 ...\n",
" lon_bnds (lon, bnds) float64 ...\n",
" time_bnds (time, bnds) datetime64[ns] ...\n",
" pr_ACCESS1-0_r1i1p1_historical (time, lat, lon) float32 ...\n",
" tasmax_ACCESS1-0_r1i1p1_historical (time, lat, lon) float32 ...\n",
" tasmin_ACCESS1-0_r1i1p1_historical (time, lat, lon) float32 ...\n",
"Attributes:\n",
" history: Mon Jul 23 14:28:17 2012: ncks -d time,0,3651 pr_day_ACCE...\n",
" creation_date: 2012-01-17T12:34:13Z\n",
" Conventions: CF-1.4\n",
" title: LOCA (LOcal Constructed Analogs) statistical downscaling ...\n"
]
}
],
"source": [
"print(ds)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7b223922-d675-46a3-9345-888799a37e64",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Data variables:\n",
" lat_bnds (lat, bnds) float64 ...\n",
" lon_bnds (lon, bnds) float64 ...\n",
" time_bnds (time, bnds) datetime64[ns] ...\n",
" pr_ACCESS1-0_r1i1p1_historical (time, lat, lon) float32 ...\n",
" tasmax_ACCESS1-0_r1i1p1_historical (time, lat, lon) float32 ...\n",
" tasmin_ACCESS1-0_r1i1p1_historical (time, lat, lon) float32 ..."
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds.data_vars"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "931a6fa3-1fd0-42d3-80fb-ca0e0731b6a8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'chunksizes': (1, 40, 40),\n",
" 'fletcher32': False,\n",
" 'shuffle': False,\n",
" 'zlib': True,\n",
" 'complevel': 1,\n",
" 'source': '<fsspec.implementations.local.LocalFileOpener object at 0x7ffd20f4c670>',\n",
" 'original_shape': (20454, 490, 960),\n",
" 'dtype': dtype('int16'),\n",
" 'missing_value': -9999,\n",
" '_FillValue': -9999,\n",
" 'scale_factor': 0.1}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds['tasmax_ACCESS1-0_r1i1p1_historical'].encoding"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "85f2cd83-8abb-4aa5-8ff5-08f89f37edfe",
"metadata": {},
"outputs": [],
"source": [
"chunk_plan={\n",
" 'time': 2920, \n",
" 'lon': 128, \n",
" 'lat': 128\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "4385d177-f2ce-4ad1-a217-d74011dda419",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7.004794520547946"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(ds.time)/chunk_plan['time']"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "18e9dccc-0b5e-4b92-aec7-ae9ca6dd051c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"28.7109375"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(len(ds.lon)*len(ds.lat))/(chunk_plan['lon']*chunk_plan['lat'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb8ecb4c-9b25-4164-b05f-e3aea188f569",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "conda:pangeo",
"language": "python",
"name": "conda-pangeo"
},
"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.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment