To follow this tutorial you need MATLAB® version 2011a or higher.

If you do not have MATLAB, take a look at this tutorial instead.

 

SOBEK outputs NetCDF files (*.nc). This tutorial shows you how to access these files in Matlab. We will investigate the contents of the netCDF file and plot a timeseries. 

 

Step-by-step guide

  1. You can find SOBEK output in the <projectname>.dsproj_data folder. Here you will find several files with the *.nc extension. These are NetCDF files. They correspond to the output selected in DeltaShell. Note: if this folder contains files with the extension *.nc.changes you have not saved your project after running the model. Save the project in DeltaShell before continuing. 

    NetCDF (Network Common Data Form) is an open standard for storing scientific data. Delft3D Flexible Mesh (and SOBEK) use the CF-1.0 convention.

  2. For this tutorial we will use the observation point output for water level. If you do not have a SOBEK model readily available, download the following output file: Water level (op).nc
  3. Open MATLAB and define a variable pointing to the *.nc file: 

    ncfile = './Water level (op).nc'
  4. To display the contents of the file, use the following build in function. This will print a list of all variables and sizes to the command window. 

    ncdisp(ncfile)
  5. In the next steps, we will extract the timesvalues and observation point names from the NetCDF file

    1. The variable for the timestamps is called 'time', but note that it is in 'milliseconds since 1970-01-01'! Check this using ncdisp. To change this to MATLAB compatible time, use the following code

      % Read data from nc file
      time = ncread(ncfile, 'time')
       
      % convert to MATLAB dates
      time = time./(1000*3600*24) + datenum('01-01-1970', 'dd-mm-yyyy')
    2. The values (which are water levels, in this case) are easily extracted:

      % Read data from nc file
      waterlevels = ncread(ncfile, 'value')
    3. Finally, the names of the observation stations are stored under the attribute 'feature_name'. Reading this data will return a transposed character array. It is convenient to transform this to a cell array immediately: 

      % Read data from nc file
      names = cellstr(ncread(ncfile, 'feature_name')')
  6. With the data read from file, we are ready to plot the result:

    % Change the index to cycle through the stations
    station_index = 1
    
    
    % Plot the data
    figure(1), clf(1)
        	plot(time, waterlevels(station_index, :), 'color', [.3 .3 .9], 'linewidth', 2)
        title(sprintf('Station: %s', names{station_index}))
        datetick('x',  'dd mmm yyyy' ,'keepticks', 'keeplimits')
        ylabel('Water level [m]')
     
    % Some extra code to make the figure less default-looking
    set(gca, ...
      'Box'         , 'on'     , ...
      'TickDir'     , 'out'     , ...
      'TickLength'  , [.02 .02] , ...
      'XMinorTick'  , 'on'      , ...
      'YMinorTick'  , 'on'      , ...
      'YGrid'       , 'on'      , ...
      'XColor'      , [.3 .3 .3], ...
      'YColor'      , [.3 .3 .3], ...
      'LineWidth'   , 1         );
    
    


 

That's it! In the same vein, you can make plots of other netCDF output files. However, the attribute names might differ between the different outputs. Take a look at the data structure first using ncdisp to avoid surprises!