0001 function mrg_mat_to_DFS0(padded_struct, datetime_name, variables)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 if ~isstruct(padded_struct)
0036 error('You must provide a MATLAB structured array')
0037 end
0038
0039
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
0047 if ~exist('variables', 'var')
0048 error('You did not supply any variables.');
0049 end
0050
0051
0052 if ~iscell(variables)
0053 error('The variables must be supplied as a MATLAB cell array.');
0054 end
0055
0056
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
0062
0063
0064 datetime = padded_struct.(datetime_name);
0065
0066 dt_dims = ndims(datetime);
0067 if dt_dims ~= 2
0068 error('The datetime object does not have 2 dimensions.');
0069 end
0070
0071
0072 if size(datetime,1) < size(datetime,2)
0073 datetime = datetime.';
0074 end
0075
0076
0077
0078
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
0087
0088 start_date = datevec(min(datetime));
0089
0090
0091
0092 [dfs_name,dfs_path] = uiputfile('*.dfs0','Choose a name for the .dfs0 file');
0093 cd(dfs_path)
0094
0095
0096
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
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
0111
0112
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
0126
0127
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