function indir_dyFC_4D(indir_FunImg,outfodr_DyFC,infile_Mask,Parameter)
%-----------------------------------------------------------
%   Written by SUN Jia-Wei 200413.
%   Mail to Authors: jiaweisun0512@163.com
%
%   Copyright(c) 2015
%	Center for Cognition and Brain Disorders, Hangzhou Normal University, Hangzhou 310015, China
%	Written by JIA Xi-Ze 201412
%	http://www.restfmri.net/
% 	Mail to Authors: jxz.rest@gmail.com, jiaxize@foxmail.com
%   Modified by Li Zi-Qi and Jia Xi-Ze 191113
%   Shirui technology co., LTD
%   Center for Cognition and Brain Disorders, Hangzhou Normal University, Hangzhou 310015, China




inpath_Misc(outfodr_DyFC,'MakeCurrentDir');
SubfodrList=dir_NameList(indir_FunImg);
[FunDir,FunName] = fileparts(indir_FunImg);
TDAPath = [outfodr_DyFC filesep 'TemporalDynamics' filesep 'TDA_4D' filesep 'FC_' FunName];
TDAMPath = [outfodr_DyFC filesep 'TemporalDynamics' filesep 'TDA_Metrics' filesep 'FC_' FunName];
inpath_Misc(TDAPath,'MakeCurrentDir');
inpath_Misc(TDAMPath,'MakeCurrentDir');

% if matlabpool('size')>0 
if ~isempty(gcp('nocreate'))
    parfor i=1:length(SubfodrList)
       [FCBrain_AllWindow, zFCBrain_AllWindow, Header] = infodr_dyFC_4D([indir_FunImg filesep SubfodrList{i}],...
                     [TDAPath filesep 'FC_' SubfodrList{i} '.nii'],...
                     infile_Mask,Parameter);
        for iROI=1:size(zFCBrain_AllWindow,5)
            MeanOut = [TDAMPath filesep 'MeanzFC_' 'ROI' num2str(iROI) filesep 'MeanzFC_' 'ROI' num2str(iROI) '_' SubfodrList{i} '.nii'];
            StdOut = [TDAMPath filesep 'StdzFC_' 'ROI' num2str(iROI) filesep 'StdzFC_' 'ROI' num2str(iROI) '_' SubfodrList{i} '.nii'];
            CVOut = [TDAMPath filesep 'CVzFC_' 'ROI' num2str(iROI) filesep 'CVzFC_' 'ROI' num2str(iROI) '_' SubfodrList{i} '.nii'];
            tda_GetTDAMetrics(zFCBrain_AllWindow(:,:,:,:,iROI),Header, MeanOut, StdOut, CVOut);
        end
    end

else
    for i=1:length(SubfodrList)
        [FCBrain_AllWindow, zFCBrain_AllWindow, Header] = infodr_dyFC_4D([indir_FunImg filesep SubfodrList{i}],...
                     [TDAPath filesep 'FC_' SubfodrList{i} '.nii'],...
                     infile_Mask,Parameter);
        for iROI=1:size(zFCBrain_AllWindow,5)
            MeanOut = [TDAMPath filesep 'MeanzFC_' 'ROI' num2str(iROI) filesep 'MeanzFC_' 'ROI' num2str(iROI) '_' SubfodrList{i} '.nii'];
            StdOut = [TDAMPath filesep 'StdzFC_' 'ROI' num2str(iROI) filesep 'StdzFC_' 'ROI' num2str(iROI) '_' SubfodrList{i} '.nii'];
            CVOut = [TDAMPath filesep 'CVzFC_' 'ROI' num2str(iROI) filesep 'CVzFC_' 'ROI' num2str(iROI) '_' SubfodrList{i} '.nii'];
            tda_GetTDAMetrics(zFCBrain_AllWindow(:,:,:,:,iROI),Header, MeanOut, StdOut, CVOut);
        end
    end
end



end