[R] Reading in FORTRAN data using R

Michael H. Prager Mike.Prager at noaa.gov
Tue Feb 7 23:15:43 CET 2006


Tyler,

One relatively easy way to get structured model data into R is to use 
output routines I have written with my colleagues.  They are called 
For2R and are available at

http://shrimp.ccfhrb.noaa.gov/~mprager/Rinter.html

To go that route, you would replace (or supplement) your Fortran write 
statements with a series of calls to our output routines. When you read 
the data from the resulting file, you won't need to specify variable 
names, lengths, or anything else, as they will all be stored in the 
output file.  A single R statement will read everything into an R "list" 
object. 

The alternative would be using the the R scan() function to read your 
variables into R.  In that case, you would need to specify the length of 
each and give it a name, similar to how you read your data into another 
Fortran program.

By the way, watch out for the term "unformatted" in a Fortran context.  
"Unformatted" in Fortran is a specific term that means something close 
to what "binary" data I/O means to a C programmer.  Your I/O is 
"list-directed," but perhaps you know that already.

Regards,
...Mike Prager




on 2/7/2006 3:30 PM Tyler Hayes said the following:
> Hi There:
>
> I was wondering if there is a way to read FORTRAN list data (similar to 
> IDL's readf function).  I often use FORTRAN for most of my number 
> crunching, and use something like IDL to visualize and perform 
> statistical analysis on that data.  Since the each file is rather large 
> (>100 Mb), formatting the output into columns or tables is impractical, 
> hence the "list" style.  Please note, this is NOT binary data, but text 
> output with no FORMAT.  For example:
>
> My FORTRAN code writes out:
>
>       open(unit=30,name=fnout,status='old')
>            
>       write(30,'(a20)') fnmod
>       write(30,'(a20)') fnstr
>       write(30,*) nfault,hpl,vplx,vply,taua,tauf,amuu,
>      &      tminn,tstepp,itime,TBIS,bulkms,ntm,ncycle,pdcy
>       write(30,*) ptrad
>       write(30,'(a1)') respfb
>       write(30,*) (timi(n), n=1,nfault)
>       write(30,*) (taub(n), n=1,nfault)
>       write(30,*) (delts(n), n=1,nf)
>       write(30,*) (cfr(n), n=1,nfault)
>       write(30,*) (dfr(n), n=1,nfault)
>       write(30,*) (slpdf(n), n=1,nf2)
>       write(30,*) (slpv(n), n=1,nfault)
>       write(30,*) (rhofcc(n), n=1,nfault)
>       write(30,*) (islip(n), n=1,nfault)
>
> (NOTE: not all variables are the same length!)
>
> I subsequently read in back the data as:
>
>       open(unit=30,name=fnin,status='old')
>
>       read(30,'(a20)') fnmod
>       read(30,'(a20)') fnstr
>       read(30,*) nfault,hpl,vplx,vply,taua,tauf,amuu,
>      &     tminn,tstepp,ntime,TBIS,bulkms,ntm,ncycle,pdcy
>       read(30,*) ptrad
>       read(30,'(a1)') respfb
>       read(30,*) (timi(n), n=1,nfault)
>       read(30,*) (taub(n), n=1,nfault)
>       read(30,*) (delts(n), n=1,nf)
>       read(30,*) (cfr(n), n=1,nfault)
>       read(30,*) (dfr(n), n=1,nfault)
>       read(30,*) (slpdf(n), n=1,nf2)
>       read(30,*) (slpv(n), n=1,nfault)
>       read(30,*) (rhofcc(n), n=1,nfault)
>       read(30,*) (islip(n), n=1,nfault)
>
>
> read.fwf is not what I need from what I read and neither is read.fortran.
>
> How would I go about reading in the above unformatted Fortran file within R?
>
> Sorry if the question is somewhat trivial, but I am a complete nube to R 
> (installed it 2 weeks ago) and am just getting caught up to speed.
>
> Thanks for any advice you may have.
>
> Cheers,
>
> t.
>
>   

-- 

Michael Prager, Ph.D.
Population Dynamics Team, NMFS SE Fisheries Science Center
NOAA Center for Coastal Fisheries and Habitat Research
Beaufort, North Carolina  28516
http://shrimp.ccfhrb.noaa.gov/~mprager/
Opinions expressed are personal, not official.  No
government endorsement of any product is made or implied.




More information about the R-help mailing list