# [ncl-talk] SPI calculation

Dennis Shea shea at ucar.edu
Sat Dec 1 08:38:06 MST 2018

```function dim_spi_n (
x        : numeric,  ; float, double
*nrun [1]* : integer,
opt      : logical,
dims [*] : integer
)

The 'nrun' is prototyped as [1]. Hence, a do loop must be used.

See attached.

On Sat, Dec 1, 2018 at 1:51 AM Soma Roy <somaroy892 at gmail.com> wrote:

> No result.
>
> Soma
>
> On Sat, Dec 1, 2018, 14:14 Oluwaseun Ilori <wilseun at gmail.com wrote:
>
>> Maybe you should try the fifth example
>> http://www.ncl.ucar.edu/Document/Functions/Built-in/dim_spi_n.shtml
>> Look at example 5
>>
>> On Sat, Dec 1, 2018, 9:35 AM Soma Roy <somaroy892 at gmail.com wrote:
>>
>>> I turned off the loop and run the script to calculate spi for 4 runlen
>>> in the script in order to save 4 runlen x time x lat x lon in a single
>>> netcdf as you point out but it shows errors.
>>>
>>>   runlen = (/3, 6, 9,12 /)
>>>   nrun   = dimsizes(runlen)
>>>
>>> fatal:Number of elements of dimension (0) of argument (1) is (4) in
>>> function (dim_spi_n), expected (1) elements
>>>
>>> printVarSummary of input data is like below;
>>> Variable: prc
>>> Type: double
>>> Total Size: 68545440 bytes
>>>             8568180 values
>>> Number of Dimensions: 3
>>> Dimensions and sizes:   [time | 492] x [lat | 129] x [lon | 135]
>>> Coordinates:
>>>             time: [ 360..1068]
>>>             lat: [ 6.5..38.5]
>>>             lon: [66.5.. 100]
>>> Number Of Attributes: 3
>>>   _FillValue :  -999
>>>   long_name :   GRIDDED RAINFALL
>>>   missing_value :       -999
>>>  : min=0   max=222.9121398925781
>>>
>>> Kindly inform me how to solve it.
>>>
>>> Thank you,
>>> Soma
>>>
>>> On Sat, Dec 1, 2018, 11:49 Soma Roy <somaroy892 at gmail.com wrote:
>>>
>>>> I turned off the loop and run the script to calculate spi for 4 runlen
>>>> in the script as you point out but it shows errors.
>>>>
>>>>   runlen = (/3,6,9,12 /)
>>>>   nrun   = dimsizes(runlen)
>>>>
>>>> fatal:Number of elements of dimension (0) of argument (1) is (4) in
>>>> function (dim_spi_n), expected (1) elements
>>>>
>>>> printVarSummary of input data is like below;
>>>> Variable: prc
>>>> Type: double
>>>> Total Size: 68545440 bytes
>>>>             8568180 values
>>>> Number of Dimensions: 3
>>>> Dimensions and sizes:   [time | 492] x [lat | 129] x [lon | 135]
>>>> Coordinates:
>>>>             time: [ 360..1068]
>>>>             lat: [ 6.5..38.5]
>>>>             lon: [66.5.. 100]
>>>> Number Of Attributes: 3
>>>>   _FillValue :  -999
>>>>   long_name :   GRIDDED RAINFALL
>>>>   missing_value :       -999
>>>>  : min=0   max=222.9121398925781
>>>>
>>>> Kindly inform me how to solve it.
>>>>
>>>> Thank you,
>>>> Soma
>>>>
>>>>
>>>>
>>>>
>>>> On Sat, Dec 1, 2018, 04:53 Dennis Shea <shea at ucar.edu wrote:
>>>>
>>>>> You keep referring to
>>>>>    *http://www.ncl.ucar.edu/Applications/spi.shtml*
>>>>> <http://www.ncl.ucar.edu/Applications/spi.shtml>
>>>>>    Example: spi_2.ncl
>>>>>
>>>>>
>>>>> This is a graphic example.
>>>>> This is looping over *each* run length ....
>>>>> To be clear "*each*" means  **one** run length to *plot* the SPI  at *that
>>>>> *run-length at two different times.
>>>>>
>>>>>   do nr=0,nrun-1
>>>>>     spi    = dim_spi_n(prc, runlen(nr), False, 0)   ; *one run length*
>>>>>     ... plot code ...
>>>>>     end do
>>>>>
>>>>>
>>>>> To get all run lengths in one function call use
>>>>>
>>>>> SPI = dim_spi_n(prc, runlen, False, 0)   ; note 'runlen' .... not ...
>>>>> "runlen(nr)'
>>>>> printVarSummary(SPI)
>>>>>
>>>>> This is what you should output to netCDF.
>>>>>
>>>>> ===
>>>>>
>>>>>
>>>>> On Fri, Nov 30, 2018 at 11:44 AM Soma Roy <somaroy892 at gmail.com>
>>>>> wrote:
>>>>>
>>>>>> One thing I would like to mention.
>>>>>>
>>>>>> In spi_2.ncl script,
>>>>>> spi=dim_spi_n(prc, runlen(nr), False, 0)
>>>>>>
>>>>>> Reduced the dimensions (len x time x latx lon) to (time x latx lon).
>>>>>>
>>>>>> So I am not getting 4 dimensions in a single file.
>>>>>>
>>>>>> Kindly inform me how to overcome it.
>>>>>>
>>>>>> Thanks,
>>>>>> Soma
>>>>>>
>>>>>> On Fri, Nov 30, 2018, 23:42 Soma Roy <somaroy892 at gmail.com wrote:
>>>>>>
>>>>>>> Thank you very much.
>>>>>>>
>>>>>>> Soma
>>>>>>>
>>>>>>> On Fri, Nov 30, 2018, 21:32 Dennis Shea <shea at ucar.edu wrote:
>>>>>>>
>>>>>>>> *Please* read the File IO on netCDF. As noted in the
>>>>>>>> documentation, the following is the most commonly used approach.
>>>>>>>>
>>>>>>>>   http://www.ncl.ucar.edu/Applications/method_1.shtml
>>>>>>>>
>>>>>>>> ===
>>>>>>>>
>>>>>>>>        *system* <http://www.ncl.ucar.edu/Document/Functions/Built-in/system.shtml>("/bin/rm -f SPI.nc")   ; remove any pre-existing file
>>>>>>>>
>>>>>>>>     ;===================================================================
>>>>>>>>     ; create global attributes of the file (optional)
>>>>>>>>     ;===================================================================
>>>>>>>>        fAtt               = True            ; assign file attributes
>>>>>>>>        fAtt at title         = "Standardized Precipitation Index"
>>>>>>>>        fAtt at Conventions   = "None"
>>>>>>>>        fAtt at creation_date = *systemfunc* <http://www.ncl.ucar.edu/Document/Functions/Built-in/systemfunc.shtml> ("date")
>>>>>>>>        *fileattdef* <http://www.ncl.ucar.edu/Document/Functions/Built-in/fileattdef.shtml>( ncdf, fAtt )            ; copy file attributes
>>>>>>>>
>>>>>>>>     ;===================================================================
>>>>>>>>     ; output variables directly; NCL will call appropriate functions
>>>>>>>>     ; to write the meta data associated with each variable
>>>>>>>>     ;===================================================================
>>>>>>>>        ncdf->SPI  = spi    ; (len,time,lat,lon)
>>>>>>>>
>>>>>>>>
>>>>>>>> On Thu, Nov 29, 2018 at 10:29 PM Soma Roy <somaroy892 at gmail.com>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> Hello Mary,
>>>>>>>>>
>>>>>>>>> I just want to know that what is the more efficient way to save
>>>>>>>>> the variable "spi" as a netcdf for multiple runlen?
>>>>>>>>>
>>>>>>>>> Say 3,6,9 and 12 or 12 and 24 as mentioned in the spi_2.ncl script.
>>>>>>>>>
>>>>>>>>> I want to save all runlen in a single netcdf file in single run
>>>>>>>>> time.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Thanking you,
>>>>>>>>> Soma
>>>>>>>>>
>>>>>>>>> On Thu, Nov 29, 2018, 20:12 Dennis Shea <shea at ucar.edu wrote:
>>>>>>>>>
>>>>>>>>>> I do not understand: *"197207 with 3 month running avg"*
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> If you used:
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> *runlen = (/ 12, 24 /)  *
>>>>>>>>>>
>>>>>>>>>> and your source array is dimensioned:
>>>>>>>>>> *(time,lat,lon) ==> (492,129,135)*then your SPI variable should be
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> *[len | 2] x [time | 492] x [lat | 129] x [lon | 135]*and your source array is dimensioned:
>>>>>>>>>> * (time,lon,lat) ==> (492,135,129)*then your SPI variable should be
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> *[len | 2] x [time | 492] x [lon | 135] x [lat | 129]  ---     0            1             2             3---*
>>>>>>>>>>
>>>>>>>>>>  *IF* you want to perform a 3-month running average of the 12 and 24 run-lengths
>>>>>>>>>>
>>>>>>>>>> nrun = 3
>>>>>>>>>> opt  = ?   ; read documentation
>>>>>>>>>> tdim = 1   ; time dimension
>>>>>>>>>> spi3= *runave_n_Wrap* <http://www.ncl.ucar.edu/Document/Functions/Contributed/runave_n_Wrap.shtml> (spi, nrun, tdim, opt)
>>>>>>>>>>
>>>>>>>>>> *printVarSummary*(spi3)
>>>>>>>>>>
>>>>>>>>>> ========
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> On Thu, Nov 29, 2018 at 1:15 AM Soma Roy <somaroy892 at gmail.com>
>>>>>>>>>> wrote:
>>>>>>>>>>
>>>>>>>>>>> Hello Mary,
>>>>>>>>>>>
>>>>>>>>>>> I save the data of 'spi' as netcdf file.
>>>>>>>>>>>
>>>>>>>>>>> Where dimension of the data is like below;
>>>>>>>>>>> spi(135x129x492) ; (lon, lat, spi values)
>>>>>>>>>>>
>>>>>>>>>>> Now my question is if I want to select 197207 with 3 month
>>>>>>>>>>> running avg. Then how to select those indices from 3rd dimension of the
>>>>>>>>>>> matrix "spi"?
>>>>>>>>>>>
>>>>>>>>>>> Start yr is 1970.
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> Thanking you,
>>>>>>>>>>> Soma
>>>>>>>>>>>
>>>>>>>>>>> On Thu, Nov 29, 2018, 12:39 Oluwaseun Ilori <wilseun at gmail.com
>>>>>>>>>>> wrote:
>>>>>>>>>>>
>>>>>>>>>>>> Good day, please could you assist with the correct script of
>>>>>>>>>>>> the spi calculation, I'm having problem listing the result of the one I'm
>>>>>>>>>>>> doing to text file for further analysis on ArcGis.
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks
>>>>>>>>>>>>
>>>>>>>>>>>> On Thu, Nov 29, 2018, 7:47 AM Soma Roy <somaroy892 at gmail.com
>>>>>>>>>>>> wrote:
>>>>>>>>>>>>
>>>>>>>>>>>>> I fixed the issue. Please ignore my last mail.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Soma
>>>>>>>>>>>>>
>>>>>>>>>>>>> On Thu, Nov 29, 2018, 11:07 Soma Roy <somaroy892 at gmail.com
>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>
>>>>>>>>>>>>>> Hello Mary,
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> I am using the attached script. Modified it as per your
>>>>>>>>>>>>>> hints. But getting some errors at run time.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> printVarSummary of the data is as follows;
>>>>>>>>>>>>>> Variable: prc
>>>>>>>>>>>>>> Type: double
>>>>>>>>>>>>>> Total Size: 73560960 bytes
>>>>>>>>>>>>>>             9195120 values
>>>>>>>>>>>>>> Number of Dimensions: 3
>>>>>>>>>>>>>> Dimensions and sizes:   [time | 528] x [lat | 129] x [lon |
>>>>>>>>>>>>>> 135]
>>>>>>>>>>>>>> Coordinates:
>>>>>>>>>>>>>>             time: [ 360..1068]
>>>>>>>>>>>>>>             lat: [ 6.5..38.5]
>>>>>>>>>>>>>>             lon: [66.5.. 100]
>>>>>>>>>>>>>> Number Of Attributes: 3
>>>>>>>>>>>>>>   _FillValue :  -999
>>>>>>>>>>>>>>   long_name :   GRIDDED RAINFALL
>>>>>>>>>>>>>>   missing_value :       -999
>>>>>>>>>>>>>>  : min=0   max=222.9121398925781
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> I am getting the following errors;
>>>>>>>>>>>>>> fatal:Dimension sizes of left hand side and right hand side
>>>>>>>>>>>>>> of assignment do not match
>>>>>>>>>>>>>> fatal:["Execute.c":8640]:Execute: Error occurred at or near
>>>>>>>>>>>>>> line 47 in file spi_2.ncl
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> fatal:NclOneDValGetClosestIndex: Non-monotonic coordinate
>>>>>>>>>>>>>> value being used, can't complete coordinate subscript
>>>>>>>>>>>>>> fatal:Could not obtain coordinate indexes, unable to perform
>>>>>>>>>>>>>> subscript
>>>>>>>>>>>>>> fatal:["Execute.c":8640]:Execute: Error occurred at or near
>>>>>>>>>>>>>> line 82 in file spi_2.ncl
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Please kindly inform me how to correct it.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Thanking you,
>>>>>>>>>>>>>> Soma
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> On Thu, Nov 29, 2018 at 9:51 AM Soma Roy <
>>>>>>>>>>>>>> somaroy892 at gmail.com> wrote:
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Thank you very much.
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Soma
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> On Thu, Nov 29, 2018, 02:28 Mary Haley <haley at ucar.edu
>>>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> Soma,
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> The spi_2.ncl script shows you how to calculate the yyyymm
>>>>>>>>>>>>>>>> variable:
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>   year    = yyyymm/100
>>>>>>>>>>>>>>>>   yrStrt  = year(0)
>>>>>>>>>>>>>>>>   yrLast  = year(ntim-1)
>>>>>>>>>>>>>>>>   nyear   = yrLast-yrStrt+1
>>>>>>>>>>>>>>>>   yyyymm  = yyyymm_time(yrStrt, yrLast, "integer")
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> You don't need the "date" or "yyyymm" arrays, because you
>>>>>>>>>>>>>>>> already have the year information. Use ispan to generate the "year" array
>>>>>>>>>>>>>>>> and now you can get your yyyymm and yrfac values as needed:
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>   year    = ispan(1970,2010,1)
>>>>>>>>>>>>>>>>   ntim    = dimsizes(year)
>>>>>>>>>>>>>>>>   yrStrt  = year(0)
>>>>>>>>>>>>>>>>   yrLast  = year(ntim-1)
>>>>>>>>>>>>>>>>   nyear   = yrLast-yrStrt+1
>>>>>>>>>>>>>>>>   yyyymm  = yyyymm_time(yrStrt, yrLast, "integer")
>>>>>>>>>>>>>>>>   yrfrac  = (/ yyyymm_to_yyyyfrac(yyyymm, 0.0) /)
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> --Mary
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> On Wed, Nov 28, 2018 at 1:05 PM Soma Roy <
>>>>>>>>>>>>>>>> somaroy892 at gmail.com> wrote:
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> Thank you very much for the hint.
>>>>>>>>>>>>>>>>> I will check it.
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> But mannualy we can't set the "date" range in the script
>>>>>>>>>>>>>>>>> without using CDO?
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> Thanking you,
>>>>>>>>>>>>>>>>> Soma
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> On Thu, Nov 29, 2018, 01:28 Guilherme Martins <
>>>>>>>>>>>>>>>>> jgmsantos at gmail.com wrote:
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> I don't know if you use climate data operators or cdo
>>>>>>>>>>>>>>>>>> software.
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> You can set the data in your file.
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> Exemple
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> cdo settaxis,1970-01-01,00:00:00,1mon input.nc output.nc
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> Guilherme.
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> Em qua, 28 de nov de 2018 16:35, Soma Roy <
>>>>>>>>>>>>>>>>>> somaroy892 at gmail.com escreveu:
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Hello,
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> I am trying to calculate standadize precipitation index
>>>>>>>>>>>>>>>>>>> using monthly gridded dataset.
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Dimension of data is like below;
>>>>>>>>>>>>>>>>>>> Rainfall(12x41) (months x years).
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> I am using the script from the below link;
>>>>>>>>>>>>>>>>>>> https://www.ncl.ucar.edu/Applications/Scripts/spi_2.ncl
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> But in my dataset there is no variable as "date".
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Can we manually set the date range inside the script?
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> I am using rainfall dataset for 1970-2010.
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Please kindly inform me how we can modify it over the
>>>>>>>>>>>>>>>>>>> same script.
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Thanking you,
>>>>>>>>>>>>>>>>>>> Soma
>>>>>>>>>>>>>>>>>>> _______________________________________________
>>>>>>>>>>>>>>>>>>> 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
>>>>>>>>>>>>>
>>>>>>>>>>>> _______________________________________________
>>>>>>>>>>> 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/20181201/c27f9696/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SomaRoy.ncl_talk
Type: application/octet-stream
Size: 2014 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20181201/c27f9696/attachment.obj>
```