Skip to content

Instantly share code, notes, and snippets.

@ccarouge
Last active August 16, 2018 02:24
Show Gist options
  • Save ccarouge/a34ccd19a86586b0c0b8804aa6bce646 to your computer and use it in GitHub Desktop.
Save ccarouge/a34ccd19a86586b0c0b8804aa6bce646 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr\n",
"import glob"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"ds = xr.open_mfdataset(\"/g/data/w35/ccc561/SURFACEMODEL/LIS_HIST_*\",decode_times=False,concat_dim=\"time\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<xarray.Dataset>\n",
"Dimensions: (RelSMC_profiles: 6, SmFrozFrac_profiles: 6, SmLiqFrac_profiles: 6, SoilMoist_profiles: 6, SoilTemp_profiles: 6, east_west: 229, north_south: 109, time: 816)\n",
"Coordinates:\n",
" * time (time) float64 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ...\n",
"Dimensions without coordinates: RelSMC_profiles, SmFrozFrac_profiles, SmLiqFrac_profiles, SoilMoist_profiles, SoilTemp_profiles, east_west, north_south\n",
"Data variables:\n",
" lat (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" lon (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Swnet_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Lwnet_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Qle_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Qh_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Qg_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Snowf_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Rainf_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Evap_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Qs_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Qsb_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" AvgSurfT_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Albedo_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" SWE_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" SnowDepth_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" SoilMoist_inst (time, SoilMoist_profiles, north_south, east_west) float32 dask.array<shape=(816, 6, 109, 229), chunksize=(1, 6, 109, 229)>\n",
" SoilTemp_inst (time, SoilTemp_profiles, north_south, east_west) float32 dask.array<shape=(816, 6, 109, 229), chunksize=(1, 6, 109, 229)>\n",
" SmLiqFrac_inst (time, SmLiqFrac_profiles, north_south, east_west) float32 dask.array<shape=(816, 6, 109, 229), chunksize=(1, 6, 109, 229)>\n",
" SmFrozFrac_inst (time, SmFrozFrac_profiles, north_south, east_west) float32 dask.array<shape=(816, 6, 109, 229), chunksize=(1, 6, 109, 229)>\n",
" SoilWet_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" ECanop_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" TVeg_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" ESoil_tavg (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" CanopInt_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" SnowCover_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Wind_f_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Rainf_f_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Tair_f_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Qair_f_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Psurf_f_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" SWdown_f_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" LWdown_f_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Landmask_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Landcover_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Soiltype_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" SandFrac_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" ClayFrac_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" SiltFrac_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" Elevation_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" LAI_inst (time, north_south, east_west) float32 dask.array<shape=(816, 109, 229), chunksize=(1, 109, 229)>\n",
" RelSMC_inst (time, RelSMC_profiles, north_south, east_west) float32 dask.array<shape=(816, 6, 109, 229), chunksize=(1, 6, 109, 229)>\n",
"Attributes:\n",
" missing_value: -9999.0\n",
" NUM_SOIL_LAYERS: 6\n",
" SOIL_LAYER_THICKNESSES: [ 0.022 0.058 0.154 0.40900001...\n",
" title: LIS land surface model output\n",
" institution: NASA GSFC\n",
" source: \n",
" history: created on date: 2018-06-13T14:18:26.063\n",
" references: Kumar_etal_EMS_2006, Peters-Lidard_etal_ISSE_2007\n",
" conventions: CF-1.6\n",
" comment: website: http://lis.gsfc.nasa.gov/\n",
" MAP_PROJECTION: EQUIDISTANT CYLINDRICAL\n",
" SOUTH_WEST_CORNER_LAT: 25.875\n",
" SOUTH_WEST_CORNER_LON: -124.875\n",
" DX: 0.25\n",
" DY: 0.25"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"a = [ i*30./(24.*60) for i in xrange(ds.time.size) ]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"ds['time']= a"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(0.020833333333333332)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds['time'][1].values"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"import cftime\n",
"import numpy as np\n",
"ds['time']=np.array(cftime.num2date(ds['time'].values,'days since 2002-11-03 19:30:00'),dtype='datetime64[ns]')"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.datetime64"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tt = ds['time'].values\n",
"type(tt[0])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"compress_values=dict(zlib=True, complevel=5)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/g/data3/hh5/public/apps/miniconda3/envs/analysis27-18.04/lib/python2.7/site-packages/xarray/coding/variables.py:135: RuntimeWarning: invalid value encountered in equal\n",
" condition |= data == fv\n",
"/g/data3/hh5/public/apps/miniconda3/envs/analysis27-18.04/lib/python2.7/site-packages/xarray/coding/variables.py:183: RuntimeWarning: invalid value encountered in multiply\n",
" data *= scale_factor\n"
]
}
],
"source": [
"ds.to_netcdf(\"test.nc\",encoding={var: compress_values for var in ds.data_vars},unlimited_dims=('time'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment