function [lat,lon,tme,sct,stn,cst,P,T,S] = woce_read(fname);
% function [lat,lon,tme,sct,stn,cst,P,T,S] = woce_read(fname);
% Reads in miscellaneous WOCE hydrography data files
% Outputs
%  lat : latitude north (deg)
%  lon : longitude east (deg)
%  tme : date in matlab format (serial day from 1-Jan-0000)
%  sct : section
%  stn : station
%  cst : cast
%  P   : pressure (dbar)
%  T   : temperature (ITS-90)
%  S   : salinity (PSS-78)

% D Menemenlis, 26 mar 2002

% initialize variables
lat=nan;
lon=nan;
tme=nan;
sct=nan;
stn=nan;
cst=nan;
P=nan;
T=nan;
S=nan;

% open file
fid = fopen(fname,'r');
if fid == -1
  error(['File ',fname,' not found.'])
end

if findstr('.C00',fname)

  sct='ar15_l';                      % section
  cst=1;                             % cast
  while ~feof(fid), tmp=fgetl(fid);  % date
    if findstr('* System UpLoad Time = ',tmp)
      tme=datenum(tmp(23:length(tmp)));
  break, end, end
  while ~feof(fid), tmp=fgetl(fid);  % station
    if findstr('* Station: ',tmp)
      stn=sscanf(tmp,'* Station: %s');
  break, end, end
  while ~feof(fid), tmp=fgetl(fid);  % latitude
    if findstr('* Latitude: ',tmp)
      lat=parse_lat(tmp(12:length(tmp)));
      if lat>0, lat=-lat; end
  break, end, end
  while ~feof(fid), tmp=fgetl(fid);  % longitude
    if findstr('* Longitude: ',tmp)
      lon=parse_lon(tmp(13:length(tmp)));
      if lon<180, lon=360-lon; end
  break, end, end
  while ~feof(fid), tmp=fgetl(fid);  % data check
    if strcmp( ...
      '* columns: p, t0(68), theta0(68), s00, sig-t00,sig2-th00',tmp)
  break, end, end
  while ~feof(fid), tmp=fgetl(fid);  % data start
    if findstr('*END*',tmp)
  break, end, end
  if feof(fid), error('unexpected header contents'), end
  tmp=fscanf(fid,'%f');
  in=find(tmp(1:50)==0);
  tmp(in)=[];
  tmp=reshape(tmp,6,length(tmp)/6);
  P=tmp(1,:)';
  T=tmp(2,:)';
  S=tmp(4,:)';
  in=find(S==0);
  P(in)=[];
  T(in)=[];
  S(in)=[];

elseif length(findstr('.PRS',fname)) | length(findstr('.prs',fname))

  if strcmp(fname(1:4),'BD04')
    sct='ar08_d';
  elseif strcmp(fname(1:4),'BD09')
    sct='ar08_e';
  elseif strcmp(fname(1:4),'EH10')
    sct='ar08_g';
  elseif strcmp(fname(1:4),'ci10')
    sct='ar04_c';
  elseif strcmp(fname(1:4),'kn04')
    sct='ar04_f';
  else
    error('unable to assign section')
  end
  cst=1;                             % cast
  tmp=fgetl(fid);                    % Cruise #
  in=findstr('Cruise #:',tmp);
  if isempty(in), error('Cruise # not found'), end
  tmp=fgetl(fid);                    % station
  in=findstr('Sta.#:',tmp);
  if isempty(in)
    error('Sta.# not found')
  else
    stn=sscanf(tmp((in+6):(in+17)),'%s');
  end
  for i=1:2, tmp=fgetl(fid); end     % date
  in=findstr('Date:',tmp);
  if isempty(in)
    error('Date not found')
  else
    dte=sscanf(tmp((in+6):(in+18)),'%s');
  end
  tmp=fgetl(fid);                    % time
  in=findstr('St.Time:',tmp);
  if isempty(in)
    error('Time not found')
  else
    stm=sscanf(tmp((in+8):(in+18)),'%s');
  end
  tme=datenum([dte ' ' stm]);
  tmp=fgetl(fid);                    % latitude
  in=findstr('St.Lat.:',tmp);
  if isempty(in)
    error('Lat not found')
  else
    lat=parse_lat(tmp((in+8):(in+20)));
  end
  tmp=fgetl(fid);                    % longitude
  in=findstr('St.Long:',tmp);
  if isempty(in)
    error('Long not found')
  else
    lon=parse_lon(tmp((in+8):(in+20)));
  end
  for i=1:8, tmp=fgetl(fid); end     % data check
  if ~findstr('PRES*    TEMP*    SALT*    OXGY*    NOBS',tmp)
    error('unexpected header contents')
  end
  tmp=fscanf(fid,'%f,%f,%f,%f,%f\n',[5 inf]);
  P=tmp(1,:)';
  T=tmp(2,:)';
  S=tmp(3,:)';
  T(find(T==-9))=nan;
  S(find(S==-9))=nan;

