Skip to content

Commit 7c5c8f1

Browse files
committed
update
1 parent 14eacbe commit 7c5c8f1

File tree

5 files changed

+208
-34
lines changed

5 files changed

+208
-34
lines changed

conda-env/dev.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies:
1111
- python=3.10.10
1212
- pip=23.1.2
1313
- numpy=1.23.5
14-
- cartopy=0.21.1
14+
- cartopy=0.22.0
1515
- matplotlib=3.7.1
1616
- cdat_info=8.2.1
1717
- cdms2=3.1.5
@@ -27,7 +27,7 @@ dependencies:
2727
- netcdf4=1.6.3
2828
- regionmask=0.9.0
2929
- rasterio=1.3.6
30-
- shapely=2.0.1
30+
- shapely=2.0.2
3131
# ==================
3232
# Testing
3333
# ==================

pcmdi_metrics/variability_mode/lib/argparse_functions.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,25 @@ def AddParserArgument(P):
150150
"--nc_out",
151151
type=bool,
152152
default=True,
153-
help="Option for generate netCDF file output: True (default) / False",
153+
help="Option for generate netCDF file output for models: True (default) / False",
154154
)
155155
P.add_argument(
156156
"--plot",
157157
type=bool,
158158
default=True,
159-
help="Option for generate individual plots: True (default) / False",
159+
help="Option for generate individual plots for models: True (default) / False",
160+
)
161+
P.add_argument(
162+
"--nc_out_obs",
163+
type=bool,
164+
default=True,
165+
help="Option for generate netCDF file output for obs: True (default) / False",
166+
)
167+
P.add_argument(
168+
"--plot_obs",
169+
type=bool,
170+
default=True,
171+
help="Option for generate individual plots for obs: True (default) / False",
160172
)
161173
P.add_argument(
162174
"--parallel",

pcmdi_metrics/variability_mode/lib/plot_map.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import sys
22

3-
import cartopy
3+
#import cartopy
44
import cartopy.crs as ccrs
55
import matplotlib.path as mpath
66
import matplotlib.pyplot as plt
77
import matplotlib.ticker as mticker
88
import numpy as np
99
from cartopy.mpl.gridliner import LATITUDE_FORMATTER, LONGITUDE_FORMATTER
1010
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter
11+
from pcmdi_metrics.variability_mode.lib import debug_print
12+
from cartopy.feature import LAND as cartopy_land
13+
from cartopy.feature import OCEAN as cartopy_ocean
1114

15+
import faulthandler
1216

13-
def plot_map(mode, model, syear, eyear, season, eof_Nth, frac_Nth, output_file_name):
17+
faulthandler.enable()
18+
19+
20+
def plot_map(mode, model, syear, eyear, season, eof_Nth, frac_Nth, output_file_name, debug=False):
1421
"""Plot dive down map and save
1522
1623
Parameters
@@ -67,6 +74,8 @@ def plot_map(mode, model, syear, eyear, season, eof_Nth, frac_Nth, output_file_n
6774
+ percentage
6875
)
6976

77+
debug_print('plot_map: projection, plot_title:' + projection +', '+ plot_title, debug)
78+
7079
gridline = True
7180

7281
if mode in [
@@ -97,6 +106,7 @@ def plot_map(mode, model, syear, eyear, season, eof_Nth, frac_Nth, output_file_n
97106
levels=levels,
98107
maskout=maskout,
99108
center_lon_global=center_lon_global,
109+
debug=debug,
100110
)
101111

102112

@@ -138,6 +148,8 @@ def plot_map_cartopy(
138148
Switch for debugging print statements (default is False)
139149
"""
140150

151+
debug_print('plot_map_cartopy starts', debug)
152+
141153
lons = data.getLongitude()
142154
lats = data.getLatitude()
143155

@@ -148,6 +160,8 @@ def plot_map_cartopy(
148160
if debug:
149161
print(min_lon, max_lon, min_lat, max_lat)
150162

163+
debug_print('Central longitude setup starts', debug)
164+
debug_print('proj: '+proj, debug)
151165
# map types example:
152166
# https://github.com/SciTools/cartopy-tutorial/blob/master/tutorial/projections_crs_and_terms.ipynb
153167

@@ -170,22 +184,35 @@ def plot_map_cartopy(
170184
central_latitude=central_latitude,
171185
standard_parallels=(20, max_lat),
172186
)
187+
else:
188+
print('Error: projection not defined!')
189+
190+
if debug:
191+
debug_print('Central longitude setup completes', debug)
192+
print('projection:', projection)
173193

174194
# Generate plot
175-
fig = plt.figure(figsize=(8, 6))
176-
ax = plt.axes(projection=projection)
195+
debug_print('Generate plot starts', debug)
196+
#fig = plt.figure(figsize=(8, 6))
197+
debug_print('fig done', debug)
198+
#ax = plt.axes(projection=projection)
199+
ax = plt.axes(projection=ccrs.NorthPolarStereo())
200+
debug_print('ax done', debug)
177201
im = ax.contourf(
178202
lons,
179203
lats,
180-
data,
204+
np.array(data),
181205
transform=ccrs.PlateCarree(),
182206
cmap=cmap,
183207
levels=levels,
184208
extend="both",
185209
)
210+
debug_print('contourf done', debug)
186211
ax.coastlines()
212+
debug_print('Generate plot completed', debug)
187213

188214
# Grid Lines and tick labels
215+
debug_print('projection starts', debug)
189216
if proj == "PlateCarree":
190217
if data_area == "global":
191218
if gridline:
@@ -203,6 +230,7 @@ def plot_map_cartopy(
203230
if gridline:
204231
gl = ax.gridlines(alpha=0.5, linestyle="--")
205232
elif "Stereo" in proj:
233+
debug_print(proj + ' start', debug)
206234
if gridline:
207235
gl = ax.gridlines(draw_labels=True, alpha=0.5, linestyle="--")
208236
gl.xlocator = mticker.FixedLocator(
@@ -226,6 +254,7 @@ def plot_map_cartopy(
226254
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
227255
circle = mpath.Path(verts * radius + center)
228256
ax.set_boundary(circle, transform=ax.transAxes)
257+
debug_print(proj + ' plotted', debug)
229258
elif proj == "Lambert":
230259
# Make a boundary path in PlateCarree projection, I choose to start in
231260
# the bottom left and go round anticlockwise, creating a boundary point
@@ -250,6 +279,7 @@ def plot_map_cartopy(
250279
right_label = ea.get_position()[0] > 0
251280
if right_label:
252281
ea.set_visible(False)
282+
debug_print('projection completed', debug)
253283

254284
# Add title
255285
plt.title(title, pad=15, fontsize=15)
@@ -268,13 +298,14 @@ def plot_map_cartopy(
268298
if maskout is not None:
269299
if maskout == "land":
270300
ax.add_feature(
271-
cartopy.feature.LAND, zorder=100, edgecolor="k", facecolor="lightgrey"
301+
cartopy_land, zorder=100, edgecolor="k", facecolor="lightgrey"
272302
)
273303
if maskout == "ocean":
274304
ax.add_feature(
275-
cartopy.feature.OCEAN, zorder=100, edgecolor="k", facecolor="lightgrey"
305+
cartopy_ocean, zorder=100, edgecolor="k", facecolor="lightgrey"
276306
)
277307

278308
# Done, save figure
309+
debug_print('plot done, save figure as ' + filename, debug)
279310
fig.savefig(filename)
280311
plt.close("all")
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import datetime
2+
import glob
3+
import os
4+
5+
from pcmdi_metrics.misc.scripts import parallel_submitter
6+
from pcmdi_metrics.variability_mode.lib import sort_human
7+
8+
9+
def find_latest(path):
10+
dir_list = [p for p in glob.glob(path + "/v????????")]
11+
return sorted(dir_list)[-1]
12+
13+
# ---------------------------------------------------------------
14+
15+
mip = "cmip6"
16+
exp = "historical"
17+
18+
datadir = '/p/user_pub/pmp/pmp_results/pmp_v1.1.2/additional_xmls/latest'
19+
param_dir = "../param"
20+
21+
modes = ["NAM", "SAM", "NAO", "PNA", "NPO", "PDO", "NPGO"]
22+
23+
case_id = "{:v%Y%m%d}".format(datetime.datetime.now())
24+
25+
num_workers = 40
26+
debug = True
27+
28+
# ---------------------------------------------------------------
29+
30+
cmds_list = list()
31+
logfilename_list = list()
32+
33+
for mode in modes:
34+
if mode in ["PDO", "NPGO", "AMO"]:
35+
var = "ts"
36+
param_file = "myParam_demo_PDO.py"
37+
else:
38+
var = "psl"
39+
param_file = "myParam_demo_NAM.py"
40+
41+
if mode in ["SAM"]:
42+
osyear = 1955
43+
else:
44+
osyear = 1900
45+
46+
if mode in ["NPO", "NPGO"]:
47+
eofn_obs = 2
48+
eofn_mod = 2
49+
else:
50+
eofn_obs = 1
51+
eofn_mod = 1
52+
53+
datadir_ver = find_latest(datadir)
54+
datadir_final = os.path.join(datadir_ver, mip, exp, 'atmos', 'mon', var)
55+
56+
xml_list = glob.glob(os.path.join(datadir_final, '*.xml'))
57+
58+
# get list of models
59+
models_list = sort_human(
60+
[r.split("/")[-1].split(".")[2] for r in xml_list]
61+
)
62+
# remove repeat
63+
models_list = sort_human(list(dict.fromkeys(models_list)))
64+
65+
if debug:
66+
models_list = models_list[0:3]
67+
68+
print(models_list)
69+
print(len(models_list))
70+
71+
for model in models_list:
72+
file_list_model = glob.glob(os.path.join(datadir_final, '*.' + model + '.*.xml'))
73+
runs_list = sort_human(
74+
[
75+
r.split("/")[-1].split(".")[3]
76+
for r in file_list_model
77+
]
78+
)
79+
80+
if debug:
81+
runs_list = runs_list[0:1]
82+
83+
print(model, runs_list)
84+
85+
for run in runs_list:
86+
87+
cmd_content = [
88+
'variability_modes_driver.py',
89+
'-p', os.path.join(param_dir, param_file),
90+
'--variability_mode', mode,
91+
'--osyear', str(osyear),
92+
'--eofn_obs', str(eofn_obs),
93+
'--eofn_mod', str(eofn_mod),
94+
'--mip', mip, '--exp', exp,
95+
'--modnames', model,
96+
'--realization', run,
97+
]
98+
99+
if model != models_list[0] or run != runs_list[0]:
100+
cmd_content.extend(["--no_plot_obs", "--no_nc_out_obs"])
101+
102+
if debug:
103+
cmd_content.append('--debug True')
104+
105+
cmd = ' '.join(cmd_content)
106+
107+
log_file = '_'.join(['variability_modes', mode, mip, exp, model, run]) + '.txt'
108+
109+
print(cmd)
110+
cmds_list.append(cmd)
111+
logfilename_list.append(log_file)
112+
113+
# =================================================
114+
# Run subprocesses in parallel
115+
# -------------------------------------------------
116+
# log dir
117+
log_dir = os.path.join(
118+
"/p/user_pub/pmp/pmp_results/pmp_v1.1.2",
119+
"log",
120+
"variability_modes",
121+
mip, exp, case_id)
122+
123+
os.makedirs(log_dir, exist_ok=True)
124+
125+
parallel_submitter(
126+
cmds_list,
127+
log_dir=log_dir,
128+
logfilename_list=logfilename_list,
129+
num_workers=num_workers,
130+
)
131+

0 commit comments

Comments
 (0)