コード例 #1
0
ファイル: mt.py プロジェクト: schoolhui/mtpy
 def _set_lon(self, longitude):
     """
     set longitude making sure the input is in decimal degrees
     
     upon setting utm coordinates are recalculated
     """
     
     self._lon = MTformat._assert_position_format('lon', longitude)
     
     if self._lon is not None and self._lat is not None:
         self._get_utm()
コード例 #2
0
ファイル: mt.py プロジェクト: lucasc15/mt3py
 def _set_lon(self, longitude):
     """
     set longitude making sure the input is in decimal degrees
     
     upon setting utm coordinates are recalculated
     """
     
     self._lon = MTformat._assert_position_format('lon', longitude)
     
     if self._lon is not None and self._lat is not None:
         self._get_utm()
コード例 #3
0
ファイル: configfile.py プロジェクト: azeezkk/mtpy
def read_survey_configfile(filename):
    """
    Read in a survey configuration file and return a dictionary.

    Input config file must contain station names as section headers!

    The output dictionary keys are station names (capitalised), 
    the values are (sub-)dictionaries. The configuration file must contain
    sections for all stations, each containing all mandatory keywords:
    
    - latitude (deg)
    - longitude (deg)
    - elevation (in meters)
    - sampling_interval (in seconds)
    - station_type (MT, E, B)

    Not mandatory, but recommended
    - declination (in degrees, positive to East) - this is set to '0.0', if omitted

    Depending on the type of station the following entries are required.

    E-field recorded:

    - E_logger_type (edl/elogger)
    - E_logger_gain (factor/gain level)
    - E_instrument_type (electrodes)
    - E_instrument_amplification (applied amplification factor)
    - E_Xaxis_azimuth (degrees)
    - E_Xaxis_length (in meters)
    - E_Yaxis_azimuth (degrees)
    - E_Yaxis_length (in meters)

    B-field recorded:
  
    - B_logger_type (edl)
    - B_logger_gain (factor/gain level)
    - B_instrument_type (coil, fluxgate)
    - B_instrument_amplification (applied amplification factor)
    - B_Xaxis_azimuth (degrees)
    - B_Yaxis_azimuth (degrees)

    """



    dict_of_allowed_values_efield = {'E_logger_type':['edl','elogger', 'zen'] ,
                                    'E_logger_gain': ['low', 'verylow','high', 
                                                      0.4, 1, 10, 11, 2, 4, 
                                                      8, 16, 32, 64],
                                    'E_instrument_type':['electrodes', 
                                                         'cu-cuso4 electrodes'],
                                    'E_instrument_amplification':[1,10]
                                    }
    
    dict_of_allowed_values_bfield = {'B_logger_type':['edl', 'zen'] ,
                                    'B_logger_gain': ['low', 'verylow','high',
                                                      0.4, 1, 10, 2, 4, 
                                                      8, 16, 32, 64],
                                    'B_instrument_type':['fluxgate', 'coil']
                                    }

    list_of_station_types = ['mt','e','b']


    error_counter = 0

    #generate config parser instance
    configobject = ConfigParser.ConfigParser()

    #check, if file is present
    if not op.isfile(filename):
        raise MTex.MTpyError_inputarguments( 'File does not'
                            ' exist: {0}'.format(filename) )


    # try to parse file - exit, if not a config file
    try:
        configobject.read(filename)
    except:
        raise MTex.MTpyError_inputarguments( 'File is not a '
                    'proper configuration file: {0}'.format(filename) )

    #obtain dict of dicts containing the input file's sections (station names)
    #excludes DEFAULT section and key-value pairs without section header
    configobject_dict = configobject._sections

    #initialise the output dictionary
    config_dict = {}

    #loop over the sections (stations) of the config file
    for station in configobject_dict:
        #read in the sub-dictionary for the current station - bringing all keys
        #to lowercase!
        temp_dict_in = dict((k.lower(),v) 
                            for k, v in configobject_dict[station].items())

        #initialise output sub-directory for current station 
        stationdict = temp_dict_in

        #stationnames are uppercase in MTpy
        stationname = station.upper()
        stationdict['station'] = stationname

        #check for presence of all mandatory keywords for the current station
        #case insensitive - allow for short forms 'lat', 'lon', and 'ele'
        for req_keyword in list_of_required_keywords:
            if req_keyword.lower() in temp_dict_in.keys():
                stationdict[req_keyword.lower()] = \
                                      temp_dict_in[req_keyword.lower()].lower()
            elif req_keyword in ['latitude', 'longitude', 'elevation']:
                if req_keyword[:3] in temp_dict_in.keys():
                    stationdict[req_keyword] = temp_dict_in[req_keyword[:3]]
            else:  
                print 'Station {0} - keyword {1} missing'.format(stationname,
                                                                 req_keyword)
                error_counter += 1
                continue

        #check format of lat/lon - convert to degrees, if given in 
        #(deg,min,sec)-triple
        for coordinate in ['latitude', 'longitude', 'elevation']:
            value = stationdict[coordinate]
            try:
                new_value = MTft._assert_position_format(coordinate,value)
            except:
                raise MTex.MTpyError_config_file('Error - wrong '
                        'coordinate format for station {0}'.format(stationname))
            stationdict[coordinate] = new_value

        if not stationdict['station_type'] in list_of_station_types:
            raise MTex.MTpyError_config_file( 'Station type not valid' )


        if stationdict['station_type'] in ['mt','e']:
            #check for required electric field parameters
            for req_keyword in list_of_efield_keywords:
                if req_keyword.lower() in temp_dict_in.keys():
                    stationdict[req_keyword.lower()] = \
                                      temp_dict_in[req_keyword.lower()].lower()
                else:  
                    print 'Station {0} - keyword {1} missing'.format(stationname,
                                                                  req_keyword)
                    error_counter += 1
                    continue

            _validate_dictionary(stationdict,dict_of_allowed_values_efield)
            

        if stationdict['station_type'] in ['mt','b']:
            #check for required magnetic field parameters
            for req_keyword in list_of_bfield_keywords:
                if req_keyword.lower() in temp_dict_in.keys():
                    stationdict[req_keyword.lower()] = \
                                     temp_dict_in[req_keyword.lower()].lower()
                else:  
                    print 'Station {0} - keyword {1} missing'.format(stationname,
                                                                     req_keyword)
                    error_counter += 1
                    continue

            _validate_dictionary(stationdict,dict_of_allowed_values_bfield)
            

        #add the station's sub-dictionary to the config dictionary
        config_dict[stationname] = stationdict

    #re-loop for setting up correct remote reference station information :
    #if rem.ref. station key is present, its information must be contained 
    #in the same config file!
    for station in config_dict.iterkeys():
        stationdict = config_dict[station]

        stationdict['rr_station'] = None
        stationdict['rr_station_latitude'] = None
        stationdict['rr_station_longitude'] = None
        stationdict['rr_station_elevation'] = None


        if stationdict.has_key('rr_station'):
            rem_station = stationdict['rr_station'] 
            try:
                #check, if values are contained in dict 
                float(stationdict['rr_station_latitude'] )
                float(stationdict['rr_station_longitude'])
                float(stationdict['rr_station_elevation'])
            except:
                try:
                    #check for shortened form
                    stationdict['rr_station_latitude']  = float(
                                            stationdict['rr_station_lat'] )
                    stationdict['rr_station_longitude'] = float(
                                            stationdict['rr_station_lon'] )
                    stationdict['rr_station_elevation'] = float(
                                            stationdict['rr_station_ele'] )                 

                except:
                    try:
                        #read from other config dict entry
                        stationdict['rr_station_latitude'] = \
                                          config_dict[rem_station]['latitude']
                        stationdict['rr_station_longitude'] = \
                                         config_dict[rem_station]['longitude']
                        stationdict['rr_station_elevation'] = \
                                         config_dict[rem_station]['elevation']

                    except:
                        #if finally failed to read rr_station info,\
                        #set rr_station back to None
                        stationdict['rr_station'] = None
                        stationdict['rr_station_latitude'] = None
                        stationdict['rr_station_longitude'] = None
                        stationdict['rr_station_elevation'] = None

        #check consistency of coordinates, if rr_station is present
        if stationdict['rr_station'] != None:
            try:
                stationdict['rr_station_latitude'] = \
                            MTft._assert_position_format(
                                'latitude',stationdict['rr_station_latitude'])
                stationdict['rr_station_longitude'] = \
                            MTft._assert_position_format(
                                'longitude',stationdict['rr_station_longitude'])
                stationdict['rr_station_elevation'] = \
                            MTft._assert_position_format(
                                'elevation',stationdict['rr_station_elevation'])

            except:
                print 'Problem with remote reference station ({0}) -'
                ' remote reference ({1}) coordinates invalid -'
                ' remote reference set to None'.format(station, 
                                                       stationdict['rr_station'])

                stationdict['rr_station'] = None
                stationdict['rr_station_latitude'] = None
                stationdict['rr_station_longitude'] = None
                stationdict['rr_station_elevation'] = None


    if error_counter != 0:
        print 'Could not read all mandatory sections and options'\
                ' in config file - found {0} errors - check configuration'\
                ' file before continuing!'.format(error_counter)
    
    else:
        return config_dict
