# [ncl-talk] how can I avoid dividing by zero?

Barry Lynn barry.h.lynn at gmail.com
Wed Aug 22 06:33:35 MDT 2018

Hi:

I am not sure of all the steps, but small numbers are not NaNs.  Perhaps
you are dividing by close to zero, when you should limit to a number
reasonably above?

Is that possible to do?

On Wed, Aug 22, 2018 at 2:40 PM Sri Nandini <snandini at marum.de> wrote:

>
>
> Yes.
>
> 1) I use geopotential height and the values were originally like this:
> 1080.752
>
>    T41    = f->Z3(:,{850},:,:)
>
>    printVarSummary(T41)                        ; [Time|12]x [lat | 96] x
> [lon | 144]
>    T41 at _FillValue = -9.96921e+36
>    print(T41)
>
>
> 2) Then i performed: dim_avg_n_Wrap and values were like: 0.0009252814
>
>    aveX    = dim_avg_n_Wrap(T41,0)
>    printVarSummary(aveX)                      ; (lat,lon)
>
>    aveX at _FillValue =9.96921e+36
>    print(where(aveX.ne.0, aveX, aveX at _FillValue))
>
>  values like this:  1080.752
>
>    aveX = 1. / where(aveX.ne.0, aveX, aveX at _FillValue)
>    print(aveX)
>
> 0.0009252814
>
> 3) I use this in the eady growth rate equation and that gives me nan
> values for few gridpoints.
>    egr = eady_growth_rate(aveX2, aveX1, aveX, XLAT, 0,  1)
>    printVarSummary(egr)
>
>    printMinMax(egr,0)
>
> Variable: egr
> Type: double
> Total Size: 110592 bytes
>             13824 values
> Number of Dimensions: 2
> Dimensions and sizes:    [lat | 96] x [lon | 144]
> Coordinates:
>             lat: [ -90..  90]
>             lon: [   0..357.5]
> Number Of Attributes: 3
>   _FillValue :    9.969209968386869e+36
>   long_name :    maximum eady growth rate
>   units :
> (0)    maximum eady growth rate : min=nan   max=nan
>
> 4) All the inputs into the eady growth rate equation have data points but
> some are almost zeros, as such i assume it gets masked out to nan?
>
>
>
>
>
>
> On Aug 22, 2018 12:15:37 PM, Barry Lynn wrote:
>
> Hi:
>
> Are the points that actually have data producing NaNs from the
> calculations?
>
> Barry
>
> On Wed, Aug 22, 2018 at 12:56 PM Sri Nandini <snandini at marum.de> wrote:
>
>
>
> Thank you
>
> I used it and have seen that i have data points except some gridpoints
> which point to nan and the fillvalue both.
> I think then there is no data over these points.
>
>
>
>
>
>
>
>
>
>
>
>
> On Aug 22, 2018 10:46:56 AM, Barry Lynn wrote:
>
> Hello Sri:
>
> I would try it this way.
>
> Set *y* to a _FillValue value where it is equal to 0, and then can do the
> divide:
>    y at _FillValue = 1.E36 ; or something like this.
>    print(where(y.ne.0,y, at _FillValue)) ;  I am assuming this works.
> If nothing prints then maybe you have no values.
> In any case, try the next two lines.
>    y = *where*(y.ne.0,y,y at _FillValue)
>
>   yinv = 1. / y
>
> However, just to check, I would
>
>
>
> On Wed, Aug 22, 2018 at 11:39 AM Sri Nandini <snandini at marum.de> wrote:
>
> Hello
>
> how can I remove the zero values to avoid dividing by zero the error I got
> is as the following:
>
>  fatal:divide: Division by 0, Can't continue
>  fatal:Div: operator failed, can't continue
>
> I have tried using   yinv = 1. / where(y.ne.0, y, y at _FillValue) but now
> it gives me all nan values
>
> Heres the bit of my code
>
>    T41    = f->Z3(:,{850},:,:)
>
>    printVarSummary(T41)
>    T41 at _FillValue = -9.96921e+36
>
>    aveX    = dim_avg_n_Wrap(T41,0)
>    printVarSummary(aveX)                      ; (lat,lon)
>    T41 at _FillValue = -9.96921e+36
>
>    aveX = 1. / where(aveX.ne.0, aveX, aveX at _FillValue)
>
> aveX1 and aveX2 below are of same type as aveX.
>    XLAT = conform ( aveX2 , xlat , 0 )
>    printVarSummary(XLAT)
>
>    egr = eady_growth_rate(aveX2, aveX1, aveX, XLAT, 0,  1)
>
> Output
> ===========================
> Variable: egr
> Type: double
> Total Size: 110592 bytes
>             13824 values
> Number of Dimensions: 2
> Dimensions and sizes:   [lat | 96] x [lon | 144]
> Coordinates:
>             lat: [ -90..  90]
>             lon: [   0..357.5]
> Number Of Attributes: 3
>   _FillValue :  -9.969209968386869e+36
>   long_name :   maximum eady growth rate
>   units :
> (0)     maximum eady growth rate : min=nan   max=nan
>
>
> Any advice is much appreciated
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
>
> --
> Barry H. Lynn, Ph.D
> Senior Associate Scientist, Lecturer,
> The Institute of the Earth Science,
> The Hebrew University of Jerusalem,
> Givat Ram, Jerusalem 91904, Israel
> Tel: 972 547 231 170
> Fax: (972)-25662581
>
> C.E.O, Weather It Is, LTD
> Weather and Climate Focus
> http://weather-it-is.com
> Jerusalem, Israel
> Local: 02 930 9525
> Cell: 054 7 231 170
> Int-IS: x972 2 930 9525
>
>
>
>
> --
> Barry H. Lynn, Ph.D
> Senior Associate Scientist, Lecturer,
> The Institute of the Earth Science,
> The Hebrew University of Jerusalem,
> Givat Ram, Jerusalem 91904, Israel
> Tel: 972 547 231 170
> Fax: (972)-25662581
>
> C.E.O, Weather It Is, LTD
> Weather and Climate Focus
> http://weather-it-is.com
> Jerusalem, Israel
> Local: 02 930 9525
> Cell: 054 7 231 170
> Int-IS: x972 2 930 9525
>
>
>

--
Barry H. Lynn, Ph.D
Senior Associate Scientist, Lecturer,
The Institute of the Earth Science,
The Hebrew University of Jerusalem,
Givat Ram, Jerusalem 91904, Israel
Tel: 972 547 231 170
Fax: (972)-25662581

C.E.O, Weather It Is, LTD
Weather and Climate Focus
http://weather-it-is.com
Jerusalem, Israel
Local: 02 930 9525
Cell: 054 7 231 170
Int-IS: x972 2 930 9525
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180822/baca12e5/attachment.html>