function [Z P] = y_ptoZ(ImgFile,OutputName)
% FORMAT [Z P] = y_ptoZ(ImgFile,OutputName)
%   Input:
%     ImgFile - p Image which wanted to be converted to Z statistical value (one-tailed)
%     OutputName - The output name
%   Output:
%     Z - Z statistical image. Also output as OutputName.
%     P - The P value
%___________________________________________________________________________
% Written by YAN Chao-Gan 151023.
% Key Laboratory of Behavioral Science and Magnetic Resonance Imaging Research Center, Institute of Psychology, Chinese Academy of Sciences, Beijing, China
% ycg.yan@gmail.com

[Data VoxelSize Header]=y_ReadRPI(ImgFile);

[nDim1,nDim2,nDim3]=size(Data);

Z=zeros(nDim1,nDim2,nDim3);
P=ones(nDim1,nDim2,nDim3);
fprintf('\n\tp to Z Calculating...\n');
for i=1:nDim1
    fprintf('.');
    for j=1:nDim2
        for k=1:nDim3
            if Data(i,j,k)~=0 && Data(i,j,k)~=1;
                    PTemp=Data(i,j,k);
                    ZTemp=norminv(1 - PTemp); % Doing one tailed!!!
                    %ZTemp=norminv(1 - PTemp/2)*sign(F);
                    P(i,j,k)=PTemp;
                    Z(i,j,k)=ZTemp;
            end

        end
    end
end
Z(isnan(Z))=0;
P(isnan(P))=1;

Header.descrip=sprintf('{Z_[%.1f]}',1);
Header.pinfo = [1;0;0];
Header.dt    =[16,0];
y_Write(Z,Header,OutputName);

fprintf('\n\tp to Z Calculation finished.\n');