Home > mrg > MRG_MIKE > mrg_dfsu_standardise.m

mrg_dfsu_standardise

PURPOSE ^

Standardises values in a DFSU file.

SYNOPSIS ^

function mrg_dfsu_standardise(increment)

DESCRIPTION ^

 Standardises values in a DFSU file.  

 INPUT
   increment       An integer specifying the increment at each timestep to
                   standardise the DFSU file with.  See NOTES.

 OUTPUT
   NO OUTPUT TO CONSOLE
   Produces a DFSU file with a '_mod' suffix 

 REQUIREMENTS
   The DHI/MIKE Matlab toolbox 2011 (developed with v. 20110304)
   mrg_struct_to_csv.m function (assuming you want csv output, else it will be skipped)

 NOTES
   The input file is copied to 'filename_mod.dfsu' and values at each
   timestep are are standardised using the following formula:
       increment*(timestep-1)
   i.e. 
       data/increment*0 at timestep 1
       data/increment*1 at timestep 2 
       data/increment*n at timestep n+1
   This function is slow becuase
       A) The whole DFSU file is copied and then 
       B) There are two nested for loops.
   A faster alternative may be to create a DFSU 'template' file (from a
   mesh file, perhaps) and then do the math in the MIKE calculator.
   Unfortunatly using two DFSU files in this way is not as easy as you
   might hope in the 2011 verison of MIKE.

 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   2012
           DP. Initial attempt and distribution. 
   v 1.1   14/02/2013
           Proper documentation

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mrg_dfsu_standardise(increment)
0002 % Standardises values in a DFSU file.
0003 %
0004 % INPUT
0005 %   increment       An integer specifying the increment at each timestep to
0006 %                   standardise the DFSU file with.  See NOTES.
0007 %
0008 % OUTPUT
0009 %   NO OUTPUT TO CONSOLE
0010 %   Produces a DFSU file with a '_mod' suffix
0011 %
0012 % REQUIREMENTS
0013 %   The DHI/MIKE Matlab toolbox 2011 (developed with v. 20110304)
0014 %   mrg_struct_to_csv.m function (assuming you want csv output, else it will be skipped)
0015 %
0016 % NOTES
0017 %   The input file is copied to 'filename_mod.dfsu' and values at each
0018 %   timestep are are standardised using the following formula:
0019 %       increment*(timestep-1)
0020 %   i.e.
0021 %       data/increment*0 at timestep 1
0022 %       data/increment*1 at timestep 2
0023 %       data/increment*n at timestep n+1
0024 %   This function is slow becuase
0025 %       A) The whole DFSU file is copied and then
0026 %       B) There are two nested for loops.
0027 %   A faster alternative may be to create a DFSU 'template' file (from a
0028 %   mesh file, perhaps) and then do the math in the MIKE calculator.
0029 %   Unfortunatly using two DFSU files in this way is not as easy as you
0030 %   might hope in the 2011 verison of MIKE.
0031 %
0032 % LICENCE
0033 %   Created by Daniel Pritchard (www.pritchard.co)
0034 %   Distributed under a creative commons CC BY-SA licence. See here:
0035 %   http://creativecommons.org/licenses/by-sa/3.0/
0036 %
0037 % DEVELOPMENT
0038 %   v 1.0   2012
0039 %           DP. Initial attempt and distribution.
0040 %   v 1.1   14/02/2013
0041 %           Proper documentation
0042 
0043 %% Start!
0044 if ~isnumeric(increment)
0045     error('Increment must be a number!');
0046 end
0047 
0048 [filename, path] = uigetfile('.dfsu','Select a DFSU file to standardise');
0049 cd(path);
0050 fprintf('\nThis function can be slow.  Please be patient.\n');
0051 new_filename = [filename(1:end-5),'_mod.dfsu'];
0052 
0053 %% Gah!
0054 % I would prefer to add a new item to the existing DFSU and do the math in
0055 % MIKE, but that doesn't seem to be easy.
0056 % Strangely this is the method the toolbox examples use!
0057 copyfile(filename, new_filename, 'f');
0058 fprintf('\nFile copied to: ''%s''\n',new_filename);
0059 
0060 %%
0061 NET.addAssembly('DHI.Generic.MikeZero.DFS');
0062 import DHI.Generic.MikeZero.DFS.*;
0063 
0064 dfsu2 = DfsFileFactory.DfsuFileOpenEdit(new_filename);
0065 
0066 no_items = dfsu2.ItemInfo.Count;
0067 no_timesteps = dfsu2.NumberOfTimeSteps;
0068 
0069 for j=1:no_items
0070     for i=0:no_timesteps-1
0071         % Read first time step from file
0072         itemData = dfsu2.ReadItemTimeStep(1,i);
0073         data     = double(itemData.Data)';
0074         % Calculate new values
0075         data  = data/(increment*double(i));
0076         % Write to memory
0077         dfsu2.WriteItemTimeStep(j,i,itemData.Time,NET.convertArray(single(data(:))));
0078     end
0079 end
0080 
0081 dfsu2.Close();
0082 
0083 fprintf('\nFile modified!\n\n');
0084 
0085 
0086 end

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