program combine_rd_files use sim_utils use compile_interface implicit none type bpm_struct real(rp) gains(4), chis logical good end type bpm_struct type file_struct type (bpm_struct) bpm(0:120) character*100 file_name end type file_struct type (file_struct), allocatable :: rdfiles(:) type (file_struct) bpm_avg, bpm_sqr, chis integer lun integer ios integer, allocatable :: count(:) integer n,i,j,ix integer in_end, ind_beg character*300 string, file_name(20) real(rp), allocatable :: column(:) lun=lunget() open(unit=lun,file='RDfile_list.dat') n=0 do while(.true.) read(lun,'(a)',IOSTAT=ios)string if(ios < 0 )exit if(index(string(1:3),'!')/=0)cycle in_end=index(string,'dat_out') if(in_end/=0)then n=n+1 ind_beg = index(string,'=') file_name(n)=string(ind_beg+2:in_end+6) endif end do close(lun) allocate(rdfiles(1:n)) do j=1,120 rdfiles(1:n)%bpm(j)%good = .false. end do do i=1,n lun = lunget() open(unit=lun,file = file_name(i)) do while(.true.) read(lun,'(a)', IOSTAT=ios)string if(index(string,'BPM')/=0)exit enddo do while(.true.) read(lun,'(a)',IOSTAT=ios)string if(ios < 0)exit call get_column_data(string, column) rdfiles(i)%file_name = file_name(i) ix = column(1) rdfiles(i)%bpm(ix)%good = .true. rdfiles(i)%bpm(ix)%gains(1:4) = column(8:11) rdfiles(i)%bpm(ix)%chis = column(13) end do close(unit=lun) end do do j=1,4 do ix=1,120 if(.not. rdfiles(1)%bpm(ix)%good)cycle ! if(any(rdfiles(1:n)%bpm(ix)%chis > 1.e-6))cycle bpm_avg%bpm(ix)%gains(j) = sum(rdfiles(1:n)%bpm(ix)%gains(j))/n bpm_sqr%bpm(ix)%gains(j) = sum((rdfiles(1:n)%bpm(ix)%gains(j))**2) chis%bpm(ix)%gains(j) = bpm_sqr%bpm(ix)%gains(j) - n* (bpm_avg%bpm(ix)%gains(j))**2 end do end do lun = lunget() open(unit = lun, file = 'gain_summary.dat' ) do ix=1,120 if(.not. rdfiles(1)%bpm(ix)%good)cycle write(lun,'(i10,4es12.4, 4es12.4)')ix, bpm_avg%bpm(ix)%gains(1:4), sqrt(chis%bpm(ix)%gains(1:4)/n) end do close(lun) end program combine_rd_files