Guest User

Untitled

a guest
Dec 10th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. def loadout():
  2.    
  3.     # This is the working directory to load files. I'll add an option to change this via console input.
  4.     dir = "/home/bradley/Desktop/BradSim/drop-6.00-3/"
  5.  
  6.     list = []
  7.    
  8.     # Changes the directory to the specified cwd
  9.     os.chdir(dir)
  10.    
  11.     # Iterates through the files to determine how many .bin files exist, storing that counter into an array
  12.     for files in glob.glob("*.bin"):
  13.         list.append(int(files[11:][:-4]))
  14.     list.sort()
  15.     list = list[:-1]
  16.     tot = len(list)
  17.    
  18.     # Hex size of a double
  19.     dubs = calcsize('d')
  20.    
  21.     # Empty arrays to fill when the work's being done
  22.     vcube = []
  23.     ccube = []
  24.     rcube = []
  25.  
  26.     # Iterate through all .bin files in the directory
  27.     for z in range(tot):
  28.         print(z)
  29.  
  30.         # Iterate opening the files and reading them into an array, converting the first value (an int) from hex to a usable format as 'num'
  31.         f = open("CartAndRad."+str(list[z])+".bin", "rb").read()
  32.         num = unpack("I", f[:4])
  33.         num = num[0]
  34.    
  35.         # Hex size of an int. This will be used as a running length counter to read through the full binary file
  36.         i = calcsize('I')
  37.    
  38.         # Empty arrays to fill up.
  39.             # pcoord: particle coordinates for the file
  40.             # pvel: particle velocities for the file
  41.             # rad: particle radii for the file
  42.         pcoord = []
  43.         pvel = []
  44.         rad = []
  45.         cube = []
  46.  
  47.         # Index counter for adding absolute particle IDs with their coordinates for later tracking
  48.         c = 0
  49.  
  50.         # Iterate through every particle in the file
  51.         for y in range(num):
  52.  
  53.             # Empty the the array for each particle, it will temporarily hold the coordinates for that particle
  54.             particle = []
  55.  
  56.             # Iterate through the coordinates (x,y,z,vx,vy,vz) of each particle
  57.             for x in range(6):
  58.  
  59.                 #Unpack convert each coordinate to a double and append it to the particle array
  60.                 temp = unpack('d', f[i:dubs+i])        
  61.                 particle.append(temp[0])
  62.  
  63.                 # Add on the hex length of a double to the running counter to then read the next double in the .bin
  64.                 i += dubs
  65.  
  66.             # Checks if the particle is above the "ground" (z = 0.25), ie, has it flown in an appreciable arc
  67.             if particle[2]>=0.25:
  68.  
  69.                 #print("Jerry, hello!")
  70.                
  71.                 # Append the first three coordinate values (x,y,z) to the pcoord array
  72.                 pcoord.append(particle[:3])
  73.  
  74.                 # Append the particle ID after the coordinates
  75.                 pcoord[c].append(y)
  76.  
  77.                 # Append the last three coordinate values (vx,vy,vz) to the pvel array
  78.                 pvel.append(particle[3:])
  79.  
  80.                 # Append, you guessed it, the particle ID afterwards
  81.                 pvel[c].append(y)
  82.  
  83.                 # Increment the counter for the particle arrays
  84.                 c += 1
  85.  
  86.         # Iterates through the particles to store the radii as well. I really don't think I'll need this,
  87.         #  but it could be useful for rough momenta calculations.
  88.         for y in range(num):
  89.             temp = unpack('d', f[i:dubs+i])
  90.            
  91.             # Store the data if the particle is above z = 0.25         
  92.             if particle[2]>=0.25:
  93.                 rad.append(temp)
  94.  
  95.             # Incremement the running hex read counter
  96.             i += dubs
  97.        
  98.         # Append all the coordinates to the great big inefficient data cubes
  99.         ccube.append(pcoord)
  100.         vcube.append(pvel)
  101.         rcube.append(rad)
  102.    
  103.     # Return it all
  104.     return ccube, vcube, rcube
Add Comment
Please, Sign In to add comment