
      Subroutine Write_EOF_PCA_for_Plot (F_pca, F_eof, ivar)
         Character (Len=*) :: F_eof, F_pca
         Character (Len=255) :: var_ln, t_unit
         Integer :: i,j,k,iidx,it, ivar, Nlat, Nlon
         Real :: tmp
         Real, Allocatable :: PCA(:,:,:), xmode(:), EOF1(:,:), X1(:), Xtmp(:)
         Real, Allocatable :: lon(:), lat(:), xtime(:)

         Nlon=Nxpc(ivar); Nlat=Nypc(ivar)
         Allocate (PCA(Nlon,Nlat,Nmode), xmode(Nmode), Xtmp(Nmode)) !, EOF1(Nmode,Nt))
         Allocate (lon(Nlon), lat(Nlat), EOF1(Nt,Nmode))
         !-------------------------------------------------------------------------         
         ! Write to PCs file:
         PCA(:,:,:)=Miss; lon(:)=Miss; lat(:)=Miss
         if (ivar==1) then 
            k=0
         else
            k=0
            Do i=1,ivar-1 
               k=k+NSpl(i)
            Enddo
         endif
         !Nlon=Nxpc(ivar); Nlat=Nypc(ivar)
         lon(1:Nlon)=lonpc(ivar,1:Nlon); lat(1:Nlat)=latpc(ivar,1:Nlat)
         Do j=1, Nlat
            Do i=1, Nlon
               k=k+1; PCA(i,j,1:Nmode) = PCs(k,1:Nmode)
            Enddo
         Enddo
         Do i=1, Nmode; xmode(i)=i; Enddo         ! PCA's modes
         var_ln="Spatial distribution of PCAs of "//trim(Vars(ivar))
         t_unit="years since 1983-01-01 "
         Call wr_ncdf_3d (F_pca,lon,lat,xmode,nlon,nlat,Nmode, PCA, &
              "pc", trim(t_unit),trim(var_ln), &
               "-", "lon", "lat","mode", Miss)
         !-------------------------------------------------------------------------
         Xtmp(1:Nmode) = Eigenvalues(1:Nmode)
         Call wr_ncdf_1d_apd (F_pca, Xtmp, "eig", Nmode, &
                "Eigen values of ", "-", "mode", Miss)

         tmp=0.0
         Do i=1,Mcount
            tmp=tmp+Eigenvalues(i)
            if (I<=Nmode) then
               ECumulate(i)=0
               Do j=1,I
                  ECumulate(i)=ECumulate(i)+Eigenvalues(j)
               Enddo
            endif
         Enddo
         Do i=1,Nmode
            ECumulate(i)=ECumulate(i)/tmp; Xtmp(i)=Eigenvalues(i)/tmp
         Enddo

         Call wr_ncdf_1d_apd (F_pca, Xtmp, "var",Nmode, &
                "Percent Contribution ", "-", "mode", Miss)

         Xtmp(1:Nmode) = ECumulate(1:Nmode)
         Call wr_ncdf_1d_apd (F_pca, Xtmp, "cum",Nmode, &
                "Cumuluted Eigenvalues", "-", "mode", Miss )
         !-------------------------------------------------------------------------
         ! Write to EOF file:
         if (Allocated(xtime)) Deallocate (xtime); Allocate (xtime(1:Nt))
         Do it=1, Nt; xtime(it)=it+1982; Enddo     ! Months
         !Do i=1, Nmode; EOF1(i,1:Nt)=EOFs(1:Nt,i); Enddo
         Do i=1, Nmode; EOF1(1:Nt,i)=EOFs(1:Nt,i); Enddo
         var_ln="Timeseries of eofs"
         Call wr_ncdf_2d (F_eof,xtime,xmode, Nt,Nmode, EOF1, "eof", &
                     trim(var_ln), "-", "year","mode", Miss)

         Deallocate (PCA,xmode,Xtmp,lon,lat,EOF1,xtime)
         print*,"Write_EOF_PCA: Done !!!"
      End Subroutine Write_EOF_PCA_for_Plot 
      !---------------------------------------------------------------------------------

