! N. R. BADNELL  OMU2OM       UoS                               09/01/19
!
      PROGRAM MAIN
!
      IMPLICIT REAL*8 (A-H,O-Z)
!
      PARAMETER (ZERO=0.0D0)
!
! CONVERT UNFORMATED OMEGA FILE TO FORMATTED.
!
      ALLOCATABLE :: ENAT(:),LAT(:),ISAT(:),OMEGA(:),NRD(:),NWR(:),EV(:)
!
      call cpu_time(timei)
!
      OPEN(7,FILE='OMEGA',STATUS='UNKNOWN',FORM='FORMATTED')  !OUTPUT
      OPEN(8,FILE='OMEGAU',STATUS='OLD',FORM='UNFORMATTED')   !INPUT
!
!  READ-IN BASIC DATA
!
      READ(8)NZED,NELC
      WRITE(7,*)NZED,NELC
!
      READ(8)NAST,MXE0,NOMWRT
      WRITE(7,*)NAST,MXE0,NOMWRT
!
      IF(NZED.EQ.NELC)THEN
        NOMT=(NAST*(NAST+1))/2
        IONE=0
        NOT=(NAST*(NAST-1))/2
      ELSE
        NOMT=(NAST*(NAST-1))/2
        IONE=1
        NOT=(NAST*(NAST+1))/2
      ENDIF
!
      IF(NOMWRT.NE.0)THEN
        NOMT=IABS(NOMWRT)
        IF(NOMT.EQ.NOT)THEN
          IONE=ABS(IONE-1)
          WRITE(*,*)'*** ASSUMING NON-DEFAULT ELASTIC TRANSITIONS'
        ENDIF
      ENDIF
!
      MXE=IABS(MXE0)                                    !CASE AS-DW FLAG
!
      ALLOCATE (ENAT(NAST),LAT(NAST),ISAT(NAST),OMEGA(NOMT),NRD(MXE)    &
     &         ,NWR(MXE),EV(MXE),STAT=IERR)
      IF(IERR.NE.0)THEN                              
        WRITE(0,*)'ALLOCATION FAILS FOR NAST,NOMT,MXE=',NAST,NOMT,MXE
        STOP 'ABORT'                                         
      ENDIF                                          
!
      READ(8)(ISAT(I),LAT(I),I=1,NAST)
      READ(8)(ENAT(I),I=1,NAST)
      WRITE(7,*)(ISAT(I),LAT(I),I=1,NAST)
      WRITE(7,710)(ENAT(I),I=1,NAST)
!
      IF(NOMWRT.GE.0)THEN                            !AND CASE MXE0.LT.0
        DO I=1,MXE
          NRD(I)=NOMT
          NWR(I)=NOMT
        ENDDO
      ELSE
        DO N=1,NOMT
          OMEGA(N)=ZERO
        ENDDO
!READ
        I0=1
        DO I=1,MXE
          READ(8,end=1)E
          EV(I)=E
          IF(E.GE.ENAT(NAST))GO TO 1
          NRD(I)=NOPEN(E,ENAT,NAST,IONE,I0)
          NRD(I)=MIN(NRD(I),NOMT)
        ENDDO
        I=MXE+1
   1    IE=I
        DO I=IE,MXE
          NRD(I)=NOMT
        ENDDO
        REWIND(8)
        NREC=4
        DO N=1,NREC
          READ(8)
        ENDDO
!WRITE - ENAT GETS ROUNDED SO NRD MAY NOT EQUAL NWR
        NREC=1+(NAST-1)/5
        DO N=1,NREC
          BACKSPACE(7)
        ENDDO
        READ(7,710)(ENAT(I),I=1,NAST)
        I0=1
        DO I=1,MXE
          E=EV(I)
          WRITE(7,700)E
          BACKSPACE(7)
          READ(7,700)E
          BACKSPACE(7)
          IF(E.GE.ENAT(NAST))GO TO 2
          NWR(I)=NOPEN(E,ENAT,NAST,IONE,I0)
          NWR(I)=MIN(NWR(I),NOMT)
        ENDDO
        I=MXE+1
   2    IE=I
        DO I=IE,MXE
          NWR(I)=NOMT
        ENDDO
      ENDIF
!
      DO I=1,MXE
!        write(77,*)i,nrd(i),nwr(i),ev(i)
        READ(8,err=3,end=100)E,(OMEGA(N),N=1,NRD(I))
   3    WRITE(7,700)E,(OMEGA(N),N=1,NWR(I))
      ENDDO     
!
      DEALLOCATE (ENAT,LAT,ISAT,OMEGA,NRD,NWR,EV,STAT=IERR)
      IF(IERR.NE.0)THEN                              
        WRITE(0,*)'DE-ALLOCATION FAILS FOR NAST,NOMT,MXE...'
        STOP 'ABORT'                                         
      ENDIF                                          
!
      call cpu_time(timef)
!
      time=(timef-timei)/60.
      write(*,777)time
 777  format(' CPU TIME=',f9.3,' MIN')
!
      STOP 'NORMAL FINISH TO CONVERSION'
!
  100 WRITE(0,*)'ATTEMPTING TO READ OMEGAS FOR ENERGY INDEX =',I
      STOP 'UNEXPECTED END-OF-FILE...'
!
  700 FORMAT(1PE14.8,6(1PE11.3)/(14X,6(E11.3)))
  710 FORMAT(1P5E16.6)
!
      END
!
!************************************************
!
      INTEGER FUNCTION NOPEN(E,ENAT,NAST,IONE,I0)
      IMPLICIT REAL*8 (A-H,O-Z)
!
      DIMENSION ENAT(NAST)
!
      DO I=I0,NAST
        IF(E.LT.ENAT(I))GO TO 1
      ENDDO
      I=NAST+1
   1  I0=I-1
      NOPEN=(I0*(I0-2*IONE+1))/2
!
      RETURN
      END
