11from typing import List , Tuple , Dict , Union , Callable
22
33import matplotlib .pyplot as plt
4- import os
4+ from pathlib import Path
55import numpy as np
66import glob
77import struct
@@ -51,8 +51,8 @@ def __init__(
5151 if type (dirs ) == str :
5252 dirs = [dirs ]
5353
54- header = dirs [0 ] + "/Header"
55- if os . path . exists (header ):
54+ header = Path ( dirs [0 ] + "/Header" )
55+ if header . exists ():
5656 with open (header , "r" ) as f :
5757 self .nReal = int (f .readline ())
5858 else :
@@ -88,8 +88,8 @@ def __init__(
8888 self .pfiles = self .pfiles [iListStart :iListEnd ]
8989
9090 self .plists : List [Dict [Tuple [int , int ], int ]] = []
91- for fileName in self .plistfiles :
92- self .plists .append (self .read_particle_list (fileName ))
91+ for filename in self .plistfiles :
92+ self .plists .append (self .read_particle_list (filename ))
9393
9494 self .IDs = set ()
9595 for plist in self .plists :
@@ -122,28 +122,28 @@ def get_index_to_time(self) -> List:
122122 print ("Index to time mapping was not initialized" )
123123 return self .indextotime
124124
125- def read_particle_list (self , fileName : str ) -> Dict [Tuple [int , int ], int ]:
125+ def read_particle_list (self , filename : str ) -> Dict [Tuple [int , int ], int ]:
126126 """
127127 Read and return a list of the particle IDs.
128128 """
129129 # 2 integers + 1 unsigned long long
130130 listUnitSize = 2 * 4 + 8
131- nByte = os . path . getsize ( fileName )
131+ nByte = Path ( filename ). stat (). st_size
132132 nPart = int (nByte / listUnitSize )
133133 plist = {}
134- with open (fileName , "rb" ) as f :
134+ with open (filename , "rb" ) as f :
135135 for _ in range (nPart ):
136136 binaryData = f .read (listUnitSize )
137137 (cpu , id , loc ) = struct .unpack ("iiQ" , binaryData )
138138 plist .update ({(cpu , id ): loc })
139139 return plist
140140
141- def _read_the_first_record (self , fileName : str ) -> Union [List [float ], None ]:
141+ def _read_the_first_record (self , filename : str ) -> Union [List [float ], None ]:
142142 """
143143 Get the first record stored in one file.
144144 """
145145 dataList = list ()
146- with open (fileName , "rb" ) as f :
146+ with open (filename , "rb" ) as f :
147147 while True :
148148 binaryData = f .read (4 * 4 )
149149
@@ -184,11 +184,11 @@ def read_particles_at_time(
184184 break
185185 iFile += 1
186186
187- fileName = self .pfiles [iFile ]
187+ filename = self .pfiles [iFile ]
188188
189189 dataList : list [float ] = []
190190 idList : list [tuple ] = []
191- with open (fileName , "rb" ) as f :
191+ with open (filename , "rb" ) as f :
192192 while True :
193193 binaryData = f .read (4 * 4 )
194194 if not binaryData :
@@ -217,14 +217,14 @@ def read_particles_at_time(
217217 raise Exception (f"There are no particles at time { time } ." )
218218
219219 if doSave :
220- fileName = f"particles_t{ time } .csv"
220+ filename = f"particles_t{ time } .csv"
221221 header = "cpu,iid,time,x,y,z,ux,uy,uz"
222222 if self .nReal == 10 :
223223 header += ",bx,by,bz"
224224 elif self .nReal == 13 :
225225 header += ",bx,by,bz,ex,ey,ez"
226226
227- with open (fileName , "w" ) as f :
227+ with open (filename , "w" ) as f :
228228 f .write (header + "\n " )
229229 for id_row , data_row in zip (idData , npData ):
230230 f .write (
@@ -236,7 +236,7 @@ def read_particles_at_time(
236236 def save_trajectory_to_csv (
237237 self ,
238238 pID : Tuple [int , int ],
239- fileName : str = None ,
239+ filename : str = None ,
240240 shiftTime : bool = False ,
241241 scaleTime : bool = False ,
242242 ) -> None :
@@ -252,8 +252,8 @@ def save_trajectory_to_csv(
252252 >>> tp.save_trajectory_to_csv((3,15))
253253 """
254254 pData = self .read_particle_trajectory (pID )
255- if fileName == None :
256- fileName = "trajectory_" + str (pID [0 ]) + "_" + str (pID [1 ]) + ".csv"
255+ if filename == None :
256+ filename = "trajectory_" + str (pID [0 ]) + "_" + str (pID [1 ]) + ".csv"
257257 header = "time [s], X [R], Y [R], Z [R], U_x [km/s], U_y [km/s], U_z [km/s]"
258258 if self .nReal == 10 :
259259 header += ", B_x [nT], B_y [nT], B_z [nT]"
@@ -265,7 +265,7 @@ def save_trajectory_to_csv(
265265 pData [:, 0 ] -= pData [0 , 0 ]
266266 if scaleTime :
267267 pData [:, 0 ] /= pData [- 1 , 0 ]
268- np .savetxt (fileName , pData , delimiter = "," , header = header , comments = "" )
268+ np .savetxt (filename , pData , delimiter = "," , header = header , comments = "" )
269269
270270 def read_particle_trajectory (self , pID : Tuple [int , int ]):
271271 """
@@ -278,10 +278,10 @@ def read_particle_trajectory(self, pID: Tuple[int, int]):
278278 >>> trajectory = tp.read_particle_trajectory((66,888))
279279 """
280280 dataList = list ()
281- for fileName , plist in zip (self .pfiles , self .plists ):
281+ for filename , plist in zip (self .pfiles , self .plists ):
282282 if pID in plist :
283283 ploc = plist [pID ]
284- with open (fileName , "rb" ) as f :
284+ with open (filename , "rb" ) as f :
285285 f .seek (ploc )
286286 binaryData = f .read (4 * 4 )
287287 (cpu , idtmp , nRecord , weight ) = struct .unpack ("iiif" , binaryData )
@@ -298,10 +298,10 @@ def read_initial_location(self, pID):
298298 Return the initial location of a test particle.
299299 """
300300
301- for fileName , plist in zip (self .pfiles , self .plists ):
301+ for filename , plist in zip (self .pfiles , self .plists ):
302302 if pID in plist :
303303 ploc = plist [pID ]
304- with open (fileName , "rb" ) as f :
304+ with open (filename , "rb" ) as f :
305305 f .seek (ploc )
306306 binaryData = f .read (4 * 4 )
307307 (cpu , idtmp , nRecord , weight ) = struct .unpack ("iiif" , binaryData )
0 commit comments