コード例 #1
0
ファイル: views.py プロジェクト: pombredanne/lizard-damage
def damage_scenario_from_type_0(all_form_data):
    damage_scenario = DamageScenario(
        name=all_form_data['name'], email=all_form_data['email'],
        scenario_type=all_form_data['scenario_type'],
        calc_type=all_form_data['calc_type'])
    if all_form_data['damagetable']:
        damage_scenario.damagetable=all_form_data['damagetable']
    damage_scenario.save()
    repairtime_roads = float(all_form_data['repairtime_roads']) * 3600 * 24
    repairtime_buildings = float(all_form_data['repairtime_buildings']) * 3600 * 24
    damage_event = damage_scenario.damageevent_set.create(
        floodtime=all_form_data['floodtime'] * 3600,
        repairtime_roads=repairtime_roads,
        repairtime_buildings=repairtime_buildings,
        floodmonth=all_form_data['floodmonth'])
    damage_event.damageeventwaterlevel_set.create(
        waterlevel=all_form_data['waterlevel'],
        index=1
        )
    return damage_scenario
コード例 #2
0
def damage_scenario_from_type_0(all_form_data):
    return DamageScenario.setup(
        name=all_form_data['name'],
        email=all_form_data['email'],
        scenario_type=all_form_data['scenario_type'],
        calc_type=all_form_data['calc_type'],
        customheights=all_form_data.get('customheights_file'),
        customlanduse=all_form_data.get('customlanduse_file'),
        damagetable=all_form_data.get('damagetable'),
        damage_events=[dict(
            floodtime_hours=all_form_data['floodtime'],
            repairtime_roads_days=all_form_data['repairtime_roads'],
            repairtime_buildings_days=all_form_data['repairtime_buildings'],
            floodmonth=all_form_data['floodmonth'],
            repetition_time=all_form_data.get('repetition_time'),
            waterlevels=[dict(
                waterlevel=all_form_data['waterlevel'],
                index=1)])])
コード例 #3
0
def damage_scenario_from_uniform_levels_batch_type(all_form_data):
    events = []
    tempdir = tempfile.mkdtemp()
    base_waterlevel_file = all_form_data['waterlevel_file']
    increment = all_form_data['increment']
    start_level = all_form_data['start_level']
    for index in range(all_form_data['number_of_increments']):
        desired_level = start_level + index * increment
        level_filename = os.path.join(tempdir, 'waterlevel_%s.tif' % desired_level)
        # ^^^ 'waterlevel_' should be retained as prefix, this is needed for
        # re-assembling the output afterwards.
        # gdal_calc.py -A in.asc --calc 2.2 --NoDataValue=-9999 --outfile out.tif
        cmd = ("gdal_calc.py -A %s --calc %s "
               "--NoDataValue=-9999 --outfile %s")
        logger.debug("Generating water level file (at %s) for batch: %s",
                     desired_level, level_filename)
        os.system(cmd % (base_waterlevel_file, desired_level, level_filename))
        event = dict(
            floodtime_hours=all_form_data['floodtime'],
            repairtime_roads_days=all_form_data['repairtime_roads'],
            repairtime_buildings_days=all_form_data['repairtime_buildings'],
            floodmonth=all_form_data['floodmonth'],
            repetition_time=all_form_data.get('repetition_time'),
            waterlevels=[{'waterlevel': level_filename,
                          'index': index + 1}])
        events.append(event)

    return DamageScenario.setup(
        name=all_form_data['name'],
        email=all_form_data['email'],
        scenario_type=all_form_data['scenario_type'],
        calc_type=all_form_data['calc_type'],
        ahn_version=all_form_data['ahn_version'],
        customheights=all_form_data.get('customheights_file'),
        customlanduse=all_form_data.get('customlanduse_file'),
        damagetable=all_form_data.get('damagetable'),
        damage_events=events)