elseif findstr('ds93c.n',fname)

  sct='ar09_c';
  tmp=fgetl(fid);
  if ~strcmp(tmp, ...
    'SHIP  CRUISE  STA CAST  LAT      LONG      DATE   YDAY  TIME  DEPTH')
    error('unexpected header contents')
  end
  tmp=fgetl(fid);
  out=sscanf(tmp(7:62),'%f');
  stn=int2str(out(2)); cst=out(3);
  lat=out(4); lon=out(5); if lon<0, lon=lon+360; end
  dte=out(6); dy=out(7); tm=out(8);
  dte=myint2str(dte,6); yr=1900+str2num(dte(1:2));
  if yr<1950, yr=yr+100; end
  tm=myint2str(tm,4);
  tme=datenum(yr,str2num(dte(3:4)),str2num(dte(5:6)), ...
      str2num(tm(1:2)),str2num(tm(3:4)),0);
  tm2=datenum(yr,0,dy,str2num(tm(1:2)),str2num(tm(3:4)),0);
  if tme~=tm2, error('date mispecification'), end
  tmp=fgetl(fid);
  if ~strcmp(tmp,'PRESS CTDTEMP  CTDSAL  CTD_O2')
    error('unexpected header contents')
  end
  tmp=fscanf(fid,'%f',[4 inf]);
  P=tmp(1,:)';
  T=tmp(2,:)';
  S=tmp(3,:)';
 
elseif findstr('.ecp',fname)

  if strcmp(fname(1:5),'roma1')
    sct='ar15_d';
  elseif strcmp(fname(1:5),'roma2')
    sct='ar15_f';
  elseif strcmp(fname(1:5),'roma3')
    sct='ar15_m';
  else
    error('unable to assign section')
  end
  tmp=fgetl(fid);
  stn=sscanf(tmp(1:4),'%s');
  cst=1;
  tmp=fgetl(fid);
  if ~strcmp(tmp,'*No station,nom campagne,nom navire,type de sonde')
    error('unexpected header contents')
  end
  tmp=fgetl(fid);
  if ~strcmp(tmp,'*(i3,2(1x,a16),1x,a6)')
    error('unexpected header contents')
  end
  tmp=fgetl(fid);
  yr=1900+str2num(tmp(5:6)); if yr<1950, yr=yr+100; end
  tme=datenum(yr,str2num(tmp(3:4)),str2num(tmp(1:2)), ...
      str2num(tmp(8:9)),str2num(tmp(10:11)),0);
  lat=parse_lat(tmp(13:23));
  lon=parse_lon(tmp(25:35));
  np=str2num(tmp(37:38));
  if np<3, error('unexpected header contents'), end
  tmp=fgetl(fid);
  if ~strcmp(tmp, ...
    '*Date,heure,lat.,lon.,nb parametres,nb mesures,fond,pmin,pmax')
    error('unexpected header contents')
  end
  tmp=fgetl(fid);
  if ~strcmp(tmp,'*(a6,1x,a4,1x,a11,1x,a11,1x,a2,4(1x,a4))')
    error('unexpected header contents')
  end
  tmp=fgetl(fid);
  if ~strcmp(tmp,'PRESSION                dbar        ')
    error('unexpected header contents')
  end
  tmp=fgetl(fid);
  if ~strcmp(tmp,'TEMPERATURE             deg.cels.   ')
    error('unexpected header contents')
  end
  tmp=fgetl(fid);
  if ~strcmp(tmp,'SALINITE                p.s.u.      ')
    error('unexpected header contents')
  end
  for n=4:np, tmp=fgetl(fid); end
  if findstr('roma1',fname), tmp=fgetl(fid); end
  tmp=fscanf(fid,'%f',[np inf]);
  P=tmp(1,:)';
  T=tmp(2,:)';
  S=tmp(3,:)';
  