コード例 #4
0
def read_survey_configfile(filename):
    """
    Read in a survey configuration file and return a dictionary.

    Input config file must contain station names as section headers!

    The output dictionary keys are station names (capitalised), 
    the values are (sub-)dictionaries. The configuration file must contain
    sections for all stations, each containing all mandatory keywords:
    
    - latitude (deg)
    - longitude (deg)
    - elevation (in meters)
    - sampling_interval (in seconds)
    - station_type (MT, E, B)

    Not mandatory, but recommended
    - declination (in degrees, positive to East) - this is set to '0.0', if omitted

    Depending on the type of station the following entries are required.

    E-field recorded:

    - E_logger_type ('edl'/'elogger'/'qel')
    - E_logger_gain (factor/gain-level)
    - E_instrument_type ('electrodes'/'dipole')
    - E_instrument_amplification (applied amplification factor)
    - E_Xaxis_azimuth (degrees)
    - E_Xaxis_length (in meters)
    - E_Yaxis_azimuth (degrees)
    - E_Yaxis_length (in meters)

    B-field recorded:
  
    - B_logger_type ('edl'/'qel_blogger')
    - B_logger_gain (factor/gain level)
    - B_instrument_type ('coil(s)', 'fluxgate')
    - B_instrument_amplification (applied amplification factor)
    - B_Xaxis_azimuth (degrees)
    - B_Yaxis_azimuth (degrees)

    """

    error_counter = 0

    #generate config parser instance
    configobject = ConfigParser.ConfigParser()

    #check, if file is present
    if not op.isfile(filename):
        raise MTex.MTpyError_inputarguments('File does not'
                                            ' exist: {0}'.format(filename))

    # try to parse file - exit, if not a config file
    try:
        configobject.read(filename)
    except:
        raise MTex.MTpyError_inputarguments(
            'File is not a '
            'proper configuration file: {0}'.format(filename))

    #obtain dict of dicts containing the input file's sections (station names)
    #excludes DEFAULT section and key-value pairs without section header
    configobject_dict = configobject._sections

    #initialise the output dictionary
    config_dict = {}

    #loop over the sections (stations) of the config file
    for station in configobject_dict:
        #read in the sub-dictionary for the current station - bringing all keys
        #to lowercase!
        temp_dict_in = dict(
            (k.lower(), v) for k, v in configobject_dict[station].items())

        #initialise output sub-directory for current station
        stationdict = temp_dict_in

        #stationnames are uppercase in MTpy
        stationname = station.upper()
        stationdict['station'] = stationname

        #check for presence of all mandatory keywords for the current station
        #case insensitive - allow for short forms 'lat', 'lon', and 'ele'
        try:
            for req_keyword in list_of_required_keywords:
                if req_keyword.lower() in temp_dict_in.keys():
                    stationdict[req_keyword.lower()] = \
                                          temp_dict_in[req_keyword.lower()].lower()
                elif req_keyword in ['latitude', 'longitude', 'elevation']:
                    if req_keyword[:3] in temp_dict_in.keys():
                        stationdict[req_keyword] = temp_dict_in[
                            req_keyword[:3]]
                else:
                    print 'Station {0} - keyword {1} missing'.format(
                        stationname, req_keyword)
                    error_counter += 1
                    continue

            #check format of lat/lon - convert to degrees, if given in
            #(deg,min,sec)-triple
            for coordinate in ['latitude', 'longitude', 'elevation']:
                value = stationdict[coordinate]
                try:
                    new_value = MTft._assert_position_format(coordinate, value)
                except:
                    raise MTex.MTpyError_config_file(
                        'Error - wrong '
                        'coordinate format for station {0}'.format(
                            stationname))
                stationdict[coordinate] = new_value

            if not stationdict['station_type'] in list_of_station_types:
                raise MTex.MTpyError_config_file('Station type not valid')
        except:
            print 'Missing information on station {0} in config file - skipping'.format(
                station)
            continue

        if stationdict['station_type'] in ['mt', 'e']:
            #check for required electric field parameters
            for req_keyword in list_of_efield_keywords:
                if req_keyword.lower() in temp_dict_in.keys():
                    stationdict[req_keyword.lower()] = \
                                      temp_dict_in[req_keyword.lower()].lower()
                else:
                    print 'Station {0} - keyword {1} missing'.format(
                        stationname, req_keyword)
                    error_counter += 1
                    continue

            _validate_dictionary(stationdict, dict_of_allowed_values_efield)

        if stationdict['station_type'] in ['mt', 'b']:
            #check for required magnetic field parameters
            for req_keyword in list_of_bfield_keywords:
                if req_keyword.lower() in temp_dict_in.keys():
                    stationdict[req_keyword.lower()] = \
                                     temp_dict_in[req_keyword.lower()].lower()
                else:
                    print 'Station {0} - keyword {1} missing'.format(
                        stationname, req_keyword)
                    error_counter += 1
                    continue

            _validate_dictionary(stationdict, dict_of_allowed_values_bfield)

        #add the station's sub-dictionary to the config dictionary
        config_dict[stationname] = stationdict

    #re-loop for setting up correct remote reference station information :
    #if rem.ref. station key is present, its information must be contained
    #in the same config file!
    for station in config_dict.iterkeys():
        stationdict = config_dict[station]
        if not stationdict.has_key('rr_station'):
            continue

        #stationdict['rr_station'] = None
        stationdict['rr_station_latitude'] = None
        stationdict['rr_station_longitude'] = None
        stationdict['rr_station_elevation'] = None

        rem_station = stationdict['rr_station']
        try:
            #check, if values are contained in dict
            float(stationdict['rr_station_latitude'])
            float(stationdict['rr_station_longitude'])
            float(stationdict['rr_station_elevation'])
        except:
            try:
                #check for shortened form
                stationdict['rr_station_latitude'] = float(
                    stationdict['rr_station_lat'])
                stationdict['rr_station_longitude'] = float(
                    stationdict['rr_station_lon'])
                stationdict['rr_station_elevation'] = float(
                    stationdict['rr_station_ele'])

            except:
                try:
                    #read from other config dict entry
                    stationdict['rr_station_latitude'] = \
                                      config_dict[rem_station]['latitude']
                    stationdict['rr_station_longitude'] = \
                                     config_dict[rem_station]['longitude']
                    stationdict['rr_station_elevation'] = \
                                     config_dict[rem_station]['elevation']

                except:
                    #if finally failed to read rr_station info,\
                    #set rr_station back to None
                    stationdict['rr_station'] = None
                    stationdict['rr_station_latitude'] = None
                    stationdict['rr_station_longitude'] = None
                    stationdict['rr_station_elevation'] = None

        #check consistency of coordinates, if rr_station is present
        if stationdict['rr_station'] != None:
            try:
                stationdict['rr_station_latitude'] = \
                            MTft._assert_position_format(
                                'latitude',stationdict['rr_station_latitude'])
                stationdict['rr_station_longitude'] = \
                            MTft._assert_position_format(
                                'longitude',stationdict['rr_station_longitude'])
                stationdict['rr_station_elevation'] = \
                            MTft._assert_position_format(
                                'elevation',stationdict['rr_station_elevation'])

            except:
                print 'Problem with remote reference station ({0}) -'
                ' remote reference ({1}) coordinates invalid -'
                ' remote reference set to None'.format(
                    station, stationdict['rr_station'])

                stationdict['rr_station'] = None
                stationdict['rr_station_latitude'] = None
                stationdict['rr_station_longitude'] = None
                stationdict['rr_station_elevation'] = None

    if error_counter != 0:
        print 'Could not read all mandatory sections and options'\
                ' in config file - found {0} errors - check configuration'\
                ' file before continue!'.format(error_counter)
        answer = 5
        while not answer in ['y', 'n']:
            answer = raw_input('\n\tDo you want to continue anyway? (y/n)')
            try:
                answer = answer.strip().lower()[0]
            except:
                continue
            if answer == 'n':
                print
                sys.exit()
    print
    return config_dict
