<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010-2017 GEM Foundation
#
# The OQ-CATK (Lite) is free software: you can redistribute
# it and/or modify it under the terms of the GNU Affero General Public
# License as published by the Free Software Foundation, either version
# 3 of the License, or (at your option) any later version.
#
# OQ-CATK (Lite) is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# with this download. If not, see <http://www.gnu.org/licenses/>
#
# Author: <NAME>

import copy as cp
import math as ma

import OQCatk.Catalogue as Cat
import OQCatk.CatUtils as CU

#-----------------------------------------------------------------------------------------

def AreaSelect(Db, XY, File=[], Owrite=False, Any=False, Buffer=[], Unwrap=False):

  P = CU.Polygon()

  if File:
    P.Import(XY, Type=File)
  else:
    P.Load(XY)

  if Unwrap:
    P.Unwrap()

  if Buffer:
    P.AddBuffer(Buffer)

  DbC = Cat.Database()
  DbC.Header = cp.deepcopy(Db.Header)

  for E in Db.Events:
    Event = {}
    Event['Location'] = []

    for L in E['Location']:
      x = L['Longitude'];
      y = L['Latitude']

      if Unwrap:
        x = x if x >= 0. else x+360.

      if P.IsInside(x,y):
        Event['Location'].append(L)

    if Event['Location']:
      Event['Id'] = E['Id']
      Event['Log'] = E['Log']
      Event['Magnitude'] = E['Magnitude']
      if Any:
        Event['Location'] = E['Location']
      DbC.Events.append(cp.deepcopy(Event))

  if Owrite:
    Db.Events = DbC.Events
  else:
    return DbC

#-----------------------------------------------------------------------------------------

def MagRangeSelect(Db, MinMag, MaxMag, Owrite=False, Any=False, TopEdge=False):

  DbC = Cat.Database()
  DbC.Header = cp.deepcopy(Db.Header)

  for E in Db.Events:
    Event = {}
    Event['Magnitude'] = []

    for M in E['Magnitude']:
      m = M['MagSize'];

      Chk1 = (m >= MinMag)
      if TopEdge:
        Chk2 = (m <= MaxMag)
      else:
        Chk2 = (m < MaxMag)

      if Chk1 and Chk2:
        Event['Magnitude'].append(M)

    if Event['Magnitude']:
      Event['Id'] = E['Id']
      Event['Log'] = E['Log']
      Event['Location'] = E['Location']
      if Any:
        Event['Magnitude'] = E['Magnitude']
      DbC.Events.append(cp.deepcopy(Event))

  if Owrite:
    Db.Events = DbC.Events
  else:
    return DbC

#-----------------------------------------------------------------------------------------

def DepRangeSelect(Db, MinDep, MaxDep, Owrite=False, TopEdge=False):

  if Owrite:
    DbC = Db
  else:
    DbC = Db.Copy()
<commit_msg>Fixed issue with magnitude selection for None type<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010-2017 GEM Foundation
#
# The OQ-CATK (Lite) is free software: you can redistribute
# it and/or modify it under the terms of the GNU Affero General Public
# License as published by the Free Software Foundation, either version
# 3 of the License, or (at your option) any later version.
#
# OQ-CATK (Lite) is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# with this download. If not, see <http://www.gnu.org/licenses/>
#
# Author: <NAME>

import copy as cp
import math as ma

import OQCatk.Catalogue as Cat
import OQCatk.CatUtils as CU

#-----------------------------------------------------------------------------------------

def AreaSelect(Db, XY, File=[], Owrite=False, Any=False, Buffer=[], Unwrap=False):

  P = CU.Polygon()

  if File:
    P.Import(XY, Type=File)
  else:
    P.Load(XY)

  if Unwrap:
    P.Unwrap()

  if Buffer:
    P.AddBuffer(Buffer)

  DbC = Cat.Database()
  DbC.Header = cp.deepcopy(Db.Header)

  for E in Db.Events:
    Event = {}
    Event['Location'] = []

    for L in E['Location']:
      x = L['Longitude'];
      y = L['Latitude']

      if Unwrap:
        x = x if x >= 0. else x+360.

      if P.IsInside(x,y):
        Event['Location'].append(L)

    if Event['Location']:
      Event['Id'] = E['Id']
      Event['Log'] = E['Log']
      Event['Magnitude'] = E['Magnitude']
      if Any:
        Event['Location'] = E['Location']
      DbC.Events.append(cp.deepcopy(Event))

  if Owrite:
    Db.Events = DbC.Events
  else:
    return DbC

#-----------------------------------------------------------------------------------------

def MagRangeSelect(Db, MinMag, MaxMag, Owrite=False, Any=False, TopEdge=False):

  DbC = Cat.Database()
  DbC.Header = cp.deepcopy(Db.Header)

  for E in Db.Events:
    Event = {}
    Event['Magnitude'] = []

    for M in E['Magnitude']:
      m = M['MagSize'];

      if m is not None:
        Chk1 = (m >= MinMag)
        if TopEdge:
          Chk2 = (m <= MaxMag)
        else:
          Chk2 = (m < MaxMag)

        if Chk1 and Chk2:
          Event['Magnitude'].append(M)

    if Event['Magnitude']:
      Event['Id'] = E['Id']
      Event['Log'] = E['Log']
      Event['Location'] = E['Location']
      if Any:
        Event['Magnitude'] = E['Magnitude']
      DbC.Events.append(cp.deepcopy(Event))

  if Owrite:
    Db.Events = DbC.Events
  else:
    return DbC

#-----------------------------------------------------------------------------------------

def DepRangeSelect(Db, MinDep, MaxDep, Owrite=False, TopEdge=False):

  if Owrite:
    DbC = Db
  else:
    DbC = Db.Copy()

{'fraction_non_alphanumeric': 0.21628586153581858, 'fraction_numerical': 0.006528087957395637, 'mean_word_length': 1.9841107124551511, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '466482', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1803, 'n_words': 560}