# [ncl-talk] Coefficient_of Divergence

Kunal Bali kunal.bali9 at gmail.com
Thu Nov 9 23:26:59 MST 2017

```Dear Dennis Sir,
thanks for the suggestion.
it is working with few time steps, but not for say 442-time steps.

I tried your suggestion, now it is not able to calculate the CoD.

I simply used

;-----calculation of coefficient of divergence
DATA_SINGLE_3D = conform(data_subset, data_single, 0)
x1 = (DATA_SINGLE_3D - data_subset)
x2 = (DATA_SINGLE_3D + data_subset)
* CoD1   = (x1/ where(x2.ne.0, x2, x2 at _FillValue))^2 *
printVarSummary(CoD1)
printMinMax(CoD1, 0)
print("---")

*CoD2 = dim_avg_n_Wrap(CoD1, 0) *   ; (nlat,mlon)
CoD2 at long_name = "Coef of Divergence"
printVarSummary(CoD2)
printMinMax(CoD2,0)

CoD2_avg = avg(CoD2)                        ; small area; no need to
weight
; for plotting only

*CoD_final = sqrt(CoD2)*
printVarSummary(CoD3)
printMinMax(CoD3,0)

CoD3 at lat2d  = lat(ilt1:ilt2,iln1:iln2)    ; use := to force reassignment
of lat2d
CoD3 at lon2d  = lon(ilt1:ilt2,iln1:iln2)    ; ditto

;  res at gsnRightString = "avg(CoD2)="+sprintf("%5.3f", CoD2_avg)
*plot = gsn_csm_contour_map(wks,CoD_final,res)*
the errors are

*Variable: CoD1*
Type: float
Total Size: 1736176 bytes
434044 values
Number of Dimensions: 3
Dimensions and sizes:    [442] x [1] x [982]
Coordinates:
Number Of Attributes: 1
_FillValue :    -28672
(0)    min=0   max=1
(0)    ---

*Variable: CoD2*
Type: float
Total Size: 3928 bytes
982 values
Number of Dimensions: 2
Dimensions and sizes:    [1] x [982]
Coordinates:
Number Of Attributes: 3
long_name :    Coef of Divergence
_FillValue :    -28672
average_op_ncl :    dim_avg_n function was applied
(0)    Coef of Divergence : min=0   max=1

*Variable: CoD3*
Type: float
Total Size: 3928 bytes
982 values
Number of Dimensions: 2
Dimensions and sizes:    [1] x [982]
Coordinates:
Number Of Attributes: 1
_FillValue :    -28672
(0)    min=0   max=1
fatal:ScalarFieldInitialize: Insufficient number of elements in sfDataArray
fatal:Unable to initialize layer-Can't Create
fatal:Unable to access object with id:-4
fatal:PID #-4 can't be found in NhlSetValues
fatal:Unable to access object with id:-4
warning:Unable to add DataItem "(null)" to DataList "cnScalarFieldData"
fatal:CompileDataList:DataList has no valid members

regards
Kunal Bali

On Fri, Nov 10, 2017 at 3:20 AM, Dennis Shea <shea at ucar.edu> wrote:

> [1] I do not have the file:
>      maiactaotE.h00v02.20010040635-20011550640.nc
>
>
> [2] where: See the 'Caveat' in the documentation
>      https://www.ncl.ucar.edu/Document/Functions/Built-in/where.shtml
>
>
>
> *CoD   = 1. / where(x2.ne.0, x1/x2, x2 at _FillValue)   ; it can be divisible
> by 0 *
>      should be
>
>       *CoD   = (x1 / where(x2.ne.0, x2, x2 at _FillValue))^2  *
>
>
> [3] I used a file with only a few time steps. See attached
>
>
>
> On Wed, Nov 8, 2017 at 11:05 PM, Kunal Bali <kunal.bali9 at gmail.com> wrote:
>
>>
>> when I try the attached script (plot1.ncl) for large time steps say 442
>> then
>> it shows an error
>>
>>
>>
>> *fatal:divide: Division by 0, Can't continuefatal:Div: operator failed,
>> can't continuefatal:["Execute.c":8640]:Execute: Error occurred at or near
>> line 120 in file plot.ncl*
>>
>> *120 line is *
>> CoD1 = ((DATA_SINGLE_3D - data_subset)/(DATA_SINGLE_3D + data_subset))^2
>>
>> So I separated this line to
>>    x1 = (DATA_SINGLE_3D - data_subset)
>>    x2 = (DATA_SINGLE_3D + data_subset)
>>
>> and then tried
>>
>>
>> *CoD   = 1. / where(x2.ne.0, x1/x2, x2 at _FillValue)   ; it can be
>> divisible by 0    CoD1 = (CoD)^2*
>>    printVarSummary(CoD1)
>>    printMinMax(CoD1, 0)
>>
>>
>> But, still showing the same error
>>
>>
>>
>> *fatal:divide: Division by 0, Can't continuefatal:Div: operator failed,
>> can't continuefatal:["Execute.c":8640]:Execute: Error occurred at or near
>> line 120 in file plot.ncl*
>>
>>
>> regards
>> Kunal Bali
>>
>>
>>
>>
>>
>>
>> On Wed, Nov 8, 2017 at 10:56 AM, Kunal Bali <kunal.bali9 at gmail.com>
>> wrote:
>>
>>> I followed your instruction, But now CoD1 and CoD2 values are showing
>>> the same values, which is incorrect? and because of this values of the
>>> square root of CoD2 is not correct.
>>>
>>>
>>> ;-----calculation of coefficient of divergence
>>>    CoD1 = ((DATA_SINGLE_3D - data_subset)/(DATA_SINGLE_3D+
>>> data_subset))^2
>>>    printVarSummary(CoD1)
>>>    printMinMax(CoD1, 0)
>>>
>>>    CoD1 at _FillValue = -28672
>>>    CoD2 = dim_avg_n_Wrap(CoD1, 0)    ; (nlat,mlon)
>>>    printVarSummary(CoD2)
>>>    printMinMax(CoD2,0)
>>>
>>>    CoD3 = sqrt(CoD2)
>>>    printVarSummary(CoD3)
>>>    printMinMax(CoD3, 0)
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> *Variable: CoD1Type: floatTotal Size: 7824960 bytes            1956240
>>> valuesNumber of Dimensions: 3Dimensions and sizes:    [3] x [660] x
>>> [988]Coordinates: Number Of Attributes: 1  _FillValue :    -28672(0)
>>> min=0   max=0.961394Variable: CoD2Type: floatTotal Size: 2608320
>>> bytes            652080 valuesNumber of Dimensions: 2Dimensions and
>>> sizes:    [660] x [988]Coordinates: Number Of Attributes: 2  _FillValue
>>> :    -28672  average_op_ncl :    dim_avg_n function was applied(0)
>>> min=0   max=0.961394Variable: CoD3Type: floatTotal Size: 2608320
>>> bytes            652080 valuesNumber of Dimensions: 2Dimensions and
>>> sizes:    [660] x [988]Coordinates: Number Of Attributes: 1  _FillValue
>>> :    -28672(0)    min=0   max=0.980507*
>>>
>>>
>>>
>>> regsrds
>>> Kunal Bali
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Wed, Nov 8, 2017 at 3:55 AM, Dennis Shea <shea at ucar.edu> wrote:
>>>
>>>>
>>>> *Variable: CoD1*
>>>> *Dimensions and sizes: [3] x [660] x [988]*
>>>> *(0) min=0   max=0.961394*
>>>>
>>>>
>>>> step 2:
>>>>
>>>>    CoD2 = sum(CoD1)/3           <===  No!
>>>>    printVarSummary(CoD2)
>>>>    printMinMax(CoD2, 0)
>>>>
>>>> https://www.ncl.ucar.edu/Document/Functions/Contributed/dim_
>>>> avg_n_Wrap.shtml
>>>>
>>>>    CoD2 = dim_avg_n_Wrap(CoD1, 0)    ; (nlat,mlon)
>>>>    printVarSummary(CoD2)
>>>>    printMinMax(CoD2,0)
>>>>
>>>> ===
>>>> If you still have large numbers, the find one grid point where a large
>>>> number occurs, then print the values.
>>>>
>>>>    print(CoD1(:,nn,mm))   ; print series where large numbers occur
>>>> (indices:  nn,mm)
>>>>
>>>> See documentation for
>>>>    https://www.ncl.ucar.edu/Document/Functions/Built-in/ind.shtml
>>>>    https://www.ncl.ucar.edu/Document/Functions/Built-in/ind_res
>>>> olve.shtml
>>>> See Examples 2 & 3
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> On Tue, Nov 7, 2017 at 1:32 PM, Kunal Bali <kunal.bali9 at gmail.com>
>>>> wrote:
>>>>
>>>>> Thanks for suggesting this.
>>>>>
>>>>> Actually, I am trying to calculate the COD at each grid cell.
>>>>>
>>>>> So as you suggested
>>>>>
>>>>>
>>>>> *DATA_SINGLE_3D = conform(data_subset, data_single, 0)    ; it worked*
>>>>>
>>>>>
>>>>>
>>>>> *Variable: DATA_SINGLE_3DType: floatTotal Size: 7824960 bytes
>>>>>   1956240 valuesNumber of Dimensions: 3Dimensions and sizes: [3] x [660] x
>>>>> [988]Coordinates: Number Of Attributes: 1  _FillValue : -28672*
>>>>> step 1:
>>>>> CoD1 = ((DATA_SINGLE_3D - data_subset)/(DATA_SINGLE_3D+ data_subset))^2
>>>>>    printVarSummary(CoD1)
>>>>>    printMinMax(CoD1, 0)
>>>>>
>>>>> *Variable: CoD1*
>>>>> *Type: float*
>>>>> *Total Size: 7824960 bytes*
>>>>> *            1956240 values*
>>>>> *Number of Dimensions: 3*
>>>>> *Dimensions and sizes: [3] x [660] x [988]*
>>>>> *Coordinates: *
>>>>> *Number Of Attributes: 1*
>>>>> *  _FillValue : -28672*
>>>>> *(0) min=0   max=0.961394*
>>>>>
>>>>>
>>>>> step 2:
>>>>>
>>>>>    CoD2 = sum(CoD1)/3
>>>>>    printVarSummary(CoD2)
>>>>>    printMinMax(CoD2, 0)
>>>>>
>>>>> *Variable: CoD2*
>>>>> *Type: float*
>>>>> *Total Size: 4 bytes*
>>>>> *            1 values*
>>>>> *Number of Dimensions: 1*
>>>>> *Dimensions and sizes: [1]*
>>>>> *Coordinates: *
>>>>> *(0) min=20691.8   max=20691.8   --------> values are very
>>>>> high? and also it's not giving at each grid cell, because of that I can't
>>>>> calculate the sqrt in the next step*
>>>>>
>>>>>
>>>>> step 3:
>>>>>
>>>>>   CoD3 = sqrt(CoD2)
>>>>>  printVarSummary(CoD3)
>>>>>  printMinMax(CoD2, 0)
>>>>>
>>>>>
>>>>> The final values are coming very high. It should be less than 0.3 at
>>>>> each grid cell.
>>>>>
>>>>> regards
>>>>> Kunal Bali
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Wed, Nov 8, 2017 at 1:18 AM, Dennis Shea <shea at ucar.edu> wrote:
>>>>>
>>>>>> If you have a one-dimensional array (1D; eg: a 'time series') and a
>>>>>> three-dimensional array (3D; eg: time series at multiple grid points), you
>>>>>> *must* make the arrays 'conformant' for an array calculation
>>>>>>
>>>>>> You have:
>>>>>>    CoD1 = ((data_single(:) - data_subset(:,:,:))/(data_single(:) +
>>>>>> data_subset(:,:,:)))^2
>>>>>>
>>>>>> You did not include a 'printVarSummary' of these variables. Assuming
>>>>>> data_single(ntim) and data_subset(ntim,nlat,mlon) then you must replicate
>>>>>> (aka, propogate) the 1D array to the 3D array size. In NCL, this is called
>>>>>>
>>>>>> https://www.ncl.ucar.edu/Document/Functions/Built-in/conform.shtml
>>>>>> https://www.ncl.ucar.edu/Document/Functions/Built-in/conform
>>>>>> _dims.shtml
>>>>>>
>>>>>> ---
>>>>>>   DATA_SINGLE_3D = conform(data_subset, data_single, 0)    ;
>>>>>> (ntim,nlat,mlon)
>>>>>>   printVarSummary(DATA_SINGLE_3D)   ; Look at data
>>>>>>
>>>>>> Do *not* use (:,:,:) in a computation... this is inefficient.
>>>>>>
>>>>>>   CoD1 = ((DATA_SINGLE_3D - data_subset)/(DATA_SINGLE_3D+
>>>>>> data_subset))^2
>>>>>>   printVarSummary(CoD1)
>>>>>>   printMinMax(CoD1, 0)
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> On Tue, Nov 7, 2017 at 11:15 AM, Kunal Bali <kunal.bali9 at gmail.com>
>>>>>> wrote:
>>>>>>
>>>>>>> Dear NCL user
>>>>>>>
>>>>>>> Suppose, I have 2 datasets
>>>>>>> e.g.
>>>>>>>
>>>>>>> data1 = of single point lat & lon (e.g 28N, 72E) with time steps
>>>>>>> 365 (having filling values)
>>>>>>> data2 = of grid box (24-32N, 68-76E) with time steps 365  (having
>>>>>>> filling values)
>>>>>>>
>>>>>>> Now I want to calculate the CoD from one single point to 400km grid
>>>>>>> box area and then lot the spatial map of the calculated product.
>>>>>>>
>>>>>>> Formula for CoD is GIVEN BELOW
>>>>>>> ​
>>>>>>>
>>>>>>> So I calculated in NCL as
>>>>>>>
>>>>>>> CoD1 = ((data1 - data2)/(data1 + data2))^2
>>>>>>> CoD_final = (sqrt(sum(CoD1)))/3
>>>>>>>
>>>>>>> please let me know is it the right way to do?
>>>>>>>
>>>>>>> I tried to plot the final product CoD_final with the attached
>>>>>>> script, but error comes as
>>>>>>>
>>>>>>> fatal:Minus: Number of dimensions do not match, can't continue
>>>>>>> fatal:["Execute.c":8578]:Execute: Error occurred at or near line
>>>>>>> 116 in file plot.ncl
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> regards
>>>>>>> Kunal Bali
>>>>>>>
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> ncl-talk mailing list
>>>>>>> ncl-talk at ucar.edu
>>>>>>> List instructions, subscriber options, unsubscribe:
>>>>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> ncl-talk mailing list
>>>>> ncl-talk at ucar.edu
>>>>> List instructions, subscriber options, unsubscribe:
>>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>>
>>>>>
>>>>
>>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> 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/20171110/77385e7b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: COD.png
Type: image/png
Size: 34015 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20171110/77385e7b/attachment.png>
```