Skip to content
Extraits de code Groupes Projets
Valider 95a1ccf0 rédigé par Thomas Dobbelaere's avatar Thomas Dobbelaere
Parcourir les fichiers

update script intersecting arrival time with routes

parent f9b50f55
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -5,53 +5,75 @@ ...@@ -5,53 +5,75 @@
Intersects arrival times with shipping routes Intersects arrival times with shipping routes
Author: Thomas Dobbelaere, Earth and Life Institute, UCLouvain, Belgium Author: Thomas Dobbelaere, Earth and Life Institute, UCLouvain, Belgium
Last modified: 19 July 2022 Last modified: 9 November 2022
""" """
import numpy as np import numpy as np
import os
import geopandas as gpd import geopandas as gpd
import shapely
from postpro import ArrivalTimeMap
import calendar import calendar
import geopandas from typing import List
import common
basedir = "/export/miro/students/tanselain/OpenOil" # --- USEFUL VARIABLES --- #
minlon, maxlon, minlat, maxlat = 50, 53, 24, 27.5
res = 0.01
basedir = "/export/miro/students/tanselain/OpenOil/"
sources = ['ras_laffan','ras_laffan_port', 'abu_fontas', 'umm_al_houl']
routes = ['Barhain', 'Doha', 'Iran', 'Ras_laffan_field', 'Ras_laffan']
path_routes = basedir + f'/Indicators/Shipping_selected/Selected_shipping_route_2019'
def compute_arrival_routes(sources, routes, year, month, outfile): # --- USEFUL FUNCTIONS --- #
sources = common.to_iterable(sources) def compute_arrival_time(
routes = common.to_iterable(routes) df: gpd.GeoDataFrame, sources: List[str], year: int, month: str, day: int
) -> None:
atm = ArrivalTimeMap(minlon, maxlon, minlat, maxlat, res)
for src in sources:
if src == "lusail":
nc_path = basedir + f'Output_backward_lusail/{year}/{month}'
else:
nc_path = basedir + f'Output_backward/{year}/{src}/{month}'
ncfile = nc_path+f'/out_{month}_{day}.nc'
if not os.path.isfile(ncfile):
continue
atm.add_arrival_times_from_file(ncfile)
a = atm.get_arrival_time_days()
df.loc[:,'atime_days'] = a.reshape(-1)[:]
time_tag = f"{year}{month:02d}" def main() -> None:
# read arrival time
shpfile = f"shp/{time_tag}/arrival_time_percentile_{time_tag}.shp" # initialize geodataframes
df_arrival = gpd.read_file(shpfile) df_routes = gpd.read_file(path_routes)
df_arrival = df_arrival[~np.isnan(df_arrival['arrival_ti'])]
# read shipping routes
path = basedir + f'/Indicators/Shipping_selected/Selected_shipping_route_2019'
df_routes = gpd.read_file(path)
df_routes = df_routes.to_crs("EPSG:4326") df_routes = df_routes.to_crs("EPSG:4326")
df_arrival.crs = df_routes.crs data = []
# intersection lons = np.arange(minlon, maxlon+res, res)
df_inter = gpd.sjoin(df_arrival, df_routes, op='intersects') lats = np.arange(minlat, maxlat+res, res)
df_inter['dn_total']= df_inter['arrival_ti'] * df_inter['int'] for i in range(lats.size-1):
for j in range(lons.size-1):
res = [] px = lons[[j, j, j+1, j+1]]
for route in routes: py = lats[[i, i+1, i+1, i]]
df_inter_route = df_inter.loc[df_inter['Route'] == route] poly = shapely.geometry.Polygon(np.column_stack([px,py]))
mean_arrival_time = df_inter_route['arrival_ti'].mean() data.append((0.0, poly))
print(route, mean_arrival_time) df = gpd.GeoDataFrame(data, columns=['atime_days','geometry'], crs=df_routes.crs)
res.append(mean_arrival_time) with open("atime_by_routes.csv","w") as csv:
return res csv.write("year,month,day,route,arrival_time\n")
for yr in range(2016,2021):
if __name__ == "__main__": for mn in range(1,13):
sources = ['ras_laffan', 'abu_fontas', 'umm_al_houl', 'ras_laffan_port'] month = calendar.month_name[mn].lower()
routes = ['Barhain', 'Doha', 'Iran', 'Ras_laffan_field', 'Ras_laffan'] print(f'{month} {yr}')
fname = "arrival_routes_201601.png" for d in range(1,32):
a = np.empty((len(routes),12,5)) compute_arrival_time(df, sources, yr, month, d)
for year in range(2016,2021): df_arrival = df[~np.isnan(df['atime_days'])]
for month in range(1,13): if df_arrival.shape[0] == 0:
print(calendar.month_name[month], year) continue
iyr = year-2016 df_inter = gpd.sjoin(df_arrival, df_routes, op='intersects')
imn = month-1 df_inter['dn_total']= df_inter['atime_days'] * df_inter['int']
a[:,imn,iyr] = compute_arrival_routes(sources, routes, year, month, fname) for route in routes:
np.save("arrival_by_route.npy", {"routes":routes, "data":a}) df_inter_route = df_inter.loc[df_inter['Route'] == route]
v = df_inter_route['atime_days'].mean()
csv.write(f"{yr},{month},{d},{route},{v}\n")
if __name__ == '__main__':
main()
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter