Loading...
You are not logged-in Login/Register





  • Posts   Search Threads
  • jaeger0November 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 !!




    jimdempseyatthecoveNovember 4, 2009 10:14 AM PST
    Rate
     
    Re: reading file


    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

    jaeger0November 4, 2009 10:40 AM PST
    Rate
     
    Re: reading file


    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.


    jimdempseyatthecoveNovember 5, 2009 2:51 PM PST
    Rate
     
    Re: reading file


    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

Forum jump:  

Intel Software Network Forums Statistics

16,377 users have contributed to 46,364 threads and 164,041 posts to date.

In the past 24 hours, we have 9 new thread(s) 31 new posts(s), and 20 new user(s).

In the past 3 days, the most popular thread for everyone has been Program compiles in release but not debug The most posts were made to You need to show us the whole The post with the most views is vectorization of sin/cos results in wrong values

Please welcome our newest member fruitbrown


For more complete information about compiler optimizations, see our Optimization Notice.