if __name__ == "__main__": # Basic parameters. Should these be inputs? base_demog_dir = '..\\Demographic_File_Generation\\' base_demog_file = 'Nigeria_Ward_smaller_minpop5000_demographics_new.json' earth_radius = 6367 exponents = {'Source': 1, 'Destination': 1, 'Distance': 1} maxConnections = {'Local': 8, 'Air': 60, 'Regional': 30, 'Sea': 5} Types = ['Local', 'Air'] if 'Local' in Types: Types.insert(0, Types.pop( Types.index('Local'))) # Important to have local up first # Load file to DemographicsFile class dg = DemographicsFile.from_file(base_demog_dir + base_demog_file) nodes = dg.nodes.values() lats = np.deg2rad(np.array([n.lat for n in nodes])) longs = np.deg2rad(np.array([n.lon for n in nodes])) pops = np.array([n.pop for n in nodes]) nodeIDs = np.array([n.id for n in nodes]) # Calculate migration matrix distances = 2 * earth_radius * dist.squareform( dist.pdist(np.vstack((longs, lats)).T, haversine)) migration_matrix = compute_gravity_matrix(pops, distances, exponents, normalize=True) outputdict = migration_outputs_by_channel(Types, nodeIDs, distances, migration_matrix, maxConnections)
n.air_temperature = temperature_base n.rainfall = rainfall_base n.humidity = humidity_base # All our nodes have values, create the output files # For that we are going to open one of the metadata file in order to keep everything consistant meta = json.load( open( os.path.join( input_path, 'Colombia_Santander_2.5arcmin_air_temperature_daily.bin.json'), 'rb'))['Metadata'] output_path = 'outputs_Santander' if not os.path.exists(output_path): os.makedirs(output_path) cfc = ClimateFileCreator(nodes, prefix='Colombia_Santander_2.5arcmin', suffix='daily', original_data_years=meta['OriginalDataYears'], idref=meta['IdReference']) cfc.generate_climate_files(output_path) # Create the demographics file dg = DemographicsFile(nodes, base_file=base_demog_path) dg.generate_file(os.path.join(output_path, 'demographics.json')) # The files should be created -> display a message print("The output files have been created in the %s folder!" % output_path)
import os from dtk.tools.demographics.DemographicsFile import DemographicsFile from dtk.tools.migration.MigrationFile import MigrationFile # In this example we will demonstrate how to create simple migrations # All you need as an input is a demographics file with more than one node # Load the demographics file demog = DemographicsFile.from_file('inputs/demographics_for_migration.json') # Get our nodes base = demog.get_node('base') extra_1 = demog.get_node('extra_1') extra_2 = demog.get_node('extra_2') # Create some local migrations local_migrations = {base: {extra_1: .01}, extra_2: {base: .02, extra_1: .03}} # Create the output path output_path = 'outputs_migration' if not os.path.exists(output_path): os.makedirs(output_path) # Generate the migration file mig = MigrationFile(demog.idref, local_migrations) mig.generate_file(os.path.join(output_path, 'local_migrations.bin'))
OutputPath = os.path.join(load_output_path(), ScenarioName) if not os.path.exists(OutputPath): os.mkdir(OutputPath) if not os.path.exists(os.path.join(OutputPath, 'input_files')): os.mkdir(os.path.join(OutputPath, 'input_files')) #Name the input files DemoFile = os.path.join(BaseInputPath, 'demographics.json') CampaignFile = os.path.join(BaseInputPath, 'campaign.json') ConfigFile = os.path.join(BaseInputPath, 'config.json') SimDurationInYears = 1 TotalPopulation = 5e5 cb = DTKConfigBuilder.from_files(ConfigFile, campaign_name=CampaignFile) cb.set_param('Simulation_Duration', SimDurationInYears * 365.0) demoFile = DemographicsFile.from_file(DemoFile) demoFile.content['Nodes'][0]['NodeAttributes'][ 'InitialPopulation'] = TotalPopulation #Fix this if len(demoFile.nodes) == 1: for node in demoFile.nodes.values(): node.pop = TotalPopulation else: raise ValueError('demographics.json assumed to have only one node') demoFile = SetAgeDistribution( demoFile, os.path.join(load_dropbox_path(), 'COVID-19', 'seattle_network', 'census', 'age distributions', 'puma_age_dists.csv'))
intermediate_dir = os.path.join(current_dir, 'intermediate', 'climate') # Make sure we have directory created if not os.path.exists(intermediate_dir): os.makedirs(intermediate_dir) if not os.path.exists(output_path): os.makedirs(output_path) # Get a setup SetupParser.init('HPC') # Create the 2 nodes we need to pull weather for node_1001 = WeatherNode(lon=27.6, lat=-17.05, name='Node 1001', pop=1000) node_others = WeatherNode(lon=28, lat=-16.5, name='Others', pop=1000) nodes = [node_1001, node_others] # Create the file dg = DemographicsFile(nodes) climate_demog = os.path.join(intermediate_dir, 'climate_demog.json') dg.generate_file(climate_demog) cg = ClimateGenerator(demographics_file_path=climate_demog, work_order_path=os.path.join(intermediate_dir, 'wo.json'), climate_files_output_path=intermediate_dir, climate_project='IDM-Zambia', start_year='2008', num_years='1', resolution='0', idRef="Gridded world grump2.5arcmin") rain_fname, humidity_fname, temperature_fname = cg.generate_climate_files()