-
Notifications
You must be signed in to change notification settings - Fork 0
/
allas_from_python.py
52 lines (38 loc) · 1.43 KB
/
allas_from_python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Examples on how to use files from Allas object storage directly from Puhti
Author: Kylli Ek, Samantha Wittke, CSC
'''
# The required packages depend on the task
import os
# For working with raster data
import rasterio
# For working with vector data
import geopandas as gpd
# For working with object storage
import boto3
# Before starting to use Allas with S3 set up your connection to Allas.
# In Puhti run:
#
# module load allas
# allas-conf --mode s3cmd
# Reading raster file directly from Allas using rasterio
open_raster = rasterio.open('/vsis3/name_of_your_Allas_bucket/name_of_your_input_raster_file.tif')
input_data = open_raster.read()
# Reading vector file directly from Allas using geopandas
vector_file = gpd.read_file('/vsis3/name_of_your_Allas_bucket/name_of_your_input_vector_file.gpkg')
# Setting the end point for Allas for the client
client = boto3.client('s3', endpoint_url='https://a3s.fi')
# List all buckets under own account and project
response = client.list_buckets()
for bucket in response['Buckets']:
print(bucket['Name'])
# List all objects within a bucket
bucketname = 'name_of_your_Allas_bucket'
response = client.list_objects_v2(Bucket=bucketname)
for object in response['Contents']:
if object['Key'].endswith('.tif'):
tif_file_path = f'/vsis3/{bucketname}/{object['Key']}'
print(tif_file_path)
#these files can then be opened as seen above