def write_binary(filename,dic,data,overwrite=False,big=True): """ Write Bruker binary data to file Parameters: * filename Filename to write to. * dic dictionary holding acqus_files and pprog_file parameters. * data array of data * overwrite True to overwrite files, False to warn. * big Endiness to write binary data with, bigendian=True, little=False No return. """ # open the file for writing f = fileiobase.open_towrite(filename,overwrite=overwrite) # convert objec to an array if it is not already one... if type(data) != np.ndarray: data = np.array(data) if np.iscomplexobj(data): put_data(f,uncomplexify_data(data),big) else: put_data(f,data,big) f.close() return
def write_3D(filename, dic, data, overwrite=False): """ Write a sparky file from 3D data """ # open the file for writing f = fileiobase.open_towrite(filename, overwrite) # write the file header put_fileheader(f, dic2fileheader(dic)) # write the axis headers put_axisheader(f, dic2axisheader(dic["w1"])) put_axisheader(f, dic2axisheader(dic["w2"])) put_axisheader(f, dic2axisheader(dic["w3"])) lentX = dic["w3"]["bsize"] lentY = dic["w2"]["bsize"] lentZ = dic["w1"]["bsize"] t_tup = (lentZ, lentY, lentX) ttX = np.ceil(data.shape[2] / float(lentX)) # total tiles in X dim ttY = np.ceil(data.shape[1] / float(lentY)) # total tiles in Y dim ttZ = np.ceil(data.shape[0] / float(lentZ)) # total tiles in Z dim tt = ttX * ttY * ttZ for i in xrange(int(tt)): put_data(f, find_tilen_3d(data, i, (t_tup))) f.close() return
def write_binary_lowmem(filename,dic,data,overwrite=False,big=True): """ Write Bruker binary data to file trace by trace (using minimal memory). Parameters: * filename Filename to write to. * dic dictionary holding acqus_files and pprog_file parameters. * data array of data * overwrite True to overwrite files, False to warn. * big Endiness to write binary data with, bigendian=True, little=False No return. """ # open the file for writing f = fileiobase.open_towrite(filename,overwrite=overwrite) cplex = np.iscomplexobj(data) # write out file trace by trace for tup in np.ndindex(data.shape[:-1]): trace = data[tup] if cplex: put_data(f,uncomplexify_data(trace),big) else: put_data(f,trace,big) f.close() return
def write_3D(filename,dic,data,overwrite=False): """ Write a sparky file from 3D data """ # open the file for writing f = fileiobase.open_towrite(filename,overwrite) # write the file header put_fileheader(f,dic2fileheader(dic)) # write the axis headers put_axisheader(f,dic2axisheader(dic["w1"])) put_axisheader(f,dic2axisheader(dic["w2"])) put_axisheader(f,dic2axisheader(dic["w3"])) lentX = dic["w3"]["bsize"] lentY = dic["w2"]["bsize"] lentZ = dic["w1"]["bsize"] t_tup = (lentZ,lentY,lentX) ttX = np.ceil(data.shape[2] / float(lentX)) # total tiles in X dim ttY = np.ceil(data.shape[1] / float(lentY)) # total tiles in Y dim ttZ = np.ceil(data.shape[0] / float(lentZ)) # total tiles in Z dim tt = ttX*ttY*ttZ for i in xrange(int(tt)): put_data( f,find_tilen_3d( data,i,(t_tup) ) ) f.close() return
def write_jcamp(dic,filename,overwrite=False): """ Write a Bruker JCAMP-DX file from a dictionary Written file will differ slightly from bruker's JCAMP-DX files in that all multi-value parameters will be written on multiple lines. Bruker is inconsistent on what is written to a single line and what is not. In addition line breaks may be slightly different but will always be within JCAMP-DX specification. Finally long floating point values may loose precision when writing. For example: ##$QS= (0..7)83 83 83 83 83 83 83 22 will be written as ##$QS= (0..7) 83 83 83 83 83 83 83 22 """ # open the file for writing f = fileiobase.open_towrite(filename,overwrite=overwrite) # create a copy of the dictionary d = dict(dic) # remove the comments and core header key from dictionary comments = d.pop("_comments") corehdr = d.pop("_coreheader") # write out the core headers for line in corehdr: f.write(line) f.write("\n") # write out the comments for line in comments: f.write(line) f.write("\n") keys = d.keys() keys.sort() # write out each key,value pair for key in keys: write_jcamp_pair(f,key,d[key]) # write ##END= and close the file f.write("##END=") f.close()
def write(filename, dic, data, dataset="spectrum", overwrite=False): """ Write dic,data pair to a HDF5 file """ # create the file f = fileiobase.open_towrite(filename, overwrite=overwrite) f.close() f = h5py.File(filename, 'w') # write the dictionary and data to the file f.create_dataset(dataset, data=data) put_dic(f, dic, dataset) f.close() return
def write(filename,dic,data,dataset="spectrum",overwrite=False): """ Write dic,data pair to a HDF5 file """ # create the file f = fileiobase.open_towrite(filename,overwrite=overwrite) f.close() f = h5py.File(filename,'w') # write the dictionary and data to the file f.create_dataset(dataset,data=data) put_dic(f,dic,dataset) f.close() return
def write(filename,comments,rec,overwrite=False): """ Write a nmrglue table to file (.tbl). Parameters: * filename Name of table file to write. * comments List of comments (strings terminated with newline). * rec Records array to write to file. * overwrite Set True to overwrite file if it exists. """ # open the file for writing f = fileiobase.open_towrite(filename,overwrite) # write out the comment lines at the top of the file for c in comments: f.write(c) # Determind the list of column names names = rec.dtype.names # Determind the list of column formats sizes = [rec.dtype[n].itemsize for n in names] kinds = [rec.dtype[n].kind for n in names] formats = [k+str(s) for s,k in zip(sizes,kinds)] # write out the NAMES and FORMATS lines f.write("# NAMES "+" ".join(names)+"\n") f.write("# FORMATS "+" ".join(formats)+"\n") # maximum string length for each column col_len = [max([len(str(i)) for i in rec[n]]) for n in names] # write out each line of the table for row in rec: s = " ".join([str(v).ljust(l) for v,l in zip(row,col_len)]) f.write(s+"\n") f.close()
def write_pprog(filename,dic,overwrite=False): """ Write a minimal Bruker pulse program DO NOT TRY TO RUN THE RESULTING PULSE PROGRAM This pulse program should return the same dictionary when read using read_pprog, nothing else. The pulse program will be nonsense. """ # open the file for writing f = fileiobase.open_towrite(filename,overwrite=overwrite) # write a comment f.write("; Minimal Bruker pulseprogram created by write_pprog\n") # write our the variables for k,v in dic["var"].iteritems(): f.write("\""+k+"="+v+"\"\n") # write out each loop for i,steps in enumerate(dic["loop"]): # write our the increments for v in dic["incr"][i]: f.write("d01 id"+str(v)+"\n") # write out the phases for v,w in zip(dic["phase"][i],dic["ph_extra"][i]): f.write("d01 ip"+str(v)+" "+str(w)+"\n") f.write("lo to 0 times "+str(steps)+"\n") # close the file f.close() return