示例#1
0
    def build_data(self):
        """
        create data to write to an input file
        
        """

        # read edi file to edi object
        eo = mtedi.Edi()
        eo.readfile(self.edipath)
        self.edi_object = eo

        # define z
        zr = np.real(eo.Z.z)
        # sign of imaginary component needs to be reversed for the pek1d inversion code
        zi = -np.imag(eo.Z.z)
        ze = eo.Z.zerr
        z = zr + 1j * zi

        # set errorfloors
        if type(self.errorfloor) in [int, float]:
            self.errorfloor = np.ones([2, 2]) * self.errorfloor

        if self.errorfloor_type in ['relative', 'offdiagonals']:
            zer = ze / np.abs(z)
            for i in range(2):
                for j in range(2):
                    zer[:, i, j][(zer[:, i, j] <
                                  self.errorfloor[i, j])] = self.errorfloor[i,
                                                                            j]
            ze = np.abs(z) * zer
            if self.errorfloor_type == 'offdiagonals':
                for i in range(2):
                    for iz in range(len(z)):
                        if ze[iz, i, i] < ze[iz, i, 1 - i]:
                            ze[iz, i, i] = ze[iz, i, 1 - i]

        elif self.errorfloor_type == 'absolute':
            for i in range(2):
                for j in range(2):
                    ze[:, i, j][(ze[:, i, j] <
                                 self.errorfloor[i, j])] = self.errorfloor[i,
                                                                           j]

        # define header info for data file
        header = '{:>5}\n{:>5}'.format(self.mode, len(eo.Z.resistivity))

        # create data array
        data_list = [1. / eo.freq]
        for i in range(2):
            for j in range(2):
                if self.mode == 'I':
                    dd = [zr, ze, zi, ze]

                for d in dd:
                    data_list.append(d[:, i, j])

        self.header = header
        self.data = np.vstack(data_list).T
        self.z = zr + 1j * zi
        self.zerr = ze
示例#2
0
文件: mt.py 项目: schoolhui/mtpy
 def _read_edi_file(self):
     """
     read in edi file and set attributes accordingly
     
     """
     
     self.edi_object = MTedi.Edi(self.fn, datatype=self._data_type)
     self.lat = self.edi_object.lat
     self.lon = self.edi_object.lon
     self.elev = self.edi_object.elev
     self.Z = self.edi_object.Z
     self.Tipper = self.edi_object.Tipper
     self.station = self.edi_object.station
     
     #--> get utm coordinates from lat and lon        
     self._get_utm()
     
     #--> make sure things are ordered from high frequency to low
     self._check_freq_order()
     
     #--> compute phase tensor
     self.pt = MTpt.PhaseTensor(z_object=self.Z, freq=self.Z.freq)
     
     #--> compute invariants 
     self.zinv = MTinv.Zinvariants(z_object=self.Z)
示例#3
0
def convert2columns(fn):

    try:
        e_object = MTedi.Edi(filename=fn)
    except:
        print('invalid EDI file')
        raise

    filebase = op.splitext(fn)[0]
    outfn = filebase + '.columns'
    outstring = ''
    for i, f in enumerate(e_object.freq):
        outstring += '{0:> 10.5f}  '.format(f)
        for j in range(2):
            for k in range(2):
                outstring += '\t{0:.4f}  {1:.4f}  {2:.4f}  '.format(float(np.real(e_object.Z.z[i, j, k])),
                                                                    float(np.imag(e_object.Z.z[i, j, k])), e_object.Z.z_err[i, j, k])
        outstring += '\n'

    with open(outfn, 'w') as F:
        F.write(outstring)

    print('\tWritten data to file: {0}\n'.format(outfn))

    return outfn
示例#4
0
文件: mt_old.py 项目: zy911k24/mtpy
    def __init__(self, fn=None, **kwargs):

        self._fn = fn
        self.station = kwargs.pop('station', None)
        self._lat = kwargs.pop('lat', None)
        self._lon = kwargs.pop('lon', None)
        self._elev = kwargs.pop('elev', None)
        self._Z = kwargs.pop('Z', MTz.Z())
        self._Tipper = kwargs.pop('Tipper', MTz.Tipper())
        self._utm_zone = kwargs.pop('utm_zone', None)
        self._east = kwargs.pop('east', None)
        self._north = kwargs.pop('north', None)
        self._rotation_angle = kwargs.pop('rotation_angle', 0)

        self.edi_object = MTedi.Edi()
        self.pt = None
        self.zinv = None
        self._utm_ellipsoid = 23

        #provide key words to fill values if an edi file does not exist
        for key in kwargs.keys():
            setattr(self, key, kwargs[key])

        #--> read in the file name given
        if self._fn is not None:
            self._set_fn(fn)
示例#5
0
def main():

    if len(sys.argv) < 4:
        print '\nNeed at least 2 arguments: <EDI file> '\
            '<output directory> <#iterations> \n\n'\
            'Optional arguments: \n [sigma scaling]\n'\
            ' [batch process flag "-b"] \n\n'
        return

    try:
        fn_in = sys.argv[1]
        fn_in = op.join(op.abspath(os.curdir), fn_in)
        edi_object = MTedi.Edi(filename=fn_in)
    except:
        print '\n\tERROR - File is not a valid EDI file: {0}\n'.format(fn_in)
        sys.exit()

    try:
        outdir = sys.argv[2]
        outdir = op.join(op.abspath(os.curdir), outdir)
        if not op.isdir(outdir):
            os.makedirs(outdir)
    except:
        print '\n\tERROR - Output directory does not exist and cannot be'\
            ' generated: {0}\n'.format(outdir)
        sys.exit()

    try:
        n_iterations = int(float(sys.argv[3]))
        if n_iterations < 0:
            raise
    except:
        print '\n\t ERROR - number of iterations must be a positive integer (incl. 0)\n'
        sys.exit()

    fn_out = None
    sigma_scaling = 1
    if len(sys.argv) > 4:
        try:
            sigma_scaling = float(sys.argv[4])
            if sigma_scaling <= 0:
                raise
        except:
            sigma_scaling = 1
            print '\nWARNING - Invalid sigma scale ..using 1 instead\n'

    try:
        print
        print 'Generating PTcross input data file'
        if n_iterations != 0:
            print '(evaluating {0} realisations of Z)'.format(n_iterations)
        print '\t...'

        generate_ptcrossdata_file(edi_object, n_iterations, sigma_scaling,
                                  outdir, fn_out)
    except:
        raise
        print '\n\tERROR - could not generate PT Cross Data file - check EDI file!\n'
