-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysisData.py
159 lines (132 loc) · 5.67 KB
/
analysisData.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
# Global mean Sea Surface Temperatures
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
import pandas as pd
from matplotlib.dates import DateFormatter
import os
from dask.distributed import Client
from dask import delayed
import dask
# Settings
year1=1982
year2=2024
# Settings compute de climatoloy
yearC1='1982'
yearC2='1992'
## Inicio
HOME=os.environ['HOME']
f = open(HOME+'/.env', 'r')
for line in f.readlines():
Name=line.strip().split('=')[0]
Content=line.strip().split('=')[-1]
if Name=='dirData' or Name=='dirAnalisis':
exec(Name + "=" + "'" + Content + "'")
f.close()
Titulos = ['Oceano Global','Hemisferio norte','Hemisferio sur','AtlanticoNorte']
Titulos_short = ['GO','NH','SH','NAtl']
# Load data
if os.uname().nodename.lower().find('eemmmbp') != -1:
base_file = dirData + '/Satelite/noaa.oisst.v2.highres/NC/sst.day.mean'
dataDir = dirAnalisis + '/SSTGlobalAnalysis/data'
elif os.uname().nodename.lower().find('sagams') != -1:
base_file = dirData + '/Satelite/noaa.oisst.v2.highres/NC/sst.day.mean'
dataDir = dirAnalisis + '/SSTGlobalAnalysis/data'
elif os.uname().nodename.lower().find('rossby') != -1:
base_file = dirData + '/Satelite/noaa.oisst.v2.highres/NC/sst.day.mean'
dataDir = dirAnalisis + '/SSTGlobalAnalysis/data'
print('>>>>> Cargando ficheros de '+base_file)
files = [f'{base_file}.{year}.nc' for year in range(year1, year2+1)]
DS = xr.open_mfdataset(files)
for i in range(0,len(Titulos)):
titulo = Titulos[i]
titulo_short = Titulos_short[i]
if titulo_short == 'NH':
sst = DS.sst.sel(lat=slice(0,65))
print('>>>>> '+titulo)
elif titulo_short == 'SH':
sst = DS.sst.sel(lat=slice( -65, 0))
print('>>>>> '+titulo)
elif titulo_short == 'GO':
sst = DS.sst.sel(lat=slice( -65, 65))
print('>>>>> '+titulo)
elif titulo_short == 'NAtl':
sst = DS.sst.sel(lat=slice(0, 65))
basins = xr.open_dataset(dataDir+'/basins.nc')
basin_surf = basins.basin[0]
basin_surf_interp = basin_surf.interp_like(sst, method='nearest')
sst = sst.where((basin_surf_interp==1) | (basin_surf_interp==4) ,drop=True)
print('>>>>> '+titulo)
elif titulo_short == 'LEB':
sst = DS.sst.sel(lat=slice(35.5,42.75)).sel(lon=slice(0,8))
print('>>>>> '+titulo)
elif titulo_short == 'NOR':
sst = DS.sst.sel(lat=slice(41.8,46.5)).sel(lon=slice(347.5,359.5))
print('>>>>> '+titulo)
elif titulo_short == 'CAN':
sst = DS.sst.sel(lat=slice(24.3,32.5)).sel(lon=slice(338,350))
print('>>>>> '+titulo)
elif titulo_short == 'SUD':
sst = DS.sst.sel(lat=slice(35.5,37.4)).sel(lon=slice(352,354))
print('>>>>> '+titulo)
elif titulo_short == 'ESA':
sst = DS.sst.sel(lat=slice(35.5,37)).sel(lon=slice(354,359))
print('>>>>> '+titulo)
# Daily analisis
print('>>>>> Daily'+titulo+titulo_short)
## Calculate global mean weigthtened
print(' > Compute weigthtened mean')
weights = np.cos(np.deg2rad(sst.lat))
weights = weights/weights.max()
weights.name = "weights"
sst_weighted = sst.weighted(weights)
sst_wmean = sst_weighted.mean(("lon", "lat"),skipna=True).load()
## Create monthly climatology
print(' > create climatology')
sst_clim = sst.sel(time=slice(yearC1,yearC2)).groupby('time.dayofyear').mean(dim='time').load();
## Create anomaly
sst_anom = sst.groupby('time.dayofyear') - sst_clim
## Calculate global mean anomaly
print(' > Compute anomaly mean')
weights = np.cos(np.deg2rad(sst.lat))
weights = weights/weights.max()
weights.name = "weights"
sst_anom_weighted = sst_anom.weighted(weights)
sst_anom_wmean = sst_anom_weighted.mean(("lon", "lat"),skipna=True).load()
## Save in netcdf
print(' > Save to netcdf')
sst_wmean.to_netcdf(dataDir+'/sstd_mean_'+titulo_short+'.nc',mode='w')
sst_anom_wmean.to_netcdf(dataDir+'/sstd_anom_mean_'+titulo_short+'.nc',mode='w')
if titulo_short=='GO' or titulo_short=='NAtl' or titulo_short=='LEB' or titulo_short=='CAN' or titulo_short=='NOR' or titulo_short=='SUD' or titulo_short=='ESA':
sst_anom_LD=sst_anom[-1,:,:]
sst_anom_LD.to_netcdf(dataDir+'/sstLD_anom_'+titulo_short+'.nc',mode='w')
# Monthly analisis
print('>>>>> Monthly'+titulo+titulo_short)
sst = sst.resample(time='ME').mean(dim='time',skipna=True).load()
## Calculate global mean weigthtened
print(' > Compute weigthtened mean')
weights = np.cos(np.deg2rad(sst.lat))
weights = weights/weights.max()
weights.name = "weights"
sst_weighted = sst.weighted(weights)
sst_wmean = sst_weighted.mean(("lon", "lat"),skipna=True).load()
## Create monthly climatology
print(' > create climatology')
sst_clim = sst.sel(time=slice(yearC1,yearC2)).groupby('time.month').mean(dim='time').load();
## Create anomaly
print(' > Compute anomaly mean')
sst_anom = sst.groupby('time.month') - sst_clim
sst_anom.load();
##Calculate global mean weigthtened
print(' > Compute weigthtened mean')
weights = np.cos(np.deg2rad(sst.lat))
weights = weights/weights.max()
weights.name = "weights"
sst_anom_weighted = sst_anom.weighted(weights)
sst_anom_wmean = sst_anom_weighted.mean(("lon", "lat"),skipna=True).load()
sst_anom_wmean_rolling = sst_anom_wmean.rolling(time=12,center=True).mean()
##Save in netcdf
print(' > to netcdf')
sst_anom.to_netcdf(dataDir+'/sstm_anom_'+titulo_short+'.nc',mode='w')
sst_wmean.to_netcdf(dataDir+'/sstm_mean_'+titulo_short+'.nc',mode='w')
sst_anom_wmean.to_netcdf(dataDir+'/sstm_anom_mean_'+titulo_short+'.nc',mode='w')