Silverfrost Forums

Welcome to our forums

Converting a FORTRAN code than doing manual changes

9 Apr 2013 7:25 #12010

I have got a FORTRAN code which has several .F files.

I need to do some fixed changes to the code. I mean, there are certain common changes that I need to do in all the .F files.

If I do this manually in each file, it is a very time consuming process.

Can I write a code that reads each line from each .f FILE and correspondingly does the necessary changes I intend?

Is there any better way?

10 Apr 2013 7:44 #12017

Yes you can do this but usually it only helps if your task would take more than a few hours to do by hand.

An intermediate approach is to use an editor with a macro facility. It is often possible to record macros for repetitive tasks. I have found that learning how to do this has saved me a lot of time in the longer term.

Both Plato and Visual Studio have macro facilities as do other popular editors.

10 Apr 2013 8:24 #12018

Quoted from PaulLaidler

An intermediate approach is to use an editor with a macro facility. It is often possible to record macros for repetitive tasks. I have found that learning how to do this has saved me a lot of time in the longer term.

Same here...

Could you give us more information about what is needed to be done?

10 Apr 2013 11:52 #12019

I wrote a quick and dirty editor, originally for converting VAX Fortran with 'Structure' to 'Record' type data. It takes a file called 'edlist' which contains a string of up to 40 characters on the odd numbered lines and replaces them with the upto 40 character string on the line below. It also allows you to enter a 40 character 'end string' for replacement, so that if it finds the first 40 character string and it is followed somewhere on the line, both strings are swapped for the new strings on the next edlist line. It is also recursive and once something has been swapped by one of the matched strings, it tries again with the other swapping specifications, but only carries out each swap once for each line. Example of file 'edlist': structure / / type end structure end type record / / type ( ) mystuff. mystuff%

and the program 'Quickedit.for' is as follows:

10 Apr 2013 11:55 #12020
      character*40 swap_lead(10000),swap_tail(10000)
      character*40 swap_lead_new(10000),swap_tail_new(10000)
      character*1000 linein,lineout,line_test,filein
      character*3 new_extension
      logical*2 result
      integer*4 isl(10000),ist(10000),isln(10000),istn(10000)
      integer*4 already_swapped(10000)
      call cmnam(filein)
      call cmnam(new_extension)
      Print *,'Processing ',trim(filein)
      if(filein .eq. ' ')stop
      open(unit=10,file='edlist',status='old')
      ios = 0
      nswap = 0
      nchange = 0
      do while(ios .eq. 0)
        read(10,1000,iostat=ios,end=2222)swap_lead(nswap+1),
     &                                   swap_tail(nswap+1)
        read(10,1000,iostat=ios,end=2222)swap_lead_new(nswap+1),
     &                                   swap_tail_new(nswap+1)
        nswap = nswap + 1
        isl(nswap)  = leng(swap_lead(nswap))
        ist(nswap)  = leng(swap_tail(nswap))
        isln(nswap) = leng(swap_lead_new(nswap))
        istn(nswap) = leng(swap_tail_new(nswap))
        call upcase@(swap_lead(nswap))
        call upcase@(swap_tail(nswap))
        if(isl(nswap) .eq. 0)nswap = nswap - 1
 1000   format(2a)
 2222   continue
      enddo
      close(unit=10)
10 Apr 2013 11:56 #12021

Continued:

c      print *,nswap
c      read(5,*)
      open(unit=10,file=filein,status='old')
      open(unit=11,file='change.out',status='unknown')
      ios = 0
      do while(ios .eq. 0)
        read(10,1000,iostat=ios,end=3333)linein
        lineout = linein
        line_test = linein
        call upcase@(line_test)
        already_swapped = 0
 1111   continue
        do i=1,nswap
          ipos_lead = 0
          ipos_tail = -1
          ipos_lead = index(line_test,swap_lead(i)(1:isl(i)))
          ipos_tail = ipos_lead
          in_bracket=0
          if(ipos_lead .gt. 0 .and. 
     &       swap_lead(i)(isl(i):isl(i)) .eq. '(' .and.
     &       swap_tail(i)(1:1) .eq. ')' )then
c
c bracket match search required
            in_bracket = 1
          endif
          if(ipos_lead .gt. 0)then
            if(in_bracket .eq. 0)then
c
c normal
              if(swap_tail(i) .ne. ' ')then
                ipos_tail = index(line_test(ipos_lead+isl(i):),
     &                          swap_tail(i)(1:ist(i)))
                if(ipos_tail .gt. 0)then
                  ipos_tail = ipos_tail+ipos_lead+isl(i)-1
                endif
              endif
            else
10 Apr 2013 11:57 #12022

Continued:

              ipos_tail = ipos_lead
              do ii=ipos_lead+isl(i),leng(line_test)
                if(line_test(ii:ii) .eq. '(')in_bracket=in_bracket+1
                if(line_test(ii:ii) .eq. ')')in_bracket=in_bracket-1
                if(in_bracket .eq. 0)then
c
c reached terminating string
                  ipos_tail = ii
                  goto 3344
                endif
              enddo
 3344         continue
            endif
          endif
c          print *,ipos_lead,ipos_tail,i,already_swapped(i)
c          print *,trim(linein)
          if(already_swapped(i) .eq.  0)then
            if(ipos_lead .gt. 0 .and. ipos_tail .gt. 0)then
              if(ipos_tail .ge. ipos_lead+isl(i) )then
c
c found a candidate for a change
                lineout = linein(:ipos_lead-1)//
     &                    swap_lead_new(i)(:isln(i))//
     &                    linein(ipos_lead+isl(i):ipos_tail-1)//
     &                    swap_tail_new(i)(:istn(i))//
     &                    linein(ipos_tail+ist(i):)
                linein=lineout
                line_test = linein
                call upcase@(line_test)
                nchange = nchange + 1
                already_swapped(i) = 1
                goto 1111
              else
                lineout = linein(:ipos_lead-1)//
     &                    swap_lead_new(i)(:isln(i))//
     &                    linein(ipos_lead+isl(i):)
                linein=lineout
                line_test = linein
                call upcase@(line_test)
                nchange = nchange + 1
                already_swapped(i) = 1
                goto 1111
              endif
            endif
          endif
        enddo
        write(11,1000)trim(lineout)
      enddo
 3333 continue
      close(unit=10)
10 Apr 2013 12:01 #12023

Continued:

      if(new_extension .ne. ' ')then
        call set_suffix@(filein,new_extension,result)
      endif
      open(unit=10,file=filein,status='unknown')
      rewind(unit=11)
      ios = 0
      do while(ios .eq. 0)
        read(11,1000,iostat=ios,end=4444)linein 
c        il = leng(linein)
c        if(il .gt. 0)then
          write(10,1000)trim(linein)
c        else
c          write(10,1020)
c 1020     format()
c        endif
 4444   continue
      enddo
      close(unit=10)
      close(unit=11,status='delete')
      print *,nchange,' changes'
      end

Run from the command line as: quickedit oldfile.f for This will output a new file with the extension '.for'. Original file 'oldfile.f' will be read and the edits made. output will be 'oldfile.for' If the new file extension is omitted then the original file will be overwritten. No responsibility taken for any destruction and havoc it may wreak! Ian

10 Apr 2013 4:20 #12031

I would just use VIM, you can do pretty much anything in VIM once you have learnt it.

If I had many files to convert I would probably do something in Python.

Please login to reply.