def get_station_xy(edipath, savepath=None, basename='stationxy.dat'):
    edilst = [ff for ff in os.listdir(edipath) if ff.endswith('.edi')]
    xylst = []
    for edifile in edilst:
        eo = mtedi.Edi(op.join(edipath, edifile))
        xylst.append([eo.station, eo.lon, eo.lat])
    with open(op.join(savepath, basename), 'w') as xyfile:
        xyfile.write('station x y\n')
        xyfile.writelines(['{} {} {}\n'.format(*line) for line in xylst])
示例#7
0
    def read_edifiles(self):
        """
        """

        self.find_edifiles()

        if len(self.edifiles) > 0:
            self.edi_objects = [mtedi.Edi(filename=efile) for efile in
                                list(self.edifiles.values())]
def convert_edi_coordinates_to_kml_file(edi_filelist, outfilename=None):

    kml_string = ''
    kml_string += '<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n'
    kml_string += '<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n'
    kml_string += '<Document>\n'

    for edi_idx, edi in enumerate(edi_filelist):

        e = EDI.Edi()
        e.readfile(edi)
        lat = e.lat
        lon = e.lon
        ele = e.elev
        station = e.head['dataid']

        kml = []

        description = 'File: {0}'.format(e.filename)

        kml.append('  <Placemark>')
        kml.append('    <name>%s</name>' % station)
        kml.append('    <description>')
        kml.append('        <p>%s</p>' % description)
        kml.append('      </description>')
        kml.append('    <Point>')
        kml.append('      <coordinates>%f,%f,%f</coordinates>' %
                   (lon, lat, ele))
        kml.append('    </Point>')
        kml.append('  </Placemark>')

        kml_string += '\n'.join(kml)

    kml_string += '\n</Document>\n'
    kml_string += '</kml>\n'

    if outfilename is None:
        outfilename = op.abspath('edi_coordinates.kml')
    else:
        try:
            outfilename = op.abspath(op.join('.', outfilename))
        except:
            outfilename = op.abspath('edi_coordinates.kml')

    outfilename = MTfh.make_unique_filename(outfilename)

    fileObj = open(outfilename, 'w')
    fileObj.write(kml_string)
    fileObj.close()

    return outfilename
示例#9
0
def main():

    if len(sys.argv) < 3:
        print '\nNeed at least 2 arguments: <EDI file> '\
                        '<output directory> \n\n'\
                        'Optional arguments: \n [output filename]\n'\
                        ' [batch process flag "-b"] \n\n'
        return

    try:
        fn_in = sys.argv[1]
        fn_in = op.join(op.abspath(os.curdir), fn_in)
        edi_object = MTedi.Edi(filename=fn_in)
    except:
        print '\n\tERROR - File is not a valid EDI file: {0}\n'.format(fn_in)
        sys.exit()

    try:
        outdir = sys.argv[2]
        outdir = op.join(op.abspath(os.curdir), outdir)
        if not op.isdir(outdir):
            os.makedirs(outdir)
    except:
        print '\n\tERROR - Output directory does not exist and cannot be'\
                                            ' generated: {0}\n'.format(outdir)
        sys.exit()

    fn_out = None
    if len(sys.argv) > 3:
        try:
            fn_out = sys.argv[3]
            fn_out = op.join(outdir, fn_out)
        except:
            fn_out = None
            print '\nWARNING - Invalid output filename...using default instead\n'

    try:
        generate_ptwiperdata_file(edi_object, outdir, fn_out)
    except:
        print '\n\tERROR - could not generate PT Cross Data file - check EDI file!\n'
示例#10
0
def test_func():
    # path to edis
    epath = EDI_DATA_DIR2

    svdir = TEMP_OUT_DIR

    elst = [
        op.join(epath, edi) for edi in os.listdir(epath)
        if (edi.endswith('.edi'))
    ]

    for efile in elst[-1:]:
        eo = mtedi.Edi(efile)
        pr = mtpr.PlotResponse(fn=efile,
                               plot_num=2,
                               plot_tipper='yri',
                               plot_pt='y')

        figfile = os.path.join(svdir, os.path.basename(efile)[:-4] + '.png')
        pr.save_plot(figfile)

        assert (os.path.exists(figfile))
#==============================================================================
if not os.path.isfile(pkfn):
    azimutharr = np.zeros((nf, ns))
    phimaxarr = np.zeros((nf, ns))
    phiminarr = np.zeros((nf, ns))
    betaarr = np.zeros((nf, ns))
    colorarr = np.zeros((nf, ns))

    latlst = np.zeros(ns)
    lonlst = np.zeros(ns)

    stationlst = []

    for ss, station in enumerate(edilst):
        #make a data type Z
        edi1 = mtedi.Edi(station[0])
        edi2 = mtedi.Edi(station[1])

        pt1 = mtpt.PhaseTensor(z_object=edi1.Z)
        pt2 = mtpt.PhaseTensor(z_object=edi2.Z)

        stationlst.append(edi1.station)

        sz, se, sn = utm2ll.LLtoUTM(refe, edi1.lat, edi1.lon)
        latlst[ss] = (sn - bhn) / 1000.
        lonlst[ss] = (se - bhe) / 1000.

        #calculate the difference between the two phase tensor ellipses
        for ii in range(nf):
            phi = np.eye(2) - (np.dot(np.linalg.inv(pt1.pt[ii]), pt2.pt[ii]))
示例#12
0
z_target = 50000
n_layers = 50
res_err = 30
phase_err = 2.5
max_iter = 20

#==============================================================================
# loop over each edi inverting for TE and TM modes
#==============================================================================
edi_list = [os.path.join(edi_path, edi) for edi in os.listdir(edi_path)
            if edi.find('.edi')>0]

log_fid = file(os.path.join(save_path, 'Occam1DLog.log'), 'w')

for edi in edi_list:
    e1 = mtedi.Edi(edi)
    
    #--> write occam1d data file
    ocd = occam1d.Data()
    if not os.path.exists(os.path.join(save_path, e1.station)):
        os.mkdir(os.path.join(save_path, e1.station))
        
    #--> write occam1d model file
    ocm = occam1d.Model()
    ocm.n_layers = n_layers
    ocm.bottom_layer = z_depth
    ocm.z1_layer = z1_thickness
    ocm.target_depth = z_target
    
    #--> write occam1d startup files
    ocs = occam1d.Startup()    
