The goal is to see how we can read the data contained in a netCDF file. Several possibilities will be examined.
Let's assume we have downlowded a file from CMEMS. We define the directory and the file name. datafile have to be adapted according to your case.
In [5]:
datafile = "~/CMEMS_INSTAC/INSITU_MED_NRT_OBSERVATIONS_013_035/history/mooring/IR_TS_MO_61198.nc"
In [6]:
import os
datafile = os.path.expanduser(datafile)
To read the file we need the netCDF4 interface for python.
In [7]:
import netCDF4
ds = netCDF4.Dataset(datafile, 'r')
where the first argurment of the files and 'r' indicates that it's open for reading ('w' would be used for writing).
ds contains all the information about the dataset:
In [8]:
ds
Out[8]:
We can access the global attributes individually:
In [9]:
print 'Institution: ' + ds.institution
print 'Reference: ' + ds.institution_references
Now we want to load some of the variables: we use the ds.variables
In [10]:
time = ds.variables['TIME']
temperature = ds.variables['TEMP']
Let's examine the variable temperature
In [11]:
temperature
Out[11]:
This means that the variable depends on two dimensions: time and depth. We also know the long_name, standard_name, units, and other useful pieces of information concerning the temperature.
To get the values corresponding to the variables, the synthax is:
In [12]:
temperature_values = temperature[:]
time_values = time[:]
To get the variable attributes:
In [13]:
print 'Time units: ' + time.units
print 'Temperature units: ' + temperature.units
Just a quick plot to see everything is fine. More details about the plots will be given later.
In [15]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(temperature)
plt.show()
It seems that we have not taken into accound the quality flags of the data. We can load the corresponding variable TEMP_QC.
In [17]:
temperatureQC = ds.variables['TEMP_QC']
plt.plot(temperatureQC[:])
plt.show()
The meaning of the quality flags is also stored in the file.
In [18]:
print 'Flag values: ' + str(temperatureQC.flag_values)
print 'Flag meanings: ' + temperatureQC.flag_meanings
Now we will generate a new plot of the time series using only data with QF = 1.
In [19]:
plt.plot(temperature[(temperatureQC[:, 0] == 1), 0])
plt.show()
The resulting plot now seems correct, with values ranging roughly between 10 and 28ºC.
Last thing to remember: close the netCDF file!
In [43]:
nc.close()