Print

Print


Dear Peter and AGP:

Peter Shenkin wrote:

> Hi,
>
> First, Fortran does not have an unsigned type.  Thus, in general,
> you can't do this in Fortran, at least in any straightforward
> way.  The good news is that any unsigned that overflows an int
> will come out as a negative number when read into a signed int,
> so you can at least know whether you have this problem;  if you
> do, you can probably handle it with difficulty by understanding
> the bit pattern.
>
> Also, I suppose that your F90 compiler could have a signed int
> KIND large enough to hold the positive values of the unsigned
> ints that form the file.
>
> Second, the Fortran standard does not provide for reading a
> "raw" binary file, though most compilers provide extensions
> for this.  Fortran unformatted IO is usually expected to
> read/write some format peculiar to the compiler and/or the OS.
>
> Bottom line:  never say never, but here's a case where I think
> the right answer really is, "Do it in C."
>
>         -P.
>

    I disagree.  I believe that a programmer familiar with F90 and F95 can do
this fairly well in Fortran, although it will require a fair amount of hacking
and most likely the use of some non-standard extensions.  See discussion below.

>
> On Mon, 10 Apr 2000, AGP wrote:
>
> > I have been programming in F90 and F95 on a win98 machine
> > and I have been given the task of extracting data from
> > a binary file into ASCII. I consider myself a good programmer
> > but this is a bit above my head. The binary file is described
> > as "data as a binary integer array (PC byte-ordered) with
> > 10800 columns (e-w) by 6000 or 4800 rows (n-s)....the data
> > are also unsigned integers".
> > How would I use FORTRAN to extract the binary into an ASCII text file.
> > Any guidance is appreciated.
> > tia
> > AGP

    There is some important information missing.
    1.  What is the size of each binary datum?  Is it one byte?  2 bytes?  4
bytes?  Or, some weird size, e.g., 12 or 24 bits?
    2.  What, if anything, is the record structure?  Does each row have a
record delimiter?  If so, what is(are) the delimiter(s)?  If not, is it what is
commonly called a stream file, i.e., one large mass of binary data?
    3.  Are all records in the file the same length?  If so, what is the record
length?  If not, how are changes in the record length indicated within the
file?

    You will need to investigate several possibilities for reading the data.
Which one works best will depend upon the file properties and the capabilities
of your Fortran compiler.
    1.  Use direct access read.  This may work if all of the records are the
same length and the record structure is consistent with what your compiler
expects for a direct access file.  On Wintel systems, some Fortran compilers
will correctly do a direct access read of a file with no record boundaries.
You may need to compile your program with a special compiler option in order to
get this capability.
    2.  A sequential acces read may work if each record is delimited by a CR-LF
combination.
    3.  Use stream file read.  Right now, you will need to use a non-standard
extension, which varies in syntax from one compielr to another, in order to do
this.  Stream I/O will be in F2K, but so far, no vendor has implemented the ISO
standard version of this feature.

    You will need the answer to question 1 in order to know what to do to read
the data into variables and transform it to proper character representation.
Here are some thoughts.
    1.  You could read each row into a CHARACTER variable using the "A"
format.  This will work if your compiler's CHARACTER data type will accept any
bit pattern, the size of the binary datum is small enough (e.g., 1 or 2 bytes),
and the maximum length of your compiler's CHARACTER variables large enough.
(On Wintel systems, Compaq Visual Fortran has a maximum length for CHARACTER
variables of 2**31-1, which is certainly large enough.  Other Fortran compilers
may have much lower limits for the maximum length of a CHARACTER variable.)
Once you have the binary data in the CHARACTER variable, you can use an
internal READ statement or the TRANSFER function to move the bits into suitably
sized INTEGER variables or array elements.
    2.  You could read the data into a signed integer of the proper size and
then copy it into a signed integer of a size large enough to properly represent
the unsigned data by using the TRANSFER function, one of the bit intrinsic
functions, or the MVBITS subroutine.
    3.  Once you have the data properly represented in INTEGER variables or
array elements, you can write it out in character representation.

    If you have any other questions, please feel free to contact me.

--
----------
Sincerely,
Craig T. Dedo                                          Internet:
[log in to unmask]
Elmbrook Computer Services                 Voice Phone:  (262) 783-5869
17130 W. Burleigh Place                        Fax Phone:     (262) 783-5928
Brookfield, WI   53005-2759                   Disclaimer:     These opinions
are mine alone.
USA                                                         They do NOT
represent any organization.

"They that can give up essential liberty to obtain a little temporary
    safety deserve neither liberty nor safety."  -- Benjamin Franklin (1759)




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%