Home > mrg > MRG_MIKE > mrg_read_dfs0.m

mrg_read_dfs0

PURPOSE ^

Read all entries from a DFS0 file.

SYNOPSIS ^

function RecData = mrg_read_dfs0(varargin)

DESCRIPTION ^

 Read all entries from a DFS0 file.

 INPUT
   ...         An optional charater string specifying the file name (or
               full file path) to read.

 OUTPUT
   RecData     A structure with all data and useful information.

 REQUIREMENTS
   Requires the MIKE Matlab toolbox.  Tested with v. 20110304

 LICENCE
   Created B. Elsaesser @ RPS Consulting Engineers
   Updated by Daniel Pritchard (www.pritchard.co)
   Original copyright B. Elsaesser.  Rewritten code distributed under a
   creative commons CC BY-SA licence. See here:
   http://creativecommons.org/licenses/by-sa/3.0/

 DEVELOPMENT
   v 1.0   2004-11
           First version. BE
   v 1.1   2005-09
           Updated. BE
   v 1.2   2007-01
           Time & date of data added to file structure. BE
           Converted to proper function. BE
   v 1.3   2009-09
           Revised to work with Mike 2009 using latest Matlab toolbox. BE
   v 1.4   2012-09-05
           Full code re-write with backwards compatability. DP
           Compatible with MIKE 2011 and 20110304 toolbox. DP
           Uses Dfs0Util (from the MIKE toolbox). DP
           Standard info returned with self-expanatory fieldnames. DP
           The 'items' field is not 100% backwards compatible (yet). DP
   v 1.5   Added ability to return useful time info from NonEqudistant
           files.  DP

 TODO
   Although the actual data reading (via Dfs0Util) is faster, this code
   is slower overall than the orignal Read_dfs0.  This might be
   unavoidable due to extra data extracted, but maybe it can be optimised
   a bit...
   Full backwards compatability on items{}

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function RecData = mrg_read_dfs0(varargin)
0002 % Read all entries from a DFS0 file.
0003 %
0004 % INPUT
0005 %   ...         An optional charater string specifying the file name (or
0006 %               full file path) to read.
0007 %
0008 % OUTPUT
0009 %   RecData     A structure with all data and useful information.
0010 %
0011 % REQUIREMENTS
0012 %   Requires the MIKE Matlab toolbox.  Tested with v. 20110304
0013 %
0014 % LICENCE
0015 %   Created B. Elsaesser @ RPS Consulting Engineers
0016 %   Updated by Daniel Pritchard (www.pritchard.co)
0017 %   Original copyright B. Elsaesser.  Rewritten code distributed under a
0018 %   creative commons CC BY-SA licence. See here:
0019 %   http://creativecommons.org/licenses/by-sa/3.0/
0020 %
0021 % DEVELOPMENT
0022 %   v 1.0   2004-11
0023 %           First version. BE
0024 %   v 1.1   2005-09
0025 %           Updated. BE
0026 %   v 1.2   2007-01
0027 %           Time & date of data added to file structure. BE
0028 %           Converted to proper function. BE
0029 %   v 1.3   2009-09
0030 %           Revised to work with Mike 2009 using latest Matlab toolbox. BE
0031 %   v 1.4   2012-09-05
0032 %           Full code re-write with backwards compatability. DP
0033 %           Compatible with MIKE 2011 and 20110304 toolbox. DP
0034 %           Uses Dfs0Util (from the MIKE toolbox). DP
0035 %           Standard info returned with self-expanatory fieldnames. DP
0036 %           The 'items' field is not 100% backwards compatible (yet). DP
0037 %   v 1.5   Added ability to return useful time info from NonEqudistant
0038 %           files.  DP
0039 %
0040 % TODO
0041 %   Although the actual data reading (via Dfs0Util) is faster, this code
0042 %   is slower overall than the orignal Read_dfs0.  This might be
0043 %   unavoidable due to extra data extracted, but maybe it can be optimised
0044 %   a bit...
0045 %   Full backwards compatability on items{}
0046 
0047 %%
0048 NET.addAssembly('DHI.Generic.MikeZero.DFS');
0049 import DHI.Generic.MikeZero.DFS.*;
0050 import DHI.Generic.MikeZero.DFS.dfs0.*;
0051 
0052 %% Open File
0053 if isempty(varargin)
0054     [fname,path] = uigetfile('*.dfs0','Select the .dfs0');
0055     name = [path,fname];
0056 elseif (~exist(char(varargin),'file'))
0057     warning([mfilename, ':filenotfound'], 'The file you supplied was not found.')
0058     [fname,path] = uigetfile('*.dfs0','Select the .dfs0');
0059     name = [path,fname];
0060 else
0061     name = char(varargin);
0062 end
0063 
0064 dfs0File  = DfsFileFactory.DfsGenericOpen(name);
0065 
0066 %% Read times and data for all items
0067 % Use the Dfs0Util for bulk-reading all data and timesteps
0068 dd = single(Dfs0Util.ReadDfs0DataDouble(dfs0File));
0069 t = dd(:,1);
0070 data = dd(:,2:end);
0071 
0072 %% Sort out time data
0073 try
0074     start_date_vec = [dfs0File.FileInfo.TimeAxis.StartDateTime.Year, ...
0075         dfs0File.FileInfo.TimeAxis.StartDateTime.Month, ...
0076         dfs0File.FileInfo.TimeAxis.StartDateTime.Day, ...
0077         dfs0File.FileInfo.TimeAxis.StartDateTime.Hour, ...
0078         dfs0File.FileInfo.TimeAxis.StartDateTime.Minute, ...
0079         dfs0File.FileInfo.TimeAxis.StartDateTime.Second];
0080 catch err
0081     disp(err.message);
0082     warning([mfilename, ':startdatefail'], 'Start date could not be determined from the DFS0 file.  Using 0000-00-00 00:00:00.')
0083     start_date_vec = [0,0,0,0,0,0];
0084 end
0085 
0086 start_date_num = datenum(double(start_date_vec));
0087 
0088 %% Read item information
0089 items = cell(dfs0File.ItemInfo.Count, 3);
0090 for i = 0:dfs0File.ItemInfo.Count-1
0091    item = dfs0File.ItemInfo.Item(i);
0092    items{i+1,1} = char(item.Name);
0093    items{i+1,2} = char(item.Quantity.Unit);
0094    items{i+1,3} = char(item.Quantity.UnitAbbreviation); 
0095 end
0096 
0097 %% Construct the output
0098 RecData = struct();
0099 
0100 RecData.dData = data;
0101 
0102 % For compatability with old code:
0103 RecData.dTime = NaN(1,4);
0104 RecData.dTime(1) = dfs0File.FileInfo.TimeAxis.NumberOfTimeSteps;
0105 try
0106     RecData.dTime(2) = dfs0File.FileInfo.TimeAxis.TimeStep;
0107 catch err
0108     disp(err.message);
0109     warning([mfilename, ':timestepfail'], 'Timestep length could not be determined from the DFS0 file.  Using NaN.')
0110     RecData.dTime(2) = NaN;
0111 end
0112 RecData.dTime(3) = RecData.dTime(2)/(60*60*24);
0113 RecData.dTime(4) = start_date_num;
0114 nameinfo = regexp(char(dfs0File.FileInfo.FileName),'\\','split');
0115 RecData.name = char(nameinfo(end));
0116 RecData.title = char(dfs0File.FileInfo.FileTitle);
0117 RecData.DeleteFloat = single(dfs0File.FileInfo.DeleteValueFloat);
0118 
0119 % Future proof returning of data
0120 % Removes abiguity on the meaning of 'dTime'
0121 % Adds additional info
0122 RecData.NumberOfTimeSteps = dfs0File.FileInfo.TimeAxis.NumberOfTimeSteps;
0123 RecData.TimeStepSec = RecData.dTime(2);
0124 RecData.TimeStepHour = RecData.dTime(2)/(60*60);
0125 RecData.TimeStepMat = RecData.dTime(2)/(60*60*24);
0126 RecData.StartDateNum = start_date_num;
0127 RecData.StartDateVec = start_date_vec;
0128 RecData.Fullname = char(dfs0File.FileInfo.FileName);
0129 RecData.TimeAxisType = char(dfs0File.FileInfo.TimeAxis.TimeAxisType);
0130 
0131 % If the file is a non-equidistant time axis, then build a timeaxis from
0132 % the data
0133 if ~isempty(strfind(RecData.TimeAxisType, 'NonEquidistant'))
0134     if strcmp(char(dfs0File.FileInfo.TimeAxis.TimeUnit), 'eumUsec')
0135         tmat = double(t)/60/60/24;
0136         RecData.TimeAxis = RecData.StartDateNum+tmat;
0137     else 
0138         warning([mfilename, ':timeaxisbuildfail'], 'Tried to build a time axis for NonEquidistant, but failed becuase the timestep is not "eumUsec".')
0139     end
0140 end
0141 
0142 % 'items' is not (yet) strictly backwards compatible, as we are missing
0143 % columns 4, 5 and 6 from Read_dfs0.m (Not sure what these columns are?!)
0144 RecData.items = items;
0145 
0146 dfs0File.Close();
0147 
0148

Generated on Thu 29-May-2014 21:29:53 by m2html © 2005