% This pulls out the date, hisg, wave direction, and wave height, from the
% WWIII grid files.
Fileshs = dir('*hs.2006*') % all of them
Filesdp = dir('*dp.2006*') % all of them
Filestp = dir('*tp.2006*') % all of them

cmd=['! ncgen -o ' 'ww3_2006HT.nc' ' wave_HT.cdl '];
eval(cmd);
cmd=['! ncgen -o ' 'ww3_2006D.nc' ' wave_D.cdl '];
eval(cmd);
cmd=['! ncgen -o ' 'ww3_2006P.nc' ' wave_P.cdl '];
eval(cmd);

nc=netcdf('ww3_2006HT.nc','w')
nc2=netcdf('ww3_2006D.nc','w')
nc3=netcdf('ww3_2006P.nc','w')


time=[];
height=[];
per=[];
DIR=[];

% Had this set up as one nice flow but it maxed out the memory on Poverty
% so I changed it.

for i=1:12
   
    tmp=read_grib(Fileshs(i).name,-1);
    for j=1:length(tmp)
        tmp2=tmp(j).stime;
        t(j)=julian(datevec(tmp2));
    end
    time=[time;t'];
        clear t
end
m=length(time);
nc{'wave_time'}(1:m)=time;
clear time

p=0;
for i=1:12  
    tmp=read_grib(Fileshs(i).name,-1);
    for j=1:length(tmp)
        p=p+1;
        hs=tmp(j).fltarray;
        nc{'hsig'}(p,:,:)=flipud(reshape(hs,[288 157])');
        clear hs
    end
end

p=0;
for i=1:12    
    tmp=read_grib(Filesdp(i).name,-1);
    for j=1:length(tmp)
        p=p+1;
        d=tmp(j).fltarray;
        nc2{'dir'}(p,:,:)=flipud(reshape(d,[288 157])');
        clear d
    end
end

p=0;
for i=1:12
    tmp=read_grib(Filestp(i).name,-1);
    for j=1:length(tmp)
        p=p+1;
        t=tmp(j).fltarray;
        nc3{'period'}(p,:,:)=flipud(reshape(t,[288 157])');
        clear t
    end
end

close(nc);
close(nc2);
close(nc3);
    