function data=ctdh(ptr,depth_range)
% function data=ctdh(ptr,depth_range)
%function ctdview4(ptr,coastfil)
% ptr = is a string containing the pointer file name 
%   (a file containing the names of all the netcdf casts)
% coast = string containing name of coast.mat file (assumed varaible is
% called "coast"
%
% Interactive display of EPIC style netCDF CTD profiles.  
% Plots the station locations for the
% cruise, then lets the user click on the station location to display the 
% profiles at that station
%

% 2. Requires metcdf.mex, mcvgt.m, laxis.m, clearg.m, stations.m
%
% Rich Signell               |  rsignell@crusty.er.usgs.gov
% U.S. Geological Survey     |  (508) 457-2229  |  FAX (508) 457-2310
% Quissett Campus            | 
% Woods Hole, MA  02543      |
%--------------------------------------------------------------------
depth_range=abs(depth_range);
%eval(['load ' coastfil])

  var1='temp';
  var2='salt';
  var3='dens';
 
fid=fopen(ptr,'r');
sta=fscanf(fid,'%s');
status=fclose(fid);
l=length(sta);
nchar=12;
m=l/nchar;
sta=reshape(sta,nchar,m)';

[m,n]=size(sta);
mexcdf('setopts',0);
for j=1:m
  cdfid=mexcdf('open',sta(j,:),'nowrite');
  lat(j)=mexcdf('varget1',cdfid,'latitude',0);
  lon(j)=-(mexcdf('varget1',cdfid,'longitude',0));
  [depnam,dlen]=mexcdf('diminq',cdfid,'depth');
  depth=mexcdf('varget',cdfid,'depth',0,dlen);
  depth=abs(depth);
  corner=[0 0 0 0];
  edge=[1 dlen 1  1];
  u1=mexcdf('varget',cdfid,var1,corner,edge);
  lab1=mexcdf('attget',cdfid,var1,'long_name');
  xlab1=mexcdf('attget',cdfid,var1,'units');
  u2=mexcdf('varget',cdfid,var2,corner,edge);
  lab2=mexcdf('attget',cdfid,var2,'long_name');
  xlab2=mexcdf('attget',cdfid,var2,'units');
  u3=mexcdf('varget',cdfid,var3,corner,edge);
  lab3=mexcdf('attget',cdfid,var3,'long_name');
  xlab3=mexcdf('attget',cdfid,var3,'units');
  ind=find(depth>=min(depth_range)&depth<=max(depth_range);
  v(j,1)=mean(u1(ind));
  v(j,2)=mean(u2(ind));
  v(j,3)=mean(u3(ind));
  rcode=mexcdf('close',cdfid);
end
data=[lon lat mean(depth_range)*ones(length(lon),1) v];