示例#13
0
def makemap(edilist, mapstretchfactor, symbolsize, labelsize, showlabel):

    #import of modules here due to warnings from Matplotlib packages
    #these warnings distract the 'usage' information
    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    import mtpy.utils.filehandling as MTfh
    import mtpy.core.edi as EDI
    import mtpy.utils.filehandling as MTfh

    lats = []
    lons = []
    names = []

    for i in edilist:
        e = EDI.Edi()
        e.readfile(i)
        lats.append(e.lat)
        lons.append(e.lon)
        names.append(e.head['dataid'].lower())

    coords = zeros((len(edilist), 2))
    coords[:, 0] = lats
    coords[:, 1] = lons

    latrange = max(lats) - min(lats)
    lonrange = max(lons) - min(lons)

    #center point for projection:
    c = [mean(lats), mean(lons)]

    #-----------------------
    #Matplotlib options
    mpl.rcParams['font.size'] = 10.
    mpl.rcParams['axes.labelsize'] = 8.
    mpl.rcParams['xtick.labelsize'] = 6.
    mpl.rcParams['ytick.labelsize'] = 6.

    plt.close('all')

    fig = plt.figure()  #figsize=(5.7,4.3))
    plt.subplots_adjust(left=0.2,
                        right=0.9,
                        top=0.90,
                        bottom=0.1,
                        wspace=0.15,
                        hspace=0.05)
    ax = plt.subplot(111)

    stretch = float(mapstretchfactor)
    total_latmin = max((min(lats) - stretch * latrange), -90)
    total_lonmin = max((min(lons) - stretch * lonrange), -180)
    total_latmax = min((max(lats) + stretch * latrange), 90)
    total_lonmax = min((max(lons) + stretch * lonrange), 180)

    total_latrange = total_latmax - total_latmin
    total_lonrange = total_lonmax - total_lonmin

    #determine number of axes labels:
    maximumlabels = 5
    latnum = maximumlabels
    lonnum = maximumlabels
    lonlat_stretch = total_lonrange / total_latrange
    if int(lonlat_stretch) > 2:
        #significantly more long than lat
        factor = int(int(lonlat_stretch) / 2.)
        latnum = int(maximumlabels / factor) + 1
        lonnum = maximumlabels
    elif int(lonlat_stretch) < 0.5:
        #significantly more long than lat
        factor = int(int(1. / lonlat_stretch) / 2.)
        lonnum = int(maximumlabels / factor) + 1
        latnum = maximumlabels

    m = Basemap(projection='merc',
                lon_0=c[1],
                lat_0=c[0],
                lat_ts=c[0],
                llcrnrlat=total_latmin,
                urcrnrlat=total_latmax,
                llcrnrlon=total_lonmin,
                urcrnrlon=total_lonmax,
                rsphere=6371200.,
                resolution='h')  #,ax=ax)

    lons.append(total_lonmin)
    lons.append(total_lonmax)
    lats.append(total_latmin)
    lats.append(total_latmax)

    xgrid, ygrid = m(*meshgrid(lons, lats))
    xdiff = xgrid.max() - xgrid.min()
    ydiff = ygrid.max() - ygrid.min()

    largest_extent = max(ydiff, xdiff)

    m.drawcoastlines(linewidth=0.25, ax=ax)
    m.drawcountries(linewidth=0.25, ax=ax)
    m.fillcontinents(color='coral', lake_color='aqua', ax=ax)
    m.drawmapboundary(fill_color='aqua', ax=ax)

    m.drawparallels([
        round(i, 3)
        for i in linspace(total_latmin, total_latmax, latnum + 1, False)
    ][1:],
                    labels=[1, 0, 0, 0],
                    fmt='%.1f')
    m.drawmeridians([
        round(i, 3)
        for i in linspace(total_lonmin, total_lonmax, lonnum + 1, False)
    ][1:],
                    labels=[0, 0, 0, 1],
                    fmt='%.1f')
    m.drawrivers()
    m.etopo()

    m.drawmapscale(
        total_lonmax - 0.15 * total_lonrange,
        total_latmax - 0.2 * total_latrange,
        c[1],
        c[0],
        2 * 10**(int(log10(largest_extent / 1000.)) - 1),
        barstyle='simple',
        labelstyle='simple',
        fontsize=12,
        fontcolor='k',
        fillcolor1='r',
        fillcolor2='g',
        ax=None,
        format='%d',
    )

    for x, y, name in zip(xgrid[0, :], ygrid[:, 0], names):
        plt.plot(x, y, 'v', ms=symbolsize, color='k', label=name)
        if showlabel is True:
            plt.text(x,
                     y,
                     name,
                     fontsize=labelsize,
                     ha='center',
                     va='bottom',
                     color='k',
                     backgroundcolor='r')  #, labelfontsize=5)

    plt.title('locations of {0} MT stations'.format(len(names)))

    f1 = 'station_locations_map.png'
    f2 = 'station_locations_map.svg'

    f1 = MTfh.make_unique_filename(f1)
    f2 = MTfh.make_unique_filename(f2)

    plt.savefig(f1, format='png', dpi=200)
    plt.savefig(f2, format='svg', transparent=True)

    return op.abspath(f1), op.abspath(f2)
