LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to open *.vtk file in fortran (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-open-%2A-vtk-file-in-fortran-887754/)

parekhharsh_j 06-22-2011 10:51 AM

how to open *.vtk file in fortran
 
Can we open *vtk files in fortran?
If yes,How?
I have written my code as below:
I keep getting error: bad real number in item 1 of list input
Please let me know my mistake
thanks

program work_file
implicit none
integer:: open_status,ln
real::x
open(UNIT=8,FILE="NSSAVE0001.vtk", IOSTAT=open_status, STATUS="old")
if(open_status >0) stop "problem opening the file"
do ln=10,100
read(8,*)x
enddo
close(8)

end program work_file

colucix 06-22-2011 11:03 AM

VTK files have a standard format. The first lines are usually something like
Code:

# vtk DataFile Version 1.0
vtk output
ASCII

ASCII indicates the format used for subsequent data. If it is BINARY, you have to open the file as UNFORMATTED. In any case, the fortran code must read the first lines as strings, whereas your code tries to read floating point data from the beginning. Check what are the first lines of your file, and see if the data are in ASCII or binary format:
Code:

od -c NSSAVE0001.vtk | head -20
or see if it is ASCII or binary using the file command:
Code:

file NSSAVE0001.vtk

parekhharsh_j 06-22-2011 11:15 AM

Quote:

Originally Posted by colucix (Post 4392917)
VTK files have a standard format. The first lines are usually something like
Code:

# vtk DataFile Version 1.0
vtk output
ASCII

ASCII indicates the format used for subsequent data. If it is BINARY, you have to open the file as UNFORMATTED. In any case, the fortran code must read the first lines as strings, whereas your code tries to read floating point data from the beginning. Check what are the first lines of your file, and see if the data are in ASCII or binary format:
Code:

od -c NSSAVE0001.vtk | head -20
or see if it is ASCII or binary using the file command:
Code:

file NSSAVE0001.vtk

Many Thanks for reply
I am interested in getting the numerical values as shown below
But as you said fortran reads the first line as string. How do I go about it?
What modification are required in code?

NSSAVE0001.vtk: ASCII C program text


First Part of NSSAVE0001.vtk would be
# vtk DataFile Version 2.0
TEMPS = 0.2000000E+00 Corps RE=5000 (G8)
ASCII
DATASET STRUCTURED_POINTS
DIMENSIONS 960 320 1
ORIGIN 0.78125E-02 0.78125E-02 0
SPACING 0.15625E-01 0.15625E-01 1
POINT_DATA 307200

SCALARS pression float
LOOKUP_TABLE default
0.28744E-01 0.28713E-01 0.28756E-01 0.28849E-01 0.28926E-01
0.28983E-01 0.29029E-01 0.29066E-01 0.29099E-01 0.29127E-01
0.29152E-01 0.29175E-01 0.29196E-01 0.29215E-01 0.29232E-01
0.29249E-01 0.29264E-01 0.29279E-01 0.29292E-01 0.29305E-01
0.29318E-01 0.29329E-01 0.29341E-01 0.29351E-01 0.29362E-01
0.29371E-01 0.29381E-01 0.29390E-01 0.29399E-01 0.29408E-01
0.29416E-01 0.29424E-01 0.29432E-01 0.29439E-01 0.29447E-01
0.29454E-01 0.29461E-01 0.29468E-01 0.29475E-01 0.29481E-01
0.29488E-01 0.29494E-01 0.29500E-01 0.29506E-01 0.29512E-01
0.29518E-01 0.29524E-01 0.29529E-01 0.29535E-01 0.29540E-01
0.29545E-01 0.29551E-01 0.29556E-01 0.29561E-01 0.29566E-01
0.29571E-01 0.29576E-01 0.29581E-01 0.29586E-01 0.29591E-01
0.29595E-01 0.29600E-01 0.29605E-01 0.29609E-01 0.29614E-01

colucix 06-22-2011 12:01 PM

Well, you have to read the header lines one after one. When you get the DATA POINTS line, read in the number of data points to find out the number of subsequent lines to read. Here is a dummy example:
Code:

PROGRAM work_file
  IMPLICIT NONE
  CHARACTER(len=100) :: dummy
  CHARACTER(len=6) :: dataformat
  CHARACTER(len=20) :: dataset
  INTEGER :: status, np, k, ln
  INTEGER, DIMENSION(3) :: dimensions
  REAL, DIMENSION(2) :: origin, spacing
  REAL, DIMENSION(5) :: x
 
  OPEN(UNIT=8, FILE='NSSAVE0001.vtk', IOSTAT=status, STATUS='old')
 
!  First two lines contain the version number and a title
  READ(8, '(A)') dummy
  READ(8, '(A)') dummy
 
!  The third line contains ASCII or BINARY
  READ(8, '(A)') dataformat
 
!  Fourth line contains the type of dataset
  READ(8, '(A7,1X,A)') dummy, dataset

!  Dimensions
  READ(8, '(A10,1X,I3,1X,I3,1X,I)') dummy, dimensions

!  Origin
  READ(8, '(A6,1X,E11.5,1X,E11.5,1X,I)') dummy, origin, k

!  Spacing
  READ(8, '(A7,1X,E11.5,1X,E11.5,1X,I)') dummy, spacing, k

!  Number of data points
  READ(8, '(A10,1X,I)') dummy, np
  READ(8, '(A)') dummy
 
!  Scalars
  READ(8, '(A)') dummy
 
!  Lookup table
  READ(8, '(A)') dummy
 
!  Finally read data
  DO ln = 1, np
    READ(8, '(E11.5,4(1X,E11.5))' ) x
  END DO
 
  CLOSE(UNIT=8, IOSTAT=status)
 
END PROGRAM work_file

On the other hand if you want to skip a fixed number of lines (that is you're not interested in the header, except the number of data points) you can substitute the first READ statements with a loop:
Code:

  DO k = 1, 7
    READ(8, '(A)') dummy
  END DO

Hope this helps.


All times are GMT -5. The time now is 10:21 AM.