SUBROUTINE CODE2
USE CODE8
USE CODE6
IMPLICIT NONE

CHARACTER(len=100) :: initfile,datafile,aux,out,t2
CHARACTER(len=100), ALLOCATABLE :: t1(:)
REAL(8), ALLOCATABLE :: one(:),zero(:),pp(:),v1(:),v2(:),dataset(:,:),auxb(:),auxa(:),mi(:)
REAL(8) :: auxt
INTEGER :: n_vars,l_w,l_d,l_m,l_t,k,k2
INTEGER, ALLOCATABLE :: l_x(:)
LOGICAL :: la,lb,ext,lmiss

!! CALL GETARG(1,initfile)
WRITE(*,*) 'Give me the init file input'
WRITE(*,*) '(include path if not in the same folder as the exe file)'
READ(*,*) initfile

OPEN(1,file=initfile)
OPEN(4,file='information.txt')
READ(1,fmt=*) datafile          
READ(1,fmt=*) iterations      
READ(1,fmt=*) burnin        
READ(1,fmt=*) step            
READ(1,fmt=*) Ii              
READ(1,fmt=*) n_vars          
READ(1,fmt=*) n_p            
READ(1,fmt=*) n_e            
READ(1,fmt=*) aux
READ(1,fmt=*) la,l_t          
READ(1,fmt=*) Hh             
WRITE(4,fmt=*) 'Done up to number of factors'
ALLOCATE(Mm(Hh),dataset(Ii,n_vars),fmix(Hh),v1(Hh),v2(Hh),pr(n_p),eq(n_e), &
        Sample_Factor(Ii),auxa(Hh),f(Ii,Hh),mi(Ii))
INQUIRE(file='f.dat',exist=ext)
IF (ext) THEN
        OPEN(2,file='f.dat')
        DO i = 1, Ii
           write(*,*) i
                READ(2,fmt=*) f(i,:)
        END DO
        CLOSE(2)
ELSE
        WRITE(*,*) 'I cannot find the f.dat file'
    WRITE(*,*) ' '
    Pause '(This file should contain initial values of the factors)'
    STOP
END IF
WRITE(4,fmt=*) 'Done reading f.dat'
OPEN(2,file=datafile)
DO i = 1, Ii
                READ(2,fmt=*) dataset(i,:)
END DO
CLOSE(2)
WRITE(4,fmt=*) 'Done reading dataset'
Sample_Factor=100000
IF(la) Sample_Factor = dataset(:,l_t)
OPEN(3,file='variables_included.txt')
cf=6
READ(1,fmt=*) aux
DO h = 1, Hh
        READ(1,fmt=*) fmix(h)%norm_rest
        IF (fmix(h)%norm_rest) THEN
                ALLOCATE(one(1),zero(1),pp(1))
                one=1.0d0
                zero=0.0d0
                pp=1.0d0
                cf=cf+1
                OPEN(cf,file=out)
                CALL init_Univariate_normal_mixture(fmix(h),Ii,1,zero,one,pp,out,cf)
                DEALLOCATE(one,zero,pp)
        ELSE
                READ(1,fmt=*) Mm(h)
                READ(1,fmt=*) fmix(h)%zero_rest
                READ(1,fmt=*) fmix(h)%first_scale
                READ(1,fmt=*) fmix(h)%scale_mix
                READ(1,fmt=*) fmix(h)%mean_mix
                READ(1,fmt=*) out
                ALLOCATE(one(Mm(h)),zero(Mm(h)),pp(Mm(h)))
                one=1.0d0
                zero=0.0d0
                pp=1.0d0/Mm(h)
                cf=cf+1
                OPEN(cf,file=out)
                CALL init_Univariate_normal_mixture(fmix(h),Ii,Mm(h),zero,one,pp,out,cf)
                DEALLOCATE(one,zero,pp)
        END IF
        fmix(h)%prior_l_a=2.0d0
        fmix(h)%prior_mu_mu=0.0d0
        fmix(h)%prior_mu_p=0.1d0
        fmix(h)%prior_tau_a=g1
        fmix(h)%prior_tau_b=g2
        fmix(h)%lgroup(:,1)=.true.
        fmix(h)%linc=.true.
        WRITE(4,fmt=*) 'READ FACTOR',h
