[ncl-talk] printing spi
Dawit Abebe
dawit.kan at gmail.com
Wed Apr 12 09:54:39 MDT 2017
Dear Guido,
I am back with some challenging question. From the SPI03 dataset I wanted to plot two seasonal SPI curves such as SPI03 ending May (i.e. JFM) and CPI03 ending through November (i.e. SON). These are two rainy seasons in the area of my study. Local pastoral community define drought as sever when both seasons fail. I am therefore interested to find out if the seasonal SPI values supports the historical drought years as defined by the community.
Can you give me some idea how I can plot the two seasons spi value (i.e. ending May and November) on the same frame?. I am thinking of using some sort of subseting command, but really not sure.
Thank you so much
Dawit
> On Apr 12, 2017, at 6:18 PM, Guido Cioni <guidocioni at gmail.com> wrote:
>
> Look at your script.
>
>>> len = (/1, 3, 6, 9, 12, 17, 24, 36/)
>>> klen = dimsizes(len)
>
> Hence klen=8.
> However you do
>
>> plt = new (klen, "graphic")
>> xyLineColors = (/"black","red","blue","green","brown","magenta","purple”/)
>
> xyLineColors has dimension 7 but you are looping over klen
>
>>> do k=0,klen-1
>
>
> Thus NCL is telling exactly what the error is, i.e. you are trying to access an element which is out of the array.
>
> Guido Cioni
> http://guidocioni.altervista <http://guidocioni.altervista/>.org
>
>> On 12 Apr 2017, at 17:11, Dawit Abebe <dawit.kan at gmail.com <mailto:dawit.kan at gmail.com>> wrote:
>>
>> Dear Dennis,
>>
>> Thanks for command lines below. I updated my script accordingly and made some adjustments based on error message particularly in
>> res at gsnCenterString = "len="+klen(k) which returned an error of reading “keen” and when adjusted the command to "res at gsnCenterString = "klen="+len(k)” the error message is no longer there.
>>
>> However, I am not able to fix other two errors:
>>
>> fatal:Subscript out of range, error in subscript #0
>> fatal:An error occurred reading xyLineColors
>> fatal:["Execute.c":8640]:Execute: Error occurred at or near line 68 in file da_spiscript.ncl
>>
>> In the script I defined xyLineColours as follows:
>>
>> plt = new (klen, "graphic")
>> xyLineColors = (/"black","red","blue","green","brown","magenta","purple”/)
>>
>> How should I handle this?
>>
>> Thanks again for unreserved support.
>>
>> Dawit
>>
>>
>>
>>> On Apr 12, 2017, at 5:43 AM, Dennis Shea <shea at ucar.edu <mailto:shea at ucar.edu>> wrote:
>>>
>>> SPI: [len | 8] x [time | 1368] x [lat | 4] x [lon | 9]
>>>
>>> You must loop over lat and lon also
>>>
>>> do nl=0,nlat-1
>>> do ml=0,mlon-1
>>> do k=0,klen-1
>>> res at xyLineColors = xyLineColors(k)
>>> res at gsnCenterString = "len="+klen(k)
>>> plt(k) = gsn_csm_xy (wks,yrfrac,spi(k,:,nl,ml),res)
>>> end do
>>> end do
>>> end do
>>>
>>>
>>> On Tue, Apr 11, 2017 at 9:31 AM, Dawit Abebe <dawit.kan at gmail.com <mailto:dawit.kan at gmail.com>> wrote:
>>> Dear All,
>>>
>>> I tried to write a script to (1) calculate SPI and (2) plot the out put. The first set of the script went well through but the second (plotting) returned error at point highlighted blue blow. The message is clear that I should adjust the dimensions, but could not get this right. I just needed some ones help. I posted my script as well as the return of NCL commands below.
>>>
>>> 1. My script
>>>
>>> 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/csm/contributed.ncl"
>>>
>>> begin
>>>
>>> diri = "./"
>>> fili = "pre_1901_2014.nc <http://pre_1901_2014.nc/>"
>>> f = addfile(diri+fili, "r")
>>>
>>> prc = f->pre
>>> prc at _FillValue = -999.0
>>> pmsg = prc at _FillValue
>>>
>>> printVarSummary(prc)
>>> dimp = dimsizes(prc)
>>> ntim = dimp(0)
>>> nlat = dimp(1)
>>> mlon = dimp(2)
>>>
>>> len = (/1, 3, 6, 9, 12, 17, 24, 36/)
>>> klen = dimsizes(len)
>>>
>>> opt = True
>>> spi = new((/klen,ntim,nlat,mlon/), typeof(prc), pmsg)
>>>
>>> do k=0,klen-1
>>> spi(k,:,:,:)= dim_spi_n(prc, len(k), True, 0)
>>> end do
>>>
>>> copy_VarCoords(prc,spi(0,:,:,:))
>>> spi at long_name = "SPI"
>>> spi!0 = "len"
>>> spi&len = len
>>> printVarSummary(spi)
>>>
>>> yyyymm = f->time
>>> year = yyyymm/100
>>> yrStrt = 1901
>>> yrLast = 2014
>>> nyear = yrLast-yrStrt+1
>>> yyyymm = yyyymm_time(yrStrt, yrLast, "integer")
>>> yrfrac = (/ yyyymm_to_yyyyfrac(yyyymm, 0.0) /)
>>>
>>>
>>> wks = gsn_open_wks ("ps","spi")
>>> res = True
>>> res at gsnDraw = False
>>> res at gsnFrame = False
>>> res at vpHeightF= 0.4
>>> res at vpWidthF = 0.8
>>> res at vpXF = 0.1
>>>
>>> res at trYMinF = -3.0
>>> res at trYMaxF = 3.0
>>> res at tmYLTickSpacingF = 0.5
>>> res at tmYLMinorPerMajor = 4
>>> res at gsnYRefLine = 0.0
>>> res at trXMinF = yrStrt
>>> res at trXMaxF = yrLast
>>> res at XBMode = "Manual"
>>> res at xyMonoDashPattern = True
>>> res at xyLineThicknessF = 1
>>>
>>> plt = new ( klen, "graphic")
>>> xyLineColors = (/"black","red","blue","green","brown","magenta","purple"/)
>>>
>>> do k=0,klen-1
>>> res at xyLineColors = xyLineColors(k)
>>> res at gsnCenterString = "len="+klen(k)
>>> plt(k) = gsn_csm_xy (wks,yrfrac,spi(k,:,:,:),res)
>>> end do
>>>
>>> resP = True
>>> resP at gsnPanelMainString = "SPI: Southern Ethiopia (1901-2014)"
>>> resP at gsnPanelMainFont = "times-roman"
>>> resP at gsnMaximize = True
>>> gsn_panel(wks,plt,(/4,2/),resP)
>>>
>>> res at gsnDraw = True
>>> res at gsnFrame = True
>>> end
>>>
>>> 2. Returns from the commands
>>>
>>> (my_ncl) Dagemawes-MacBook-Pro:~ dawitabebe$ ncl -Q dawitspiscript.ncl
>>>
>>> Variable: prc
>>> Type: double
>>> Total Size: 393984 bytes
>>> 49248 values
>>> Number of Dimensions: 3
>>> Dimensions and sizes: [time | 1368] x [lat | 4] x [lon | 9]
>>> Coordinates:
>>> time: [ 380..41987]
>>> lat: [3.75..5.25]
>>> lon: [36.75..40.75]
>>> Number Of Attributes: 5
>>> correlation_decay_distance : 450
>>> long_name : precipitation
>>> units : mm/month
>>> missing_value : 9.96920996839e+36
>>> _FillValue : -999
>>>
>>> Variable: spi
>>> Type: double
>>> Total Size: 3151872 bytes
>>> 393984 values
>>> Number of Dimensions: 4
>>> Dimensions and sizes: [len | 8] x [time | 1368] x [lat | 4] x [lon | 9]
>>> Coordinates:
>>> len: [1..36]
>>> time: [ 380..41987]
>>> lat: [3.75..5.25]
>>> lon: [36.75..40.75]
>>> Number Of Attributes: 2
>>> long_name : SPI
>>> _FillValue : -999
>>> fatal:Number of subscripts do not match number of dimensions of variable,(2) Subscripts used, (3) Subscripts expected
>>> fatal:["Execute.c":8640]:Execute: Error occurred at or near line 5902 in file $NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl
>>>
>>> fatal:["Execute.c":8640]:Execute: Error occurred at or near line 72 in file dawitspiscript.ncl
>>>
>>> Thank you for your support
>>>
>>> Dawit
>>>
>>> ==============================================================================================================
>>>
>>>
>>>
>>>> On Apr 10, 2017, at 4:11 PM, Dennis Shea <shea at ucar.edu <mailto:shea at ucar.edu>> wrote:
>>>>
>>>> Generally, i s recommended that you write a script with NCL commands; then execute the script.
>>>>
>>>> %> ncl dawit.ncl
>>>>
>>>> Why? NCL's interactive environment is not as 'friendly' in error recovery as in other tools.
>>>>
>>>> On Mon, Apr 10, 2017 at 7:05 AM, Guido Cioni <guidocioni at gmail.com <mailto:guidocioni at gmail.com>> wrote:
>>>> Sorry I didn't follow the whole conversation so you would have to send me the entire script for me to check...
>>>>
>>>> What do you mean by "failed to return the graph"? You get no error or warning and no file is produced?
>>>> Try with the x11 workstation before using the postscript one so that you can easily debug any issue in the script beforehand.
>>>>
>>>>
>>>> Guido Cioni
>>>> http://guidocioni.altervista <http://guidocioni.altervista/>.org
>>>>
>>>>> On 10 Apr 2017, at 15:01, Dawit Abebe <dawit.kan at gmail.com <mailto:dawit.kan at gmail.com>> wrote:
>>>>>
>>>>> Dear Guido,
>>>>>
>>>>> What a shame of myself!
>>>>>
>>>>> Thanks Guido. I did the correction and went through smoothly but the last command failed to return the graph (4 one side and 3 on the other side in one plot). What do you that I missed to complete this?
>>>>>
>>>>> Thanks again
>>>>> …………………………………...
>>>>>
>>>>> ncl 109> do k=0,klen-1
>>>>> ncl 110> res at xyLineColors = xyLineColors(k)
>>>>> ncl 111> res at gsnCenterString = "len="+klen(k)
>>>>> ncl 112> plt(k) = gsn_csm_xy (wks, yrfrac, spi(k,:), res)
>>>>> ncl 113> end do
>>>>> ncl 114> resP = True
>>>>> ncl 115> resP at txString = "SPI: Southern Ethiopia 1901-2014"
>>>>> ncl 116> resP at gsnMaximize = True
>>>>> ncl 117> gsn_panel (wks,plt,(/4,2/), resP)
>>>>>
>>>>> ………………….
>>>>>
>>>>>
>>>>>> On Apr 10, 2017, at 3:39 PM, Guido Cioni <guidocioni at gmail.com <mailto:guidocioni at gmail.com>> wrote:
>>>>>>
>>>>>> There is a typo in your code. It should be xyLineColors and not xyLineColours.
>>>>>> See here https://www.ncl.ucar.edu/Document/Graphics/Resources/xy.shtml <https://www.ncl.ucar.edu/Document/Graphics/Resources/xy.shtml>.
>>>>>>
>>>>>> (Look closely at the warning/error messages that you are receiving: most of the times they can help you a lot in identifying the problem)
>>>>>>
>>>>>> Hope that helps
>>>>>>
>>>>>> Guido Cioni
>>>>>> http://guidocioni.altervista <http://guidocioni.altervista/>.org
>>>>>>
>>>>>>> On 10 Apr 2017, at 14:26, Dawit Abebe <dawit.kan at gmail.com <mailto:dawit.kan at gmail.com>> wrote:
>>>>>>>
>>>>>>> Dear Dennis and all
>>>>>>>
>>>>>>> I’m sorry for bothering you all but tried to do xy plot of spi following example 1but failed. You hinted in your email below that I should change the spi array but could not figure out how to do thi. I tried the following but could not able to continue after some step. I have gone through the documentations and examples but failed to fix my problem.
>>>>>>>
>>>>>>> ………………………………
>>>>>>>
>>>>>>> Variable: spi
>>>>>>> Type: double
>>>>>>> Total Size: 3151872 bytes
>>>>>>> 393984 values
>>>>>>> Number of Dimensions: 4
>>>>>>> Dimensions and sizes: [len | 8] x [time | 1368] x [lat | 4] x [lon | 9]
>>>>>>> Coordinates:
>>>>>>> len: [1..36]
>>>>>>> time: [ 380..41987]
>>>>>>> lat: [3.75..5.25]
>>>>>>> lon: [36.75..40.75]
>>>>>>> Number Of Attributes: 2
>>>>>>> long_name : SPI
>>>>>>> _FillValue : -999
>>>>>>>
>>>>>>> Plot Parameter
>>>>>>> ncl 55> yyyymm = f->time
>>>>>>> ncl 56> yyyymm = yyyymm/100yrStrt = 1901
>>>>>>> ncl 57> yrLast = 2014
>>>>>>> ncl 58> nyear = yrLast-yrStrt+1
>>>>>>> ncl 59> yyyymm = yyyymm_time(yrStrt, yrLast, "integer")
>>>>>>> ncl 60> yrfrac = (/yyyymm_to_yyyyfrac(yyyymm, 0.0)/)
>>>>>>> ncl 61>
>>>>>>> ncl 62> wks = gsn_open_wks ("ps","spi")
>>>>>>> ncl 63> res = True
>>>>>>> ncl 64> res at gsnDraw=False
>>>>>>> ncl 65> res at gsnFrame=False
>>>>>>> ncl 66> res at vpHeightF = 0.4
>>>>>>> ncl 67> res at vpWidthF = 0.8
>>>>>>> ncl 68> res at vpXF = 0.1
>>>>>>> ncl 69>
>>>>>>> ncl 70> res at trYMinF = -3.0
>>>>>>> ncl 71> res at trYMaxF = 3.0
>>>>>>> ncl 72> res at gsnYRefLine = 0.0
>>>>>>> ncl 73> res at xyMonoDashPattern = True
>>>>>>> ncl 74> res at xyLineThicknessF = 1
>>>>>>> ncl 75>
>>>>>>> ncl 76> plt = new (klen, "graphic")
>>>>>>> ncl 77> xyLineColors = (/"black","red","blue","green","brown","magenta","grape"/)
>>>>>>> ncl 78> do k=0,klen-1
>>>>>>> ncl 79> res at xyLineColours = xyLineColours(k)
>>>>>>> ncl 80> res at gsnCenterString = "len="+klen(k)
>>>>>>> ncl 81> plt(k) = gsn_csm_xy (wks, yrfrac, spi(k,:), res)
>>>>>>> ncl 82> end do
>>>>>>> fatal:Undefined identifier: (xyLineColours) is undefined, can't continue
>>>>>>> fatal:["Execute.c":8638]:Execute: Error occurred at or near line 79
>>>>>>> ……………………………
>>>>>>>
>>>>>>> Thanks for your help
>>>>>>>
>>>>>>> Dawit
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> On Apr 6, 2017, at 9:00 PM, Dennis Shea <shea at ucar.edu <mailto:shea at ucar.edu>> wrote:
>>>>>>>>
>>>>>>>> I do not understand the question.
>>>>>>>>
>>>>>>>> https://www.ncl.ucar.edu/Applications/ <https://www.ncl.ucar.edu/Applications/>
>>>>>>>>
>>>>>>>> Under Data Analysis: Click 'Standardized Precipitation Index'
>>>>>>>>
>>>>>>>> See: Example 1
>>>>>>>>
>>>>>>>> You need to make the same array changes as in the print.
>>>>>>>>
>>>>>>>> EG: spi(0,:,)==> spi(0,:,nl,ml)
>>>>>>>>
>>>>>>>>
>>>>>>>> On Thu, Apr 6, 2017 at 1:22 AM, Dawit Abebe <dawit.kan at gmail.com <mailto:dawit.kan at gmail.com>> wrote:
>>>>>>>> Hello All,
>>>>>>>>
>>>>>>>> Thanks to Dennis, I am able to appreciate the power of NCL computing SPI. My next step is to produce
>>>>>>>> (1) a time serious XY plot for seasonal (SPI-3 ending in May and ending in November), annual (SPI-12) and multi annual (SPI-24 and SPI-36 months) and
>>>>>>>> (2) Shape map of selected annual SPI with extremely sever drought events to examine the spatial characteristics of the drought.
>>>>>>>>
>>>>>>>> I’m not asking for help before I try by my self (thanks Dennis for the push to do this). However, I need a quick comment how I should proceed from the last step producing the SPI output, which was printing:
>>>>>>>>
>>>>>>>> print(yyyymm+sprintf("%8.2f", prc(:,nl,ml)) \
>>>>>>>> +sprintf("%8.2f", spi(0,:,nl,ml))+sprintf("%8.2f", spi(1,:,nl,ml)) \
>>>>>>>> +sprintf("%8.2f", spi(2,:,nl,ml))+sprintf("%8.2f", spi(3,:,nl,ml)) \
>>>>>>>> +sprintf("%8.2f", spi(4,:,nl,ml))+sprintf("%8.2f", spi(5,:,nl,ml)) \
>>>>>>>> +sprintf("%8.2f", spi(6,:,nl,ml)))
>>>>>>>>
>>>>>>>> Do I need to go through this and then start with plot parameters, or I can continue with plot parameters without the print command?
>>>>>>>>
>>>>>>>> Thank you so much
>>>>>>>> Dawit
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>> On Apr 5, 2017, at 4:43 PM, Dennis Shea <shea at ucar.edu <mailto:shea at ucar.edu>> wrote:
>>>>>>>>>
>>>>>>>>> The error mesages is telling you the exact problem:
>>>>>>>>> fatal:Plus: Number of dimensions do not match, can't continue
>>>>>>>>>
>>>>>>>>> spi has 4-dimensions:
>>>>>>>>> [len | 7] x [time | 1368] x [lat | 4] x [lon | 9]
>>>>>>>>>
>>>>>>>>> prc has 3-dimensions
>>>>>>>>> [time | 1368] x [lat | 4] x [lon | 9]
>>>>>>>>>
>>>>>>>>> yyyymm has 1-dimension
>>>>>>>>> [time | 1368]
>>>>>>>>>
>>>>>>>>> ==============
>>>>>>>>> The print statement you used was copied from:
>>>>>>>>> https://www.ncl.ucar.edu/Document/Functions/Built-in/dim_spi_n.shtml <https://www.ncl.ucar.edu/Document/Functions/Built-in/dim_spi_n.shtml>
>>>>>>>>> Example 2
>>>>>>>>> That was working upon a single time series prc[*]
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> You have a 4-D array. *You* must make the appropriate changed to handle your situation.
>>>>>>>>> The eamples are not generic to all dimensions.
>>>>>>>>>
>>>>>>>>> nlat=4
>>>>>>>>> mlon=9
>>>>>>>>>
>>>>>>>>> do nl=0,nlat-1
>>>>>>>>> do ml=0,mlon-1
>>>>>>>>> print("------------------------------------------------")
>>>>>>>>>
>>>>>>>>> print(yyyymm+sprintf("%8.2f", prc(:,nl,ml)) \
>>>>>>>>> +sprintf("%8.2f", spi(0,:,nl,ml))+sprintf("%8.2f", spi(1,:,nl,ml)) \
>>>>>>>>> +sprintf("%8.2f", spi(2,:,nl,ml))+sprintf("%8.2f", spi(3,:,nl,ml)) \
>>>>>>>>> +sprintf("%8.2f", spi(4,:,nl,ml))+sprintf("%8.2f", spi(5,:,nl,ml)) \
>>>>>>>>> +sprintf("%8.2f", spi(6,:,nl,ml)))
>>>>>>>>>
>>>>>>>>> end do
>>>>>>>>> en do
>>>>>>>>>
>>>>>>>>> On Wed, Apr 5, 2017 at 2:20 AM, Dawit Abebe <dawit.kan at gmail.com <mailto:dawit.kan at gmail.com>> wrote:
>>>>>>>>> Hello all,
>>>>>>>>>
>>>>>>>>> Kindly help me to fix my problem with printing SPI. I pasted below first the return of printVarSummary(spi), which seems okay. However, my attempt to print the output (text in blue) did not work. What was wrong?
>>>>>>>>>
>>>>>>>>> Greatly appreciate your help.
>>>>>>>>>
>>>>>>>>> Dawit
>>>>>>>>>
>>>>>>>>> …………………………………….
>>>>>>>>>
>>>>>>>>> copy_VarCoords(prc,spi(0,:,:,:))
>>>>>>>>> spi at long_name = "SPI"
>>>>>>>>> spi!0 = "len"
>>>>>>>>> spi&len = len
>>>>>>>>> printVarSummary(spi)
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Variable: spi
>>>>>>>>> Type: double
>>>>>>>>> Total Size: 2757888 bytes
>>>>>>>>> 344736 values
>>>>>>>>> Number of Dimensions: 4
>>>>>>>>> Dimensions and sizes: [len | 7] x [time | 1368] x [lat | 4] x [lon | 9]
>>>>>>>>> Coordinates:
>>>>>>>>> len: [1..36]
>>>>>>>>> time: [ 380..41987]
>>>>>>>>> lat: [3.75..5.25]
>>>>>>>>> lon: [36.75..40.75]
>>>>>>>>> Number Of Attributes: 2
>>>>>>>>> long_name : SPI
>>>>>>>>> _FillValue : -999
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> yyyymm = cd_calendar(f->time, -1)
>>>>>>>>>
>>>>>>>>> print(yyyymm+sprintf("%8.2f", prc) \
>>>>>>>>> +sprintf("%8.2f", spi(0,:))+sprintf("%8.2f", spi(1,:)) \
>>>>>>>>> +sprintf("%8.2f", spi(2,:))+sprintf("%8.2f", spi(3,:)) \
>>>>>>>>> +sprintf("%8.2f", spi(4,:))+sprintf("%8.2f", spi(5,:)) \
>>>>>>>>> +sprintf("%8.2f", spi(6,:)))
>>>>>>>>> fatal:Plus: Number of dimensions do not match, can't continue
>>>>>>>>> fatal:["Execute.c":8638]:Execute: Error occurred at or near line 37
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> _______________________________________________
>>>>>>>>> ncl-talk mailing list
>>>>>>>>> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
>>>>>>>>> List instructions, subscriber options, unsubscribe:
>>>>>>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> ncl-talk mailing list
>>>>>>> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
>>>>>>> List instructions, subscriber options, unsubscribe:
>>>>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
>>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170412/b982a355/attachment.html
More information about the ncl-talk
mailing list