Skip to content

Commit 70021ac

Browse files
authored
Merge pull request #934 from OceanParcels/transform_to_int64_particle_ID
Change Particle-ID type from int (int32) to int64
2 parents 9872f1e + e064a4f commit 70021ac

3 files changed

Lines changed: 9 additions & 11 deletions

File tree

parcels/particle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
__all__ = ['ScipyParticle', 'JITParticle', 'Variable']
1111

12-
indicators_64bit = [np.float64, np.int64, c_void_p]
12+
indicators_64bit = [np.float64, np.uint64, np.int64, c_void_p]
1313

1414

1515
class Variable(object):
@@ -178,7 +178,7 @@ class ScipyParticle(_Particle):
178178
yi = Variable('yi', dtype=np.int32, to_write=False)
179179
zi = Variable('zi', dtype=np.int32, to_write=False)
180180
ti = Variable('ti', dtype=np.int32, to_write=False, initial=-1)
181-
id = Variable('id', dtype=np.int32)
181+
id = Variable('id', dtype=np.int64)
182182
fileid = Variable('fileid', dtype=np.int32, initial=-1, to_write=False)
183183
dt = Variable('dt', dtype=np.float64, to_write=False)
184184
state = Variable('state', dtype=np.int32, initial=StateCode.Evaluate, to_write=False)

parcels/particlefile.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ def open_netcdf_file(self, data_shape):
125125
self.dataset.parcels_mesh = self.parcels_mesh
126126

127127
# Create ID variable according to CF conventions
128-
self.id = self.dataset.createVariable("trajectory", "i4", coords,
129-
fill_value=-2**(31)) # maxint32 fill_value
128+
self.id = self.dataset.createVariable("trajectory", "i8", coords, fill_value=-2**(63)) # minint64 fill_value
130129
self.id.long_name = "Unique identifier for each particle"
131130
self.id.cf_role = "trajectory_id"
132131

@@ -253,8 +252,8 @@ def read_from_npy(self, file_list, time_steps, var):
253252
"""
254253

255254
data = np.nan * np.zeros((self.maxid_written+1, time_steps))
256-
time_index = np.zeros(self.maxid_written+1, dtype=int)
257-
t_ind_used = np.zeros(time_steps, dtype=int)
255+
time_index = np.zeros(self.maxid_written+1, dtype=np.int64)
256+
t_ind_used = np.zeros(time_steps, dtype=np.int64)
258257

259258
# loop over all files
260259
for npyfile in file_list:
@@ -266,7 +265,7 @@ def read_from_npy(self, file_list, time_steps, var):
266265
'"parcels_convert_npydir_to_netcdf %s" to convert these to '
267266
'a NetCDF file yourself.\nTo avoid this error, make sure you '
268267
'close() your ParticleFile at the end of your script.' % self.tempwritedir)
269-
id_ind = np.array(data_dict["id"], dtype=int)
268+
id_ind = np.array(data_dict["id"], dtype=np.int64)
270269
t_ind = time_index[id_ind] if 'once' not in file_list[0] else 0
271270
t_ind_used[t_ind] = 1
272271
data[id_ind, t_ind] = data_dict[var]

parcels/particleset.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def to_dict(self, pfile, time, deleted_only=False):
540540
if np.any(to_write) > 0:
541541
for var in pfile.var_names:
542542
data_dict[var] = pd[var][to_write]
543-
pfile.maxid_written = max(pfile.maxid_written, np.max(data_dict['id']))
543+
pfile.maxid_written = np.maximum(pfile.maxid_written, np.max(data_dict['id']))
544544

545545
pset_errs = (to_write & (pd['state'] != OperationCode.Delete)
546546
& np.less(1e-3, np.abs(time - pd['time']), where=np.isfinite(pd['time'])))
@@ -552,10 +552,9 @@ def to_dict(self, pfile, time, deleted_only=False):
552552
pfile.time_written.append(time)
553553

554554
if len(pfile.var_names_once) > 0:
555-
first_write = (_to_write_particles(pd, time)
556-
& np.isin(pd['id'], pfile.written_once, invert=True))
555+
first_write = (_to_write_particles(pd, time) & np.isin(pd['id'], pfile.written_once, invert=True))
557556
if np.any(first_write):
558-
data_dict_once['id'] = pd['id'][first_write]
557+
data_dict_once['id'] = np.array(pd['id'][first_write]).astype(dtype=np.int64)
559558
for var in pfile.var_names_once:
560559
data_dict_once[var] = pd[var][first_write]
561560
pfile.written_once.extend(pd['id'][first_write])

0 commit comments

Comments
 (0)