[Thread Prev][Thread Next][Index]

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





William S. Kessler wrote:
We talked a month ago about reading integer data implicitly as real (see the mail archives with the above subject). As Ryo and Ansley pointed out, fortran does a few things that, to me at least, seemed inconsistent.

Here's another example.

I have fortran output that I wrote as F7.3 format, but read into Ferret with an F7.1 format. (My error was not changing a leftover Ferret FILE statement after changing the fortran code).

Surprisingly, Ferret handled this by ignoring my error and reading the F7.3 format correctly!
Hi Billy,

This believe this behavior is standard FORTRAN -- not something that Ferret is specifically programmed to do. The number of decimal points indicated on an *input* FORTRAN format is really a legacy of the punch card days, when often the decimal point was implicit in (omitted from) the input data. If the input data contain an explicit decimal point, it will over-ride the format specification.

   - Steve

Specifically, a line of my output is:

1130.575 1130.787 154.86880 154.88049 -8.58850 -8.58770 696.0 182 5.000 507. -0.138 -0.053

The last two values were written with format (....,2f7.3).

I read this into Ferret, apparently incorrectly, with format (....2f7.1).

And the Ferret listing is, correctly:  -0.1380 -0.05300

I like it! I don't understand it but I like it! Obviously I don't know fortran as well as I thought I did. And if Ferret fixed all my errors so gracefully, I'd be much more productive.

Billy

On 29 Dec 09, at 11:31 AM, Ansley Manke wrote:

Hi,
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 (http://ferret.pmel.noaa.gov/Ferret/documentation/users-guide/commands-reference/SET#_VPINDEXENTRY_1616)

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 (correct?). Regards, Ryo -------- 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