program sum_multi C C adds the data from different columns of (possibly different) C (X,Y1,Y2,...) files, taken with (possibly different) weights. C Checks that the X sequence be the same in all files involved. C Ignores lines beginning with #. C A possible use: summing up DOS sequences from WIEN2k or Siesta. C C written by Andrei Postnikov, postnikov@univ-metz.fr C implicit none integer ifi,nfi,npts,ifn,ipts,ipt,iline,ii parameter (nfi=10,npts=5000) integer ncol(nfi) character*60 fname(nfi),fsum,fnam character*256 string character*1 lab double precision xx(npts),xread,yy(npts,nfi), . weight(nfi),small,yres,dum data small/1.0d-06/ ifi=0 101 ifi=ifi+1 if (ifi.gt.nfi) then print *,' Number of input files exceeds nfi=',nfi stop endif print *,' File name to add, if any, or Q if that was all:' read *, fname(ifi) fnam=fname(ifi) if (fnam(1:len_trim(fnam)).ne.'Q') then C --- hopefully a regular file name... open (11,file=fnam,status='old',form='formatted',err=106) 102 print *,' Add data from column Nr:' read *, ncol(ifi) if (ncol(ifi).eq.1) then print *,' First column contains argument values!' goto 102 elseif (ncol(ifi).lt.1) then print *,' Illegal column number!' goto 102 endif C --- Now read all data from this column into an array iline=0 ipts=0 C --- read new line of data... 103 continue read (11,'(a256)',end=104) string lab=string(1:1) ! 1st char in the line if (string(1:1).eq.'#') then iline=iline+1 goto 103 else iline=iline+1 ipts=ipts+1 if (ipts.gt.npts) then print *,' Number of data lines exceeds npts=',npts stop endif read (string,*,err=107,end=104) xread, . (dum,ii=1,ncol(ifi)-2),yy(ipts,ifi) if (ifi.eq.1) then C store xread values into a reference array, C to control these equivalence in all other arrays: xx(ipts)=xread else C check the consistency of argument values if (abs(xread-xx(ipts)).gt.small) then print *,' Different argument value: line ',iline, . ' of file ',fname(ifi) print *, xread,' differs from ',xx(ipts) stop endif endif ! if (ifi.eq.1) goto 103 endif ! if (string(1:1).eq.'#') 104 continue C regular end of file print *,' Read in ',ipts,' data points.' print *,' Take them with the weight :' read *, weight(ifi) close (11) goto 101 else C end of filename list ifn=ifi-1 endif ! if (fnam(1:len_trim(fnam)).ne.'Q') C C The list of 'ifn' files is through. C Assemble the data into output file: 105 print *,' name of the RESULTING file, to be open as NEW:' read *, fsum open (12,file=fsum,status='new',form='formatted',err=105) write (12, 301) do ifi=1,ifn fnam=fname(ifi) write (12,302,advance="no") ncol(ifi),weight(ifi) do ii=1,len_trim(fnam) write (12,303,advance="no") fnam(ii:ii) enddo write (12, 304) enddo write (12, 301) do ipt=1,ipts yres = 0.d0 do ifi=1,ifn yres = yres + yy(ipt,ifi)*weight(ifi) enddo write (12,'(2f16.8)') xx(ipt),yres enddo close (12) stop 106 print *, 'Error opening as old formatted' ifi=ifi-1 goto 101 107 print *,'Error reading file ',fname(ifi),' line ',iline, . ' column ',ncol(ifi) stop 301 format('#') 302 format('# adding data from column ',i2,' (weighted ',1pe10.4, . '), of ') 303 format(a1) 304 format() end