-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregcm_precipitation_plotting.py
221 lines (156 loc) · 5.89 KB
/
regcm_precipitation_plotting.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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 9 23:46:12 2024
@author: Ece
"""
# let's dive into regcm dataset
import xarray as xr
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.dates as mdates
# Load your NetCDF file
file_path = 'C:/Users/Downloads/bozkurt_ATM.1990060100.nc'#'path_to_your_regcm5_output.nc'
data = xr.open_dataset(file_path)
# Inspect the dataset
print(data)
print(data.head)
data_frame = data.to_dataframe();
for var in data.data_vars:
print(f"{var}: {data[var].attrs.get('units', 'No units attribute')}")
file_path2 = "C:/Users/OneDrive/Desktop/bozkurt_nonhydro_ATM.1990060100.nc"
data2 = xr.open_dataset(file_path2)
# Inspect the dataset
print(data2)
print(data2.head)
data_frame2 = data2.to_dataframe();
for var in data2.data_vars:
print(f"{var}: {data2[var].attrs.get('units', 'No units attribute')}")
file_path3 = 'C:/Users/Downloads/bozkurt_hydro_ATM.1990060100.nc'
data3 = xr.open_dataset(file_path3)
# Inspect the dataset
print(data3)
print(data3.head)
data_frame3 = data3.to_dataframe();
for var in data3.data_vars:
print(f"{var}: {data3[var].attrs.get('units', 'No units attribute')}")
file_path4 = 'C:/Users/Downloads/bozkurt_SRF.1990060100.nc'
data4 = xr.open_dataset(file_path4)
# Inspect the dataset
print(data4)
print(data4.head)
data_frame4 = data4.to_dataframe();
for var in data4.data_vars:
print(f"{var}: {data4[var].attrs.get('units', 'No units attribute')}")
pr_data = data4[['pr']]
df_prData = pr_data.to_dataframe()
pr_multiplied = (data4['pr'] /1000) * 3600
# You can create a new xarray or add the modified variable back to the original dataset
data4['pr_modified'] = pr_multiplied
# Plot the data (e.g., using the first time slice or averaging over time)
pr_multiplied.isel(time=0).plot()
# Add labels and title
plt.title('Precipitation (Modified)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
# Show the plot
plt.show()
precipitation = data4['pr_modified'].mean(dim=['iy', 'jx']) # Replace with your variable name
plt.plot(precipitation['time'], precipitation, label='Precipitation')
# Add labels and title
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Temperature and Precipitation over Time')
plt.legend()
plt.show()
file_path5 = 'C:/Users/Downloads/bozkurt_STS.1990060100.nc'
data5 = xr.open_dataset(file_path5)
# Inspect the dataset
print(data5)
print(data5.head)
data_frame5 = data5.to_dataframe();
for var in data5.data_vars:
print(f"{var}: {data5[var].attrs.get('units', 'No units attribute')}")
pr_multiplied = (data5['pr'] /1000) * 3600
# You can create a new xarray or add the modified variable back to the original dataset
data5['pr_modified'] = pr_multiplied
precipitation = data5['pr_modified'].mean(dim=['iy', 'jx']) # Replace with your variable name
plt.plot(precipitation['time'], precipitation, label='Precipitation')
# Add labels and title
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Temperature and Precipitation over Time')
plt.legend()
plt.show()
file_path6 = 'C:/Users/Downloads/bozkurt_SAV.1990060600.nc'
data6 = xr.open_dataset(file_path6)
# Inspect the dataset
print(data6)
print(data6.head)
data_frame6 = data6.to_dataframe();
for var in data6.data_vars:
print(f"{var}: {data6[var].attrs.get('units', 'No units attribute')}")
pr_multiplied = (data5['pr'] /1000) * 3600
# You can create a new xarray or add the modified variable back to the original dataset
data5['pr_modified'] = pr_multiplied
precipitation = data5['pr_modified'].mean(dim=['iy', 'jx']) # Replace with your variable name
plt.plot(precipitation['time'], precipitation, label='Precipitation')
# Add labels and title
plt.xlabel('Time')
plt.ylabel('Value')
plt.title('Temperature and Precipitation over Time')
plt.legend()
plt.show()
file_path7 = 'C:/Users/Downloads/2021/bozkurt_nonhydro_SRF.2021080100.nc'
data7 = xr.open_dataset(file_path7)
# Inspect the dataset
print(data7)
print(data7.head)
data_frame7 = data7.to_dataframe();
for var in data7.data_vars:
print(f"{var}: {data7[var].attrs.get('units', 'No units attribute')}")
pr_multiplied = (data7['pr']) * 3600
# You can create a new xarray or add the modified variable back to the original dataset
data7['pr_modified'] = pr_multiplied
precipitation = data7['pr_modified'].mean(dim=['iy', 'jx']) # Replace with your variable name
plt.plot(precipitation['time'], precipitation, label='Precipitation')
# Add labels and title
plt.xlabel('Time')
plt.ylabel('Value')
plt.title(' Precipitation over Time')
plt.xticks(rotation=45) # Rotate x-axis labels by 45 degrees
plt.legend()
plt.show()
print(data7['time'])
print(data7['time'].values)
# Check if 'time' is a datetime object
if not isinstance(precipitation['time'].values[0], pd.Timestamp):
precipitation['time'] = pd.to_datetime(precipitation['time'].values)
plt.figure(figsize=(24, 12)) # Adjust figure size
plt.plot(precipitation['time'], precipitation, label='Precipitation')
# Set major ticks every 6 hours
plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=6))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
# Rotate the labels for better readability
plt.xticks(rotation=45)
# Add labels and title
plt.xlabel('Time')
plt.ylabel('Precipitation (mm)')
plt.title('Precipitation Over Time')
plt.legend()
plt.tight_layout() # Prevent clipping
plt.show()
precipitation_real = data7['pr'].mean(dim=['iy', 'jx']) # Replace with your variable name
plt.figure(figsize=(24, 12)) # Adjust figure size
plt.plot(precipitation_real['time'], precipitation_real, label='Precipitation')
# Set major ticks every 6 hours
plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=6))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
# Rotate the labels for better readability
plt.xticks(rotation=45)
# Add labels and title
plt.xlabel('Time')
plt.ylabel('Precipitation (mm)')
plt.title('Precipitation Over Time')
plt.legend()
plt.tight_layout() # Prevent clipping
plt.show()