示例#14
0
def convert2edi(station,
                directory,
                survey_configfile,
                instrument_response_file,
                string2strip=None,
                datestring=None):

    basedir = op.abspath(os.curdir)
    #name of intermediate coherence file:
    infn_coh = '{0}.coh'.format(station.upper())
    directory = op.abspath(directory)
    os.chdir(directory)

    print
    #print station,directory, survey_configfile,None,instrument_response_file
    print directory

    if string2strip is not None:

        j_filename_list = [
            i for i in os.listdir(directory)
            if op.basename(i).upper().endswith('.j'.upper())
        ]
        j_filename_list = [
            i for i in j_filename_list
            if '{0}'.format(station.upper()) in op.basename(i).upper()
        ]

        j_file = j_filename_list[0]
        new_j_file = '%s.j' % (station.upper())
        shutil.move(j_file, new_j_file)
        print 'renamed j_file %s into %s' % (j_file, new_j_file)

        coh_filenames_list = [
            i for i in os.listdir(directory)
            if op.basename(i).upper().endswith('c2'.upper())
        ]
        for coh in coh_filenames_list:
            suffix2 = op.splitext(coh)[-1]
            suffix1 = op.splitext(op.splitext(coh)[-2])[-1]
            newcoh = '%s' % (station.upper()) + suffix1 + suffix2
            shutil.move(coh, newcoh)
            print 'renamed coh file %s into %s' % (coh, newcoh)

    edifn, cohfn = MTbp.convertbirrpoutput(station, directory,
                                           survey_configfile, None,
                                           instrument_response_file)
    if edifn is None:
        print
        try:
            os.remove(infn_coh)
        except:
            pass
        os.chdir(basedir)
        raise MTex.MTpyError_processing(
            '\tERROR - could not write EDI file - check j-file!\n')
        #sys.exit()

    #parse the folder, find the j-file used before (the first one matching the
    #specs), and determine the date from its name by stripping off the station names
    j_filename_list = [
        i for i in os.listdir(directory)
        if op.basename(i).upper().endswith('.j'.upper())
    ]
    j_filename_list = [
        i for i in j_filename_list
        if '{0}'.format(station.upper()) in op.basename(i).upper()
    ]

    j_file = j_filename_list[0].upper()
    #print j_file

    if datestring is not None:

        #try:
        day = int(float(datestring))
        day = int(float(datestring[-2:]))
        month_num = int(float(datestring[-4:-2]))
        year = int(float(datestring[-6:-4]))

        #except:
        #    datestring = None

    if datestring is None:
        dateinfo = j_file.replace('.J', '')
        dateinfo = dateinfo.replace(station.upper(), '')

        #TODO : automatise these two steps if possible...!!!
        #...maybe by agreeing on a common format for the date...??
        #dateinfo = dateinfo.replace('_RR_B125_','')
        if string2strip is not None:
            for i in string2strip:
                dateinfo = dateinfo.replace(i, '')
                dateinfo = dateinfo.replace(i.upper(), '')

        #split the date information
        dateinfo = dateinfo.split('-')

        try:
            day = int(float(dateinfo[0]))
            month = dateinfo[1].lower()
            month_num = {
                'jan': 1,
                'feb': 2,
                'mar': 3,
                'apr': 4,
                'may': 5,
                'jun': 6,
                'jul': 7,
                'aug': 8,
                'sep': 9,
                'oct': 10,
                'nov': 11,
                'dec': 12,
            }[month]
            year = 14

        except:
            try:
                datestring = int(float(dateinfo[0]))
                day = int(float(dateinfo[0][-2:]))
                month_num = int(float(dateinfo[0][-4:-2]))

                # month_num = {'jan':1,'feb':2,'mar':3,'apr':4,'may':5,'jun':6,
                #             'jul':7,'aug':8,'sep':9,'oct':10,'nov':11,'dec':12,}[month]
                year = 14

            except:
                day = 0
                month_num = 0
                year = 0

    # re read the edi file:
    e_object = MTedi.Edi(filename=edifn)
    e_object.head['loc'] = 'roma'
    e_object.head['acqby'] = 'UofA'
    e_object.head['acqdate'] = '2014/{0:02d}/{1:02d}'.format(month_num, day)

    outfn_base = '{0}_{1}_{2:02d}{3:02d}{4:02d}'.format(
        edi_prefix, station.upper(), year, month_num, day)

    outfn = outfn_base + '.edi'
    if 1:
        outfn_true = e_object.writefile(outfn,
                                        allow_overwrite=True,
                                        use_info_string=True)
        if outfn_true is None:
            raise
        print 'EDI file written: {0}\n'.format(outfn_true)
    # except:
    #     print '\tERROR - could not write final EDI file {0}\n'.format(outfn)

    outfn = outfn_true

    #rename coherence file accordingly:
    outfn_coh = outfn_base + '.coh'

    try:
        os.rename(infn_coh, outfn_coh)
    except:
        print 'Warning - could not find coherence file: {0}'.format(infn_coh)

    #rename j file, so that another one can be found and processed, if this code
    # is called within a loop:
    os.rename(j_filename_list[0], j_filename_list[0] + '.done')

    #remove intermediate EDI file
    os.remove(edifn)

    os.chdir(basedir)

    return outfn, outfn_coh
示例#15
0
# directory path where edi files are
dirpath = r"d:\Peacock\MTData\EDI_Files\Counts"

# path to save edi files that have distortion removed
drpath = os.path.join(dirpath, 'DR')

# make sure the drpath exists
if not os.path.exists(drpath):
    os.mkdir(drpath)

#make a list of all edi files in the directory
edi_list = [
    os.path.join(dirpath, edi) for edi in os.listdir(dirpath)
    if edi.find('.edi') > 0
]

#loop over edi files and remove distortion
dlst = []
for edi in edi_list:
    e1 = mtedi.Edi(filename=edi)
    d, zd = distortion.remove_distortion(z_object=e1.Z)
    e1.Z = zd
    e1.writefile(os.path.join(dirpath, 'DR', e1.station.lower()))
    dlst.append({'station': e1.station, 'd': d})

#print results for easy viewing
for dd in dlst:
    print '--> Distortion tensor for {0} is:'.format(dd['station'])
    print '{0}|{1: .2f} {2: .2f}|'.format('' * 5, dd['d'][0, 0], dd['d'][0, 1])
    print '{0}|{1: .2f} {2: .2f}|'.format('' * 5, dd['d'][1, 0], dd['d'][1, 1])
示例#16
0
文件: mt.py 项目: schoolhui/mtpy
    def __init__(self, fn=None, **kwargs):
        
        self._fn = fn
        self.station = kwargs.pop('station', None)
        self._lat = kwargs.pop('lat', None)
        self._lon = kwargs.pop('lon', None)
        self._elev = kwargs.pop('elev', None)
        self._Z = kwargs.pop('Z', MTz.Z())
        self._Tipper = kwargs.pop('Tipper', MTz.Tipper())
        self._utm_zone = kwargs.pop('utm_zone', None)
        self._east = kwargs.pop('east', None)
        self._north = kwargs.pop('north', None)
        self._rotation_angle = kwargs.pop('rotation_angle', 0)
        self._data_type = kwargs.pop('data_type', 'z')
        
        #provide key words to fill values if an edi file does not exist
        if 'z_object' in kwargs:
            self._Z = kwargs['z_object']
            
        if 'z_array' in kwargs:
            self._Z.z = kwargs['z_array']
        
        if 'zerr_array' in kwargs:
            self._Z.zerr = kwargs['zerr_array']
        
        if 'freq' in kwargs:
            self._Z.freq = kwargs['freq']
            self._Tipper.freq = kwargs['freq']
            
        if 'tipper_object' in kwargs:
            self._Tipper = kwargs['tipper_object']
            
        if 'tipper' in kwargs:
            self._Tipper.tipper = kwargs['tipper']
        
        if 'tippererr' in kwargs:
            self._Tipper.tippererr = kwargs['tippererr']
            
        if 'resisitivity' in kwargs:
            self._Z.resistivity = kwargs['resistivity']
        
        if 'resisitivity_err' in kwargs:
            self._Z.resistivity_err = kwargs['resistivity_err']
        
        if 'phase' in kwargs:
            self._Z.phase = kwargs['phase']
            
        if 'phase_err' in kwargs:
            self._Z.phase = kwargs['phase_err']
        
        
        self.edi_object = MTedi.Edi()
        self.pt = None
        self.zinv = None
        self._utm_ellipsoid = 23

        #--> read in the edi file if its given
        if self._fn is not None:
            if self._fn[-3:] == 'edi':
                self._read_edi_file()
            else:
                not_fn = self._fn[os.path.basename(self._fn).find['.']:]
                raise MTex.MTpyError_file_handling('File '+\
                          'type {0} not supported yet.'.format(not_fn))
