	program aap
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr

	nikhefnr=42
	call chamber()
C	call labelaar()
	end



	subroutine labelaar()
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr
	
	integer labelcounter(100)
	character*512 line,lia
	character*40 chname
	character*20 lname

	do i=1,100
	   labelcounter(i)=0
	enddo


        nlables=0
	ncomps=0
        open(20,file='bolcabling.csv')
 10	continue
	read(20,'(a)',END=20)line
	if (line(1:6).eq.'NIKHEF') then
           call setlables(line)
        endif
	if (line(1:3).eq.'BOL') then
	   call getnikhefnr(line,inow,chname)
	   atlasname=chname
	   call setcomps(line)
	   if (ncomps.ne.nlables) then
	      print *,'FATAL: ncomps ne nlables ',ncomps, nlables
	   endif
	   do i=1,ncomps
	      if (lcomps(i).ne.'0') labelcounter(i)=labelcounter(i)+1
	   enddo
	endif
        goto 10
 20	continue

	if (ncomps.eq.0) then
	   print *,'FATAL: no comps found on exit labelaar '
	endif

	   write(*,*)'========================================'
	   write(*,*)'---------->LABELS en AANTAL<------------'
	   do i=1,nlables
	      lname=lables(i)
	      write(*,*)lname,labelcounter(i)
	   enddo
	   write(*,*)'---------->The End<---------------------'
	   write(*,*)'========================================'
	   close(20)
	end



	subroutine chamber()
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr
	character*512 line,lia
	character*40 chname
	integer inow
        nlables=0
	ncomps=0
        open(20,file='bolcabling.csv')
 10	continue
	read(20,'(a)',END=20)line
	if (line(1:6).eq.'NIKHEF') then
           call setlables(line)
        endif
	if (line(1:3).eq.'BOL') then
	   call getnikhefnr(line,inow,chname)
	   if (nikhefnr.eq.inow) then
	      atlasname=chname
	      call setcomps(line)
	      if (ncomps.ne.nlables) then
		 print *,'FATAL: ncomps ne nlables ',ncomps, nlables
	      endif
	   endif
	endif
        goto 10
 20	continue
	call prilables()
	if (ncomps.eq.0) then
	   print *,'FATAL: no comps found on exit ', nikhefnr
	endif
	close(20)
	end

C         daily='Daily.'//datum

	subroutine setlables(line)
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr



	character*512 line
        integer icount,i1,i2
	icount=0
        i1=1
	i2=1
        ilast=512
	do i=6,ilast
	  if ((line(i:i).eq.',') .or. (i.eq.ilast)) then
		icount=icount+1 
                i1=i2+2
                i2=i-1
                if (icount.gt.3) then
                   nlables=nlables+1
		   lables(nlables)=line(i1:i2)
C                   write(*,*)lables(nlables)
	        endif
	  endif
	enddo
	end
		   


	subroutine setcomps(line)
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr



	character*512 line
        integer icount,i1,i2
	ncomps=0
	icount=0
        i1=1
	i2=1
        ilast=512
	do i=1,ilast
	  if ((line(i:i).eq.',') .or. (i.eq.ilast)) then
		icount=icount+1 
                i1=i2+2
                i2=i-1
                if (icount.gt.3) then
                   ncomps=ncomps+1
		   lcomps(ncomps)=line(i1:i2)
C                   write(*,*)lcomps(ncomps)
	        endif
	  endif
	enddo
	end
		   




	subroutine getnikhefnr(line,nr,chname)
	character*40 chname
        integer nr
	character*512 line
	character*2 cnr
        integer icount,i1,i2
        nr=0
	icount=0
        i1=4
	i2=1
        ilast=8
	do i=1,ilast
	   if (line(i:i).eq.',') then
	      i2=i-1
	   endif
	enddo
	if (i2.gt.1) then
	   cnr=line(i1:i2)
	   read(cnr,*)nr
	endif

