1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| % ------------------------------------------------------------
% readgrib .m
% Hege Eskild
% Spring 2014
%
% This function reads the GRIB weather file . Gives out
% vectors with latitude , longitude and time , and matrices
% with significant wave height , mean wave period , mean wave
% direction and U- and V- components of wind .
%
% If another GRIB files with different variable names is used
% The name inside the parentheses must be changed .
%
%------------------------------------------------------------
% Variable name Definition
% ----------------------Input ---------------------------------
% GRIBinput Weather file . Can be forecast or
% hindcast in GRIB format
% ---------------------Output --------------------------------
% lon Vector with longitude values from
% GRIB
% lat Vector with latitude values from
% GRIB
% time Vector with time values from GRIB
% Uwind Matrix with U component of wind
% from GRIB
% Vwind Matrix with V component of wind
% from GRIB
% SWH Matrix with Hs from GRIB
% MWD Matrix with mean wave direction
% from GRIB
% MWP Matrix with mean wave period
% from GRIB
function [lon, lat, time, Uwind, Vwind, SWH, MWD, MWP] = readgrib(GRIBinput)
% Reading the GRIB file
wave = ncgeodataset(GRIBinput);
% Checking contends
wave.variables;
lon = wave{'lon'}(:) ; % Defining longitude vector
lengthlon = length(lon); % Defining length of lon vector
%If the lon values are defined as 0 -360 degrees , the values are
%are change to be between -180 to 180 so that the corresponds
%to the Matlab values .
for i = 1:lengthlon
if lon(i)>180
lon(i) = lon(i)-360;
end
end
lat = wave{'lat'}(:); % Defining latitude vector
lengthlat = length(lat);
time = wave{'time'}(:); % Defining time vector
lengthtime = length(time);
%Creating matrices of size time *lat* lon including U and V
%component of wind , significant wave height , mean wave period
%and mean wave direction
Uwind = wave{'10_metre_U_wind_component_surface'}(:,:,:);
Vwind = wave{'10_metre_V_wind_component_surface'}(:,:,:);
SWH = wave{'Significant_wave_height_msl'}(:,:,:);
MWD = wave{'Mean_wave_direction_msl'}(:,:,:);
MWP = wave{'Mean_wave_period_msl'}(:,:,:);
S = size(MWP);
end |
Partager