def meshconvert(meshin, meshout, infile, outfile, interpolation="cubic", dtype=np.float64): """""" numin = np.prod(np.array(meshin)) datain = np.fromfile(infile, dtype=dtype) nfields = int(len(datain) / numin) assert ((nfields * numin) == len(datain)) numout = np.prod(np.array(meshout)) dataout = np.zeros(nfields * numout) for f in range(nfields): dataout[f * numout:(f + 1) * numout] = to_fortran( convertone( np.reshape(datain[f * numin:(f + 1) * numin], (meshin[0], meshin[1], meshin[2]), "F"), meshin, meshout, interpolation)) np.tofile(outfile, dataout)
def write_to_file(model_data, output_directory, write_csv=False, precision=np.float32): """ Write pca model to output directory :param output_file_name: :param model_data: Data to write :param output_directory: place to write the data :return: """ for p_level_data in model_data: p_level = p_level_data['pLevel'] # create directory for persistence level p_level_output_directory = os.path.join(output_directory, 'persistence-' + str(p_level)) if not os.path.exists(p_level_output_directory): os.makedirs(p_level_output_directory) for c_id, crystal_data in enumerate(p_level_data['models']): # create directory for crystal crystal_output_directory = os.path.join(p_level_output_directory, 'crystal-' + str(c_id)) if not os.path.exists(crystal_output_directory): os.makedirs(crystal_output_directory) # export W, w0, and z... for V in ['W', 'w0', 'z']: # ...as csvs if (write_csv): np.savetxt(os.path.join(crystal_output_directory, V + '.csv'), crystal_data[V], delimiter=',') # ...and bins (with associated dims files) np.tofile(os.path.join(crystal_output_directory, V + '.bin'), precision(crystal_data[V])) dims = open(V + '.bin.dims', 'w') dims.write( str(crystal_data[V].shape[0]) + ' ' + str(crystal_data[V].shape[1]) + ' ') dims.write( "float32") if precision == np.float32 else dims.write( "float64")
def write_header(h,nx,ny,nz,it,t,re,fr,sc): t_i4=np.dtype('<i4') t_f8=np.dtype('<f8') ipar=[52,nx,ny,nz,it] fpar=[t,re,fr,sc] np.tofile(h,ipar,t_i4,count=5) np.tofile(h,fpar,t_f8,count=4) np.tofile(h,[52],t_i4,count=1)
def save_data(args, data): np.tofile("test_file", data) return None
def _append_profile(self, ind, bz_i, is_same_bathy, fid): """Append a profile to file""" bp_i = (Ellipsis, ind) if bz_i is 1 else (ind, Ellipsis) printer = np.array([self.profiles[bp_i], self.z_axis]).T np.tofile(fid, printer)
print 'Progress = {} out of {}'.format(0, number_limit-1) with open('range_limited/B1hor_lr_{}.npy'.format(share_limit[0]),'w+') as df: np.savez(df, M = data, emittance = emit, shape = (l,w)) crapdata = [] for num in share_limit[1:]: data = ia.interpolate_to_center(num) emit = ia.emittance(data) if data is None: print '****FILE NUMBER {} HAS ZERO MASS****'.format(num) crapdata.append(num) continue #M = np.vstack((M,data.reshape(l*w))) #emittance = np.hstack((emittance, np.array(emit))) with open('range_limited/B1hor_lr_{}.npy'.format(num),'w+') as df: np.savez(df, M = data, emittance = emit, shape = (l,w)) print 'Progress = {} out of {}'.format(num, share_limit.max()) if crapdata != []: print 'crap data = ', crapdata """ U, S, Vt = svd(M, full_matrices = False) file0 = open('B1hor_range_limited_processed.npy','w+') np.tofile(file0,{'data': M, 'emittances': emittance, 'super_grid': super_grid}) file0.close() file1 = open('B1hor_range_limit_SVD.npy','w') np.tofile(file1,{'U': U, 'S': S,'Vt': Vt, 'emittances': emittance}) file1.close()"""
def parse(cmd): if len(cmd) == 1: print(help) return if len(cmd) < 3: out = cmd[1] + ".zcmp" else: out = cmd[2] if len(cmd) < 4: lbl = 'data' else: lbl = cmd[3] infile = cmd[1] if infile[-4:-1].lower() == ".8x": print("I'm lazy, so just assuming this is a TI Variable") s = np.fromfile(infile, dtype=np.uint8)[74:-2] else: s = np.fromfile(infile, dtype=np.uint8) n = len(s) print("%d bytes in" % (n)) if n == 0: np.tofile(out, s) return s if n <= minsize: s = np.append(np.array([n], dtype=np.uint8), s) s.tofile(out) return s head = minsize tail = minsize t = np.array([], dtype=np.uint8) base = 0 cnt = 128 cc = 10 while tail <= n - minsize: head = tail srch = 0 match = [0, 0, minsize - 1] while srch < head: while s[head] != s[srch]: srch += 1 if srch < head: u = head v = srch k = 0 while s[u] == s[v] and u < n - 1: u += 1 v += 1 k += 1 if s[u] == s[v]: u += 1 v += 1 k += 1 if k >= match[2]: match = [u, v, k] srch += 1 if match[2] >= minsize: u = match[0] v = match[1] k = match[2] m = s[base:tail] if len(m) > 0: t = np.append(t, size(len(m), False)) t = np.append(t, m) cc += 218 + 21 * len(m) if len(m) >= 64: cc += 111 cc += 285 + 21 * k if k >= 64: cc += 111 if tail - v + k >= 64: cc += 115 t = np.append(t, np.array(size(k, False, 128), dtype=np.uint8)) t = np.append(t, np.array(size(tail - v + k), dtype=np.uint8)) tail += k base = tail if tail >= cnt: cnt += 128 else: tail += 1 if base < n: m = s[base:n] t = np.append(t, size(len(m), False)) t = np.append(t, m) print("%d bytes out" % (len(t))) print("Ratio: %d%%" % (len(t) * 100 / n)) print("Decompression Speed for LZD %dcc, about %f times slower than LDIR" % (cc, cc / (21 * n - 5.0))) if out[-4:] == ".z80" or out[-4:] == ".asm": f = open(out, 'w') f.write(lbl + ':\n') f.write('.dw ' + lbl + '_end-2-' + lbl) k = 16 hx = '0123456789ABCDEF' for i in t: if k == 16: k = 0 f.write('\n.db $') else: f.write(',$') f.write(hx[i >> 4] + hx[i & 15]) k += 1 f.write('\n' + lbl + '_end:') f.close() else: t.tofile(out) return t
def comp(s): global cc n = len(s) if n == 0: np.tofile(out, s) return s if n <= minsize: s = np.append(np.array([n], dtype=np.uint8), s) s.tofile(out) return s head = minsize tail = minsize t = np.array([], dtype=np.uint8) base = 0 cnt = 128 cc = 10 while tail <= n - minsize: head = tail srch = 0 match = [0, 0, minsize - 1] while srch < head: while s[head] != s[srch]: srch += 1 if srch < head: u = head v = srch k = 0 while s[u] == s[v] and u < n - 1: u += 1 v += 1 k += 1 if s[u] == s[v]: u += 1 v += 1 k += 1 if k >= match[2]: match = [u, v, k] srch += 1 if match[2] >= minsize: u = match[0] v = match[1] k = match[2] m = s[base:tail] if len(m) > 0: t = np.append(t, size(len(m), False)) t = np.append(t, m) cc += 218 + 21 * len(m) if len(m) >= 64: cc += 111 cc += 285 + 21 * k if k >= 64: cc += 111 if tail - v + k >= 64: cc += 115 t = np.append(t, np.array(size(k, False, 128), dtype=np.uint8)) t = np.append(t, np.array(size(tail - v + k), dtype=np.uint8)) tail += k base = tail if tail >= cnt: cnt += 128 else: tail += 1 if base < n: m = s[base:n] t = np.append(t, size(len(m), False)) t = np.append(t, m) return t