C get Atlas name
	icount=0
        i1=1
	i2=-1
        ilast=40
	do i=1,ilast
	   if (line(i:i).eq.',') then
	      i1=i2+2
	      i2=i-1
	      icount=icount+1
	   endif
	   if (icount.eq.2) then
	      chname=line(i1:i2)
	   endif
	enddo
	end
		   


	subroutine prilables
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr

	
	write(*,*)' '
	write(*,*)'For BOL chamber nikhef number: ',nikhefnr
	write(*,*)'    ATLAS name= ',atlasname
	write(*,*)' '
	write(*,*)'---->Groffe indicatie positie lables: '
	write(*,*)' '
	write(*,*)'                                  C-side '
	call priprolable(1)
	write(*,*)'        --------------------
     +------------------------------------------'
	call pripraxilable(1)
	call priccclable(1)
	call priprolable(3)
	write(*,*)'       |                        
     +                                      |'
	write(*,*)'RO-side|                        
     +                                      |HV-side'
	write(*,*)'       |                        
     +                                      |'
		   
	call priprolable(2)
	call priccclable(2)	
        call pripraxilable(2)
	write(*,*)'        --------------------
     +------------------------------------------'
	call priprolable(4)
	write(*,*)'                                  A-side '
	write(*,*)' '
	write(*,*)' '
	write(*,*)' '
	write(*,*)'Alle lables op deze kamer
     +   (nummers verwijzen naar uitzonderingen): '
	call prilist()
	write(*,*)' '
	call prirasmux()

	end

	subroutine prilist()
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr
	character*18 lab(4)
	character*2 rem1
	character*3 rem
	character*80 lc
	   lab(1)='                  '
	   lab(2)='                  '
	   lab(3)='                  '
	   lab(4)='                  '
	   il=1
	do i=1,nlables
	   if (lcomps(i).ne.'0') then
	      rem='   '
	      if (lcomps(i).ne.'1') then
		 lc=lcomps(i)
		 rem1='('//lc(1:1)
		 rem=rem1//')'
	      endif
	      lab(il)=rem//lables(i)
	      il=il+1
	   endif
	   if (il.eq.5 .or. i.eq.nlables) then
	      write(*,*)lab(1),lab(2),lab(3),lab(4)
	      lab(1)='                  '
	      lab(2)='                  '
	      lab(3)='                  '
	      lab(4)='                  '
	      il=1
	   endif
	enddo
	end



	subroutine prirasmux()
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
     	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr
	character*80 lc

	   write(*,*)'Standard RASMUX LED CALBING SCHEMA blz. 17 '
	   write(*,*)'Standard RASMUX CAMERA CALBING SCHEMA blz. 16 (tekening)
     +  en blz. 25 (lijst) '


C 1Axx en 1Axx no 13
	if ((atlasname(9:9).eq.'1') .and.
     +     (atlasname(11:12).ne.'13') )  then
	   write(*,*) 'Voor deze kamer SPECIAAL RASMUX SCHEMA: See blz 41 '
	endif

C BOl2 A+C met pro
	if ((atlasname(9:9).eq.'2') .and.
     +     (atlasname(11:12).ne.'13') )  then
	   write(*,*)'RASMUX CAMERA vol (geen plek voor RC_PRO_xxx cable).'
           write(*,*)' Maak patchpanel.'
	   write(*,*) 'Voor deze kamer SPECIAAL RASMUX SCHEMA: See blz 41 '
	endif

C BOl3 A+C met pro
	if ((atlasname(9:9).eq.'3') .and.
     +     (atlasname(11:12).eq.'13') )  then
	   write(*,*)'RASMUX CAMERA vol (geen plek voor RC_PRO_xxx cable).'
           write(*,*)' Maak patchpanel.'
	   write(*,*) 'Voor deze kamer SPECIAAL RASMUX SCHEMA: See blz 42 '
	endif

C BOl4 A+C met pro
	if ((atlasname(9:9).eq.'4') .and.
     +     (atlasname(11:12).eq.'13') )  then
	   write(*,*)'RASMUX CAMERA vol (geen plek voor RC_PRO_xxx cable).'
           write(*,*)' Maak patchpanel.'
	   write(*,*) 'Voor deze kamer SPECIAAL RASMUX SCHEMA: See blz 42 '
	endif

