Looking for function to parse FORTRAN formatted data 
Author Message
 Looking for function to parse FORTRAN formatted data

I need to parse some data according to the specifications of fortran
formatted reads, ie data in specified fixed-width columns.  The
scanf() family of functions does not work because of the blank-space
interpretation rules.

This might be a sufficiently common problem that someone has done it
already, hence I'm asking if anyone has code to do this before I roll
my own.

Keith Refson
--
Dr Keith Refson,        "Paradigm is a word too often used by those who would
Dept of Earth Sciences      like to have a new idea but cannot think of one."
Parks Road,                  -- Mervyn King, Deputy Governor, Bank of England
Oxford OX1 3PR, UK        

             earth.ox.ac.uk         Fax: 01865 272072
--



Mon, 14 Apr 2003 13:33:01 GMT  
 Looking for function to parse FORTRAN formatted data


Quote:
> I need to parse some data according to the specifications of fortran
> formatted reads, ie data in specified fixed-width columns.  The
> scanf() family of functions does not work because of the blank-space
> interpretation rules.

> This might be a sufficiently common problem that someone has done it
> already, hence I'm asking if anyone has code to do this before I roll
> my own.

I don't know the details of FORTRAN fixed width fields but assuming that the
layout is similar to a table: i.e. each line/row may be considered as a
record and each column as a value then you can read in each line with fgets
into an appropriately sized buffer.

Then if you know the length of each field then you can use this as an offset
into this buffer as the point where to start copying. As you progress keep
adding the length of the current field to the offset.

Once you have the array of fields you can perform any necessary conversion
using strtod etc.

Semi pseudo-code. Not compiled and assuming that buf has been filled by a
call to fgets and that data is created large enough to hold the resulting
strings.

void readFortranFields(char *buf, char *data[], int fieldWidth[], int
numFields)
{
    size_t offset = 0;
    size_t field;
    size_t len = strlen(buf);

    for( field = 0; field < numFields && offset < len; ++field)
   {
        strncpy(data[field], buf + offset, fieldWidth[field]);
        offset += fieldWidth[field];
   }

Quote:
}

--
Bob Wightman

Views expressed in this message are my own
and not those of my employer.

Keith Refson - real
--



Tue, 15 Apr 2003 13:19:59 GMT  
 Looking for function to parse FORTRAN formatted data

Quote:
> I need to parse some data according to the specifications of fortran
> formatted reads, ie data in specified fixed-width columns.  The
> scanf() family of functions does not work because of the blank-space
> interpretation rules.

Note that blanks are by far not the only problem you're going to have.
There's also that crazy exponent formatting stuff:

*) Fortran may be writing 'd', 'D', 'q' or 'Q' instead of 'e' or 'E'
   to start the exponent.
*) a three-digit exponent has no 'e' at all, by default, i.e.
        1.2345-345  means 1.2345E-345, really.
*) exponents might be shifted (i.e. all numbers silently multiplied by
   10^(something)). Even for formats with no printed exponent at all.

For the blank-separation stuff, it's usually enough to split up the
line into separate numbers according to the known field widths, before
you use sscanf() to scan each of them, individually. 'D' and 'Q' can
be replaced by an 'e'. Just replace the first character after the
input field by a '\0' or blank, sscanf(), and put the original
character back in.

The missing 'e' is more of a show-stopper. It will usually require
separate scanning of mantissa and exponent. In gnuplot, although we do
handle 'd' and 'q', we explicitly refused to care about missing 'e'
representations. We considered that to be a design bug of Fortran,
which should be handled by filtering the data through a little fortran
proggy to talk some sense into it, rather than by trying to squeeze it
through sscanf().

--

Even if all the snow were burnt, ashes would remain.
--



Tue, 15 Apr 2003 13:20:07 GMT  
 Looking for function to parse FORTRAN formatted data

Quote:
>I need to parse some data according to the specifications of fortran
>formatted reads, ie data in specified fixed-width columns.  The
>scanf() family of functions does not work because of the blank-space
>interpretation rules.
>This might be a sufficiently common problem that someone has done it
>already, hence I'm asking if anyone has code to do this before I roll
>my own.

This has been done in the libraries that go with f2c (fortran 77 to C
translator - ftp://netlib.bell-labs.com/netlib/f2c).  The easy solution
would be to do the I/O in fortran and use f2c.

Paul Nulsen
--



Tue, 15 Apr 2003 13:21:05 GMT  
 Looking for function to parse FORTRAN formatted data

Quote:
> I need to parse some data according to the specifications of fortran
> formatted reads, ie data in specified fixed-width columns.  The
> scanf() family of functions does not work because of the blank-space
> interpretation rules.

> This might be a sufficiently common problem that someone has done it
> already, hence I'm asking if anyone has code to do this before I roll
> my own.

And somewhere in the middle is lex.  You can use flex (or lex) to
generate highly optimized C code that will look for the patterns for
you.

You just write a few simple rules containing regular expressions and
corresponding calls to your functions (or any C code).  You can use
state to say, "only use these rules when I'm in state 'foo'".

--
--Ed Cashin                     PGP public key:

    Note: If you want me to send you email, don't munge your address.
--



Wed, 16 Apr 2003 03:58:44 GMT  
 Looking for function to parse FORTRAN formatted data

Quote:
> I need to parse some data according to the specifications of fortran
> formatted reads, ie data in specified fixed-width columns.  The
> scanf() family of functions does not work because of the blank-space
> interpretation rules.

Thanks for all the advice.  I ended up writing myself a function which
takes scanf() like arguments and format strings but changes the
semantics to eliminate whitespace skipping and treats a format string
length specifier as an exact field to find the data in.  

Fortunately I don't have to cope with the wilder excesses permitted by
Fortran formats!  This works well and is quite compact in use.

Keith Refson
--
Dr Keith Refson,        "Paradigm is a word too often used by those who would
Dept of Earth Sciences      like to have a new idea but cannot think of one."
Parks Road,                  -- Mervyn King, Deputy Governor, Bank of England
Oxford OX1 3PR, UK        

             earth.ox.ac.uk         Fax: 01865 272072
--



Tue, 22 Apr 2003 04:17:10 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Q: Looking for parsing command line options function

2. looking for an example of an fortran function call

3. Changing .MAT data format to binary format

4. Data Grid, Windows forms, Time Data Formatting

5. Parsing array of scientific format numbers from a string to doubles

6. FORMAT parsing?

7. parsing the format string at compile time...

8. Number parsing and formatting help needed - VarParseNumFromStr, VarBstrFromR8, GetNumberFormat

9. book on text parsing/formatting?

10. read with a FORTRAN format description

11. fscanf-like with a FORTRAN-like format desciption

12. C equivalent of FORTRAN formatting shortcut?

 

 
Powered by phpBB® Forum Software