function [URL] = usgsmurl(archive, Times, Depths, dset_stride, ...
                            ranges, variable, server)
%
% This function will build the USGS Mass Bay Model file name.
%

% The preceding empty line is important.
% $Log: usgsmurl.m,v $
% Revision 1.1  1999/06/01 07:10:34  dbyrne
%
%
% Moved up from kleedata/
%
% Revision 1.2  1999/05/28 17:32:51  kwoklin
% Add all globec datasets. Fix depth representation for all globec datasets
% and nbneer dataset. Fix frontal display for htn and glkfront. Make use of
% getjgsta for all jgofs datasets. Point usgsmbay to new server. Point htn,
% glk, fth and prevu to new FF server.                                 klee
%

% $Id: usgsmurl.m,v 1.1 1999/06/01 07:10:34 dbyrne Exp $
% klee      05/05/99

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

Server = ['http://crusty.er.usgs.gov/cgi-bin/nph-nc/mbay/meanbb.cdf'];
tmpvariable = [variable];

% reduce original lat/lon arrays (68x68) size to 17 by 17, hard-coded to avoid network transfer
spacing = 4;
LatArray = [41.6543   41.6507   41.6487   41.6479   41.6485   41.6505   41.6541    
            41.7213   41.7290   41.7348   41.7405   41.7467   41.7534   41.7608
            41.7852   41.7948   41.8029   41.8111   41.8201   41.8295   41.8393
            41.8452   41.8564   41.8660   41.8761   41.8871   41.8986   41.9105
            41.9013   41.9142   41.9253   41.9371   41.9499   41.9634   41.9773
            41.9533   41.9680   41.9808   41.9943   42.0091   42.0245   42.0405
            42.0010   42.0177   42.0322   42.0475   42.0643   42.0818   42.1000
            42.0445   42.0633   42.0796   42.0968   42.1156   42.1353   42.1558
            42.0836   42.1045   42.1225   42.1416   42.1624   42.1843   42.2070
            42.1178   42.1406   42.1603   42.1810   42.2037   42.2275   42.2524
            42.1471   42.1716   42.1925   42.2146   42.2389   42.2645   42.2913
            42.1720   42.1976   42.2196   42.2428   42.2684   42.2954   42.3238
            42.1935   42.2198   42.2424   42.2664   42.2930   42.3212   42.3510
            42.2121   42.2386   42.2616   42.2861   42.3135   42.3427   42.3736
            42.2282   42.2544   42.2774   42.3024   42.3304   42.3603   42.3923
            42.2423   42.2675   42.2904   42.3156   42.3442   42.3749   42.4077
            42.2535   42.2780   42.3010   42.3266   42.3558   42.3872   42.4208];

Lat2 = [    41.6595   41.6669   41.6763   41.6876   41.7010   41.7166   41.7342
            41.7693   41.7790   41.7898   41.8019   41.8152   41.8297   41.8452
            41.8501   41.8617   41.8742   41.8875   41.9017   41.9167   41.9322
            41.9233   41.9369   41.9512   41.9662   41.9820   41.9983   42.0151
            41.9921   42.0079   42.0244   42.0415   42.0594   42.0780   42.0970
            42.0576   42.0757   42.0946   42.1143   42.1349   42.1564   42.1784
            42.1195   42.1402   42.1618   42.1845   42.2083   42.2332   42.2591
            42.1777   42.2012   42.2259   42.2518   42.2793   42.3084   42.3389
            42.2315   42.2578   42.2856   42.3151   42.3466   42.3803   42.4161
            42.2793   42.3084   42.3392   42.3723   42.4078   42.4463   42.4878
            42.3204   42.3520   42.3857   42.4220   42.4615   42.5046   42.5517
            42.3549   42.3886   42.4249   42.4641   42.5071   42.5544   42.6067
            42.3837   42.4193   42.4578   42.4996   42.5456   42.5966   42.6536
            42.4076   42.4449   42.4852   42.5292   42.5779   42.6321   42.6930
            42.4274   42.4661   42.5080   42.5538   42.6047   42.6615   42.7257
            42.4438   42.4837   42.5269   42.5743   42.6270   42.6860   42.7528
            42.4579   42.4987   42.5431   42.5919   42.6460   42.7068   42.7757];

