forked from ESMG/gridtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkGridsExample02.py
96 lines (76 loc) · 3.49 KB
/
mkGridsExample02.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
#!/usr/bin/env python
# conda: gridTools
# Gridtools library demonstration in
# * Command line
# * ipython
# * jupyter lab console
import os, sys, logging
from gridtools.gridutils import GridUtils
grd = GridUtils()
grd.printMsg("This demonstrates how to work with the debugging system of GridUtils().")
grd.printMsg("")
grd.printMsg("This code generates some useful debugging information by gathering version")
grd.printMsg("for specific libraries. This is helpful to software developers. The")
grd.printMsg("developers may ask for other information depending on the nature of")
grd.printMsg("the problem.")
grd.printMsg("---")
from gridtools.sysinfo import SysInfo
info = SysInfo()
info.show(vList=['platform','python','esmf','esmpy','xgcm','xesmf',
'netCDF4','numpy','xarray',
'cartopy','matplotlib',
'jupyter_core','jupyterlab','notebook',
'dask'])
grd.printMsg("---")
#grd.printMsg("This demonstrates how to access the internal help messages from the GridUtils()")
#grd.printMsg("library. You may need to exit the help page by pressing the q key.")
# Help (module)
#print(help(GridUtils))
#grd.printMsg("")
# Without debugging, a failed assigment to a plot parameter subkey will result in
# a message and the program will continue running. We can easily cause a problem
# by trying to use a non-existing subkey. The only one that exists at the moment is
# projection.
grd.printMsg("")
grd.printMsg("Using a 'test' subkey to try and assign {'a': 1} into plot parameters.")
testParameters = {
'a': 1
}
grd.printMsg("""Attempting to run: grd.setPlotParameters(testParameters, subKey='test')""")
grd.setPlotParameters(testParameters, subKey='test')
grd.printMsg("""
Note: nothing should have printed, it was silently ignored as the debug level
by default is zero(0) and the display of messages (verbosity) by default is at
the INFO level which is one higher than DEBUG. With both these settings, no
messages are emitted.
""")
grd.printMsg("Lets change the debug level to 1 (MESSAGES) and the verbosity to DEBUG and try again.")
grd.printMsg("")
grd.setDebugLevel(1)
grd.setVerboseLevel(logging.DEBUG)
# This should also work
grd.setVerboseLevel("DEBUG")
grd.printMsg("""Attempting to run: grd.setPlotParameters(testParameters, subKey='test')""")
grd.setPlotParameters(testParameters, subKey='test')
grd.printMsg("")
grd.printMsg("""
If that message is utilizing the debugMsg() function, changing the debug level to 3
will attempt to start the python debugger pdb so you can poke around at the code and
attempt to reason out why it is failing at that point.
""")
grd.printMsg("Lets change the debug level to 3 (BREAKPOINT) and try again.")
grd.printMsg("NOTE: The program should stop with a (Pdb) prompt. Use quit() to exit.")
grd.printMsg(" Or you can use c to continue and we can try debug level 2 (RAISE)")
grd.printMsg(" which will stop the program by raising the exception.")
grd.printMsg("")
grd.setDebugLevel(3)
grd.printMsg("""Attempting to run: grd.setPlotParameters(testParameters, subKey='test')""")
grd.setPlotParameters(testParameters, subKey='test')
grd.printMsg("")
grd.printMsg("Change debug level to RAISE (2).")
grd.setDebugLevel(2)
grd.printMsg("""Attempting to run: grd.setPlotParameters(testParameters, subKey='test')""")
grd.setPlotParameters(testParameters, subKey='test')
# No code beyond this point will run as the program will stop
# with an exception.
print("The program should never reach this point and should not be printed.")