function [argout1, argout2, argout3, argout4, argout5, argout6] = getwvs(...
          mode, get_ranges, get_dset, get_vars, get_dset_stride, get_num_urls, ...
          get_georange, get_variables, get_archive, whichurl)
%
% 
% GETwvs   Part of the DODS browse package.
%
% This function is used to get wvs coastline
%

%
% $Log: getwvs.m,v $
% Revision 1.1  1999/11/29 17:28:24  kwoklin
% Adding World Vector Shoreline as a data set.  klee
%

% $Id: getwvs.m,v 1.1 1999/11/29 17:28:24 kwoklin Exp $
% klee 11/26/99

% used by DODS browser

% used only locally
global Pass_Times Pass_Depths old_var old_dset old_ranges 

% mode may be 'cat' (Catalogue data holdings) or 'get' (Get Data)
%
% if 'get', will have a second arguement, if 0, eval total size of request
% and return. If 1 or greater, get the data.
%

if exist(get_archive) == 2
  eval(get_archive)
else
  dodsmsg(['Problem reading dataset metadata ' get_archive '.m'])
  return
end

if nargin >= 1

  % initialize return arguments
  argout1 = [];
  argout2 = [];
  argout3 = [];
  argout4 = [];
  argout5 = [];
  argout6 = [];

  % SET UP THE METADATA
  % ************************************************************************
  % Set-up for accessing the FRONTS DODS server:
  % Create  constraint expression form browser input values-
  % date, lat/lon, archive type and depth
  % ************************************************************************

  if strcmp(mode,'cat') 		% 'cat' return catalogue of time value
    
    %catch any unmatched data range
    if(get_ranges(4,1) > TimeRange(2) | get_ranges(4,2) < TimeRange(1))
      dodsmsg(' Requested time range does not overlap data. Please respecify')
      return
    end
    
    %save these values
    old_ranges = get_ranges;
    old_dset = get_dset;
    old_var = get_vars;

    %fill up returned arguments with dummy values to prevent crashes
    num_url = 1;  %set default
    loaddodsbool = 1; %set default 
    times = []; Pass_Times = []; depths = []; Pass_Depths = []; 
    lats = []; lons = []; infotext = [];
    url = [];
    
    if strcmp(CatalogServer, 'annual')
      [times, Pass_Times] = annual(TimeRange, get_ranges);
      if isempty(times), num_url = 0;  end
    end
    argout6 = url;

    % FIXED so that if there are routines with names in TimeName, they
    % are not accidentally evaluated -- dbyrne 99/06/01
    %argout1, 2, 3, 4, 5, 6 are x, y, z, t, n, URL, respectively
    if isempty(times) & exist(TimeName)==1, argout4 = eval(deblank(TimeName)); 
    elseif ~isempty(times), argout4 = times; else argout4 = []; end
    if isempty(depths) & exist(DepthName)==1, argout3 = eval(deblank(DepthName)); 
    elseif ~isempty(depths), argout3 = depths; else argout3 = []; end
    if isempty(lats) & exist(LatitudeName)==1, argout2 = eval(deblank(LatitudeName));
    elseif ~isempty(lats), argout2 = lats; else argout2 = []; end
    if isempty(lons) & exist(LongitudeName)==1, argout2 = eval(deblank(LongitudeName));
    elseif ~isempty(lons), argout1 = lons; else argout1 = []; end

    % indicate if there really is any data 
    if any(length(argout1) > 0 | length(argout2) > 0 | ...
      length(argout3) > 0 | length(argout4) > 0)
      if argout4 > 1e-10
        % jg server returns a big negative number if any data is null
        argout5 = num_url;
      else
        argout5 = 0;
      end
    else
      argout5 = 0;
    end
    
    return                              % catalog query ('cat') mode over

  else                                  % get data w/o catalog query
    %fill up returned arguments with dummy values to prevent crashes
    num_url = 1;  loaddodsbool = 1; %set default 
    times = []; Pass_Times = []; depths = []; Pass_Depths = []; 
    lats = []; lons = []; infotext = [];
    url = [];

    %Before going to get, check first if the data ranges has changed, second if the 
    %size of the data reqested is larger than the check_level in browse.m

    if isempty(old_var) | isempty(old_dset) | isempty(old_ranges)
      [x,y,z,t,n,URL] = getwvs('cat',get_ranges, get_dset, get_vars, ...
          get_dset_stride, get_num_urls, get_georange, get_variables, get_archive); 
      get_num_urls = n;
    end
    if size(get_vars) ~= size(old_var) | (get_dset ~= old_dset) | ...
          any(any(get_ranges ~= old_ranges))
      [x,y,z,t,n,URL] = getwvs('cat',get_ranges, get_dset, get_vars, ...
          get_dset_stride, get_num_urls, get_georange, get_variables, get_archive);
      get_num_urls = n;
    end

    if get_num_urls > 0
      %  The browser always sets the longitude range to go from -180 to 180.  Need
      %  to find out how the data set specifies the longitude range: -180 to 180 or
      %  0 to 360 and set up start and end longitudes accordingly. If the range
      %  selected in the browser is between 0 and 180 it does not matter.

      %  add new lat/lon code here
     
      if strcmp(mode, 'datasize')

        %Eval total size of data requested. If larger than check_level, warning is issued.
        %[datasize, nurls]=getxxx('datasize')
	argout2 = get_num_urls;
        argout1 = max((get_ranges(2,2)-get_ranges(2,1)),...
                      (get_ranges(1,2)-get_ranges(1,1)))...
                  /0.003*2*8/1e6;

        return
	
      else
 
        %Most The followings are added from getrectg.m.
        %get selected variables and match those to variablelist (DodsName) 
        %and serverlist (Server)
        whichvariables = [];
        %Replace DodsName SelectableVariables if umbrella is used.  R. Platner 06/06/99
        if strcmp(deblank(DodsName),'Umbrella') == 1
          DodsName = UmbrellaDods;
          SelectableVariables = UmbrellaVariables;
          get_variables = UmbrellaVariables;
        end
        for i = 1:size(get_variables,1)
	  for j = 1:size(SelectableVariables,1)
	    if strcmp(deblank(get_variables(i,:)), deblank(SelectableVariables(j,:)))
	      whichvariables = [whichvariables j];
	    end
	  end
        end
        if ~isempty(whichvariables)
	  variablelist = DodsName(whichvariables,:); 
	  % take into account that there may be mutliple servers
	  if size(Server,1) > 1
	    serverlist = Server(whichvariables,:); 
	  else
	    serverlist = Server; 
	  end
        else
	  dodsmsg('Getwvs: No variables were selected!')
        end


        % construct and evaluate URL(s)
        VERSION = version; VERSION = str2num(VERSION(1));
        tmpvariablelist = []; tmpserverlist = []; tmpvarlist = []; tmpserlist = [];
        if size(serverlist,1) == 1
          tmpvariablelist = str2mat(TimeName,LongitudeName,LatitudeName, ...
	    DepthName,variablelist);
          for i = 1:size(tmpvariablelist,1)
              if abs(tmpvariablelist(i,1)) ~= 32
                  tmpvarlist = [tmpvarlist; tmpvariablelist(i,:)];
              end
          end
	  tmpwhichvariables = [1 2 3 4 whichvariables+4];
          tmpserverlist = str2mat(TimeName,LongitudeName,LatitudeName, ...
	    DepthName, serverlist);
	  % there will be only one URL.  Initialize it.
	  iURL = '';
          % return the URL to be displayed by the browser. D.B. 98/4/5
	  eval([ '[iURL,loaddodsbool,infotext] =', URL_m_File, '(get_archive, get_ranges, mode,', ...
		 'tmpvarlist, serverlist, Pass_Times, Pass_Depths, whichurl);'])
	  if ~isempty(iURL)
	    argout5 = iURL;
	    loaddods('-F',iURL);
	  end
	else
          % nothing to be done here for wvs data set.
        end

        % Do the custom calculation here
        tmplon = eval(LongitudeName);
        if tmplon(1) > 1e-10 | tmplon(1) == -9999
          rindex = find(lat >= get_ranges(2,1) & lon >= get_ranges(1,1));
          lat1 = lat(rindex);   lon1 = lon(rindex);
          bindex = find(lat == -9999 & lon == -9999);
          [tmplat, tmplon] = wvstest(rindex, bindex, lat1, lon1);
          lat = tmplat;
          lon = tmplon;
        end

        % Check for Longitude convention used in browser, need to convert lon < 0
	% to lon+360 when get_georange(1) == 0
        % Adopted from old version of getrectg, here multiple lonname is allowed. klee 07/07/99
        TempLongitude = [];
        if ~isempty(deblank(LongitudeName(1,:)))
          if exist(deblank(LongitudeName(1,:))) == 1
            lonsize = 0;
            lonsize = size(LongitudeName,1);
            str1 = sprintf('%s%s%s','(TempLongitude >=180).*(TempLongitude-360)+', ...
		'(TempLongitude < -180).*(TempLongitude+360)+', ...
	        '(TempLongitude >= -180 & TempLongitude < 180).*TempLongitude');
            str2 = sprintf('%s%s%s','(TempLongitude >= 360).*(TempLongitude-360)+', ...
		'(TempLongitude < 0).*(TempLongitude+360)+', ...
		'(TempLongitude >= 0 & TempLongitude < 360).*TempLongitude');
            if lonsize == 1
              TempLongitude = eval(deblank(LongitudeName));
              if abs(get_ranges(1,1) - get_ranges(1,2)) < 360
                if get_georange(1) == 0
                  eval(['TempLongitude =', str1, ';'])
                elseif get_georange(1) == 0
                  eval(['TempLongitude =', str2, ';'])
                end
              end  
              eval([LongitudeName '= TempLongitude; clear TempLongitude'])
            elseif lonsize > 1
              for i = 1:lonsize
                TempLongitude = eval(deblank(LongitudeName(i,:)));
                if abs(get_ranges(1,1) - get_ranges(1,2)) < 360
                  if get_georange(1) == -180
                    eval(['TempLongitude =', str1, ';'])
                  elseif get_georange(1) == 0
                    eval(['TempLongitude =', str2, ';'])
                  end
                end
                eval([LongitudeName(i,:) '= TempLongitude; clear TempLongitude;'])
              end  % end of for loop
            end  % end of if lonsize == 1, elseif lonsize > 1
          end  % end of exist
        end  % end of ~isempty

        % now make up a list of all returned variables
	% (above list plus time, longitude, latitude, depth),
	% evaluate their sizes, scale them and convert nulls to NaNs.
	% this has to be a temporary variable list -- 'variablelist'
	% is still in use
	tmpvariablelist = str2mat(TimeName,LongitudeName,LatitudeName, ...
	    DepthName, variablelist);       
	tmpwhichvariables = [1 2 3 4 whichvariables+4];

	for i = 1:size(tmpvariablelist,1)
	  tmpname = deblank(tmpvariablelist(i,:));
	  if VERSION < 5 % fix v4 bug
	    if size(tmpname,2) > 19
	      tmpname = tmpname(1:19);
	    end
	  end
	  if exist(tmpname) == 1
            tmpout = eval(tmpname);
	    % make a temporary variable to work with
	    sizes(i,:) = size(tmpout);
	    if exist('DataNull') == 1
	      if ~isnan(DataNull(tmpwhichvariables(i)))
		j = find(tmpout == DataNull(tmpwhichvariables(i)));
		tmpout(j) = NaN*j;
	      else
		% do nothing because null values are already NaNs
	      end
	    else
	      % if we have no defined null, do nothing
	      % we used to do this!: (bad, bad)
	      % j = find(tmpout < -1.0e+33);
	      % tmpout(j) = NaN*j;
	    end
	    % stack data in a column and scale it if possible
	    if exist('DataScale') == 1
              if size(DataScale,1) ~= size(DodsName,1)+4
                dodsmsg(['Error is in ' get_archive '.m'])
	      	%argout1 = [argout1; tmpout(:)];
              else
		if ~isnan(DataScale(tmpwhichvariables(i),1))
		  argout1 = [argout1; DataScale(tmpwhichvariables(i),1) + ...
                	  tmpout(:)*DataScale(tmpwhichvariables(i),2)];
	   	else % scale exists but is a nan -- don't scale
		  argout1 = [argout1; tmpout(:)];
	        end
              end
	    else % there is no DataScale
	      argout1 = [argout1; tmpout(:)];
	    end
	  else % the supposedly returned variable DOES NOT EXIST 
	    % in this workspace.  Put in an empty placeholder.
	    if ~isempty(tmpname)
	      eval([tmpname '= [];'])
	    end
	    sizes(i,:) = [0 0];
	  end
	end

	% now convert variablelist to user-friendly names
        names = str2mat(TimeName, LongitudeName, LatitudeName, DepthName, ...
                SelectableVariables(whichvariables,:));
        URL = argout5;

        argout2 = sizes;
        argout3 = names;
        argout4 = tmpwhichvariables;
        argout5 = URL;
	
	return
      end % /* end of strcmp(mode, 'datasize') */
    end % /* end of if num_urls > 0 */
  end % /* end of strcmp(mode, 'cat') */
end % /* end of if nargin >= 1 */