Skip to content

Instantly share code, notes, and snippets.

@swaldman3
Created September 14, 2016 09:06
Show Gist options
  • Save swaldman3/235741f739c13f5168ffb3c4d2c94a8f to your computer and use it in GitHub Desktop.
Save swaldman3/235741f739c13f5168ffb3c4d2c94a8f to your computer and use it in GitHub Desktop.
Matlab function to extract timestep times from a Delft3D map file
function [ sdTimesteps ] = fnCalcD3DTimesteps( nfs )
%FNCALCD3DTIMESTEPS Work out dates of D3D timesteps
% Function to take an "nfs" object (a Delft3D data file opened with
% vs_use) and output an array of MATLAB serial dates corresponding to the
% output timesteps in the file.
% Requires Delft3D MATLAB toolbox.
% Simon Waldman / Heriot-Watt University 2016
%check input
assert(exist('nfs', 'var') && isstruct(nfs), 'Input is not a struct.');
%Not checking whether it's the corrent type of Delft3D file because it'd be
%a major hassle. If you get errors, make sure you're feeling it the right
%file!
ITDATE = vs_let(nfs, 'map-const', {0}, 'ITDATE', {0}); %start date & time as two numbers in an array
TUNIT = vs_let(nfs, 'map-const', {0}, 'TUNIT', {0}); %units of time in seconds (e.g. 60 if it's minutes)
DT = vs_let(nfs, 'map-const', {0}, 'DT', {0}); % number of TUNITs between timesteps
ITMAPC = vs_let(nfs, 'map-info-series', {0}, 'ITMAPC', {0}); % array of timesteps
% Time steps should be TUNIT*DT*ITMAPC seconds after the time given in
% ITDATE.
% ITDATE is an array with two values. The first is yyyymmdd, and the second
% appears to always be zero - but that may just mean that for the examples I've tried, the time is at midnight.
sdStartDate = datenum(num2str(ITDATE(1)), 'yyyymmdd');
%NB THIS WILL BE WRONG IF ITDATE(2)~!0.
secondoffsets = ITMAPC .* DT .* TUNIT;
a = zeros(length(secondoffsets),1);
sdOffsets = datenum(a,a,a,a,a,secondoffsets);
sdTimesteps = sdStartDate + sdOffsets;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment