Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Data Transfer to Fortran

by cheech (Beadle)
on Nov 26, 2009 at 17:15 UTC ( #809620=perlquestion: print w/ replies, xml ) Need Help??
cheech has asked for the wisdom of the Perl Monks concerning the following question:

Hello, A Perl script determines the cwd and makes a new folder in it. In the new folder, a new text file is created and this is written to it:

20091122 51 41 46 45.80 30.93
20091123 47 39 43 46.27 30.60
20091124 49 40 45 46.93 29.60
Fortran must read-in all of this for analysis, but doesn't know where the text file is located a priori. What's the best way to get the data into Fortran?

Please Don't:
-Ask why Fortran is needed
-Suggest better alternatives to Fortran
-Comment on the practicality of this task

Thanks,

Dave

Comment on Data Transfer to Fortran
Re: Data Transfer to Fortran
by cdarke (Prior) on Nov 26, 2009 at 17:41 UTC
    That really depends on how the Perl is talking to the FORTRAN. If Perl is calling the FORTRAN program using system then the file name could be passed on the command-line. Alternatively a pipe could be used (see open). However if the FORTRAN is a library in a DLL or .so file then you could call the FORTRAN subroutines from XS, passing the data or file name.
Re: Data Transfer to Fortran
by Anonymous Monk on Nov 26, 2009 at 17:42 UTC
    What do you mean? Give fortran a file, any which way fortran takes files...
      Well, fortran could open/read the file if it knew where it was, but it doesn't...

      Hmm. I'd put the file in a fixed location that Fortran knows about.

Re: Data Transfer to Fortran
by Marshall (Prior) on Nov 26, 2009 at 18:57 UTC
    I would change your Perl program to just output these strings to STDOUT instead of to a file. You have an easy format for FORTRAN to parse. The FORTRAN program reads from STDIN and does what it does and outputs to STDOUT. Use the OS to "pipe" your Perl output(STDOUT) into the FORTRAN program's STDIN.

    #shell command... yourPerl.pl | FortranProg.exe
    Fortran remains the "numero uno number cruncher" language. Many scientific programs are written in Fortran and use this IO method of STDIN to STDOUT "filters":
    cat data | Fortran1 | Fortran2 | Fortran3, etc.

    To make a temp file between your Perl and Fortran, also use the OS:

    yourPerl.pl > tempfile Fortran <tempfile >outputfile del tempfile
    Fortran can process command line args just like 'C' or Perl.
    perlprog | fortan -gain 3dB
    PS: FORTRAN is an acronym and should be spelled in all caps. I got lazy above.
      Thanks for the input, that's a new idea to me. However, a master Perl scripts will be transferring this data back and forth between other Perl scripts, fortran scripts, VBA, etc. Would you still recommend this route?
        YES! Get text in, send text out. Doesn't matter what the language of the programs are!

        As I mentioned above, it is possible for any of these languages to process a command line argument like "file name".
        YourFORTRAN -f name for example.

        But hearing about how much this file format will be "shuffled around", I even more strongly recommend the "filter" approach.

        Update:

        Another way to think about your input file, is that is a database. Well, what kind of operations can we do? 1) Transform it in some way by additions, deletions, or changes to existing records. 2) Make reports from it based upon some subset of the data, perhaps using some calculations from the data(or subset) and/or sorting of the data. All of these things can be done with the filter model.

        Let's go through some simple tasks:
        -generate some kind of report A of the whole dataset - ok, make a genreport filter. cat data | genreport.pl
        -generate the same report but only between dates X -Y, - ok, filter out data not between those dates with a date filter and pipe result to the genreport filter.
        cat data | date.pl -s startdate -e enddate | genreport.pl
        this date gizmo might just be a fancy grep command..
        -add records, just use cat (or type on Windows), cat new >>data
        -delete records between dates, well that is just inverse of the date filter
        -change in some way, use transform filter, I don't know what this would do but I hope you are getting the idea..

        We are beyond Perl here, but the way to set up something like I think you need is to have a bunch of little "filter" programs that can be combined and used in different orders. These programs are "stitched together" via a shell script, not with Perl. Each of these programs takes to the extent possible input from STDIN and output goes to STDOUT.

Re: Data Transfer to Fortran
by BrowserUk (Pope) on Nov 26, 2009 at 19:39 UTC

    Why not have the perl script invoke the fortran code supplying the name of the file, once it has created it?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      That would be great, how do I do that?

        Supply the path/name of the file on the command line to the fortran program?

        my $path = '...'; open OUT, '>', $path or die $!; ## write data to file close OUT; system qq[ fortranProg $path ]; ...

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://809620]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-09-02 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls