Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

didi i made a mistake,binary file?

Status
Not open for further replies.

mikibelavista

Technical User
Jan 18, 2012
32
I have modified one code,guy creates like this input file:
integer*2 veli(nxmax)
open(unit=36, file='vel.mod', form='unformatted')
do 130 k=1,nz
do 130 j=1,ny
do 131 i=1,nx
131 veli(i)=vel(i,j,k)*1000.
130 write(36) (veli(i),i=1,nx)
c
stop
My code ::
program newbuild

integer nmax,i
real*4,dimension(61) :: n1,n2,n3,n4,n5,n6,n7
integer*2,allocatable :: b1:)),b2:)),b3:)),b4:)),b5:)),b6:)),b7:))

open(15,file='nodev1.dat',status='old')
open(16,file='nodev2.dat',status='old')
open(17,file='nodev3.dat',status='old')
open(18,file='nodev4.dat',status='old')
open(19,file='nodev5.dat',status='old')
open(20,file='nodev6.dat',status='old')
open(21,file='nodev7.dat',status='old')

read(15,*)n1
read(16,*)n2
read(17,*)n3
read(18,*)n4
read(19,*)n5
read(20,*)n6
read(21,*)n7


nmax=61
allocate(b1(nmax))
allocate(b2(nmax))
allocate(b3(nmax))
allocate(b4(nmax))
allocate(b5(nmax))
allocate(b6(nmax))
allocate(b7(nmax))

open(36,file='vel.mod',form='unformatted')
b1=n1
b2=n2
b3=n3
b4=n4
b5=n5
b6=n6
b7=n7

write(36)b1
write(36)b2
write(36)b3
write(36)b4
write(36)b5
write(36)b6
write(36)b7

end program
In my case nx=61 and nz=7 and ny=1
 
You did make a binary file but whether it is a mistake is debatable. What do you want to do with vel.mod?

You don't really need the labels from F77 onwards. The loops can be written as
Code:
      do k=1,nz
         do 130 j=1,ny
            do 131 i=1,nx
               veli(i)=vel(i,j,k)*1000.
            enddo
            write(36) (veli(i),i=1,nx)
         enddo
      enddo
It takes up a few more lines but it is easier to debug.
 
Ok,I will explain.I have velocities at every node at mesh,mesh is 2d.So I have written my code to create input file.I am giving part of the next code how he reads vel.mod:
integer*2 ivel(nxmax,nymax,nzmax)
do 20 k=1,nz
do 20 j=1,ny
read(10) (ivel(i,j,k),i=1,nx)
write(*,*)ivel
20 continue

In my programme I have 61x7 velocities and they are written in 7 data files.Then I think that I have problem with reading this binary from vel.mod and with further calculations.
 
... and what is your problem ?
If you want to have some support from us, please give us a chanve and explain, what goes wrong in the first place. Your code offers a lot of opportunity to improvement. But what is your trouble ? Do you receive compiler errors, linker errors, runtime errors (then there is a message involved that would be illuminating on the source of the error) ? Or is it that your program just does not do what you want it to do ? Then give a clue as to what you want to do and what you achieved.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Ok, I have problem with later results.I have so far used only od -h to check vel.mod file.
My question is for example if my programme can be written somehow in different way using do loops but to read these 7 data files?
 
Just to add od -h out put for both codes::
milenko@milenkons:~/fg77/f05k/bin$ od -h vel.mod
0000000 007a 0000 1388 1388 1388 1388 1388 1388
0000020 1388 1388 1388 1388 1388 1388 1388 1388
*
0000160 1388 1388 1388 1388 1388 1388 1388 007a
0000200 0000 007a 0000 141e 141e 141e 141e 141e
0000220 141e 141e 141e 141e 141e 141e 141e 141e
*
0000400 007a 0000 007a 0000 14b4 14b4 14b4 14b4
0000420 14b4 14b4 14b4 14b4 14b4 14b4 14b4 14b4
*
0000600 14b4 007a 0000 007a 0000 1549 1549 1549
0000620 1549 1549 1549 1549 1549 1549 1549 1549
*
0001000 1549 1549 007a 0000 007a 0000 15df 15df
0001020 15df 15df 15df 15df 15df 15df 15df 15df
*
0001200 15df 15df 15df 007a 0000 007a 0000 16a8
0001220 16a8 16a8 16a8 16a8 16a8 16a8 16a8 16a8
*
0001400 16a8 16a8 16a8 16a8 007a 0000 007a 0000
0001420 1770 1770 1770 1770 1770 1770 1770 1770
*
0001600 1770 1770 1770 1770 1770 007a 0000
0001616
milenko@milenkons:~/fg77/f05k/bin$ ./newbuild
milenko@milenkons:~/fg77/f05k/bin$ od -h vel.mod
0000000 007a 0000 1388 1388 1388 1388 1388 1388
0000020 1388 1388 1388 1388 1388 1388 1388 1388
*
0000160 1388 1388 1388 1388 1388 1388 1388 007a
0000200 0000 007a 0000 141e 141e 141e 141e 141e
0000220 141e 141e 141e 141e 141e 141e 141e 141e
*
0000400 007a 0000 007a 0000 15e0 14b4 14b4 14b4
0000420 14b4 14b4 14b4 14b4 14b4 14b4 14b4 14b4
*
0000600 14b4 007a 0000 007a 0000 154a 154a 154a
0000620 154a 154a 154a 154a 154a 154a 154a 154a
*
0001000 154a 154a 007a 0000 007a 0000 15e0 15e0
0001020 15e0 15e0 15e0 15e0 15e0 15e0 15e0 15e0
*
0001200 15e0 15e0 15e0 007a 0000 007a 0000 16a8
0001220 16a8 16a8 16a8 16a8 16a8 16a8 16a8 16a8
*
0001400 16a8 16a8 16a8 16a8 007a 0000 007a 0000
0001420 1770 1770 1770 1770 1770 1770 1770 1770
*
0001600 1770 1770 1770 1770 1770 007a 0000
0001616
 
Of course there are more simple ways to read your files and to bring the data into b1 ... b7.

But I am somewhat confused on your data:

Your read your files nodev1 ... nodev7 in formatted real*4 input (32 bit).
You then copy them to integer*2 variables b1 ... b7 (16 bit)
Then you save b1 ... b7 to a binary file.

Is that really what you want to do, especially purging real*4 to integer*2 ?

Norbert



The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Yes,I want to convert velocities into integer*2.Please,suggest some simple solutions.
 
Code:
program newbuild_2
implicit none                       ! no better way to catch typos
integer, parameter :: nmax = 61     ! for better maintenance declare only once
real*4 :: n(nmax)
integer*2 :: b(nmax)
integer :: j                        ! loop counter
integer :: iError                   ! error indicator
character*10 :: cFile               ! filename

open (unit = 36, file = 'vel.mod', form = 'unformatted')

do j = 1, 7
    write (cFile, '(''nodev'',i1,''.dat'')') j
    open (unit = 15, file = cfile, status = 'old', iostat = iError)
    if (iError .eq. 0) read (15, *, iostat = iError) n
    if (iError .eq. 0) then
        b = nint (n)
        write (36) b
    else
        write (*,*) 'error processing file  ', cFile
        exit
    endif
    close (unit = 15)
enddo

close (unit = 36)

end

That is how I would do it, but every programmer has developed his own style over the years.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top