-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtype.py
More file actions
169 lines (130 loc) · 5.24 KB
/
Copy pathtype.py
File metadata and controls
169 lines (130 loc) · 5.24 KB
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
import matplotlib.pyplot as plt
from data_visualization import dtu_coolwarm_cmap, dtu_grey, dtu_blues_cmap,dtu_reds_cmap
from dictionaries import CIMR
from algorhitms import bristol,bristol_CIMR
from skimage.transform import downscale_local_mean
from footprint_operator import resample
import pandas as pd
import xarray as xr
from dictionaries import MWI,OW_tiepoints
import numpy as np
import matplotlib.pyplot as plt
from data_visualization import dtu_coolwarm_cmap, dtu_grey, dtu_blues_cmap,dtu_reds_cmap
from dictionaries import CIMR
from algorhitms import bristol,bristol_CIMR
from skimage.transform import downscale_local_mean
from footprint_operator import resample
import pandas as pd
import xarray as xr
from dictionaries import MWI,OW_tiepoints
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.colors import Normalize
# Load datasets
ds_314H = xr.open_dataset(r"C:\Users\user\OneDrive\Desktop\Bachelor\CSV\convolved_36.5_H_CIMR.nc")
ds_314V = xr.open_dataset(r"C:\Users\user\OneDrive\Desktop\Bachelor\CSV\convolved_36.5_V_CIMR.nc")
ds_187V = xr.open_dataset(r"C:\Users\user\OneDrive\Desktop\Bachelor\CSV\convolved_18.7_V_CIMR.nc")
print("Footprints loaded...")
# Extract tb arrays
tb37h = ds_314H["tb"].values
tb37v = ds_314V["tb"].values
tb18v = ds_187V["tb"].values
print("Brightness temperatures extracted...")
lat = ds_314H["lat"].values
lon = ds_314H["lon"].values
print("Lat and lon extracted...")
# Compute SIC with Bristol
def compute_gradient_ratio(tb_low, tb_high):
"""
Compute the gradient ratio GR(freq_low, freq_high, V).
Parameters:
- tb_low: Brightness temperature at the lower frequency (e.g., 19 GHz, vertical polarization)
- tb_high: Brightness temperature at the higher frequency (e.g., 37 GHz, vertical polarization)
Returns:
- Gradient Ratio (float)
"""
return (tb_low - tb_high) / (tb_low + tb_high)
GR = compute_gradient_ratio(tb18v, tb37h)
# Acces CICE and merge with OW where SIC =0
CICE = r"C:\Users\user\OneDrive\Desktop\Bachelor\CSV\CICE\CICE"
OW = r"C:\Users\user\OneDrive\Desktop\Bachelor\CSV\CICE\OW_0_SIC.csv"
df_cice = pd.read_csv(CICE)
df_OW = pd.read_csv(OW)
df_cice = df_cice.rename(columns={'TLAT': 'lat', 'TLON': 'lon'})
df_OW = df_OW.rename(columns={'TLAT': 'lat', 'TLON': 'lon'})
#%%
# Flatten arrays
flat_lat = lat.flatten()
flat_lon = lon.flatten()
flat_GR = GR.flatten()
# Create DataFrame
df_sic = pd.DataFrame({'lat': flat_lat,
'lon': flat_lon,
'GR': flat_GR})
df_sic = df_sic.sort_values(by=['lat', 'lon']).reset_index(drop=True)
print(df_sic)
#%%
import numpy as np
from scipy.spatial import cKDTree
def latlon_to_cartesian(lat, lon):
lat_rad = np.radians(lat)
lon_rad = np.radians(lon)
x = np.cos(lat_rad) * np.cos(lon_rad)
y = np.cos(lat_rad) * np.sin(lon_rad)
z = np.sin(lat_rad)
return np.stack([x, y, z], axis=-1)
# Convert coordinates to Cartesian
sic_cart = latlon_to_cartesian(df_sic['lat'].values, df_sic['lon'].values)
cice_cart = latlon_to_cartesian(df_cice['lat'].values, df_cice['lon'].values)
# Build KDTree
tree = cKDTree(sic_cart)
# Query 16 nearest neighbors
distances, indices = tree.query(cice_cart, k=16)
# Compute distance-weighted average
weights = 1 / (distances + 1e-12) # avoid division by zero
weighted_sic = np.average(df_sic['GR'].values[indices], axis=1, weights=weights)
# Assign to df_cice
df_cice['GR'] = weighted_sic
#%%
print(df_cice['GR'].describe())
#%%
def plot_diff(lat, lon, cvalue, colorbar_min, colorbar_max):
fig, ax = plt.subplots(figsize=(10, 10),
subplot_kw={'projection': ccrs.LambertAzimuthalEqualArea(
central_latitude=90, central_longitude=0)})
ax.set_extent([-180, 180, 66.5, 90], crs=ccrs.PlateCarree())
norm = Normalize(vmin=colorbar_min, vmax=colorbar_max)
# Add land with DTU grey
land = cfeature.NaturalEarthFeature('physical', 'land', '50m',
edgecolor='face', facecolor=dtu_grey)
ax.add_feature(land)
ax.coastlines(resolution='50m', linewidth=0.5, color='black')
# Gridlines
# Gridlines
gl = ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)
gl.xlabel_style = {'size': 20}
gl.ylabel_style = {'size': 20}
# Disable the 0° meridian label
gl.top_labels = True
gl.bottom_labels = False
gl.left_labels = True
gl.right_labels = True
# Scatter plot
sc = ax.scatter(lon, lat,
c=cvalue,
cmap=dtu_coolwarm_cmap,
norm=norm,
s=0.1,
transform=ccrs.PlateCarree())
# Colorbar below the plot, exclude 0 and move it closer
cbar = fig.colorbar(sc, ax=ax, orientation='horizontal', pad=0.04, shrink=0.8)
cbar.set_ticks([0.05,0.25])
cbar.set_label('GR [cm]', fontsize=22, labelpad=10)
cbar.ax.tick_params(labelsize=20)
cvalue = df_cice['GR']
lat = df_cice['lat']
lon = df_cice['lon']
colorbar_min = 0.05
colorbar_max = 0.25
plot_diff(lat, lon, cvalue, colorbar_min, colorbar_max)