Hello,

I'm reading a netCDF file to extract a timeseries using a command line named ncks.

I dump its output in a temporary file and then I read the temporary file to extract the values and push them into an array.

Is there a way to store in memory the extracted records (@tmplines in the code below) without writing them on a temporary file?

Since I have to call the subroutine thousands of times I imagine that it would speed up things.

Thanks for any suggestion and best regards.

use strict; use IPC::Run qw( run ); use Tie::File; use Fcntl 'O_RDONLY'; # ... cut ... my @ts = get_time_series(File=>$ncfile,Variable=>$ncvar,Id=>$nc_id); # ... cut ... sub get_time_series { my %args = @_; my $file = $args{File}; my $variable = $args{Variable}; my $id = $args{Id}; my $tmpfile = "$id.tmp"; # Example: # ncks -v vmro3 -d station,1,1 myfile.nc | grep "station\[" # my $command = qq(ncks -v $variable -d station,$id,$id $file | grep + \"station\\[\" > $tmpfile); run $command; # This will write in file the following records: #station[3385]=3386 #time[0]=3287.02083333 station[3385]=3386 vmro3[3385]=1.53524e-08 mole + mole-1 #time[1]=3287.0625 station[3385]=3386 vmro3[7513]=1.55109e-08 mole mol +e-1 #time[2]=3287.10416667 station[3385]=3386 vmro3[11641]=1.56481e-08 mol +e mole-1 my @ts; tie my @tmplines, 'Tie::File', $tmpfile, mode => O_RDONLY; foreach my $ii (1..$#tmplines) { my $line = $tmplines[$ii]; my @pa = anytrim(split(/\s/,$line)); my ($dummy,$value) = split(/=|\s/,$pa[2]); push @ts, $value; } unlink $tmpfile or die "$tmpfile: $!"; return @ts; }

In reply to Execute a command line and save in memory its output by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.