function [URL] = dinourl(archive, Times, Depths, dset_stride, ...
                            ranges, variable, server)
%
% This function will build the DINO MODEL file name.
%

% The preceding empty line is important.

if exist(archive) == 2
  eval(archive)
else
  disp('Metadata not found!')
  return
end

Server = ['http://rhody.gso.uri.edu/cgi-bin/nph-nc/data/dino/' deblank(server)];

tmpvariable = [];
for i = 1:size(variable,1)
  tmpvariable = [tmpvariable; deblank(variable(i,:))];
end

 % reduce size of the original lat/lon arrays into 13 by 7, hard-coded to avoided network transfer
 LatArray = [41.6048   41.6110   41.6220   41.6377   41.6590   41.6885   41.7270
             41.9056   41.9287   41.9440   41.9572   41.9710   41.9883   42.0115
             42.1985   42.2062   42.2107   42.2140   42.2171   42.2211   42.2287
             42.4765   42.4655   42.4563   42.4473   42.4375   42.4267   42.4171
             42.7397   42.7103   42.6874   42.6660   42.6429   42.6165   42.5889
             42.9848   42.9398   42.9047   42.8718   42.8363   42.7950   42.7494
             43.2092   43.1522   43.1073   43.0649   43.0186   42.9638   42.9012
             43.4124   43.3494   43.2989   43.2500   43.1954   43.1288   43.0496
             43.5944   43.5325   43.4797   43.4266   43.3656   43.2893   43.1952
             43.7578   43.6862   43.6274   43.5692   43.5026   43.4192   43.3165
             43.9035   43.8269   43.7640   43.7019   43.6310   43.5421   43.4332
             44.0352   43.9584   43.8941   43.8299   43.7558   43.6625   43.5478
             44.1590   44.0809   44.0151   43.9490   43.8725   43.7753   43.6553];
 NNlat = 13;

 LonArray = [-70.9534  -70.8321  -70.7014  -70.5649  -70.4043  -70.1997  -69.9456
             -71.0694  -70.9050  -70.7672  -70.6284  -70.4673  -70.2631  -70.0108
             -71.1249  -70.9459  -70.7995  -70.6553  -70.4913  -70.2871  -70.0393
             -71.1234  -70.9406  -70.7924  -70.6477  -70.4843  -70.2831  -70.0419
             -71.0694  -70.8918  -70.7477  -70.6069  -70.4482  -70.2532  -70.0210
             -70.9696  -70.8039  -70.6681  -70.5348  -70.3839  -70.1980  -69.9765
             -70.8335  -70.6829  -70.5581  -70.4343  -70.2930  -70.1178  -69.9081
             -70.6713  -70.5324  -70.4164  -70.3008  -70.1686  -70.0044  -69.8083
             -70.4932  -70.3582  -70.2479  -70.1390  -70.0150  -69.8609  -69.6781
             -70.3054  -70.1906  -70.0931  -69.9948  -69.8808  -69.7372  -69.5623
             -70.1157  -70.0141  -69.9264  -69.8368  -69.7316  -69.5972  -69.4293
             -69.9260  -69.8294  -69.7470  -69.6630  -69.5646  -69.4381  -69.2793
             -69.7321  -69.6402  -69.5624  -69.4833  -69.3907  -69.2719  -69.1237];
  NNlon = 7;

  %maxlon = max(LonArray(find(LonArray >= ranges(1,1) & LonArray <= ranges(1,2) & ...
  %                           LatArray >= ranges(2,1) & LatArray <= ranges(2,2))));
  maxlon = max(LonArray(find(LonArray >= ranges(1,1) & LonArray <= ranges(1,2))));
  [imaxlonu imaxlonv] = find(LonArray == maxlon);
  %minlon = min(LonArray(find(LonArray >= ranges(1,1) & LonArray <= ranges(1,2) & ...
  %                           LatArray >= ranges(2,1) & LatArray <= ranges(2,2))));
  minlon = min(LonArray(find(LonArray >= ranges(1,1) & LonArray <= ranges(1,2))));
  [iminlonu iminlonv] = find(LonArray == minlon);
  %maxlat = max(LatArray(find(LatArray >= ranges(2,1) & LatArray <= ranges(2,2) & ...
  %                           LonArray >= ranges(1,1) & LonArray <= ranges(1,2))));
  maxlat = max(LatArray(find(LatArray >= ranges(2,1) & LatArray <= ranges(2,2))));
  [imaxlatu imaxlatv] = find(LatArray == maxlat);
  %minlat = min(LatArray(find(LatArray >= ranges(2,1) & LatArray <= ranges(2,2) & ...
  %                           LonArray >= ranges(1,1) & LonArray <= ranges(1,2))));
  minlat = min(LatArray(find(LatArray >= ranges(2,1) & LatArray <= ranges(2,2))));
  [iminlatu iminlatv] = find(LatArray == minlat);


  % select WESN index
  % tmp_WESN are the absolute bounding limits within the box
  % tmp_WESN_else are the row/column pairs of tmp_WESN
  % taking account the fact that if there're two points on the line intersecting the box ...
  if min(imaxlatv) < min(iminlatv)
    % if maxlat is on the west of minlat, take the northern one of the minlonu
    tmpwest =  min(iminlonv); 
    tmpwelse = max(iminlonu);
  else  
    tmpwest = max(iminlonv); 
    tmpwelse = min(iminlonu);
  end
  if tmpwest < 1, tmpwest = 1; end
  if max(imaxlatv) > max(iminlatv)
    % if maxlatv is on the east of minlatv, take the northern one of the maxlonu
    tmpeast =  min(imaxlonv); 
    tmpeelse = max(imaxlonu);
  else
    tmpeast = max(imaxlonv);
    tmpeelse = min(imaxlonu);
  end
  if tmpeast > NNlon, tmpeast = NNlon; end
  if max(iminlonu) > max(imaxlonu)
     % if minlonu is on the north of maxlonu, take the eastern part of minlatv
     tmpsouth = min(iminlatu); 
     tmpselse = min(iminlatv);
  else 
    tmpsouth = max(iminlatu);
    tmpselse = max(iminlatv);
  end
  if tmpsouth > NNlat, tmpsouth = NNlat; end
  if min(iminlonu) < min(imaxlonu)
    % if minlonu is on the south of maxlonu, take the eastern part of maxlatv
    tmpnorth = min(imaxlatu); 
    tmpnelse = min(imaxlatv);
  else
    tmpnorth = max(imaxlatu);
    tmpnelse = max(imaxlatv);
  end
  if tmpnorth < 1, tmpnorth = 1; end
  

  % Now, the tmp_WESN is defined by the pseudo Lon/LatArray.
  % Compare cols and rows to define WESN out-bounds in original array space. 
  % Users defined box (ranges) is within the bound.
  % deltadegree is the spacing (in degree) between two grids in Lon/LatArray (9 lines in between)
  % delta_WESN is the difference (in deltadegree) between user-defined boundaries and tmp_WESN

  deltadegree = 0;     

  % south first
  if tmpsouth > 1
    deltadegree = abs(LatArray(tmpsouth,tmpselse) - LatArray(tmpsouth-1,tmpselse)) / 10;
  end
  if tmpsouth > 1 & ranges(2,1) > min(LatRange)
    % if not on the west bound and user-defined box is within the LatRange, come here
    deltasouth = ceil(abs(ranges(2,1) - LatArray(tmpsouth,tmpselse)) / deltadegree);
  else
    % or on the west bound, or requesting box outbound the geographic range
    deltasouth = 0;
  end
  orowstart = (tmpsouth - 1) * 10 + 1;   % calculate back to original array space
  south = orowstart - deltasouth;       
 
  %then north
  if tmpnorth < NNlat
    deltadegree = abs(LatArray(tmpnorth,tmpnelse) - LatArray(tmpnorth+1,tmpnelse)) / 10;
