예제 #1
0
def create_shake_events(
        event_id=None,
        population_path=None,
        working_dir=None,
        locale='en',
        force_flag=False):
    """

    :param working_dir: The locale working dir where all the shakemaps are
            located.
    :type working_dir: str

    :param event_id: (Optional) Id of the event. Will be used to
        fetch the ShakeData for this event. The grid.xml file in the
        unpacked event will be used to initialise the state of the a
        ShakeGrid instance. If no event id is supplied, the most recent
        event recorded on working dir will be used.
    :type event_id: str

    :param locale:(Optional) string for iso locale to use for outputs.
        Defaults to en. Can also use 'id' or possibly more as translations
        are added.
    :type locale: str

    :param force_flag: Whether to force retrieval of the dataset.
    :type force_flag: bool

    :return: Shake Events to process
    :rtype: list[ShakeEvent]
    """
    shake_events = []

    if not os.path.exists(population_path):
        population_path = None

    # cron job executed this script minutely, so it is possible in one
    # minute that we have more than one shake_event. We can resolve this
    # by only retrieveng the shake id for that particular minute.

    # retrieve all the shake ids
    shake_ids = ShakeData.get_list_event_ids_from_folder(working_dir)
    shake_ids.sort()
    shake_ids.reverse()
    if not shake_ids:
        return []

    if event_id:
        shake_events.append(
            ShakeEvent(
                working_dir=working_dir,
                event_id=event_id,
                locale=locale,
                force_flag=force_flag,
                population_raster_path=population_path)
        )
    else:
        last_int = int(shake_ids[0])
        # sort descending
        for shake_id in shake_ids:
            if last_int - int(shake_id) < 100:
                shake_event = ShakeEvent(
                    working_dir=working_dir,
                    event_id=shake_id,
                    locale=locale,
                    force_flag=force_flag,
                    population_raster_path=population_path)
                shake_events.append(shake_event)
            else:
                break

    return shake_events