Home > mrg > MRG_MIKE > mrg_mat_to_DFS0.m

mrg_mat_to_DFS0

PURPOSE ^

Process MATLAB data to a DFS0 file.

SYNOPSIS ^

function mrg_mat_to_DFS0(padded_struct, datetime_name, variables)

DESCRIPTION ^

 Process MATLAB data to a DFS0 file.
 
 INPUT
   padded_struct   A MATLAB structure with an equidistant timestep
   datetime        A string which identified the MATLAB datenum object in the
                   structure
   variables       A cell array containing strings which identify the
                   objects in the structure to write to the DFS0 file

 OUTPUT
   NO OUTPUT AT CONSOLE
   Outputs a DFS0 file

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

 LICENCE
   Created by Daniel Pritchard (www.pritchard.co)
   Distributed under a creative commons CC BY-SA licence. See here:
   http://creativecommons.org/licenses/by-sa/3.0/

 DEVELOPMENT
   v 1.1   Unknown
           DP. Inital attempt.  
   v 1.2   04/11/11 
           DP. Modifications to make it more generic.
   v 1.3   14/02/2013
           DP. Documentation!
   v 1.4   Getting timesteps right!
   v 1.5   No longer round timesteps: Problematic for sub-second
           timesteps

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function mrg_mat_to_DFS0(padded_struct, datetime_name, variables)
0002 % Process MATLAB data to a DFS0 file.
0003 %
0004 % INPUT
0005 %   padded_struct   A MATLAB structure with an equidistant timestep
0006 %   datetime        A string which identified the MATLAB datenum object in the
0007 %                   structure
0008 %   variables       A cell array containing strings which identify the
0009 %                   objects in the structure to write to the DFS0 file
0010 %
0011 % OUTPUT
0012 %   NO OUTPUT AT CONSOLE
0013 %   Outputs a DFS0 file
0014 %
0015 % REQUIREMENTS
0016 %   Requires the MIKE Matlab toolbox.  Tested with v. 20110304.
0017 %
0018 % LICENCE
0019 %   Created by Daniel Pritchard (www.pritchard.co)
0020 %   Distributed under a creative commons CC BY-SA licence. See here:
0021 %   http://creativecommons.org/licenses/by-sa/3.0/
0022 %
0023 % DEVELOPMENT
0024 %   v 1.1   Unknown
0025 %           DP. Inital attempt.
0026 %   v 1.2   04/11/11
0027 %           DP. Modifications to make it more generic.
0028 %   v 1.3   14/02/2013
0029 %           DP. Documentation!
0030 %   v 1.4   Getting timesteps right!
0031 %   v 1.5   No longer round timesteps: Problematic for sub-second
0032 %           timesteps
0033 
0034 %% Is the input a structured array
0035 if ~isstruct(padded_struct)
0036     error('You must provide a MATLAB structured array')
0037 end
0038 
0039 %% Is a datetime name supplied?  If not, get one.
0040 if ~exist('datetime_name', 'var')
0041     datetime_name = get_dt_name(padded_struct);
0042 elseif ~ischar(datetime_name)
0043     error('The datetime_name must be a string');
0044 end
0045 
0046 %% Is variables supplied?  If not error
0047 if ~exist('variables', 'var') 
0048     error('You did not supply any variables.');
0049 end
0050 
0051 %% Is it a cell array
0052 if ~iscell(variables)
0053     error('The variables must be supplied as a MATLAB cell array.');
0054 end
0055 
0056 %% Are all the variables there?
0057 if any(~isfield(padded_struct, variables))
0058     error('Some of the expected variables are missing from the supplied structured array');
0059 end
0060 
0061 %% Check all variables the same length?
0062 
0063 %% Checks on the datetime variable
0064 datetime = padded_struct.(datetime_name);
0065 % Are there 2 dimentions?
0066 dt_dims = ndims(datetime);
0067 if dt_dims ~= 2
0068     error('The datetime object does not have 2 dimensions.');
0069 end
0070 
0071 % Put the longest dimension first
0072 if size(datetime,1) < size(datetime,2)
0073     datetime = datetime.';
0074 end
0075 
0076 %% Are the time steps equidistant?  If so then setup the MIKE timestep
0077 % Check time step for equidistant spacing
0078 %timestep = round((datetime(2) - datetime(1))*24*60*60);
0079 timestep = (datetime(2) - datetime(1))*24*60*60;
0080 t_steps = diff(datetime)*24*60*60;
0081 
0082 if any(abs(t_steps - timestep)>=1/60)
0083     error('Timestep is not equidistant in file!');
0084 end
0085 
0086 %% Setup variables to describe the DFS0 file
0087 % Temporal dimension information
0088 start_date = datevec(min(datetime));
0089 % NB: 'timestep' assigned above
0090 
0091 %% OK - Where do we save the DFS0 file?
0092 [dfs_name,dfs_path] = uiputfile('*.dfs0','Choose a name for the .dfs0 file');
0093 cd(dfs_path)
0094 
0095 %% Here after this is just copied from DHI example...
0096 % Load libraries
0097 assemb = NET.addAssembly('DHI.Generic.MikeZero');
0098 import DHI.Generic.MikeZero.*
0099 import DHI.Generic.MikeZero.DFS.*;
0100 import DHI.Generic.MikeZero.DFS.dfs123.*;
0101 
0102 % Create an empty dfs0 file object
0103 dfs0 = dfsTSO(dfs_name,1);
0104 
0105 set(dfs0,'filetitle','Data from MATLAB');
0106 set(dfs0,'startdate',double([start_date(1), start_date(2), start_date(3), start_date(4), start_date(5), start_date(6)]));
0107 set(dfs0,'timestep',[0 0 0 0 0 timestep]);
0108 addTimesteps(dfs0,length(datetime));
0109 
0110 % 1 dbar = 0.1 bar = 10 kPa
0111 % 1 mbar = 0.001 bar = 0.1 kPa = 1 hPa (hectopascal)
0112 % 1 dbar = 100 hPa
0113 
0114 for n = 1:length(variables)
0115     addItem(dfs0,variables{n});
0116 end
0117 
0118 for j=1:length(variables)
0119     var = variables{j};
0120     dfs0(j) = single(padded_struct.(var));
0121 end
0122 
0123 save(dfs0);
0124 close(dfs0);
0125 % DFS0 writing complete!
0126 
0127 %% Begin nested functions for mat2DFS0 function...
0128     function datetime_name = get_dt_name(padded_struct)
0129         gdn_names = fieldnames(padded_struct);
0130         gdn_options = gdn_names;
0131         gdn_options{end+1} = 'None of the above!';
0132         gdn_choice = menu(sprintf('Which object contains the MATLAB datetime information?'),gdn_options);
0133         if strcmp(gdn_options(gdn_choice), 'None of the above!')
0134             error('No, that is not an option');
0135         else
0136             datetime_name = char(gdn_names(gdn_choice));
0137         end
0138     end
0139 end

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