def _get_time_segments(starttime, endtime, minmag): if starttime is None: starttime = HistoricTime.utcnow() - timedelta(days=30) if endtime is None: endtime = HistoricTime.utcnow() # earthquake frequency table: minmag:earthquakes per day freq_table = {0: 3000 / 7, 1: 3500 / 14, 2: 3000 / 18, 3: 4000 / 59, 4: 9000 / 151, 5: 3000 / 365, 6: 210 / 365, 7: 20 / 365, 8: 5 / 365, 9: 0.05 / 365} floormag = int(np.floor(minmag)) ndays = (endtime - starttime).days + 1 freq = freq_table[floormag] nsegments = int(np.ceil((freq * ndays) / SEARCH_LIMIT)) days_per_segment = int(np.ceil(ndays / nsegments)) segments = [] startseg = starttime endseg = starttime while startseg <= endtime: endseg = min(endtime, startseg + timedelta(days_per_segment)) segments.append((startseg, endseg)) startseg += timedelta(days=days_per_segment, microseconds=1) return segments
def _get_time_segments(starttime, endtime, minmag): if starttime is None: starttime = HistoricTime.utcnow() - timedelta(days=30) if endtime is None: endtime = HistoricTime.utcnow() # carve out an exception here for historic events (pre-1900), # as there are only a few hundred of these in the database. if endtime < datetime(1951, 1, 1): return [(starttime, endtime)] # earthquake frequency table: minmag:earthquakes per day freq_table = { 0: 10000 / 7, 1: 3500 / 14, 2: 3000 / 18, 3: 4000 / 59, 4: 9000 / 151, 5: 3000 / 365, 6: 210 / 365, 7: 20 / 365, 8: 5 / 365, 9: 0.05 / 365 } floormag = int(np.floor(minmag)) ndays = (endtime - starttime).days + 1 freq = freq_table[floormag] nsegments = int(np.ceil((freq * ndays) / SEARCH_LIMIT)) days_per_segment = int(np.ceil(ndays / nsegments)) segments = [] startseg = starttime endseg = starttime while startseg <= endtime: endseg = startseg + timedelta(days_per_segment) if endseg > endtime: endseg = endtime segments.append((startseg, endseg)) startseg += timedelta(days=days_per_segment, microseconds=1) return segments
def read_event_file(eventxml): """ Read event.xml file from disk, returning a dictionary of attributes. Input XML format looks like this: .. code-block:: xml <earthquake id="2008ryan " lat="30.9858" lon="103.3639" mag="7.9" year="2008" month="05" day="12" hour="06" minute="28" second="01" timezone="GMT" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" /> Args: eventxml (str): Path to event XML file OR file-like object. Returns: dict: Dictionary with keys: - eventsourcecode Origin network and origin code (i.e., us2017abcd). - eventsource Origin network ("us"). - time Origin time as an HistoricTime object. - lat Origin latitude - lon Origin longitude - depth Origin depth - mag Origin magnitude - created Process time as an HistoricTime object. - locstring Location string - mechanism Moment mechanism, one of: - 'RS' (Reverse) - 'SS' (Strike-Slip) - 'NM' (Normal) - 'ALL' (Undetermined) """ # fill in default values for mechanism, rake and dip # these may be overriden by values in event.xml, source.txt, or by values # passed in after reading input data. # event = {'mech': DEFAULT_MECH, # 'rake': DEFAULT_RAKE, # 'dip': DEFAULT_DIP} if isinstance(eventxml, str): root = minidom.parse(eventxml) else: data = eventxml.read() root = minidom.parseString(data) # Turn XML content into dictionary eq = root.getElementsByTagName('earthquake')[0] xmldict = dict(eq.attributes.items()) root.unlink() eqdict = {} eqdict['eventsourcecode'] = xmldict['id'] if 'network' in xmldict: eqdict['eventsource'] = xmldict['network'] else: eqdict['eventsource'] = 'us' #?? #look for the productcode attribute if 'productcode' in xmldict: eqdict['productcode'] = xmldict['productcode'] # fix eventsourcecode if not specified correctly if not eqdict['eventsourcecode'].startswith(eqdict['eventsource']): eqdict['eventsourcecode'] = eqdict['eventsource'] + eqdict['eventsourcecode'] year = int(xmldict['year']) month = int(xmldict['month']) day = int(xmldict['day']) hour = int(xmldict['hour']) minute = int(xmldict['minute']) second = int(xmldict['second']) microseconds = int((second - int(xmldict['second']))*1e6) eqdict['time'] = HistoricTime(year,month,day,hour,minute,second,microseconds) eqdict['lat'] = float(xmldict['lat']) eqdict['lon'] = float(xmldict['lon']) eqdict['depth'] = float(xmldict['depth']) eqdict['mag'] = float(xmldict['mag']) # make created field in event.xml optional - set to current UTC time if not # supplied. if 'created' in xmldict: eqdict['created'] = HistoricTime.utcfromtimestamp(int(xmldict['created'])) else: eqdict['created'] = HistoricTime.utcnow() eqdict['locstring'] = xmldict['locstring'] if 'mech' in xmldict: eqdict['mech'] = xmldict['mech'] return eqdict
def read_event_file(eventxml): """ Read event.xml file from disk, returning a dictionary of attributes. Input XML format looks like this: .. code-block:: xml <earthquake id="2008ryan " lat="30.9858" lon="103.3639" mag="7.9" year="2008" month="05" day="12" hour="06" minute="28" second="01" timezone="GMT" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" /> Args: eventxml (str): Path to event XML file OR file-like object. Returns: dict: Dictionary with keys: - eventsourcecode Origin network and origin code (i.e., us2017abcd). - eventsource Origin network ("us"). - time Origin time as an HistoricTime object. - lat Origin latitude - lon Origin longitude - depth Origin depth - mag Origin magnitude - created Process time as an HistoricTime object. - locstring Location string - mechanism Moment mechanism, one of: - 'RS' (Reverse) - 'SS' (Strike-Slip) - 'NM' (Normal) - 'ALL' (Undetermined) """ # fill in default values for mechanism, rake and dip # these may be overriden by values in event.xml, source.txt, or by values # passed in after reading input data. # event = {'mech': DEFAULT_MECH, # 'rake': DEFAULT_RAKE, # 'dip': DEFAULT_DIP} if isinstance(eventxml, str): root = minidom.parse(eventxml) else: data = eventxml.read() root = minidom.parseString(data) # Turn XML content into dictionary eq = root.getElementsByTagName('earthquake')[0] xmldict = dict(eq.attributes.items()) root.unlink() eqdict = {} eqdict['eventsourcecode'] = xmldict['id'] if 'network' in xmldict: eqdict['eventsource'] = xmldict['network'] else: eqdict['eventsource'] = 'us' #?? #look for the productcode attribute if 'productcode' in xmldict: eqdict['productcode'] = xmldict['productcode'] # fix eventsourcecode if not specified correctly if not eqdict['eventsourcecode'].startswith(eqdict['eventsource']): eqdict['eventsourcecode'] = eqdict['eventsource'] + eqdict[ 'eventsourcecode'] year = int(xmldict['year']) month = int(xmldict['month']) day = int(xmldict['day']) hour = int(xmldict['hour']) minute = int(xmldict['minute']) second = int(xmldict['second']) microseconds = int((second - int(xmldict['second'])) * 1e6) eqdict['time'] = HistoricTime(year, month, day, hour, minute, second, microseconds) eqdict['lat'] = float(xmldict['lat']) eqdict['lon'] = float(xmldict['lon']) eqdict['depth'] = float(xmldict['depth']) eqdict['mag'] = float(xmldict['mag']) # make created field in event.xml optional - set to current UTC time if not # supplied. if 'created' in xmldict: eqdict['created'] = HistoricTime.utcfromtimestamp( int(xmldict['created'])) else: eqdict['created'] = HistoricTime.utcnow() eqdict['locstring'] = xmldict['locstring'] if 'mech' in xmldict: eqdict['mech'] = xmldict['mech'] return eqdict