Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mov run #994

Merged
merged 18 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pre-commit check
  • Loading branch information
lee1043 committed Nov 2, 2023
commit 6f05b680d9483a62bd55b89098b8dddba8056c61
2 changes: 1 addition & 1 deletion pcmdi_metrics/mjo/lib/post_process_plot_ensemble_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def main():
# mip = "cmip5"
mip = 'cmip6'
mip = "cmip6"
exp = "historical"
version = "v20230924"
period = "1985-2004"
Expand Down
56 changes: 30 additions & 26 deletions pcmdi_metrics/variability_mode/lib/plot_map.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import faulthandler
import sys

#import cartopy
# import cartopy
import cartopy.crs as ccrs
import matplotlib.path as mpath
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
from cartopy.mpl.gridliner import LATITUDE_FORMATTER, LONGITUDE_FORMATTER
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter
from pcmdi_metrics.variability_mode.lib import debug_print
from cartopy.feature import LAND as cartopy_land
from cartopy.feature import OCEAN as cartopy_ocean
from cartopy.mpl.gridliner import LATITUDE_FORMATTER, LONGITUDE_FORMATTER
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter

import faulthandler
from pcmdi_metrics.variability_mode.lib import debug_print

faulthandler.enable()


def plot_map(mode, model, syear, eyear, season, eof_Nth, frac_Nth, output_file_name, debug=False):
def plot_map(
mode, model, syear, eyear, season, eof_Nth, frac_Nth, output_file_name, debug=False
):
"""Plot dive down map and save

Parameters
Expand Down Expand Up @@ -74,7 +76,9 @@ def plot_map(mode, model, syear, eyear, season, eof_Nth, frac_Nth, output_file_n
+ percentage
)

debug_print('plot_map: projection, plot_title:' + projection +', '+ plot_title, debug)
debug_print(
"plot_map: projection, plot_title:" + projection + ", " + plot_title, debug
)

gridline = True

Expand Down Expand Up @@ -148,7 +152,7 @@ def plot_map_cartopy(
Switch for debugging print statements (default is False)
"""

debug_print('plot_map_cartopy starts', debug)
debug_print("plot_map_cartopy starts", debug)

