#!/usr/bin/env python # coding: utf-8 # In[2]: # module to get sunrise and sunset times #import nbimporter from SunTimes import rise_set_func, rise_set # In[3]: # modules to read NetCDF4 files import xarray as xr import matplotlib.pyplot as plt import os # In[5]: # import hourly era5 data fpath = '/tiger/scratch/gpfs/GEOCLIM/demetray/era5/era5.2m_temperature.2000-07.nc' ds = xr.open_dataset(fpath) # In[6]: # transform longitude [0, 360) -> [-180, 180) ds = ds.roll(longitude=ds.longitude.size//2) lon_new = ds.longitude.where(ds.longitude<180, other=ds.longitude-360).values ds = ds.assign_coords(longitude=lon_new) # In[7]: ds.longitude # In[8]: # testing rise_set_func on new york, 1/17 # sunrise time should be: 12:17 UTC # sunset time should be: 21:56 UTC day = 17 mo = 1 yr = 2022 lat = 40.7 lon = -74 rise_set_func(lat, lon, day, mo, yr) # In[9]: # testing rise_set on sample xr.DataArrays from ERA5 hourly t2m r = rise_set(ds.latitude[0:2], ds.longitude[0:2], ds.time[0:2]) print(r) # In[ ]: