%matplotlib inline
import netCDF4
import matplotlib.pyplot as plt

"""
Explanation: Reading a remote file using OPeNDAP
Now we will read a remote file using the OPeNDAP protocol. The advantage is that the file has not to be downloaded on your computer, while you can access the variables you want using it as it were on your computer. Furthermore, if the file is automatically updated on the remote server, you can run the same script without having to bother downloading it everytime it is updaded.
We first import the modules needed to load and plot the data.
End of explanation
"""


dataurl = "http://thredds.socib.es/thredds/dodsC/mooring/conductivity_and_temperature_recorder/buoy_canaldeibiza-scb_sbe37006/L1/dep0003_buoy-canaldeibiza_scb-sbe37006_L1_latest.nc"

"""
Explanation: We will use data from a mooring located in the Ibiza Channel.<br/>
To get the file name, browse the directories in the thredds daa server until you find the file you want (http://thredds.socib.es/thredds/dodsC/mooring/conductivity_and_temperature_recorder/buoy_canaldeibiza-scb_sbe37006/L1/dep0003_buoy-canaldeibiza_scb-sbe37006_L1_latest.nc.html). Then copy the field called Data URL.
End of explanation
"""


with netCDF4.Dataset(dataurl) as ds:
    temperature_values = ds.variables['WTR_TEM_SBE37'][:]
    time_values = ds.variables['time'][:]
    temperature_units = ds.variables['WTR_TEM_SBE37'].units
    time_units = ds.variables['time'].units

"""
Explanation: Read the file
The method is exactly the same as the previous example, except the file has not been downloaded on your machine. In the present case, the file is updated several times a day, meaning that running the same code at different time of the day could produce different results, as more data will be included.
To be sure the file is closed, we will use the context manager with ... as ....
End of explanation
"""


with netCDF4.Dataset(dataurl) as ds:
    print ds.variables.keys()

"""
Explanation: The variable storing the temperature is not called temp or TEMP, but WTR_TEM_SBE37. The variable names can be obtained by checking the link on the thredds server or by running:
End of explanation
"""


plt.plot(time_values, temperature_values)
plt.xlabel(time_units, fontsize=20)
plt.ylabel(temperature_units, fontsize=20)

"""
Explanation: In another example we will see how to do it in a more clever way.
A simple plot
End of explanation
"""


time2 = netCDF4.num2date(time_values, time_units)
print time2[0:5]

plt.plot(time2, temperature_values)
plt.ylabel(temperature_units, fontsize=20)
plt.show()

"""
Explanation: With the time units in seconds since 1st January 1970, it is not easy to see which period the curve represents.<br/> 
The solution is to change the time format using the units attribute of the variable and the num2date function.
End of explanation
"""


