# [ncl-talk] If statement

Melissa Lazenby M.Lazenby at sussex.ac.uk
Mon Feb 27 04:43:43 MST 2023

```Hi Adam

Thank you very much for your help in this - it worked perfectly!

Kindest Regards
Melissa

Dr. Melissa Lazenby
Lecturer in Climate Change
Department of Geography
Chichester 1 C150
University of Sussex

"Education is the most powerful weapon which you can use to change the world" Nelson Mandela

[cid:5112fc2a-f63e-48a2-b196-b76385d5e55b]
________________________________
From: Adam Phillips <asphilli at ucar.edu>
Sent: Saturday, February 25, 2023 3:48 PM
To: Melissa Lazenby <M.Lazenby at sussex.ac.uk>
Cc: Dennis Shea <shea at ucar.edu>; Ncl-talk <ncl-talk at ucar.edu>
Subject: Re: [ncl-talk] If statement

Hi Melissa,
I would create an array of strings pertaining to the individual bar colors, like this:
bc = new(34,string)
bc = "red"
time = ispan(1855,1888,1)
selyr = (/1857,1858,1859/)
do gg = 0,dimsizes(selyr)-1
bc = where(time.eq.selyr(gg),"pink",bc)
end do
print(time+" "+bc)

and then you can tell NCL to color each bar by the bc array:
res at gsnXYBarChartColors2 = bc
plot1 = gsn_csm_xy(wks,fspan(1855.6,1888.7,34),stdarr(:,0),sres)
See the 2nd panel of example #6 here:
https://www.ncl.ucar.edu/Applications/bar.shtml#ex6<https://www.ncl.ucar.edu/Applications/bar.shtml#ex6>

Hope that helps!

On Fri, Feb 24, 2023 at 10:04 AM Melissa Lazenby via ncl-talk <ncl-talk at mailman.ucar.edu<mailto:ncl-talk at mailman.ucar.edu>> wrote:
Hi Dennis

Many thanks for your speedy response.
I have gotten confused in which variable I am trying to set.
What I actually want to do is select years which appear in the x axis differently based on their certainty.
For example I want to make the year 1858 show up as a pink bar instead of a red bar to show less certainty.
Therefore I will need to make an if statement to reflect that for the x axis - however I have used the function fspan in my x value expression so not sure how to do this.

Would this maybe work?
if ((fspan.eq.1857) .or. (fspan.eq.1858) .or. (fspan.eq.1859)) then
sres at gsnXYBarChartColors = (/"pink"/)
else
sres at gsnXYBarChartColors = (/"red"/)
end if
plot1 = gsn_csm_xy(wks,fspan(1855.6,1888.7,34),stdarr(:,0),sres)

Many thanks for all your help!

Kindest Regards
Melissa

Dr. Melissa Lazenby
Lecturer in Climate Change
Department of Geography
Chichester 1 C150
University of Sussex

"Education is the most powerful weapon which you can use to change the world" Nelson Mandela

[cid:18685db63bd45fc2e572]
________________________________
From: Dennis Shea <shea at ucar.edu<mailto:shea at ucar.edu>>
Sent: Friday, February 24, 2023 2:50 PM
To: Melissa Lazenby <M.Lazenby at sussex.ac.uk<mailto:M.Lazenby at sussex.ac.uk>>
Cc: Ncl-talk <ncl-talk at ucar.edu<mailto:ncl-talk at ucar.edu>>
Subject: Re: [ncl-talk] If statement

I have not looked at the script but:
---
do hh = 0,33
stdarr(hh,0) = (/ (yIndex(hh::34)) /)
---

hh is a scalar. So, when (say) hh=0

stdarr(0,0) = (/ (yIndex(0::34)) /)     ; ==> 35 elements of yIndex

35 elements are being assigned to one array element.

NCL should have issued an error message.

Maybe, I am missing something!

On Fri, Feb 24, 2023 at 6:11 AM Melissa Lazenby via ncl-talk <ncl-talk at mailman.ucar.edu<mailto:ncl-talk at mailman.ucar.edu>> wrote:
Hi All NCL Users

I am struggling with a conditional statement that I would like to perform.
Essentially, I am trying to make some bars pink instead of red to show a difference in uncertainty.
I have the following code below to perform this but I think there is an error with how I am looping the array.

do hh = 0,33
stdarr(hh,0) = (/ (yIndex(hh::34)) /)

if ((hh.eq.1) .or. (hh.eq.2) .or. (hh.eq.3)) then
sres at gsnXYBarChartColors = (/"pink"/)
else
sres at gsnXYBarChartColors = (/"red"/)
end if
plot1 = gsn_csm_xy(wks,fspan(1855.6,1888.7,34),stdarr(hh,0),sres)           ; draw each time series

end do

This error occurs:
(0)     gsn_csm_xy: Fatal: X and Y must have the same dimensions sizes, or one must be one-dimensional and both have the same rightmost dimension.

Any help on this would be much appreciated!

Many thanks!

Kindest Regards
Melissa

Full Code:

;*******************************************************
; Mpwapwa.ncl
;
; Concepts illustrated:
;   - Drawing multiple bar charts on a page
;   - Drawing three custom legends outside a bar chart
;   - Using bar charts to draw standard deviations for four time series
;   - Drawing a time series plot
;*******************************************************
;
; These files are loaded by default in NCL V6.2.0 and newer

begin

x = (/1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885, 1886, 1887, 1888, 1889/)

yIndex = (/0,2,-1,-3,2,0,0,0,0,0,0,0,0,0,0,0.05,2,0,-1,-1,-2,1,1,-2,-2,1,-1,-3,0.05,0.05,0.05,-3,0.05,0/)

y20CR3 = (/-1.7049024,  -1.9956352, 0.1017878,  1.5787968,  0.2902762,  0.1615032,  0.7573914,  0.127654184,      0.3391526,  0.18975348, 1.4533978,  0.28738338, 0.8228474,  -1.2142216, 0.67589808, 1.3983746,  1.1087944,  0.58095216, -1.3422622, 0.226781852,      2.173188,   0.6511904,  0.9723054,  -0.4989326, -1.5402072, -0.1535788, -0.226605,  -1.6808838, -0.19809544,      -0.1772666, 0.0769234,  -0.6346154, -1.2222682, -0.0073483/)

;scaled by 5 and deleted first value to go for year 2.
yCMIP5 = (/-0.9094243, 0.5458735, -0.8682191, -1.826278, -0.6501346, 0.1305088, 0.51643, 0.6247665, -0.4082086, 0.3641764, 1.476322, 0.842743, -0.5013026, -0.1281874, 0.4829363, -0.7015711, 0.269588, -0.2834916, -0.8827865, -0.4242322, -1.171241, -0.8289737, -0.4638377, 0.8812776, 0.5154026, -1.191759 ,0.8223722, -1.198702, -0.8878552, -1.724694, -1.62958, 1.02131, -1.440305, -2.299565/)

stdarr = new((/34,3/),"float")
do hh = 0,33
stdarr(hh,0) = (/ (yIndex(hh::34)) /)
stdarr(hh,1) = (/ (y20CR3(hh::34)) /)
stdarr(hh,2) = (/ (yCMIP5(hh::34)) /)
end do
print("Index Data = "+dimsizes(yIndex)+", 20CR3 = "+dimsizes(y20CR3)+", CMIP5 = "+dimsizes(yCMIP5))

printVarSummary(stdarr)

;======================================================================================
wks = gsn_open_wks("X11","Mpwapwa_3_Plot")          ; send graphics to PNG file

sres = True

sres at trXMinF = 1855
sres at trXMaxF = 1890
sres at tmXBValues = ispan(1855,1890,5)     ; create major tickmarks with labels every 5 years
sres at tmXBLabels = sres at tmXBValues
sres at tmXBMinorValues = ispan(1855,1890,1)  ; as tmXBMode = "Explicit", this resource needs to be set to get minor tickmarks. Create minor tickmarks every year.

sres at vpWidthF = 0.8
sres at vpHeightF = 0.5
sres at vpXF = .15
sres at trYMinF = -3.5
sres at trYMaxF = 3.5
sres at gsnDraw = True
;sres at gsnFrame = False
sres at gsnXYBarChart = True
sres at gsnXYBarChartBarWidth = 0.20           ; change bar widths
sres at tmXBMode          = "Explicit"         ; explicit labels
sres at tmXBLabelFontHeightF = 0.0205
sres at tmXTLabelFontHeightF = 0.0205
sres at tmYLLabelFontHeightF = 0.0225
sres at tiMainFontHeightF = 0.025
sres at tiMainFont = "helvetica"
sres at tiMainString = "Precipitation Anomalies for Mpwapwa"
sres at gsnRightString = ""
sres at tiYAxisString = "Pr Anomalies (mm/day) DJFMA"
sres at gsnYRefLine           = 0.              ; reference line

do hh = 0,33
stdarr(hh,0) = (/ (yIndex(hh::34)) /)

if ((hh.eq.1) .or. (hh.eq.2) .or. (hh.eq.3)) then
sres at gsnXYBarChartColors = (/"pink"/)
else
sres at gsnXYBarChartColors = (/"red"/)
end if
plot1 = gsn_csm_xy(wks,fspan(1855.6,1888.7,34),stdarr(hh,0),sres)           ; draw each time series

end do

sres at gsnXYBarChartColors = (/"black"/)                         ; seperately, not
plot2 = gsn_csm_xy(wks,fspan(1855.8,1888.9,34),stdarr(:,1),sres)           ; advancing the frame
sres at gsnXYBarChartColors = (/"blue"/)                             ; drawn on the X-axis
plot3 = gsn_csm_xy(wks,fspan(1856.4,1889.2,34),stdarr(:,2),sres)

lbres                    = True          ; labelbar only resources
lbres at vpWidthF           = 0.2           ; labelbar width
lbres at vpHeightF          = 0.1           ; labelbar height
lbres at lbBoxMajorExtentF  = 0.56          ; puts space between color boxes
lbres at lbMonoFillPattern  = True          ; Solid fill pattern
lbres at lbLabelFontHeightF = 0.025         ; font height. default is small
lbres at lbLabelJust        = "CenterLeft"  ; left justify labels
lbres at lbPerimOn          = False
lbres at lgPerimColor        = "white"
colors = (/"red",   "black",  "blue"/)
labels = (/"Index", "Reanalysis", "CMIP5"/)
xpos   = (/0.15,    0.40,     0.7/)

do i=0,2
lbres at lbFillColors = colors(i)
gsn_labelbar_ndc(wks,1,labels(i),xpos(i),0.20,lbres)
end do

frame(wks)
end

Dr. Melissa Lazenby
Lecturer in Climate Change
Department of Geography
Chichester 1 C150
University of Sussex

"Education is the most powerful weapon which you can use to change the world" Nelson Mandela

[cid:18685db63bd5b2dc94e1]
_______________________________________________
ncl-talk mailing list
ncl-talk at mailman.ucar.edu<mailto:ncl-talk at mailman.ucar.edu>
List instructions, subscriber options, unsubscribe:
https://mailman.ucar.edu/mailman/listinfo/ncl-talk<https://mailman.ucar.edu/mailman/listinfo/ncl-talk>
_______________________________________________
ncl-talk mailing list
ncl-talk at mailman.ucar.edu<mailto:ncl-talk at mailman.ucar.edu>
List instructions, subscriber options, unsubscribe:
https://mailman.ucar.edu/mailman/listinfo/ncl-talk<https://mailman.ucar.edu/mailman/listinfo/ncl-talk>

--
Associate Scientist IV, Climate Analysis Section
Climate and Global Dynamics Laboratory
National Center for Atmospheric Research
www.cgd.ucar.edu/staff/asphilli/<http://www.cgd.ucar.edu/staff/asphilli>

<http://www.cgd.ucar.edu/staff/asphilli>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230227/e52e7060/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Outlook-wdomdmsr.png
Type: image/png
Size: 24937 bytes
Desc: Outlook-wdomdmsr.png
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230227/e52e7060/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Outlook-nxmphdna.png
Type: image/png
Size: 24937 bytes
Desc: Outlook-nxmphdna.png
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230227/e52e7060/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Outlook-eenhxgzr.png
Type: image/png
Size: 24937 bytes
Desc: Outlook-eenhxgzr.png
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20230227/e52e7060/attachment-0002.png>
```