


Converts U and V velcity data into magnitude and direction.
INPUT
curr_east A positive integer defining the column number for the U
component in the DFS0 file.
curr_north A positive integer defining the column number for the V
component in the DFS0 file.
wind Is either 1 if the input data is wind data, otherwise 0.
See NOTES.
OUTPUT
NO OUTPUT AT CONSOLE
Produces a DFS2 file with '_dir' appended to the filename.
Resulting file contains ONLY the calculated speed and direction.
REQUIREMENTS
Requires the MIKE Matlab toolbox. Tested with v. 20110304.
NOTES
Wind directions are typically specifiy as the direction the wind is
*coming from*, whereas other directions (e.g. currents) are specified
as the direction they are *going to*. The wind input allows for this,
and ensures wind directions are calcuated correctly.
LICENCE
Created B. Elsaesser (b.elsaesser@qub.ac.uk)
Updated by Daniel Pritchard (www.pritchard.co)
Original copyright B. Elsaesser. Rewritten code distributed under a
creative commons CC BY-SA licence. See here:
http://creativecommons.org/licenses/by-sa/3.0/
DEVELOPMENT
v 1.0 2012-09-12
DP. Inital attempt.
TODO
Can we add descriptions to DFS2 files? Ask for user input, a-la
mrg_dfs0_car_to_pol?

