def main(): # MAIN PROGRAM BODY # Parse the command line switches optioncount = len(sys.argv) SAC = False outputfile_defined = False filelist = [] dir="" extension = '.txt' if optioncount > 1: if optioncount == 4: if sys.argv[3] == '-s': SAC = True # print "SAC set to true." outfile = sys.argv[2] infile = sys.argv[1] filelist.append(infile) elif optioncount == 3: if "." in sys.argv[1]: infile = sys.argv[1] filelist.append(infile) outputfile_defined = True outfile = sys.argv[2] else: if len(sys.argv[2])==4 and "." in sys.argv[2]: # set a different extension extension = sys.argv[2] filelist = os.listdir(sys.argv[1]) dir=sys.argv[1] elif optioncount == 2: if "." in sys.argv[1]: infile = sys.argv[1] filelist.append(infile) else: dir = sys.argv[1] filelist = os.listdir(sys.argv[1]) for n in range(len(filelist)): if extension in filelist[n]: if len(filelist)>1: infile = dir+"/"+filelist[n] if string.find(infile,'.') > 0: outfile = infile[:string.find(infile,'.')]+'.sac' seedfile = infile[:string.find(infile,'.')]+'.mseed' else: outfile = infile +'.sac' seedfile = infile + '.mseed' PNE = load(infile) # PNE[0] is the header where: # PNE[0][0][1] = The comment descriptor of the data # PNE[0][1][1] = The station name # PNE[0][2][1] = Component axis(Z,N, or E) # PNE[0][4][1] = Start time # PNE[0][5][1] = Calibration factor # PNE[0][6][1] = Time correction in seconds Comment = PNE[0][0][1] Stname = PNE[0][1][1][:7] Component = PNE[0][2][1][:3] St_time = time.strptime(PNE[0][4][1][:-4],"%d_%b_%Y_%H:%M:%S") Frac_sec = int(PNE[0][4][1][21:]) CF = np.float32(PNE[0][5][1]) TC = PNE[0][6][1] # time correction Offset = float(PNE[1][0][0]) # # Delta is calculated from the offset time of last sample # minus offset of first sample / total number of samples. Delta = (float(PNE[1][len(PNE[1])-1][0])-Offset)/(len(PNE[1])-1) # Load Data array # Samples in file are multiplied by 10,000 to convert from # measurements of centimeters to microns, then it's divided by # the Amplification (conversion) factor, known as CF Data = [] for n in range (len(PNE[1])-1): Datum = np.float32(np.float32(PNE[1][n][1])*10000.0/CF) Data.append(Datum) b = np.arange(len(Data),dtype=np.float32) for n in range(len(Data)): # Load the array with time-history data b[n] = Data[n] t = SACTrace(data = b) # set the SAC header values t.scale = 1.0 # Set the scale for use with DIMAS software t.delta = Delta t.nzyear = St_time.tm_year t.nzjday = St_time.tm_yday t.nzhour = St_time.tm_hour t.nzmin = St_time.tm_min t.nzsec = St_time.tm_sec t.nzmsec = Frac_sec # int((Frac_second)*1000) t.kstnm = Stname[:7] t.kcmpnm = Component t.IDEP = 4 # 4 = units of velocity (in Volts) # Dependent variable choices: # (1)unknown, # (2)displacement(nm), # (3)velocity(nm/sec), # (4)velocity(volts), # (5)nm/sec/sec t.kinst = "Velocity" # Instrument type t.knetwk = "LM" # Network designator t.kuser0 = "Nanometr" # Place the system of units into the user text field 0 # t.WriteSacBinary(outfile) with open(outfile,'wb') as sacfile: t.write(sacfile) print " File successfully written: {0}".format(outfile) sacfile.close() st=read(outfile) st.write(seedfile,format="mseed") print " File successfully written: {0}".format(seedfile) print "File written to {}".format(outfile) else: print "Useage: PNE2SAC infile.txt (outfile.asc)" print "Or, PNE2SAC target_directory target_extension(like .txt)" print "No infile or directory specified." print len(sys.argv)
def csv2sac(infile, cconstant): # Channel = ["", "", "", ""] units = ['Counts ', 'Counts ', 'Counts ', 'Counts '] comment = ['Velocity', 'Velocity', 'Velocity', 'Velocity'] (header, stack) = load(infile) first_sample, medsps = timingvalidate(stack) if first_sample <> 0: print "\n\nA timing error exists in the 1st sample of the 1st record in this DAT series.\n" print "Remove the first DAT file in the folder and try again." sys.exit() # print "The first sample shows instantaneous sample rate of {} S/second.".format(medsps) # datetime = stack[0][13]+","+stack[0][14] # Frac_second = float(stack[0][17]) datetime = stack[0][15] + "," + stack[0][ 16] # New cs4 format the fields are offset by two more columns Frac_second = float(stack[0][17]) St_time = time.strptime(datetime, "%Y/%m/%d,%H:%M:%S") stdate = str(St_time.tm_year) + "_" + str(St_time.tm_mon) + "_" + str( St_time.tm_mday) stmin = str(St_time.tm_hour) + "_" + str(St_time.tm_min) + "_" + str( St_time.tm_sec) + "_" + str(int(Frac_second * 1000)) Filetime = stdate + "_" + stmin + "_" Station = cconstant[0] Network = cconstant[15] # File naming convention: YYYY.DDD.HH.MM.SS.SSS.NN.STATION.CHANNEL seedfil = infile[0:string.rfind( infile, '\\')] + "/" + Filetime + Network + "_" + Station sacfil = infile[0:string.rfind(infile, '.')] for i in range(0, 4): Channel[i] = cconstant[(2 * i) + 1] Samplecount = len(stack) print "Sample count stands at {} samples.".format(Samplecount) Delta = 1.0 / float(getsps(stack)) # Delta = 1/medsps print "Delta = {0:.8f}, Sample rate = {1:.8f}".format(Delta, 1 / Delta) # # stack[1] = channel 1 time history # . # # stack[4] = channel 4 time history # for i in range(0, 4): # Build each channel b = np.arange( len(stack), dtype=np.float32) # Establishes the size of the datastream for n in range(len(stack)): # Load the array with time-history data b[n] = np.float32( stack[n][i + 1]) # Convert the measurement from counts to volts. t = SACTrace(data=b) # set the SAC header values t.scale = 1.0 # Set the scale for each channel for use with DIMAS software t.delta = Delta t.nzyear = St_time.tm_year t.nzjday = St_time.tm_yday t.nzhour = St_time.tm_hour t.nzmin = St_time.tm_min t.nzsec = St_time.tm_sec t.nzmsec = int((Frac_second) * 1000) t.kstnm = Station t.kcmpnm = Channel[i] t.IDEP = 4 # 4 = units of velocity (in Volts) # Dependent variable choices: (1)unknown, (2)displacement(nm), # (3)velocity(nm/sec), (4)velocity(volts), # (5)nm/sec/sec t.kinst = comment[i - 1] # Instrument type t.knetwk = Network # Network designator t.kuser0 = units[ i - 1] # Place the system of units into the user text field 0 out = sacfil + "_{}.sac".format(Channel[i]) seed = seedfil + "_{}.mseed".format(Channel[i]) if Channel[ i] != "UNK": # We do not write streams in which the channel name is UNK with open(out, 'wb') as sacfile: t.write(sacfile) print " File successfully written: {0}".format(out) sacfile.close() st = read(out) st.write(seed, format="mseed") print " File successfully written: {0}".format(seed) # subprocess.call(["del",f],shell=True) # for i in range(0,1): # Build special channel for timing b = np.arange(len(stack), dtype=np.float32) # Establishes the size of the datastream for n in range(len(stack)): # Load the array with time-history data b[n] = np.float32(stack[n][13]) # Get the timing value. t = SACTrace(data=b) # set the SAC header values t.scale = 1.0 # Set the scale for each channel. This one is important to declare. t.delta = Delta t.nzyear = St_time.tm_year t.nzjday = St_time.tm_yday t.nzhour = St_time.tm_hour t.nzmin = St_time.tm_min t.nzsec = St_time.tm_sec t.nzmsec = int((Frac_second) * 1000) t.kstnm = Station t.kcmpnm = 'GPS' # This is a GPS timing signal. t.IDEP = 1 # 4 = units of velocity (in Volts) # Dependent variable choices: (1)unknown, (2)displacement(nm), # (3)velocity(nm/sec), (4)velocity(volts), # (5)nm/sec/sec t.kinst = 'GPS' # Instrument type t.knetwk = Network # Network designator t.kuser0 = 'digital' # Place the system of units into the user text field 0 out = sacfil + "_{}.sac".format('GPS') with open(out, 'wb') as sacfile: t.write(sacfile) # print " File successfully written: {}.sac".format(out) # print "Published sample rate in sac file = {}".format(t.stats.sampling_rate) sacfile.close()
def csv2sac(infile,cconstant): # Channel = ["","","",""] units = ['Counts ','Counts ','Counts ','Counts '] comment = ['Velocity','Velocity','Velocity','Velocity'] (header,stack) = load(infile) first_sample,medsps = timingvalidate(stack) if first_sample <> 0: print "\n\nA timing error exists in the 1st sample of the 1st record in this DAT series.\n" print "Remove the first DAT file in the folder and try again." sys.exit() # print "The first sample shows instantaneous sample rate of {} S/second.".format(medsps) # datetime = stack[0][13]+","+stack[0][14] # Frac_second = float(stack[0][17]) datetime = stack[0][15]+","+stack[0][16] # New cs4 format the fields are offset by two more columns Frac_second = float(stack[0][17]) St_time = time.strptime(datetime,"%Y/%m/%d,%H:%M:%S") stdate = str(St_time.tm_year)+"_"+str(St_time.tm_mon)+"_"+str(St_time.tm_mday) stmin = str(St_time.tm_hour)+"_"+str(St_time.tm_min)+"_"+str(St_time.tm_sec)+"_"+str(int(Frac_second*1000)) Filetime = stdate+"_"+stmin+"_" Station = cconstant[0] Network = cconstant[15] # File naming convention: YYYY.DDD.HH.MM.SS.SSS.NN.STATION.CHANNEL seedfil = infile[0:string.rfind(infile,'\\')]+"/"+Filetime+Network+"_"+Station sacfil = infile[0:string.rfind(infile,'.')] for i in range(0,4): Channel[i]=cconstant[(2*i)+1] Samplecount = len(stack) print "Sample count stands at {} samples.".format(Samplecount) Delta = 1.0/float(getsps(stack)) # Delta = 1/medsps print "Delta = {0:.8f}, Sample rate = {1:.8f}".format(Delta,1/Delta) # # stack[1] = channel 1 time history # . # # stack[4] = channel 4 time history # for i in range(0,4): # Build each channel b = np.arange(len(stack),dtype=np.float32) # Establishes the size of the datastream for n in range(len(stack)): # Load the array with time-history data b[n] = np.float32(stack[n][i+1]) # Convert the measurement from counts to volts. t = SACTrace(data = b) # set the SAC header values t.scale=1.0 # Set the scale for each channel for use with DIMAS software t.delta=Delta t.nzyear=St_time.tm_year t.nzjday=St_time.tm_yday t.nzhour=St_time.tm_hour t.nzmin=St_time.tm_min t.nzsec=St_time.tm_sec t.nzmsec=int((Frac_second)*1000) t.kstnm=Station t.kcmpnm=Channel[i] t.IDEP=4 # 4 = units of velocity (in Volts) # Dependent variable choices: (1)unknown, (2)displacement(nm), # (3)velocity(nm/sec), (4)velocity(volts), # (5)nm/sec/sec t.kinst=comment[i-1] # Instrument type t.knetwk=Network # Network designator t.kuser0=units[i-1] # Place the system of units into the user text field 0 out = sacfil+"_{}.sac".format(Channel[i]) seed = seedfil+"_{}.mseed".format(Channel[i]) if Channel[i] !="UNK": # We do not write streams in which the channel name is UNK with open(out,'wb') as sacfile: t.write(sacfile) print " File successfully written: {0}".format(out) sacfile.close() st=read(out) st.write(seed,format="mseed") print " File successfully written: {0}".format(seed) # subprocess.call(["del",f],shell=True) # for i in range(0,1): # Build special channel for timing b = np.arange(len(stack),dtype=np.float32) # Establishes the size of the datastream for n in range(len(stack)): # Load the array with time-history data b[n] = np.float32(stack[n][13]) # Get the timing value. t = SACTrace(data = b) # set the SAC header values t.scale=1.0 # Set the scale for each channel. This one is important to declare. t.delta=Delta t.nzyear=St_time.tm_year t.nzjday=St_time.tm_yday t.nzhour=St_time.tm_hour t.nzmin=St_time.tm_min t.nzsec= St_time.tm_sec t.nzmsec= int((Frac_second)*1000) t.kstnm=Station t.kcmpnm='GPS' # This is a GPS timing signal. t.IDEP=1 # 4 = units of velocity (in Volts) # Dependent variable choices: (1)unknown, (2)displacement(nm), # (3)velocity(nm/sec), (4)velocity(volts), # (5)nm/sec/sec t.kinst='GPS' # Instrument type t.knetwk=Network # Network designator t.kuser0='digital' # Place the system of units into the user text field 0 out = sacfil+"_{}.sac".format('GPS') with open(out,'wb') as sacfile: t.write(sacfile) # print " File successfully written: {}.sac".format(out) # print "Published sample rate in sac file = {}".format(t.stats.sampling_rate) sacfile.close()