function wrf2mat(wrfFile,outDir)

% USAGE:
%    wrf2mat(wrfFile,outFile)
%    will take the TUV struct, write the "date, lat, lon, time, U,V currents",
%    for use with LCS calculation. 


% No NaN's allowed in TUVstruct2mat function - id all
ind = ~(isnan(T.U)  |  isnan(T.V));
if sum(ind) == 0
    fprintf('No data in this TUV struct, no mat file written')
    return;
end

try, outDir;
catch
    outDir = pwd;
end

uvFlag = 9999.0;


% See how many valid times exist
index = find(isfinite(T.TimeStamp));
if isempty(index)
    return;
end

[yyyy1,mm1,dd1,hh1,MM1,ss1]=datevec(T.TimeStamp(index(1)));    % startdate
[yyyy2,mm2,dd2,hh2,MM2,ss2]=datevec(T.TimeStamp(index(end)));  % enddate

% Create the file name
fname = sprintf('%s_%s_%.4d_%.2d_%.2d_%.2d00-%.4d_%.2d_%.2d_%.2d00.mat', ...
    T.Type,T.DomainName,yyyy1,mm1,dd1,hh1,yyyy2,mm2,dd2,hh2);

d=T.TimeStamp;
[yyyy, mm, dd, HH, MM] = datevec(d);
doy = date2doy(d);
doy = doy(:);
yyyy = yyyy(:);
mm = mm(:);
dd = dd(:);
HH = HH(:);
MM = MM(:);
date = [ doy, yyyy, mm, dd, HH, MM];

time = cumsum(diff(d));
time = [0;time(:)];
lon = unique(T.LonLat(:,1));
lat = unique(T.LonLat(:,2));

% Put things on a grid
[Tgrid,gridDim] = gridTotals( T );
s = size(Tgrid.U);

% U, V will be a 3D matrix with dimensions [Lat,Lon,Time]
vlon = reshape( Tgrid.U, [ gridDim, s(2) ] );
vlat = reshape( Tgrid.V, [ gridDim, s(2) ] );

% permute [Time,Lat,Lon]
vlon = permute(vlon,[3,1,2]);
vlat = permute(vlat,[3,1,2]);

save(fname,'date','vlon','vlat','time','lon','lat');
end


