Re: Challenge: reading ascii data
- From: "David Frank" <dave_frank@xxxxxxxxxxx>
- Date: Thu, 8 Nov 2007 08:32:15 -0500
"David Frank" <dave_frank@xxxxxxxxxxx> wrote in message
news:13j5uvlp26i9i79@xxxxxxxxxxxxxxxxxxxxx
I have a Fortran solution to this problem and challenge the
AWK/RUBY/PLI/ETC advocates to post their solutions that
can process the U,V records below -> internal arrays and then output UV
product records in format requested by
original (homework?) assignment.
Exception: no UV record is to be output unless both U,V, 3d indexed
records exist as positive non-zero weather
data..
I will post my Fortran code once someone posts a non-Fortran solution that
matches the UV output format shown.
As I dont really expect my challenge to be met, here is my solution using a
type variable to simplify things.
! -----------------------
program demo ! input array processing
implicit none
integer :: i, i1,i2,i3, n,ntimes
real :: u, v
character(80) :: dat
type time_uv
integer :: time
real :: u(3,3,3), v(3,3,3)
end type
type (time_uv),allocatable :: tuv(:)
open (11,file='demo.in')
ntimes = 0
do ! count time frames
read (11,'(a)',end=101) dat
if (dat(1:4) == 'time') ntimes = ntimes+1
end do
101 rewind (11)
allocate ( tuv(ntimes) )
do n = 1,ntimes
tuv(n)%u = 0 ; tuv(n)%v = 0 ! positive non-zero inputs expected
do
read (11,'(a)') dat
i = index(dat,':')
if (i > 0) exit ! assume time: xxxx minutes record
end do
read (dat(6:),*) tuv(n)%time ! get time
do
read (11,'(a)',end=102) dat
if (dat(1:4) == 'time') then
backspace(11) ; exit ! go back and process time record
end if
i = index(dat,'(') ; if (i == 0) cycle
read (dat(i+1:),*) i1,i2,i3
i = index(dat,'=')
if (dat(1:1) == 'U') then
read (dat(i+2:),*) tuv(n)%u(i1,i2,i3)
else if (dat(1:1) == 'V') then
read (dat(i+2:),*) tuv(n)%v(i1,i2,i3)
end if
end do
end do ! 1:ntimes
102 close (11)
open (22,file='demo.out')
do n = 1,ntimes ! output time frames time,u,v,uv from arrays
write (22,90) 'time: ',tuv(n)%time,' minutes'
do 1 i1 = 1,3
do 1 i2 = 1,3
do 1 i3 = 1,3
u = tuv(n)%u(i1,i2,i3)
if (u == 0) cycle ! U value not avail
write (22,91) 'U(',i1,i2,i3, u
1 end do
do 2 i1 = 1,3
do 2 i2 = 1,3
do 2 i3 = 1,3
v = tuv(n)%v(i1,i2,i3)
if (v == 0) cycle ! V value not avail
write (22,91) 'V(',i1,i2,i3, v
2 end do
do 3 i1 = 1,3
do 3 i2 = 1,3
do 3 i3 = 1,3
u = tuv(n)%u(i1,i2,i3) ; v = tuv(n)%v(i1,i2,i3)
if (u*v > 0) write (22,92) 'UV(',i1,i2,i3, u*v
3 end do
write (22,*) ! blank record between time frames
end do
90 format (a,i0.2,a)
91 format (a,2(i0,','),i0,') = ',f0.2)
92 format (a,2(i0,','),i0,') = ',f0.3)
end program
Above program outputs:
time: 00 minutes
U(1,1,1) = 12.34
U(1,1,2) = 10.00
U(1,1,3) = 11.01
U(1,2,1) = 10.05
U(1,2,2) = 12.40
U(1,2,3) = 11.20
U(1,3,1) = 12.80
U(1,3,2) = 10.30
U(1,3,3) = 11.25
V(1,1,1) = 11.40
V(1,1,2) = 12.00
V(1,1,3) = 13.50
V(1,2,1) = 11.00
V(1,2,2) = 11.70
V(1,2,3) = 11.25
V(1,3,1) = 11.50
V(1,3,2) = 10.60
UV(1,1,1) = 140.676
UV(1,1,2) = 120.000
UV(1,1,3) = 148.635
UV(1,2,1) = 110.550
UV(1,2,2) = 145.080
UV(1,2,3) = 126.000
UV(1,3,1) = 147.200
UV(1,3,2) = 109.180
time: 30 minutes
U(1,1,1) = 13.30
U(1,1,2) = 11.00
U(1,1,3) = 11.30
U(1,2,1) = 10.00
U(1,2,2) = 12.30
U(1,2,3) = 10.10
U(1,3,1) = 10.90
U(1,3,2) = 11.40
U(1,3,3) = 11.75
V(1,1,1) = 12.40
V(1,1,2) = 11.00
V(1,1,3) = 11.60
V(1,2,1) = 11.20
V(1,2,2) = 11.90
V(1,2,3) = 11.35
V(1,3,1) = 12.50
V(1,3,2) = 11.60
V(1,3,3) = 13.20
UV(1,1,1) = 164.920
UV(1,1,2) = 121.000
UV(1,1,3) = 131.080
UV(1,2,1) = 112.000
UV(1,2,2) = 146.370
UV(1,2,3) = 114.635
UV(1,3,1) = 136.250
UV(1,3,2) = 132.240
UV(1,3,3) = 155.100
.
- Follow-Ups:
- Re: Challenge: reading ascii data
- From: Rich Townsend
- Re: Challenge: reading ascii data
- From: robin
- Re: Challenge: reading ascii data
- References:
- Challenge: reading ascii data
- From: David Frank
- Challenge: reading ascii data
- Prev by Date: Re: dynamic allocation doubt
- Next by Date: Re: Implicit none with IVF
- Previous by thread: Challenge: reading ascii data
- Next by thread: Re: Challenge: reading ascii data
- Index(es):
Relevant Pages
|
Loading