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: [mg97603] Re: Reading in an ASCII file written by a FORTRAN program
  • From: hayes.tyler at gmail.com
  • Date: Mon, 16 Mar 2009 06:30:30 -0500 (EST)
  • References: <gphdlo$ogj$1@smc.vnet.net> <gpl5q6$os8$1@smc.vnet.net>

OpenRead is exactly what I'm looking for. And yes I do know which are
strings, doubles, integers, etc. Perhaps I'm dating myself here, but
FORTRAN 77 uses implicit declaration of names with i-n being integers
so I can figure it out (this was relaxed in FORTRAN 90 but I find I
still use many of the same naming conventions). The real problem was I
couldn't figure out how to get Import to know where it was when
reading in the file and was treating it as a one-shot lump Read. Of
course, it seems now that is why Mathematica has things like OpenRead!

Cheers,

t.


On Mar 16, 5:23 am, David Bailey <d... at removedbailey.co.uk> wrote:
> hayes.ty... 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 Baileyhttp://www.dbaileyconsultancy.co.uk



  • Prev by Date: Re: Using Mathematica notebooks in presentations?
  • Next by Date: PlotMarkers bug?
  • Previous by thread: Re: Reading in an ASCII file written by a FORTRAN program
  • Next by thread: BarChart - Extendng the Y-axis and labeling the endpoints