コード例 #5
0
ファイル: configfile.py プロジェクト: lucasc15/mt3py
def read_survey_configfile(filename):
    """
    Read in a survey configuration file and return a dictionary.

    Input config file must contain station names as section headers!

    The output dictionary keys are station names (capitalised), 
    the values are (sub-)dictionaries. The configuration file must contain
    sections for all stations, each containing all mandatory keywords:
    
    - latitude (deg)
    - longitude (deg)
    - elevation (in meters)
    - sampling_interval (in seconds)
    - station_type (MT, (Q)E, (Q)B)

    Not mandatory, but recommended
    - declination (in degrees, positive to East) - this is set to '0.0', if omitted

    Depending on the type of station the following entries are required.

    E-field recorded:

    - E_logger_type ('edl'/'elogger'/'qel')
    - E_logger_gain (factor/gain-level)
    - E_instrument_type ('electrodes'/'dipole')
    - E_instrument_amplification (applied amplification factor)
    - E_Xaxis_azimuth (degrees)
    - E_Xaxis_length (in meters)
    - E_Yaxis_azimuth (degrees)
    - E_Yaxis_length (in meters)

    B-field recorded:
  
    - B_logger_type ('edl'/'qel_blogger')
    - B_logger_gain (factor/gain level)
    - B_instrument_type ('coil(s)', 'fluxgate')
    - B_instrument_amplification (applied amplification factor)
    - B_Xaxis_azimuth (degrees)
    - B_Yaxis_azimuth (degrees)


    A global section can be used to include parameters for all stations.
    The name of the section must be one of:

        global/main/default/general 

    """





    error_counter = 0

    #generate config parser instance
    configobject = configparser.ConfigParser()

    #check, if file is present
    if not op.isfile(filename):
        raise MTex.MTpyError_inputarguments( 'File does not'
                            ' exist: {0}'.format(filename) )


    # try to parse file - exit, if not a config file
    try:
        configobject.read(filename)
    except:
        raise MTex.MTpyError_inputarguments( 'File is not a '
                    'proper configuration file: {0}'.format(filename) )

    #obtain dict of dicts containing the input file's sections (station names)
    #excludes DEFAULT section and key-value pairs without section header
    configobject_dict = configobject._sections

    #initialise the output dictionary
    config_dict = {}

    #loop over the sections (stations) of the config file
    for station in configobject_dict:
        #read in the sub-dictionary for the current station - bringing all keys
        #to lowercase!
        temp_dict_in = dict((k.lower(),v.lower()) 
                            for k, v in list(configobject_dict[station].items()))

        #initialise output sub-directory for current station 
        stationdict = temp_dict_in

        #stationnames are uppercase in MTpy
        stationname = station.upper()
        if stationname in ['GLOBAL','MAIN','DEFAULT','GENERAL']:
            stationname = 'GLOBAL'

        stationdict['station'] = stationname

        #add the station's sub-dictionary to the config dictionary
        config_dict[stationname] = stationdict


    # Check if a global section is present
    if 'GLOBAL' in config_dict: 
        globaldict = config_dict['GLOBAL']
    else:
        #set defaults for location
        globaldict={}
    # for i in ['latitude', 'longitude', 'elevation']:
    #     #skip if values are present
    #     if i in globaldict.keys() or i[:3] in globaldict.keys():
    #         continue
    #     #otherwise set defaults
    #     globaldict[i] = 0
        

    #remove other general sections to avoid redundancy
    for i in ['MAIN','DEFAULT','GENERAL']:
        if i in config_dict:
            dummy = config_dict.pop(i)

    # RE-loop to check for each station if required keywords are present,
    # if not if they can be pulled from the global section  
    
    #============================================================
    # local function definition
    def fromglobals(key,stationdict,globaldict):
        """
            Check if stationdict contains key. 
            If not search for key in global dict and add it to station dict.

            Return if global dict is not defined.
            Return True if key was present in either dictionary, False if not.

        """

        if key in list(stationdict.keys()):
            return True, stationdict.get(key)

        if globaldict is None or len(globaldict) == 0:
            return False, None


        if key in globaldict:
            stationdict[key] = globaldict[key]
            return True,globaldict.get(key)

        return False, None

    #============================================================


    for station in sorted(config_dict):
        #do not alter the global section
        if station == 'GLOBAL':
            continue
        
        stationdict =  config_dict[station]
        


        #check for presence of all mandatory keywords for the current station
        #case insensitive - allow for short forms 'sampling', 'lat', 'lon', and 'elev'
        for idx,req_keyword in enumerate(list_of_required_keywords):
            shortform = list_of_required_keywords_short[idx]

            try:
                found = False
                #import ipdb
                #ipdb.set_trace()
                if fromglobals(req_keyword,stationdict,globaldict)[0] is False:
                    #try short form instead
                    found,value = fromglobals(shortform,stationdict,globaldict)
                    #print shortform,value

                    if found is True:
                        stationdict[req_keyword] = value
  
                else:  
                    found = True

                if found is False:
                    print('Station {0} - keyword {1} missing'.format(stationname,
                                                                     req_keyword))
                    error_counter += 1
                    raise Exception

                if req_keyword in ['elevation','latitude', 'longitude']:
                    #check format of lat/lon - convert to degrees, if given in 
                    #(deg,min,sec)-triple#assert correct format
                    value = stationdict[req_keyword]
                    try:
                        new_value = MTft._assert_position_format(req_keyword,value)
                    except:
                        raise MTex.MTpyError_config_file('Error - wrong '
                                'coordinate format for station {0}'.format(stationname))
                    
                    stationdict[req_keyword] = new_value



            except:
                raise
                print('Missing information on station {0} in config file'\
                        ' - setting default (dummy) value'.format(station))
                stationdict[req_keyword] = list_of_keyword_defaults_general[idx]
            
            #to avoid duplicates remove the now obsolete short form from 
            #the station dictionary
            dummy = stationdict.pop(shortform,None)


        if not stationdict['station_type'] in list_of_station_types:
            raise MTex.MTpyError_config_file( 'Station type not valid' )


        if stationdict['station_type'] in ['mt','e']:
            #check for required electric field parameters - not done for QEL loggers yet
            for req_keyword in list_of_efield_keywords:
                if req_keyword.lower() in list(temp_dict_in.keys()):
                    stationdict[req_keyword.lower()] = \
                                      temp_dict_in[req_keyword.lower()].lower()
                else:  
                    print('Station {0} - keyword {1} missing'.format(stationname,
                                                                  req_keyword))
                    error_counter += 1
                    continue

            _validate_dictionary(stationdict,dict_of_allowed_values_efield)
            

        if stationdict['station_type'] in ['mt','b']:
            #check for required magnetic field parameters
            for req_keyword in list_of_bfield_keywords:
                if req_keyword.lower() in list(temp_dict_in.keys()):
                    stationdict[req_keyword.lower()] = \
                                     temp_dict_in[req_keyword.lower()].lower()
                else:  
                    print('Station {0} - keyword {1} missing'.format(stationname,
                                                                     req_keyword))
                    error_counter += 1
                    continue

            _validate_dictionary(stationdict,dict_of_allowed_values_bfield)
            


    #re-loop for setting up correct remote reference station information :
    #if rem.ref. station key is present, its information must be contained 
    #in the same config file!
    for station in config_dict.keys():
        stationdict = config_dict[station]
        if 'rr_station' not in stationdict:
            continue

        #stationdict['rr_station'] = None
        stationdict['rr_station_latitude'] = None
        stationdict['rr_station_longitude'] = None
        stationdict['rr_station_elevation'] = None


        rem_station = stationdict['rr_station'] 
        try:
            #check, if values are contained in dict 
            float(stationdict['rr_station_latitude'] )
            float(stationdict['rr_station_longitude'])
            float(stationdict['rr_station_elevation'])
        except:
            try:
                #check for shortened form
                stationdict['rr_station_latitude']  = float(
                                        stationdict['rr_station_lat'] )
                stationdict['rr_station_longitude'] = float(
                                        stationdict['rr_station_lon'] )
                stationdict['rr_station_elevation'] = float(
                                        stationdict['rr_station_ele'] )                 

            except:
                try:
                    #read from other config dict entry
                    stationdict['rr_station_latitude'] = \
                                      config_dict[rem_station]['latitude']
                    stationdict['rr_station_longitude'] = \
                                     config_dict[rem_station]['longitude']
                    stationdict['rr_station_elevation'] = \
                                     config_dict[rem_station]['elevation']

                except:
                    #if finally failed to read rr_station info,\
                    #set rr_station back to None
                    stationdict['rr_station'] = None
                    stationdict['rr_station_latitude'] = None
                    stationdict['rr_station_longitude'] = None
                    stationdict['rr_station_elevation'] = None

        #check consistency of coordinates, if rr_station is present
        if stationdict['rr_station'] != None:
            try:
                stationdict['rr_station_latitude'] = \
                            MTft._assert_position_format(
                                'latitude',stationdict['rr_station_latitude'])
                stationdict['rr_station_longitude'] = \
                            MTft._assert_position_format(
                                'longitude',stationdict['rr_station_longitude'])
                stationdict['rr_station_elevation'] = \
                            MTft._assert_position_format(
                                'elevation',stationdict['rr_station_elevation'])

            except:
                print('Problem with remote reference station ({0}) -')
                ' remote reference ({1}) coordinates invalid -'
                ' remote reference set to None'.format(station, 
                                                       stationdict['rr_station'])

                stationdict['rr_station'] = None
                stationdict['rr_station_latitude'] = None
                stationdict['rr_station_longitude'] = None
                stationdict['rr_station_elevation'] = None


    if error_counter != 0:
        print('Could not read all mandatory sections and options'\
                ' in config file - found {0} errors - check configuration'\
                ' file before continue!'.format(error_counter))
        answer = 5
        while not answer in ['y','n']:
            answer = input('\n\tDo you want to continue anyway? (y/n)')
            try:
                answer = answer.strip().lower()[0]
            except:
                continue
            if answer == 'n':
                print()
                sys.exit()
    print() 
    return config_dict
コード例 #6
0
edi_path = r"c:\Users\jpeacock\Documents\SaudiArabia\edi_files"
save_path = r"c:\Users\jpeacock\Documents\SaudiArabia\edi_files_z"
plot_path = r"c:\Users\jpeacock\Documents\SaudiArabia\resp_plots"

edi_list = [
    os.path.join(edi_path, edi) for edi in os.listdir(edi_path)
    if edi[-4:] == '.edi'
]

for edi in edi_list:
    edi_fid = file(edi, 'r')
    edi_lines = edi_fid.readlines()
    if edi_lines[25][24] == 'N':
        real_lon = edi_lines[25].strip().split()[4][4:]
        real_lon = '{0}:{1}'.format(real_lon[0:2], real_lon[2:])
        dd = mtft._assert_position_format('long', real_lon)
        lon_str = mtft.convert_dms_tuple2string(
            mtft.convert_degrees2dms_tuple(dd))
        # replace the retarded long string output by phoenix
        # replace in header
        edi_lines[10] = '{0}{1}\n'.format(edi_lines[10][0:9], lon_str)
        # replace in info
        edi_lines[25] = '{0}{1}  {2}'.format(edi_lines[25][0:24], lon_str,
                                             edi_lines[25][34:])
        # replace in definemeas
        edi_lines[61] = '{0}{1}\n'.format(edi_lines[61][0:12], lon_str)
    new_fid = file(os.path.join(save_path, os.path.basename(edi)), 'w')
    new_fid.write(''.join(edi_lines))
    new_fid.close()
    edi_fid.close()