0001 function mrg_dfs2_cart_to_pol(curr_east,curr_north,wind) 0002 % Converts U and V velcity data into magnitude and direction. 0003 % 0004 % INPUT 0005 % curr_east A positive integer defining the column number for the U 0006 % component in the DFS0 file. 0007 % curr_north A positive integer defining the column number for the V 0008 % component in the DFS0 file. 0009 % wind Is either 1 if the input data is wind data, otherwise 0. 0010 % See NOTES. 0011 % 0012 % OUTPUT 0013 % NO OUTPUT AT CONSOLE 0014 % Produces a DFS2 file with '_dir' appended to the filename. 0015 % Resulting file contains ONLY the calculated speed and direction. 0016 % 0017 % REQUIREMENTS 0018 % Requires the MIKE Matlab toolbox. Tested with v. 20110304. 0019 % 0020 % NOTES 0021 % Wind directions are typically specifiy as the direction the wind is 0022 % *coming from*, whereas other directions (e.g. currents) are specified 0023 % as the direction they are *going to*. The wind input allows for this, 0024 % and ensures wind directions are calcuated correctly. 0025 % 0026 % LICENCE 0027 % Created B. Elsaesser (b.elsaesser@qub.ac.uk) 0028 % Updated by Daniel Pritchard (www.pritchard.co) 0029 % Original copyright B. Elsaesser. Rewritten code distributed under a 0030 % creative commons CC BY-SA licence. See here: 0031 % http://creativecommons.org/licenses/by-sa/3.0/ 0032 % 0033 % DEVELOPMENT 0034 % v 1.0 2012-09-12 0035 % DP. Inital attempt. 0036 % 0037 % TODO 0038 % Can we add descriptions to DFS2 files? Ask for user input, a-la 0039 % mrg_dfs0_car_to_pol? 0040 0041 %% Load libraries 0042 NET.addAssembly('DHI.Generic.MikeZero'); 0043 import DHI.Generic.MikeZero.* 0044 import DHI.Generic.MikeZero.DFS.*; 0045 import DHI.Generic.MikeZero.DFS.dfs123.*; 0046 0047 %% Get file and important info 0048 [file, path] = uigetfile('*.dfs2', 'Choose a DFS2 file to process'); 0049 in_filename = [path,file]; 0050 out_filename = [path,file(1:end-5),'_dir.dfs2']; 0051 dfs2_in = DfsFileFactory.Dfs2FileOpenEdit(in_filename); 0052 nsteps = dfs2_in.FileInfo.TimeAxis.NumberOfTimeSteps; 0053 deleteval = double(dfs2_in.FileInfo.DeleteValueFloat); 0054 0055 %% Create the new DFS2 file 0056 %factory = mrg_effing_factory(); 0057 factory = DHI.Generic.MikeZero.DFS.DfsFactory; 0058 builder = Dfs2Builder.Create('Matlab dfs2 file','Matlab DFS',0); 0059 0060 % Set up the header 0061 builder.SetDataType(dfs2_in.FileInfo.DataType); 0062 builder.SetGeographicalProjection(dfs2_in.FileInfo.Projection); 0063 builder.SetTemporalAxis(dfs2_in.FileInfo.TimeAxis); 0064 builder.SetSpatialAxis(dfs2_in.SpatialAxis); 0065 builder.DeleteValueFloat = dfs2_in.FileInfo.DeleteValueFloat; 0066 0067 % Add custom block(s) - Probably not needed? 0068 % M21_Misc : {orientation (should match projection), drying depth, -900=has projection, land value, 0, 0, 0} 0069 % builder.AddCustomBlock(dfsCreateCustomBlock(factory, 'M21_Misc', [327, 0.2, -900, 10, 0, 0, 0], 'System.Single')); 0070 for j=1:double(dfs2_in.FileInfo.CustomBlocks.Count) 0071 custom_vec = zeros(1,7); 0072 for i=1:7 0073 custom_vec(i) = double(dfs2_in.FileInfo.CustomBlocks.Item(j-1).Data.Item(i-1)); 0074 end 0075 custom_name = char(dfs2_in.FileInfo.CustomBlocks.Item(j-1).Name); 0076 builder.AddCustomBlock(dfsCreateCustomBlock(factory, custom_name, custom_vec, 'System.Single')); 0077 end 0078 0079 % Define and add items items 0080 if wind 0081 builder.AddDynamicItem('Derived Wind Direction', eumQuantity.Create(eumItem.eumIWindDirection, eumUnit.eumUdegree), DfsSimpleType.Float, DataValueType.Instantaneous); 0082 builder.AddDynamicItem('Derived Wind Speed', eumQuantity.Create(eumItem.eumIWindSpeed, eumUnit.eumUmeterPerSec), DfsSimpleType.Float, DataValueType.Instantaneous); 0083 else 0084 builder.AddDynamicItem('Derived Current Direction', eumQuantity.Create(eumItem.eumICurrentDirection, eumUnit.eumUdegree), DfsSimpleType.Float, DataValueType.Instantaneous); 0085 builder.AddDynamicItem('Derived Current Speed', eumQuantity.Create(eumItem.eumICurrentSpeed, eumUnit.eumUmeterPerSec), DfsSimpleType.Float, DataValueType.Instantaneous); 0086 end 0087 0088 % Create the file ready for data 0089 builder.CreateFile(out_filename); 0090 0091 dfs2_out = builder.GetFile(); 0092 0093 %% Do the heavy lifting 0094 % matlab polar coodinates are orientated against the clock and start at 0095 % East with zero, this is taken care of by swapping x,y in the function 0096 % (output) 0097 0098 for i=0:nsteps-1 0099 east_DFS = dfs2_in.ReadItemTimeStep(curr_east,i); 0100 north_DFS = dfs2_in.ReadItemTimeStep(curr_north,i); 0101 east_data = double(east_DFS.Data); 0102 north_data = double(north_DFS.Data); 0103 if north_DFS.Time ~= east_DFS.Time 0104 error('mrg_dfs2_cart_to_pol:timeissue','Time mismatch between objects?!') 0105 end 0106 time = east_DFS.Time; 0107 [dir,speed] = cart2pol(north_data,east_data); 0108 % Following code shamelessly ripped from mrg_dfs0_cart_to_pol (BE) 0109 if wind 0110 dir = dir*180/pi + 180; 0111 else 0112 dir = dir*180/pi; 0113 index = ~(dir > 0); 0114 dir = dir + index * 360; 0115 end 0116 dfs2_out.WriteItemTimeStepNext(1,NET.convertArray(single(dir))); 0117 dfs2_out.WriteItemTimeStepNext(2,NET.convertArray(single(speed))); 0118 end 0119 0120 %% Finish up 0121 dfs2_in.Close(); 0122 dfs2_out.Close(); 0123 0124 %% Assembly import