Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resample DINO data and modify Jumper.ipynb to make it compatible #22

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
286 changes: 125 additions & 161 deletions Notebooks/Jumper.ipynb

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions Notebooks/Resample_ssh.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 66,
"id": "37c00295-e62d-446f-aaa8-6bb4d88fd617",
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr\n",
"import numpy as np\n",
"import cftime\n",
"\n",
"# Load the dataset with time decoding\n",
"ds_monthly = xr.open_dataset('DINO_1m_grid_T.nc', decode_times=True, use_cftime=True)\n",
"\n",
"\n",
"# Resample the dataset to yearly frequency and compute the mean\n",
"ds_yearly = ds_monthly.resample(time_counter='YE').mean()\n",
"\n",
"# Save the resampled dataset to a new NetCDF file\n",
"ds_yearly.to_netcdf('DINO_1m_To_1y_grid_T.nc')"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "0a3d4608-5a7f-428f-b433-2b33da3b9ecc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<xarray.Dataset> Size: 20MB\n",
"Dimensions: (time_counter: 50, y: 199, x: 62)\n",
"Coordinates:\n",
" nav_lat (y, x) float32 49kB ...\n",
" nav_lon (y, x) float32 49kB ...\n",
" * time_counter (time_counter) int64 400B 0 360 720 1080 ... 16920 17280 17640\n",
"Dimensions without coordinates: y, x\n",
"Data variables:\n",
" ssh (time_counter, y, x) float32 2MB ...\n",
" sst (time_counter, y, x) float32 2MB ...\n",
" sss (time_counter, y, x) float32 2MB ...\n",
" saltflx (time_counter, y, x) float32 2MB ...\n",
" qns (time_counter, y, x) float32 2MB ...\n",
" qsr (time_counter, y, x) float32 2MB ...\n",
" mldr10_1 (time_counter, y, x) float32 2MB ...\n",
" mldr10_1max (time_counter, y, x) float32 2MB ...\n",
"Attributes:\n",
" name: DINO_1m_grid_T\n",
" description: ocean T grid variables monthly\n",
" title: ocean T grid variables monthly\n",
" Conventions: CF-1.6\n",
" timeStamp: 2024-Nov-02 12:56:48 GMT\n",
" uuid: 4580aece-1ad3-4429-9fa2-2417c992e848\n",
"\n",
"---------------\n",
"\n",
"<xarray.DataArray 'time_counter' (time_counter: 50)> Size: 400B\n",
"array([ 0, 360, 720, 1080, 1440, 1800, 2160, 2520, 2880, 3240,\n",
" 3600, 3960, 4320, 4680, 5040, 5400, 5760, 6120, 6480, 6840,\n",
" 7200, 7560, 7920, 8280, 8640, 9000, 9360, 9720, 10080, 10440,\n",
" 10800, 11160, 11520, 11880, 12240, 12600, 12960, 13320, 13680, 14040,\n",
" 14400, 14760, 15120, 15480, 15840, 16200, 16560, 16920, 17280, 17640])\n",
"Coordinates:\n",
" * time_counter (time_counter) int64 400B 0 360 720 1080 ... 16920 17280 17640\n",
"Attributes:\n",
" axis: T\n",
" standard_name: time\n",
" long_name: Time axis\n",
" time_origin: 1900-01-01 00:00:00\n",
" bounds: time_counter_bounds\n",
" units: days since 0001-12-30 00:00:00.000000\n",
" calendar: 360_day\n"
]
}
],
"source": [
"ds_yearly_from_file = xr.open_dataset('DINO_1m_To_1y_grid_T.nc', decode_times=False)\n",
"\n",
"print(ds_yearly_from_file)\n",
"print('\\n---------------\\n')\n",
"print(ds_yearly_from_file['time_counter'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4df47882-f2c2-47aa-b165-da157f498a85",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:netcdf]",
"language": "python",
"name": "conda-env-netcdf-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
13 changes: 7 additions & 6 deletions lib/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def getData(path,term):
"""
grid = []
for file in sorted(os.listdir(path)):
if term+"." in file: #add param!=""
if term[1] in file: #add param!=""
grid.append(path+"/"+file)
return grid

Expand All @@ -126,10 +126,10 @@ def getAttributes(self):
self.time_dim = list(array.dims)[0]
self.y_size = array.sizes['y']
self.x_size = array.sizes['x']
if "deptht" in array[self.term].dims:
if "deptht" in array[self.term[0]].dims:
self.z_size = array.sizes['deptht']
self.shape = (self.z_size,self.y_size,self.x_size)
elif "olevel" in array[self.term].dims:
elif "olevel" in array[self.term[0]].dims:
self.z_size = array.sizes['olevel']
self.shape = (self.z_size,self.y_size,self.x_size)
else:
Expand Down Expand Up @@ -162,7 +162,7 @@ def loadFile(self,file):
(xarray.DataArray) : The loaded simulation data.
"""
array = xr.open_dataset(file, decode_times=False,chunks={"time": 200, "x":120})
array = array[self.term]
array = array[self.term[0]]
self.len = self.len + array.sizes[self.time_dim]
#print(self.len)
#if self.ye:
Expand Down Expand Up @@ -206,7 +206,7 @@ def getSSCA(self,array):
Parameters:
array (xarray.Dataset): The last dataset containing simulation data in the simulation file.
"""
array = array[self.term].values
array = array[self.term[0]].values
n = np.shape(array)[0]//12 *12
array = array[-n:]
ssca = np.array(array).reshape((n//12, 12)+ self.shape) #np.array(array[self.term])
Expand Down Expand Up @@ -260,7 +260,8 @@ def getPC(self,n):
Returns:
(numpy.ndarray): The principal component map.
"""
map_ = np.zeros((np.product(self.shape)), dtype=float)
# map_ = np.zeros((np.product(self.shape)), dtype=float)
map_ = np.zeros((np.prod(self.shape)), dtype=float)
map_[~self.bool_mask] = np.nan
map_[self.bool_mask] = self.pca.components_[n]
map_ = map_.reshape(self.shape)
Expand Down
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
numpy==2.0.2
pandas==2.2.2
python-dateutil==2.9.0.post0
pytz==2024.1
six==1.16.0
matplotlib==3.9.2
scipy==1.13.1
xarray[complete]==2024.7.0
scikit-learn==1.5.1