program merge_binaries implicit none integer :: nlats, nmlts, ntimes, nlats0, nmlts0, diff integer :: ntimes_total, ierr integer :: nfiles, nfields, nfields0, nlxnm integer :: ntemp, iyr, imo, ida, ihr, imi integer :: i,l,m,n real, dimension(100) :: clats, mlts real :: swv,bx,by,bz,aei,ae,au,al,dsti,dst,hpi,sjh,pot, version real, dimension(10000) :: data character (len=100) files(100), temp, outfile character (len= 30) field character (len= 15) form logical :: IsBinary write(6,*) 'Enter number of output files to merge (100 max):' read(5,*) nfiles if (nfiles <= 1) then write(*,*) "You don't need to run this code." stop endif if (nfiles.gt.100) then nfiles = 100 write(6,*) 'nfiles changed to 100' endif ntimes_total = 0 diff = 0 do n=1,nfiles write(6,*) 'Enter file name of file number ',n,':' read(5,'(A100)') temp files(n) = temp ierr = 0 open(11, file=temp, status='old',form='UNFORMATTED', & iostat=ierr) if (ierr.ne.0) then write(*,*) "Error opening file:", temp stop endif nlats = 0 IsBinary = .true. read(11,iostat=ierr) nlats,nmlts,ntimes if ((ierr.ne.0).or.(nlats.gt.100)) then write(*,*) "Error reading variables nlats, nmlts, ntimes" IsBinary = .false. endif if (IsBinary) then read(11) (clats(i),i=1,nlats) read(11) (mlts(i),i=1,nmlts) read(11) nfields close(11) else close(11) open(11, file=temp, status='old',iostat=ierr) read(11,*) nlats,nmlts,ntimes read(11,*) (clats(i),i=1,nlats) read(11,*) (mlts(i),i=1,nmlts) read(11,*) nfields close(11) endif if (n.eq.1) then nlats0 = nlats nmlts0 = nmlts nfields0 = nfields endif if (nlats-nlats0.ne.0) then write(6,*) 'Mismatch in number of latitude' diff = 1 endif if (nmlts-nmlts.ne.0) then write(6,*) 'Mismatch in number of MLTs' diff = 1 endif if (nfields-nfields.ne.0) then write(6,*) 'Mismatch in number of fields' diff = 1 endif ntimes_total = ntimes_total + ntimes enddo if (diff.gt.0) then write(6,*) 'Discrepencies in files. Can not continue' stop else nlxnm = nlats * nmlts write(form,'(a,i2,a)') '(1p,',nmlts,'e15.7)' write(6,*) 'Enter output file name:' read(5,*) outfile if (IsBinary) then open(12, file=outfile, status='unknown',form='UNFORMATTED') write(12) nlats,nmlts,ntimes_total write(12) (clats(i),i=1,nlats) write(12) (mlts(i),i=1,nmlts) write(12) nfields else open(12, file=outfile, status='unknown') write(12,'(3I4)') nlats,nmlts,ntimes_total write(12,*) (clats(i),i=1,nlats) write(12,*) (mlts(i),i=1,nmlts) write(12,*) nfields endif ntimes_total = 1 do n=1,nfiles write(6,*) 'Reading from file ',files(n) if (IsBinary) then open(11, file=files(n), status='old',form='UNFORMATTED') read(11) nlats,nmlts,ntimes read(11) (clats(i),i=1,nlats) read(11) (mlts(i),i=1,nmlts) read(11) nfields else open(11, file=files(n), status='old') read(11,*) nlats,nmlts,ntimes read(11,*) (clats(i),i=1,nlats) read(11,*) (mlts(i),i=1,nmlts) read(11,*) nfields endif do m=1,nfields if (IsBinary) then read(11) field if (n.eq.1) write(12) field else read(11,'(a)') field if (n.eq.1) write(12,'(a)') field endif enddo do m=1,ntimes if (IsBinary) then read(11) ntemp,iyr,imo,ida,ihr,imi read(11) swv,bx,by,bz,aei,ae,au,al,dsti,dst, & hpi,sjh,pot write(12) ntimes_total,iyr,imo,ida,ihr,imi write(12) swv,bx,by,bz,aei,ae,au,al,dsti,dst, & hpi,sjh,pot ntimes_total = ntimes_total + 1 do l=1,nfields read(11) (data(i),i=1,nlxnm) write(12) (data(i),i=1,nlxnm) enddo else read(11,*) ntemp,iyr,imo,ida,ihr,imi read(11,*) swv,bx,by,bz,aei,ae,au,al,dsti,dst, & hpi,sjh,pot write(12,'(6I6)') ntimes_total,iyr,imo,ida,ihr,imi write(12,'(1p,13e15.7)') swv,bx,by,bz,aei,ae,au,al, & dsti,dst,hpi,sjh,pot ntimes_total = ntimes_total + 1 do l=1,nfields read(11,*) (data(i),i=1,nlxnm) write(12,form) (data(i),i=1,nlxnm) enddo endif enddo if (IsBinary) then read(11,iostat=ierr) Version if (ierr /= 0) Version = 2.0 endif close(11) enddo if (IsBinary) write(12) Version close(12) endif end