コード例 #7
0
def read_survey_configfile(filename):
    """
    Read in a survey configuration file and return a dictionary.

    Input config file must contain station names as section headers!

    The output dictionary keys are station names (capitalised), 
    the values are (sub-)dictionaries. The configuration file must contain
    sections for all stations, each containing all mandatory keywords:
    
    - latitude (deg)
    - longitude (deg)
    - elevation (in meters)
    - sampling_interval (in seconds)
    - station_type (MT, (Q)E, (Q)B)

    Not mandatory, but recommended
    - declination (in degrees, positive to East) - this is set to '0.0', if omitted

    Depending on the type of station the following entries are required.

    E-field recorded:

    - E_logger_type ('edl'/'elogger'/'qel')
    - E_logger_gain (factor/gain-level)
    - E_instrument_type ('electrodes'/'dipole')
    - E_instrument_amplification (applied amplification factor)
    - E_Xaxis_azimuth (degrees)
    - E_Xaxis_length (in meters)
    - E_Yaxis_azimuth (degrees)
    - E_Yaxis_length (in meters)

    B-field recorded:
  
    - B_logger_type ('edl'/'qel_blogger')
    - B_logger_gain (factor/gain level)
    - B_instrument_type ('coil(s)', 'fluxgate')
    - B_instrument_amplification (applied amplification factor)
    - B_Xaxis_azimuth (degrees)
    - B_Yaxis_azimuth (degrees)


    A global section can be used to include parameters for all stations.
    The name of the section must be one of:

        global/main/default/general 

    """





    error_counter = 0

    #generate config parser instance
    configobject = ConfigParser.ConfigParser()

    #check, if file is present
    if not op.isfile(filename):
        raise MTex.MTpyError_inputarguments( 'File does not'
                            ' exist: {0}'.format(filename) )


    # try to parse file - exit, if not a config file
    try:
        configobject.read(filename)
    except:
        raise MTex.MTpyError_inputarguments( 'File is not a '
                    'proper configuration file: {0}'.format(filename) )

    #obtain dict of dicts containing the input file's sections (station names)
    #excludes DEFAULT section and key-value pairs without section header
    configobject_dict = configobject._sections

    #initialise the output dictionary
    config_dict = {}

    #loop over the sections (stations) of the config file
    for station in configobject_dict:
        #read in the sub-dictionary for the current station - bringing all keys
        #to lowercase!
        temp_dict_in = dict((k.lower(),v.lower()) 
                            for k, v in configobject_dict[station].items())

        #initialise output sub-directory for current station 
        stationdict = temp_dict_in

        #stationnames are uppercase in MTpy
        stationname = station.upper()
        if stationname in ['GLOBAL','MAIN','DEFAULT','GENERAL']:
            stationname = 'GLOBAL'

        stationdict['station'] = stationname

        #add the station's sub-dictionary to the config dictionary
        config_dict[stationname] = stationdict


    # Check if a global section is present
    if config_dict.has_key('GLOBAL'): 
        globaldict = config_dict['GLOBAL']
    else:
        #set defaults for location
        globaldict={}
    # for i in ['latitude', 'longitude', 'elevation']:
    #     #skip if values are present
    #     if i in globaldict.keys() or i[:3] in globaldict.keys():
    #         continue
    #     #otherwise set defaults
    #     globaldict[i] = 0
        

    #remove other general sections to avoid redundancy
    for i in ['MAIN','DEFAULT','GENERAL']:
        if config_dict.has_key(i):
            dummy = config_dict.pop(i)

    # RE-loop to check for each station if required keywords are present,
    # if not if they can be pulled from the global section  
    
    #============================================================
    # local function definition
    def fromglobals(key,stationdict,globaldict):
        """
            Check if stationdict contains key. 
            If not search for key in global dict and add it to station dict.

            Return if global dict is not defined.
            Return True if key was present in either dictionary, False if not.

        """

        if key in stationdict.keys():
            return True, stationdict.get(key)

        if globaldict is None or len(globaldict) == 0:
            return False, None


        if key in globaldict:
            stationdict[key] = globaldict[key]
            return True,globaldict.get(key)

        return False, None

    #============================================================


    for station in sorted(config_dict):
        #do not alter the global section
        if station == 'GLOBAL':
            continue
        
        stationdict =  config_dict[station]
        


        #check for presence of all mandatory keywords for the current station
        #case insensitive - allow for short forms 'sampling', 'lat', 'lon', and 'elev'
        for idx,req_keyword in enumerate(list_of_required_keywords):
            shortform = list_of_required_keywords_short[idx]

            try:
                found = False
                #import ipdb
                #ipdb.set_trace()
                if fromglobals(req_keyword,stationdict,globaldict)[0] is False:
                    #try short form instead
                    found,value = fromglobals(shortform,stationdict,globaldict)
                    #print shortform,value

                    if found is True:
                        stationdict[req_keyword] = value
  
                else:  
                    found = True

                if found is False:
                    print 'Station {0} - keyword {1} missing'.format(stationname,
                                                                     req_keyword)
                    error_counter += 1
                    raise Exception

                if req_keyword in ['elevation','latitude', 'longitude']:
                    #check format of lat/lon - convert to degrees, if given in 
                    #(deg,min,sec)-triple#assert correct format
                    value = stationdict[req_keyword]
                    try:
                        new_value = MTft._assert_position_format(req_keyword,value)
                    except:
                        raise MTex.MTpyError_config_file('Error - wrong '
                                'coordinate format for station {0}'.format(stationname))
                    
                    stationdict[req_keyword] = new_value



            except:
                raise
                print 'Missing information on station {0} in config file'\
                        ' - setting default (dummy) value'.format(station)
                stationdict[req_keyword] = list_of_keyword_defaults_general[idx]
            
            #to avoid duplicates remove the now obsolete short form from 
            #the station dictionary
            dummy = stationdict.pop(shortform,None)


        if not stationdict['station_type'] in list_of_station_types:
            raise MTex.MTpyError_config_file( 'Station type not valid' )


        if stationdict['station_type'] in ['mt','e']:
            #check for required electric field parameters - not done for QEL loggers yet
            for req_keyword in list_of_efield_keywords:
                if req_keyword.lower() in temp_dict_in.keys():
                    stationdict[req_keyword.lower()] = \
                                      temp_dict_in[req_keyword.lower()].lower()
                else:  
                    print 'Station {0} - keyword {1} missing'.format(stationname,
                                                                  req_keyword)
                    error_counter += 1
                    continue

            _validate_dictionary(stationdict,dict_of_allowed_values_efield)
            

        if stationdict['station_type'] in ['mt','b']:
            #check for required magnetic field parameters
            for req_keyword in list_of_bfield_keywords:
                if req_keyword.lower() in temp_dict_in.keys():
                    stationdict[req_keyword.lower()] = \
                                     temp_dict_in[req_keyword.lower()].lower()
                else:  
                    print 'Station {0} - keyword {1} missing'.format(stationname,
                                                                     req_keyword)
                    error_counter += 1
                    continue

            _validate_dictionary(stationdict,dict_of_allowed_values_bfield)
            


    #re-loop for setting up correct remote reference station information :
    #if rem.ref. station key is present, its information must be contained 
    #in the same config file!
    for station in config_dict.iterkeys():
        stationdict = config_dict[station]
        if not stationdict.has_key('rr_station'):
            continue

        #stationdict['rr_station'] = None
        stationdict['rr_station_latitude'] = None
        stationdict['rr_station_longitude'] = None
        stationdict['rr_station_elevation'] = None


        rem_station = stationdict['rr_station'] 
        try:
            #check, if values are contained in dict 
            float(stationdict['rr_station_latitude'] )
            float(stationdict['rr_station_longitude'])
            float(stationdict['rr_station_elevation'])
        except:
            try:
                #check for shortened form
                stationdict['rr_station_latitude']  = float(
                                        stationdict['rr_station_lat'] )
                stationdict['rr_station_longitude'] = float(
                                        stationdict['rr_station_lon'] )
                stationdict['rr_station_elevation'] = float(
                                        stationdict['rr_station_ele'] )                 

            except:
                try:
                    #read from other config dict entry
                    stationdict['rr_station_latitude'] = \
                                      config_dict[rem_station]['latitude']
                    stationdict['rr_station_longitude'] = \
                                     config_dict[rem_station]['longitude']
                    stationdict['rr_station_elevation'] = \
                                     config_dict[rem_station]['elevation']

                except:
                    #if finally failed to read rr_station info,\
                    #set rr_station back to None
                    stationdict['rr_station'] = None
                    stationdict['rr_station_latitude'] = None
                    stationdict['rr_station_longitude'] = None
                    stationdict['rr_station_elevation'] = None

        #check consistency of coordinates, if rr_station is present
        if stationdict['rr_station'] != None:
            try:
                stationdict['rr_station_latitude'] = \
                            MTft._assert_position_format(
                                'latitude',stationdict['rr_station_latitude'])
                stationdict['rr_station_longitude'] = \
                            MTft._assert_position_format(
                                'longitude',stationdict['rr_station_longitude'])
                stationdict['rr_station_elevation'] = \
                            MTft._assert_position_format(
                                'elevation',stationdict['rr_station_elevation'])

            except:
                print 'Problem with remote reference station ({0}) -'
                ' remote reference ({1}) coordinates invalid -'
                ' remote reference set to None'.format(station, 
                                                       stationdict['rr_station'])

                stationdict['rr_station'] = None
                stationdict['rr_station_latitude'] = None
                stationdict['rr_station_longitude'] = None
                stationdict['rr_station_elevation'] = None


    if error_counter != 0:
        print 'Could not read all mandatory sections and options'\
                ' in config file - found {0} errors - check configuration'\
                ' file before continue!'.format(error_counter)
        answer = 5
        while not answer in ['y','n']:
            answer = raw_input('\n\tDo you want to continue anyway? (y/n)')
            try:
                answer = answer.strip().lower()[0]
            except:
                continue
            if answer == 'n':
                print
                sys.exit()
    print 
    return config_dict