Lat3 = [    41.7540   41.7765   41.8020
            41.8616   41.8786   41.8950
            41.9482   41.9645   41.9803
            42.0323   42.0497   42.0669
            42.1164   42.1362   42.1562
            42.2011   42.2245   42.2483
            42.2861   42.3143   42.3434
            42.3712   42.4055   42.4418
            42.4547   42.4966   42.5425
            42.5333   42.5841   42.6417
            42.6044   42.6645   42.7355
            42.6660   42.7353   42.8201
            42.7189   42.7965   42.8941
            42.7635   42.8481   42.9565
            42.8003   42.8907   43.0074
            42.8307   42.9254   43.0484
            42.8562   42.9543   43.0819];
LatArray = [LatArray, Lat2, Lat3];
NNlat = 17;

LonArray = [-70.5630  -70.5349  -70.5005  -70.4612  -70.4165  -70.3687  -70.3186
            -70.5890  -70.5479  -70.5124  -70.4746  -70.4327  -70.3887  -70.3429
            -70.6138  -70.5714  -70.5355  -70.4980  -70.4571  -70.4145  -70.3705
            -70.6404  -70.5988  -70.5635  -70.5268  -70.4869  -70.4455  -70.4027
            -70.6696  -70.6296  -70.5956  -70.5600  -70.5213  -70.4811  -70.4395
            -70.7020  -70.6639  -70.6314  -70.5973  -70.5600  -70.5211  -70.4807
            -70.7380  -70.7020  -70.6711  -70.6386  -70.6029  -70.5655  -70.5265
            -70.7777  -70.7440  -70.7149  -70.6840  -70.6501  -70.6144  -70.5770
            -70.8207  -70.7892  -70.7619  -70.7329  -70.7009  -70.6671  -70.6316
            -70.8651  -70.8355  -70.8099  -70.7828  -70.7528  -70.7211  -70.6878
            -70.9091  -70.8813  -70.8574  -70.8321  -70.8042  -70.7748  -70.7438
            -70.9511  -70.9250  -70.9027  -70.8793  -70.8535  -70.8264  -70.7979
            -70.9908  -70.9663  -70.9457  -70.9242  -70.9007  -70.8760  -70.8500
            -71.0280  -71.0052  -70.9864  -70.9670  -70.9458  -70.9237  -70.9004
            -71.0622  -71.0414  -71.0247  -71.0075  -70.9889  -70.9694  -70.9488
            -71.0931  -71.0750  -71.0608  -71.0462  -71.0303  -71.0135  -70.9957
            -71.1207  -71.1078  -71.0970  -71.0855  -71.0725  -71.0586  -71.0436];

Lon2 = [    -70.2648  -70.2079  -70.1491  -70.0882  -70.0252  -69.9604  -69.8940
            -70.2938  -70.2417  -70.1874  -70.1308  -70.0715  -70.0096  -69.9450
            -70.3234  -70.2733  -70.2210  -70.1661  -70.1081  -70.0469  -69.9820
            -70.3569  -70.3081  -70.2569  -70.2028  -70.1452  -70.0838  -70.0180
            -70.3948  -70.3470  -70.2966  -70.2430  -70.1855  -70.1236  -70.0566
            -70.4372  -70.3903  -70.3407  -70.2876  -70.2301  -70.1677  -70.0994
            -70.4842  -70.4386  -70.3899  -70.3374  -70.2803  -70.2176  -70.1483
            -70.5364  -70.4922  -70.4448  -70.3935  -70.3370  -70.2746  -70.2048
            -70.5929  -70.5506  -70.5050  -70.4553  -70.4003  -70.3388  -70.2695
            -70.6513  -70.6113  -70.5681  -70.5206  -70.4677  -70.4082  -70.3404
            -70.7098  -70.6725  -70.6319  -70.5873  -70.5373  -70.4807  -70.4157
            -70.7665  -70.7321  -70.6946  -70.6531  -70.6066  -70.5537  -70.4926
            -70.8215  -70.7901  -70.7558  -70.7179  -70.6752  -70.6265  -70.5702
            -70.8747  -70.8465  -70.8156  -70.7814  -70.7429  -70.6989  -70.6480
            -70.9261  -70.9011  -70.8737  -70.8433  -70.8090  -70.7699  -70.7247
            -70.9760  -70.9541  -70.9302  -70.9036  -70.8737  -70.8397  -70.8005
            -71.0270  -71.0085  -70.9882  -70.9657  -70.9404  -70.9117  -70.8788];