C BOl4 A+C met pro
	if ((atlasname(9:9).eq.'4') .and.
     +     (atlasname(11:12).ne.'13') )  then
	   write(*,*)'RASMUX CAMERA vol (geen plek voor RC_PRO_xxx cable).'
           write(*,*)' Maak patchpanel (wordt later 2de rasmux).'
	   write(*,*) 'Voor deze kamer SPECIAAL RASMUX SCHEMA: See blz 43 '
	endif

C BOl6 A+C met pro
	if ((atlasname(9:9).eq.'6') .and.
     +     ((atlasname(11:12).ne.'15').and.(atlasname(11:12).ne.'11')) )
     +     then
	   write(*,*) 'Voor deze kamer SPECIAAL RASMUX SCHEMA: See blz 44 '
	endif

	if ((atlasname(9:9).eq.'6') .and.
     +     ((atlasname(11:12).eq.'11').or.(atlasname(11:12).ne.'15')) )
     +     then
	   write(*,*) 'Voor deze kamer SPECIAAL RASMUX SCHEMA: See blz 45 '
	endif

	end



	subroutine pripraxilable(ipri)
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr

	
	character*11 lab1,lab2,lab3,lab4,
     +               plab1,plab2,plab3,plab4
	character*16 space
	space=''
	if (ipri.eq.1) then
