forked from huangynj/Python-Chinamap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_correct_Chinamap_with_SCS-subplot.py
160 lines (125 loc) · 5.49 KB
/
plot_correct_Chinamap_with_SCS-subplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'''
;***********************************************************;
; ;
; This script is a example to plot contours on China map. ;
; Written by Huang Yongjie(IAP/CAS), 2016-05-22. ;
; Convert to Python by YONGJIE HUANG, 2019-02-07. ;
; ;
;***********************************************************;
'''
import numpy as np
import Ngl, Nio
#----------------------------------------------------------------------
# This function attaches outlines from the given shapefile to the plot.
#----------------------------------------------------------------------
def add_shapefile_outlines(wks,plot,filename,color="black",ThicknessF=1.0):
#---Read data off shapefile
f = Nio.open_file(filename, "r")
lon = np.ravel(f.variables["x"][:])
lat = np.ravel(f.variables["y"][:])
plres = Ngl.Resources() # resources for polylines
plres.gsLineColor = color # default is black
plres.gsLineThicknessF = ThicknessF # default is 1.0
plres.gsSegments = f.variables["segments"][:,0]
return Ngl.add_polyline(wks, plot, lon, lat, plres)
#----------------------------------------------------------------------
# Main code
#----------------------------------------------------------------------
# Read variables
f = Nio.open_file("pres.mon.ltm.nc")
pres = f.variables["pres"][0,:,:]
lat = f.variables["lat"][:]
lon = f.variables["lon"][:]
#print(pres)
#----------------------------------------------------------------------
wks = Ngl.open_wks("x11","PyNGL_China_boundary")
Ngl.define_colormap(wks,"gui_default")
res = Ngl.Resources()
res.nglMaximize = True
res.nglDraw = False
res.nglFrame = False
#---------------------------------------------------------------
# set for the map
res.sfXArray = lon
res.sfYArray = lat
res.mpLimitMode = "LatLon"
res.mpMinLatF = 17.
res.mpMaxLatF = 55.
res.mpMinLonF = 72.
res.mpMaxLonF = 136.
#~/anaconda3/envs/ncl_to_python//lib/python3.6/site-packages/ngl/ncarg/database/
#res.mpDataSetName = "Earth..4"
res.mpDataSetName = "./database/Earth..4"
res.mpDataBaseVersion = "MediumRes" # or "Ncarg4_1"
res.mpFillOn = True
res.mpFillBoundarySets = "NoBoundaries"
res.mpFillAreaSpecifiers = ["land","water"]
res.mpSpecifiedFillColors = ["white","white"]
res.mpAreaMaskingOn = True
res.mpMaskAreaSpecifiers = ["China"]
res.mpOutlineBoundarySets = "NoBoundaries"
res.mpOutlineSpecifiers = ["China","China:Provinces"]
#res.mpLandFillColor = "white"
#res.mpOceanFillColor = "white"
#res.mpInlandWaterFillColor = "white"
#res.mpNationalLineColor = "black"
#res.mpProvincialLineColor = "black"
#res.mpGeophysicalLineColor = "black"
res.mpNationalLineThicknessF = 2
res.mpProvincialLineThicknessF = 1
res.mpGridAndLimbOn = False
#---------------------------------------------------------------
# set for the plot
res.cnFillPalette = "gui_default"
res.cnFillOn = True
res.cnFillDrawOrder = "PreDraw"
res.cnLinesOn = False
res.cnLineLabelsOn = False
res.cnLevelSelectionMode = "ManualLevels"
res.cnMinLevelValF = 520.
res.cnMaxLevelValF = 1080.
res.cnLevelSpacingF = 20.
#res.nglSpreadColors = True
res.lbLabelAutoStride = True
res.lbLabelFontHeightF = 0.009
res.lbOrientation = "horizontal"
res.pmTickMarkDisplayMode = "Always"
map = Ngl.contour_map(wks,pres,res)
#-- write Left and Right strings to the plot
vpx = Ngl.get_float(map,'vpXF')
vpy = Ngl.get_float(map,'vpYF')
vpw = Ngl.get_float(map,'vpWidthF')
fnth = Ngl.get_float(map,'tmXBLabelFontHeightF')
txres = Ngl.Resources()
txres.txFontHeightF = fnth
txres.txJust = "CenterLeft"
Ngl.text_ndc(wks,"Surface pressure over part China Map with SCS",vpx,vpy+0.02,txres)
txres.txJust = "CenterRight"
Ngl.text_ndc(wks,"hPa",vpx+vpw,vpy+0.02,txres)
#--- add South China Sea ---
nhres = res
nhres.nglMaximize = False
nhres.vpHeightF = 0.18
nhres.vpWidthF = 0.18
nhres.mpMinLatF = 2.0
nhres.mpMaxLatF = 23.0
nhres.mpMinLonF = 105.0
nhres.mpMaxLonF = 123.0
nhres.lbLabelBarOn = False
nhres.tmXBOn = False
nhres.tmXTOn = False
nhres.tmYLOn = False
nhres.tmYROn = False
map_nanhai = Ngl.contour_map(wks,pres,nhres)
adres = Ngl.Resources()
adres.amParallelPosF = 0.495 # -0.5 is the left edge of the plot.
adres.amOrthogonalPosF = 0.49 # -0.5 is the top edge of the plot.
adres.amJust = "BottomRight"
plotnh = Ngl.add_annotation(map,map_nanhai,adres)
#--- add Yangtze and Yellow rivers ---
plotrv = add_shapefile_outlines(wks, map, "./cnmap_NetCDF/rivers.nc", "blue", 3.0)
#--- add City ---
plotct = add_shapefile_outlines(wks, map, "./cnmap_NetCDF/diquJie_polyline.nc", "grey")
Ngl.draw(map)
Ngl.frame(wks)
Ngl.end()