Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Reading in an ASCII file written by a FORTRAN program

  • To: mathgroup at smc.vnet.net
  • Subject: [mg97581] Re: Reading in an ASCII file written by a FORTRAN program
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Mon, 16 Mar 2009 04:23:26 -0500 (EST)
  • References: <gphdlo$ogj$1@smc.vnet.net>

hayes.tyler at gmail.com wrote:
> Hello All:
> 
> I didn't want to hijack another's thread about reading in fixed format
> files into Mathematica and I  was wondering if there is a way to read
> FORTRAN written data.
> 
> For my thesis used FORTRAN code for my number
> crunching, and used function calls to DISLIN to visualize that data.
> Now I have Mathematica, whose graphics capabilities are well beyond
> anything I could previously do, I would like to recreate and improve
> those graphs. The way I created the files of data (which I always used
> a .d extension, but it doesn't matter what that would be called) is
> demonstrated below. 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, some are strings, some
> doubles, etc.)
> 
> I subsequently read back the data into FORTRAN 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)
> 
> So, the big question then is: How would I go about reading in the
> above unformatted Fortran file within Mathematica?
> 
> Thanks for any advice you may have.
> 
> Cheers,
> 
> t.
> 
Yes, any text data can be read into Mathematica. As a last resort, one 
can always read such a file as a List of strings and manipulate the 
strings to extract the data you require. However, this is always a last 
resort, and not necessary here.

First open the file:

str=OpenRead["c:\\myfile"];

Now read the two character variables:

fnmod=Read[str,Record];
tnstr=Read[str,Record];

(You may need to remove trailing spaces in these variables using 
StringTrim). Note that if you print out FullForm[fnmod] you will see 
exactly what characters are in the string.

It may be easiest to read the rest one number at a time:

x=Read[str,Number];

Note that all numbers will end up as Double Precision inside Mathematica.

Since you didn't include any declarations in your code, it is not 
possible to determine if any of those arrays are CHARACTER variables, 
but hopefully you have the general idea by now. Remember that you can 
always read tricky bits as strings, and then pull the strings apart in 
Mathematica. The ToExpression function may be useful to convert a string 
into a number (say).

Don't for get to close the stream when you are done:

Close[str];

David Bailey
http://www.dbaileyconsultancy.co.uk




  • Prev by Date: Re: Re: Weird NMinimize behaviour
  • Next by Date: Re: Maintaining a Mathematica bug list
  • Previous by thread: Reading in an ASCII file written by a FORTRAN program
  • Next by thread: Re: Reading in an ASCII file written by a FORTRAN program