This is a short follow up of the previous post about komod module, that is essentially a set of wrapper functions for PyNGL module. Here I am going to show how to plot a grid of your model and how to draw a transect. I am going to use the same data set af before: mean temperature from the World Ocean Atlas 2009 (5 deg. resolution).
Import modules:
import komod
import Nio
from IPython.display import Image
import numpy as np
Get variables from the netCDF file:
ff =Nio.open_file('temperature_annual_5deg.nc')
temp = ff.variables['t_mn'][:]
lat = ff.variables['lat'][:]
lon = ff.variables['lon'][:]
lev = ff.variables['depth'][:]
Create lat/lon mesh grid:
lon_m, lat_m = np.meshgrid(lon,lat)
Now we can plot our grid. This function use centers of the grid boxes, not corners, so result might not be a perfectly accurate representation of your grid, but rather rough estimate of its shape and density. Only necessary input parameters are 2d arrays of lats and lons:
komod.pltgrd(lon_m,lat_m, region = 'Global', add_cyclic=True)
!convert -scale 70% -rotate -90 grid.ps grid.png
Image(filename='grid.png')
g = Nio.open_file('GR15s.nc')
lat_g = g.variables['grid_center_lat'][:]
lon_g = g.variables['grid_center_lon'][:]
komod.pltgrd(lon_g,lat_g, region = 'Global', add_cyclic=True)
!convert -scale 70% -rotate -90 grid.ps grid.png
Image(filename='grid.png')
Very dense grid, you can hardly see any details. We can show only every n$^{th}$ element of grid by specifying every parameter:
komod.pltgrd(lon_g,lat_g, region = 'Global', every=3, add_cyclic=True)
!convert -scale 70% -rotate -90 grid.ps grid.png
Image(filename='grid.png')
Yes I know there is an ugly white in the middle, but fixing it would require a bit more effort than I want to invest in this function :) At the end it do the thing it was created for - show how the grid looks like.
You can look at the regions as well:
komod.pltgrd(lon_g,lat_g, region = 'Arctic', every=1, add_cyclic=True)
!convert -scale 70% -rotate -90 grid.ps grid.png
Image(filename='grid.png')
You can see that the Fram Strait have much better resolution than the Bering Strait. North pole is still a problem :(
Now we are almost ready to plot a transect. But first let's have a look at its position on the map and how its related to the grid. For this we use komod.pltgrd_line function. We have to provide lat/lon arrays with coordinates and also start and end points of your transect:
komod.pltgrd_line(lon_m, lat_m, lon1=335, lat1=50, lon2=335, lat2=-50, every=1, region = 'Global', add_cyclic=True )
!convert -scale 70% -rotate -90 grid_line.ps grid_line.png
Image(filename='grid_line.png')
It's maybe a bit hard to see, but there is a line now at the center of the Atlantic Ocean. This function uses Ngl.gc_interp to calculate positions of the points between the end points. Number of intermediate points is 10 by default, and can be changed (npoints parameter).
After we find a good position for our transect, we have to get the data to plot it. This is done with komod.get_transect and you have to provide lat/lon arrays, 3D array with your data and start/end points of the transect. Two main variables that this function return is data_prof with vertical profiles at the points along your line (10 point by default) and x_kilometers - the distance (in kilometers) between your start point and every point of your transect. This variables will be used as an input for plotting function.
data_prof, x_kilometers, m_grid, n_grid = komod.get_transect(lat_m, lon_m, temp[0,0:20,:,:], lon1=335, lat1=50, lon2=335, lat2=-50)
Finally we can plot our transect:
komod.plt_transect(distances=x_kilometers , levels=lev[0:20], vvalues=data_prof, datamin = -2., datamax = 30., datastep = 1)
!convert -scale 50% -rotate -90 output.ps output.png
Image(filename='output.png')
With only 10 points between start and end the transect look kind of rough. Let's increase number of points:
data_prof, x_kilometers, m_grid, n_grid = komod.get_transect(lat_m, lon_m, temp[0,0:20,:,:],
lon1=335, lat1=50, lon2=335, lat2=-50, npoints=30)
komod.plt_transect(distances=x_kilometers , levels=lev[0:20], vvalues=data_prof, datamin = -2., datamax = 30., datastep = 1)
!convert -scale 50% -rotate -90 output.ps output.png
Image(filename='output.png')
Now it look squared :) The reason is that for some points on the line the same grid boxes are chosen, so we have repetitions. You can see it in the x_kilometers variable - some distances occur twice:
x_kilometers
We can filter out this repetition by norep parameter:
data_prof, x_kilometers, m_grid, n_grid = komod.get_transect(lat_m, lon_m, temp[0,0:20,:,:], norep=True,
lon1=335, lat1=50, lon2=335, lat2=-50, npoints=30)
komod.plt_transect(distances=x_kilometers , levels=lev[0:20], vvalues=data_prof, datamin = -2., datamax = 30., datastep = 1)
!convert -scale 50% -rotate -90 output.ps output.png
Image(filename='output.png')
Better :)
x_kilometers
komod.plt_transect(distances=x_kilometers , levels=lev[0:20],
vvalues=data_prof, datamin = -2., datamax = 30., datastep = 2,
vpWidthF=0.5, vpHeightF=0.3)
!convert -scale 50% -rotate -90 output.ps output.png
Image(filename='output.png')
Comments !