%  else
    % if tmpnorth is in between the east bound of LatArray and the east bound of LatRange
%    deltadegree = abs(LatArray(tmpnorth,tmpnelse) - LatArray(tmpnorth-1,tmpnelse)) / 10;
  end
  if tmpnorth <= NNlat & ranges(2,2) < max(LatRange)
    deltanorth = ceil(abs(ranges(2,2) - LatArray(tmpnorth,tmpnelse)) / deltadegree);
  else
    deltanorth = 9;
  end
  orowend = (tmpnorth - 1) * 10 + 1;
  north = orowend + deltanorth;

  % west/east
  if tmpwest > 1
    deltadegree = abs(LonArray(tmpwelse,tmpwest) - LonArray(tmpwelse,tmpwest-1)) / 10;
  end
  if tmpwest > 1 & ranges(1,1) > min(LonRange)
    deltawest = ceil(abs(ranges(1,1) - LonArray(tmpwelse,tmpwest)) / deltadegree);
  else
    deltawest = 0;
  end
  ocolstart = (tmpwest - 1) * 10 + 1;
  west = ocolstart - deltawest;
  
  if tmpeast < NNlon
    deltadegree = abs(LonArray(tmpeelse,tmpeast) - LonArray(tmpeelse,tmpeast+1)) / 10;
