A Soft Starter: Max Daylight¶
This notebook will show you the process of using a simple geocat-comp function.
Finding an old NCL function¶
Say you have an old NCL script that you are looking to convert into python that uses the daylight_fao56
routine and you want to see if GeoCAT has converted the function to python.
The best way to find the python equivalent function, if it exists, is to go to GeoCAT-comp’s documentation and search for the function name, as shown below. If functions are a translation of NCL functions, they will have their NCL counterparts linked in the documentation.
We can see that the GeoCAT-comp equivalent of NCL’s daylight_fao56
is max_daylight
, which we can now use in our python script to provide the same functionality as existed in NCL.
Imports¶
[1]:
from geocat.comp import max_daylight
import xarray as xr
import numpy as np
Now, let’s look at max_daylight
’s documentation to see what it does.
Calculate maximum daylight hours¶
So, let’s get the number of daylight hours for an entire year in Boulder (lat approx 40N)
[2]:
daylight_hours = max_daylight(np.arange(0, 365), 40)
Now, let’s put our data into an xarray DataArray.
This could, of course, have all been done in one step, but we wanted to introduce the absolute basics of geocat-comp.
[3]:
daylight_hours = xr.DataArray(daylight_hours, dims=("days", "hours of daylight"))
daylight_hours
[3]:
<xarray.DataArray (days: 365, hours of daylight: 1)> array([[ 9.21056836], [ 9.22130351], [ 9.23298075], [ 9.24559158], [ 9.25912689], [ 9.27357699], [ 9.2889316 ], [ 9.30517995], [ 9.32231074], [ 9.34031217], [ 9.35917201], [ 9.3788776 ], [ 9.39941587], [ 9.42077339], [ 9.44293638], [ 9.46589074], [ 9.4896221 ], [ 9.51411584], [ 9.53935709], [ 9.56533079], ... [ 9.20073221], [ 9.19190951], [ 9.1840506 ], [ 9.17716138], [ 9.171247 ], [ 9.16631194], [ 9.16235992], [ 9.15939395], [ 9.15741629], [ 9.15642843], [ 9.15643115], [ 9.15742443], [ 9.15940751], [ 9.16237889], [ 9.1663363 ], [ 9.17127674], [ 9.17719647], [ 9.18409103], [ 9.19195523], [ 9.20078319]]) Dimensions without coordinates: days, hours of daylight
A little visualization¶
And now, let’s take a little peek at the data using xarray’s built-in plotting functionality:
[4]:
daylight_hours.plot()
Matplotlib is building the font cache; this may take a moment.
[4]:
[<matplotlib.lines.Line2D at 0x7f9fb3426350>]