<s> import numpy <EOL> import h5py <EOL> from worldengine . version import __version__ <EOL> from worldengine . biome import biome_name_to_index , biome_index_to_name <EOL> from worldengine . model . world import World , Step , Size , GenerationParameters <EOL> def save_world_to_hdf5 ( world , filename ) : <EOL> f = h5py . File ( filename , libver = '<STR_LIT>' , mode = '<STR_LIT:w>' ) <EOL> general_grp = f . create_group ( "<STR_LIT>" ) <EOL> general_grp [ "<STR_LIT>" ] = __version__ <EOL> general_grp [ "<STR_LIT:name>" ] = world . name <EOL> general_grp [ "<STR_LIT:width>" ] = world . width <EOL> general_grp [ "<STR_LIT>" ] = world . height <EOL> elevation_grp = f . create_group ( "<STR_LIT>" ) <EOL> elevation_ths_grp = elevation_grp . create_group ( "<STR_LIT>" ) <EOL> elevation_ths_grp [ "<STR_LIT>" ] = world . layers [ '<STR_LIT>' ] . thresholds [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> elevation_ths_grp [ "<STR_LIT>" ] = world . layers [ '<STR_LIT>' ] . thresholds [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> elevation_ths_grp [ "<STR_LIT>" ] = world . layers [ '<STR_LIT>' ] . thresholds [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] <EOL> elevation_data = elevation_grp . create_dataset ( "<STR_LIT:data>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> elevation_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> plates_data = f . create_dataset ( "<STR_LIT>" , ( world . height , world . width ) , dtype = numpy . uint16 ) <EOL> plates_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> ocean_data = f . create_dataset ( "<STR_LIT>" , ( world . height , world . width ) , dtype = numpy . bool ) <EOL> ocean_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> sea_depth_data = f . create_dataset ( "<STR_LIT>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> sea_depth_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> if world . has_biome ( ) : <EOL> biome_data = f . create_dataset ( "<STR_LIT>" , ( world . height , world . width ) , dtype = numpy . uint16 ) <EOL> for y in range ( world . height ) : <EOL> for x in range ( world . width ) : <EOL> biome_data [ y , x ] = biome_name_to_index ( world . layers [ '<STR_LIT>' ] . data [ y ] [ x ] ) <EOL> if world . has_humidity ( ) : <EOL> humidity_grp = f . create_group ( "<STR_LIT>" ) <EOL> humidity_quantiles_grp = humidity_grp . create_group ( "<STR_LIT>" ) <EOL> for k in world . layers [ '<STR_LIT>' ] . quantiles . keys ( ) : <EOL> humidity_quantiles_grp [ k ] = world . layers [ '<STR_LIT>' ] . quantiles [ k ] <EOL> humidity_data = humidity_grp . create_dataset ( "<STR_LIT:data>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> humidity_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> if world . has_irrigation ( ) : <EOL> irrigation_data = f . create_dataset ( "<STR_LIT>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> irrigation_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> if world . has_permeability ( ) : <EOL> permeability_grp = f . create_group ( "<STR_LIT>" ) <EOL> permeability_ths_grp = permeability_grp . create_group ( "<STR_LIT>" ) <EOL> permeability_ths_grp [ '<STR_LIT>' ] = world . layers [ '<STR_LIT>' ] . thresholds [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> permeability_ths_grp [ '<STR_LIT>' ] = world . layers [ '<STR_LIT>' ] . thresholds [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> permeability_data = permeability_grp . create_dataset ( "<STR_LIT:data>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> permeability_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> if world . has_watermap ( ) : <EOL> watermap_grp = f . create_group ( "<STR_LIT>" ) <EOL> watermap_ths_grp = watermap_grp . create_group ( "<STR_LIT>" ) <EOL> watermap_ths_grp [ '<STR_LIT>' ] = world . layers [ '<STR_LIT>' ] . thresholds [ '<STR_LIT>' ] <EOL> watermap_ths_grp [ '<STR_LIT>' ] = world . layers [ '<STR_LIT>' ] . thresholds [ '<STR_LIT>' ] <EOL> watermap_ths_grp [ '<STR_LIT>' ] = world . layers [ '<STR_LIT>' ] . thresholds [ '<STR_LIT>' ] <EOL> watermap_data = watermap_grp . create_dataset ( "<STR_LIT:data>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> watermap_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> if world . has_precipitations ( ) : <EOL> precipitation_grp = f . create_group ( "<STR_LIT>" ) <EOL> precipitation_ths_grp = precipitation_grp . create_group ( "<STR_LIT>" ) <EOL> precipitation_ths_grp [ '<STR_LIT>' ] = world . layers [ '<STR_LIT>' ] . thresholds [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> precipitation_ths_grp [ '<STR_LIT>' ] = world . layers [ '<STR_LIT>' ] . thresholds [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> precipitation_data = precipitation_grp . create_dataset ( "<STR_LIT:data>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> precipitation_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> if world . has_temperature ( ) : <EOL> temperature_grp = f . create_group ( "<STR_LIT>" ) <EOL> temperature_ths_grp = temperature_grp . create_group ( "<STR_LIT>" ) <EOL> th = world . layers [ '<STR_LIT>' ] . thresholds <EOL> temperature_ths_grp [ '<STR_LIT>' ] = th [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> temperature_ths_grp [ '<STR_LIT>' ] = th [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> temperature_ths_grp [ '<STR_LIT>' ] = th [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] <EOL> temperature_ths_grp [ '<STR_LIT>' ] = th [ <NUM_LIT:3> ] [ <NUM_LIT:1> ] <EOL> temperature_ths_grp [ '<STR_LIT>' ] = th [ <NUM_LIT:4> ] [ <NUM_LIT:1> ] <EOL> temperature_ths_grp [ '<STR_LIT>' ] = th [ <NUM_LIT:5> ] [ <NUM_LIT:1> ] <EOL> temperature_data = temperature_grp . create_dataset ( "<STR_LIT:data>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> temperature_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> if world . has_icecap ( ) : <EOL> icecap_data = f . create_dataset ( "<STR_LIT>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> icecap_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> if world . has_lakemap ( ) : <EOL> lake_map_data = f . create_dataset ( "<STR_LIT>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> lake_map_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> if world . has_rivermap ( ) : <EOL> river_map_data = f . create_dataset ( "<STR_LIT>" , ( world . height , world . width ) , dtype = numpy . float ) <EOL> river_map_data . write_direct ( world . layers [ '<STR_LIT>' ] . data ) <EOL> generation_params_grp = f . create_group ( "<STR_LIT>" ) <EOL> generation_params_grp [ '<STR_LIT>' ] = world . seed <EOL> generation_params_grp [ '<STR_LIT>' ] = world . n_plates <EOL> generation_params_grp [ '<STR_LIT>' ] = world . ocean_level <EOL> generation_params_grp [ '<STR_LIT>' ] = world . step . name <EOL> f . close ( ) <EOL> def _from_hdf5_quantiles ( p_quantiles ) : <EOL> quantiles = { } <EOL> for p_quantile in p_quantiles : <EOL> quantiles [ p_quantile . title ( ) ] = p_quantiles [ p_quantile ] . value <EOL> return quantiles <EOL> def _from_hdf5_matrix_with_quantiles ( p_matrix ) : <EOL> return numpy . array ( p_matrix [ '<STR_LIT:data>' ] ) , _from_hdf5_quantiles ( p_matrix [ '<STR_LIT>' ] ) <EOL> def load_world_to_hdf5 ( filename ) : <EOL> f = h5py . File ( filename , libver = '<STR_LIT>' , mode = '<STR_LIT:r>' ) <EOL> w = World ( f [ '<STR_LIT>' ] . value , <EOL> Size ( f [ '<STR_LIT>' ] . value , f [ '<STR_LIT>' ] . value ) , <EOL> f [ '<STR_LIT>' ] . value , <EOL> GenerationParameters ( f [ '<STR_LIT>' ] . value , <EOL> f [ '<STR_LIT>' ] . value , <EOL> Step . get_by_name ( f [ '<STR_LIT>' ] . value ) ) ) <EOL> e = numpy . array ( f [ '<STR_LIT>' ] ) <EOL> e_th = [ ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , None ) ] <EOL> w . set_elevation ( e , e_th ) <EOL> w . set_plates ( numpy . array ( f [ '<STR_LIT>' ] ) ) <EOL> w . set_ocean ( numpy . array ( f [ '<STR_LIT>' ] ) ) <EOL> w . set_sea_depth ( numpy . array ( f [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in f . keys ( ) : <EOL> biome_data = [ ] <EOL> for y in range ( w . height ) : <EOL> row = [ ] <EOL> for x in range ( w . width ) : <EOL> value = f [ '<STR_LIT>' ] [ y , x ] <EOL> row . append ( biome_index_to_name ( value ) ) <EOL> biome_data . append ( row ) <EOL> biome = numpy . array ( biome_data , dtype = object ) <EOL> w . set_biome ( biome ) <EOL> if '<STR_LIT>' in f . keys ( ) : <EOL> data , quantiles = _from_hdf5_matrix_with_quantiles ( f [ '<STR_LIT>' ] ) <EOL> w . set_humidity ( data , quantiles ) <EOL> if '<STR_LIT>' in f . keys ( ) : <EOL> w . set_irrigation ( numpy . array ( f [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in f . keys ( ) : <EOL> p = numpy . array ( f [ '<STR_LIT>' ] ) <EOL> p_th = [ <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , None ) <EOL> ] <EOL> w . set_permeability ( p , p_th ) <EOL> if '<STR_LIT>' in f . keys ( ) : <EOL> data = numpy . array ( f [ '<STR_LIT>' ] ) <EOL> thresholds = { } <EOL> thresholds [ '<STR_LIT>' ] = f [ '<STR_LIT>' ] . value <EOL> thresholds [ '<STR_LIT>' ] = f [ '<STR_LIT>' ] . value <EOL> thresholds [ '<STR_LIT>' ] = f [ '<STR_LIT>' ] . value <EOL> w . set_watermap ( data , thresholds ) <EOL> if '<STR_LIT>' in f . keys ( ) : <EOL> p = numpy . array ( f [ '<STR_LIT>' ] ) <EOL> p_th = [ <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , None ) <EOL> ] <EOL> w . set_precipitation ( p , p_th ) <EOL> if '<STR_LIT>' in f . keys ( ) : <EOL> t = numpy . array ( f [ '<STR_LIT>' ] ) <EOL> t_th = [ <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , f [ '<STR_LIT>' ] . value ) , <EOL> ( '<STR_LIT>' , None ) <EOL> ] <EOL> w . set_temperature ( t , t_th ) <EOL> if '<STR_LIT>' in f . keys ( ) : <EOL> w . set_icecap ( numpy . array ( f [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in f . keys ( ) : <EOL> m = numpy . array ( f [ '<STR_LIT>' ] ) <EOL> w . set_lakemap ( m ) <EOL> if '<STR_LIT>' in f . keys ( ) : <EOL> m = numpy . array ( f [ '<STR_LIT>' ] ) <EOL> w . set_rivermap ( m ) <EOL> f . close ( ) <EOL> return w </s>
