      program readsum
      parameter (nddat=100,ndnuc=131)
      real efun(nddat),
     *     stab(nddat),
     *     anum(nddat),
     *     deln(nddat),
     *     ambn(nddat),
     *     ambp(nddat),
     *     spii(nddat),
     *     spik(nddat),
     *     anor(nddat),
     *     anoi(nddat),
     *     epro(nddat),
     *     apro(nddat),
     *     q2to(nddat),
     *     spin(nddat),
     *     am_i(nddat),
     *     am_s(nddat),
     *     aq_s(nddat)
      real allefun(nddat,ndnuc),allepro(nddat,ndnuc),
     *     allam_s(nddat,ndnuc)
      common /all/ efun,stab,anum,deln,
     *             ambn,ambp,spii,spik,
     *             anor,anoi,epro,apro,q2to,
     *             spin,am_i,am_s,aq_s
      character*79 line
      character*70 filnam,filnao
c
      nnuc=97
 1256 continue
      nnuc=nnuc+2
      read (*,'(i2,1x,a)',end=1257) idata,filnam
      filnao=trim(filnam)//'.txt'
      open(21,file=filnao,form='formatted',status='old')
      write(*,*) idata,filnao
c
      efun=0.0d0
      stab=0.0d0
      anum=0.0d0
      deln=0.0d0
      ambn=0.0d0
      ambp=0.0d0
      spii=0.0d0
      spik=0.0d0
      anor=0.0d0
      anoi=0.0d0
      epro=0.0d0
      apro=0.0d0
      q2to=0.0d0
      spin=0.0d0
      am_i=0.0d0
      am_s=0.0d0
      aq_s=0.0d0
c
      call readfile(idata,21)
c
      close (21)
c
      filnao=trim(filnam)//'.csv'
      open(22,file=filnao,form='formatted',status='unknown')
c
      write(22,'(17(14x,a4,'',''))')
     *             'anum','stab','efun','deln',
     *             'ambn','ambp','spii','spik',
     *             'anor','anoi','epro','apro','q2to',
     *             'spin','am_i','am_s','aq_s'
c
      do inuc=1,idata
         write(22,'(17(e18.10,'',''))')
     *             anum(inuc),stab(inuc),efun(inuc),deln(inuc),
     *             ambn(inuc),ambp(inuc),spii(inuc),spik(inuc),
     *             anor(inuc),anoi(inuc),epro(inuc),apro(inuc),
     *             q2to(inuc),
     *             spin(inuc),am_i(inuc),am_s(inuc),aq_s(inuc)
      end do
c
      allefun(:,nnuc)=efun(:)
      allepro(:,nnuc)=epro(:)
      allam_s(:,nnuc)=am_s(:)
c
      close (22)
c
      goto 1256
c
 1257 continue
c
      filnao=trim(filnam)//'.csv'
c
      filnao(3:5)='rel'
      open(22,file=filnao,form='formatted',status='unknown')
c
      write(22,'(a15,17(i18,a1))')
     *           '           Q20,',(inuc,',',inuc=99,131,2)
c
      do idat=1,idata
         write(22,'(18(e18.10,'',''))')
     *             q2to(idat),(allefun(idat,inuc)
     *                        -allefun(   1,inuc),inuc=99,131,2)
      end do
c
      close (22)
c
      filnao(3:5)='pro'
      open(22,file=filnao,form='formatted',status='unknown')
c
      write(22,'(a15,17(i18,a1))')
     *           '           Q20,',(inuc,',',inuc=99,131,2)
c
      do idat=1,idata
         write(22,'(18(e18.10,'',''))')
     *             q2to(idat),(allepro(idat,inuc)
     *                        -allepro(   1,inuc),inuc=99,131,2)
      end do
c
      close (22)
c
      filnao(3:5)='m_s'
      open(22,file=filnao,form='formatted',status='unknown')
c
      write(22,'(a15,17(i18,a1))')
     *           '           Q20,',(inuc,',',inuc=99,131,2)
c
      do idat=1,idata
         write(22,'(18(e18.10,'',''))')
     *             q2to(idat),(allam_s(idat,inuc),inuc=99,131,2)
      end do
c
      close (22)
c
      end
c
      subroutine readfile(idata,ifil)
      parameter (nddat=100)
      real efun(nddat),
     *     stab(nddat),
     *     anum(nddat),
     *     deln(nddat),
     *     ambn(nddat),
     *     ambp(nddat),
     *     spii(nddat),
     *     spik(nddat),
     *     anor(nddat),
     *     anoi(nddat),
     *     epro(nddat),
     *     apro(nddat),
     *     q2to(nddat),
     *     spin(nddat),
     *     am_i(nddat),
     *     am_s(nddat),
     *     aq_s(nddat)
      common /all/ efun,stab,anum,deln,
     *             ambn,ambp,spii,spik,
     *             anor,anoi,epro,apro,q2to,
     *             spin,am_i,am_s,aq_s