C Cameraas C side
	    lab1='RC_PRA_CRO'
	    lab2='RC_PRA_CHV'
	    lab3='RC_AXI_CRO'
	    lab4='RC_AXI_CHV'
	   plab1='          '
	   plab2='          '
	   plab3='          '
	   plab4='          '
	   do i=1,nlables
	      if ((lables(i).eq.lab1) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab1
		 plab1=lab1
	      endif
	      if ((lables(i).eq.lab2) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab2
		 plab2=lab2
	      endif
	      if ((lables(i).eq.lab3) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab3
		 plab3=lab3
	      endif
	      if ((lables(i).eq.lab4) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab4
		 plab4=lab4
	      endif

	   enddo
	   write(*,*)'       | ',plab3, plab1,space, plab2, plab4,' | '

C Leds C side
	    lab1='RL_PRA_CRO'
	    lab2='RL_PRA_CHV'
	    lab3='RL_AXI_CRO'
	    lab4='RL_AXI_CHV'
	   plab1='          '
	   plab2='          '
	   plab3='          '
	   plab4='          '
	   do i=1,nlables
	      if ((lables(i).eq.lab1) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab1
		 plab1=lab1
	      endif
	      if ((lables(i).eq.lab2) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab2
		 plab2=lab2
	      endif
	      if ((lables(i).eq.lab3) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab3
		 plab3=lab3
	      endif
	      if ((lables(i).eq.lab4) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab4
		 plab4=lab4
	      endif

	   enddo
	   write(*,*)'       | ',plab3, plab1,space, plab2, plab4,' | '
	endif			! ipri=1


	if (ipri.eq.2) then
C Cameraas A side
	    lab1='RC_PRA_ARO'
	    lab2='RC_PRA_AHV'
	    lab3='RC_AXI_ARO'
	    lab4='RC_AXI_AHV'
	   plab1='          '
	   plab2='          '
	   plab3='          '
	   plab4='          '
	   do i=1,nlables
	      if ((lables(i).eq.lab1) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab1
		 plab1=lab1
	      endif
	      if ((lables(i).eq.lab2) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab2
		 plab2=lab2
	      endif
	      if ((lables(i).eq.lab3) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab3
		 plab3=lab3
	      endif
	      if ((lables(i).eq.lab4) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab4
		 plab4=lab4
	      endif

	   enddo
	   write(*,*)'       | ',plab3, plab1,space, plab2, plab4,' | '

C Leds C side
	    lab1='RL_PRA_ARO'
	    lab2='RL_PRA_AHV'
	    lab3='RL_AXI_ARO'
	    lab4='RL_AXI_AHV'
	   plab1='          '
	   plab2='          '
	   plab3='          '
	   plab4='          '
	   do i=1,nlables
	      if ((lables(i).eq.lab1) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab1
		 plab1=lab1
	      endif
	      if ((lables(i).eq.lab2) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab2
		 plab2=lab2
	      endif
	      if ((lables(i).eq.lab3) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab3
		 plab3=lab3
	      endif
	      if ((lables(i).eq.lab4) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab4
		 plab4=lab4
	      endif

	   enddo
	   write(*,*)'       | ',plab3, plab1,space, plab2, plab4,' | '
	endif			! ipri=2

	end




	subroutine priccclable(ipri)
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr

	
	character*11 lab1,lab2,lab3,lab4,
     +               plab1,plab2,plab3,plab4
	character*18 space
	space=''
	if (ipri.eq.1) then
C Leds C side
	    lab1='SL_CCC_RO'
	    lab2='SL_CCC_HV'
	   plab1='         '
	   plab2='         '
	   do i=1,nlables
	      if ((lables(i).eq.lab1) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab1
		 plab1=lab1
	      endif
	      if ((lables(i).eq.lab2) .and. (lcomps(i).ne.'0')) then
		 if (lcomps(i).ne.'1') print *,'unknown flag ',lab2
		 plab2=lab2
	      endif
	   enddo
	   write(*,*)'       | ',plab1,space, space, '  ',plab2,' | '
	endif			! ipri=1


	if (ipri.eq.2) then
C Leds A side --NO CCC LEDS
	   plab1='          '
	   plab2='          '
	   write(*,*)'       | ',plab1,space, space,'  ', plab2,' | '
	endif			! ipri=2

	end





	subroutine priprolable(ipri)
	common /caap/lables,lcomps,atlasname, 
     +               nlables,ncomps, nikhefnr
	character*80 lables(100),lcomps(100)
	character*40 atlasname
        integer nlables, ncomps, nikhefnr

	
	character*11 lab1,lab2,lab3,lab4,
     +               plab1,plab2,plab3,plab4
	character*16 space
	space=''


	lab1='RC_PRO_RO'
	lab2='RC_PRO_HV'
	lab3=''
	lab4=''
	plab1='          '
	plab2='          '
	plab3='          '
	plab4='          '
	do i=1,nlables
	   if ((lables(i).eq.lab1) .and. (lcomps(i).ne.'0')) then
	      if (.not.((lcomps(i).eq.'1').or.
     +                  (lcomps(i).ne.'2').or.
     +                  (lcomps(i).ne.'3')) )
     +			 print *,'unknown flag ',lab1
	      plab1=lab1
	   endif
	   if ((lables(i).eq.lab2) .and. (lcomps(i).ne.'0')) then
	      if (.not.((lcomps(i).eq.'1').or.
     +                  (lcomps(i).ne.'2').or.
     +                  (lcomps(i).ne.'3')) )
     +			 print *,'unknown flag ',lab2
	      plab2=lab2
	   endif
	   
	enddo
C for BOL1A type C side
	if (ipri.eq.1) then 
	plab3=plab1
	plab4=plab2
	if (atlasname(9:10).ne.'1A') then
	   plab3='          '
	   plab4='          '
	endif
	write(*,*)'             ',plab3,
     +  '                              ',plab4
C for BOL1C type A side
	else if (ipri.eq.4) then 
	plab3=plab1
	plab4=plab2
	if (atlasname(9:10).ne.'1C') then
	   plab3='          '
	   plab4='          '
	endif
	write(*,*)'             ',plab3,
     +  '                              ',plab4
C for BOL2,4A type (A side) and 6 C
	else if (ipri.eq.2) then
	plab3=plab1
	plab4=plab2
	if (.not.((atlasname(9:10).eq.'2A') .or.
     +           (atlasname(9:10).eq.'4A').or.
     +           (atlasname(9:10).eq.'6C') )) then
	   plab3='          '
	   plab4='          '
	endif
	write(*,*)'  ',plab3,'                       
     +                               ',plab4
C for BOL6A type (Cside) and 2,4C
	else if (ipri.eq.3) then
	plab3=plab1
	plab4=plab2
	if (.not.( (atlasname(9:10).eq.'6A').or.
     +	     (atlasname(9:10).eq.'4C').or.
     +	     (atlasname(9:10).eq.'2C') ) )then
	   plab3='          '
	   plab4='          '
	endif
	write(*,*)'  ',plab3,'                       
     +                               ',plab4
	endif
	end
	
