function out = coops_get(station,dates,interval,datum)


% All stations are listed here.
%url = 'http://tidesandcurrents.noaa.gov/station_retrieve.shtml?type=Historic%20Tide%20Data&state=All%20Stations&id1=';
out.station = station;
station = num2str(station);
if length(dates)==1 % Get one year.
   dates = [datenum(dates,1,1) datenum(dates,12,31)];
end
bdate = datestr(dates(1),'yyyymmdd');
edate = datestr(dates(2),'yyyymmdd');

% Check interval.
int = {'six minute','W1';'hourly','W2';...
   'high/low','W3';'monthly','W4'};
id = find(strcmp(interval,int(:,161)));
if ~isempty(id)
   int = int{id,2};
else
   disp([9,'Bad interval',10])
   return
end

% Check datum.
if sum(strcmp(datum,{'mhhw','mhw','msl','mlw','mllw'}))==0
   datum = 'msl';
end

unit = 'meters';

% shift = {'local','d';'gmt','g';'lst','s'};
shift = 'g';   %GMT

%% 
url = ['http://tidesandcurrents.noaa.gov/station_info.shtml?stn=',...
   station];
tmp = urlread(url);
str = {'Latitude','Longitude'};
for ii=1:length(str)
   t = regexp(tmp,[str{ii},'.*'],'match');
   t = t{:};
   id1 = strfind(t,'<B>');
   id2 = strfind(t,'</B>');
   t = t(id1(1)+3:id2(1)-1);
   t = regexp(t,'\d+\.?\d+','match');
   t = str2num(char(t{:}));
   eval(['out.',str{ii},' = t(1)+t(2)/60;'])
end

%%
url = ['http://tidesandcurrents.noaa.gov/data_menu.shtml?bdate=',bdate,...
   '&edate=',edate,'&wl_sensor_hist=',int,'&relative=&datum=',datum,...
   '&unit=',unit,'&shift=',shift,'&stn=',station,...
   '&type=Historic+Tide+Data&format=View+Data'];

data = urlread(url);
id1 = regexp(data,'<pre>','end');
id2 = regexp(data,'</pre>','start');
if isempty(id1)
   id1 = regexp(data,'Error:');
   if ~isempty(id1)
      id2 = regexp(data,'<');
      id2 = id2(find(id2>id1,1,'first'));
      warning(data(id1:id2-1))
   else
      warning('You have a problem!')
   end
   return
end

data = data(id1(end)+1:id2(1)-1);
id = regexp(data,'-{2}','end');
data = data(id(end)+2:end);
id = regexp(data,'\n');
d = [];
for ii=1:length(id)-1
   line = data(id(ii):id(ii+1));
   tmp = regexp(line,'\d+ \d+:\d+','match');
   t = datenum(tmp,'yyyymmdd HH:MM');
   idc = findstr(':',line);
   tmp = str2num(line(idc+3:end));
   if length(tmp)==1
      tmp = [tmp nan];
   end
   d = [d; t tmp];
end

out.time  = d(:,1);
out.predicted = d(:,2);
out.measured = d(:,3);

return
%% OLD
%GETCOOPS   Get NOAA/NOS/COOPS tide data
%
% This only works in 2008a 32-bit!!!!!
%
%  Get Co-ops tide data using loaddods (ugh). Hopefully someday they update
%  their data to include all verified data and in the format so that
%  njtoolbox can be used.
%
% 12/09
%%

if ~exist('dtype','var')
   datatype = 'hour';
end
if ~exist('datum','var')
   datum = 'msl';
end

%%
tmp = urlread('http://opendap.co-ops.nos.noaa.gov/stations/stationsXML.jsp');
id = regexp(tmp,['"[a-z_A-Z, -]+" ID="',num2str(station)],'end');
name = regexp(tmp,['"[a-z_A-Z, -]+" ID="',num2str(station)],'match');
name = regexp(name{:},'[a-z_A-Z, -]+','match');
out.id = station;
out.name = name{1};
tmp = tmp(id:end);
id = regexp(tmp,'</long>','start');
tmp = tmp(1:id(1));
lon = regexp(tmp,'<long>-?[0-9\.]+<','match');
out.lon = str2num(cell2mat(regexp(lon{:},'-?[0-9\.]+','match')));
lat = regexp(tmp,'<lat>-?[0-9\.]+<','match');
out.lat = str2num(cell2mat(regexp(lat{:},'-?[0-9\.]+','match')));



dtype = {'six','SixMin_Verified_Water_Level','WATERLEVEL_SIXMIN_VFD_PX';
   'hour','Hourly_Height_Verified_Water_Level','WATERLEVEL_HOURLY_VFD_PX';
   'highlow','High_Low_Verified_Water_Level','WATERLEVEL_HIGHLOW_VFD_PX';
   'daily','Daily_Verified_Water_Level','WATERLEVEL_DAILY_VFD_PX'};
dtype = dtype(strmatch(datatype,dtype(:,1)),:);

datum = upper(datum);

url = ['http://opendap.co-ops.nos.noaa.gov/dods/IOOS/',dtype{2},...
   '?&',dtype{3},'._STATION_ID="',num2str(station),'"&',dtype{3},...
   '._DATUM="',datum,'"&',dtype{3},'._BEGIN_DATE="',...
   datestr(dates(1),'yyyymmdd'),'"&',dtype{3},'._END_DATE="',...
   datestr(dates(2),'yyyymmdd'),'"']

loaddap(url);

eval(['tmp = ',dtype{3},';'])
out.datum = tmp.ml__DATUM(1,:);
out.t = datenum(tmp.DATE_TIME);
out.wl = tmp.WL_VALUE;
out.sigma = tmp.SIGMA;
