Takes a mesh file and produces a DFSU file filled with very simple data. INPUT start_date A date vector (e.g. [2012, 12, 01, 23, 35, 00] specifying the start date for the DFSU output ntimesteps A numeric value specifying the number of timesteps in the output. timestep_sec A numeric value specifying the timestep in seconds ... Optional input specified using the ('keyword', value) syntax. Can be any of: nitems An optional integer specifying the number of items in the output file. Defualts to 1. See NOTES. base An optional numerical value specifying the baseline value at each time point. Also specifys the value at timestep 1. Defaults to 0. See NOTES. increment An optional numerical value specifying the increment to add at each timestep. Defaults to 0. See NOTES. isarea A logical indicating if the file is to be used as a decoupled area file OUTPUT NO OUTPUT AT CONSOLE Produces a DFSU file with the layout prescribed by the mesh file. NOTES This function will create a DFSU file with the spatial dimensions specified by a .mesh file. Each element is filled with: base+increment*timestep Timestep in this instance is the zero-indexed timestep in the DFSU file. So so using, base = 10 and increment=1: The first timestep = 10 The second timestep = 11 The third timestep = 12 ... The nth timestep = 10+(n-1) Note all output items are undefined, but this can esily be changed in the data utility tool in MIKE Zero. REQUIREMENTS Requires the MIKE Matlab toolbox. Tested with v. 20110304 mrg_effing_factory from the MRG_MIKE_toolbox 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.0 2011-09-01 Initial attempt. DP v 1.1 2012-09-05 Documentation. DP v 1.2 2012-09-18 Using varargin, adding 'base', adding 'nitems'. DP Changed defualts to zero everywhere at all timesteps. DP Syntax changes. DP TODO Item types. Different data for different items
0001 function mrg_create_dfsu(start_date, ntimesteps, timestep_sec, varargin) 0002 % Takes a mesh file and produces a DFSU file filled with very simple data. 0003 % 0004 % INPUT 0005 % start_date A date vector (e.g. [2012, 12, 01, 23, 35, 00] specifying 0006 % the start date for the DFSU output 0007 % ntimesteps A numeric value specifying the number of timesteps in 0008 % the output. 0009 % timestep_sec A numeric value specifying the timestep in seconds 0010 % 0011 % ... Optional input specified using the ('keyword', value) 0012 % syntax. Can be any of: 0013 % nitems An optional integer specifying the number of items in 0014 % the output file. Defualts to 1. See NOTES. 0015 % base An optional numerical value specifying the baseline 0016 % value at each time point. Also specifys the value at 0017 % timestep 1. Defaults to 0. See NOTES. 0018 % increment An optional numerical value specifying the increment to add at 0019 % each timestep. Defaults to 0. See NOTES. 0020 % isarea A logical indicating if the file is to be used as a 0021 % decoupled area file 0022 % 0023 % OUTPUT 0024 % NO OUTPUT AT CONSOLE 0025 % Produces a DFSU file with the layout prescribed by the mesh file. 0026 % 0027 % NOTES 0028 % This function will create a DFSU file with the spatial dimensions 0029 % specified by a .mesh file. Each element is filled with: 0030 % 0031 % base+increment*timestep 0032 % 0033 % Timestep in this instance is the zero-indexed timestep in the DFSU 0034 % file. So so using, base = 10 and increment=1: 0035 % The first timestep = 10 0036 % The second timestep = 11 0037 % The third timestep = 12 0038 % ... 0039 % The nth timestep = 10+(n-1) 0040 % 0041 % Note all output items are undefined, but this can esily be changed in 0042 % the data utility tool in MIKE Zero. 0043 % 0044 % REQUIREMENTS 0045 % Requires the MIKE Matlab toolbox. Tested with v. 20110304 0046 % mrg_effing_factory from the MRG_MIKE_toolbox 0047 % 0048 % LICENCE 0049 % Created by Daniel Pritchard (www.pritchard.co) 0050 % Distributed under a creative commons CC BY-SA licence. See here: 0051 % http://creativecommons.org/licenses/by-sa/3.0/ 0052 % 0053 % DEVELOPMENT 0054 % v 1.0 2011-09-01 0055 % Initial attempt. DP 0056 % v 1.1 2012-09-05 0057 % Documentation. DP 0058 % v 1.2 2012-09-18 0059 % Using varargin, adding 'base', adding 'nitems'. DP 0060 % Changed defualts to zero everywhere at all timesteps. DP 0061 % Syntax changes. DP 0062 % 0063 % TODO 0064 % Item types. 0065 % Different data for different items 0066 0067 %% Checking input 0068 % Number of optional input arguments must be less than 2 x options... 0069 numvarargs = length(varargin); 0070 if numvarargs > 8 0071 error('mrg_create_dfsu:TooManyInputs', 'Requires at most 4 optional inputs'); 0072 end 0073 0074 % Set defaults on optional inputs 0075 nitems = 1; 0076 base = 0; 0077 increment = 0; 0078 isarea = 0; 0079 0080 while ~isempty(varargin), 0081 if ischar(varargin{1}) 0082 switch lower(varargin{1}(1:3)) 0083 case 'nit' 0084 nitems = varargin{2}; 0085 case 'bas' 0086 base = varargin{2}; 0087 case 'inc' 0088 increment = varargin{2}; 0089 case 'isa' 0090 isarea = 1; 0091 otherwise 0092 error('mrg_create_dfsu:VariableInputNotHandled','You provided an optional input not handled by the function.'); 0093 end 0094 else 0095 error('mrg_create_dfsu:VariableInputFail','Optional inputs must be specified using the (keyword, value) syntax'); 0096 end 0097 varargin([1 2])=[]; 0098 end 0099 0100 % Input checks 0101 if ~(isnumeric(start_date)&&length(start_date)==6); 0102 error('mrg:InputFail','start_date must be date vector!'); 0103 end 0104 0105 if ~isnumeric(increment) 0106 error('mrg:InputFail','increment must be a number!'); 0107 end 0108 %% 0109 NET.addAssembly('DHI.Generic.MikeZero'); 0110 NET.addAssembly('DHI.Generic.MikeZero.EUM'); 0111 import DHI.Generic.MikeZero.* 0112 import DHI.Generic.MikeZero.DFS.*; 0113 import DHI.Generic.MikeZero.DFS.dfsu.*; 0114 0115 %% Get mesh 0116 return_path = cd; 0117 [filename, path] = uigetfile('.mesh','Select a MESH file to act as DFSU template'); 0118 cd(path); 0119 0120 [Elmts,Nodes,proj] = mzReadMesh(filename); 0121 X = Nodes(:,1); 0122 Y = Nodes(:,2); 0123 Z = Nodes(:,3); 0124 code = Nodes(:,4); 0125 0126 %% If area is requested, calculate element centered waterdepths 0127 % There is some discrepenacy here between this and the values form the 0128 % model... Anyway. It'll do for now. 0129 if isarea 0130 [~,~,ze] = mzCalcElmtCenterCoords(Elmts,X,Y,Z); 0131 twd = ze*-1; 0132 twd(twd<0) = 0; 0133 end 0134 0135 %% Create a new empty dfsu file object 0136 %factory = mrg_effing_factory(); 0137 factory = DfsFactory(); 0138 builder = DfsuBuilder.Create(DfsuFileType.Dfsu2D); 0139 0140 % Create a temporal definition matching input file 0141 start = System.DateTime(start_date(1),start_date(2),start_date(3),start_date(4),start_date(5),start_date(6)); 0142 builder.SetTimeInfo(start, timestep_sec); 0143 0144 % Create a spatial defition based on mesh input file 0145 builder.SetNodes(NET.convertArray(single(X)),NET.convertArray(single(Y)),NET.convertArray(single(Z)),NET.convertArray(int32(code))); 0146 builder.SetElements(mzNetToElmtArray(Elmts)); 0147 builder.SetProjection(factory.CreateProjection(proj)) 0148 0149 % Add item(s) 0150 for a = 1:nitems 0151 builder.AddDynamicItem(['MATLAB_generated_',num2str(a)],DHI.Generic.MikeZero.eumQuantity(eumItem.eumIItemUndefined,eumUnit.eumUUnitUndefined)); 0152 end 0153 0154 % Create the file - make it ready for data 0155 new_filename = [filename(1:end-5),'_mod.dfsu']; 0156 [filename, path] = uiputfile('.dfsu', 'Select a location for the DFSU file', new_filename); 0157 cd(path) 0158 dfs = builder.CreateFile(filename); 0159 0160 %% Put some data in the file 0161 for i=0:ntimesteps-1 0162 % NB: The first timestep is just filled with base (becuase the 0163 % increment is multiplied by 'i' which is zero) 0164 data = repmat(base,dfs.NumberOfElements,1)+increment*i; 0165 for j=1:nitems 0166 % NB: All items get the same data, unless isarea is true! 0167 if j==1&&isarea==1 0168 dfs.WriteItemTimeStepNext(0, NET.convertArray(single(twd))); 0169 else 0170 dfs.WriteItemTimeStepNext(0, NET.convertArray(single(data))); 0171 end 0172 end 0173 end 0174 0175 % Close the file 0176 dfs.Close(); 0177 cd(return_path) 0178 end