function out=getgcm(lon,lat,dpt,tme,fld,flag)

% Function out=getgcm(lon,lat,dpt,tme,fld,flag)
% get MIT GCM time series
%
% INPUTS
% lon   Nx*Ny*Nz matrix of desired longitudes (deg E)
% lat   Nx*Ny*Nz matrix of desired latitudes (deg N)
% dpt   Nx*Ny*Nz matrix of desired depths (m)
% tme   Nt vector of times at which GCM output is desired
%         tme is in matlab datenum format, i.e., days from 1-Jan-0000
%
% fld   model field to get (default Tave)
%         H      : sea surface height from snapshots (m)
%         S      : salinity (PSU)
%         T      : temperature (deg C)
%         U      : eastward zonal velocity (m/s)
%         V      : northward  meridional velocity(m/s)
%         W      : upward vertical velocity (m/s)
%         Have   : sea surface height from time-averages (m)
%
% flag  refers to various GCM outputs and locations (default 0)
%         0: 1980-2000 c20000630 integration
%
% OUTPUT
% out   Nx*Ny*Nz*Nt output array
%
% SEE ALSO
% readgcm

% check input and set defaults
if nargin < 4, error('missing input argument'); end
if any(size(lon)~=size(lat)), error('lon-lat mismatch'); end
if any(size(lon)~=size(dpt)), error('lon-dpt mismatch'); end
if nargin <5, fld='T'; end
if nargin <6, flag=0; end

% temporary restrictions
if ~any(fld==['H' 'S' 'T' 'U' 'V' 'W'])
  error('feature not yet supported');
end

switch fld
  case {'H' 'S' 'T' 'U' 'V' 'W'}
    pref = [fld 'ave'];
end

switch flag
  
  case 0                             % 1980-2000 c20000630 integration
    
    switch fld                       % check tme range
      case 'H'
        if min(tme) < datenum(1980,1,1,1,0,0) | ...
              max(tme) > datenum(2000,1,270)
          error('tme out of range')
        end
      otherwise
        if min(tme) < datenum(1980,1,5) | ...
              max(tme) > datenum(2000,1,265)
          error('tme out of range')
        end
    end
    
    % initialize output array
    [Nx Ny Nz]=size(lon);
    Nt=length(tme);
    out=nan*ones(Nx,Ny,Nz,Nt);
  
    % determine size of domain that needs to be read in
    [mlon,mlat,mdpt]=getgrid(fld);
    Ix=max(find(mlon<=(min(lon)))):min(find(mlon>=(max(lon))));
    Iy=max(find(mlat<=(min(lat)))):min(find(mlat>=(max(lat))));
    Iz=max(find(mdpt<=(min(dpt)))):min(find(mdpt>=(max(dpt))));
    
    % initialize some more variables
    pn='/escher17/medea/odap/c20000630/y';             % path name
    mx=length(mlon); my=length(mlat); mz=length(mdpt); % model dimensions
    if fld=='H', mz=1; end
    [LAT   LON  DPT] = meshgrid(lat     , lon    , dpt    );
    [MLAT MLON MDPT] = meshgrid(mlat(Iy),mlon(Ix),mdpt(Iz));

    % indices and temporary arrays are used to avoid
    % reading fields that are already in memory
    out_old=nan*ones(Nx,Ny,Nz,2);
    tme_old=nan*ones(2,1);
    
    for t=1:Nt                       % loop over all time
      
      yr=str2num(datestr(tme(t),10)); % year
      dy=tme(t)-datenum(yr,1,1);      % day
      
      if dy>0 & mod(dy-5,10) == 0    % no time-interpolation required
        
        if any(tme_old) == tme(t)    % output array already in memory
          
          out(:,:,:,t) = out_old(:,:,:,find(tme_old==tme));
          
        else                         % need to read in array
          
          % get new field
          [fn tx]      = get_fn(pn,pref,yr,dy);
          tmp          = readgcm(fn,tx,mx,my,mz,Ix,Iy,Iz);
          out(:,:,:,t) = interp3(MLAT,MLON,MDPT,tmp,lat,lon,dpt);
          
          % replace oldest array in memory
          [Y I]            = min(tme_old);
          out_old(:,:,:,I) = out(:,:,:,t);
          tme_old(I)       = tme(t);
          
        end
        
      else                           % time-interpolation required

        [fn1,tx1,fn2,tx2,tme1,tme2] = get_fn(pn,pref,yr,dy);
        
        I1 = find(tme_old==tme1);    % index of tme1 array
        I2 = find(tme_old==tme2);    % index of tme2 array
        
        read1=0;
        read2=0;
        if isempty(I1), read1=1; end
        if isempty(I2), read2=1; end
        if isempty(I1) & isempty(I2)
          I1=1;
          I2=2;
        elseif isempty(I1)
          I1=1;
          if I2==1, I1=2; end
        elseif isempty(I2)
          I2=1;
          if I1==1, I2=2; end
        end

        if read1                     % get tme1 array if needed
          tmp = readgcm(fn1,tx1,mx,my,mz,Ix,Iy,Iz);
          out_old(:,:,:,I1) = interp3(MLAT,MLON,MDPT,tmp,lat,lon,dpt);
          tme_old(I1) = tme1;
        end
        
        if read2                     % get tme2 array if needed
          tmp = readgcm(fn2,tx2,mx,my,mz,Ix,Iy,Iz);
          out_old(:,:,:,I2) = interp3(MLAT,MLON,MDPT,tmp,lat,lon,dpt);
          tme_old(I2) = tme2;
        end

        % time-interpolate
        a1 = (tme2-tme(t)) / (tme2-tme1);
        a2 = (tme(t)-tme1) / (tme2-tme1);
        out(:,:,:,t) = a1*out_old(:,:,:,I1) + a2*out_old(:,:,:,I2);
       
      end
      
    end                              % loop over all time
    
  otherwise
    
    error('feature not yet supported');
    
end
