! integer function rmcomments(lu,comcharin,echo) ! ! Read input lines from unit lu. If current line contains the comment ! character comcharin, strip the line from position of comchar to end, ! and write any remaining line to a new unit. If no comment in current ! line, write entire line to new unit. ! Return new unit, rewound (e.g., ready to be read by namelist). ! If echo > 0, echo output lines to stdout. ! If comcharin is ' ', then default comment char is ';' ! ! Args: integer,intent(in) :: lu,echo character(len=1),intent(in) :: comcharin ! Local: character(len=1) :: comchar logical isopen integer :: ios,compos,nline character*80 line ! if (lu <= 0) then write(6,"('>>> rmcomments: bad input lu=',i5)") lu rmcomments = -1 return endif if (len_trim(comcharin) > 0) then comchar = comcharin else comchar = ';' write(6,"('rmcomments: using default semicolon as ', + 'comment character.')") endif inquire(unit=lu,opened=isopen) if (.not.isopen) then open(unit=lu,iostat=ios) if (ios /= 0) then write(6,"('>>> WARNING rmcomments: error openeing input', + ' file with unit lu=',i2,' ios=',i5)") lu,ios rmcomments = -1 return endif endif rmcomments = nextlu() rewind lu nline = 0 read_loop: do line = ' ' read(lu,"(a)",iostat=ios) line if (ios > 0) then write(6,"('>>> rmcomments: error reading from input', + ' unit lu=',i3,' at line ',i5)") lu,nline return endif if (ios < 0) exit read_loop ! eof nline = nline+1 compos = index(line,comchar) if (compos == 0) then write(rmcomments,"(a)") line if (echo > 0) write(6,"(a)") line(1:len_trim(line)) else if (compos > 1) then if (len_trim(line(1:compos-1)) > 0) then write(rmcomments,"(a)") line(1:compos-1) if (echo > 0) write(6,"(a)") line(1:compos-1) endif endif endif enddo read_loop rewind rmcomments return end function rmcomments