示例#17
0
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""

import sys
import matplotlib
#matplotlib.use('Qt4agg', force=True)
from pylab import *

import mtpy.core.edi as MTedi

fn = sys.argv[1]

edi = MTedi.Edi(edi_fn=fn)

try:
    edi.read_edi_file()
except Exception, why:
    print why
    sys.exit(1)

res_te = []
res_tm = []
phi_te = []
phi_tm = []
reserr_te = []
reserr_tm = []
phierr_te = []
phierr_tm = []
示例#18
0
@author: Alison Kirkby

plots edi files (res/phase vs period) for all edis in a directory and saves out as png files
"""

import os
os.chdir(r'C:\Git\mtpy')

import mtpy.imaging.plotresponse as mtpr
import mtpy.core.edi as mtedi
import os.path as op

# path to edis
epath = r'C:\Git\mtpy\examples\data\edi_files'

svdir = r'C:\Git\mtpy\examples\plots\edi_plots'

elst = [
    op.join(epath, edi) for edi in os.listdir(epath) if (edi.endswith('.edi'))
]

for efile in elst[-1:]:
    eo = mtedi.Edi(efile)
    pr = mtpr.PlotResponse(fn=efile,
                           plot_num=2,
                           plot_tipper='yri',
                           plot_pt='y')
    pr.save_plot(
        op.join(svdir, op.join(svdir,
                               op.basename(efile)[:-4] + '.png')))
        f_index += 1

    if f_index == nf:
        data_list.append({
            'name': name,
            'res': res,
            'phase': phase,
            'res_err': res_err,
            'phase_err': phase,
            'tipper': tipper,
            'tipper_err': tipper_err
        })
        print 'Station = {0}, no. freq = {1}'.format(name, nf)

        #write edi file
        data_edi = mtedi.Edi()
        data_z = mtz.Z()
        data_z.freq = frequency
        data_z.set_res_phase(res,
                             phase,
                             reserr_array=res_err,
                             phaseerr_array=phase_err)

        data_T = mtz.Tipper(tipper_array=tipper, tippererr_array=tipper_err)
        data_T.freq = frequency

        data_edi.Z = data_z
        data_edi.Tipper = data_T

        #---------------HEADER----------------------------------------------
        data_edi.head = dict([('dataid', name), ('acqby', 'P. E. Wannamaker'),
示例#20
0
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""

import os, sys
import os.path as op

import mtpy.core.edi as MTedi

from pylab import *

fn = sys.argv[1]

edi = MTedi.Edi()
try:
    edi.readfile(fn)
except:
    print '\n\tFile does not exist: {0}\n'.format(fn)
    sys.exit()

res_te = []
res_tm = []
phi_te = []
phi_tm = []
reserr_te = []
reserr_tm = []
phierr_te = []
phierr_tm = []
示例#21
0
def generate_input_file(edifilename, outputdir=None):

    eo = EDI.Edi()
    eo.readfile(edifilename)
    filebase = op.splitext(op.split(edifilename)[-1])[0]

    outfilename1 = '{0}_bayesian1d_z.in'.format(filebase)
    outfilename2 = '{0}_bayesian1d_zvar.in'.format(filebase)
    outdir = op.split(edifilename)[0]

    if outputdir is not None:
        try:
            if not op.isdir(outputdir):
                os.makedirs(outputdir)
                outdir = outputdir
        except:
            pass

    outfn1 = op.join(outdir, outfilename1)
    outfn2 = op.join(outdir, outfilename2)

    outfn1 = MTfh.make_unique_filename(outfn1)
    outfn2 = MTfh.make_unique_filename(outfn2)

    freqs = eo.freq

    z_array = eo.Z.z
    z_err_array = eo.Z.z_err

    if len(freqs) != len(z_array):
        raise MTex.MTpyError_edi_file('ERROR in Edi file {0} - number of '
                                      'freqs different from length of Z array'.format(eo.filename))

    sorting = np.argsort(freqs)

    outstring1 = ''
    outstring2 = ''

    for idx in sorting:
        z = z_array[idx]
        z_err = z_err_array[idx]
        f = freqs[idx]
        outstring1 += '{0}\t'.format(f)
        outstring2 += '{0}\t'.format(f)
        for i in np.arange(2):
            for j in np.arange(2):
                if np.imag(z[i % 2, (j + 1) / 2]) < 0:
                    z_string = '{0}-{1}i'.format(np.real(z[i % 2, (j + 1) / 2]),
                                                 np.abs(np.imag(z[i % 2, (j + 1) / 2])))
                else:
                    z_string = '{0}+{1}i'.format(np.real(z[i % 2, (j + 1) / 2]),
                                                 np.imag(z[i % 2, (j + 1) / 2]))

                z_err_string = '{0}'.format(z_err[i % 2, (j + 1) / 2])

                outstring1 += '{0}\t'.format(z_string)
                outstring2 += '{0}\t'.format(z_err_string)

        outstring1 = outstring1.rstrip() + '\n'
        outstring2 = outstring2.rstrip() + '\n'

    Fout1 = open(outfn1, 'w')
    Fout2 = open(outfn2, 'w')
    Fout1.write(outstring1.expandtabs(4))
    Fout2.write(outstring2.expandtabs(4))
    Fout1.close()
    Fout2.close()

    return outfn1, outfn2
        fn_inj = os.path.join(edipath_inj, station + '.edi')
        if os.path.isfile(fn_base) == False or os.path.isfile(fn_inj) == False:
            print station
            station_list.remove(station)

    ns = len(station_list)
    nf = 43  # number of frequencies

    z_base_arr = np.zeros((ns, nf, 2, 2), dtype='complex')
    z_inj_arr = np.zeros((ns, nf, 2, 2), dtype='complex')

    for ss, station in enumerate(station_list):
        fn_base = os.path.join(edipath_base, station + '.edi')
        fn_inj = os.path.join(edipath_inj, station + '.edi')
        if os.path.isfile(fn_base) == True and os.path.isfile(fn_inj) == True:
            edi_base = mtedi.Edi(fn_base)
            edi_inj = mtedi.Edi(fn_inj)
            z_err_base = np.sqrt(edi_base.Z.zerr.copy())
            z_err_inj = np.sqrt(edi_inj.Z.zerr.copy())

            #print z_err_base[0, 0, 1]
            #compute a static shift correction
            sx = np.sqrt(
                np.mean(edi_base.Z.resistivity[0:15, 0, 1] /
                        edi_inj.Z.resistivity[0:15, 0, 1]))
            sy = np.sqrt(
                np.mean(edi_base.Z.resistivity[0:15, 1, 0] /
                        edi_inj.Z.resistivity[0:15, 1, 0]))

            #remove the static shift from the injection survey
            s, new_z = edi_inj.Z.no_ss(1 / sx, 1 / sy)
示例#23
0
def plotedi(fn, saveplot=False, component=None):

    edi = MTedi.Edi()
    try:
        edi.readfile(fn)
    except:
        print '\n\tERROR - not a valid EDI file: {0}\n'.format(fn)
        sys.exit()

    if saveplot is True:
        import matplotlib
        matplotlib.use('Agg')

    from pylab import *

    lo_comps = []
    if component is not None:
        'n' in component.lower()
        try:
            if 'n' in component.lower():
                lo_comps.append('n')
        except:
            pass
        try:
            if 'e' in component.lower():
                lo_comps.append('e')
        except:
            pass
    if len(lo_comps) == 0:
        lo_comps = ['n', 'e']

    res_te = []
    res_tm = []
    phi_te = []
    phi_tm = []
    reserr_te = []
    reserr_tm = []
    phierr_te = []
    phierr_tm = []

    for r in edi.Z.resistivity:
        res_te.append(r[0, 1])
        res_tm.append(r[1, 0])

    for p in edi.Z.phase:
        phi_te.append(p[0, 1] % 90)
        phi_tm.append(p[1, 0] % 90)

    if np.mean(phi_te) > 90 and np.mean(phi_tm) > 90:
        phi_te = [i % 90 for i in phi_te]
        phi_tm = [i % 90 for i in phi_tm]

    for r in edi.Z.resistivity_err:
        reserr_te.append(r[0, 1])
        reserr_tm.append(r[1, 0])
    for p in edi.Z.phase_err:
        phierr_te.append(p[0, 1])
        phierr_tm.append(p[1, 0])

    periods = 1. / edi.freq

    resplotelement_xy = None
    resplotelement_yx = None

    ax1 = subplot(211)
    if 'n' in lo_comps:
        resplotelement_xy = errorbar(periods,
                                     res_te,
                                     reserr_te,
                                     marker='x',
                                     c='b',
                                     fmt='x')
    if 'e' in lo_comps:
        resplotelement_yx = errorbar(periods,
                                     res_tm,
                                     reserr_tm,
                                     marker='x',
                                     c='r',
                                     fmt='x')
    xscale('log')
    yscale('log')
    minval = min(min(res_te, res_tm))
    maxval = max(max(res_te, res_tm))
    xlim(0.5 * min(periods), 2 * max(periods))

    ylim([0.1, 100])
    #ylim([minval/10,maxval*10])

    autoscale(False)

    ylabel('app.res. in Ohm m')
    setp(ax1.get_xticklabels(), visible=False)
    ## share x only
    ax2 = subplot(212, sharex=ax1)
    autoscale(False)

    #ylim(-45,135)
    if 'n' in lo_comps:
        errorbar(periods, phi_te, phierr_te, marker='x', c='b', fmt='x')
    if 'e' in lo_comps:
        errorbar(periods, phi_tm, phierr_tm, marker='x', c='r', fmt='x')
    ylabel('phase')
    xlabel('period (in s)')
    plot([xlim()[0], xlim()[1]], [45, 45], '-.', c='0.7')
    ylim([-0, 90])

    ax1.legend([resplotelement_xy, resplotelement_yx],
               ['$E_{X}/B_Y$', '$E_Y/B_X$'],
               loc=2,
               ncol=1,
               numpoints=1,
               markerscale=0.8,
               frameon=True,
               labelspacing=0.3,
               prop={'size': 8},
               fancybox=True,
               shadow=False)

    tight_layout()
    if saveplot is True:

        ioff()
        outfn = op.splitext(fn)[0] + '.png'
        savefig(outfn, bbox_inches='tight')
        close('all')
        ion()
        return outfn

    else:
        ion()
        show(block=True)

        return None
示例#24
0
header_string = """#Data file \n# Period(s) Code GG_Lat GG_Lon X(m) Y(m) Z(m) Component Real Imag Error
> Full_Impedance \n> exp(-i\omega t)\n> [mV/km]/[nT]
> 0.00
"""

edilist = glob.glob(os.path.join(edipath, '*.[Ee][Dd][Ii]'))
edilist = [os.path.abspath(os.path.join(os.curdir, i)) for i in edilist]

lo_ediobjs = []
coord_list = []
utm_list = []

for edi in edilist:

    e = E.Edi()
    e.readfile(edi)

    lo_ediobjs.append(e)

    utm_list.append(conv.LLtoUTM(23, e.lat, e.lon))
    coord_list.append([e.lat, e.lon])

lat0 = np.mean(np.array(coord_list)[:, 0])
lon0 = np.mean(np.array(coord_list)[:, 1])
coord_list = []

# check f\or utm zones...if different assign zone!
# for the moment assume all have the same zone

for utm in utm_list:
示例#25
0
    if os.path.isfile(os.path.join(edipath1, station + ediext)) == True:
        imp1 = os.path.join(edipath1, station + ediext)
        implst.append(imp1)

    if os.path.isfile(os.path.join(edipath2, station + ediext)) == True:
        imp2 = os.path.join(edipath2, station + ediext)
        implst.append(imp2)
    if os.path.isfile(os.path.join(edipath3, station + ediext)) == True:
        imp3 = os.path.join(edipath3, station + ediext)
        implst.append(imp3)

    if implst == [] or len(implst) == 1:
        stationclst.remove(station)
        pass
    else:
        implst = [mtedi.Edi(implst[0]), mtedi.Edi(implst[1])]
        period = implst[0].period
        n = len(period)

        z1 = implst[0].Z.z
        z2 = implst[1].Z.z

        rpdict1 = implst[0].Z.resistivity
        rpdict2 = implst[1].Z.resistivity
        # compute static shift for each x and y component
        sx = np.sqrt(np.mean(rpdict1[0:10, 0, 1] / rpdict2[0:10, 0, 1]))
        sy = np.sqrt(np.mean(rpdict1[0:10, 1, 0] / rpdict2[0:10, 1, 0]))

        # remove static shift from 2nd set of files
        z2[:, 0, 0] = z2[:, 0, 0] * sx
        z2[:, 0, 1] = z2[:, 0, 1] * sx
示例#26
0
def plotedi(fn, saveplot=False, component=None):
    edi = MTedi.Edi()
    try:
        edi.readfile(fn)
    except:
        print '\n\tERROR - not a valid EDI file: {0}\n'.format(fn)
        sys.exit()

    # if saveplot is True:
    #     import matplotlib
    # matplotlib.use('Agg')

    import pylab

    lo_comps = []
    if component is not None:
        'n' in component.lower()
        try:
            if 'n' in component.lower():
                lo_comps.append('n')
        except:
            pass
        try:
            if 'e' in component.lower():
                lo_comps.append('e')
        except:
            pass
    if len(lo_comps) == 0:
        lo_comps = ['n', 'e']

    res_te = []
    res_tm = []
    phi_te = []
    phi_tm = []
    reserr_te = []
    reserr_tm = []
    phierr_te = []
    phierr_tm = []

    for r in edi.Z.resistivity:
        res_te.append(r[0, 1])
        res_tm.append(r[1, 0])

    for p in edi.Z.phase:
        phi_te.append(p[0, 1] % 90)
        phi_tm.append(p[1, 0] % 90)

    if pylab.np.mean(phi_te) > 90 and pylab.np.mean(phi_tm) > 90:
        phi_te = [i % 90 for i in phi_te]
        phi_tm = [i % 90 for i in phi_tm]

    for r in edi.Z.resistivity_err:
        reserr_te.append(r[0, 1])
        reserr_tm.append(r[1, 0])
    for p in edi.Z.phase_err:
        phierr_te.append(p[0, 1])
        phierr_tm.append(p[1, 0])

    periods = 1. / edi.freq

    resplotelement_xy = None
    resplotelement_yx = None

    axes = pylab.figure('EDI ' + fn)
    ax1 = pylab.subplot(211)

    if 'n' in lo_comps:
        resplotelement_xy = pylab.errorbar(periods,
                                           res_te,
                                           reserr_te,
                                           marker='x',
                                           c='b',
                                           fmt='x')
    if 'e' in lo_comps:
        resplotelement_yx = pylab.errorbar(periods,
                                           res_tm,
                                           reserr_tm,
                                           marker='x',
                                           c='r',
                                           fmt='x')
    pylab.xscale('log', nonposx='clip')
    pylab.yscale('log', nonposy='clip')
    minval = pylab.min(pylab.min(res_te, res_tm))
    maxval = pylab.max(pylab.max(res_te, res_tm))
    pylab.xlim(0.5 * pylab.min(periods), 2 * pylab.max(periods))

    # ylim([0.1,100])
    pylab.ylim([minval / 10, maxval * 10])

    pylab.autoscale(False)

    pylab.ylabel(r' $\rho$ (in $\Omega m$)')
    pylab.setp(ax1.get_xticklabels(), visible=False)
    # share x only
    ax2 = pylab.subplot(212, sharex=ax1)
    pylab.autoscale(False)

    # ylim(-45,135)
    if 'n' in lo_comps:
        pylab.errorbar(periods, phi_te, phierr_te, marker='x', c='b', fmt='x')
    if 'e' in lo_comps:
        pylab.errorbar(periods, phi_tm, phierr_tm, marker='x', c='r', fmt='x')
    pylab.ylabel('Phase angle ($\degree$)')
    pylab.xlabel('Period (in s)')
    pylab.plot([pylab.xlim()[0], pylab.xlim()[1]], [45, 45], '-.', c='0.7')
    pylab.ylim([-0, 90])

    ax1.legend([resplotelement_xy, resplotelement_yx],
               ['$E_{X}/B_Y$', '$E_Y/B_X$'],
               loc=2,
               ncol=1,
               numpoints=1,
               markerscale=0.8,
               frameon=True,
               labelspacing=0.3,
               prop={'size': 8},
               fancybox=True,
               shadow=False)

    pylab.tight_layout()
    if saveplot is True:

        pylab.ioff()
        outfn = op.splitext(fn)[0] + '.png'
        pylab.savefig(outfn, bbox_inches='tight')
        pylab.close('all')
        pylab.ion()
        return outfn

    else:
        pylab.ion()
        pylab.show(block=True)

        return None
}

data_arr = np.zeros(
    100,
    dtype=[
        ("freq", np.float),
        ("z", (np.complex, (2, 2))),
        ("z_err", (np.float, (2, 2))),
        ("tipper", (np.complex, (2, 2))),
        ("tipper_err", (np.float, (2, 2))),
    ],
)

count = 0
for edi_fn in edi_fn_list:
    edi_obj = mtedi.Edi(edi_fn)
    # get sampling rate from directory path
    for fkey in sorted(sr_dict.keys(), reverse=True):
        if str(fkey) in edi_fn:
            # locate frequency range
            f_index = np.where((edi_obj.Z.freq >= sr_dict[fkey][1])
                               & (edi_obj.Z.freq <= sr_dict[fkey][0]))

            data_arr["freq"][count:count +
                             len(f_index[0])] = edi_obj.Z.freq[f_index]
            data_arr["z"][count:count + len(f_index[0])] = edi_obj.Z.z[f_index]
            data_arr["z_err"][count:count +
                              len(f_index[0])] = edi_obj.Z.z_err[f_index]
            if edi_obj.Tipper.tipper is not None:
                data_arr["tipper"][count:count +
                                   len(f_index[0]
示例#28
0
def compute_spatial_median_ss(edi_file,
                              ss_tol=0.2,
                              freq_tol=0.15,
                              distance_radius=1000,
                              n_freq=20,
                              write_new_edi='y'):
    """
    Compute the median of all stations within a given radius (distance_radius).  
    For each station only resistivities up to  n_freq will be used to
    estimate the median resistivity.  The input station resistivity is then
    divided by the median.  The median is only calculated for the off-diagonal
    terms.  This works well if your station spacing is small <= 500m.
    
    Arguments:
    -----------
        **edi_file**  : string
                        full path to edi file to compute static shift for
                        Note that the other stations are assumed to be in 
                        the same directory as edi_file.
        
        **ss_tol** : float
                     tolerance or error about 1.0 to give to static shift 
                     estimation.  if 1-ss_tol < ss_estimation > 1+ss_tol then
                     the returned value is the ss_estimation, otherwise 1.0 
                     is returned.  *default* is 0.2
                     
        **freq_tol** : float
                       tolerance to look for frequencies in other edi files
                       that were found in edi_file.  This is important if
                       the frequencies for each edi file are different.
                       frequency match occurs when 
                       edi_file(freq)*(1-freq_tol) < edi_file2(freq) and 
                       edi_file2(freq) > edi_file(freq)*(1+freq_tol) 
                       *default* is 0.15
                       
        **distance_radius** : float
                              radius to search for other stations.  Any
                              station that is within this radius is used
                              to calculate the median static shift.
                              *default* is 1000.
                              
        **n_freq** : int
                     number of frequencies to use to calculate median, assuming
                     the first frequency is the highest frequency.
                     *default* is 20.
        
        **write_new_edi** : [ 'y' | 'n' ]
                            * 'y' will write a new edi file if there is a 
                              static shift
                            * 'n' will not write a new edi_file
                            
                            File will be saved to edi_path\SS\station.edi
                     
    Returns:
    --------
        **static_shift_x** : float
                             estimated median static shift in x direction
                             
        **static_shift_y** : float
                             estimated median static shift in y direction
        
        **new_edi_fn** : string
                      full path to new edi file if write_new_edi == 'y'
                      otherwise None is returned.
              
    """

    #convert meters to decimal degrees so we don't have to deal with zone
    #changes
    dm_deg = distance_radius * 8.994423457456377e-06

    #get path to all the other edi_files
    edi_path = os.path.dirname(edi_file)

    #make a list of edi files in the directory
    edi_list = [
        os.path.join(edi_path, edi) for edi in os.listdir(edi_path)
        if edi.find('.edi') > 0
    ]

    #remove the edi_file to be estimated for
    edi_list.remove(edi_file)
    n_edi = len(edi_list)

    #read the edi file
    edi1 = mtedi.Edi(filename=edi_file)

    #check to make sure the first frequency is the highest, if not flip
    #things around
    if edi1.freq[0] < edi1.freq[-1]:
        edi1.freq = edi1.freq[::-1]
        edi1.Z.z = edi1.Z.z[::-1]

    #make a dictionary of frequencies
    freq_dict = dict([('{:.6g}'.format(ff), ii)
                      for ii, ff in enumerate(edi1.freq[0:n_freq])])

    #get resistivity and phase
    res1, phase1, reserr1, phaseerr1 = edi1.Z.res_phase

    #make a resistivity array invovling all stations that are within dm
    res_array = np.zeros((n_edi, n_freq, 2, 2))

    kk = 0
    for kedi in edi_list:
        edi2 = mtedi.Edi(kedi)

        #check to make sure the first frequency is the highest, if not flip
        #things around
        if edi2.freq[0] < edi2.freq[-1]:
            edi2.freq = edi2.freq[::-1]
            edi2.Z.z = edi2.Z.z[::-1]

        delta_d = np.sqrt((edi1.lat - edi2.lat)**2 + (edi1.lon - edi2.lon)**2)
        if delta_d <= dm_deg:
            print 'Station {0} is within dm'.format(edi2.station)
            res2, phase2, reserr2, phaseerr2 = edi2.Z.res_phase
            for jj, ff in enumerate(edi2.freq[0:n_freq]):
                try:
                    ii = freq_dict['{0:.6g}'.format(ff)]
                    res_array[kk, ii, :, :] = res2[jj, :, :]
                except KeyError:
                    freq_find = False
                    for fkey in freq_dict.keys():
                        if float(fkey) * (1 - freq_tol) < ff < float(fkey) * (
                                1 + freq_tol):
                            ii = freq_dict[fkey]
                            res_array[kk, ii, :, :] = res2[jj, :, :]
                            freq_find = True
                            break
                        else:
                            pass
                    if freq_find == False:
                        print 'Did not find frequency {0:.6g} for {1}'.format(
                            ff, edi2.station)
            kk += 1

    if kk == 0:
        print '**** No stations with in {0} m'.format(distance_radius)
        return 1.0, 1.0, None

    #convert the res array into a masked array for averaging
    res_array = np.ma.masked_equal(res_array, 0)

    #compute the static shift of x-components

    static_shift_x = res1[0:n_freq, 0, 1] / np.ma.extras.median(
        res_array[:, :, 0, 1], axis=0)
    static_shift_x = np.median(
        static_shift_x[np.where(static_shift_x != np.inf)])

    #check to see if the estimated static shift is within given tolerance
    if 1.0 - ss_tol < static_shift_x < 1.0 + ss_tol:
        static_shift_x = 1.0

    #compute the static shift of y-components
    static_shift_y = res1[0:n_freq, 1, 0] / np.ma.extras.median(
        res_array[:, :, 1, 0], axis=0)
    static_shift_y = np.median(
        static_shift_y[np.where(static_shift_y != np.inf)])

    #check to see if the estimated static shift is within given tolerance
    if 1.0 - ss_tol < static_shift_y < 1.0 + ss_tol:
        static_shift_y = 1.0

    print 'Static shift in x-direction = {0:.2f}'.format(static_shift_x)
    print 'Static shift in y-direction = {0:.2f}'.format(static_shift_y)

    #write new edi file if there is a static shift correction
    if write_new_edi == 'y':
        svpath = os.path.join(edi_path, 'SS')
        if not os.path.exists(svpath):
            os.mkdir(svpath)

        new_edi_fn = os.path.join(svpath,
                                  os.path.basename(edi_file)[:-4] + '_ss.edi')

        static_shift, new_z = edi1.Z.no_ss(static_shift_x, static_shift_y)

        edi1.Z.z = new_z

        edi1.writefile(new_edi_fn)

        return static_shift_x, static_shift_y, new_edi_fn
    else:

        return static_shift_x, static_shift_y, None