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)])])
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)
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