[ncl-talk] Using functions
Wei Huang
huangwei at ucar.edu
Thu Jan 15 08:59:17 MST 2015
Emma,
The line:
q_save(nt,nx) = where(xq .eq. 0, 0, 1)
should be:
q_save = where(xq .eq. 0, 0, 1)
Wei
================================================
1850 Table Mesa Dr.
Boulder, CO 80307
Phone: 303-497-8924
On Thu, Jan 15, 2015 at 8:57 AM, Wei Huang <huangwei at ucar.edu> wrote:
> Emma,
>
> Your function is slow is because you did not take the advantage of NCL's
> array operation, but looped through individuals.
>
> You may try to change your code to something like:
>
> undef("Qsave")
> function Qsave(quality:numeric)
> local ntime,nxtrack,nx,ii,ic,bit,bstr,nn,mm,xq,xq_tmp,xq_ref,xq_ref1
> begin
> xq_ref1 = "1010101010011111" ; Never allow these flags to be set in the
> filtering
> xq_ref = stringtocharacter(xq_ref1)
> nbits = dimsizes(xq_ref)
> q_save = new(dimsizes(quality),integer)
> dims = dimsizes(quality)
> ntime = dims(2)
> nxtrack = dims(1)
> ii = touint(quality)
> bstr = new((/ntime, nxtrack/), string)
> bstr = ""
> ic = ii
>
> do nn = 0, nbits-1
> bit = (ic % 2) ; pick off the lowest bit
> ic = ic/2 ; shift right one bit, drop lowest
> bstr = bit + bstr
> end do ; nn
>
> xq = toint64(bstr)
> q_save(nt,nx) = where(xq .eq. 0, 0, 1)
>
> return(q_save)
> end
>
> and let us know if this works for your case.
>
> Regards,
>
> Wei
>
> ================================================
> 1850 Table Mesa Dr.
> Boulder, CO 80307
> Phone: 303-497-8924
>
> On Thu, Jan 15, 2015 at 3:35 AM, Emma Andersson <
> emma.andersson at chalmers.se> 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
>>
>> -----------------------------------------------------------------------------------
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> 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/20150115/988bb1b7/attachment.html
More information about the ncl-talk
mailing list