subroutine opt_out (in_file, out_file, vari, value) use bmad use constraints_mod implicit none type (indep_var_struct) vari real*8 value(*) integer i, j, n, ix, i_sc, ix_sc, ip, ip_tot, ix_in, ix_out integer iu0, iu1, iu2, ix_ex, ix1, ix2 integer ixv, ixe character*(*) in_file, out_file ! character*132 line, line_in, line_out, string character*200 line, line_in, line_out, string character*40 ele_name character*16 val_name logical :: init_needed = .true., var_located(1000) logical need_another_line, check_vars, unlocated_exist logical :: first_time_through = .true. type ptr_struct integer n_line, n_command, n_var logical overlay end type ptr_struct type (ptr_struct) ptr(0:1000) save iu0, iu1, ptr, ip_tot, unlocated_exist, var_located ! parser file if (init_needed) then iu0 = lunget() open (unit = iu0, file = in_file, status = 'old', action='read') iu1 = lunget() open (unit = iu1, status = 'scratch') ip = 0 ! number of variables located n = 0 ! line number count ptr(0)%n_line = 0 ptr(0)%n_command = 0 need_another_line = .true. do j = 1, vari%n_var var_located(j) = .false. ! not yet enddo do while (.true.) if (need_another_line) then 100 read (iu0, '(a)', end = 1000) line write (iu1, '(a)') line n = n + 1 ! line number ix = index(line, '!') if (ix == 1) goto 100 ! read again if a comment line if (ix > 1) line = line(:ix-1) ! strip off comment i_sc = 1 ! command count on this line else line = line(ix_sc+1:) ! strip off old command i_sc = i_sc + 1 ! next command on this line endif ix_sc = index(line, ';') if (ix_sc == 0) then need_another_line = .true. string = line else need_another_line = .false. string = line(:ix_sc) endif check_vars = .false. call str_upcase (string, string) ix1 = index(string, '[') if (ix1 /= 0) then ix2 = index(string, ']') call string_trim (string(:ix1-1), ele_name, ix) val_name = string(ix1+1:ix2-1) ptr(ip+1)%overlay = .false. check_vars = .true. elseif (index(string, 'OVERLAY') /= 0) then ! overlay ? ix1 = index(string, ':') call string_trim (string(:ix1-1), ele_name, ix) do ix1 = index(string, '}') if (ix1 /= 0) exit if (index(string, '&') == 0) then print *, 'ERROR IN OPT_OUT: CANNOT FIND "}" OR "&" FOR AN OVERLAY!' call err_exit endif read (iu0, '(a)', end = 1000) string ! write (iu1, '(a)') line write (iu1, '(a)') string n = n + 1 enddo string = string(ix1:) ix1 = index(string, ',') string = string(ix1+1:) ix2 = index(string, '=') call string_trim(string(:ix2-1), val_name, ix) ptr(ip+1)%overlay = .true. check_vars = .true. endif if (check_vars) then do j = 1, vari%n_var if (vari%v(j)%ele_name == ele_name .and. & vari%v(j)%val_name == val_name) then ip = ip + 1 ptr(ip)%n_line = n ptr(ip)%n_command = i_sc ptr(ip)%n_var = j var_located(j) = .true. goto 500 endif enddo 500 continue endif enddo 1000 continue ip_tot = ip close (unit = iu0) unlocated_exist = .false. do i = 1, vari%n_var if (.not. var_located(i)) unlocated_exist = .true. enddo init_needed = .false. endif ! write the results to a new file rewind (unit = iu1) iu2 = lunget() if(first_time_through)then open (unit = iu2, file = out_file) first_time_through = .false. else open (unit = iu2, file = out_file, status = 'old') endif do ip = 1, ip_tot do i = ptr(ip-1)%n_line+1, ptr(ip)%n_line-1 read (iu1, '(a)') line write (iu2, '(a)') trim(line) enddo if (ptr(ip)%n_line > ptr(ip-1)%n_line) then read (iu1, '(a)') line_in i_sc = ptr(ip)%n_command ix_in = 1 ix_out = 1 else i_sc = ptr(ip)%n_command - ptr(ip-1)%n_command endif do i = 1, i_sc-1 ix_sc = index (line_in(ix_in:), ';') line_out(ix_out:) = line_in(ix_in:ix_in+ix_sc-1) ix_in = ix_in + ix_sc ix_out = ix_out + ix_sc enddo if (ptr(ip)%overlay) then ix1 = index (line_in(ix_in:), '}') ix2 = index (line_in(ix_in+ix1:), '=') ! ix = ix1 + ix2 - 1 ix = ix1 + ix2 else ix = index(line_in(ix_in:), ':=') + 1 ! old style if (ix == 1) ix = index(line_in(ix_in:), '=') ! new style endif if (ix <= 0) then print *, 'ERROR IN OPT_OUT: Could not process this line: ' print *, line call err_exit endif line_out(ix_out:) = line_in(ix_in:ix_in+ix) ix_in = ix_in + ix ix_out = ix_out + ix ! ix_out = ix_out + ix + 1 ! ix_in = ix_in + ix + 1 j = ptr(ip)%n_var write (line_out(ix_out:), '(es22.10)') value(j) ix_out = ix_out + 23 ! ix_out = ix_out + 14 ix_ex = index(line_in(ix_in:), '!') ix_sc = index(line_in(ix_in:), ';') if (ix_ex /= 0 .or. ix_sc /= 0) then if (ix_ex /= 0 .and. ix_sc /= 0) then ix = min(ix_ex, ix_sc) else ix = max(ix_ex, ix_sc) endif line_out(ix_out:) = line_in(ix_in+ix-1:) ix_in = ix_in + ix ix_out = ix_out + ix endif if (ptr(ip+1)%n_line /= ptr(ip)%n_line) then write (iu2, '(a)') trim(line_out) endif enddo do while (.true.) read (iu1, '(a)', end = 2000) line write (iu2, '(a)') trim(line) enddo 2000 continue ! if (unlocated_exist) then write (iu2, *) write (iu2, *) '! Vars not in original file' write (iu2, *) do j = 1, vari%n_var if (.not. var_located(j)) then write (iu2, '(4a, g20.8)') trim(vari%v(j)%ele_name), '[', & trim(vari%v(j)%val_name), '] =', value(j) endif enddo endif ! close(iu2) end subroutine opt_out