LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-22-2011, 10:51 AM   #1
parekhharsh_j
LQ Newbie
 
Registered: Jun 2011
Posts: 20

Rep: Reputation: Disabled
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
 
Old 06-22-2011, 11:03 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
1 members found this post helpful.
Old 06-22-2011, 11:15 AM   #3
parekhharsh_j
LQ Newbie
 
Registered: Jun 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
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
 
Old 06-22-2011, 12:01 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.

Last edited by colucix; 06-22-2011 at 12:02 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
3D array in MAT file ->DAT file to read in FORTRAN BrandonPossible Programming 2 07-03-2009 01:14 AM
Error while installing VTK on suse 10.2. Compilation stops at 81% vsravani Linux - Software 4 05-06-2009 09:45 PM
c++ vtk code compiling from terminal njac Programming 3 05-07-2008 10:11 PM
g77 in gcc 4.1.0 not found only gfortran fortran 95 compiler! I need fortran 77. TheBrick Linux - Software 3 07-04-2007 06:39 AM
LXer: Sun's Fortran replacement goes open-source LXer Syndicated Linux News 0 01-16-2007 01:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration