Keeping S5P SO2 L2 measurement time for bin_spatial

I am trying to ingest and regrid S5P SO2 products using bin_spatial. This works great, but I also need the measurement time for each grid point. I can’t figure out how to propagate this through to the final HARP product, or even access it before using bin_spatial. Does anyone have any pointers? Thanks!

bin_spatial propagates input datetime values for the full grid (not per cell).
For datetime_start variables (which is available by default in the SO2 product) you will get the minimum for your grid. For datetime (which can be derived using derive(datetime {time})) you get the average, and for datetime_stop the maximum.

If you don’t have a datetime variable available before you perform bin_spatial then you probably excluded it by using an exclude or keep operation somewhere.

Hi, thanks for your quick reply. I have the datetime_start and datetime_stop values, but they do not seem to be right for my grid. Here is an example using orbit 22976:

import harp
from cftime import num2date

# Set file to ingest
fpath = '/PATH/TO/TROPOMI/SO2'
fname = ''

# Set grid
latN, latS, lonE, lonW = [60, -60, -50, -90]
step = 0.05
nlat = int((latN - latS) / step) + 1
nlon = int((lonE - lonW) / step) + 1

# Set harp operations
operations = str(
    f"bin_spatial({nlat}, {latS}, {step}, {nlon}, {lonW}, {step});"
    "derive(SO2_column_number_density [DU]);"
    "derive(latitude {latitude});"
    "derive(longitude {longitude});"
    "derive(datetime {time});"
    "derive(datetime_stop {time} [seconds since 2010-01-01]);"
    "derive(datetime_start {time} [seconds since 2010-01-01]);"

product = harp.import_product(

start = num2date([0],
    units='seconds since 2010-01-01'
stop = num2date([0],
    units='seconds since 2010-01-01'

print(start, stop)

Using this I get start and stop times of 2022-03-20 20:39:04.195000 and 2022-03-20 20:39:05.035000. Looking at the datetime_length value, it looks like the datetime_end is just the datetime_start + datetime_length (which is 0.84s).

Looking at the TROPOMI product files, it seems the start time is right, but the end time is wrong (should be around (14:31:47).

From reading the docs I thought that datetime_end should be the end time for the full grid, have I misunderstood this, or is there something not quite right with how I am calling harp?

Thanks again for your help!

The order of operations matter. Make sure to put all datetime derivations before the bin_spatial operation. Otherwise you are using the single mininum datetime_start to perform the derivation.

The datetime_length is averaged, so you end up with the average integration time. So after binning the consistency of start+length=stop will no longer be valid.

Ah, I see now, thanks for your help sorting that out!