elseif findstr('.s87',fname)

  if strcmp(fname(1:3),'EW9')
    sct='ar09_b';
  elseif strcmp(fname(1:3),'A13')
    sct='ar09_d';
  else
    error('unable to assign section')
  end
  tmp=fgetl(fid);
  in=findstr(' ',tmp);
  stn=tmp((in(2)+1):(in(3)-1));
  cst=1;
  lat=str2num(tmp((in(3)+1):(in(4)-1)));
  lon=str2num(tmp((in(4)+1):(in(5)-1)));
  dt=tmp((in(5)+1):(in(6)-1));
  tm=tmp((in(7)+1):(in(8)-1));
  tme=datenum(str2num(dt(1:4)),str2num(dt(6:7)), ...
      str2num(dt(9:10)),str2num(tm(1:2)),str2num(tm(4:5)),0);
  while ~feof(fid), tmp=fgetl(fid);
  if strcmp('@PRTESA',sscanf(tmp,'%s')), break, end, end
  if feof(fid), error('unexpected header contents'), end
  tmp=fscanf(fid,'%f',[3 inf]);
  P=tmp(1,:)';
  T=tmp(2,:)';
  S=tmp(3,:)';

elseif findstr('.csv',fname)

  clear P T S sct stn
  tmp=fgetl(fid);
  if ~strcmp(tmp, ...
  'Platform,St_No,Latdeg,Longdeg,Year,Mnth,Dy,Hr,Sound,d/p,TEMP,PSAL')
  error('unexpected header contents'), end
  tmp=fscanf(fid,'%*4c,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n',[11 inf]);
  ie=[find(abs(diff(tmp(1,:)))>0) length(tmp(1,:))];
  is=[0 ie(1:(length(ie)-1))]+1;
  for n=1:length(is)
    sct{n} = fname(1:6);
    stn{n} = num2str(tmp(1,is(n))); disp(stn{n})
    lat(n) = tmp(2,is(n));
    lon(n) = tmp(3,is(n));
    hr = myint2str(tmp(7,is(n)),4);
    tme(n) = datenum(tmp(4,is(n)),tmp(5,is(n)),tmp(6,is(n)), ...
        str2num(hr(1:2)),str2num(hr(3:4)),0);
    P{n} = tmp( 9,is(n):ie(n));
    T{n} = tmp(10,is(n):ie(n));
    S{n} = tmp(11,is(n):ie(n));
  end
  in=find(lon<0); lon(in)=lon(in)+360;

elseif findstr('ar07e_fct.txt',fname)
  
  clear P T S sct stn
  for n=1:16, tmp=fgetl(fid); end
  n=0;
  while ~feof(fid)
    n=n+1;
    tmp=sscanf(tmp,'%f');
    sct{n}='ar07e_f';
    stn{n}=num2str(tmp(1));
    dt=myint2str(tmp(2),6);
    yr=1900+str2num(dt(1:2)); if yr<1950, yr=yr+100; end
    hr=myint2str(tmp(3),4);
    tme(n)=datenum(yr,str2num(dt(3:4)),str2num(dt(5:6)), ...
      str2num(hr(1:2)),str2num(hr(3:4)),0);
    lat(n)=tmp(5);
    lon(n)=tmp(4);
    np=tmp(7);
    tmp=fgetl(fid);
    tmp=fscanf(fid,'%f',[3 np]);
    P{n}=tmp(1,:)';
    T{n}=tmp(2,:)';
    S{n}=tmp(3,:)';
    for j=1:3, tmp=fgetl(fid); end
  end
  in=find(lon<0); lon(in)=lon(in)+360;

