5454
5555from matplotlib .ticker import (
5656 LogFormatter , NullFormatter , LogFormatterSciNotation ,
57- MultipleLocator
57+ MultipleLocator , FuncFormatter
5858)
5959
6060# Load amesCAP modules
7676global current_version
7777current_version = 3.5
7878
79+ PRESSURE_CONVERSIONS = {
80+ "Pa" : {"scale" : 1.0 , "label" : "Pressure [Pa]" },
81+ "hPa" : {"scale" : 1e-2 , "label" : "Pressure [hPa]" },
82+ "mbar" : {"scale" : 1e-2 , "label" : "Pressure [mbar]" },
83+ }
84+
85+ ALTITUDE_CONVERSIONS = {
86+ "m" : {"scale" : 1.0 , "label" : "Altitude [m]" },
87+ "km" : {"scale" : 1e-3 , "label" : "Altitude [km]" },
88+ }
7989
8090def debug_wrapper (func ):
8191 """
@@ -641,6 +651,18 @@ def main():
641651# Alt. TRUE = include NaNs (use np.mean)
642652include_NaNs = eval ("np.array(show_NaN_in_slice)" , namespace )
643653
654+ # Define the default Z unit to plot
655+ pres_unit_pref = "Pa"
656+ alt_unit_pref = "m"
657+
658+ try :
659+ pres_unit_pref = eval ("pres_unit_pref" , namespace )
660+ except NameError :
661+ pass
662+ try :
663+ alt_unit_pref = eval ("alt_unit_pref" , namespace )
664+ except NameError :
665+ pass
644666
645667def mean_func (arr , axis ):
646668 """
@@ -3610,18 +3632,29 @@ def do_plot(self):
36103632 var_info += f" (& { var_info2 } )"
36113633
36123634 if self .vert_unit == "Pa" :
3635+ conv = PRESSURE_CONVERSIONS .get (pres_unit_pref , PRESSURE_CONVERSIONS ["Pa" ])
3636+
36133637 ax .set_yscale ("log" )
36143638 ax .invert_yaxis ()
3615- ax .yaxis .set_major_formatter (CustomTicker ())
3639+ ax .yaxis .set_major_formatter (
3640+ FuncFormatter (lambda val , _ : CustomTicker ()(val * conv ["scale" ]))
3641+ )
36163642 ax .yaxis .set_minor_formatter (NullFormatter ())
3617- ylabel_txt = "Pressure [Pa]"
3643+ ylabel_txt = conv [ "label" ]
36183644 else :
3619- ylabel_txt = "Altitude [m]"
3645+ conv = ALTITUDE_CONVERSIONS .get (alt_unit_pref , ALTITUDE_CONVERSIONS ["m" ])
3646+
3647+ # Rescale altitude tick labels
3648+ ax .yaxis .set_major_formatter (
3649+ FuncFormatter (lambda val , _ : f"{ val * conv ['scale' ]:.0f} " )
3650+ )
3651+ ylabel_txt = conv ["label" ]
36203652
36213653 if self .Xlim :
36223654 plt .xlim (self .Xlim )
36233655 if self .Ylim :
3624- plt .ylim (self .Ylim )
3656+ Ylim_native = [y / conv ["scale" ] for y in self .Ylim ]
3657+ plt .ylim (Ylim_native )
36253658
36263659 super (Fig_2D_lat_lev , self ).make_title (var_info , "Latitude" ,
36273660 ylabel_txt )
@@ -3725,18 +3758,29 @@ def do_plot(self):
37253758 var_info += f" (& { var_info2 } )"
37263759
37273760 if self .vert_unit == "Pa" :
3761+ conv = PRESSURE_CONVERSIONS .get (pres_unit_pref , PRESSURE_CONVERSIONS ["Pa" ])
3762+
37283763 ax .set_yscale ("log" )
37293764 ax .invert_yaxis ()
3730- ax .yaxis .set_major_formatter (CustomTicker ())
3765+ ax .yaxis .set_major_formatter (
3766+ FuncFormatter (lambda val , _ : CustomTicker ()(val * conv ["scale" ]))
3767+ )
37313768 ax .yaxis .set_minor_formatter (NullFormatter ())
3732- ylabel_txt = "Pressure [Pa]"
3769+ ylabel_txt = conv [ "label" ]
37333770 else :
3734- ylabel_txt = "Altitude [m]"
3771+ conv = ALTITUDE_CONVERSIONS .get (alt_unit_pref , ALTITUDE_CONVERSIONS ["m" ])
3772+
3773+ # Rescale altitude tick labels
3774+ ax .yaxis .set_major_formatter (
3775+ FuncFormatter (lambda val , _ : f"{ val * conv ['scale' ]:.0f} " )
3776+ )
3777+ ylabel_txt = conv ["label" ]
37353778
37363779 if self .Xlim :
37373780 plt .xlim (self .Xlim )
37383781 if self .Ylim :
3739- plt .ylim (self .Ylim )
3782+ Ylim_native = [y / conv ["scale" ] for y in self .Ylim ]
3783+ plt .ylim (Ylim_native )
37403784
37413785 super (Fig_2D_lon_lev , self ).make_title (
37423786 var_info , "Longitude" , ylabel_txt )
@@ -3864,8 +3908,6 @@ def do_plot(self):
38643908 idmin = np .argmin (abs (SolDay - self .Xlim [0 ]))
38653909 idmax = np .argmin (abs (SolDay - self .Xlim [1 ]))
38663910 plt .xlim ([LsDay [idmin ], LsDay [idmax ]])
3867- if self .Ylim :
3868- plt .ylim (self .Ylim )
38693911
38703912 Ls_ticks = [item for item in ax .get_xticks ()]
38713913 labels = [item for item in ax .get_xticklabels ()]
@@ -3891,13 +3933,27 @@ def do_plot(self):
38913933 rotation = 0 )
38923934
38933935 if self .vert_unit == "Pa" :
3936+ conv = PRESSURE_CONVERSIONS .get (pres_unit_pref , PRESSURE_CONVERSIONS ["Pa" ])
3937+
38943938 ax .set_yscale ("log" )
38953939 ax .invert_yaxis ()
3896- ax .yaxis .set_major_formatter (CustomTicker ())
3940+ ax .yaxis .set_major_formatter (
3941+ FuncFormatter (lambda val , _ : CustomTicker ()(val * conv ["scale" ]))
3942+ )
38973943 ax .yaxis .set_minor_formatter (NullFormatter ())
3898- ylabel_txt = "Pressure [Pa]"
3944+ ylabel_txt = conv [ "label" ]
38993945 else :
3900- ylabel_txt = "Altitude [m]"
3946+ conv = ALTITUDE_CONVERSIONS .get (alt_unit_pref , ALTITUDE_CONVERSIONS ["m" ])
3947+
3948+ # Rescale altitude tick labels
3949+ ax .yaxis .set_major_formatter (
3950+ FuncFormatter (lambda val , _ : f"{ val * conv ['scale' ]:.0f} " )
3951+ )
3952+ ylabel_txt = conv ["label" ]
3953+
3954+ if self .Ylim :
3955+ Ylim_native = [y / conv ["scale" ] for y in self .Ylim ]
3956+ plt .ylim (Ylim_native )
39013957
39023958 super (Fig_2D_time_lev , self ).make_title (
39033959 var_info , "L$_s$" , ylabel_txt )
@@ -4895,19 +4951,30 @@ def do_plot(self):
48954951 - self .nPan * label_factor ))
48964952
48974953 if self .vert_unit == "Pa" :
4954+ conv = PRESSURE_CONVERSIONS .get (pres_unit_pref , PRESSURE_CONVERSIONS ["Pa" ])
4955+
48984956 ax .set_yscale ("log" )
48994957 ax .invert_yaxis ()
4900- ax .yaxis .set_major_formatter (CustomTicker ())
4958+ ax .yaxis .set_major_formatter (
4959+ FuncFormatter (lambda val , _ : CustomTicker ()(val * conv ["scale" ]))
4960+ )
49014961 ax .yaxis .set_minor_formatter (NullFormatter ())
4902- ylabel_txt = "Pressure [Pa]"
4962+ ylabel_txt = conv [ "label" ]
49034963 else :
4904- ylabel_txt = "Altitude [m]"
4964+ conv = ALTITUDE_CONVERSIONS .get (alt_unit_pref , ALTITUDE_CONVERSIONS ["m" ])
4965+
4966+ # Rescale altitude tick labels
4967+ ax .yaxis .set_major_formatter (
4968+ FuncFormatter (lambda val , _ : f"{ val * conv ['scale' ]:.0f} " )
4969+ )
4970+ ylabel_txt = conv ["label" ]
49054971
49064972 plt .ylabel (ylabel_txt ,
49074973 fontsize = (label_size - self .nPan * label_factor ))
49084974
49094975 if self .Dlim :
4910- plt .ylim (self .Dlim )
4976+ Ylim_native = [y / conv ["scale" ] for y in self .Dlim ]
4977+ plt .ylim (Ylim_native )
49114978 if self .Vlim :
49124979 plt .xlim (self .Vlim )
49134980
0 commit comments