Skip to content

Commit 9dc5570

Browse files
Cleaning particle.py by removing _Particle baseclass
1 parent b41a4f2 commit 9dc5570

1 file changed

Lines changed: 26 additions & 43 deletions

File tree

parcels/particle.py

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -127,43 +127,7 @@ def supported_dtypes(self):
127127
return [np.int32, np.uint32, np.int64, np.uint64, np.float32, np.double, np.float64, c_void_p]
128128

129129

130-
class _Particle:
131-
"""Private base class for all particle types."""
132-
133-
lastID = 0 # class-level variable keeping track of last Particle ID used
134-
135-
def __init__(self):
136-
ptype = self.getPType()
137-
# Explicit initialisation of all particle variables
138-
for v in ptype.variables:
139-
if isinstance(v.initial, attrgetter):
140-
initial = v.initial(self)
141-
else:
142-
initial = v.initial
143-
# Enforce type of initial value
144-
if v.dtype != c_void_p:
145-
setattr(self, v.name, v.dtype(initial))
146-
147-
# Placeholder for explicit error handling
148-
self.exception = None
149-
150-
def __del__(self):
151-
pass # superclass is 'object', and object itself has no destructor, hence 'pass'
152-
153-
@classmethod
154-
def getPType(cls):
155-
return ParticleType(cls)
156-
157-
@classmethod
158-
def getInitialValue(cls, ptype, name):
159-
return next((v.initial for v in ptype.variables if v.name is name), None)
160-
161-
@classmethod
162-
def setLastID(cls, offset):
163-
_Particle.lastID = offset
164-
165-
166-
class ScipyParticle(_Particle):
130+
class ScipyParticle:
167131
"""Class encapsulating the basic attributes of a particle, to be executed in SciPy mode.
168132
169133
Parameters
@@ -198,6 +162,8 @@ class ScipyParticle(_Particle):
198162
dt = Variable('dt', dtype=np.float64, to_write=False)
199163
state = Variable('state', dtype=np.int32, initial=StatusCode.Evaluate, to_write=False)
200164

165+
lastID = 0 # class-level variable keeping track of last Particle ID used
166+
201167
def __init__(self, lon, lat, pid, fieldset=None, ngrids=None, depth=0., time=0., cptr=None):
202168

203169
# Enforce default values through Variable descriptor
@@ -210,14 +176,23 @@ def __init__(self, lon, lat, pid, fieldset=None, ngrids=None, depth=0., time=0.,
210176
type(self).time.initial = time
211177
type(self).time_nextloop.initial = time
212178
type(self).id.initial = pid
213-
_Particle.lastID = max(_Particle.lastID, pid)
179+
type(self).lastID = max(type(self).lastID, pid)
214180
type(self).obs_written.initial = 0
215181
type(self).dt.initial = None
216182

217-
super().__init__()
183+
ptype = self.getPType()
184+
# Explicit initialisation of all particle variables
185+
for v in ptype.variables:
186+
if isinstance(v.initial, attrgetter):
187+
initial = v.initial(self)
188+
else:
189+
initial = v.initial
190+
# Enforce type of initial value
191+
if v.dtype != c_void_p:
192+
setattr(self, v.name, v.dtype(initial))
218193

219194
def __del__(self):
220-
super().__del__()
195+
pass # superclass is 'object', and object itself has no destructor, hence 'pass'
221196

222197
def __repr__(self):
223198
time_string = 'not_yet_set' if self.time is None or np.isnan(self.time) else f"{self.time:f}"
@@ -242,11 +217,11 @@ def add_variable(cls, var, *args, **kwargs):
242217
if isinstance(var, list):
243218
return cls.add_variables(var)
244219
if not isinstance(var, Variable):
245-
if len(args) > 0:
220+
if len(args) > 0 and 'dtype' not in kwargs:
246221
kwargs['dtype'] = args[0]
247-
if len(args) > 1:
222+
if len(args) > 1 and 'initial' not in kwargs:
248223
kwargs['initial'] = args[1]
249-
if len(args) > 2:
224+
if len(args) > 2 and 'to_write' not in kwargs:
250225
kwargs['to_write'] = args[2]
251226
dtype = kwargs.pop('dtype', np.float32)
252227
initial = kwargs.pop('initial', 0)
@@ -273,6 +248,10 @@ def add_variables(cls, variables):
273248
NewParticle = NewParticle.add_variable(var)
274249
return NewParticle
275250

251+
@classmethod
252+
def getPType(cls):
253+
return ParticleType(cls)
254+
276255
@classmethod
277256
def set_lonlatdepth_dtype(cls, dtype):
278257
cls.lon.dtype = dtype
@@ -282,6 +261,10 @@ def set_lonlatdepth_dtype(cls, dtype):
282261
cls.lat_nextloop.dtype = dtype
283262
cls.depth_nextloop.dtype = dtype
284263

264+
@classmethod
265+
def setLastID(cls, offset):
266+
ScipyParticle.lastID = offset
267+
285268

286269
ScipyInteractionParticle = ScipyParticle.add_variables([
287270
Variable("vert_dist", dtype=np.float32),

0 commit comments

Comments
 (0)