Skip to content

Instantly share code, notes, and snippets.

@matthewturk
Created August 6, 2018 16:28
Show Gist options
  • Save matthewturk/3b04959f220c8e98c42ece490aff40ed to your computer and use it in GitHub Desktop.
Save matthewturk/3b04959f220c8e98c42ece490aff40ed 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 netCDF4\n",
"import numpy as np\n",
"import xarray as xr\n",
"import cartopy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"d = netCDF4.Dataset(\"grid1.nc\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"odict_keys(['time', 'x', 'y', 'z', 'origin_latitude', 'origin_longitude', 'origin_altitude', 'projection', 'ProjectionCoordinateSystem', 'radar_latitude', 'radar_longitude', 'radar_altitude', 'radar_time', 'radar_name', 'reflectivity', 'velocity', 'gate_id', 'differential_phase', 'ROI'])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.variables.keys()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"odict_keys(['time', 'z', 'y', 'x', 'nradar', 'nradar_str_length'])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.dimensions.keys()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"x, y, z = (d.variables[ax][:] for ax in 'xyz')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<class 'netCDF4._netCDF4.Variable'>\n",
"int32 ProjectionCoordinateSystem()\n",
" latitude_of_projection_origin: 36.4912000112\n",
" longitude_of_projection_origin: -97.5939000025\n",
" _CoordinateTransformType: Projection\n",
" _CoordinateAxes: x y z time\n",
" _CoordinateAxesTypes: GeoX GeoY Height Time\n",
" grid_mapping_name: azimuthal_equidistant\n",
" semi_major_axis: 6370997.0\n",
" inverse_flattening: 298.25\n",
" longitude_of_prime_meridian: 0.0\n",
" false_easting: 0.0\n",
" false_northing: 0.0\n",
"unlimited dimensions: \n",
"current shape = ()\n",
"filling on, default _FillValue of -2147483647 used"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.variables[\"ProjectionCoordinateSystem\"]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"ref = d.variables[\"reflectivity\"][:]\n",
"lat = d.variables[\"radar_latitude\"][0]\n",
"lon = d.variables[\"radar_longitude\"][0]\n",
"\n",
"plt.figure(figsize=(6, 3))\n",
"ax = plt.axes(projection=cartopy.crs.Mollweide())\n",
"#ax.set_global()\n",
"ax.coastlines()\n",
"\n",
"buff = ref[0,...].T\n",
"\n",
"x0, x1 = x.min(), x.max()\n",
"y0, y1 = y.min(), y.max()\n",
"\n",
"tmc = cartopy.crs.Orthographic(central_latitude=lat, central_longitude=lon)\n",
"ax.imshow(buff.max(axis=2), extent = [x0, x1, y0, y1], transform=tmc)\n",
"ax.set_xlim((x0, x1), transform=tmc)\n",
"ax.set_ylim((y0, y1), transform=tmc)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"lat, lon"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x, y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(buff.max(axis=2), extent = [x.min(), x.max(), y.min(), y.max()])"
]
}
],
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment