Deriving area while using bin_spatial

@sander.niemeijer I am using bin_spatial to regrid the data to approximately 1 km by 1 km. However, the issue I am facing is that I am unable to derive the area.

I want to obtain the final result as a GeoDataFrame with polygon geometry, as I was achieving before using bin_spatial, as shown in the image

operations_trop = ";".join([
    "tropospheric_NO2_column_number_density_validity>30",
    "latitude>31.0",
    "latitude<32.0",
    "longitude>74.0",
    "longitude<75.0",
    "keep(latitude_bounds,longitude_bounds,tropospheric_NO2_column_number_density, latitude, longitude)",
    "bin_spatial(100, 31.05, 0.008983, 100, 73.65, 0.008983)",
    "derive(latitude {latitude})",
    "derive(longitude {longitude})",
    "derive(tropospheric_NO2_column_number_density [Pmolec/cm2])",
    "derive(area {time} [km2])",
])
tropomi_NO2 = harp.import_product(file_path, operations=operations_trop)

idx = tropomi_NO2.latitude_bounds.data.shape[0]

# loop over pixels
for x in range(idx):
    lat_c = tropomi_NO2.latitude_bounds.data[x]
    lon_c = tropomi_NO2.longitude_bounds.data[x]
    poly = Polygon(zip(lon_c, lat_c))
    tropomi.loc[x, 'pixel_id'] = x
    tropomi.loc[x, 'geometry'] = poly
    tropomi.loc[x, 'NO2'] = tropomi_NO2.tropospheric_NO2_column_number_density.data[x]
    tropomi.loc[x, 'lat'] = tropomi_NO2.latitude.data[x]
    tropomi.loc[x, 'long'] = tropomi_NO2.longitude.data[x]
    tropomi.loc[x, 'pixelarea'] = tropomi_NO2.area.data[x]

@sander.niemeijer kindly help

You need to use "derive(area {latitude,longitude} [km2])" for gridded data.

@sander.niemeijer thanks but this time the shape of lat and long bounds are (2,) instead of (4,). It isn’t possible to make polygon from 2 points how the below code will work?

idx = tropomi_NO2.latitude_bounds.data.shape[0]

# loop over pixels
for x in range(idx):
    lat_c = tropomi_NO2.latitude_bounds.data[x]
    lon_c = tropomi_NO2.longitude_bounds.data[x]
    poly = Polygon(zip(lon_c, lat_c))
    tropomi.loc[x, 'pixel_id'] = x
    tropomi.loc[x, 'geometry'] = poly
    tropomi.loc[x, 'NO2'] = tropomi_NO2.tropospheric_NO2_column_number_density.data[x]
    tropomi.loc[x, 'lat'] = tropomi_NO2.latitude.data[x]
    tropomi.loc[x, 'long'] = tropomi_NO2.longitude.data[x]
    tropomi.loc[x, 'pixelarea'] = tropomi_NO2.area.data[x]

The two points are the lower left and upper right corner points of a grid cell. You should be able to turn those into a polygon that traces the 4 corner points of the cell yourself.

tried but unable to do also getting error in tropospheric_NO2_column_number_density as shape has changed (1,99,99)

Just flatten the array if you don’t want the sub-dimensions. Note that these questions are getting beyond the scope of this forum. You are asking basic python/numpy questions.

apologies I am a beginner :face_holding_back_tears: