The main difference with the previous example is the way we will read the data from the file.
Instead of the netCDF4 module, we will use the cf-python package, which implements the CF data model for the reading, writing and processing of data and metadata.
In [1]:
%matplotlib inline
import cf
import netCDF4
import matplotlib.pyplot as plt
The data file is the same.
In [2]:
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"
We use the function read. Doing so, we easily obtain a nice summary of the file content.
In [3]:
f = cf.read(dataurl)
print f
We see that the file contains 4 variables:
From the previous commands we cannot know the name of the variables within the file. But that's not necessary. Temperature can be retrived using its standard name:
In [4]:
temperature = f.select('sea_water_temperature')
temperature
Out[4]:
The number of variables which have a standard name corresponding to sea_water_temperature is:
In [5]:
print len(temperature)
but in other cases (ex: different sensors measuring temperature with data in a common file), one can obtain more than one variable.
To get the temperature values, we select the first element (index = 0 in python, not 1) and convert it into an array.
In [6]:
temperature_values = temperature[0].array
temperature_units = temperature[0].units
print temperature_values[0:20]
print 'Temperature units: ' + temperature_units
We inspect the corresponding coordinates:
In [7]:
temperature[0].coords()
Out[7]:
To extract the time variable:
In [8]:
time = temperature[0].coord('time')
time
Out[8]:
and to get the values:
In [9]:
time_values = temperature[0].coord('time').array
time_units = temperature[0].coord('time').units
print time_values[0:20]
print ' '
print 'Time units: ' + time_units
In [11]:
time2 = netCDF4.num2date(time_values, time_units)
plt.plot(time2, temperature_values)
plt.ylabel(temperature_units, fontsize=20)
plt.show()
In [ ]: