- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have the following file format to read in:
PIXEL_SIZE = 1 mm THICKNESS_SLICES = 1 mm NUMBER_OF_SLICES = 101 IMAGE_DIMENSIONS = 57X57 pixels SLICES;SLICE Nr 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 1..
So I opened the file with OPEN(UNIT=file_id, FILE=filename, ACTION='READ', iostat=iErr)
I read out the image dimenstions, with read(file_id,*, iostat=iErr) line,dummy,Vimage%pixel_spacing(1),dummy2
...
Finally I would like to read out the image, as a sequence of 57X57 0,1
however, the normal rad call reads an entire line, not a single sign.
How can I manage this.
I think the read/write IO and formatting in Fortran is somewhat complicated !!
PIXEL_SIZE = 1 mm THICKNESS_SLICES = 1 mm NUMBER_OF_SLICES = 101 IMAGE_DIMENSIONS = 57X57 pixels SLICES;SLICE Nr 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 1..
So I opened the file with OPEN(UNIT=file_id, FILE=filename, ACTION='READ', iostat=iErr)
I read out the image dimenstions, with read(file_id,*, iostat=iErr) line,dummy,Vimage%pixel_spacing(1),dummy2
...
Finally I would like to read out the image, as a sequence of 57X57 0,1
however, the normal rad call reads an entire line, not a single sign.
How can I manage this.
I think the read/write IO and formatting in Fortran is somewhat complicated !!
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Try reading the entire line into a character array
character(1024) :: InputLine
...
10 continue
read(iouin, 100, end = 999) InputLine
100 format(A1024)
if(InputLine .eq. ' ') goto 10 ! ignore blank line
! PIXEL_SIZE = 1 mm THICKNESS_SLICES = 1 mm NUMBER_OF_SLICES = 101 IMAGE_DIMENSIONS = 57X57 pixels SLICES;SLICE Nr 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 1..
Pixel_Size_Arg = RINDEX(InputLine, 'PIXEL_SIZE = ')
if(Pixel_Size_Arg .eq. 0) goto 888 ! error
read(InputLine(Pixel_Size_Arg:), *)Pixel_Size
...
SlicePos = RINDEX(InputLine, 'SLICE Nr ')
if(SlicePos .eq. 0) goto 888 ! error
InputLine = ADJUSTL(InputLine(SlicePos:))
do I=1,nPixels
read(InputLine,*) Pixel(I)
InputLine = ADJUSTL(InputLine(2:))
end do
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - jimdempseyatthecove
Try reading the entire line into a character array
character(1024) :: InputLine
...
10 continue
read(iouin, 100, end = 999) InputLine
100 format(A1024)
if(InputLine .eq. ' ') goto 10 ! ignore blank line
! PIXEL_SIZE = 1 mm THICKNESS_SLICES = 1 mm NUMBER_OF_SLICES = 101 IMAGE_DIMENSIONS = 57X57 pixels SLICES;SLICE Nr 1 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 1..
Pixel_Size_Arg = RINDEX(InputLine, 'PIXEL_SIZE = ')
if(Pixel_Size_Arg .eq. 0) goto 888 ! error
read(InputLine(Pixel_Size_Arg:), *)Pixel_Size
...
SlicePos = RINDEX(InputLine, 'SLICE Nr ')
if(SlicePos .eq. 0) goto 888 ! error
InputLine = ADJUSTL(InputLine(SlicePos:))
do I=1,nPixels
read(InputLine,*) Pixel(I)
InputLine = ADJUSTL(InputLine(2:))
end do
Jim Dempsey
Thanks Jim,
I had that kind of solution in mind, but I hoped, there is an easier (more comfortable) way to do that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well, if you use fixed column input and punched cards you might have better luck.
Kidding asside
If you do know that the input file has fixed field lengths you can declare a type with union
Example:
[cpp] ! Variables type TypeInput union map character(132) :: InputLine end map map character :: Blank1 character(6) :: AnnealTether end map map character(4) :: I4Field character(1) :: ws1 character(16):: Value character(1) :: ws2 character(110):: text end map map character :: Blank2 character(13) :: DensityReset character(16):: DensityResetValue end map map character :: Blank3 character(24) :: AdjustingBallastObject character(3) :: AdjustingBallastObjectNumber character(3) :: discard character(20):: AdjustingBallastObjectBallast end map end union end type TypeInput [/cpp]
and declare an instance of the type
type(TypeInput) :: theInput
and in the program
[cpp] READ(IOUIN, 100, end=999) theInput.InputLine 100 FORMAT(A132) if(theInput.DensityReset .eq. 'Reset Density') then ResetDensity = .true. READ(theInput.DensityResetValue, 201) ResetDensityValue 201 FORMAT(E9.6) endif [/cpp]
etc...
Read the text from the file once, test data for what you expect, use read from variable to convert text to number.
Jim Dempsey

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page