Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29273 Discussions

Input Conversion Error/Reading an ASCII file

theonlymoxey
Beginner
2,160 Views
Morning all,
I am having a large issue brought about by having to pick up someone elses codes and try to make sense of them. In addition I only started using FORTRAN this week. So here goes..
This is my code
program reorder
c
c Re-orders the pixels to convert to crsfrqs required
c input format...
c
implicit none
integer i,j,k,l,pix(512,512)
character*12 yname,newname
c
print*,'Please enter the name of your visilog file'
print*,'(12 chars max, also type the extension)'
read(5,'(a12)') yname
c
print*,'Enter the name for your new file:'
print*,'(12 chars max, also type the extension)'
read(5,'(a12)') newname
c
print*,'WORKING......'
9 open(9,file=yname, status='unknown')
open(2,file=newname, status='unknown')
c
c
do 3 i=1,512
do 4 k=1,512
pix(i,k)=0
4 continue
3 continue
c
c Read and re-order...
c
c i is x cord...
c
do i=1,512
read (9,99) (pix(i,j), j=1,512)
99 format(512I3)
end do
c
c print*,'pix(1,512=',pix(1,512)
c print*,'pix(2,2=',pix(2,2)
c
c Write out in the preferred format into the new file...
c
do 30 k=1,512
do 40 l=1,512
if(pix(k,l).gt.100)then
pix(k,l)=0
elseif(pix(k,l).lt.100)then
pix(k,l)=255
else
continue
endif
write(2,62)pix(k,l)
62 format(i3)
40 continue
30 continue
c
write (6,*) 'Program converted in ',newname
write (6,*) 'Bye Bye.'
c
continue
end
When I run Debug the error returns on line 34 (the line "read (9,99) (pix(i,j), j=512)").
The error message is code 64 (0x40) which implies that there is an input conversion error. I am unsure if this is a case of trying to read the wrong file type (trying to read a .txt) or if the fact that I am trying to read an ASCII (to be accurate it is an ASCII of an image file that was converted via MATLAB. The dimensions of the ASCII are 236x121).
Any help will be hugely appriciated!!
0 Kudos
11 Replies
TimP
Honored Contributor III
2,160 Views
According to ifort documentation, the default is as if you had specified recl=132 in your open. If your input file actually has 1536 characters per record (line in your text editor), you would need to specify it. Needless to say, such a feature isn't often used, due to the difficulty text editors may have with long lines. If your input file actually has spread these data over multiple lines, you need a separate read for each line.
0 Kudos
theonlymoxey
Beginner
2,160 Views

Hi there

