#!/usr/bin/env python # coding: utf-8 # # Parallel Jobs # # ## Example: Airport monitoring # # - Get a Sentinel-2 clipped image for 10 airports in a country. # - Run all jobs in parallel # - Visualize the results # In[1]: import up42 import pandas as pd import geopandas as gpd from pathlib import Path # ### Random airports in Spain # Airport locations scrapped from: https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat # In[2]: country = "Spain" dat = "https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat" airports = pd.read_table(dat, sep=",", usecols=[0, 1, 3, 6, 7], names=["uid",'airport', "country", "lat", "lon"]) airports = airports[airports.country==country] airports = gpd.GeoDataFrame(airports, geometry=gpd.points_from_xy(airports.lon, airports.lat)) world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) world = world[world.name == country] airports = airports[airports.within(world.iloc[0].geometry)] display(airports.head()) # In[3]: airports=airports.sample(6) # In[4]: # Visualize locations ax = world.plot(figsize=(10,10), color='white', edgecolor='black') airports.plot(markersize=20, ax=ax, color="r") # In[5]: # Buffer airport point locations by roughly 100m airports.geometry = airports.geometry.buffer(0.001) # ### Prepare UP42 workflows # Create a new project on UP42 or use an existing one. # In[ ]: # Authenticate with UP42 up42.authenticate(project_id="123", project_api_key="456") #up42.authenticate(cfg_file="config.json") project = up42.initialize_project() # In[8]: # Increase the parallel job limit for the project. # Only works when you have added your credit card information to the UP42 account. project.update_project_settings(max_concurrent_jobs=10) # In[9]: workflow = project.create_workflow("workflow_airports", use_existing=True) # In[10]: # Fill the workflow with tasks #blocks = up42.get_blocks(basic=True) selected_block = "sobloo-s2-l1c-aoiclipped" workflow.add_workflow_tasks([selected_block]) workflow.get_workflow_tasks(basic=True) # ### Run jobs in parallel # # Queries & downloads one image per airport in parallel. # # Crude, this will soon be available in the API in one simple command! # In[11]: # Run jobs in parallel up42.settings(log=False) input_parameters_list = workflow.construct_parameters_parallel(geometries=airports.geometry.to_list(), interval_dates=[("2018-01-01","2020-12-31")], geometry_operation="bbox") # Adjust cloud cover parameter for params in input_parameters_list: params[f"{selected_block}:1"]["max_cloud_cover"] = 10 # In[11]: real_jobs = workflow.run_jobs_parallel(input_parameters_list=input_parameters_list) # In[ ]: real_jobs.download_results() # In[13]: # Visualize downloaded results real_jobs.plot_results() # In[ ]: