I have this code that works, but takes approximately one hour to complete, is there any way to speed it up?
I am trying to create a table/dataframe with the band values for each pixel, as well as the lat/long to import into a postgres database
import pandas as pd
from snappy import ProductIO, GeoPos, PixelPos
import re
import numpy as np
import time
# Measure execution time
start_time = time.time()
product_path = r"E:\PhD\gis_stuff\Lai_Processed\Subset_S2A_MSIL2A_20180420T112121_N0207_R037_T30UVD_20180420T132427_resampled_BandMath.dim"
product = ProductIO.readProduct(product_path)
# Define the list of bands to retrieve
bands_to_retrieve = ['lai', 'ndvi', 'fapar', 'fcover', 'gndvi', 'gci', 'EVI', 'EVI2', 'lai2']
# Create an empty DataFrame to store the data
data = {'Date': [], 'X': [], 'Y': [], 'Latitude': [], 'Longitude': []}
for band in bands_to_retrieve:
data[band] = []
df = pd.DataFrame(data)
# Extract date from the filename
match = re.search(r'L2A_(.*?)T', product_path)
if match:
date = match.group(1)
else:
date = None
# Get the width and height of the product
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()
# Get geocoding
gc = product.getSceneGeoCoding()
# Iterate over each pixel
for y in range(height):
for x in range(width):
print(x,y)
# Get geo-coordinates for each pixel
geoPos = gc.getGeoPos(PixelPos(x, y), None)
lat = geoPos.getLat()
lon = geoPos.getLon()
# Retrieve values for each band
band_values = {}
for band_name in bands_to_retrieve:
band = product.getBand(band_name)
band_array = np.zeros(1, dtype=np.float32)
band.readPixels(x, y, 1, 1, band_array)
band_values[band_name] = band_array[0]
# Append data to the DataFrame
df = df.append({'Date': date, 'X': x, 'Y': y, 'Latitude': lat, 'Longitude': lon, **band_values}, ignore_index=True)
# Save the DataFrame to a CSV file
csv_filename = f"{date}.csv"
df.to_csv(csv_filename, index=False)
# Calculate and print the execution time
end_time = time.time()
execution_time = end_time - start_time
print(f"Execution time: {execution_time} seconds")
...