-
Notifications
You must be signed in to change notification settings - Fork 4
/
Plots_for_netCDF4.py
executable file
·106 lines (79 loc) · 2.97 KB
/
Plots_for_netCDF4.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
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
"""
Code developed by Jesus Vergara Temprado and Kirsty Pringle
Aerosol modellers group
Institute for climate and atmospheric science (ICAS)
University of Leeds 2016
"""
import numpy as np
import iris
import sys
from __main__ import *
sys.path.append(dir_scripts)
from glob import glob
import matplotlib.pyplot as plt
import iris.plot as iplt
import iris.quickplot as qplt
from scipy.io import netcdf
import datetime
import UKCA_lib as ukl
import netCDF4
import matplotlib
import getpass
#username=getpass.getuser()
plt.interactive(0)
#files_directory='/nfs/a201/'+username+'/UKCA_TEST_FILES/tebxd/'
#saving_folder=files_directory+'PLOTS'
files_directory=output_files_directory
folders=[files_directory+'All_time_steps/',files_directory+'L1/']
#folders=[files_directory+'L1/']
for data_folder in folders:
saving_folder=data_folder+'PLOTS/'
ukl.create_folder(saving_folder)
nc_files=glob(data_folder+'*nc')
for nc_file in nc_files:
#mb=netCDF4.Dataset(nc_file,'r')
cube=iris.load(nc_file)[0]
try: ## KP_Comment: Causes error with data with 1D time dimension added except statement:
cube=cube.collapsed(['time'],iris.analysis.MEAN)
except:
print("Cube time dimension is already collapsed")
print cube.var_name
print cube.shape
try:
if cube.ndim==2:
qplt.contourf(cube,cmap=plt.cm.RdBu_r)
plt.gca().coastlines()
stash=''
try:
stash=ukl.get_stash(cube)
except:
stash='L1'
if isinstance(cube.var_name,str) or isinstance(cube.var_name,unicode):
name=stash+'_'+cube.var_name
else:
name=stash
saving_str=saving_folder+'2D_plot'+name+'.png'
plt.savefig(saving_str)
plt.close()
if cube.ndim==3:
try:
stash=ukl.get_stash(cube)
except:
stash='L1'
if isinstance(cube.var_name,str) or isinstance(cube.var_name,unicode):
name=stash+'_'+cube.var_name
else:
name=stash
ukl.zonal_mean_plot(cube,saving_folder,name)
ukl.zonal_mean_plot(cube,saving_folder,name,logscale=True)
ukl.level_plot(cube,saving_folder,name)
ukl.level_plot(cube,saving_folder,name,logscale=True)
ukl.level_plot(cube,saving_folder,name,logscale=True,level=22)#around 600hpa
ukl.level_plot(cube,saving_folder,name,logscale=False,level=22)#around 600hpa
except:
print cube
print 'not managed to plot automatically. Check wether if there is any other dimension apart from [time, level, lat, lon]'
#