def read(cls, event_file, regex=regex): """ Read events from event_file with the help of given regular expression. :param event_file: location of event_file :param regex: regular expression, see Events.regex<Tab> """ with open(event_file, 'r') as f: filedata = f.read() event_matches = re.finditer(regex, filedata, re.VERBOSE + re.MULTILINE) list_ = [i.groupdict() for i in event_matches] #util.ipshell() for event in list_: # convert numbers to float and int types for key, item in event.iteritems(): if util.isint(item): event[key] = int(item) elif util.isfloat(item): event[key] = float(item) else: event[key] = item.strip() #if event[key] == '': # event[key] = None #if key == 'depth' and regex == cls.regex: # event[key] *= 1 #util.ipshell() log.info('Read event information of %d events from events event_file %s' % (len(list_), event_file)) return cls(list_)
def extract_cdo_file(): file = data_path + 'CDO_CAL.txt' #AF, IQ regex = """ ^ (?P<stn>\d+)\s+ (?P<wban>\d+)\s+ (?P<date>\d+)\s+ (?P<temp>[-\d.]+)\s+ (?P<temp_count>\d+)\s+ (?P<dewp>[-\d.]+)\s+ (?P<dewp_count>\d+)\s+ (?P<slp>[\d.]+)\s+ (?P<slp_count>\d+)\s+ (?P<stp>[\d.]+)\s+ (?P<stp_count>\d+)\s+ (?P<visib>[\d.]+)\s+ (?P<visib_count>\d+)\s+ (?P<wdsp>[\d.]+)\s+ (?P<wdsp_count>\d+)\s+ (?P<mxspd>[\d.]+)\s+ (?P<gust>[\d.]+)\s+ (?P<max>[-\d.]+)\*?\s+ (?P<min>[-\d.]+)\*?\s+ (?P<prcp>[\d.]+) (?P<prcp_flag>.)\s+ (?P<sndp>[\d.]+)\s+ (?P<frshtt>\d+) """ with open(file, 'r') as f: filedata = f.read() matches = re.finditer(regex, filedata, re.VERBOSE + re.MULTILINE) data_list = [i.groupdict() for i in matches] data = {} for i, dp in enumerate(data_list): # convert numbers to float and int types for key, item in dp.iteritems(): if key == 'date': dp[key] = UTC(item).toordinal() elif item is not None and key != 'frshtt': if isint(item): dp[key] = int(item) elif isfloat(item): dp[key] = float(item) if dp['stn'] == 854420: dp['station'] = 'Antofagasta' elif dp['stn'] == 854320: dp['station'] = 'Calama' elif dp['stn'] == 854180: dp['station'] = 'Iquique' else: dp['station'] = 'other' if i == 0: for key, item in dp.iteritems(): data[key] = [] for key, item in dp.iteritems(): #ipshell() data[key].append(item) np.savez(data_path + 'climate_CAL', **data) #AF, IQ
def read(cls, event_file, regex=regex): """ Read events from event_file with the help of given regular expression. :param event_file: location of event_file :param regex: regular expression, see Events.regex<Tab> """ with open(event_file, 'r') as f: filedata = f.read() event_matches = re.finditer(regex, filedata, re.VERBOSE + re.MULTILINE) list_ = [i.groupdict() for i in event_matches] #util.ipshell() for event in list_: # convert numbers to float and int types for key, item in event.iteritems(): if util.isint(item): event[key] = int(item) elif util.isfloat(item): event[key] = float(item) else: event[key] = item.strip() #if event[key] == '': # event[key] = None #if key == 'depth' and regex == cls.regex: # event[key] *= 1 #util.ipshell() log.info( 'Read event information of %d events from events event_file %s' % (len(list_), event_file)) return cls(list_)
def read_regex(event_file, regex=regex_GEOFON, creation_info='GEOFON'): """ Read events from event_file with the help of given regular expression. """ with open(event_file, 'r') as f: filedata = f.read() event_matches = re.finditer(regex, filedata, re.VERBOSE + re.MULTILINE) list_ = [i.groupdict() for i in event_matches] events = [] for event in list_: # convert numbers to float and int types for key, item in event.iteritems(): if util.isint(item): event[key] = int(item) elif util.isfloat(item): event[key] = float(item) else: event[key] = item.strip() if 'latitude_sign' in event and event['latitude_sign'] == 'S': event['latitude'] = -event['latitude'] if 'longitude_sign' in event and event['longitude_sign'] == 'W': event['longitude'] = -event['longitude'] if 'AM' in event: ci = creation_info + (' automatic' if event['AM'] == 'A' else ' manual') else: ci = creation_info ev = Event( event_type='earthquake', creation_info=ci, origins=[ Origin(time=UTC(event['time']), latitude=event['latitude'], longitude=event['longitude'], depth=event['depth']) ], magnitudes=[Magnitude(mag=event['magnitude'], magnitude_type='M')], event_descriptions=[ EventDescription(event['flinn'], 'flinn-engdahl region') ] if 'flinn' in event else None) events.append(ev) events.sort(key=lambda x: x.origins[0].time) return Catalog(events)
def read_regex(event_file, regex=regex_GEOFON, creation_info='GEOFON'): """ Read events from event_file with the help of given regular expression. """ with open(event_file, 'r') as f: filedata = f.read() event_matches = re.finditer(regex, filedata, re.VERBOSE + re.MULTILINE) list_ = [i.groupdict() for i in event_matches] events = [] for event in list_: # convert numbers to float and int types for key, item in event.iteritems(): if util.isint(item): event[key] = int(item) elif util.isfloat(item): event[key] = float(item) else: event[key] = item.strip() if 'latitude_sign' in event and event['latitude_sign'] == 'S': event['latitude'] = -event['latitude'] if 'longitude_sign' in event and event['longitude_sign'] == 'W': event['longitude'] = -event['longitude'] if 'AM' in event: ci = creation_info + (' automatic' if event['AM'] == 'A' else ' manual') else: ci = creation_info ev = Event(event_type='earthquake', creation_info=ci, origins=[Origin(time=UTC(event['time']), latitude=event['latitude'], longitude=event['longitude'], depth=event['depth'])], magnitudes=[Magnitude(mag=event['magnitude'], magnitude_type='M')], event_descriptions=[EventDescription(event['flinn'], 'flinn-engdahl region')] if 'flinn' in event else None ) events.append(ev) events.sort(key=lambda x: x.origins[0].time) return Catalog(events)
# by TR import argparse from obspy.core import UTCDateTime as UTC from sito.util import isint parser = argparse.ArgumentParser( description='Convert Julian day date to normal date or ' 'backwards.') parser.add_argument('date') parser.add_argument('-u', '--underscores', action='store_true', help='use underscores instead of minus in output') args = parser.parse_args() date = args.date under = args.underscores utc = UTC(date) if len(date) < 8 or (len(date) == 8 and not isint(date)): if under: print utc.strftime('%Y_%m_%d'), else: print utc.date, else: if under: print utc.strftime('%Y_%j'), else: print utc.strftime('%Y-%j'),
#!/usr/bin/env python # by TR import argparse from obspy.core import UTCDateTime as UTC from sito.util import isint parser = argparse.ArgumentParser(description="Convert Julian day date to normal date or " "backwards.") parser.add_argument("date") parser.add_argument("-u", "--underscores", action="store_true", help="use underscores instead of minus in output") args = parser.parse_args() date = args.date under = args.underscores utc = UTC(date) if len(date) < 8 or (len(date) == 8 and not isint(date)): if under: print utc.strftime("%Y_%m_%d"), else: print utc.date, else: if under: print utc.strftime("%Y_%j"), else: print utc.strftime("%Y-%j"),