!pip install xarray netCDF4 geopy


"""
Explanation: Querying Total Evapotranspiration from  OzWALD hosted by NCI via OpenDAP
This example shows how to query the Total Evapotranspiration from OpenDAP 
http://dap.nci.org.au/thredds/remoteCatalogService?catalog=http://dapds00.nci.org.au/thredds/catalog/ub8/au/OzWALD/daily/catalog.xml
The notebook provides widget interfaces for users to interact with. It then shows users how to perform various operations with xarray and then plot it using matplotlib - including locating the nearest grid to the selected city, and slicing the data by time.
End of explanation
"""


#setup widgets
import ipywidgets as widgets
w = widgets.Dropdown(
    options=['Melbourne', 'Sydney', 'Canberra', 'Brisbane', 'Adelaide', 'Hobart', 'Perth', 'Darwin'],
    description='Capital city:',
    disabled=False,
)
arrYears = [str(i) for i in range(2000,2017)]
wYear = widgets.SelectionSlider(
    options=arrYears,
    value=arrYears[-1],
    description='Select year:',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True
)

w

wYear

year = wYear.value

"""
Explanation: Setup widgets to select city and year
End of explanation
"""


import xarray as xr


max_day_etot = "http://dapds00.nci.org.au/thredds/dodsC/ub8/au/OzWALD/daily/OzWALD.daily.ETtot.{}.nc".format(year)
max_day_etot

dataset = xr.open_dataset(max_day_etot)
dataset

dataset.ETtot


"""
Explanation: Setup xarray
End of explanation
"""


from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="CSIRO widget")
location = geolocator.geocode("{},australia".format(w.value))
print((location.latitude, location.longitude))
print(location.raw)

"""
Explanation: Use geopy to get the lat-long coordinates for the selected city
End of explanation
"""


lon_st = location.longitude
lat_st = location.latitude

etot = dataset.ETtot.sel(longitude=lon_st, latitude=lat_st, method='nearest')
etot

"""
Explanation: Use city coordinates to find the nearest point in the grid for the data point
End of explanation
"""


p = etot.plot()


"""
Explanation: Plot using matplotlib
End of explanation
"""


import pandas as pd

startdate = year + '-01-01'
enddate = year + '-06-30'

timerange = pd.date_range(startdate, enddate, freq='D')
timerange

six_month_ds = etot.sel({'time': timerange})

six_month_ds.plot()


"""
Explanation: Use pandas to create a 6-month date range to filter the data for the dates selected
End of explanation
"""


# get Melbourne coordinates
geolocator = Nominatim(user_agent="CSIRO widget")
mel_location = geolocator.geocode("Melbourne,australia".format(w.value))
etot_mel = dataset.ETtot.sel(longitude=mel_location.longitude, latitude=mel_location.latitude, method='nearest')

# get sydney coordinates and data
syd_location = geolocator.geocode("Sydney,australia".format(w.value))
etot_syd = dataset.ETtot.sel(longitude=syd_location.longitude, latitude=syd_location.latitude, method='nearest')

etot_mel.plot()

etot_syd.plot()

"""
Explanation: Compare Melbourne and Sydney
End of explanation
"""


