Update to establish running version with plotting of lat-lon-fields
This commit is contained in:
parent
339af5e826
commit
abf569ec36
@ -8,9 +8,11 @@
|
|||||||
# Load necessary modules #####################################################################
|
# Load necessary modules #####################################################################
|
||||||
import numpy as np #
|
import numpy as np #
|
||||||
import time # to measure elapsed time
|
import time # to measure elapsed time
|
||||||
|
# read data
|
||||||
from get_data import get_data_woa2009 as read_data
|
from get_data import get_data_woa2009 as read_data
|
||||||
|
# plotting modules / functions
|
||||||
from myplot import myplot_2dmap # plot of lat-lon fields
|
from myplot import myplot_2dmap # plot of lat-lon fields
|
||||||
from myplot_inputs import mycalc_create_pn # create plotname
|
from myplot_inputs import myplot_create_pn # create plotname
|
||||||
# ############################################################################################
|
# ############################################################################################
|
||||||
|
|
||||||
# Input 0 ####################################################################################
|
# Input 0 ####################################################################################
|
||||||
@ -24,7 +26,7 @@ from myplot_inputs import mycalc_create_pn # create plotname
|
|||||||
print 'MANUAL DEFINED OUTPUTS MISSING'
|
print 'MANUAL DEFINED OUTPUTS MISSING'
|
||||||
dim = ('lon','lat','z','time',)
|
dim = ('lon','lat','z','time',)
|
||||||
dir_plot = '/glusterfs/inspect/users/xg911182/Code/Python/plots_test/'
|
dir_plot = '/glusterfs/inspect/users/xg911182/Code/Python/plots_test/'
|
||||||
variables = {'s':{},'theta':{},'grd':[],'valid':[]}
|
variables = {'s':{},'temp':{},'grd':[],'valid':[]}
|
||||||
lonlat_reg = [25.0, 25.0+360.0, -90.0, 90.0] # plotting range for 2dmap
|
lonlat_reg = [25.0, 25.0+360.0, -90.0, 90.0] # plotting range for 2dmap
|
||||||
# Input 1 ####################################################################################
|
# Input 1 ####################################################################################
|
||||||
# Physical constants
|
# Physical constants
|
||||||
@ -162,16 +164,17 @@ i_t = 0
|
|||||||
date_str = '2009'
|
date_str = '2009'
|
||||||
for i_key in range(0,len(list_allkeys)):
|
for i_key in range(0,len(list_allkeys)):
|
||||||
print 'put this plotname stuff into a function'
|
print 'put this plotname stuff into a function'
|
||||||
pname_str = mycalc_create_pn({'lonlat':lonlat_reg})
|
# create plotname
|
||||||
plotname_dum = dir_plot + 'Map2d_' + list_allkeys[i_key] + pname_str['lonlat'] + \
|
plotname = myplot_create_pn({'dir_plot':dir_plot,'beg_str':'Map2d',\
|
||||||
'_z' + str(grd.z[i_dep]) + grd.Uz + '_' + date_str + '.png'
|
'varname':list_allkeys[i_key],'zlev':[grd.z[i_dep],grd.Uz],\
|
||||||
|
'lonlat':lonlat_reg,'date':date_str,'end_str':'.png'})
|
||||||
# define plot input
|
# define plot input
|
||||||
title_in = 'Depth='+'{:.1f}'.format(grd.z[i_dep])+grd.Uz
|
title_in = 'Depth='+'{:.1f}'.format(grd.z[i_dep])+grd.Uz
|
||||||
data_in = data[list_allkeys[i_key]]['val'][:,:,i_dep,i_t]
|
data_in = data[list_allkeys[i_key]]['val'][:,:,i_dep,i_t]
|
||||||
data_in[data_in==data[list_allkeys[i_key]]['fill_value']] = np.nan
|
data_in[data_in==data[list_allkeys[i_key]]['fill_value']] = np.nan
|
||||||
myplot_2dmap(data_in,grd,lonlat_range=lonlat_reg,saveplot=1,\
|
myplot_2dmap(data_in,grd,lonlat_range=lonlat_reg,saveplot=1,\
|
||||||
title_c=title_in,plotname=plotname_dum)
|
title_c=title_in,plotname=plotname,unit_data=data[list_allkeys[i_key]]['units'])
|
||||||
del data_in, title_in, pname_str,plotname_dum
|
del data_in, title_in, plotname
|
||||||
print ' ----------------------------------------------'
|
print ' ----------------------------------------------'
|
||||||
end_time = time.time()
|
end_time = time.time()
|
||||||
print 'Elapsed time to plot 2d fields (lat-lon): '+ '{:.2f}'.format(end_time-beg_time)+'s'
|
print 'Elapsed time to plot 2d fields (lat-lon): '+ '{:.2f}'.format(end_time-beg_time)+'s'
|
||||||
|
31
myplot.py
31
myplot.py
@ -130,7 +130,7 @@ def myplot_1d(xxx,yyy,FS=22,trend=0,rm_yyy=0,yyy_per=0,col_c=('b','r','k'),label
|
|||||||
|
|
||||||
#def myplot_2dmap(xxx,yyy,FS=22,trend=0,rm_yyy=0,yyy_per=0,col_c=('b','r','k'),label_c=('None'),xlabel_c='no xlabel info',ylabel_c='no ylabel info',saveplot=0,plotname='dummy.png',window='step',title_c=[]):
|
#def myplot_2dmap(xxx,yyy,FS=22,trend=0,rm_yyy=0,yyy_per=0,col_c=('b','r','k'),label_c=('None'),xlabel_c='no xlabel info',ylabel_c='no ylabel info',saveplot=0,plotname='dummy.png',window='step',title_c=[]):
|
||||||
def myplot_2dmap(data_in,grd,FS=22,plotname='dummy.png',saveplot=0,lonlat_range=[0.0,360.0,-90.0,90.0],\
|
def myplot_2dmap(data_in,grd,FS=22,plotname='dummy.png',saveplot=0,lonlat_range=[0.0,360.0,-90.0,90.0],\
|
||||||
xlabel_c=[],ylabel_c=[],title_c=[]):
|
xlabel_c=[],ylabel_c=[],title_c=[],cbar_in=[],unit_data=[],):
|
||||||
# from myplot import myplot_2dmap
|
# from myplot import myplot_2dmap
|
||||||
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
# input choices for myplot_1d %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
# input choices for myplot_1d %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
@ -150,6 +150,7 @@ def myplot_2dmap(data_in,grd,FS=22,plotname='dummy.png',saveplot=0,lonlat_range=
|
|||||||
import math
|
import math
|
||||||
from mycalc_matrix import mymatrix_smooth
|
from mycalc_matrix import mymatrix_smooth
|
||||||
from mpl_toolkits.basemap import Basemap, addcyclic, shiftgrid
|
from mpl_toolkits.basemap import Basemap, addcyclic, shiftgrid
|
||||||
|
from myplot_inputs import myplot_create_cbar
|
||||||
# => cartopy instead of basemap
|
# => cartopy instead of basemap
|
||||||
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
# general plotting setup
|
# general plotting setup
|
||||||
@ -180,8 +181,27 @@ def myplot_2dmap(data_in,grd,FS=22,plotname='dummy.png',saveplot=0,lonlat_range=
|
|||||||
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
# plotting of data_in
|
# plotting of data_in
|
||||||
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
mymapf = plt.contourf(lonall, latall, data_in, 10, cmap=plt.cm.Reds)
|
# create colorbar rang and labelinge
|
||||||
mymap = plt.contour(lonall, latall, data_in, 10, colors='k')
|
if not cbar_in:
|
||||||
|
cbar_min = np.nanpercentile(data_in[:],2)
|
||||||
|
cbar_max = np.nanpercentile(data_in[:],98)
|
||||||
|
num = 11
|
||||||
|
else:
|
||||||
|
cbar_min = cbar_in[0]
|
||||||
|
cbar_max = cbar_in[1]
|
||||||
|
num = cbar_in[2]
|
||||||
|
cbar_range = myplot_create_cbar(cbar_min,cbar_max,num=num)
|
||||||
|
cbar_label = map(str, cbar_range[::1])
|
||||||
|
if len(cbar_in) > 3:
|
||||||
|
cbar_label[-1] = cbar_in[3]
|
||||||
|
elif unit_data:
|
||||||
|
cbar_label[-1] = '[' + unit_data + ']'
|
||||||
|
# actual plotting
|
||||||
|
mymapf = plt.contourf(lonall, latall, data_in, cbar_range, cmap=plt.cm.Reds)
|
||||||
|
#mymapf = plt.contourf(lonall, latall, data_in, 10, cmap=plt.cm.Reds, \
|
||||||
|
# vmin=cbar_min, vmax=cbar_max)
|
||||||
|
mymap = plt.contour(lonall, latall, data_in, cbar_range, colors='k')
|
||||||
|
plt.clim(cbar_range[0],cbar_range[1])
|
||||||
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
# include contour labels, colorbar, etc.
|
# include contour labels, colorbar, etc.
|
||||||
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
@ -190,7 +210,10 @@ def myplot_2dmap(data_in,grd,FS=22,plotname='dummy.png',saveplot=0,lonlat_range=
|
|||||||
del fmt_d
|
del fmt_d
|
||||||
if title_c:
|
if title_c:
|
||||||
plt.title(title_c, fontsize=FS)
|
plt.title(title_c, fontsize=FS)
|
||||||
plt.colorbar(mymapf, orientation='horizontal', shrink=0.95)
|
# colorbar settings
|
||||||
|
cbar = plt.colorbar(mymapf, orientation='horizontal', shrink=0.9)
|
||||||
|
cbar.set_ticks(cbar_range[::1])
|
||||||
|
cbar.set_ticklabels(cbar_label)
|
||||||
#plt.colorbar(mymapf, orientation='horizontal', shrink=0.64)
|
#plt.colorbar(mymapf, orientation='horizontal', shrink=0.64)
|
||||||
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
# labeling, axis, etc
|
# labeling, axis, etc
|
||||||
|
133
myplot_inputs.py
133
myplot_inputs.py
@ -1,33 +1,116 @@
|
|||||||
def mycalc_create_pn(dict_in):
|
def myplot_create_pn(dict_in):
|
||||||
|
# necessary modules
|
||||||
|
import numpy as np
|
||||||
|
import math # to use floor (nothing else)
|
||||||
# Allocation
|
# Allocation
|
||||||
pname_str = {}
|
|
||||||
# check if restriction to region exist
|
|
||||||
list_allkeys = dict_in.keys()
|
list_allkeys = dict_in.keys()
|
||||||
|
pname_str = []
|
||||||
|
# check if specific plotting directory given
|
||||||
|
if 'dir_plot' in list_allkeys:
|
||||||
|
pname_str = dict_in['dir_plot']
|
||||||
|
# check if beginning_string given
|
||||||
|
if 'beg_str' in list_allkeys:
|
||||||
|
pname_str = pname_str + dict_in['beg_str']
|
||||||
|
else:
|
||||||
|
pname_str = pname_str + 'plot'
|
||||||
|
# check if variable name given
|
||||||
|
if 'varname' in list_allkeys:
|
||||||
|
if dict_in['varname'][0] == '_':
|
||||||
|
pname_str = pname_str + dict_in['varname']
|
||||||
|
else:
|
||||||
|
pname_str = pname_str + '_' + dict_in['varname']
|
||||||
|
else:
|
||||||
|
pname_str = pname_str + '_unknown_var'
|
||||||
|
# check if specific height level given
|
||||||
|
if 'zlev' in list_allkeys:
|
||||||
|
if isinstance(dict_in['zlev'], basestring):
|
||||||
|
if dict_in['zlev'][0] == '_':
|
||||||
|
pname_str = pname_str + dict_in['zlev']
|
||||||
|
else:
|
||||||
|
pname_str = pname_str + '_' + dict_in['zlev']
|
||||||
|
else:
|
||||||
|
if dict_in['zlev'][0] == 0:
|
||||||
|
fac = 1
|
||||||
|
else:
|
||||||
|
fac = 10**(-min(math.floor(np.log10(abs(dict_in['zlev'][0]))),0))
|
||||||
|
if fac > 1:
|
||||||
|
pname_str = pname_str + '_z' + str(int(fac*dict_in['zlev'][0])) + \
|
||||||
|
'e-' + str(np.log10(fac)) + dict_in['zlev'][1]
|
||||||
|
else:
|
||||||
|
pname_str = pname_str + '_z' + str(int(dict_in['zlev'][0])) + dict_in['zlev'][1]
|
||||||
|
# check if restriction to region is given
|
||||||
if 'lonlat' in list_allkeys:
|
if 'lonlat' in list_allkeys:
|
||||||
dummy = dict_in['lonlat']
|
dummy = dict_in['lonlat']
|
||||||
if dummy[0] < 0:
|
if dummy is str:
|
||||||
pn1 = '{:03d}'.format(int(abs(round(dummy[0])))) + 'W'
|
if dummy[0] == '_':
|
||||||
elif dummy[0] > 360:
|
pname_str = pname_str + dummy
|
||||||
pn1 = '{:03d}'.format(int(round(dummy[0])-360)) + 'E'
|
else:
|
||||||
|
pname_str = pname_str + '_' + dummy
|
||||||
else:
|
else:
|
||||||
pn1 = '{:03d}'.format(int(round(dummy[0]))) + 'E'
|
if dummy[0] < 0:
|
||||||
if dummy[1] < 0:
|
pn1 = '{:03d}'.format(int(abs(round(dummy[0])))) + 'W'
|
||||||
pn2 = '{:03d}'.format(int(abs(round(dummy[1])))) + 'W'
|
elif dummy[0] > 360:
|
||||||
elif dummy[1] > 360:
|
pn1 = '{:03d}'.format(int(round(dummy[0])-360)) + 'E'
|
||||||
pn2 = '{:03d}'.format(int(round(dummy[1])-360)) + 'E'
|
else:
|
||||||
|
pn1 = '{:03d}'.format(int(round(dummy[0]))) + 'E'
|
||||||
|
if dummy[1] < 0:
|
||||||
|
pn2 = '{:03d}'.format(int(abs(round(dummy[1])))) + 'W'
|
||||||
|
elif dummy[1] > 360:
|
||||||
|
pn2 = '{:03d}'.format(int(round(dummy[1])-360)) + 'E'
|
||||||
|
else:
|
||||||
|
pn2 = '{:03d}'.format(int(round(dummy[1]))) + 'E'
|
||||||
|
if dummy[2] < 0:
|
||||||
|
pn3 = '{:02d}'.format(int(abs(round(dummy[2])))) + 'S'
|
||||||
|
else:
|
||||||
|
pn3 = '{:02d}'.format(int(round(dummy[2]))) + 'N'
|
||||||
|
if dummy[3] < 0:
|
||||||
|
pn4 = '{:02d}'.format(int(abs(round(dummy[3])))) + 'S'
|
||||||
|
else:
|
||||||
|
pn4 = '{:02d}'.format(int(round(dummy[3]))) + 'N'
|
||||||
|
if 360-(dummy[1]-dummy[0])<1.5:
|
||||||
|
pname_str = pname_str + '_reg' + pn3 + 'to' + pn4
|
||||||
|
else:
|
||||||
|
pname_str = pname_str + '_reg' + pn1 + 'to' + pn2 + pn3 + 'to' + pn4
|
||||||
|
del dummy
|
||||||
|
# check if date information given
|
||||||
|
if 'date' in list_allkeys:
|
||||||
|
if isinstance(dict_in['date'], basestring):
|
||||||
|
if dict_in['date'][0] == '_':
|
||||||
|
pname_str = pname_str + dict_in['date']
|
||||||
|
else:
|
||||||
|
pname_str = pname_str + '_' + dict_in['date']
|
||||||
else:
|
else:
|
||||||
pn2 = '{:03d}'.format(int(round(dummy[1]))) + 'E'
|
print '*** no date string yet included for numerical values in mycalc_create_pn ***'
|
||||||
if dummy[2] < 0:
|
|
||||||
pn3 = '{:02d}'.format(int(abs(round(dummy[2])))) + 'S'
|
|
||||||
else:
|
|
||||||
pn3 = '{:02d}'.format(int(round(dummy[2]))) + 'N'
|
|
||||||
if dummy[3] < 0:
|
|
||||||
pn4 = '{:02d}'.format(int(abs(round(dummy[3])))) + 'S'
|
|
||||||
else:
|
|
||||||
pn4 = '{:02d}'.format(int(round(dummy[3]))) + 'N'
|
|
||||||
pname_str['lonlat'] = '_reg' + pn1 + 'to' + pn2 + pn3 + 'to' + pn4
|
|
||||||
del dummy
|
|
||||||
|
|
||||||
del dict_in, list_allkeys
|
# check if end_str is given
|
||||||
|
if 'end_str' in list_allkeys:
|
||||||
|
pname_str = pname_str + dict_in['end_str']
|
||||||
|
else:
|
||||||
|
pname_str = pname_str + '.png'
|
||||||
return pname_str
|
return pname_str
|
||||||
|
|
||||||
|
def myplot_create_cbar(cbar_min, cbar_max, num=[]):
|
||||||
|
# import modules
|
||||||
|
import numpy as np
|
||||||
|
from math import floor
|
||||||
|
# round values
|
||||||
|
if abs(cbar_max) > abs(cbar_min):
|
||||||
|
fac = floor(np.log10(abs(cbar_max)))
|
||||||
|
else:
|
||||||
|
fac = floor(np.log10(abs(cbar_min)))
|
||||||
|
fac = fac-1
|
||||||
|
cbar_max = round(cbar_max,-int(fac))
|
||||||
|
cbar_min = round(cbar_min,-int(fac))
|
||||||
|
# make array symmetric around zero?
|
||||||
|
if cbar_min<0 and cbar_max>0:
|
||||||
|
if abs(cbar_max+cbar_min) < 0.1*cbar_max:
|
||||||
|
cbar_min = -max(cbar_max,abs(cbar_min))
|
||||||
|
cbar_max = max(cbar_max,abs(cbar_min))
|
||||||
|
# create cbar
|
||||||
|
if num:
|
||||||
|
cbar_range = np.linspace(cbar_min, cbar_max, num=num)
|
||||||
|
else:
|
||||||
|
cbar_range = np.linspace(cbar_min, cbar_max)
|
||||||
|
# output
|
||||||
|
return cbar_range
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user