
     Subroutine Calc_EOFs_for_Fcs_DL (Fname)
        Character (Len=*) :: Fname
        Real, Allocatable :: lon(:),lat(:),lev(:),xtime(:),xtmp(:)
        Real, Allocatable :: Xpre(:), Fpre(:), A(:,:), x11(:), Tmp
        Integer :: nlon,nlat,nz,ntime
        Integer :: ilt,imon,it,imod

        print*,"Begining Calc_EOFs_for_Fcs_DL"
        Call rd_ncdf_4d (Fname,lon,lat,lev,xtime,nlon,nlat,nz,ntime,PCall,"pc")
        Nmode=ntime
        Deallocate (lon,lat,lev,xtime)
        Call rd_ncdf_3d (Fname,lon,lat,lev,nlon,nlat,nz,Eigall,"eig")
        Deallocate (lon,lat,lev)
        Call rd_ncdf_3d (Fname,lon,lat,lev,nlon,nlat,nz,xtball,"mean")
        Deallocate (lon,lat,lev)
        Call rd_ncdf_3d (Fname,lon,lat,lev,nlon,nlat,nz,sxall,"std")
        Deallocate (lon,lat,lev)
        Call rd_ncdf_3d (Fname,lon,lat,lev,nlon,nlat,nz,Nsple,"nt")
        Deallocate (lon,lat,lev)
        print*,"Reading data done !!!"
        print*, "Nx,Nt,Nmode,LT,Nmon=",Nz,Nt,Nmode,LT,Nmon

        Allocate (xtmp(Nx),x11(Nmode))
        Allocate (Xpre(Nx), Fpre(Nmode), A(Nx,Nmode))
        Allocate (EOFall(LT,Nmon,Nt,Nmode))
        Do ilt=1,LT
           Do imon=1,Nmon
              tmp=Sqrt(Nsple(ilt,imon,1))
              !A(1:Nx,1:Nmode)=PCall(ilt,imon,1:Nx,1:Nmode)    ! Cac PCs
              Do j=1,Nx; A(j,1:Nmode)=PCall(ilt,imon,j,1:Nmode); Enddo
              !Nt1=0
              Do it=1,Nt
                 xtmp(1:Nx) = Xfc(ilt,imon,it,1:Nx)
                 Do j=1,Nx
                    if (xtmp(j)==Miss) print*,"================>",ilt,imon,it,j,xtmp(j)
                    Xpre(j) = (xtmp(j)-xtball(ilt,imon,j))/sxall(ilt,imon,j)/tmp;
                 Enddo
                 Fpre=Matmul(Xpre,A)
                 !print*,A
                 !print*,"ilt,imon,it=",ilt,imon,it
                 !print*,"===>",Fpre
                 !print*,"--->",Eigall(ilt,imon,1:Nmode)
                 Do j=1, Nmode; x11(j)=Fpre(j)/Eigall(ilt,imon,j); Enddo

                 EOFall(ilt,imon,it,1:Nmode)=x11(1:Nmode)
                 if (ilt==3.and.imon>=9) then
                    print*,"Fpre: ",Fpre(1:Nmode)
                    print*,"EOFs: ",EOFall(ilt,imon,it,1:Nmode)
                    print*,"Eigen values: ",Eigall(ilt,imon,1:Nmode) !Xfc(ilt,imon,it,1:Nx)
                    !pause
                 endif
                 !TC1(Nt1,imon)=TC(it,imon)
              Enddo 
           Enddo
        Enddo
        !print*,EOFall
        print*,"Calc_EOFs_for_Fcs_DL...Done"
     End Subroutine Calc_EOFs_for_Fcs_DL
     !-------------------------------------------------------------------------------------

