def readEvent(self, type=None): # Read header data = fortran.read(self.hnd) if data is None: return None if len(data) == 20: ndum, mdum, jdum, edum, wdum \ = struct.unpack("=iiiff", data) else: raise IOError("Invalid MGREAD file") self.nevent += 1 if ndum > 0: if type is None or type == 0: self.readTracking(ndum, mdum, jdum, edum, wdum) else: fortran.skip(self.hnd) return 0 elif ndum == 0: if type is None or type == 1: self.readEnergy(mdum, jdum, edum, wdum) else: fortran.skip(self.hnd) return 1 else: if type is None or type == 2: self.readSource(-ndum, mdum, jdum, edum, wdum) else: fortran.skip(self.hnd) return 2
def readStat(self, n): """Read detector det statistical data""" if self.statpos < 0: return None f = open(self.file, "rb") f.seek(self.statpos) f.seek(self.statpos) if self.nisomers: nskip = 7 * n else: nskip = 6 * n for i in range(nskip): fortran.skip(f) # Detector Data total = fortran.read(f) A = fortran.read(f) errA = fortran.read(f) Z = fortran.read(f) errZ = fortran.read(f) data = fortran.read(f) if self.nisomers: iso = fortran.read(f) else: iso = None f.close() return (total, A, errA, Z, errZ, data, iso)
def readEnergy(self, icode, jtrack, etrack, wtrack): self.icode = icode self.jtrack = jtrack self.etrack = etrack self.wtrack = wtrack data = fortran.read(self.hnd) if data is None: raise IOError("Invalid energy deposition event") self.data = struct.unpack("=4f", data) return icode
def readStat(self, n): """Read detector n statistical data""" if self.statpos < 0: return None f = open(self.file, "rb") f.seek(self.statpos) for i in range(n): fortran.skip(f) # Detector Data data = fortran.read(f) f.close() return data
def readData(self, det): """Read detector det data structure""" f = open(self.file, "rb") fortran.skip(f) # Skip header for i in range(2 * det): fortran.skip(f) # Detector Header & Data fortran.skip(f) # Detector Header data = fortran.read(f) f.close() return data
def readTracking(self, ntrack, mtrack, jtrack, etrack, wtrack): self.ntrack = ntrack self.mtrack = mtrack self.jtrack = jtrack self.etrack = etrack self.wtrack = wtrack data = fortran.read(self.hnd) if data is None: raise IOError("Invalid track event") fmt = "=%df" % (3 * (ntrack + 1) + mtrack + 1) self.data = struct.unpack(fmt, data) return ntrack
def readSource(self, ncase, npflka, nstmax, tkesum, weipri): self.ncase = ncase self.npflka = npflka self.nstmax = nstmax self.tkesum = tkesum self.weipri = weipri data = fortran.read(self.hnd) if data is None: raise IOError("Invalid source event") fmt = "=" + ("i8f" * npflka) self.data = struct.unpack(fmt, data) return ncase
def readData(self, n): """Read detector n data structure""" f = open(self.file, "rb") fortran.skip(f) for i in range(n): fortran.skip(f) # Detector Header if self.detector[i].lowneu: fortran.skip(f) # Detector low enetry neutron groups fortran.skip(f) # Detector data fortran.skip(f) # Detector Header if self.detector[n].lowneu: fortran.skip(f) # Detector low enetry neutron groups data = fortran.read(f) # Detector data f.close() return data
def readData(self, n): """Read detector det data structure""" f = open(self.file, "rb") fortran.skip(f) if self.evol: fortran.skip(f) for i in range(n): fortran.skip(f) # Detector Header & Data if self.evol: fortran.skip(f) # TDecay fortran.skip(f) # Detector data if self.nisomers: fortran.skip(f) # Isomers header fortran.skip(f) # Isomers data fortran.skip(f) # Detector Header & Data if self.evol: fortran.skip(f) # TDecay data = fortran.read(f) # Detector data f.close() return data
def readHeader(self, filename): """Read header information, and return the file handle""" self.reset() self.file = filename f = open(self.file, "rb") # Read header data = fortran.read(f) if data is None: raise IOError("Invalid USRxxx file") size = len(data) over1b = 0 if size == 116: (title, time, self.weight) = \ struct.unpack("=80s32sf", data) self.ncase = 1 self.nbatch = 1 elif size == 120: (title, time, self.weight, self.ncase) = \ struct.unpack("=80s32sfi", data) self.nbatch = 1 elif size == 124: (title, time, self.weight, self.ncase, self.nbatch) = \ struct.unpack("=80s32sfii", data) elif size == 128: (title, time, self.weight, self.ncase, over1b, self.nbatch) = \ struct.unpack("=80s32sfiii", data) else: raise IOError("Invalid USRxxx file") if over1b > 0: self.ncase = long(self.ncase) + long(over1b) * 1000000000 self.title = title.strip() self.time = time.strip() return f
def readHeader(self, filename): """Read USRBIN detector information""" f = Usrxxx.readHeader(self, filename) for i in range(1000): # Header data = fortran.read(f) if data is None: break size = len(data) # Statistics are present? if size == 14 and data[:10] == "STATISTICS": self.statpos = f.tell() break if size != 86: raise IOError("Invalid USRBIN file") # Parse header header = struct.unpack("=i10siiffifffifffififff", data) bin = Detector() bin.nb = header[0] bin.name = header[1].strip() bin.type = header[2] bin.score = header[3] bin.xlow = float(bmath.format(header[4], 9, useD=False)) bin.xhigh = float(bmath.format(header[5], 9, useD=False)) bin.nx = header[6] if bin.nx > 0 and bin.type not in (2, 12, 8, 18): bin.dx = (bin.xhigh - bin.xlow) / float(bin.nx) else: bin.dx = float(bmath.format(header[7], 9, useD=False)) if bin.type in (1, 11): bin.ylow = -math.pi bin.yhigh = math.pi else: bin.ylow = float(bmath.format(header[8], 9, useD=False)) bin.yhigh = float(bmath.format(header[9], 9, useD=False)) bin.ny = header[10] if bin.ny > 0 and bin.type not in (2, 12, 8, 18): bin.dy = (bin.yhigh - bin.ylow) / float(bin.ny) else: bin.dy = float(bmath.format(header[11], 9, useD=False)) bin.zlow = float(bmath.format(header[12], 9, useD=False)) bin.zhigh = float(bmath.format(header[13], 9, useD=False)) bin.nz = header[14] if bin.nz > 0 and bin.type not in (2, 12): # 8=special with z=real bin.dz = (bin.zhigh - bin.zlow) / float(bin.nz) else: bin.dz = float(bmath.format(header[15], 9, useD=False)) bin.lntzer = header[16] bin.bk = header[17] bin.b2 = header[18] bin.tc = header[19] self.detector.append(bin) size = bin.nx * bin.ny * bin.nz * 4 if fortran.skip(f) != size: raise IOError("Invalid USRBIN file") f.close()
def readHeader(self, filename): """Read boundary crossing detector information""" f = Usrxxx.readHeader(self, filename) for i in range(1000): # Header data = fortran.read(f) if data is None: break size = len(data) # Statistics are present? if size == 14: # In statistics # 1: total, error # 2: N,NG,Elow (array with Emaxi) # 3: Differential integrated over solid angle # 4: -//- errors # 5: Cumulative integrated over solid angle # 6: -//- errors # 7: Double differential data self.statpos = f.tell() for det in self.detector: data = unpackArray(fortran.read(f)) det.total = data[0] det.totalerror = data[1] for j in range(6): fortran.skip(f) break if size != 78: raise IOError("Invalid USRBDX file") # Parse header header = struct.unpack("=i10siiiifiiiffifffif", data) det = Detector() det.nb = header[0] # mx det.name = header[1].strip() # titusx det.type = header[2] # itusbx det.dist = header[3] # idusbx det.reg1 = header[4] # nr1usx det.reg2 = header[5] # nr2usx det.area = header[6] # ausbdx det.twoway = header[7] # lwusbx det.fluence = header[8] # lfusbx det.lowneu = header[9] # llnusx det.elow = header[10] # ebxlow det.ehigh = header[11] # ebxhgh det.ne = header[12] # nebxbn det.de = header[13] # debxbn det.alow = header[14] # abxlow det.ahigh = header[15] # abxhgh det.na = header[16] # nabxbn det.da = header[17] # dabxbn self.detector.append(det) if det.lowneu: data = fortran.read(f) det.ngroup = struct.unpack("=i", data[:4])[0] det.egroup = struct.unpack("=%df" % (det.ngroup + 1), data[4:]) else: det.ngroup = 0 det.egroup = [] size = (det.ngroup + det.ne) * det.na * 4 if size != fortran.skip(f): raise IOError("Invalid USRBDX file") f.close()
def readHeader(self, filename): """Read residual nuclei detector information""" f = Usrxxx.readHeader(self, filename) self.nisomers = 0 if self.ncase <= 0: self.evol = True self.ncase = -self.ncase data = fortran.read(f) nir = (len(data) - 4) // 8 self.irrdt = struct.unpack("=i%df" % (2 * nir), data) else: self.evol = False self.irrdt = None for i in range(1000): # Header data = fortran.read(f) if data is None: break size = len(data) self.irrdt = None # Statistics are present? if size == 14 and data[:8] == "ISOMERS:": self.nisomers = struct.unpack("=10xi", data)[0] data = fortran.read(f) data = fortran.read(f) size = len(data) if size == 14 and data[:10] == "STATISTICS": self.statpos = f.tell() break if size != 38: raise IOError("Invalid RESNUCLEi file header size=%d" % (size)) # Parse header header = struct.unpack("=i10siif3i", data) det = Detector() det.nb = header[0] det.name = header[1].strip() det.type = header[2] det.region = header[3] det.volume = header[4] det.mhigh = header[5] det.zhigh = header[6] det.nmzmin = header[7] self.detector.append(det) if self.evol: data = fortran.read(f) self.tdecay = struct.unpack("=f", data) else: self.tdecay = 0.0 size = det.zhigh * det.mhigh * 4 if size != fortran.skip(f): raise IOError("Invalid RESNUCLEi file") f.close()