forked from lexi-jones/RCLVatlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_parcels_CMEMS.py
246 lines (199 loc) · 9.43 KB
/
run_parcels_CMEMS.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Stella Bērziņa
# Date Created: 15/07/21 by lexi
# Last Edited: 17/09/24 by stella
# Run an OceanParcels simulation
## this is the script that looks at config file and calculates lavd from Lexi Jones-Kellet
import time,sys
import numpy as np
import xarray as xr
from glob import glob
from datetime import datetime
from parcels import FieldSet,Variable,JITParticle, Field
from config import *
################# for sure not all of these are needed but i have them lol
from numpy import arange, ones
import matplotlib as mpl
import matplotlib.lines as mlines
from matplotlib.cm import get_cmap
from matplotlib import colors
from matplotlib.colors import ListedColormap
from matplotlib.figure import Figure
from datetime import datetime, timedelta
from netCDF4 import Dataset
import io
import os
import warnings
warnings.filterwarnings("ignore")
import gsw
from xgcm.grid import Grid
# from concurrent.futures import ProcessPoolExecutor
# from concurrent.futures import ThreadPoolExecutor
import dask
from dask_jobqueue import SLURMCluster
from dask.distributed import Client
import cartopy.crs as ccrs
from cartopy.mpl.ticker import (LongitudeFormatter, LatitudeFormatter,LatitudeLocator)
import cartopy.feature as cfeature
import xarray as xr
import numpy as np
import matplotlib.pylab as plt
import matplotlib.cm as cm
from scipy.interpolate import CloughTocher2DInterpolator, LinearNDInterpolator, NearestNDInterpolator
import glob
import intake
from pathlib import Path
import dask
import cmocean.cm as cmo #also for pretty color palettes
import pandas as pd
dask.config.set({"array.slicing.split_large_chunks": True})
import sys
# to access intake catalog of eerie
eerie_cat=intake.open_catalog("https://raw.githubusercontent.com/eerie-project/intake_catalogues/main/eerie.yaml")
data_oce = eerie_cat["dkrz"]["disk"]["model-output"]["icon-esm-er"]["eerie-control-1950"]["v20231106"]["ocean"]["gr025"]["2d_daily_mean"].to_dask()
sys.path.append('./RCLVatlas/')
from functions_for_parcels import *
# this finds the date from what you write in the command line
date_input = sys.argv[1] # user input: particle intialization date
# selects the data you want to look at in time. Spatial selection in the config file
start_year,start_month,start_day = int(str(date_input)[0:4]),int(str(date_input)[4:6]),int(str(date_input)[6:8])
start_date = datetime(start_year,start_month,start_day) # format datetime
print(start_year, start_month)
data_sub=data_oce.sel(time=str(start_year))
data_sub=data_sub[["u", "v"]]
data_sub['v']=data_sub['v'][:,0,:,:]
data_sub['u']=data_sub['u'][:,0,:,:]
################################## this code chunk is from xlcs package and example notebook
# - create grid spacing variable (`dx`, `dy`) with values in meters
# - because new values are not stored at the same location as the velocity we create two new dimensions
# - `longitude`, `latitude`: The variable values are located at the cell center.
# - `longitude_g`, `latitude_g`: The variable values are located on the cell faces, excluding both outer boundaries.
# - See [Simple Grids](https://xgcm.readthedocs.io/en/latest/grids.html) for more details.
# - create the Grid object
ds = data_sub.copy(deep=True)
lon, lat = np.meshgrid(ds.lon, ds.lat)
ds = ds.assign_coords(
{
"dx": xr.DataArray(
gsw.distance(lon, lat, axis=1),
dims=["lat", "longitude_g"],
coords={
"lat": ds.lat.data,
"longitude_g": 0.5 * (ds.lon.data[1:] + ds.lon.data[:-1]),
},
),
"dy": xr.DataArray(
gsw.distance(lon, lat, axis=0),
dims=["latitude_g", "lon"],
coords={
"latitude_g": 0.5 * (ds.lat.data[1:] + ds.lat.data[:-1]),
"lon": ds.lon.data,
},
),
}
)
coords = {
"X": {"center": "lon", "inner": "longitude_g"},
"Y": {"center": "lat", "inner": "latitude_g"},
}
grid = Grid(ds, periodic=[], coords=coords)
variables = {"U": "u", "V": "v"}
dimensions = {
"time": "time",
"lon": "lon",
"lat": "lat",
}
# this fieldset is created to do Parcels integration
fs = FieldSet.from_xarray_dataset(ds, variables, dimensions, mesh="spherical")
print('Fieldset created.')
#### now calculate vorticity
# LAVD requires interpolating the vorticity along the trajectories, so we include it as an extra field to the Parcel's fieldset object `fs`.
# Note: the derivatives are interpolated back to the center of the cells.
vg_x = grid.diff(ds.v, "X", boundary="extend") / ds.dx
ug_y = grid.diff(ds.u, "Y", boundary="extend") / ds.dy
ds["vorticity"] = grid.interp(vg_x, "X", to="center", boundary="extend") - grid.interp(
ug_y, "Y", to="center", boundary="extend"
)
# add the field to the FS object
field1 = Field.from_xarray(ds["vorticity"], "vorticity", dimensions)
fs.add_field(field1)
print("Vorticity added")
################################## end of xlcs package code chunk
# at this point we have not run the parcels. We have just created fieldset with velocity and vorticity
################################## RCLVatlas original code
### Create particleset ###
class SpinnyParticle(JITParticle):
u = Variable('u',dtype=np.float64)
v = Variable('v',dtype=np.float64)
vort = Variable('vort',dtype=np.float64)
pset_dynamic,num_particles = particle_grid2d(fs,SpinnyParticle,
[grid_bounds['lat_bound_south'],grid_bounds['lat_bound_north'],grid_bounds['lag_grid_res']],
[grid_bounds['lon_bound_west'],grid_bounds['lon_bound_east'],grid_bounds['lag_grid_res']],
start_date)
### Execute particle simulation ###
print("Running Lagrangian simulation ...")
traj_output_file_path = lag_traj_dir + str(date_input) + '_' + filename_str + '.zarr'
simulate_particles2d(pset_dynamic,traj_output_file_path,
sim_params['runtime'],sim_params['runtime_unit'],
sim_params['timestep'],sim_params['output_freq'],
sim_params['backwards'])
print('Trajectory output file: %s'%(traj_output_file_path))
### Calculate Lagrangian average vorticity deviation (LAVD) ###
print("Calculating the LAVD ...")
traj_ds = xr.open_dataset(traj_output_file_path) # open the Lagrangian trajectory dataset that was just produced
vort_premask = traj_ds.variables["vort"]
vort = np.array(vort_premask.where(vort_premask != 0)) #filters out land values
LAVD = calc_LAVD(vort,sim_params['output_freq'],sim_params['runtime'])
LAVD_output_file_path = LAVD_dir + str(date_input) + '_LAVD_' + filename_str + '.npy'
np.save(LAVD_output_file_path,LAVD)
print('LAVD output file: %s'%(LAVD_output_file_path))
# #### the original version
# # Lexi Jones
# # Date Created: 07/15/21
# # Last Edited: 12/20/22
# # Run an OceanParcels simulation
# import time,sys
# import numpy as np
# import xarray as xr
# from glob import glob
# from datetime import datetime
# from parcels import FieldSet,Variable,JITParticle
# from config import *
# sys.path.append('./RCLVatlas/')
# from functions_for_parcels import *
# date_input = sys.argv[1] # user input: particle intialization date
# start_year,start_month,start_day = int(str(date_input)[0:4]),int(str(date_input)[4:6]),int(str(date_input)[6:8])
# start_date = datetime(start_year,start_month,start_day) # format datetime
# ### Create Parcels fieldset ###
# parcels_input_files = sorted(glob(gos_vel_dir+'dt_global_allsat_phy_l4_*.nc'))
# filenames = {'U': parcels_input_files,'V': parcels_input_files}
# variables = {'U': 'ugos','V': 'vgos'} #name of the velocity variables in the netCDF file
# dimensions = {'U': {'lon':'longitude','lat':'latitude','time':'time'},
# 'V': {'lon':'longitude','lat':'latitude','time':'time'}}
# fieldset = FieldSet.from_netcdf(filenames, variables, dimensions)
# print('Fieldset created.')
# ### Create particleset ###
# class SpinnyParticle(JITParticle):
# u = Variable('u',dtype=np.float64)
# v = Variable('v',dtype=np.float64)
# vort = Variable('vort',dtype=np.float64)
# pset_dynamic,num_particles = particle_grid2d(fieldset,SpinnyParticle,
# [grid_bounds['lat_bound_south'],grid_bounds['lat_bound_north'],grid_bounds['lag_grid_res']],
# [grid_bounds['lon_bound_west'],grid_bounds['lon_bound_east'],grid_bounds['lag_grid_res']],
# start_date)
# ### Execute particle simulation ###
# print("Running Lagrangian simulation ...")
# traj_output_file_path = lag_traj_dir + str(date_input) + '_' + filename_str + '.nc'
# simulate_particles2d(pset_dynamic,traj_output_file_path,
# sim_params['runtime'],sim_params['runtime_unit'],
# sim_params['timestep'],sim_params['output_freq'],
# sim_params['backwards'])
# print('Trajectory output file: %s'%(traj_output_file_path))
# ### Calculate Lagrangian average vorticity deviation (LAVD) ###
# print("Calculating the LAVD ...")
# traj_ds = xr.open_dataset(traj_output_file_path) # open the Lagrangian trajectory dataset that was just produced
# vort_premask = traj_ds.variables["vort"]
# vort = np.array(vort_premask.where(vort_premask != 0)) #filters out land values
# LAVD = calc_LAVD(vort,sim_params['output_freq'],sim_params['runtime'])
# LAVD_output_file_path = LAVD_dir + str(date_input) + '_LAVD_' + filename_str + '.npy'
# np.save(LAVD_output_file_path,LAVD)
# print('LAVD output file: %s'%(LAVD_output_file_path))