-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathensemble_Member_spatial.py
More file actions
116 lines (89 loc) · 4.27 KB
/
ensemble_Member_spatial.py
File metadata and controls
116 lines (89 loc) · 4.27 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
#%%
from parcels import FieldSet, ParticleSet, AdvectionRK4_3D, AdvectionRK4, ParticleFile, JITParticle, StatusCode, Variable
import numpy as np
from glob import glob
from datetime import timedelta as delta
from argparse import ArgumentParser
import pandas as pd
import pickle
from datetime import datetime
p = ArgumentParser()
p.add_argument('-m', '--member', type=int, default=1, help='Member number')
# p.add_argument('-y', '--year', type=int, default=2010, help='Year')
p.add_argument('-dr', '--delta_r', type=float, help='delta r')
args = p.parse_args()
member = args.member
delta_r = args.delta_r
#Some SIMULATION parameter
location = 'Cape_Hatteras'
start_time = np.datetime64('2010-01-02')
end_time = np.datetime64('2015-12-31')
outfile = f"/storage/shared/oceanparcels/output_data/data_Claudio/NEMO_Ensemble/{location}/spatial_long/dr_{delta_r*100:03.0f}/{location}_dr{delta_r*100:03.0f}_m{member:03d}.zarr"
print("Output file: ", outfile)
#%% Import Fieldset
data_path = '/storage/shared/oceanparcels/input_data/NEMO_Ensemble/'
ufiles = sorted(glob(f"{data_path}NATL025-CJMCYC3.{member:03d}-S/1d/*/NATL025*U.nc")) #Load all years
vfiles = [f.replace('U.nc', 'V.nc') for f in ufiles]
wfiles = [f.replace('U.nc', 'W.nc') for f in ufiles]
mesh_mask = f"{data_path}GRID/coordinates_NATL025_v2.nc"
maskfile = f"{data_path}GRID/NATL025-CJMenobs01_byte_mask.nc"
filenames = {'U': {'lon': mesh_mask, 'lat': mesh_mask, 'depth': wfiles[0], 'data': ufiles},
'V': {'lon': mesh_mask, 'lat': mesh_mask, 'depth': wfiles[0], 'data': vfiles},
'W': {'lon': mesh_mask, 'lat': mesh_mask, 'depth': wfiles[0], 'data': wfiles},
'mask': {'lon': mesh_mask, 'lat': mesh_mask, 'depth': wfiles[0], 'data': maskfile}}
variables = {'U': 'vozocrtx', 'V': 'vomecrty', 'W': 'vovecrtz', 'mask': 'fmask'}
dimensions = {'U': {'lon': 'glamf', 'lat': 'gphif', 'depth': 'depthw', 'time': 'time_counter'},
'V': {'lon': 'glamf', 'lat': 'gphif', 'depth': 'depthw', 'time': 'time_counter'},
'W': {'lon': 'glamf', 'lat': 'gphif', 'depth': 'depthw', 'time': 'time_counter'},
'mask': {'lon': 'glamf', 'lat': 'gphif', 'depth': 'depthw'}}
fieldset = FieldSet.from_nemo(filenames, variables, dimensions, netcdf_decodewarning=False)
#%% Declare the ParticleSet
span = delta_r
L_range = np.linspace(-span, span, 125) # This L_range and theta_range makes that alway there are 1001 particles
theta_range = np.arange(0, 2*np.pi, np.pi/30)
lon_0, lat_0 = (-73.61184289610455, 35.60913368957989)
lonp = [lon_0]
latp = [lat_0]
for r in L_range:
for theta in theta_range:
lonp.append(lon_0 + np.sin(theta)*r)
latp.append(lat_0 + np.cos(theta)*r)
print("Number of particles: ", len(lonp))
times = [start_time]*len(lonp)
depp = np.ones(len(lonp))
hex_ids = [590726022320619519]*len(lonp)
class EnsembleParticle(JITParticle):
"""
Particle class definition with additional variables
"""
# dynamic variables
u = Variable('u', dtype=np.float32, initial=0)
v = Variable('v', dtype=np.float32, initial=0)
w = Variable('w', dtype=np.float32, initial=0)
hexbin_id = Variable('hexbin_id', dtype=np.int16, initial=0)
pset = ParticleSet(fieldset, EnsembleParticle, lon=lonp, lat=latp,
depth=depp, time=times, hexbin_id=hex_ids)
# %%Declare Kernels
def KeepInOcean(particle, fieldset, time):
if particle.state == StatusCode.ErrorThroughSurface:
particle_ddepth = 1.0
particle.state = StatusCode.Success
def CheckOutOfBounds(particle, fieldset, time):
if particle.state == StatusCode.ErrorOutOfBounds:
particle.delete()
def SampleField(particle, fieldset, time):
"""
Sample the fieldset at the particle location and store it in the
particle variable.
"""
(ui, vi, wi) = fieldset.UVW.eval(time, particle.depth, particle.lat, particle.lon,
particle=particle, applyConversion=False)
particle.u = ui
particle.v = vi
particle.w = wi
pfile = ParticleFile(outfile, pset, outputdt=delta(days=1), chunks=(len(pset), 1))
# Error handling kernels go at the end of the kernel list
pset.execute([AdvectionRK4_3D, SampleField, KeepInOcean, CheckOutOfBounds],
dt=delta(hours=1), endtime=end_time,
output_file=pfile)
# %%