コード例 #4
0
ファイル: views.py プロジェクト: pombredanne/lizard-damage
def unpack_zipfile_into_scenario(zipfile, scenario_name='', scenario_email=''):
    """
    Create scenario structure from (user uploaded) zip file

    TODO: make a class for index.csv
    (so we can use it in analyze_zip_file as well)
    """
    with ZipFile(zipfile, 'r') as myzip:
        index = myzip.read('index.csv')
        index_data = [line.strip().split(',') for line in index.split('\n') if line.strip()]

        scenario_data = {}
        if scenario_name:
            scenario_data['name'] = scenario_name
        if scenario_email:
            scenario_data['email'] = scenario_email
        damage_table = None
        for line in index_data:
            #print '%r' % line[0]
            if line[0] == 'scenario_name':
                if not scenario_data['name']:
                    scenario_data['name'] = line[1]
            elif line[0] == 'scenario_email':
                if not scenario_data['email']:
                    scenario_data['email'] = line[1]
            elif line[0] == 'scenario_type':
                scenario_data['scenario_type'] = int(line[1])
            elif line[0] == 'scenario_calc_type':
                scenario_data['calc_type'] = {'min': 1, 'max': 2, 'avg': 3}.get(line[1].lower(), 'max')
            elif line[0] == 'scenario_damage_table':
                if line[1]:
                    zip_temp = tempfile.mkdtemp()
                    myzip.extract(line[1], zip_temp)  # extract to temp dir
                    damage_table = os.path.join(zip_temp, line[1])
            elif line[0] == 'event_name':
                # Header for second part: create damage_scenario object
                logger.info('Create a damage scenario using %r' % scenario_data)
                damage_scenario = DamageScenario(**scenario_data)
                damage_scenario.save()
                if damage_table:
                    logger.info('adding damage table...')
                    with open(damage_table) as damage_table_file:
                        damage_scenario.damagetable.save(
                            os.path.basename(damage_table),
                            File(damage_table_file), save=True)
            else:
                # This is an event
                damage_event = DamageEvent(
                    name=line[0],
                    scenario=damage_scenario,
                    floodtime=float(line[2]) * 3600,
                    repairtime_roads=float(line[3]) * 3600 * 24,
                    repairtime_buildings=float(line[4]) * 3600 * 24,
                    floodmonth=line[5]
                    )
                if line[6]:
                    damage_event.repetition_time = line[6]
                damage_event.save()

                if scenario_data['scenario_type'] == 2:
                    zip_file_names = myzip.namelist()
                    re_match = re.match(
                        '(.*[^0-9])([0-9]+)(\.asc)$', line[1]).groups()  # i.e. ('ws', '324', '.asc')

                    re_pattern = re.compile('%s[0-9]+%s' % (re_match[0], re_match[2]))
                    water_level_filenames = [
                        fn for fn in zip_file_names if re.match(re_pattern, fn)]
                    water_level_filenames.sort()
                else:
                    water_level_filenames = [line[1], ]

                for index, water_level_filename in enumerate(water_level_filenames):
                    water_level_tempdir = tempfile.mkdtemp()
                    myzip.extract(water_level_filename, water_level_tempdir)
                    tempfilename = os.path.join(water_level_tempdir, water_level_filename)
                    with open(tempfilename) as water_level_tempfile:
                        damage_event_waterlevel = DamageEventWaterlevel(event=damage_event, index=index)
                        damage_event_waterlevel.waterlevel.save(
                            water_level_filename, File(water_level_tempfile), save=True)
                        damage_event_waterlevel.save()
                    os.remove(tempfilename)
    return damage_scenario
コード例 #5
0
def unpack_zipfile_into_scenario(zipfile, scenario_name='', scenario_email=''):
    """
    Create scenario structure from (user uploaded) zip file
    """
    zip_temp = tempfile.mkdtemp()
    with ZipFile(zipfile, 'r') as myzip:
        config = BatchConfig(myzip.open('index.csv').readlines())

        scenario_data = {
            'name': getattr(config, 'scenario_name', scenario_name),
            'email': getattr(config, 'scenario_email', scenario_email),
            'scenario_type': int(config.scenario_type),
            'ahn_version': getattr(config, 'ahn_version', '2'),
            'customheights': None,
            'customlanduse': None,
            'damagetable': None,
            'damage_events': []
        }
        scenario_data['calc_type'] = {
            'min': 1, 'max': 2, 'avg': 3,
        }.get(config.scenario_calc_type.lower(), 2)

        if config.scenario_damage_table:
            # extract to temp dir
            myzip.extract(config.scenario_damage_table, zip_temp)
            scenario_data['damagetable'] = os.path.join(
                zip_temp, config.scenario_damage_table)

        for event in config.events:
            # This is an event
            damage_event = {
                'name': event['event_name'],
                'floodtime_hours': event['floodtime'],
                'repairtime_roads_days': event['repairtime_roads'],
                'repairtime_buildings_days': event['repairtime_buildings'],
                'floodmonth': event['floodmonth'],
                'waterlevels': [],
                'repetition_time': event.get('repetition_time', None) or None
            }
            scenario_data['damage_events'].append(damage_event)

            if scenario_data['scenario_type'] == 2:
                # guess the waterlevel names from the zip
                zip_file_names = myzip.namelist()
                re_match = re.match(
                    '(.*[^0-9])([0-9]+)(\.asc)$',
                    event['waterlevel']).groups()  # i.e. ('ws', '324', '.asc')

                re_pattern = re.compile(
                    '%s[0-9]+%s' % (re_match[0], re_match[2]))
                water_level_filenames = [
                    fn for fn in zip_file_names
                    if re.match(re_pattern, fn)]
                water_level_filenames.sort()
            else:
                water_level_filenames = [event['waterlevel']]

            for index, water_level_filename in enumerate(
                    water_level_filenames):
                myzip.extract(water_level_filename, zip_temp)
                tempfilename = os.path.join(
                    zip_temp, water_level_filename)

                # check area
                area = get_area_with_data(gdal.Open(tempfilename))
                max_area = settings.LIZARD_DAMAGE_MAX_WATERLEVEL_SIZE
                if area > max_area:
                    # cleanup and raise
                    shutil.rmtree(zip_temp)
                    raise AreaError(
                        name=water_level_filename,
                        area='{:.0f}'.format(area / 1000000.),
                    )

                damage_event['waterlevels'].append({
                    'waterlevel': tempfilename,
                    'index': index
                })
        damage_scenario = DamageScenario.setup(**scenario_data)

    shutil.rmtree(zip_temp)
    return damage_scenario