[ncl-talk] In condtnl_expr missing .and. false results depend on the order

Mary Haley haley at ucar.edu
Fri Oct 2 10:53:31 MDT 2015


The logical missing value is one of those things that causes many of us
grief.  In some cases I wish we'd never implemented the concept of a
_FillValue for a logical type, but I understand why it was done.

The issue becomes, then, how to best interpret logicals and missing values
in conditional expressions.

What you are seeing is simply the result of "lazy evaluation".  Please see
this note in our V5.2.0 release, where we described an important change
that was made with regard to this behavior:

http://www.ncl.ucar.edu/prev_releases.shtml#LazyEvaluationUpdate5.2.0
http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclExpressions.shtml#LazyEvaluation

When you have  a ".and." type of evaluation, and the first part of it
evaluates to either "False", then it doesn't bother evaluating the rest of
your expression. So, if it encounters a False first, then False will be
returned. However, in every case, if there's a missing somewhere in the
expression, it's going to return missing.

Here are some samples:

ncl 0> msg = new(1,logical)   ; create a variable that is missing
ncl 1> print("msg = " + msg)
(0)     msg = Missing
ncl 2> print(False.and.msg)
(0)     False
ncl 3> print(msg.and.False)
(0)     Missing
ncl 4> print(True.and.msg)
(0)     Missing
ncl 5> print(msg.and.True)
(0)     Missing
ncl 6> print(msg.and.msg)
(0)     Missing
ncl 7>

In order to correctly evaluate expressions that may contain missing values,
you should use the "ismissing" function. This way you are always comparing
pure Trues and Falses, rather than missing values.

Depending on how you want the expression to evaluate when missing values
are encountered, you would use something like:

   a = where((.not.ismissing(x).and.x.lt.2.) .and.
(.not.ismissing(y).and.y.lt.2.), a at _FillValue, a)

The above is just an example. You need to figure out the proper way to
using the "ismissing" logic for your own situation.

--Mary


On Thu, Oct 1, 2015 at 5:54 PM, Qi Tang <tang30 at llnl.gov> wrote:

> Hi,
>
> It seems that in the condtnl_expr, the value of missing .and. false give
> the value of the first term:
>
> missing .and. false -> missing
> false .and. missing -> false
>
> Is this a bug? The results to both expressions above should be missing.
> Otherwise, it makes it really easy to make mistakes when using the where
> function when there can be missing in the condtnl_expr.
>
> A short example below demonstrates this problem. I am using v6.2.1 on
> Redhat 6.7.
>
> Qi
>
> ==========
>    x = new(5,float,-99.)
>    y = new(5,float,-99.)
>    x(0) = 1.
>    x(2) = 3.
>    x(4) = 4.
>    y(0) = 1.
>    y(1) = 3.2
>    print(x)
>    print(y)
>
>    a = x
>    b = y
>    c = y
>
>    a = where(x.lt.2. .and. y.lt.2., a at _FillValue, a)
>    b = where(x.lt.2. .and. y.lt.2., b at _FillValue, b)
>    c = where(y.lt.2. .and. x.lt.2., c at _FillValue, c)
>    print(a)
>    print(b)
>    print(c)
> ========
>
> The arrays a, b, c should be identical, but they are not. The output is:
>
> Variable: x
> Type: float
> Total Size: 20 bytes
>              5 values
> Number of Dimensions: 1
> Dimensions and sizes:    [5]
> Coordinates:
> Number Of Attributes: 1
>    _FillValue :    -99
> (0)     1
> (1)    -99
> (2)     3
> (3)    -99
> (4)     4
>
> Variable: y
> Type: float
> Total Size: 20 bytes
>              5 values
> Number of Dimensions: 1
> Dimensions and sizes:    [5]
> Coordinates:
> Number Of Attributes: 1
>    _FillValue :    -99
> (0)     1
> (1)    3.2
> (2)    -99
> (3)    -99
> (4)    -99
>
> Variable: a
> Type: float
> Total Size: 20 bytes
>              5 values
> Number of Dimensions: 1
> Dimensions and sizes:    [5]
> Coordinates:
> Number Of Attributes: 1
>    _FillValue :    -99
> (0)    -99
> (1)    -99
> (2)     3
> (3)    -99
> (4)     4
>
> Variable: b
> Type: float
> Total Size: 20 bytes
>              5 values
> Number of Dimensions: 1
> Dimensions and sizes:    [5]
> Coordinates:
> Number Of Attributes: 1
>    _FillValue :    -99
> (0)    -99
> (1)    -99
> (2)    -99
> (3)    -99
> (4)    -99
>
> Variable: c
> Type: float
> Total Size: 20 bytes
>              5 values
> Number of Dimensions: 1
> Dimensions and sizes:    [5]
> Coordinates:
> Number Of Attributes: 1
>    _FillValue :    -99
> (0)    -99
> (1)    3.2
> (2)    -99
> (3)    -99
> (4)    -99
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20151002/65eca7da/attachment.html 


More information about the ncl-talk mailing list