I am trying to build a numpy array fromm the values of an image band in GEE to export for analysis. The roi is quite large (over the South Pacific), so several images make up each day. I have adapted this code for exporting the numpy array, and used this code to produce a mosaic of all the immages for each day in the date range.

My code:

import ee
import numpy as np

poly = ee.Geometry.Polygon(
  [[[124, -60], [124, -10], [310, -10], [310, -60]]], None,

start = ee.Date('2019-12-30')
finish = ee.Date('2020-01-17')

collection = ee.ImageCollection("COPERNICUS/S5P/OFFL/L3_AER_AI").filterDate(start, finish).select('absorbing_aerosol_index').map(lambda image: image.clip(poly))

# Difference in days between start and finish
diff = finish.difference(start, 'day')

# Make a list of all dates

range = ee.List.sequence(0, diff.subtract(1)).map(lambda day: start.advance(day,'day'))

# Funtion for iteraton over the range of dates
def day_mosaics(date, newlist):
    # Cast
    date = ee.Date(date)
    newlist = ee.List(newlist)

    # Filter collection between date and the next day
    filtered = collection.filterDate(date, date.advance(1,'day'))

    timeBand = ee.Image(date.millis()) \
        .divide(1000 * 3600 * 24) \
        .int() \

    # Make the mosaic
    image = ee.Image(filtered.mosaic()).addBands(timeBand)

    # Add the mosaic to a list only if the collection has images
    return ee.List(ee.Algorithms.If(filtered.size(), newlist.add(image), newlist))

# Iterate over the range to make a new list, and then cast the list to an imagecollection
newcol = ee.ImageCollection(ee.List(range.iterate(day_mosaics, ee.List([]))))
# print(newcol)

listOfImages =newcol.toList(newcol.size())
firstImage = ee.Image(listOfImages.get(0))
secondImage = ee.Image(listOfImages.get(1))

# convert to numpy array

band = secondImage.select('absorbing_aerosol_index').unmask(-9999).sampleRectangle(region=poly) 

band_arr = band_arrs.get('absorbing_aerosol_index')
np_arr = np.array(band_arr_b4.getInfo())

# convert to nan

np_arr[np_arr == -9999] = np.nan

import matplotlib.pyplot as plt

The output is a 50 x 186 array-I don't know the original pixel count in the image (or how to find it), but this seems too small. Furthermore, when quicly I try to visualise this array, the result is the following:

enter image description here

It appears to be upisde-down and half of the AOI is missing.

What am I doing wrong?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.