[ncl-talk] problem with dim_avg_n and userdefined function (numeric and float)

Mary Haley haley at ucar.edu
Tue Oct 28 08:08:38 MDT 2014


Thanks for providing a short script that very clearly illustrates the
problem.

The problem seems to be with the "0" that's being passed in as the second
argument to max2n.

If I change this to a variable:

; abnormal  loops !!!!
zero = 0
do i=1, 2
    print("i="+(/i/))
    print("Before v=max2n(i-3,0)") ; Printed, got here
    v=max2n(i-3,zero)
    print("After v=max2n(i-3,0)") ; Crashed, didn't get here
    print("v is " + v)
end do

then the code runs file.

I have filed a ticket on this, NCL-2098.  Hopefully the work-around of
using a variable instead of passing in a direct value is acceptable for now.

--Mary


On Tue, Oct 28, 2014 at 5:39 AM, 李嘉鹏 <lijpbasin at 126.com> wrote:

>
> I agree with 陶玮 on the second script.
>
> To be sure, I replaced all the variable and function names while checking
> for typos, and eliminated as much code as possible to make the problem look
> more obvious (at least obvious enough for me) like a bug.
>
>  - The program crashed in the second loop (i = 2), causing a "Segmentation
> fault".
>
>  - I thought `numeric` is equivalent to `integer` since two integer values
> are passed to the function, so I tried `integer` too, same problem, and
> `float` did not cause any trouble.
>
>
>
>
> Here is the script I tested:
>
> undef("max2n")
> function max2n(n1:numeric, n2:numeric) ; Crashes
> ;function max2n(n1:integer, n2:integer) ; Crashes
> ;function max2n(n1:float, n2:float) ; Fine
> begin
>     if (n1 .ge. n2)  then
>         return(n1)
>     else
>         return(n2)
>     end if
> end
>
> begin
>
> ; abnormal  loops !!!!
> do i=1, 2
>     print("i="+(/i/))
>     print("Before v=max2n(i-3,0)") ; Printed, got here
>     v=max2n(i-3,0)
>     print("After v=max2n(i-3,0)") ; Crashed, didn't get here
>     print("v is " + v)
> end do
>
> end
>
>
> At 2014-10-28 12:53:25, "陶玮" <1201111616 at pku.edu.cn> wrote:
> >Hi, everyone.
> >Q1:
> >I was using NCL V6.2.0 to extract data from WRF results, however, it
> seemed that there was a bug in function of dim_avg_n.To simplify matters, I
> used dummy data, the script is as follows:
> >QUESTION
> 1:-----------------------------------------------------------------
> >load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> >load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> >load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl"
> >
> >begin
> >
> >nyears=5
> >ncases=4
> >nspec=4
> >ntimes_real=680
> >nvlevs=2
> >
> >colspectmp=new((/nyears,ncases,nspec,ntimes_real,100,100/), float)
> >colspec=new((/nyears,ncases,nspec,100,100/), float)
> >colspectmp=1.
> >
> >specdntmp=new((/nyears,ncases,nspec,ntimes_real,nvlevs,100,100/), float)
> >specdn=new( (/nyears,ncases,nspec,nvlevs,100,100/), float)
> >specdntmp=1.
> >
> >;abnormal !!!! It seemed that no values were passed to colspec !!!!
> >colsepc=dim_avg_n((/colspectmp/), 3)
> >print(avg(colspec))
> >
> >;normal !!!!
> >specdn=dim_avg_n((/specdntmp/), 3)
> >print(avg(specdn))
> >
> >end
>
> >--------------------------------------------------------------------------------------
> >
> >Q2:What's the difference between numeric and float when using userdefined
> function. though max((/a,b/)) would do the same thing, I am
> >eager to know the answer.
>
> >QUESTION2---------------------------------------------------------------------------------
> >load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> >load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> >load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl"
> >
> >undef( "fmax2v")
> >function fmax2v(x:float, y:float)
> >begin
> >        if (x .ge. y)  then
> >                return(x)
> >        else
> >                return(y)
> >        end if
> >end
> >
> >undef( "max2v")
> >function max2v(x:numeric, y:numeric)
> >begin
> >        if (x .ge. y)  then
> >                return(x)
> >        else
> >                return(y)
> >        end if
> >end
> >
> >begin
> >
> >x=3
> >
> >; normal loops !!!!!
> >do ii=1, 2
> >do jj=1, 3
> >        print("ii="+(/ii/)+"   jj="+(/jj/))
> >        print("1:  "+(/fmax2v(ii-x,0)/))
> >        tmp=(/fmax2v(ii-x,0)/)
> >        print("2:  "+ (/tmp/))
> >end do
> >end do
> >
> >; abnormal  loops !!!!
> >do ii=1, 2
> >do jj=1, 3
> >        print("ii="+(/ii/)+"   jj="+(/jj/))
> >        print("1:  "+(/max2v(ii-x,0)/))
> >        tmp=(/max2v(ii-x,0)/)
> >        print("2:  "+ (/tmp/))
> >end do
> >end do
> >
> >
> >end
> >--------------------------------------------------------
> >
> >With best regards.
> >
> >
> >
> >
> >
> >
> >_______________________________________________
> >ncl-talk mailing list
> >List instructions, subscriber options, unsubscribe:
> >http://mailman.ucar.edu/mailman/listinfo/ncl-talk
> _______________________________________________
> 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/20141028/b7c6c5e6/attachment.html 


More information about the ncl-talk mailing list