-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclouds_calipso.py
243 lines (190 loc) · 9.04 KB
/
clouds_calipso.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import xarray as xr
import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
import matplotlib.path as mpath
import cartopy.crs as ccrs
import cartopy.feature
import matplotlib.gridspec as gridspec
import xesmf as xe
import cartopy.feature as feat
# =========== LOAD ERA5, MAR_noBS, MAR_BS, Cloudsat =======================
def preprocess(ds):
data = ds.sel(lat=slice(-90, -40))
return data
file_str = '/projects/NS9600K/shofer/blowing_snow/MAR/new/3D_monthly/'
file_str_nobs = '/projects/NS9600K/shofer/blowing_snow/MAR/3D_monthly_nDR/'
file_str_ERA = '/projects/NS9600K/shofer/blowing_snow/MAR/ERA5/SINGLELEVS/'
file_str_calipso = '/projects/NS9600K/shofer/blowing_snow/sat_data/cloudsat/'
file_str_zz = '/projects/NS9600K/shofer/blowing_snow/MAR/case_study_BS_2009/'
# Cloud cover years: 2006/07 to 2011/02
year_s = '2006-07-01'
year_e = '2011-02-18'
# Open the no driftig snow file
ds_bs_CC = (xr.open_dataset(
file_str + 'mon-CC-MAR_ERA5-1979-2019.nc').sel(TIME=slice(year_s, year_e)).rename(
{'X': 'x', 'Y': 'y'}))
ds_nobs_CC = (xr.open_dataset(
file_str_nobs + 'mon-CC-MAR_ERA5-1979-2019.nc').sel(TIME=slice(year_s, year_e)).rename(
{'X': 'x', 'Y': 'y'}))
# .isel(x=slice(10, -10), y=slice(10, -10))
data_ERA = xr.open_mfdataset(
file_str_ERA + 'ERA5_*.nc', combine='by_coords')
data_ERA = data_ERA.rename({'latitude': 'lat', 'longitude': 'lon'}).sel(
time=slice(year_s, year_e))
cloud_data_ERA = data_ERA.tcc
ds_cloudsat = xr.open_dataset(
file_str_calipso + 'cf_2deg_cloudsat_calipso_total_annual.nc')
# ==========================================================================
# CREATE the ICE MASK
# =========================================================
MAR_grid = xr.open_dataset(
file_str_zz + 'MARcst-AN35km-176x148.cdf')
ds_grid = xr.Dataset({'RIGNOT': (['y', 'x'], MAR_grid.RIGNOT.values),
'SH': (['y', 'x'], MAR_grid.SH.values),
'LAT': (['y', 'x'], MAR_grid.LAT.values),
'LON': (['y', 'x'], MAR_grid.LON.values),
'ICE': (['y', 'x'], MAR_grid.ICE.values),
'AIS': (['y', 'x'], MAR_grid.AIS.values),
'GROUND': (['y', 'x'], MAR_grid.GROUND.values),
'AREA': (['y', 'x'], MAR_grid.AREA.values),
'SOL': (['y', 'x'], MAR_grid.SOL.values),
'ROCK': (['y', 'x'], MAR_grid.ROCK.values)},
coords={'x': (['x'], ds_nobs_CC.x * 1000),
'y': (['y'], ds_nobs_CC.y * 1000)})
ais = ds_grid['AIS'].where(ds_grid)['AIS'] > 0 # Only AIS=1, other islands =0
# Ice where ICE mask >= 30% (ICE[0-100%], dividing by 100 in the next ligne)
ice = ds_grid['ICE'].where(ds_grid['ICE'] > 30)
# Combine ais + ice/100 * factor area for taking into account the projection
ice_msk = (ais * ice * ds_grid['AREA'] / 100)
grd = ds_grid['GROUND'].where(ds_grid['GROUND'] > 30)
grd_msk = (ais * grd * ds_grid['AREA'] / 100)
lsm = (ds_grid['AIS'] < 1)
ground = (ds_grid['GROUND'] * ds_grid['AIS'] > 30)
shf = (ds_grid['ICE'] / ds_grid['ICE']).where((ds_grid['ICE'] > 30) &
(ds_grid['GROUND'] < 50) & (ds_grid['ROCK'] < 30) & (ais > 0))
shelf = (shf > 0)
x2D, y2D = np.meshgrid(ds_grid['x'], ds_grid['y'])
sh = ds_grid['SH']
dh = (ds_grid['x'].values[0] - ds_grid['x'].values[1]) / 2.
msk = ds_grid['SOL'].where(ds_grid['SOL'] == 4)
# =========== COMPUTE THE MEAN OVER THE SAME TIME PERIOD ==================
BS = ds_bs_CC.mean(dim='TIME')
BS['x'] = BS.x * 1000
BS['y'] = BS.y * 1000
# Add LAT LON to MAR data
BS['lat'] = ds_grid.LAT
BS['lon'] = ds_grid.LON
BS['RIGNOT'] = ds_grid.RIGNOT.where(ds_grid.RIGNOT > 0)
BS['shelf'] = shelf
BS['ground'] = ground
BS['msk'] = msk
BS['SOL'] = ds_grid.SOL
NOBS = ds_nobs_CC.mean(dim='TIME')
NOBS['x'] = BS.x * 1000
NOBS['y'] = BS.y * 1000
NOBS['lat'] = ds_grid.LAT
NOBS['lon'] = ds_grid.LON
NOBS['RIGNOT'] = ds_grid.RIGNOT.where(ds_grid.RIGNOT > 0)
NOBS['shelf'] = shelf
NOBS['ground'] = ground
NOBS['msk'] = msk
NOBS['SOL'] = ds_grid.SOL
ERA = cloud_data_ERA.mean(dim='time')
cloudsat = ds_cloudsat.cf
# =========== REGRID TO THE SAME GRID =====================================
# This creates the output grid, atm I think can be done with any variable
# as long as lat lon grid is present
ds_out = xe.util.grid_global(2, 2).isel(y=slice(0, 15))
# Can be any MAR input grid as long as lat lon is present (rename!)
# REGRID ERA
ds_in = ERA
regridder_ERA = xe.Regridder(ds_in, ds_out, 'bilinear')
regrid_ERA = regridder_ERA(ERA)
# MAR
regridder_BS = xe.Regridder(BS, ds_out, 'bilinear')
regrid_BS = regridder_BS(BS)
regrid_NOBS = regridder_BS(NOBS)
# CLOUDSAT
ds_in = cloudsat
regridder_cloudsat = xe.Regridder(ds_in, ds_out, 'bilinear')
regrid_cloudsat = regridder_cloudsat(cloudsat.transpose())
# ========== COMPUTE THE DIFFERENCES BETWEEN CLOUDSAT and ERA,MAR, MARBS ==
diff_test = (regrid_BS.CC - regrid_cloudsat).where((regrid_BS.shelf > 0)
| (regrid_BS.ground > 0))
diff_test_nobs = (regrid_NOBS.CC - regrid_cloudsat).where((regrid_BS.shelf > 0)
| (regrid_BS.ground > 0))
diff_test_ERA = (regrid_ERA - regrid_cloudsat).where((regrid_BS.shelf > 0)
| (regrid_BS.ground > 0))
print('NoBS mean: {:.2f}, NOBS STD: {:.2f}'.format(
diff_test_nobs.mean().values * 100, diff_test_nobs.std().values * 100))
print('BS mean: {:.2f}, BS STD: {:.2f}'.format(
diff_test.mean().values * 100, diff_test.std().values * 100))
print('ERA mean: {:.2f}, ERA STD: {:.2f}'.format(
diff_test_ERA.mean().values * 100, diff_test_ERA.std().values * 100))
# ============ PLOTTING ROUTINE ========================
# Plotting routines
plt.close('all')
proj = ccrs.SouthPolarStereo()
fig = plt.figure(figsize=(14, 7))
spec2 = gridspec.GridSpec(ncols=3, nrows=1, figure=fig)
ax1 = fig.add_subplot(spec2[0, 0], projection=proj)
ax2 = fig.add_subplot(spec2[0, 1], projection=proj)
# plt.setp(ax2.get_yticklabels(), visible=False)
ax3 = fig.add_subplot(spec2[0, 2], projection=proj)
# PLOT EVERYTHING
ax = [ax1, ax2, ax3]
names = ['No Blowing Snow', 'Blowing Snow', 'ERA5']
for i in range(3):
# Limit the map to -60 degrees latitude and below.
ax[i].set_extent([-180, 180, -90, -60], ccrs.PlateCarree())
ax[i].add_feature(feat.LAND)
# ax[i].add_feature(feat.OCEAN)
ax[i].gridlines()
# Compute a circle in axes coordinates, which we can use as a boundary
# for the map. We can pan/zoom as much as we like - the boundary will be
# permanently circular.
theta = np.linspace(0, 2 * np.pi, 100)
center, radius = [0.5, 0.5], 0.5
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)
ax[i].set_boundary(circle, transform=ax[i].transAxes)
cmap = 'YlGnBu_r'
cont = ax[0].pcolormesh(diff_test_nobs['lon'], diff_test_nobs['lat'],
diff_test_nobs * 100,
transform=ccrs.PlateCarree(), vmin=-100, vmax=100, cmap='RdBu_r')
cont2 = ax[1].pcolormesh(diff_test['lon'], diff_test['lat'],
diff_test * 100,
transform=ccrs.PlateCarree(), vmin=-100, vmax=100, cmap='RdBu_r')
cont3 = ax[2].pcolormesh(diff_test_ERA['lon'], diff_test_ERA['lat'],
diff_test_ERA * 100, transform=ccrs.PlateCarree(), vmin=-100, vmax=100, cmap='RdBu_r')
# cont2 = ax[1].pcolormesh(trend_CC['lon'], trend_CC['lat'],
# trend_CC.slope*30, transform=ccrs.PlateCarree(), vmin=-15, vmax=15, cmap='RdBu_r')
# xr.plot.contour(ground, levels=1, colors='black', linewidths=0.2)
# xr.plot.contour('x','y',ds_grid.SOL, levels=1, colors='black', linewidths=0.2, ax=ax[1], transform=proj)
letters = ['A', 'B', 'C']
for i in range(3):
xr.plot.contour(ds_grid.SOL, levels=1, colors='black',
linewidths=0.4, transform=proj, ax=ax[i])
xr.plot.contour(ground, levels=1, colors='black', linewidths=0.4, ax=ax[i])
# ax[i].add_feature(feat.COASTLINE.with_scale(
# '50m'), zorder=1, edgecolor='black')
ax[i].set_title(names[i], fontsize=16)
ax[i].text(0.04, 1.02, letters[i], fontsize=22, va='center', ha='center',
transform=ax[i].transAxes, fontdict={'weight': 'bold'})
# fig.canvas.draw()
fig.tight_layout()
cb = fig.colorbar(cont3, ax=ax, ticks=list(
np.arange(-100, 120, 20)), shrink=0.85, orientation='horizontal')
cb.set_label('Cloud Cover Difference (%)', fontsize=16)
cb.ax.tick_params(labelsize=11)
plt.subplots_adjust(left=0.02, right=0.98, top=0.99, bottom=0.24)
# fig.colorbar(cont2, ax=ax[1], ticks=list(
# np.arange(-15, 15.5, 3)), shrink=0.8)
# cbar = fig.colorbar(cont, ax=ax, ticks=[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
# orientation = 'horizontal', fraction = 0.13, pad = 0.01, shrink = 0.8)
# cbar.set_label(
# 'Average DJF cloud cover 2002-2015 (%)', fontsize=18)
fig.savefig('/projects/NS9600K/shofer/blowing_snow/Calipso_difference_new.png',
format='PNG', dpi=300)