def open(self, path): self.path = path self.f = open(self.path, "rb") self.f.seek(0, 0) self.line_header = self.f.read(3200) b_h = self.f.read(400) format = struct.unpack(b'>h', b_h[24:26:])[0] # Check if valid. if format in [1, 2, 3, 4, 5, 8]: self.order = "big" # Else test little endian. else: format = struct.unpack(b'<h', b_h[24:26:])[0] if format in [1, 2, 3, 4, 5, 8]: self.order = "little" else: msg = 'Unable to determine the endianness of the file. ' + \ 'Please specify it.' raise Exception(msg) # if (int.from_bytes(b_h[97:101:], "big") > 255): # self.order = "little" # else: # self.order = "big" # self.order="little" self.bin_head = Heads.BinHead(b_h, self.order) size_file = os.path.getsize(self.path) coef = self.check_coef() self.count_trace = (size_file - 3600) / (self.bin_head.Samples * coef + 240) self.count_trace = int(self.count_trace)
def write(filename, Data,SegyTraceHeaders=pandas.DataFrame(), SegyHeader=None, text_head=None,order="big",dt=1000,SampleFormat=3): print("START WRITE") if (isinstance(SegyTraceHeaders, dict)): SegyTraceHeaders = create_head_traces(SegyTraceHeaders) elif (isinstance(SegyTraceHeaders, pandas.DataFrame)): pass else: raise Exception("Not definde type") file = open(filename, "wb") if (SegyHeader == None): bin_head = Heads.BinHead(bytearray(400), order) else: bin_head = SegyHeader coef = retCoef(SampleFormat) bin_head.Interval = dt try: bh_s = bin_head.Samples except Exception as e: bin_head.Samples = len(Data[0]) bin_head.Format = SampleFormat if (text_head != None): file.write(text_head) else: file.write(bytearray(3200)) if (SegyTraceHeaders.empty): segyTraceHeaders = get_null_trace() else: segyTraceHeaders = SegyTraceHeaders # segyTraceHeaders.TRACE_SAMPLE_COUNT = len(Data[0]) Heads.writeBinHead(file, bin_head, order) all_c = bytearray() start = time() # df_dict = segyTraceHeaders.to_dict(orient='index') for i in range(0,len(Data)): if(i!=0 and i%1000000==0): file.write(all_c) all_c=bytearray() if(SegyTraceHeaders.empty): b = Heads.writeTraceHeadEmpty(file, segyTraceHeaders, order) else: b = Heads.writeTraceHead(file, segyTraceHeaders.iloc[i], order) a= Heads.writeData(file,Data[i],coef,order) try: all_c+=b+a except Exception as e: print(e) file.write(all_c) print(time() - start)
def load_all_file(self,path): with open(path ,"r+b") as f: self.f = mmap(f.fileno(), 0) self.f.seek(0, 0) self.line_header = self.f.read(3200) b_h = self.f.read(400) if (int.from_bytes(b_h[24:26:], "big") > 255): self.order = "little" else: self.order = "big" self.bin_head = Heads.BinHead(b_h, self.order) print(self.bin_head.__sizeof__()) size_file = os.path.getsize(path) coef = self.check_coef() self.count_trace = (size_file - 3600) / (self.bin_head.Samples * coef + 240) self.count_trace = int(self.count_trace)