elseif findstr('.txt',fname)

  clear P T S sct stn
  n=0;
  while ~feof(fid)
    n=n+1;
    while ~feof(fid), tmp=fgetl(fid);
    if findstr('STATION NUMBER',tmp), break, end, end
    if feof(fid), break, end
    stn{n}=sscanf(tmp(20:length(tmp)),'%s'); disp(stn{n})
    tmp=fgetl(fid); dt=tmp(20:32);
    tmp=fgetl(fid); tm=tmp(21:24);
    tme(n)=datenum([dt tm(1:2) ':' tm(3:4)]);
    for j=1:3, tmp=fgetl(fid); end
    sct{n}=fname(1:6);
    tmp=fgetl(fid);
    lat(n)=parse_lat(tmp(20:29));
    lon(n)=parse_lon(tmp(31:40));
    for j=1:6, tmp=fgetl(fid); end
    if ~length(findstr(tmp, ...
          'PRESS  TEMP   SAL  SIGMA-T S.V.A.  G.A.      D.O.'))
      error('unexpected header line')
    end
    tmp=fgetl(fid);
    j=0;
    pst=zeros(5000,3);
    while ~feof(fid)
      tmp=fgetl(fid); if strcmp(tmp(1:4),'SSSS'), break, end
      tmp2=str2num(tmp);
      if length(tmp2)>=3
        j=j+1;
        pst(j,:)=tmp2(1:3);
      end
    end
    P{n}=pst(1:j,1);
    T{n}=pst(1:j,2);
    S{n}=pst(1:j,3);
  end

elseif findstr('M5',fname)

  sct='ar07e';
  tmp=fgetl(fid);
  if ~strcmp(tmp(1:20),'MAIN-HEADER CTD cast')
    error('unexpected header contents')
  end
  stn=tmp(24:26);
  cst=str2num(tmp(27:29));
  tmp=fgetl(fid);
  tmp=fgetl(fid);
  if ~strcmp(tmp(1:27),'Stopdate;  Stoptime;  (UTC)')
    error('unexpected header contents')
  end
  tme=datenum(str2num(tmp(50:53)),str2num(tmp(55:56)), ...
      str2num(tmp(58:59)),str2num(tmp(62:63)),str2num(tmp(65:66)),0);
  tmp=fgetl(fid);
  tmp=fgetl(fid);
  if ~strcmp(tmp(1:46),'Stopposition  Lat.;Long.;(ggg mm.mm n/s/e/w);:')
    error('unexpected header contents')
  end
  lat=parse_lat(tmp(50:60));
  lon=parse_lon(tmp(62:73));
  while ~feof(fid), tmp=fgetl(fid);
  if strcmp('CHANNEL-HEADER',tmp), break, end, end
  tmp=fgetl(fid);
  if ~strcmp(tmp(1:28),' 1;  PRESSURE  ;   10;  dbar')
    error('unexpected header contents')
  end
  tmp=fgetl(fid);
  if ~strcmp(tmp(1:28),' 2;  TEMPERATUR;    8;  degC')
    error('unexpected header contents')
  end
  tmp=fgetl(fid);
  if ~strcmp(tmp(1:28),' 3;  SALINITY  ;   11;  psu ')
    error('unexpected header contents')
  end
  while ~feof(fid), tmp=fgetl(fid);
  if strcmp('DATA',tmp), break, end, end
  tmp=fscanf(fid,'%f',[4 inf]);
  P=tmp(1,:)';
  T=tmp(2,:)';
  S=tmp(3,:)';

else
  error('file type not recognized')
end
fid=fclose(fid);

% check data
if min(lat)<-90 | max(lat)>90 | min(lon)<0 | max(lon)>360 | ...
      min(tme)<datenum(1980,1,1) | max(tme)>datenum(2002,1,1)
  error('invalid lat, lon, or tme')
end
if length(tme)>1
  for n=1:length(tme)
    in=find(P{n}==0|isnan(P{n}));
    P{n}(in)=[]; T{n}(in)=[]; S{n}(in)=[];
    S{n}(find(S{n}==0))=nan;
    S{n}(find(P{n}>1000&S{n}<30))=nan;
    T{n}(find(T{n}==0))=nan;
    if min(P{n})<0 | max(P{n})>1e4 | min(S{n})<0 | ... 
          max(S{n})>45 | min(T{n})<-5 | max(T{n})>40
    error('invalid P, S, or T'), end
  end
else
  in=find(P==0|isnan(P));
  P(in)=[]; T(in)=[]; S(in)=[];
  S(find(P>1000&S<30))=nan;
  S(find(S==0))=nan; T(find(T==0))=nan;
  if min(P)<0 | max(P)>1e4 | min(S)<0 | ... 
        max(S)>45 | min(T)<-5 | max(T)>40
  error('invalid P, S, or T'), end
end