コード例 #8
0
ファイル: mseed.py プロジェクト: kinverarity1/mtpy
def convertfile_miniseed2ts(infile, outfile, unit=None, lat = None, lon = None, elev = None):

    try:
        dummystream = read(infile)
    except: 
        raise MTpyError_inputarguments('infile is not miniSeed')

    no_traces = len(dummystream)
    lo_outfn = []


    for trace in range(no_traces):

        station, channel, location, network,  samplingrate, t0, nsamples, data =\
                                         readfile_obspy(infile,trace)

        channel = channel.lower()
        if channel[-1] == 'e':
            channel = channel[:-1]+ 'y'
        if channel[-1] == 'n':
            channel = channel[:-1] +'x'

        ts_tuple = [station.upper(), channel.lower(), samplingrate,t0, nsamples]


        if unit is not None:
            try:
                unit = unit.lower()
            except:
                unit = None

        # if unit is None:
        #     ts_tuple.append('unknown') 

        if lat is not None:
            try:
                lat = FT._assert_position_format('lat', lat)
            except:
                lat = None

        # if lat is None:
        #     ts_tuple.append(0.)


        if lon is not None:
            try:
                lon = FT._assert_position_format('lon', lon)
            except:
                lon = None

        # if lon is None:
        #     ts_tuple.append(0.)


        if elev is not None:
            try:
                elev = FT._assert_position_format('elev', elev)
            except:
                elev = None

        # if elev is None:
        #     ts_tuple.append(0.)


        ts_tuple.append(data)

        if outfile.lower().endswith('mseed'):
            outfilebase = op.splitext(op.abspath(outfile))[0]
            newoutfile = '{0}.{1}'.format(outfilebase,ts_tuple[1])
        else:
            newoutfile = outfile


        outfilename = MTfh.write_ts_file_from_tuple(newoutfile,tuple(ts_tuple))
        outfilename = newoutfile
        lo_outfn.append(outfilename)

    return lo_outfn