[ncl-talk] Using functions

Walter Kolczynski walter.kolczynski at noaa.gov
Fri Jan 16 12:37:39 MST 2015


Emma,

You're best bet would be to use some tricks to eliminate the loops 
entirely. If you can convert the bitwise mask and the input into bit 
arrays, you can just multiply them to perform the bit-wise AND. 
Replacing your innermost loop (over the bits) would look something like:

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

...

xq_ref_bits = rm_single_dims( getbitsone( toushort(43679) ) )  ; Convert 
mask into bit array ( 1010101010011111b = 43679 )
ii_bits = getbitsone( ii )                                     ; Convert 
QC flag into bit array

bitand           = xq_ref_bits * ii_bits   ; perform bit-wise AND
q_save(nt,nx)    = dim_product(bitand)     ; pass if all bits are zero

But, we can get rid of ALL the loops by making use of conform to 
duplicate the bitmask into an array of the same size as all the QC flags:

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

...

quality_bits    = getbitsone(quality)                ; Convert all QC 
flags into bit array
xq_ref_bits     = rm_single_dims( getbitsone( toushort(43679) ) )   ; 
Convert mask into bit array ( 1010101010011111b = 43679 )
xq_ref_bits_big = conform( quality_bits, xq_ref_bits, 2 )        ; 
Expand xq_ref_bits to be the same size as quality_bits

bitand     = xq_ref_bits_big * quality_bits         ; perform bit-wise AND
q_save     = dim_product(bitand)                    ; array of same size 
as quality

return(q_save)

Caveat: I did not rigorously test this code, only enough to make sure it 
runs. Make sure you thoroughly test it to make sure it produces the 
expected output.

- Walter

P.S. If you expect the input to always be a ushort, you should enforce 
this in your argument list instead of using the generic "numeric"


On 15-Jan-15 05:35, Emma Andersson wrote:
> Hi NCL-talk,
>
> I’m having some trouble with a my own written function that I call 
> within a loop.
>
> The function I’ve written converts quality flags from a satellite data 
> set into an array of integers with zeros and ones.
> The quality flags are given in ushort and corresponds to a 16-bit 
> number. What I do is that I check each pixels
> bit number if certain flags has been set. I compare with a reference 
> bit number which contains the flags that I don’t want to be set
> and then determines whether my new array value should be set to a one 
> or a zero. I have basically written a kind of “bitand” function,
> where if the output from the comparison are all zeros then I set my 
> new array value to zero. I hope this description was understandable.
>
> What happens when I call my function from a main script, is that it 
> goes slower and slower for each step in the loop. My question is,
> why it goes slower? Is something accumulating inside the function for 
> each time I call it? I’m quite sure it doesn’t accumulate outside
> in the main script, because I did some print statements inside the 
> function and it was clearly there it was going slower. I also delete
> the output in the main script after it being used? Would it be better 
> to use a procedure instead of a function?
>
> Kind regards,
> Emma Andersson
>
> -----------------------------------------------------------------------------------
> PhD student
> Global Environmental Measurement Techniques and Modelling
> Earth and Space Sciences
> Chalmers University of Technology
> Gothenburg, Sweden
> emma.andersson(at)chalmers.se <http://chalmers.se>
> -----------------------------------------------------------------------------------
>
>
>
>
>
>
>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

-- 
Walter Kolczynski, Jr.
Global Ensemble Team
NOAA/NWS/NCEP/EMC (via I.M. Systems Group)
(301) 683-3781

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20150116/574b353d/attachment.html 


More information about the ncl-talk mailing list