Esempio n. 1
0
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
Esempio n. 2
0
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