Exporting averaging kernel layers

Hi! :slight_smile:

I want to export each of the layers from averaging kernel variable (TROPOMI NO2), but when I use harp.export_product function, the “tropospheric_NO2_column_number_density_avk” is suddenly missing in the exported .nc file. Any idea why?

vertical_layer_count = 34
variable_name = "tropospheric_NO2_column_number_density_avk"

for i in range(vertical_layer_count):
    try:
        operations = ';'.join([
            f'index(vertical) == {i}',  # Select the ith vertical layer
            f'keep(latitude, longitude, latitude_bounds, longitude_bounds, {variable_name})',
            'bin_spatial(1251, 35, 0.02, 2751, -15, 0.02)',
            'derive(longitude{longitude})',
            'derive(latitude{latitude})'
        ])
        
        selected_product = harp.import_product(input_file, operations=operations)

        output_file_nc = os.path.join(output_folder_nc, f"layer_{i}.nc")
        harp.export_product(selected_product, output_file_nc)
        print(f"NetCDF for layer {i} exported to: {output_file_nc}")

        # Convert the exported NetCDF to a TIFF
        nc_file = xr.open_dataset(output_file_nc)
    
        output_tif_file = os.path.join(output_folder_tif, f"layer_{i}.tif")

        if variable_name not in nc_file:
            raise KeyError(f"Variable '{variable_name}' not found in the dataset.")
        
        nc_file[variable_name].rio.to_raster(output_tif_file)
        print(f"Layer {i} successfully converted to TIFF: {output_tif_file}")

    except Exception as e:
        print(f"An error occurred while processing layer {i}: {e}")

HARP does not support regridding of averaging kernels (i.e. applying the bin_spatial operation) as this is generally not something you should do.
See also this post on some background on how to apply averaging kernels.

Thank you! After removing bin_spatial and deriving, it worked.