[Thread Prev][Thread Next][Index]

Re: [ferret_users] reusing information from variable definitions



Hi Marco,

I have a few ideas, most of which I think you know, but there may be some new details.  As you know, the `var,return=` keywords are intended to let you get information that might otherwise appear just in the output of commands such as SHOW.  For convenience,
https://ferret.pmel.noaa.gov/Ferret/documentation/users-guide/variables-xpressions/EMBEDDED-XPRESSIONS#Chapter3-RETURN

Other useful constructs are the ones for access to attributes of files and variables, see here:
https://ferret.pmel.noaa.gov/Ferret/documentation/users-guide/variables-xpressions/VARIABLES#_VPINDEXENTRY_attributes

To find out how many datasets are open, if the current default dataset corresponds to the last opened, you could define a variable based on the dataset, so for instance this which uses ..nvars, the number of variables in the dataset.

yes? use etopo5
yes? use etopo20
yes? use etopo60

yes? let nvars = ..nvars
yes? let last_dset = `nvars,return=dsetnum`
 !-> MESSAGE/CONTINUE 3
3

Note things can get messy, if for example the set of scripts may open datasets and close some of them, or if we did a "set data 2" here so the default dataset isn't the highest number opened.  The dataset number for each open dataset stays the same within a Ferret session.


The LET/LIKE=  acts just the same as this grave-accent evaluation:

yes? let/units="`aa,return=units`"/"`aa,return=title`" tr = aa+123

except that it transfers all of the attributes. In those grave-accent evaluations, aa has to refer to the variable in a particular dataset.  Once the dataset number is known, you could use SET VARIABLE/UNITS= /TITLE=

yes? define symbol dset = `ndset`
yes? set variable/units="`aa[d=($dset)],return=units`"/title='`aa[d=($dset)],return=units`" tr

(I see that we should have a SET VARIABLE/LIKE= to pass all of the attributes!)



On 2/11/2020 2:30 AM, Marco van Hulten wrote:
Hello—

On 11 Feb 03:09 Fiedler, Russell (O&A, Hobart) wrote:
It looks like the /LIKE qualifier could be what you're after.

https://ferret.pmel.noaa.gov/Ferret/documentation/users-guide/commands-reference/DEFINE#_VPINDEXENTRY_1348

All attributes are inherited (apart from missing_value, scaling and offset). You can override some if you want.

e.g. (From the examples)



yes? let/like=temperature/title="Surface Temperature" sst = temperature[z=0:5@MAX]

so

let/like=aa bb=aa+666
let/like=bb/title="`bb,return=original_definition`" cc = bb

which should be the same as

let/like=aa/title="`bb,return=original_definition`" cc = bb
Yes, this would be the solution to the problem that I stated.  The
/LIKE functionality is very useful.  But I forgot to mention something
that complicates the issue.  I will try to state the issue correctly
now.

Several datasets are loaded.  It is known that AA is in one of the
datasets but not in which one.  First, Tr is set as a function of AA:

     let Tr = AA + 123.

If I were to do

     let/like=AA Tr = AA + 123.

this would often result in failure as the current dataset may not be
containing AA.  This 'Tr' is a general variable that I refer to in
later scripts, but AA's name is only known at the stage of this command.

The next thing I do is check in which dataset Tr is available, so

     repeat/i=1:`ndsets` ( if `Tr,return=isReady` then ;\
         let ndset = `i` ;\
         exit/loop \
     )
     set data `ndset`

Note that I cannot determine 'ndsets' before defining Tr.  Only after
ndsets is determined, I can set attributes, e.g. like

     def attr Tr.units = "`AA.units`"

or, if not depending on current default dataset,

     def symb Ndset = `ndset`  ! avoid embedded ````
     def attr Tr.units = "`AA.units[d=($Ndset)]`"

or by redefining

     set data `ndset`
     let/like=AA Tr = AA + 123.

or, again not depending on current dataset (apparently both in the
/LIKE and the expression I must say what the right dataset is, which is
somewhat inconsistent as let Tr=AA; set data `ndset` works fine, i.e.,
without specifying the dataset in the expression):

     let/like=AA[d=`ndset`] Tr = AA[d=`ndset`] + 123.

But none of this would work, because I need to know the name of AA
(whether I set an attribute of Tr or redefine Tr with /LIKE).


I think the above is a MWE equivalent to what I try to accomplish in a
more complicated setting.  For further rationale of my challenge, I
explain it below concretely (see http://complot.nongnu.org/ for full
context).

One may ask why I want to refer to AA only once (let Tr = AA + 123.).
This is not just an issue of inelegant code duplication.  My main user
script is called with a tracer name argument, so in short this happens:

     def symb Tr = ($1)  ! user may have given "GbSi" as an argument
let GbSi = GSi ! variable expected in one of the datasets
     let SbSi = BSi
     let TPoC = POC
     ...
     ! Here we determine where ($Tr) is in (ndset).
     ! And then we do the proper initialisation:
     set data `ndset`
     let/like=GSi Tr = ($Tr)
     !        ^^^
     !        this we don't know

Am I overlooking something obvious?

—Marco


[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement