forked from AZMP-NL/python-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xarray_explore_map.py
176 lines (127 loc) · 4.56 KB
/
xarray_explore_map.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
'''
This is a test with xarray, see if I can manipulate a lot of netCDF files.
Try this script in /home/cyrf0006/research/AZMP_database
'''
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import xarray as xr
import datetime
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from scipy.interpolate import griddata # here should remove nans or empty profiles
import netCDF4
# This is a dataset
ds = xr.open_mfdataset('*.nc')
# Select a depth range
## ds = ds.sel(level=ds['level']<500)
## ds = ds.sel(level=ds['level']>10)
## # Selection of a subset region
#ds = ds.where((ds.longitude>-58) & (ds.longitude<-46), drop=True)
## #ds = ds.where((ds.latitude>50) & (ds.latitude<55), drop=True)
#ds = ds.where((ds.latitude>42) & (ds.latitude<56), drop=True)
# Select only one year
ds = ds.sel(time=ds['time.year']>1984)
ds = ds.sel(time=ds['time.year']>1999)
# Select only summer
ds = ds.sel(time=ds['time.season']=='JJA')
## --- Map temperature --- ##
# Temperature to pandas
da_temp = ds.temperature
df_temp = da_temp.to_pandas()
# Find depth range to averae
idx = np.squeeze(np.where((df_temp.columns>=50) & (df_temp.columns<=150)))
#idx = np.squeeze(np.where((df_temp.columns>=10) & (df_temp.columns<=50)))
# All 3 arrays
temp = np.array(df_temp[df_temp.columns[idx]].mean(axis=1))
lons = np.array(ds.longitude)
lats = np.array(ds.latitude)
# Meshgrid 1D data (after removing NaNs)
idx = np.argwhere(~np.isnan(temp))
x = np.arange(np.min(lons[idx]), np.max(lons[idx]), .1)
y = np.arange(np.min(lats[idx]), np.max(lats[idx]), .1)
lon, lat = np.meshgrid(x,y)
# griddata
LN = np.squeeze(lons[idx])
LT = np.squeeze(lats[idx])
TT = np.squeeze(temp[idx])
temp_itp = griddata((LN, LT), TT, (lon, lat), method='linear')
## ---- Bathymetry ---- ####
dataFile = '/home/cyrf0006/Data/GEBCO/GRIDONE_1D.nc'
decim_scale = 4
v = np.linspace(0, 4000, 9)
# Load data
dataset = netCDF4.Dataset(dataFile)
# Extract variables
x = dataset.variables['x_range']
y = dataset.variables['y_range']
spacing = dataset.variables['spacing']
# Compute Lat/Lon
nx = int((x[-1]-x[0])/spacing[0]) + 1 # num pts in x-dir
ny = int((y[-1]-y[0])/spacing[1]) + 1 # num pts in y-dir
lonz = np.linspace(x[0],x[-1],nx)
latz = np.linspace(y[0],y[-1],ny)
# Reshape data
zz = dataset.variables['z']
Z = zz[:].reshape(ny, nx)
# Reduce data according to Region params
lonz = lonz[::decim_scale]
latz = latz[::decim_scale]
Z = Z[::decim_scale, ::decim_scale]
## ---- Plot ---- ##
lon_0 = lons.mean()
lat_0 = lats.mean()
lon_0 = -50
lat_0 = 50
#lonLims = [-58, -46]
#latLims = [42, 56]
lonLims = [-70, -40]
latLims = [40, 65]
proj = 'merc'
m = Basemap(projection='merc',lon_0=lon_0,lat_0=lat_0, llcrnrlon=lonLims[0],llcrnrlat=latLims[0],urcrnrlon=lonLims[1],urcrnrlat=latLims[1], resolution='l')
x,y = m(*np.meshgrid(lonz,latz))
c = m.contour(x, y, np.flipud(-Z), v, colors='darkgrey');
#c = m.contourf(x, y, np.flipud(Z), v, cmap=plt.cm.PuRd_r, extend="min");
m.fillcontinents(color='grey');
#v = np.arange(np.floor(np.min(tmax)), np.ceil(np.max(tmax))+1)
v = 20
xi, yi = m(lon, lat)
lon_casts, lat_casts = m(lons[idx], lats[idx])
cs = m.contourf(xi,yi,temp_itp, v, cmap=plt.cm.RdBu_r)
m.fillcontinents(color='grey');
# Add Colorbar
cbar = m.colorbar(cs, location='bottom')
#cbar.set_label(tmax_units)
# Add casts identification
x, y = m(lons, lats)
m.scatter(x,y,10,marker='o',color='k', alpha=.5)
#m.plot(x, y, '.k')
# Add Grid Lines
m.drawparallels(np.arange(latLims[0], latLims[1], 1.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(lonLims[0], lonLims[1], 1.), labels=[0,0,0,1], fontsize=10)
# Add Coastlines, States, and Country Boundaries
m.drawcoastlines()
m.drawstates()
m.drawcountries()
# Add Title
plt.title('Temperature')
plt.show()
## BELOW IS WORK IN PROGRESS
plt.contourf(df_temp.index, df_temp.columns, df_temp.T, 20)
# HERE!!
# this is now a dataArray
da_temp = ds['temperature']
da_lat = ds['latitude']
da_lon = ds['longitude']
# Averrage profile of the whole timeseries
A = da_temp.mean(dim='time')
# Selection of only summer data
summer_temp = temp.sel(time=temp['time.season']=='JJA')
summer_lat = lat.sel(time=lat['time.season']=='JJA')
summer_lon = lon.sel(time=lon['time.season']=='JJA')
ds = xr.Dataset()
ds['data1'] = xr.DataArray(np.arange(100), coords={'t1': np.linspace(0, 1, 100)})
ds['data1b'] = xr.DataArray(np.arange(100, 200), coords={'t1': np.linspace(0, 1, 100)})
ds['data2'] = xr.DataArray(np.arange(200, 500), coords={'t2': np.linspace(0, 1, 300)})
ds['data2b'] = xr.DataArray(np.arange(600, 900), coords={'t2': np.linspace(0, 1, 300)})
ds.where(ds.data1 < 50, drop=True)