
program cbpm_anal

 use precision_def
 use tbt_mod
 use cesr_read_data_mod
 use nr
 use cbpm_interface

 implicit none

 type (tbt_all_data_struct) tbt
 character*120 line, input_file
 character*100 file_name
 character*6 cnumber
 character*3 cbunch,cbpm
 character*60 directory

 integer number/-99999/
 integer ios, i,j,k,lun, lun_avg
 integer gain_correct
 integer dummy
 integer n
 integer highest_x(5), highest_y(5)
 integer numbers(100)/100*0/,m, bunches(100)/100*0/
 integer i_qx, i_qy
 integer nargs
 integer ix
 integer nn
 integer nlow
 integer ntune_x, ntune_y
 integer system
 integer status
 integer n_bpm, fft_window

 real(rp) rms_x, rms_y, ave_x, ave_y
 real(rp), allocatable :: x(:), y(:), fftx(:), ffty(:)
 real(rp) tune_x, tune_y
 real(rp) tunex(5), tuney(5)
 real(rp) fit_xtune(5), fit_ytune(5)
 real(rp) amp_x(1:5,0:120), amp_y(1:5,0:120)
 real(rp) pie
 real(rp) bunch_currents(100)/100*0/

 type bpm_struct 
   real(rp) ave_x,ave_y,rms_x,rms_y, tune_x, tune_y
 end type

 type bpm_bunch_struct
   type(bpm_struct) bpm(0:120)
   real(rp) tune_x_avg, tune_y_avg
 end type
 
 type RD_file_struct
   integer number, bunch, coupling8, coupling9, chromx, chromy
   real(rp) current, bunch_current
 end type

 type (bpm_bunch_struct), allocatable :: bunch(:)
 type (RD_file_struct) RD_files(100)

 logical err, another/.true./
 logical dir_e

 namelist/bpm_data/numbers, bunches, bunch_currents, tune_x, tune_y
     pie = atan(1.)*4

  nargs = cesr_iargc()
  if (nargs == 1) then
    call cesr_getarg(1, input_file)
  else
    input_file = 'input.dat'
    print '(a,$)','  Input file name?  (Default = input.dat)  '
    read(5,'(a)') line
    call string_trim(line, line, ix)
    input_file = line
    if (ix==0) input_file = 'input.dat'
  endif

    open (unit=5, file= input_file, Status='old', IOSTAT=ios)
      print *,' iostat = ',ios
     read(5,nml = bpm_data, IOSTAT=ios)
      print *,' iostat = ',ios
      write(6,nml=bpm_data)
    close(5)

     lun=13
     lun_avg = lunget()
    open(unit=lun_avg, file='average_tunes.dat') 
    write(lun_avg,'(2a12,2a14,a12,a6)')'RD_file','bunch','tune_x_avg', 'tune_y_avg', 'current', 'bunch'

    do m=1,size(RD_files)
  
      RD_files(m)%number = numbers(m)
      RD_files(m)%bunch = bunches(m)
      RD_files(m)%bunch_current = bunch_currents(m)
      if(RD_files(m)%number == 0)cycle

!      call number_to_true_number(number, 'TBT', number,err, .true.) 
      print '(a,2i,es12.4)',' true number = ', RD_files(m)%number, RD_files(m)%bunch, RD_files(m)%bunch_current
      write(21, '(2i,es12.4)') RD_files(m)%number, RD_files(m)%bunch, RD_files(m)%bunch_current
     number = RD_files(m)%number
 
     gain_correct = gain_and_pedestal_correct$ ! can also do pedestal_correct or no_correct

   call tbt_read_data_file(number, gain_correct, .true., tbt, err)
   print '(a,i7)',' Success reading file ',number

     if(err)then
         print '(a,i7)',' error reading file ', number
         stop
     endif

   call tbt_read_data_file(number+1, gain_correct, .false., tbt, err) ! can uncomment to get data from both files
   print '(a,i7)',' Success reading file ',number+1

     if(err)then
         print '(a,i7)',' error reading file ', number+1
         stop
     endif
!this is end of section previously commented - most is just printing stuff
     write(cnumber,'(i5)')number

  allocate(bunch(size(tbt%bunch)))

   directory = trim('RD_'//trim(cnumber))
    status= system('mkdir ' // directory)
    if(status == 0)print '(a20,a)',' create directory = ', trim(directory)

  RD_files(m)%current = tbt%total_current

  do i=1,size(tbt%bunch)

    write(cbunch,'(i3.3)')(tbt%bunch(i)%ix_bunch+1)/2
   print *,cbunch, (tbt%bunch(i)%ix_bunch+1)/2

    !if((tbt%bunch(i)%ix_bunch+1)/2 == RD_files(m)%bunch) &
    !above commented to do full loop
     call svd_tunes(tbt, i, directory)
     !change above to force certain bunch

    
  end do !bunch
  deallocate(bunch)
   end do !another RD file

end
