cstr='Comb'+start[0:2]+'to'+stop[0:2]+'d'+str(d) sstr=start[0:2]+'to'+stop[0:2]+'d'+str(d) #make database db=h5py.File(os.path.join(dirpath,'TimeSeriesDB.hdf5')) #make a subgroup for the day sgdb=db.create_group(day) #create data set in the subgroup dset=sgdb.create_dataset('TS',(ns,na)) #load the timeseries into the database for ii,station in enumerate(slst): print 'Loading '+station for jj,comp in enumerate(complst): try: dset[ii*nc+jj,:]=np.loadtxt(os.path.join(dirpath,station,day,cstr, station+sstr+'.'+comp)) print '\t Loaded '+comp except IOError: cfile,clst=mt.combineFewFiles(os.path.join(dirpath,station,day), station,start,stop,crate, complst=[comp],d=d) dset[ii*nc+jj,:]=np.loadtxt(cfile[0]) db.close()
#for 'pb01' would be (0,4) stationid=(0,4) s1,s2=stationid[0],stationid[1] #number of frequencies to read from high frequency data, starting from #high frequencies down n1=12 #number of frequencies to read from low frequency data, starting from #low frequencies and going up n2=12 #make a new list to put the new edi file names into nedilst=[] for edihf in os.listdir(edipath1): cedilst=[] for edibb in os.listdir(edipath2): if edihf[s1:s2]==edibb[s1:s2]: cedilst.append(os.path.join(edipath1,edihf)) cedilst.append(os.path.join(edipath2,edibb)) break nedi=mt.combineEdifiles(cedilst[0],cedilst[1],n1,n2) nedilst.append(nedi) #if you want to plot the new edi file to see if the frequencies match #use (uncomment the following for a loop #for ii,nedi in enumerate(nedilst,1): # mtplot.plotResPhase(nedi,fignum=ii) #or for just one find the index in edilst and use #mtplot.plotResPhase(nedilst[index])
def removeDistortion(self): """removeDistortion(self) will remove the distortion from the impedance tensor as prescribed by Bibby et al. [2005]. Saves the edifile as station+dr.edi. Returns saved filename with full path.""" zlst=np.array(self.zlst) zvar=np.array(self.zvar) #calculate ellipticity to figure out the distortion from 1d structure ptdict=Z.getPhaseTensor(self) zd=[] #rotated identity matrix for static shift removal im=np.array([[0,-1],[1,0]]) for ii,ellip in enumerate(ptdict['ellipticity']): if ellip<.1: X=zlst[ii].real Y=zlst[ii].imag #calculate static shift factor gx=np.sqrt(abs(np.linalg.det(X))) gy=np.sqrt(abs(np.linalg.det(Y))) #remove static shift from real and imaginary parts #need to use the dot for matric multiplication Xs=np.dot(X,im)/gx Ys=np.dot(Y,im)/gy #append real and imaginary parts sequentially zd.append(Xs) zd.append(Ys) else: pass if len(zd)==0: print 'There is no 1D structure for this station' else: #estimate distortion matrix zd=np.array(zd) D=np.array([[zd[:,0,0].mean(),zd[:,0,1].mean()], [zd[:,1,0].mean(),zd[:,1,1].mean()]]) Dvar=np.array([[zd[:,0,0].std(),zd[:,0,1].std()], [zd[:,1,0].std(),zd[:,1,1].std()]]) Dinv=np.linalg.inv(D) #remove distortion as (D^-1)Z[ii] zdr=np.array([np.dot(np.linalg.inv(D),zlst[ii]) for ii in range(len(zlst))]) #estimate errors zvardr=np.zeros_like(zlst) for jj in range(len(zlst)): X=zlst[jj].real Xvar=zvar[jj].real zvardr[jj,0,0]=np.sqrt((Dinv[0,0]*X[0,0]* np.sqrt((Dvar[1,1]/Dinv[0,0])**2+ (Xvar[0,0]/X[0,0])**2))**2+(Dinv[0,1]*X[1,0] *np.sqrt((Dvar[0,1]/Dinv[0,1])**2+ (Xvar[0,1]/X[0,1])**2))**2) zvardr[jj,0,1]=np.sqrt((Dinv[0,0]*X[0,1]* np.sqrt((Dvar[1,1]/Dinv[0,0])**2+ (Xvar[0,1]/X[0,1])**2))**2+(Dinv[0,1]*X[1,1] *np.sqrt((Dvar[0,1]/Dinv[0,1])**2+ (Xvar[1,1]/X[1,1])**2))**2) zvardr[jj,1,0]=np.sqrt((Dinv[1,0]*X[0,0]* np.sqrt((Dvar[1,0]/Dinv[1,0])**2+ (Xvar[0,0]/X[0,0])**2))**2+(Dinv[1,1]*X[1,0] *np.sqrt((Dvar[0,0]/Dinv[1,1])**2+ (Xvar[1,0]/X[1,0])**2))**2) zvardr[jj,1,1]=np.sqrt((Dinv[1,0]*X[1,0]* np.sqrt((Dvar[1,0]/Dinv[1,0])**2+ (Xvar[0,1]/X[0,1])**2))**2+(Dinv[1,1]*X[1,1] *np.sqrt((Dvar[1,1]/Dinv[1,1])**2+ (Xvar[1,1]/X[1,1])**2))**2) #make new edi file. need to flip zdr and zvardr back to normal order #for edi file. newedifn=mt.rewriteedi(self.filename,zdr, zvardr.real) return newedifn
#for 'pb01' would be (0,4) stationid = (0, 4) s1, s2 = stationid[0], stationid[1] #number of frequencies to read from high frequency data, starting from #high frequencies down n1 = 12 #number of frequencies to read from low frequency data, starting from #low frequencies and going up n2 = 12 #make a new list to put the new edi file names into nedilst = [] for edihf in os.listdir(edipath1): cedilst = [] for edibb in os.listdir(edipath2): if edihf[s1:s2] == edibb[s1:s2]: cedilst.append(os.path.join(edipath1, edihf)) cedilst.append(os.path.join(edipath2, edibb)) break nedi = mt.combineEdifiles(cedilst[0], cedilst[1], n1, n2) nedilst.append(nedi) #if you want to plot the new edi file to see if the frequencies match #use (uncomment the following for a loop #for ii,nedi in enumerate(nedilst,1): # mtplot.plotResPhase(nedi,fignum=ii) #or for just one find the index in edilst and use #mtplot.plotResPhase(nedilst[index])
def __init__(self,impvar): if type(impvar) is str: if impvar[-3:].find('imp')>=0: self.filename=impvar self.station,self.periodlst,self.zlst=mt.readimp(impvar) self.lat='NaN' self.lon='NaN' self.zvar=np.zeros_like(self.zlst) self.tip=np.zeros((len(self.zlst),2)) self.tipvar=np.zeros_like(self.tip) elif impvar[-3:].find('edi')>=0: self.filename=impvar self.station=os.path.basename(impvar)[:-4] impdict=mt.readedi(impvar) self.lat=impdict['lat'] self.lon=impdict['lon'] #print impdict['frequency'] #flip things around to order from short period to long period if impdict['frequency'][0]<impdict['frequency'][-1]: self.freqlst=impdict['frequency'][::-1] self.periodlst=1/impdict['frequency'][::-1] self.zlst=impdict['z'][::-1,:,:] self.zvar=impdict['zvar'][::-1,:,:] self.tip=impdict['tipper'][::-1,:] self.tipvar=impdict['tippervar'][::-1,:] print 'Flipped arrays so frequency is decending' else: self.freqlst=impdict['frequency'] self.periodlst=1/impdict['frequency'] self.zlst=impdict['z'] self.zvar=impdict['zvar'] self.tip=impdict['tipper'] self.tipvar=impdict['tippervar'] print 'Opened impedance file: '+impvar else: try: impvar['station'] except KeyError: impvar['station']='n/a' try: impvar['lat'] except KeyError: impvar['lat']='NaN' try: impvar['lon'] except KeyError: impvar['lon']='NaN' try: impvar['periodlst'] except KeyError: impvar['periodlst']=range(len(impvar['zlst'])) try: impvar['zvar'] except KeyError: impvar['zvar']=np.zeros_like(impvar['zlst']) try: impvar['tip'] except KeyError: impvar['tip']=np.zeros((len(impvar['zlst']),2)) try: impvar['tipvar'] except KeyError: impvar['tipvar']=np.zeros((len(impvar['zlst']),2)) self.station=impvar['station'] self.lat=impvar['lat'] self.lon=impvar['lon'] self.periodlst=impvar['periodlst'] self.zlst=impvar['zlst'] self.zvar=impvar['zvar'] self.tip=impvar['tip'] self.tipvar=impvar['tipvar'] print 'Impedance tensor read.'