Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

reading file

jaeger0
Beginner
830 Views
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 !!


0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
830 Views

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
0 Kudos
jaeger0
Beginner
830 Views

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.
0 Kudos
jimdempseyatthecove
Honored Contributor III
830 Views

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
0 Kudos
Reply