c
      call readobs(idata,ifil,             
     *             efun,'*  TOTAL:  (STAB)=',1,18,64,1)
      call readobs(idata,ifil,
     *             stab,'*  TOTAL:  (STAB)=',1,18,18,1)
      call readobs(idata,ifil,
     *             anum,'*  in',1,5,5,2)
      call readobs(idata,ifil,
     *             deln,'*  PAIRING: (NEU)=',1,18,18,1)
      call readobs(idata,ifil,
     *             ambn,'*  E-FERMI: (NEU)=',1,18,18,1)
      call readobs(idata,ifil,
     *             ambp,'*  E-FERMI: (NEU)=',1,18,41,1)
      call readobs(idata,ifil,
     *             spii,'* I,K=',1,6,7,2)
      call readobs(idata,ifil,
     *             spik,'* I,K=',1,6,13,2)
      call readobs(idata,ifil,
     *             anor,'* I,K=',1,6,24,3)
      call readobs(idata,ifil,
     *             anoi,'* I,K=',1,6,43,3)
      call readobs(idata,ifil,
     *             epro,'*              E_TOTAL =',1,24,64,1)
      call readobs(idata,ifil,
     *             apro,'*                    A =',1,24,64,1)
      call readobs(idata,ifil,
     *             q2to,'*  Q20 =',1,8,8,7)
      call readobs(idata,ifil,
     *             spin,'*     TOTAL',1,11,69,4)
      call readobs(idata,ifil,
     *             am_i,'*  M1_X =',1,9,47,5)
      call readobs(idata,ifil,
     *             am_s,'*   9/2   1    1   9/2   1',1,26,26,6)
      call readobs(idata,ifil,
     *             am_s,'*   1/2   1    1   1/2   1',1,26,26,6)
      call readobs(idata,ifil,
     *             am_s,'*   2     1    1   2     1',1,26,26,6)
      call readobs(idata,ifil,
     *             am_s,'*   1     1    1   1     1',1,26,26,6)
      call readobs(idata,ifil,
     *             aq_s,'*   9/2   1    2   9/2   1',1,26,26,6)
      call readobs(idata,ifil,
     *             aq_s,'*   1/2   1    2   1/2   1',1,26,26,6)
      call readobs(idata,ifil,
     *             aq_s,'*   2     1    2   2     1',1,26,26,6)
      call readobs(idata,ifil,
     *             aq_s,'*   1     1    2   1     1',1,26,26,6)
c
      end
c
      subroutine readobs(idata,ifil,obse,begi,ista,isto,iski,ityp)
      parameter (nddat=100)
      real obse(nddat)
      character*79 line
      character begi
      character*79 skip
c
      iobs=0
c
      rewind ifil
c
 1258 continue
      read (ifil,'(a)',end=1259) line
!     write(*,*) line(ista:isto),'||||',begi(1:ista-isto+1)
      if (line(ista:isto).ne.begi(1:isto-ista+1)) go to 1258
      if (ityp.eq.1) read (line,'(a,f13.6)',err=1260) skip(1:iski),obs
      if (ityp.eq.2) then
          read (line,'(a,i3)',err=1260) skip(1:iski),kobs
          obs=kobs
      endif
      if (ityp.eq.3) read (line,'(a,f19.12)',err=1260) skip(1:iski),obs
      if (ityp.eq.4) read (line,'(a,f7.3)',err=1260) skip(1:iski),obs
      if (ityp.eq.5) read (line,'(a,f9.4)',err=1260) skip(1:iski),obs
      if (ityp.eq.6) read (line,'(a,f11.5)',err=1260) skip(1:iski),obs
      if (ityp.eq.7) read (line,'(a,f8.4)',err=1260) skip(1:iski),obs
      go to 1261
 1260 obs=-99999.
      write(*,*) line
 1261 continue
      iobs=iobs+1
      if (iobs.gt.nddat) stop ' increase nddat in readobs'
      obse(iobs)=obs
!     write(*,*) iobs,obse(iobs)
c
      go to 1258
 1259 continue
      if (iobs.gt.0.and.iobs.ne.idata)
     *    write(*,*) ' for --> |',begi(1:isto-ista+1),'| only',iobs,
     *               ' points found out of',idata
      end

