Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

progress bar in fortran
dear friends,
as we install a rpm file or install via yum, we saw a progress bar
like

ftnchek-emacs-3.3. 100% |=========================|

is there any way to generate something like that using fortran? where
the progress percentage will update in the same line?

Report this thread to moderator Post Follow-up to this message
Old Post
rudra
04-02-08 01:28 PM


Re: progress bar in fortran
rudra ha scritto:
> is there any way to generate something like that using fortran? where
> the progress percentage will update in the same line?

I dodn't know if this is enough, but you can print a backspace from
fortran, in this way:
write(*,'(a)',advance='no') char(8)
it is possible to print the bar in this way, just use a bit of fantasy.
You can delete the entire line, but you cannot go to the previous line
in this way. Also I don't know how portable it is.



--
Lorenzo `paulatz' Paulatto
Trieste

``Grandissima mi par l'inezia di coloro che vorrebbero che Iddio avesse
fatto l'universo pił proporzionato alla piccola capacitą del lor discorso.''
--Galileo Galilei (Opere VII)

Report this thread to moderator Post Follow-up to this message
Old Post
Lorenzo `paulatz' Paulatto
04-03-08 12:43 AM


Re: progress bar in fortran
In article <d46a483c-164d-4cd8-8f4e-04bb0ca26566@s19g2000prg.googlegroups.co
m>,
rudra  <bnrj.rudra@gmail.com> wrote:
>dear friends,
> as we install a rpm file or install via yum, we saw a progress bar
>like
>
>ftnchek-emacs-3.3. 100% |=========================|
>
>is there any way to generate something like that using fortran? where
>the progress percentage will update in the same line?

This does it, at least with g95, gfortran and Sun Sparc f95, writing 10
asterisks on the same line, one per second. But some compilers don't yet
allow the FLUSH statement, and some that do won't do what's required
without it.

PROGRAM progressbar
! Uses FLUSH (f2003 but not f95) and assumes unit 6 = stdout
DO i=1,10
CALL sleep(1)
WRITE(6,"(A)",ADVANCE='NO')'*'
FLUSH 6
END DO
WRITE(*,*)
END PROGRAM progressbar

-- John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington 6140, New Zealand
e-mail john.harper@vuw.ac.nz phone (+64)(4)463 6780 fax (+64)(4)463 5045

Report this thread to moderator Post Follow-up to this message
Old Post
John Harper
04-03-08 12:43 AM


Re: progress bar in fortran
Lorenzo `paulatz' Paulatto wrote:
> rudra ha scritto: 
>
> I dodn't know if this is enough, but you can print a backspace from
> fortran, in this way:
>    write(*,'(a)',advance='no') char(8)
> it is possible to print the bar in this way, just use a bit of fantasy.
> You can delete the entire line, but you cannot go to the previous line
> in this way. Also I don't know how portable it is.
>
>
>
After the initial write,
1      write(*,'(a)',            )'       -----------------------------'
2      write(*,'(a)',advance='no')'waiting'
you can add to line 2 with something like
3      write(*,'(a)',advance='no')'.'
until you reach the end of the(non-advancing) line 1.

Report this thread to moderator Post Follow-up to this message
Old Post
Sjouke Burry
04-03-08 12:43 AM


Re: progress bar in fortran
> PROGRAM progressbar
> ! Uses FLUSH (f2003 but not f95) and assumes unit 6 = stdout
>   DO i=1,10
>      CALL sleep(1)
>      WRITE(6,"(A)",ADVANCE='NO')'*'
>      FLUSH 6
>   END DO
>   WRITE(*,*)
> END PROGRAM progressbar

I suggest that if you go for Fortran 2003, you do it completely and use
OUTPUT_UNIT of the ISO_FORTRAN_ENV intrinsic module instead of "6". Which
gives:

program progressbar
use iso_fortran_env
do i = 1, 10
call sleep(1) ! SLEEP is not a standard intrinsic, but you will
! replace this with code actually doing something anyway!
write (*,"(a)",advance='no') '*'
flush (output_unit)
end do
write(*,*)
end program progressbar

It still isn't what the OP wanted (ie having the progress percentage
overwriting itself all along). I've tried making TL format descriptors
and non-advancing I/O but can't get anything that works.

--
FX

Report this thread to moderator Post Follow-up to this message
Old Post
FX
04-03-08 12:43 AM


Re: progress bar in fortran
FX ha scritto:
> It still isn't what the OP wanted (ie having the progress percentage
> overwriting itself all along). I've tried making TL format descriptors
> and non-advancing I/O but can't get anything that works.

With TL I always failed too, but the trick with backspace character
char(8) I posted before really works. An example program follow; I have
tried to use flush(6) instead of close(6) and open(6) but it does not
work with ifort 10.


PROGRAM test_bar

character(len=1) :: bar, back
back = char(8)
bar  = '='

! test loop
imax=30
i = 1
call print_bar()
do i = 1,imax
call delete_bar()
call print_bar()
call sleep(1)
enddo
write(6,'(a)') '| done.'
CONTAINS
SUBROUTINE print_bar
! print the percentage and the bar
write(6,'(2x,1i3,1a1,2x,1a1,256a1)', advance='no') &
100*i/imax,'%','|', (bar, k =1,50*i/imax)
close(6)
open(6)
END SUBROUTINE print_bar
SUBROUTINE delete_bar
! delete the bar and the percentage
write(6,'(256a1)', advance='no') (back, k =1,(50*i/imax)+9)
END SUBROUTINE delete_bar
END PROGRAM test_bar

--
Lorenzo `paulatz' Paulatto
Trieste

