Re: A Simple where() Function for NCL Versions prior to 4.3.0

From: Lunde, Bruce N CIV NAVOCEANO, NP1 <bruce.lunde_at_nyahnyahspammersnyahnyah>
Date: Fri, 31 Aug 2007 16:57:21 -0500

Hello,

In case someone is interested, here is an updated version of
a where-function for NCL versions that don't have the where().
It is a simple where-function, not as powerful as the new
built-in where().

Updates include:
* A name change to avoid conflicts.
* Some error checks.
* Order of input arguments to match where() instead of mask().
* Addition of copy_VarCoords() (now requires contributed.ncl)

Bruce

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;
; NAME: wheres = WHERE substitute Scalar
; PURPOSE:
; A where() function for NCL versions prior to V4.3.0 which performs
simple
; substitutions such as
; anew = where(aold.gt.3.3, 45.5 , aold)
; MASK , value-if-TRUE, value-if-FALSE
; ARRAY , SCALAR , ARRAY
; and preserves the existence/nonexistence of any _FillValue
attribute.
; NOTES:
; * The second argument (the "True" value) MUST be a scalar.
; * Uses "copy_VarCoords()" from "contributed.ncl"
; HISTORY:
; 07-08-20 Bruce Lunde. Based on suggestions from Dennis Shea and Mary
Haley
; in NCL-Talk archives.
; 07-08-24 Bruce Lunde. Changed order of arguments to match NCL's
where()
; instead of NCL's mask().
; EXAMPLE:
; a=random_uniform(-2,2,(/2,2,2/))
; print(wheres(a.gt.1.1,888,a))
; a@_FillValue = 7777
; print(wheres(a.lt.-0.5,888,a))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;

undef("wheres")
function wheres(foomask,fooval,foo)
local dims_fooval, rank_fooval, ierror, fvexists, fvold, ans
begin
  dims_fooval = dimsizes(fooval)
  rank_fooval = dimsizes(dims_fooval)
  ierror = False
  if( rank_fooval .ne. 1 )then
    ierror = True
  else
    if( dims_fooval .ne. 1 )then
      ierror = True
    end if
  end if
  if( ierror )then
    print("*** ERROR in Function wheres().")
    print("*** Second Input Argument is not a Scalar.")
    print("*** EXIT.")
    exit
  end if
  fvexists = False
  if( isatt(foo,"_FillValue") )then
    fvexists = True
    fvold = foo@_FillValue
  end if
  foo@_FillValue = fooval
  ans = mask(foo,foomask,False)
  copy_VarCoords(foo,ans)
  delete(ans@_FillValue) ;.. Necessary so that fooval not changed to
_FillValue
  if( fvexists )then
    foo@_FillValue = fvold
    ans@_FillValue = fvold
  else
    delete(foo@_FillValue)
  end if
  return(ans)
end

_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Aug 31 2007 - 15:57:21 MDT

This archive was generated by hypermail 2.2.0 : Sat Sep 01 2007 - 08:04:57 MDT