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.


In [2]:
%matplotlib inline
import netCDF4
import matplotlib.pyplot as plt

We will use data from a mooring located in the Ibiza Channel.
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.


In [3]:
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"

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 ....


In [4]:
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

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:


In [5]:
with netCDF4.Dataset(dataurl) as ds:
    print ds.variables.keys()


[u'station_name', u'time', u'LAT', u'LON', u'DEPTH', u'WTR_TEM_SBE37', u'QC_WTR_TEM_SBE37', u'COND_SBE37', u'QC_COND_SBE37', u'SALT_SBE37', u'QC_SALT_SBE37']

In another example we will see how to do it in a more clever way.

A simple plot


In [6]:
plt.plot(time_values, temperature_values)
plt.xlabel(time_units, fontsize=20)
plt.ylabel(temperature_units, fontsize=20)


Out[6]:
<matplotlib.text.Text at 0x3d33690>

With the time units in seconds since 1st January 1970, it is not easy to see which period the curve represents.
The solution is to change the time format using the units attribute of the variable and the num2date function.


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


[datetime.datetime(2015, 10, 1, 0, 0) datetime.datetime(2015, 10, 1, 1, 0)
 datetime.datetime(2015, 10, 1, 2, 0) datetime.datetime(2015, 10, 1, 3, 0)
 datetime.datetime(2015, 10, 1, 4, 0)]

In [8]:
plt.plot(time2, temperature_values)
plt.ylabel(temperature_units, fontsize=20)
plt.show()


Now that's clearer. In another chapter we will see how to further improve the figure.


In [ ]: