Last active
August 24, 2016 23:32
-
-
Save julienchastang/2129368a26ce0d85cff13cf0bc05cbf4 to your computer and use it in GitHub Desktop.
Gridded Data Template
This file contains 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": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Grab the usual imports:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" import netCDF4\n", | |
" import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Let's first create our netCDF file:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" try:\n", | |
" ncfile.close() # just to be safe, make sure dataset is not already open.\n", | |
" except:\n", | |
" pass\n", | |
" ncfile = netCDF4.Dataset('grid.nc',\n", | |
" mode='w',\n", | |
" format='NETCDF4_CLASSIC')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Let's define the size of our data:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" lat_size = 361\n", | |
" lon_size = 720\n", | |
" z_size = 1\n", | |
" time_size = 1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Create latitude, longitude, and time netCDF dimensions:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" lat_dim = ncfile.createDimension('lat_dim', lat_size)\n", | |
" lon_dim = ncfile.createDimension('lon_dim', lon_size)\n", | |
" z_dim = ncfile.createDimension('z_dim', z_size)\n", | |
" time_dim = ncfile.createDimension('time_dim', time_size)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Create the time dimension. Make sure [to follow UDUNITS guidance described in the CF convention](http://cfconventions.org/cf-conventions/v1.6.0/cf-conventions.html#time-coordinate) for the `time` and all variables. For example:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" time = ncfile.createVariable('time', np.float64, ('time_dim', ))\n", | |
" time.standard_name = 'time'\n", | |
" time.units = 'hours since 2016-01-15 00:00:00'\n", | |
" time.long_name = 'time'\n", | |
" time[:] = np.array([0])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Define the mesh:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" latitudes = np.linspace(-90.0, 90.0, num=lat_size)\n", | |
" longitudes = np.linspace(-180.0, 180.0, num=lon_size)\n", | |
" lons, lats = np.meshgrid(longitudes, latitudes)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Create `latitude` variable:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" latitude = ncfile.createVariable('latitude', np.float32, ('lat_dim',\n", | |
" 'lon_dim', ))\n", | |
" latitude.standard_name = 'latitude'\n", | |
" latitude.units = 'degrees_north'\n", | |
" latitude.long_name = 'latitude'\n", | |
" latitude[:] = lats" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Create `longitude` variable:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" longitude = ncfile.createVariable('longitude', np.float32, ('lat_dim',\n", | |
" 'lon_dim', ))\n", | |
" longitude.standard_name = 'longitude'\n", | |
" longitude.units = 'degrees_east'\n", | |
" longitude.long_name = 'longitude'\n", | |
" longitude[:] = lons" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Create `z` variable:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" z = ncfile.createVariable('z', np.float32, ('z_dim'))\n", | |
" z.standard_name = 'air_pressure_at_sea_level'\n", | |
" z.units = 'hPa'\n", | |
" z.long_name = 'mean sea level pressure'\n", | |
" z[:] = np.array([850])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Create a variable, say temperature:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" temperature = ncfile.createVariable('temperature', np.float64, (\n", | |
" 'time_dim', 'z_dim', 'lat_dim', 'lon_dim'))\n", | |
" temperature.units = 'Celsius'\n", | |
" temperature.standard_name = 'air_temperature'\n", | |
" temperature.coordinates = 'time z latitude longitude'\n", | |
" temperature[:] = np.zeros((time_size, z_size, lat_size, lon_size))\n", | |
" ncfile.Conventions = \"CF-1.6\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"Finally close the datasets we've been working with." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
" ncfile.close();" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "-" | |
} | |
}, | |
"source": [ | |
"You can load this netCDF file into the IDV." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": { | |
"autoscroll": "json-false", | |
"collapsed": false, | |
"ein.tags": [ | |
"worksheet-0" | |
], | |
"slideshow": { | |
"slide_type": "subslide" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"netcdf grid {\r\n", | |
"dimensions:\r\n", | |
"\tlat_dim = 361 ;\r\n", | |
"\tlon_dim = 720 ;\r\n", | |
"\tz_dim = 1 ;\r\n", | |
"\ttime_dim = 1 ;\r\n", | |
"variables:\r\n", | |
"\tdouble time(time_dim) ;\r\n", | |
"\t\ttime:standard_name = \"time\" ;\r\n", | |
"\t\ttime:units = \"hours since 2016-01-15 00:00:00\" ;\r\n", | |
"\t\ttime:long_name = \"time\" ;\r\n", | |
"\tfloat latitude(lat_dim, lon_dim) ;\r\n", | |
"\t\tlatitude:standard_name = \"latitude\" ;\r\n", | |
"\t\tlatitude:units = \"degrees_north\" ;\r\n", | |
"\t\tlatitude:long_name = \"latitude\" ;\r\n", | |
"\tfloat longitude(lat_dim, lon_dim) ;\r\n", | |
"\t\tlongitude:standard_name = \"longitude\" ;\r\n", | |
"\t\tlongitude:units = \"degrees_east\" ;\r\n", | |
"\t\tlongitude:long_name = \"longitude\" ;\r\n", | |
"\tfloat z(z_dim) ;\r\n", | |
"\t\tz:standard_name = \"air_pressure_at_sea_level\" ;\r\n", | |
"\t\tz:units = \"hPa\" ;\r\n", | |
"\t\tz:long_name = \"mean sea level pressure\" ;\r\n", | |
"\tdouble temperature(time_dim, z_dim, lat_dim, lon_dim) ;\r\n", | |
"\t\ttemperature:units = \"Celsius\" ;\r\n", | |
"\t\ttemperature:standard_name = \"air_temperature\" ;\r\n", | |
"\t\ttemperature:coordinates = \"time z latitude longitude\" ;\r\n", | |
"\r\n", | |
"// global attributes:\r\n", | |
"\t\t:Conventions = \"CF-1.6\" ;\r\n", | |
"}\r\n" | |
] | |
} | |
], | |
"source": [ | |
" !ncdump -h grid.nc" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.5.1" | |
}, | |
"name": "Grid.ipynb", | |
"widgets": { | |
"state": {}, | |
"version": "1.1.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment