Forum Jump

Select Group :
Select Forum :
Sorted By :
Sort Order :
From The :
 
Thread Tools  Search this thread 
jaeger0
Total Points:
710
Status Points:
210
Brown Belt
November 4, 2009 12:24 AM PST
reading file
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 !!


jimdempseyatthecove
Total Points:
36,372
Status Points:
36,372
Black Belt
November 4, 2009 10:14 AM PST
Rate
 
#1

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

--------

Blog: The Parallel Void


www.quickthreadprogramming.com


jaeger0
Total Points:
710
Status Points:
210
Brown Belt
November 4, 2009 10:40 AM PST
Rate
 
#2 Reply to #1

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.


jimdempseyatthecove
Total Points:
36,372
Status Points:
36,372
Black Belt
November 5, 2009 2:51 PM PST
Rate
 
#3 Reply to #2

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:

    ! 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

and declare an instance of the type

    type(TypeInput) :: theInput


and in the program

        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

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

--------

Blog: The Parallel Void


www.quickthreadprogramming.com




Intel Software Network Forums Statistics

8458 users have contributed to 31570 threads and 100530 posts to date.
In the past 24 hours, we have 16 new thread(s) 129 new posts(s), and 150 new user(s).

In the past 3 days, the most popular thread for everyone has been gemm(A,A,A) like possible? The most posts were made to gemm(A,A,A) like possible? The post with the most views is Quoting - rase if (k.eq.0

Please welcome our newest member soundmyth