[ncl-talk] Passing a variable value from NCL to bash Script
Dave Allured - NOAA Affiliate
dave.allured at noaa.gov
Wed Mar 1 12:56:22 MST 2017
Here is a method using only standard out, and no temp file. The leading
colons (:) are needed in this case, to ensure unique match strings.
program.ncl:
;;Perform certain computations ...
print (":w1_out=" + w1)
print (":dw1_out=" + dw1)
exit
loop.sh:
ncl_out="`ncl k1=${w1} dw11=${dw1} opti.error.ncl`"
w1=`echo "$ncl_out" | grep :w1_out= | cut -f2 -d=`
dw1=`echo "$ncl_out" | grep :dw1_out= | cut -f2 -d=`
echo $w1
echo $dw1
--Dave
On Wed, Mar 1, 2017 at 8:38 AM, Alan Brammer <abrammer at albany.edu> wrote:
> Think Rick + Dennis answered the reversed question. To pass a value out
> of NCL you probably need to write it to a file or some form.
>
> When you call system or systemfunc NCL launches a sub shell, so
> environment variables will disappear as soon as the call is finished.
>
> To stick with the bash loop, writing the variables to a text file would be
> the simplest solution. Then set the bash variables to the contents of the
> text file(s).
>
> asciiwrite("w1", w1 )
> asciiwrite("dw1", dw1)
> ....
> ....
> w1=$(cat w1)
> dw1=$(cat dw1)
>
> Currently though, you're appending the ncl script to opti.error.ncl on
> each loop. Which doesn't seem right. Also it seems cleaner, given the
> pseudocode, to just do the loop within NCL and define the main script as a
> function with the w1, dw1 arguments.
>
>
> On Wed, Mar 1, 2017 at 8:59 AM, Rick Brownrigg <brownrig at ucar.edu> wrote:
>
>> Hi,
>>
>> If I understand correctly, you can pass in the values on the command-line
>> invocation of NCL as:
>>
>> ncl w1=X dw1=Y
>>
>> where X and Y are the actual values. To pass in string arguments, we
>> tell people to place quotes around the whole var=value pair, like:
>>
>> ncl 'myStringVar="foo"'
>>
>> You can find more info about passing in arguments at:
>>
>> http://ncl.ucar.edu/Document/Manuals/Ref_Manual/NclCLO.shtml
>>
>> Hope that helps...
>> Rick
>>
>> On Wed, Mar 1, 2017 at 12:20 AM, Abheera Hazra <hazra.abheera at gmail.com>
>> wrote:
>>
>>> Hi,
>>>
>>> I am using NCL to calculate a few terms for an optimization algorithm
>>> and I am calling the NCL function from within a 'while loop' in a bash
>>> script. The 'while loop' runs till a certain condition is met and for the
>>> condition to be met, the bash script needs to accept a new value for an
>>> existing variable from the NCL script. I have tried using system,
>>> systemfunc with even the export command but nothing seems to work. The code
>>> looks as follows:
>>>
>>> ------------------------------------------
>>> #!/bin/bash
>>>
>>> w1=0.4
>>> dw1=0.1
>>> inc=0.001
>>>
>>> while awk 'BEGIN { if ('$dw1'<'$inc') {exit 1}}'; do
>>>
>>> cat <<'EOF' >> opti.error.ncl
>>>
>>> ;;;;Perform certain computations on w1 and update the w1 and dw1
>>> w1=nw1
>>> dw1=ndw1
>>>
>>> system("w1=" + w1)
>>> system("dw1=" + dw1)
>>>
>>> ;;;;w1 and dw1 need to be passed to the bash script
>>>
>>> EOF
>>>
>>>
>>> ncl k1=${w1} dw11=${dw1} opti.error.ncl
>>>
>>> echo $w1
>>> echo $dw1
>>>
>>> done
>>> ------------------------------------------
>>>
>>> But this does not update w1 and dw1 values. Any input would be greatly
>>> appreciated.
>>>
>>> Thanks,
>>> Abheera
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170301/a57daea0/attachment.html
More information about the ncl-talk
mailing list