Lon3 = [    -69.8256  -69.7548  -69.6827
            -69.8768  -69.8033  -69.7215
            -69.9122  -69.8351  -69.7462
            -69.9462  -69.8655  -69.7708
            -69.9825  -69.8982  -69.7980
            -70.0231  -69.9352  -69.8294
            -70.0700  -69.9785  -69.8667
            -70.1249  -70.0303  -69.9122
            -70.1890  -70.0920  -69.9683
            -70.2607  -70.1631  -70.0357
            -70.3386  -70.2428  -70.1149
            -70.4196  -70.3281  -70.2042
            -70.5027  -70.4177  -70.3021
            -70.5869  -70.5102  -70.4061
            -70.6708  -70.6032  -70.5124
            -70.7539  -70.6960  -70.6193
            -70.8401  -70.7926  -70.7308];
LonArray = [LonArray, Lon2, Lon3];
NNlon = 17;

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))));
[iminlonu iminlonv] = find(LonArray == minlon);
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))));
[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 ...
tmpwest = min(iminlonv);
if tmpwest < 1, tmpwest = 1; end
tmpwelse = iminlonu(find(iminlonv == tmpwest),1);

tmpeast = max(imaxlonv);
if tmpeast > NNlon, tmpeast = NNlon; end
tmpeelse = imaxlonu(find(imaxlonv == tmpeast),1);

tmpsouth = min(iminlatu);
if tmpsouth < 1, tmpsouth = 1; end
tmpselse = iminlatv(find(iminlatu == tmpsouth),1);

tmpnorth = max(imaxlatu);
if tmpnorth > NNlat, tmpnorth = NNlat; end
tmpnelse = imaxlatv(find(imaxlatu == tmpnorth),1);

% 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 (3 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)) / spacing;
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) * spacing + 1;   % calculate back to original array space
south = orowstart - deltasouth;       
 
%then north
if tmpnorth < NNlat
  deltadegree = abs(LatArray(tmpnorth,tmpnelse) - LatArray(tmpnorth+1,tmpnelse)) / spacing;
end
if tmpnorth < NNlat & ranges(2,2) < max(LatRange)
  deltanorth = ceil(abs(ranges(2,2) - LatArray(tmpnorth,tmpnelse)) / deltadegree);
else
  deltanorth = spacing - 1;
end
orowend = (tmpnorth - 1) * spacing + 1;
north = orowend + deltanorth;

% west/east
if tmpwest > 1
  deltadegree = abs(LonArray(tmpwelse,tmpwest) - LonArray(tmpwelse,tmpwest-1)) / spacing;
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) * spacing + 1;
west = ocolstart - deltawest;
  
if tmpeast < NNlon
  deltadegree = abs(LonArray(tmpeelse,tmpeast) - LonArray(tmpeelse,tmpeast+1)) / spacing;
end
if tmpeast < NNlon & ranges(1,2) < max(LonRange)
  deltaeast = ceil(abs(ranges(1,2) - LonArray(tmpeelse,tmpeast)) / deltadegree);
else
  deltaeast = spacing - 1;
end
ocolend = (tmpeast - 1) * spacing + 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)
  tmpvar = deblank(tmpvariable(i,:));
  if strcmp(tmpvar,'depth') 
    Constraint3 = [sprintf('%s',tmpvar,'[',num2str(StartRow),':', ...
        num2str(dset_stride),':',num2str(EndRow),'][', ... 
        num2str(StartColumn), ':', num2str(dset_stride), ':',...
        num2str(EndColumn), ']')];
    URL = [URL, sprintf('%s',',',Constraint3)];
  end
  if (strcmp(tmpvar,'elev') | strcmp(tmpvar,'heat_flux') ...
     | strcmp(tmpvar,'cd'))
    Constraint3 = [sprintf('%s',tmpvar,'[',num2str(Times),':', ...
        num2str(Times),'][', ...
        num2str(StartRow),':',num2str(dset_stride),':',num2str(EndRow),'][', ...
        num2str(StartColumn), ':', num2str(dset_stride), ':',...
        num2str(EndColumn), ']')];  
    if isempty(findstr(URL,'time'))
      Constraint3 = [Constraint3, sprintf('%s', ',', 'time','[',num2str(Times),':',num2str(Times),']')];
    end
    URL = [URL, sprintf('%s',',',Constraint3)];
  end
  if (strcmp(tmpvar,'conc') | strcmp(tmpvar,'salt') ...
     | strcmp(tmpvar,'temp') | strcmp(tmpvar,'u') ...
     | strcmp(tmpvar,'v') | strcmp(tmpvar,'w') ...
     | strcmp(tmpvar,'km') | strcmp(tmpvar,'am'))
    Constraint3 = [sprintf('%s',tmpvar,'[',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'))
      Constraint3 = [Constraint3, sprintf('%s',',', 'time','[',num2str(Times),':',num2str(Times),']')];
    end
    URL = [URL, sprintf('%s',',',Constraint3)];
  end
end

return