lons = data.getLongitude()
lats = data.getLatitude()
Expand All @@ -160,8 +164,8 @@ def plot_map_cartopy(
if debug:
print(min_lon, max_lon, min_lat, max_lat)

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

Expand All @@ -185,19 +189,19 @@ def plot_map_cartopy(
standard_parallels=(20, max_lat),
)
else:
print('Error: projection not defined!')
print("Error: projection not defined!")

if debug:
debug_print('Central longitude setup completes', debug)
print('projection:', projection)
debug_print("Central longitude setup completes", debug)
print("projection:", projection)

# Generate plot
debug_print('Generate plot starts', debug)
#fig = plt.figure(figsize=(8, 6))
debug_print('fig done', debug)
#ax = plt.axes(projection=projection)
debug_print("Generate plot starts", debug)
fig = plt.figure(figsize=(8, 6))
debug_print("fig done", debug)
# ax = plt.axes(projection=projection)
ax = plt.axes(projection=ccrs.NorthPolarStereo())
debug_print('ax done', debug)
debug_print("ax done", debug)
im = ax.contourf(
lons,
lats,
Expand All @@ -207,12 +211,12 @@ def plot_map_cartopy(
levels=levels,
extend="both",
)
debug_print('contourf done', debug)
debug_print("contourf done", debug)
ax.coastlines()
debug_print('Generate plot completed', debug)
debug_print("Generate plot completed", debug)

# Grid Lines and tick labels
debug_print('projection starts', debug)
debug_print("projection starts", debug)
if proj == "PlateCarree":
if data_area == "global":
if gridline:
Expand All @@ -230,7 +234,7 @@ def plot_map_cartopy(
if gridline:
gl = ax.gridlines(alpha=0.5, linestyle="--")
elif "Stereo" in proj:
debug_print(proj + ' start', debug)
debug_print(proj + " start", debug)
if gridline:
gl = ax.gridlines(draw_labels=True, alpha=0.5, linestyle="--")
gl.xlocator = mticker.FixedLocator(
Expand All @@ -254,7 +258,7 @@ def plot_map_cartopy(
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)
ax.set_boundary(circle, transform=ax.transAxes)
debug_print(proj + ' plotted', debug)
debug_print(proj + " plotted", debug)
elif proj == "Lambert":
# Make a boundary path in PlateCarree projection, I choose to start in
# the bottom left and go round anticlockwise, creating a boundary point
Expand All @@ -271,7 +275,7 @@ def plot_map_cartopy(
draw_labels=True,
alpha=0.8,
linestyle="--",
crs=cartopy.crs.PlateCarree(),
crs=ccrs.PlateCarree(),
)
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
Expand All @@ -284,7 +288,7 @@ def plot_map_cartopy(
right_label = ea.get_position()[0] > 0
if right_label:
ea.set_visible(False)
debug_print('projection completed', debug)
debug_print("projection completed", debug)

# Add title
plt.title(title, pad=15, fontsize=15)
Expand All @@ -311,6 +315,6 @@ def plot_map_cartopy(
)

# Done, save figure
debug_print('plot done, save figure as ' + filename, debug)
debug_print("plot done, save figure as " + filename, debug)
fig.savefig(filename)
plt.close("all")
12 changes: 8 additions & 4 deletions pcmdi_metrics/variability_mode/param/myParam_demo_NAM.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
# Analysis Options
# -------------------------------------------------
variability_mode = "NAM" # Available domains: NAM, NAO, SAM, PNA, PDO
seasons = ["DJF", "MAM", "JJA", "SON"] # Available seasons: DJF, MAM, JJA, SON, monthly, yearly
seasons = [
"DJF",
"MAM",
"JJA",
"SON",
] # Available seasons: DJF, MAM, JJA, SON, monthly, yearly

ConvEOF = True # Calculate conventioanl EOF for model
CBF = True # Calculate Common Basis Function (CBF) for model
Expand All @@ -31,8 +36,8 @@
reference_data_path = os.path.join(
"/p/user_pub/PCMDIobs/obs4MIPs/NOAA-ESRL-PSD/20CR/mon/psl/gn/latest",
"psl_mon_20CR_PCMDI_gn_187101-201212.nc"
#"/p/user_pub/PCMDIobs/PCMDIobs2/atmos/mon/psl/20CR/gn/v20200707",
#"psl_mon_20CR_BE_gn_v20200707_187101-201212.nc",
# "/p/user_pub/PCMDIobs/PCMDIobs2/atmos/mon/psl/20CR/gn/v20200707",
# "psl_mon_20CR_BE_gn_v20200707_187101-201212.nc",
)

varOBS = "psl"
Expand Down Expand Up @@ -88,4 +93,3 @@
# Output for models
nc_out = True
plot = True

2 changes: 1 addition & 1 deletion pcmdi_metrics/variability_mode/param/myParam_demo_PDO.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@

# Output for models
nc_out = True
plot = True
plot = True
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ def find_latest(path):
dir_list = [p for p in glob.glob(path + "/v????????")]
return sorted(dir_list)[-1]


# ---------------------------------------------------------------

mip = "cmip6"
exp = "historical"

datadir = '/p/user_pub/pmp/pmp_results/pmp_v1.1.2/additional_xmls/latest'
datadir = "/p/user_pub/pmp/pmp_results/pmp_v1.1.2/additional_xmls/latest"
param_dir = "../param"

modes = ["NAM", "SAM", "NAO", "PNA", "NPO", "PDO", "NPGO"]

case_id = "{:v%Y%m%d}".format(datetime.datetime.now())

num_workers = 40
debug = True
debug = True

# ---------------------------------------------------------------

Expand All @@ -49,64 +50,72 @@ def find_latest(path):
else:
eofn_obs = 1
eofn_mod = 1

datadir_ver = find_latest(datadir)
datadir_final = os.path.join(datadir_ver, mip, exp, 'atmos', 'mon', var)
xml_list = glob.glob(os.path.join(datadir_final, '*.xml'))
datadir_final = os.path.join(datadir_ver, mip, exp, "atmos", "mon", var)

xml_list = glob.glob(os.path.join(datadir_final, "*.xml"))

# get list of models
models_list = sort_human(
[r.split("/")[-1].split(".")[2] for r in xml_list]
)
models_list = sort_human([r.split("/")[-1].split(".")[2] for r in xml_list])
# remove repeat
models_list = sort_human(list(dict.fromkeys(models_list)))

if debug:
models_list = models_list[0:3]

print(models_list)
print(len(models_list))

for model in models_list:
file_list_model = glob.glob(os.path.join(datadir_final, '*.' + model + '.*.xml'))
file_list_model = glob.glob(
os.path.join(datadir_final, "*." + model + ".*.xml")
)
runs_list = sort_human(
[
r.split("/")[-1].split(".")[3]
for r in file_list_model
]
[r.split("/")[-1].split(".")[3] for r in file_list_model]
)

if debug:
runs_list = runs_list[0:1]

print(model, runs_list)

for run in runs_list:

cmd_content = [
'variability_modes_driver.py',
'-p', os.path.join(param_dir, param_file),
'--variability_mode', mode,
'--osyear', str(osyear),
'--eofn_obs', str(eofn_obs),
'--eofn_mod', str(eofn_mod),
'--mip', mip, '--exp', exp,
'--modnames', model,
'--realization', run,
"variability_modes_driver.py",
"-p",
os.path.join(param_dir, param_file),
"--variability_mode",
mode,
"--osyear",
str(osyear),
"--eofn_obs",
str(eofn_obs),
"--eofn_mod",
str(eofn_mod),
"--mip",
mip,
"--exp",
exp,
"--modnames",
model,
"--realization",
run,
]

if model != models_list[0] or run != runs_list[0]:
cmd_content.extend(["--no_plot_obs", "--no_nc_out_obs"])

if debug:
cmd_content.append('--debug True')
cmd_content.append("--debug True")

cmd = " ".join(cmd_content)

cmd = ' '.join(cmd_content)
log_file = (
"_".join(["variability_modes", mode, mip, exp, model, run]) + ".txt"
)

log_file = '_'.join(['variability_modes', mode, mip, exp, model, run]) + '.txt'

print(cmd)
print(cmd)
cmds_list.append(cmd)
logfilename_list.append(log_file)

Expand All @@ -118,8 +127,11 @@ def find_latest(path):
"/p/user_pub/pmp/pmp_results/pmp_v1.1.2",
"log",
"variability_modes",
mip, exp, case_id)

mip,
exp,
case_id,
)

os.makedirs(log_dir, exist_ok=True)

parallel_submitter(
Expand All @@ -128,4 +140,3 @@ def find_latest(path):
logfilename_list=logfilename_list,
num_workers=num_workers,
)

8 changes: 4 additions & 4 deletions pcmdi_metrics/variability_mode/variability_modes_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@
eof_lr_obs[season](region_subdomain),
frac_obs[season],
output_img_file_obs,
debug=debug
debug=debug,
)
plot_map(
mode + "_teleconnection",
Expand All @@ -462,7 +462,7 @@
eof_lr_obs[season](longitude=(lon1g, lon2g)),
frac_obs[season],
output_img_file_obs + "_teleconnection",
debug=debug
debug=debug,
)
debug_print("obs plotting end", debug)

Expand Down Expand Up @@ -757,7 +757,7 @@
eof_lr_cbf(region_subdomain),
frac_cbf,
output_img_file + "_cbf",
debug=debug
debug=debug,
)
plot_map(
mode + "_teleconnection",
Expand All @@ -768,7 +768,7 @@
eof_lr_cbf(longitude=(lon1g, lon2g)),
frac_cbf,
output_img_file + "_cbf_teleconnection",
debug=debug
debug=debug,
)

debug_print("cbf pcs end", debug)
Expand Down
Loading