function [varargout] = nic_icatb_scaleICA(icasig, A, fmriFiles, scaleType, dataType, complexInfo, mask_ind)
% [scaledICASig scaledA]=scaleICA(icasig,A,origData)
% ---------------------------------------------------------------
% This function takes the timecourses and the images resulting from ICA. It also
% takes the original data that was Analyzed with ICA.
% The function returns a scaled timecourse and a scaled image.
% -----------------------------------------------------------------------
% -icasig = ica signal in 2D(number of components, x*y*z)
% -A = ica timecourse in 2d(number of components, number of timepoints)
% -origData = original dataata that icasig and A was obtained from.
% data can be either 2D(x*y*z,number of timepoints) or 4D(x,y,z,t)
% -scaleType lets you select how to scale the timecourse
% 1 means difference between max and min
% 2 means the top 10% and bottom 10%
% fmriFiles = functional files to load
% --------------------------------------------------------------------

% load defaults
% icatb_defaults;
% global WRITE_COMPLEX_IMAGES;
% global SCALE_DEFAULT;

% [modalityType, dataTitle] = icatb_get_modality;

% Calibrate Options
calibrateOptions = str2mat('No', 'Calibrate', 'Z-scores');

% Add one to the index
if ~exist('scaleType', 'var')
    scaleType = SCALE_DEFAULT;
end

scaleType = scaleType + 1;

if scaleType > size(calibrateOptions, 1)
    error(['Presently there are ', num2str(size(calibrateOptions, 1)), ' scaling methods only.']);
end

% calibrate Type
calibrateType = lower(deblank(calibrateOptions(scaleType, :)));

% Return the available calibration schemes if no input arguments are
% specified
if nargin == 0
    varargout{1} = calibrateOptions;
    return;
end

% Initialise output vars
scaledICASig = 0;
scaledA = 0;

if ~exist('dataType', 'var')
    dataType = 'real';
end

if ~exist('complexInfo', 'var')
    complexInfo = [];
end

if strcmpi(dataType, 'complex')
    % convert component images to complex data
    icasig = complex_data(icasig);
    % convert time courses to complex data
    A = complex_data(A);
end

if strcmpi(calibrateType, 'no')
    % Not calibrating
    A = A'; % Transposing A such that it is of dimension Timepoints by components (each column represents a time course)
    disp('Not scaling components');

elseif strcmpi(calibrateType, 'calibrate')
    % calibrating components to percent signal change

    %disp('--Loading Functional Data and Calibrating Components');
    disp(['--Loading  data and scaling components to data units']);

    % load original data for scaling timecourses and
    % components
    [origData, HInfo] = nic_rsdd_loadData(fmriFiles,mask_ind);
    
    %[origData, HInfo] = icatb_read_data(fmriFiles, [], mask_ind);

    % Adjust global variable to make components as complex_data type
    if strcmpi(dataType, 'complex')
        origWriteImages = WRITE_COMPLEX_IMAGES;
        WRITE_COMPLEX_IMAGES = complexInfo.complexType;
        % convert original data to complex data
        origData = complex_data(origData);
        % restore the original value for the WRITE_COMPLEX_IMAGES
        WRITE_COMPLEX_IMAGES = origWriteImages;
    end

%     %reshape origData to 2 dimensions (x*y*z, t)
%     if(size(origData, 4) > 1)
%         origData = reshape(origData, [size(origData,1)*size(origData,2)*size(origData,3),size(origData,4)]);
%     end
% 
%     % Masked out data
%     origData = origData(mask_ind, :);

    %force icasig to be components*voxels
    if size(icasig,1) > size(icasig,2)
        icasig = icasig';
    end

    %force timecourse to be components*voxels
    if size(A,1) > size(A,2)
        A=A';
    end
    numberVoxels = size(icasig,2);

%     if strcmpi(modalityType, 'fmri')
     [icasig, A] = icatb_percentSignalChange(origData, icasig, A);     
%     end

elseif strcmpi(calibrateType, 'z-scores')
    % calibrating to Z scores both the components and timecourses

    disp('Converting components to z-scores');

%     if strcmpi(modalityType, 'fmri')
        [A] = icatb_convertToZScores(A); % time course
        [icasig] = nic_icatb_convertImageToZScores(icasig); % spatial maps
%     else        
%         Loop over components
%         for n = 1:size(A, 1)
%             A(n, :) = A(n, :)./std(A(n, :));
%             icasig(n, :) = icasig(n, :)./std(icasig(n, :));
%         end
%         End loop over components
%     end

    A = A';

    % Add your calibrate code below

end
% end for scaling steps



scaledA = A; % scaled time course of dimension Time points by components
scaledICASig = icasig; % scaled component of dimension components by voxels

% return output
varargout{1} = scaledICASig;
varargout{2} = scaledA;


