Home > mrg > MRG_MIKE > mrg_dfsu_apply.m

mrg_dfsu_apply

PURPOSE ^

Applys an arbitary function (specified by fhandle) to a selected item in a DFSU file.

SYNOPSIS ^

function mrg_dfsu_apply(fhandle, varargin)

DESCRIPTION ^

 Applys an arbitary function (specified by fhandle) to a selected item in a DFSU file.  

 INPUT
   fhandle       A function handle, constucted using the '@' syntax. See
                 'doc function_handle' for more information 
   ...           Optional items passed verbatium to the function specified
                 by fhandle

 OUTPUT
   NO OUTPUT TO CONSOLE
   Modifies a DFSU file directly.

 NOTES
   This function takes a DFSU file and loops through it
   timestep-by-timestep applying the function specified by fhandle. At
   each timestep, fhandle is supplied a vector with N values (where N is
   the number of elements in the domain).  The result, which must also be
   of length N, is written back to the DFSU file. Everything in varargin
   gets passed to fhandel, allowing the use of moderatly complex
   functions.

 USAGE
   func = @mrg_assign;
   % Every cell greater than -12.5 gets a value of 1
   mrg_dfsu_apply(func, '>', -12.5, 1);
   % Every cell less than -12.5 gets a value of 0 
   mrg_dfsu_apply(func, '<', -12.5, 0);
   % Every euqal to -12.5 gets a value of 1
   mrg_dfsu_apply(func, '=', -12.5, 1);

 REQUIREMENTS
   The DHI/MIKE Matlab toolbox 2011/13 (developed with v. 20130222)

 WARNING
  This modifies the DFSU file directly, ensure you copy the file first,
  before attempting to apply this function.

 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   July 2013
           DP. Initial attempt.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mrg_dfsu_apply(fhandle, varargin)
0002 % Applys an arbitary function (specified by fhandle) to a selected item in a DFSU file.
0003 %
0004 % INPUT
0005 %   fhandle       A function handle, constucted using the '@' syntax. See
0006 %                 'doc function_handle' for more information
0007 %   ...           Optional items passed verbatium to the function specified
0008 %                 by fhandle
0009 %
0010 % OUTPUT
0011 %   NO OUTPUT TO CONSOLE
0012 %   Modifies a DFSU file directly.
0013 %
0014 % NOTES
0015 %   This function takes a DFSU file and loops through it
0016 %   timestep-by-timestep applying the function specified by fhandle. At
0017 %   each timestep, fhandle is supplied a vector with N values (where N is
0018 %   the number of elements in the domain).  The result, which must also be
0019 %   of length N, is written back to the DFSU file. Everything in varargin
0020 %   gets passed to fhandel, allowing the use of moderatly complex
0021 %   functions.
0022 %
0023 % USAGE
0024 %   func = @mrg_assign;
0025 %   % Every cell greater than -12.5 gets a value of 1
0026 %   mrg_dfsu_apply(func, '>', -12.5, 1);
0027 %   % Every cell less than -12.5 gets a value of 0
0028 %   mrg_dfsu_apply(func, '<', -12.5, 0);
0029 %   % Every euqal to -12.5 gets a value of 1
0030 %   mrg_dfsu_apply(func, '=', -12.5, 1);
0031 %
0032 % REQUIREMENTS
0033 %   The DHI/MIKE Matlab toolbox 2011/13 (developed with v. 20130222)
0034 %
0035 % WARNING
0036 %  This modifies the DFSU file directly, ensure you copy the file first,
0037 %  before attempting to apply this function.
0038 %
0039 % LICENCE
0040 %   Created by Daniel Pritchard (www.pritchard.co)
0041 %   Distributed under a creative commons CC BY-SA licence. See here:
0042 %   http://creativecommons.org/licenses/by-sa/3.0/
0043 %
0044 % DEVELOPMENT
0045 %   v 1.0   July 2013
0046 %           DP. Initial attempt.
0047 
0048 %% Start!
0049 if ~isa(fhandle, 'function_handle')
0050     error('fhandle must be a valid function handle!');
0051 end
0052 
0053 [filename, path] = uigetfile('.dfsu','Select a DFSU file to process');
0054 cd(path);
0055 
0056 choice = questdlg(sprintf('This function WILL modify your DFSU file.\n\nDo you want to continue'), ...
0057     'Warning!', 'OK', 'Cancel', 'Cancel');
0058 if strcmp(choice, 'Cancel')
0059     error('Canceled by user')
0060 end
0061 
0062 % Load libraries
0063 NET.addAssembly('DHI.Generic.MikeZero.DFS');
0064 NET.addAssembly('DHI.Generic.MikeZero.EUM');
0065 import DHI.Generic.MikeZero.DFS.*;
0066 import DHI.Generic.MikeZero.DFS.dfsu.*;
0067 import DHI.Generic.MikeZero.*
0068 
0069 % Get file
0070 dfsu_file = DfsFileFactory.DfsuFileOpenEdit(filename);
0071 
0072 % Read some item information
0073 items = {};
0074 for i = 0:dfsu_file.ItemInfo.Count-1
0075     item = dfsu_file.ItemInfo.Item(i);
0076     items{i+1,1} = char(item.Name);
0077     items{i+1,2} = char(item.Quantity.Unit);
0078     items{i+1,3} = char(item.Quantity.UnitAbbreviation);
0079 end
0080 
0081 % Ask the user to select the item to process
0082 choice = menu(sprintf('Which of these objects do you want to process?'),items{:,1}, 'Ummm... None of the above!');
0083 if choice == 0 || choice == length(items(:,1))+1
0084     error('Do or do not.  There is no try.');
0085 else
0086     dfsu_item = choice; % A number delimiting the item position in the DFSU file.  The same as mat_item (i.e. no zero indexing).
0087 end
0088 
0089 %%
0090 no_timesteps = dfsu_file.NumberOfTimeSteps;
0091 h = waitbar(0,'Please wait...');
0092 for i=0:no_timesteps-1
0093     % Read a timestep from the file
0094     itemData = dfsu_file.ReadItemTimeStep(dfsu_item,i);
0095     data     = double(itemData.Data)';
0096     % Calculate new values
0097     data  = fhandle(data, varargin{:});
0098     % Write to memory
0099     dfsu_file.WriteItemTimeStep(dfsu_item,i,itemData.Time,NET.convertArray(single(data(:))));
0100     waitbar(i+1/no_timesteps);
0101 end
0102 close(h)
0103 dfsu_file.Close();
0104 
0105 fprintf(1, '\nFile modified!\n\n');
0106 
0107 
0108 end

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