function [asqSeries, abcache] = calibsimfd(IFO,gpsSec,strainSeries,fs,varargin)
% CALIBSIMFD - converts time-series in strain -> AS-Q counts
%       -- Uses Frequency-Domain conversion
%   asqSeries = calibsimfd(IFO,gpsSec,strainSeries,fs)
%   IFO     - IFO name ('H1','H2','L1')
%   gpsSec - GPS time of sim (whole seconds)
%   strainSeries - time-series vector in strain amplitude
%   fs    - time interval of time-series elements
%   varargin - holder for filter cache
%   
%   asqSeries - output time-series vector in AS-Q counts
%
%   [asqSeries,abcache] = calibsim_fd(abcache)
%       - abcache is cached Alpha-Beta constants
%
% NEEDS: read_alphabetafile, h2asq_fd

% To run, open the OS's .cshrc file and add the following:
% setenv LIGO_CALIB_DATA xxxx
% where xxx is the directory containing the calibration files

% $Id: calibsimfd.m,v 1.4 2004/05/18 21:19:42 stuver Exp $
inOk = nargchk(nargin,4,5);
outOk = nargchk(nargout,1,2);
if(~inOk | ~outOk)
    error ('syntax is [asqSeries[,abcache]] = calibsim_fd(IFO,gpsSec,strainSeries,fs[,abcache])');
    return
end
if(nargin > 4)
    if length(varargin) > 1
        error('varargin only designed for abcache')
    else
        abcache=varargin{1};
    end
else
    abcache=[];
end

% SET file path to LIGO_CALIB_DATA value
% IF calibration directory not defined yet
%   IF calib data environment variable is defined
%       SET calibration directory to calib data environment variable
%   ELSE
%       SET calibration directory to default path
%   ENDIF
% ENDIF
DEFAULT_CALIB_DIR = '/usr/center/raid1/s2/calibration';
persistent calDir;
persistent oldAbFile;
persistent oldOlgFile;
persistent oldSfFile;
if isempty(calDir)
    calDir = getenv('LIGO_CALIB_DATA');
    if isempty(calDir)
        calDir = DEFAULT_CALIB_DIR;
        fprintf(' LIGO_CALIB_DATA not defined - use %s\n',calDir);
    end    
end

time=find(abcache >= gpsSec-60 & abcache <= gpsSec-1); % search abcache for filter coefficents
if ~isempty(time) % if the coefficents are cached, use them
    alpha=abcache(time,2);
    beta=abcache(time,3);
else % if the coefficients are not cached
%   SET alpha, beta file names for IFO
%   IF file path is not empty
%       ADD file path to file name
%   ENDIF
%   IF file does not exist
%       EXIT with error
%   ELSE
%       IF alpha-beta file is new
%           WRITE message to logging
%       ENDIF
%   ENDIF
%   GET alpha, beta values for this GPS time
    nd=size(abcache,1)+1;
    abFile = strcat(IFO,'AlphaBeta.txt');
    if ~isempty(calDir)
        abFile = strcat(calDir,'/',abFile);
    end
    if exist(abFile,'file') == 0
        msg = sprintf(' Oops - AlphaBeta file %s not found!\n',abFile);
        error(msg);            
    else
        oldAbFile = checkfilechange(oldAbFile,abFile,'ALPHA-BETA');
    end    
    duration = 0.5; % read the filter coefficients from file:
    [gpsStartMin,alphaRay,betaRay] = read_alphabetafile(abFile,gpsSec,duration);
    abcache(nd,1)=gpsStartMin(1); % add this time to the cache
    alpha = alphaRay(1);
    if alpha==0
        alpha=1;
    end
    beta = betaRay(1);
    if beta==0
        beta=1;
    end
    abcache(nd,2)=alpha; % add the newly read coefficents to the cache
    abcache(nd,3)=beta;
end
% GET gain, response file names for IFO
% IF file path is not empty
%    ADD file path to file names
% END
% IF gain, response files are new
%   WRITE message to logging
% ENDIF
if(IFO == 'H1')
    olgFile = 'H1_olg_734073939.txt';
    sfFile = 'H1_sf_734073939.txt';
else 
    if( IFO == 'H2')
        if(gpsSec < 731849043)
            olgFile = 'H2_olg_734234126.txt';
            sfFile = 'H2_sf_734234126.txt';
        else
            olgFile = 'H2_olg_734234127.txt';
            sfFile = 'H2_sf_734234127.txt';
        end    
    else 
        if( IFO == 'L1')
            olgFile = 'H2_olg_734234126.txt';
            sfFile = 'H2_sf_734234126.txt';
        end
    end
end
if ~isempty(calDir)
    olgFile = strcat(calDir,'/',olgFile);
    sfFile = strcat(calDir,'/',sfFile);
end
oldOlgFile = checkfilechange(oldOlgFile,olgFile,'OPEN-LOOP GAIN');
oldSfFile = checkfilechange(oldSfFile,sfFile,'SENSING FUNCTION');
% ADD zero-padding to input series (pad on both ends)
% CONVERT input series to AS-Q counts
% REMOVE zero-padding
nsamp = length(strainSeries);
padSeries = zeros([2*nsamp,1]);
nstart  = floor(nsamp/2);
nend = nstart + (nsamp-1);
padSeries(nstart:nend) = strainSeries(1:nsamp);
outSeries = h2asq_fd(olgFile,sfFile,alpha,beta,padSeries,fs);
asqSeries = outSeries(nstart:nend);
return

function [oldFile] = checkfilechange(oldFile,newFile,fileType)
% CHECKFILECHANGE - write message if file changes

% IF old file is defined
%   IF new file not the same as old file
%       WRITE message about new file
%       SET old file = new file
%   ENDIF
% ELSE
%   WRITE message about new file
%   SET old file = new file
% ENDIF
if ~isempty(oldFile)
    if newFile ~= oldFile
        fprintf(' %s file: %s\n',fileType,newFile);
        oldFile = newFile;
    end
else
    fprintf(' %s file: %s\n',fileType,newFile);
    oldFile = newFile;
end
return