``Grandissima mi par l'inezia di coloro che vorrebbero che Iddio avesse
fatto l'universo pił proporzionato alla piccola capacitą del lor discorso.''
--Galileo Galilei (Opere VII)

Report this thread to moderator Post Follow-up to this message
Old Post
Lorenzo `paulatz' Paulatto
04-03-08 12:43 AM


Re: progress bar in fortran
In article <ft0v8k$1297$1@nef.ens.fr>, FX <coudert@alussinan.org> wrote:
>
>I suggest that if you go for Fortran 2003, you do it completely and use
>OUTPUT_UNIT of the ISO_FORTRAN_ENV intrinsic module instead of "6". Which
>gives:
>
>program progressbar
>  use iso_fortran_env
>  do i = 1, 10
>     call sleep(1) ! SLEEP is not a standard intrinsic, but you will
>                   ! replace this with code actually doing something anyway
!
>     write (*,"(a)",advance='no') '*'
>     flush (output_unit)
>  end do
>  write(*,*)
>end program progressbar

I had thought of that, but didn't post it myself for two reasons.

1. At present there are no full 2003 compilers but there are many f95
compilers that implement some f2003 features. I wanted to use the
minimum number of such features that could do the job requested.

2. I had tried a version like the above and found that
flush output_unit   didn't work with g95. I now find that
flush (output_unit) does. I have reported the problem to Andy.

Sorry I failed to spot that sleep is non-standard. It seems to be a
language extension that's OK with all 3 compilers I tried.

-- John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington 6140, New Zealand
e-mail john.harper@vuw.ac.nz phone (+64)(4)463 6780 fax (+64)(4)463 5045

Report this thread to moderator Post Follow-up to this message
Old Post
John Harper
04-03-08 03:56 AM


Re: progress bar in fortran
John Harper <harper@mcs.vuw.ac.nz> wrote:

> 2. I had tried a version like the above and found that
> flush output_unit   didn't work with g95. I now find that
> flush (output_unit) does. I have reported the problem to Andy.

I had forgotten that "flush output_unit" was allowed at all (but I just
checked and I see it is supposed to be). Apparently I'm not the only
one. :-)

--
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain

Report this thread to moderator Post Follow-up to this message
Old Post
Richard Maine
04-03-08 03:56 AM


Re: progress bar in fortran
In article <1207186750.632159@bats.mcs.vuw.ac.nz>,
John Harper <harper@mcs.vuw.ac.nz> wrote:
>
>2. I had tried a version like the above and found that
>flush output_unit   didn't work with g95. I now find that
>flush (output_unit) does. I have reported the problem to Andy.

Andy now says he has fixed that bug, only 4 hours after I reported it!

-- John Harper, School of Mathematics, Statistics and Computer Science,
Victoria University, PO Box 600, Wellington 6140, New Zealand
e-mail john.harper@vuw.ac.nz phone (+64)(4)463 6780 fax (+64)(4)463 5045

Report this thread to moderator Post Follow-up to this message
Old Post
John Harper
04-03-08 03:56 AM


Re: progress bar in fortran
In article <d46a483c-164d-4cd8-8f4e-04bb0ca26566@s19g2000prg.googlegroups.co
m>,
rudra  <bnrj.rudra@gmail.com> wrote:
>
>is there any way to generate something like that using fortran? where
>the progress percentage will update in the same line?

Probably non-standard, but writing char(13) writes a carriage-return
which moves the cursor back to the start of the line.

Really crappy somewhat non-standard F77 code follows: works under g77 on
linux and IRIX and IFC on Linux. It uses an external C function
hires_time() to get a double precision time value (secs since the epoch
in our case): I'm sure you can roll your own version, and there may
well be an F90/F95 equivalent.

Not thread safe. Use at own risk. Warranty void if protective sticker
removed. You should really be using something more modern than F77
anyway :).

c       print a 'progress bar' to unit 0.
c	Call with ndone=0 before each bar starts
c	Call with ndone=ntotal to finish
c
subroutine progress(string,ndone,ntotal)
implicit none
character*(*) string
character*255 prog,oldprog
double precision oldtime,hires_time,tl
integer ndone,ntotal,i
save oldprog,oldtime

if (ndone.eq.0) oldtime=hires_time()
tl=hires_time()-oldtime
if (tl.lt.0) tl=0
if (ndone.gt.0) tl=(1.0*ntotal/ndone)*tl-tl

c       When finished, print the total time taken rather
c       than just 00:00!
if (ndone.eq.ntotal) tl=hires_time()-oldtime

write(prog,'(a25,1x,''['')') string
do i=1,40
prog(27+i:27+i)=' '
enddo
write(prog(43:51),'(f7.1,''%'')') 100.0*ndone/ntotal
do i=1,40
if ((1.0*ndone/ntotal).gt.(1.0*i/40)) then
if (prog(27+i:27+i).eq.' ') prog(27+i:27+i)='#'
endif
enddo
prog(67:67)=']'
write(prog(70:72),'(i2.2,'':'')')int(tl/3600)
write(prog(73:75),'(i2.2,'':'')')int((tl-int(tl/3600)*3600)/60)
write(prog(76:77),'(i2.2)')int((tl-int(tl/60)*60))
if (prog.ne.oldprog) write(0,'(a,a,$)') prog(1:77),char(13)
oldprog=prog
if (ndone.eq.ntotal) write(0,*)
return
end


--
Mark Mackey   http://www.swallowtail.org/
code code code code code code code code code code code code code bug code co
de code code code bug code code code code code code code code code code code
code code code code code code code code code code code code code code code c

Report this thread to moderator Post Follow-up to this message
Old Post
Mark Mackey
04-03-08 11:27 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Fortran archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 02:11 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.