run code/nctoolbox/setup_nctoolbox.m
clear all

downloadForecast=1;
downloadNowcast=0;
renewCash=0;                            %only necessary if files have been corrupted

%dates=[datenum(2011,04,06,0,0,0):1:floor(now)];
dates=[floor(now)-10:1:floor(now)];
hours=[3 9 15 21];
rootDir='jplroms/';

if downloadNowcast
  webDir='http://ourocean.jpl.nasa.gov:8080/thredds/dodsC/MBNowcast/';%mb_das_2011040609.nc'
  outDir=fullfile(rootDir,'nowcast');
  for i=1:length(dates)
    for h=1:length(hours)  
      fname=['mb_das_' datestr(dates(i)+hours(h)/24,'yyyymmddHH') '.nc'];
      disp(fname)

      if exist(fname)==2&renewCash
        disp('cashed')
      else
        try 
          ds=ncdataset(fullfile(webDir,fname));
          ucar.nc2.FileWriter.writeToFile(ds.netcdf,fullfile(outDir,fname));
        catch
          disp('failed')
        end
      end

    end
  end
end


if downloadForecast
  webDir='http://ourocean.jpl.nasa.gov:8080/thredds/dodsC/MBForecast/';%mb_fcst_2011040603.nc';
  outDir=fullfile(rootDir,'forecast');
  for i=1:length(dates)
    fname=['mb_fcst_' datestr(dates(i)+3/24,'yyyymmddHH') '.nc'];
    disp(fname)

    if exist(fname)==2&renewCash
      disp('cashed')
    else
      try
        ds=ncdataset(fullfile(webDir,fname));
        ucar.nc2.FileWriter.writeToFile(ds.netcdf,fullfile(outDir,fname));
      catch
        disp('failed')
      end
    end

  end
end




