[Thread Prev][Thread Next][Index]

Re: [ferret_users] a hint for reading integer data in fortran format

Thanks for the note, Billy. We'll add to the documentation.

 This particular file could also be read using the "delimited" option, which lets you set data types of "date" and

Something along the lines of

set data/form=delim/delim=" "/type="numeric,date,numeric,..."/var="id,date,time..."   file.dat

Ryo, you are correct.  Ferret does nothing but pass the format statement on to Fortran to be used by in reading the data.  An integer format such as I5 don't trigger a Ferret error, but because the format is just used as given, the field is read incorrectly.  So if the file contains:

    1   3.00
    2   5.00
    3   7.00
    4   8.00
    5   9.00

and if an I format is used, the file data is read into the floating point Ferret variable incorrectly:
yes? file/var=v1,v2/format=(i5,f8.2) a.dat
yes? list v1,v2
             DATA SET: ./a.dat
             X: 0.5 to 15.5
 Column  1: V1
 Column  2: V2
                  V1    V2
1    /  1:  1.401E-45  3.000
2    /  2:  2.803E-45  5.000
3    /  3:  4.204E-45  7.000
4    /  4:  5.605E-45  8.000
5    /  5:  7.006E-45  9.000

But with an F5.0 format it's ok
yes? file/var=v1,v2/format=(f8.0,f8.2) a.dat
yes? list v1,v2
             DATA SET: ./a.dat
             X: 0.5 to 5.5
 Column  1: V1
 Column  2: V2
              V1    V2
1    /  1:   1.00  3.000
2    /  2:   2.00  5.000
3    /  3:   3.00  7.000
4    /  4:   4.00  8.000
5    /  5:   5.00  9.000

Ryo Furue wrote:
Hi Billy,

| It is not obvious how to read ASCII integer data into Ferret using
| FILE/FORMAT, since the only permitted formats are floating point (as
| far as I know). However, Ferret is willing to be fooled into reading
| integer data anyway, simply by referring to an value of N places
| (e.g. fortran format i8) as a floating point value with the same
| number of places (e.g. f8.0).

It seems that that is what the Fortran standard says.
(Since it's very hard to decipher the standardspeak of the standard
 document itself, let me cite the trusty Metcalf and Reid (1999).)
On input, the Fortran language is rather dangerously permissible.
With "F8.2", for example, an input string "314" is read as 3.14 .
That is, the fraction part ".2" in "F8.2" is honored even without
a decimal point in the input.  Likewise, the same string is read
as 314.0 with "F8.0".

In short, you can read an integer input with "F8.0" and the like.
(Of course, the destination must be a floating point variable.)
I guess Ferret can do it, too, because it's implemented in Fortran

Metcalf & Reid. 1999. Fortran 90/95 Explained, 2nd Ed.
  Oxford University Press.

[Thread Prev][Thread Next][Index]

Contact Us
Dept of Commerce / NOAA / OAR / PMEL / TMAP

Privacy Policy | Disclaimer | Accessibility Statement