Converts U and V velcity data into magnitude and direction in a DFSO file 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 DFS0 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. Requires mrg_read_dfs0 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 March 2012 BE. Revised for Mike2011 version and new Read_dfs0 function output. BE. Modified item description & additional check in input (equidistance time axis) v 1.1 2012-09-12 DP. Documentation. Name change. DP. Uses modified mrg_read_dfs0 function. Modified to account for the fact that mrg_read_dfs0 no longer CD's the MATLAB working directory in the original directory. TODO Update to use .NET frameworks for writing files. Better yet, create a generic mrg_write_dfs0 function and pass the file saving on to this function.
0001 function mrg_dfs0_cart_to_pol(curr_east,curr_north,wind) 0002 % Converts U and V velcity data into magnitude and direction in a DFSO file 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 DFS0 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 % Requires mrg_read_dfs0 0020 % 0021 % NOTES 0022 % Wind directions are typically specifiy as the direction the wind is 0023 % *coming from*, whereas other directions (e.g. currents) are specified 0024 % as the direction they are *going to*. The wind input allows for this, 0025 % and ensures wind directions are calcuated correctly. 0026 % 0027 % LICENCE 0028 % Created B. Elsaesser (b.elsaesser@qub.ac.uk) 0029 % Updated by Daniel Pritchard (www.pritchard.co) 0030 % Original copyright B. Elsaesser. Rewritten code distributed under a 0031 % creative commons CC BY-SA licence. See here: 0032 % http://creativecommons.org/licenses/by-sa/3.0/ 0033 % 0034 % DEVELOPMENT 0035 % v 1.0 March 2012 0036 % BE. Revised for Mike2011 version and new Read_dfs0 function output. 0037 % BE. Modified item description & additional check in input (equidistance time axis) 0038 % v 1.1 2012-09-12 0039 % DP. Documentation. Name change. 0040 % DP. Uses modified mrg_read_dfs0 function. Modified to account 0041 % for the fact that mrg_read_dfs0 no longer CD's the MATLAB 0042 % working directory in the original directory. 0043 % TODO 0044 % Update to use .NET frameworks for writing files. Better yet, create a 0045 % generic mrg_write_dfs0 function and pass the file saving on to this 0046 % function. 0047 0048 %% read data 0049 0050 RecData = mrg_read_dfs0; 0051 0052 %check defined data columns 0053 if curr_east > length(RecData.items(:,1)) 0054 msgbox('Column number for east item is greater than number of items') 0055 return; 0056 elseif curr_north > length(RecData.items(:,1)) 0057 msgbox('Column number for north item is greater than number of items') 0058 return; 0059 end 0060 0061 % check that file is equidistant time axis file 0062 if RecData.dTime(2) < 0 0063 msgbox('File has a non equidistant time axis, please convert & try again') 0064 return 0065 end 0066 0067 data = RecData.dData(:,[curr_east curr_north]); 0068 0069 %% convert direction and current into polar coordinates 0070 % matlab polar coodinates are orientated against the clock and start at 0071 % East with zero, this is taken care of by swapping x,y in the function 0072 % (output) 0073 [new_data(:,1),new_data(:,2)] = cart2pol(data(:,2),data(:,1)); 0074 0075 if wind 0076 new_data(:,1) = new_data(:,1)*180/pi + 180; 0077 else 0078 new_data(:,1) = new_data(:,1)*180/pi; 0079 index = ~(new_data(:,1) > 0); 0080 new_data(:,1) = new_data(:,1)+ index * 360; 0081 end 0082 0083 %% write data to dfs0 file 0084 % prepare all inpout parameter to dfs0 file 0085 filename = [RecData.Fullname(1:end-5),'_dir.dfs0']; 0086 0087 dfs0 = dfsTSO(filename,1); 0088 0089 % Set a file title 0090 set(dfs0,'filetitle',RecData.title); 0091 0092 % Set startdate and timestep interval 0093 set(dfs0,'startdate',datevec(RecData.dTime(4))); 0094 set(dfs0,'timestep',[0 0 0 0 0 RecData.dTime(2)]); 0095 0096 % Add number of timesteps 0097 addTimesteps(dfs0,RecData.dTime(1)); 0098 0099 % define item description 0100 if wind 0101 def_descript1 = cellstr('derived wind direction'); 0102 def_descript2 = cellstr('derived wind speed'); 0103 else 0104 def_descript1 = cellstr('derived current direction'); 0105 def_descript2 = cellstr('derived current speed'); 0106 end 0107 0108 idescript1 = inputdlg('Enter item description','Direction component',... 0109 1,def_descript1); 0110 idescript2 = inputdlg('Enter item description','Magnitude component',... 0111 1,def_descript2); 0112 0113 % Add Items & define item structure 0114 if wind 0115 addItem(dfs0,char(idescript1),'Wind direction','deg'); 0116 addItem(dfs0,char(idescript2),'Wind Speed','m/s'); 0117 else 0118 addItem(dfs0,char(idescript1),'Current direction','deg'); 0119 addItem(dfs0,char(idescript2),'Current magnitude','m/s'); 0120 end 0121 0122 % write data to file 0123 dfs0(1) = single(new_data(:,1)); 0124 dfs0(2) = single(new_data(:,2)); 0125 0126 % Save and close files 0127 save(dfs0); 0128 close(dfs0);