%  else 
%    deltadegree = abs(LonArray(tmpeelse,tmpeast) - LonArray(tmpeelse,tmpeast-1)) / 10;
  end
  if tmpeast <= NNlon & ranges(1,2) < max(LonRange)
    deltaeast = ceil(abs(ranges(1,2) - LonArray(tmpeelse,tmpeast)) / deltadegree);
  else
    deltaeast = 9;
  end
  ocolend = (tmpeast - 1) * 10 + 1;
  east = ocolend + deltaeast;
  

  % Convert WESN to start/end col/row 
  % ML row/col index starts from 1, dods server index starts from 0
  % (dset_stride is used to define how many grids in between)
  StartColumn = west - 1; EndColumn = east - 1;
  StartRow = south - 1; EndRow = north - 1; 

  % Now to request data.
  Constraint1 = [sprintf('%s','lat','[',num2str(StartRow),':', ...
        num2str(dset_stride),':',num2str(EndRow),'][', ... 
        num2str(StartColumn), ':', num2str(dset_stride), ':',...
        num2str(EndColumn), ']')];
  Constraint2 = [sprintf('%s','lon','[',num2str(StartRow),':', ...
        num2str(dset_stride),':',num2str(EndRow),'][', ... 
        num2str(StartColumn), ':', num2str(dset_stride), ':',...
        num2str(EndColumn), ']')];
  URL = sprintf('%s',Server,'?',Constraint1,',',Constraint2);

  for i = 1:size(tmpvariable,1)
    if strcmp(tmpvariable(i,:),'depth') 
      Constraint3 = [sprintf('%s',tmpvariable,'[',num2str(StartRow),':', ...
          num2str(dset_stride),':',num2str(EndRow),'][', ... 
          num2str(StartColumn), ':', num2str(dset_stride), ':',...
          num2str(EndColumn), ']')];
      URL = [URL, sprintf('%s',',',Constraint3);
    end
    if (strcmp(tmpvariable(i,:),'elev') | strcmp(tmpvariable(i,:),'heat_flux') ...
       | strcmp(tmpvariable(i,:),'cd'))
      Constraint3 = [sprintf('%s',tmpvariable,'[',num2str(Times),':', ...
          num2str(Times),'][', ...
          num2str(StartRow),':',num2str(dset_stride),':',num2str(EndRow),'][', ...
          num2str(StartColumn), ':', num2str(dset_stride), ':',...
          num2str(EndColumn), ']')];  
      if isempty(findstr(URL,'time'))
        Constraint4 = [sprintf('%s','time','[',num2str(Times),':',num2str(Times),']')];
      end
      URL = [URL, sprintf('%s',',',,Constraint3,',',Constraint4)];
    end
    if (strcmp(tmpvariable(i,:),'conc') | strcmp(tmpvariable(i,:),'salt') ...
       | strcmp(tmpvariable(i,:),'temp') | strcmp(tmpvariable(i,:),'u') ...
       | strcmp(tmpvariable(i,:),'v') | strcmp(tmpvariable(i,:),'w') ...
       | strcmp(tmpvariable(i,:),'km') | strcmp(tmpvariable(i,:),'am'))
      Constraint3 = [sprintf('%s',tmpvariable,'[',num2str(Times),':', ...
          num2str(Times),'][', ...
          num2str(Depths),':',num2str(Depths),'][', ...
          num2str(StartRow),':',num2str(dset_stride),':',num2str(EndRow),'][', ...
          num2str(StartColumn), ':', num2str(dset_stride), ':',...
          num2str(EndColumn), ']')];
      if isempty(findstr(URL,'time'))
        Constraint4 = [sprintf('%s','time','[',num2str(Times),':',num2str(Times),']')];
      end
      URL = [URL, sprintf('%s',',',Constraint3,',',Constraint4)];
    end
  end

return