Yeh the data file being read is a multiple lines one. I have attached it for you to see.
The purpose of this code (I'm told) is to read the ASCII file in and export a reordered single column of data which will then be used in a second inherited code

UPDATE:
Seems the upload didn't work. Here is the file forviewing from my DB http://dl.dropbox.com/u/3076541/img28cropped.txt

0 Kudos
Johannes_Rieke
New Contributor III
2,160 Views
Hi,

as TimP said the "resolution" of your integers in rows and columns should be correct (236x121 ??). I can't download your file, so I just guess it contains 236 rows and 121 columns of (space separated?) integers (i3). So maybe the modified code works:

[bash]program reorder ! ! Re-orders the pixels to convert to crsfrqs required ! input format... ! implicit none integer, parameter :: irow=512, icol=512 ! this is the resolution of your pic integer :: i,j,k,l,pix(irow,icol) character*12 :: yname,newname ! print*,'Please enter the name of your visilog file' print*,'(12 chars max, also type the extension)' read(*,'(a12)') yname ! print*,'Enter the name for your new file:' print*,'(12 chars max, also type the extension)' read(*,'(a12)') newname ! print*,'WORKING......' 9 open(9,file=yname, status='unknown') open(2,file=newname, status='unknown') ! ! pix = 0 ! ! Read and re-order... ! ! i is x cord... ! do i=1,irow read (9,*) pix(i,1:icol) end do ! ! Write out in the preferred format into the new file... ! do 30 k=1,irow do 40 l=1,icol if(pix(k,l).gt.100)then pix(k,l)=0 elseif(pix(k,l).lt.100)then pix(k,l)=255 else continue endif write(2,'(i3)')pix(k,l) 40 continue 30 continue ! write (*,*) 'Program converted in ',newname write (*,*) 'Bye Bye.' ! continue close(9) close(2) end [/bash]You will have to correct the rows and columns (irow,icol) in it. If the integers are not space seperated, you will have to use in line 31 something like this:
[fortran]read (9,'(i3)') (pix(i,1:icol)[/fortran]
But I'm not sure, whether the latter works.

Kind regards,
Johannes
0 Kudos
theonlymoxey
Beginner
2,160 Views
Hi Johannes

I'm not sure why the download doesn't work. I am new to this forum but if you put the previously posted URL into your browser you will be able to view it.
If not you can try downloading here

As for the code you posted, I did try it (with both versions of line 31) but sadly no joy.

The aim of this code is to reorder a collection of ASCII characters on a 236x121 matrix. There are no spaces in between the characters. This used to be an image until it was converted in MATLAB to a .txt file.

Thank you for your help but something still isn't working. Appriciate the effort though!

0 Kudos
SergeyKostrov
Valued Contributor II
2,160 Views
Hi,

Quoting theonlymoxey
...
If not you can try downloading here
...

I have a question regarding your input file.It looks like a verydownsampledMRI-image. So, is that a correct input image?



Best regards,
Sergey
0 Kudos
Johannes_Rieke
New Contributor III
2,160 Views
Hi,

now as I saw the ASCII I think I understand what the code should do. It reads a character and convert it into an three digit integer (0 to 255). I modified the code (you should uncomment the file name read in part, if you change your file name, be careful of length), and the read parts works, but I'm not sure, whether the output in one column is that what you want. It depends on the next program in your tool chain...

Kind regards,
Johannes

ps: The read line 35 works fine with IFORT but won't with GCC!

[fortran]program reorder ! ! Re-orders the pixels to convert to crsfrqs required ! input format... ! implicit none integer, parameter :: irow=121, icol=236 ! this is the resolution of your pic integer :: i,j,k,l,pix(irow,icol) character*80 :: yname,newname character(len=1),dimension(irow,icol) :: pixc ! ! print*,'Please enter the name of your visilog file' ! print*,'(12 chars max, also type the extension)' ! read(*,'(a12)') yname yname(1:16) = 'img28cropped.txt' !! ! print*,'Enter the name for your new file:' ! print*,'(12 chars max, also type the extension)' ! read(*,'(a12)') newname newname(1:20) = 'img28cropped_new.txt' ! print*,'WORKING......' 9 open(9,file=yname, status='unknown') open(2,file=newname, status='unknown') ! ! pix = 0 pixc = ' ' ! ! Read and re-order... ! ! i is x cord... ! do i=1,irow read (9,'(A1)') pixc(i,1:icol) pix(i,1:icol) = ichar(pixc(i,1:icol)) end do ! ! Write out in the preferred format into the new file... ! do 30 k=1,irow do 40 l=1,icol if(pix(k,l).gt.100)then pix(k,l)=0 elseif(pix(k,l).lt.100)then pix(k,l)=255 else continue endif write(2,'(i3)')pix(k,l) 40 continue 30 continue ! write (*,*) 'Program converted in ',newname write (*,*) 'Bye Bye.' ! continue close(9) close(2) end [/fortran]
0 Kudos
theonlymoxey
Beginner
2,160 Views
Hi Sergey
I'll admit it does look like one. In actual fact it is a greyscale image of a combustion flame in an optical engine. The flame here has taken on a C shape with the two large black lumps curl around to meet each other.
0 Kudos
theonlymoxey
Beginner
2,160 Views
Hi Johannes
I will have to try that tomorrow when I am back in my office!
Many thanks!
0 Kudos
Les_Neilson
Valued Contributor II
2,160 Views
I thought it looked more like a leaping dolphin, and I'm sure I saw an outline of Australia in there somewhere.
:-)

Les
0 Kudos
SergeyKostrov
Valued Contributor II
2,160 Views

I'd like to provide additional information:

1. I've downloaded a 'img28cropped.txt' file

2. Opened the file in an editor as binary and manually deleted all New Line and Carriage return
characters ( 0x0D & 0x0A )

3. Saved as a new file

4. Opened the new file with ImageJ as a Raw image with dimensions 235 by 121 and 8-bit type

A note for the Step 2:

Source image looks like ( '...' means skipped characters or '***' skipped rows ):

@@@@@...@@@@@DA
@@@@@...@@@@@DA
@@@@@...@@@@@DA
***
@@@@@...@@@@@DA
@@@@@...@@@@@DA
@@@@@...@@@@@DA

Width ( Columns ) = 235
Height ( Rows ) = 121

'DA' means two hex-characters 0x0D and 0x0A

Converted image looks like ( '...' means skipped characters or '***' skipped rows ):

@@@@@...@@@@@@@@@@...@@@@@@@@@@...@@@@@***@@@@@...@@@@@@@@@@...@@@@@@@@@@...@@@@@

All New Line and Carriage return characters ( 0x0D & 0x0A ) deleted anddata could be loaded
into a matrix.

Note: ImageJ is an application widely used by Software Developers working on image processing projects.

Best regards,
Sergey

0 Kudos
SergeyKostrov
Valued Contributor II
2,160 Views

Enclosed are two files for testing:

img28cropped.1.237x121.txt - Source image
img28cropped.2.235x121.txt - Manually Converted image

Best regards,
Sergey

0 Kudos
Reply