END DO
IF (n_p>0) THEN
        DO np = 1, n_p
                READ(1,fmt=*) aux
                READ(1,fmt=*) l_d
                READ(1,fmt=*) lmiss,l_m
                IF (lmiss) mi=dataset(:,l_m)
                READ(1,fmt=*) k 
                pr(np)%eq%k=k
                ALLOCATE(l_x(pr(np)%eq%k))
                READ(1,fmt=*) l_x             
                READ(1,fmt=*) pr(np)%eq%drop_perfect_predict,pr(np)%eq%loc_constant 
               
                READ(1,fmt=*) la               
                IF (la) THEN
                        READ(1,fmt=*) v1      
                        READ(1,fmt=*) v2       
                        WHERE (v1==0.0d0) v2=0.1d0    
                ELSE
                        v1 = 0.0d0
                        v2 = 0.1d0
                END IF
                CALL NORMA(pr(np)%eq%nor,Hh,v1,v2)
                READ(1,fmt=*) out
                cf = cf + 1
                OPEN(cf,file=out)
                CALL elements_multi(pr(np)%eq,dataset(:,:),n_vars,Hh,Ii,l_x,l_m,lmiss,cf,l_d)
            IF (k.eq.pr(np)%eq%k) THEN
                        WRITE(3,'(A21,I4,A23)') "For Probit equation ",np," ALL variables included"
                        WRITE(3,'(A)') ' ' 
                ELSE
                        WRITE(3,'(A21,I4,A20)') "For Probit equation ",np," variables included:"
                        ALLOCATE(t1(pr(np)%eq%k))
                        DO h = 1, pr(np)%eq%k
                                WRITE(t2,*) l_x(pr(np)%eq%icol(h))
                                WRITE(t1(h),*) 'v'//TRIM(ADJUSTL(t2))
                                t1(h) = TRIM(ADJUSTL(t1(h)))
                        END DO
                        WRITE(3,'(40A5)') t1
                        DEALLOCATE(t1)
                        WRITE(3,'(A)') ' ' 
                END IF
                DEALLOCATE(l_x)
                pr(np)%eq%b = 0.1d0                               
                pr(np)%eq%a = pr(np)%eq%nor%v     
                pr(np)%eq%t = 1.0d0                         
                ALLOCATE(pr(np)%d(Ii))
                pr(np)%d = dataset(:,l_d)
                READ(1,fmt=*) la,l_m               
                IF (la) THEN
                        ALLOCATE(auxb(pr(np)%eq%k))
                        READ(1,fmt=*) out
                        OPEN(cf+1,file=out)
                        DO h = 1, l_m
                                READ(cf+1,fmt=*) auxb,auxa,auxt
                        END DO
                        CLOSE(cf+1)
                        pr(np)%eq%b = auxb             
                        DO h = 1, Hh
                                IF (v1(h).ne.1.0d0) pr(np)%eq%a(h) = auxa(h)
                        END DO
                        DEALLOCATE(auxb)
                END IF
                WRITE(4,fmt=*) 'DONE WITH PROBIT EQUATION',np
        END DO
        WRITE(3,'(A)') ' ' 
END IF
IF (n_e>0) THEN
        DO ne = 1, n_e
                READ(1,fmt=*) aux
                READ(1,fmt=*) l_w
                READ(1,fmt=*) lmiss,l_m
                IF (lmiss) mi=dataset(:,l_m)
                READ(1,fmt=*) k
                eq(ne)%k=k
                ALLOCATE(l_x(eq(ne)%k))
                READ(1,fmt=*) l_x          
                READ(1,fmt=*) la      
                IF (la) THEN
                        READ(1,fmt=*) v1    
                        READ(1,fmt=*) v2    
                        WHERE (v1==0.0d0) v2=0.1d0  
                ELSE
                        v1 = 0.0d0
                        v2 = 0.1d0
                END IF
                CALL NORMA(eq(ne)%nor,Hh,v1,v2)
                READ(1,fmt=*) out
                cf = cf + 1
                OPEN(cf,file=out)
                CALL elements_linear(eq(ne),dataset(:,:),n_vars,Hh,Ii,l_x,l_w,l_m,lmiss,cf)
            IF (k.eq.eq(ne)%k) THEN
                        WRITE(3,'(A21,I4,A23)') "For Linear equation ",ne," ALL variables included"
                        WRITE(3,'(A)') ' ' 
                ELSE
                        WRITE(3,'(A21,I4,A20)') "For Linear equation ",ne," variables included:"
                        ALLOCATE(t1(eq(ne)%k))
                        DO h = 1, eq(ne)%k
                                WRITE(t2,*) l_x(eq(ne)%icol(h))
                                WRITE(t1(h),*) 'v'//TRIM(ADJUSTL(t2))
                                t1(h) = TRIM(ADJUSTL(t1(h)))
                        END DO
                        WRITE(3,'(40A5)') t1
                        DEALLOCATE(t1)
                        WRITE(3,'(A)') ' ' 
                END IF
                DEALLOCATE(l_x)
                eq(ne)%b = 0.1d0                                 
                eq(ne)%a = eq(ne)%nor%v       
                eq(ne)%t = 1.0d0                        
                READ(1,fmt=*) la,l_m                 
                IF (la) THEN
                        ALLOCATE(auxb(eq(ne)%k))
                        READ(1,fmt=*) out
                        OPEN(cf+1,file=out)
                        DO h = 1, l_m
                                READ(cf+1,fmt=*) auxb,auxa,auxt
                        END DO
                        CLOSE(cf+1)
                        eq(ne)%b = auxb                  
                        DO h = 1, Hh
                                IF (v1(h).ne.1.0d0) eq(ne)%a(h) = auxa(h)
                        END DO
                        eq(ne)%t=auxt
                        DEALLOCATE(auxb)        
                END IF
                WRITE(4,fmt=*) 'DONE WITH LINEAR EQUATION',ne
        END DO
        WRITE(3,'(A)') ' ' 
END IF
READ(1,fmt=*)
READ(1,fmt=*) prior_var
WRITE(4,fmt=*) 'DONE WITH PRIORS'
CLOSE(1)
CLOSE(3)
WRITE(*,*) 'DONE WITH INIT FILE'
WRITE(4,fmt=*) 'DONE WITH INIT FILE'

END SUBROUTINE CODE2
