<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Emma,<br>
    <br>
    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:<br>
    <tt><br>
    </tt><tt>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"<br>
      <br>
      ...<br>
      <br>
      xq_ref_bits = rm_single_dims( getbitsone( toushort(43679) ) )  ;
      Convert mask into bit array ( 1010101010011111b = 43679 )<br>
      ii_bits = getbitsone( ii )                                     ;
      Convert QC flag into bit array<br>
      <br>
      bitand           = xq_ref_bits * ii_bits   ; perform bit-wise AND<br>
      q_save(nt,nx)    = dim_product(bitand)     ; pass if all bits are
      zero</tt><br>
    <br>
    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:<br>
    <br>
    <tt><tt>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"<br>
        <br>
        ...<br>
        <br>
      </tt></tt><tt>quality_bits    = getbitsone(quality)               
                     ; Convert all QC flags into bit array</tt><tt><br>
    </tt><tt><tt>xq_ref_bits     = rm_single_dims( getbitsone(
        toushort(43679) ) )   ; Convert mask into bit array (
        1010101010011111b = 43679 )<br>
      </tt>xq_ref_bits_big = conform( quality_bits, xq_ref_bits, 2 )   
             ; Expand xq_ref_bits to be the same size as quality_bits</tt><tt><br>
    </tt><tt><br>
    </tt><tt>bitand     = xq_ref_bits_big * quality_bits         ;
      perform bit-wise AND</tt><tt><br>
    </tt><tt>q_save     = dim_product(bitand)                    ; array
      of same size as quality</tt><br>
    <tt><br>
    </tt><tt>return(q_save)</tt><br>
    <br>
    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.<br>
    <br>
    - Walter<br>
    <br>
    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"<br>
    <br>
    <br>
    <div class="moz-cite-prefix">On 15-Jan-15 05:35, Emma Andersson
      wrote:<br>
    </div>
    <blockquote
      cite="mid:D0ECD5BB-BF71-4975-A3A9-06931E34D2F9@chalmers.se"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
      Hi NCL-talk,
      <div class=""><br class="">
      </div>
      <div class="">I’m having some trouble with a my own written
        function that I call within a loop. </div>
      <div class=""><br class="">
      </div>
      <div class="">The function I’ve written converts quality flags
        from a satellite data set into an array of integers with zeros
        and ones.</div>
      <div class="">The quality flags are given in ushort and
        corresponds to a 16-bit number. What I do is that I check each
        pixels</div>
      <div class="">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</div>
      <div class="">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, </div>
      <div class="">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.</div>
      <div class=""><br class="">
      </div>
      <div class="">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, </div>
      <div class="">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 </div>
      <div class="">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 </div>
      <div class="">the output in the main script after it being used?
        Would it be better to use a procedure instead of a function?</div>
      <div class=""><br class="">
      </div>
      <div class="">Kind regards,</div>
      <div class="">Emma Andersson</div>
      <div class=""><br class="">
      </div>
      <div class="">
        <div class="">-----------------------------------------------------------------------------------</div>
        <div class="">PhD student</div>
        <div class="">Global Environmental Measurement Techniques and
          Modelling</div>
        <div class="">Earth and Space Sciences</div>
        <div class="">Chalmers University of Technology</div>
        <div class="">Gothenburg, Sweden</div>
        <div class="">emma.andersson(at)<a moz-do-not-send="true"
            href="http://chalmers.se" class="">chalmers.se</a></div>
        <div class="">
          <div class="">-----------------------------------------------------------------------------------</div>
          <div class=""><br class="">
          </div>
        </div>
      </div>
      <div class=""><br class="">
      </div>
      <div class=""><br class="">
      </div>
      <div class=""><br class="">
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
<a class="moz-txt-link-freetext" href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a>
</pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Walter Kolczynski, Jr.
Global Ensemble Team
NOAA/NWS/NCEP/EMC (via I.M. Systems Group)
(301) 683-3781</pre>
  </body>
</html>