Reading a file using CF module

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"

Read the file

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


long_name:station name field summary
------------------------------------
Data           : long_name:station name(ncdim%name_strlen(33)) 
Axes           : ncdim%name_strlen(33)

sea_water_electrical_conductivity field summary
-----------------------------------------------
Data           : sea_water_electrical_conductivity(time(1451)) S m-1
Axes           : longitude(1) = [0.783667] degree_east
               : depth(1) = [1.0] m
               : latitude(1) = [38.82445] degree_north
               : time(1451) = [2015-10-01 00:00:00, ..., 2015-11-30 10:00:00] gregorian
Ancillary vars : <CF Field: sea_water_electrical_conductivity status_flag(time(1451)) >

sea_water_salinity field summary
--------------------------------
Data           : sea_water_salinity(time(1451)) psu
Axes           : longitude(1) = [0.783667] degree_east
               : depth(1) = [1.0] m
               : latitude(1) = [38.82445] degree_north
               : time(1451) = [2015-10-01 00:00:00, ..., 2015-11-30 10:00:00] gregorian
Ancillary vars : <CF Field: sea_water_salinity status_flag(time(1451)) >

sea_water_temperature field summary
-----------------------------------
Data           : sea_water_temperature(time(1451)) C
Axes           : longitude(1) = [0.783667] degree_east
               : depth(1) = [1.0] m
               : latitude(1) = [38.82445] degree_north
               : time(1451) = [2015-10-01 00:00:00, ..., 2015-11-30 10:00:00] gregorian
Ancillary vars : <CF Field: sea_water_temperature status_flag(time(1451)) >

We see that the file contains 4 variables:

  1. temperature
  2. salinity
  3. conductivity. Each of them has 4 dimensions: longitude, latitude, time and depth.

Read variable, coordinates and units

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]:
<CF Field: sea_water_temperature(time(1451)) C>

The number of variables which have a standard name corresponding to sea_water_temperature is:


In [5]:
print len(temperature)


1

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


[23.67 23.67 23.65 23.6 23.56 23.53 23.52 23.54 23.56 23.58 23.63 23.72
 23.92 24.13 24.12 24.01 23.83 23.74 23.66 23.65]
Temperature units: C

We inspect the corresponding coordinates:


In [7]:
temperature[0].coords()


Out[7]:
{'dim0': <CF DimensionCoordinate: time(1451) gregorian>,
 'dim1': <CF DimensionCoordinate: latitude(1) degree_north>,
 'dim2': <CF DimensionCoordinate: longitude(1) degree_east>,
 'dim3': <CF DimensionCoordinate: depth(1) m>}

To extract the time variable:


In [8]:
time = temperature[0].coord('time')
time


Out[8]:
<CF DimensionCoordinate: time(1451) gregorian>

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


[  1.44365760e+09   1.44366120e+09   1.44366480e+09   1.44366840e+09
   1.44367200e+09   1.44367560e+09   1.44367920e+09   1.44368280e+09
   1.44368640e+09   1.44369000e+09   1.44369360e+09   1.44369720e+09
   1.44370080e+09   1.44370440e+09   1.44370800e+09   1.44371160e+09
   1.44371520e+09   1.44371880e+09   1.44372240e+09   1.44372600e+09]
 
Time units: seconds since 1970-01-01 00:00:00

A simple plot


In [11]:
time2 = netCDF4.num2date(time_values, time_units)
plt.plot(time2, temperature_values)
plt.ylabel(temperature_units, fontsize=20)
plt.show()



In [ ]: