function [sigSeries] = calibsimfd(IFO,gpsSec,strainSeries,fs,sigType)
% CALIBSIMFD - converts time-series in strain -> gw channel counts
%       -- Uses Frequency-Domain conversion
%   sigSeries = calibsimfd(IFO,gpsSec,strainSeries,fs[,sigType])
%   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
%   sigType [OPTIONAL] signal type
%               'AS_Q'  original AS_Q
%               'DARM_ERR'  DARM_ERR signal from S4
%               <default = AS_Q>
%
%   sigSeries - output time-series vector in signal channel counts
%
% $Id: calibsimfd.m,v 1.16 2005/04/13 00:50:39 thorne Exp $

% NEEDS: 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

inOk = nargchk(4,5,nargin);
outOk = nargchk(1,1,nargout);
if(~(isempty(inOk) && isempty(outOk)))
    error ('syntax is [sigSeries] = calibsim_fd(IFO,gpsSec,strainSeries,fs[,sigType])');
end

% CHECK for valid IFO (LIGO for now)
% IF IFO has no calibration 
%   CLEAR output time series
%   RETURN
% ENDIF
testIFO = upper(IFO);
validIfoFlag = false;
switch testIFO
    case 'H1'
        validIfoFlag = true;
    case 'H2'
        validIfoFlag = true;
    case 'L1'
        validIfoFlag = true;
    otherwise
        validIfoFlag = false;
end
if(validIfoFlag == false)
    sigSeries = [];
    return
end

% IF signal type was input
%   CHECK for a valid type
%   IF invalid 
%       CREATE error message
%   ENDIF
% ELSE
%   SET to default type
% ENDIF
DEF_SIG = 'AS_Q';
if(nargin > 4)
    if( (strcmp(sigType,'AS_Q')==false) && ...
        (strcmp(sigType,'DARM_ERR') == false))
        msgId = 'calibsim_fd:badSignal';
        warning(msgId,'%s: signal type %s is not valid - assume AS_Q',msgId,sigType);
        sigType = DEF_SIG;
    end
else
    sigType = DEF_SIG;
end

% 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(IFO,gpsSec,padSeries,fs,sigType);
sigSeries = outSeries(nstart:nend);
return
