# Exporting averaging kernel layers

**URL:** https://forum.atmospherictoolbox.org/t/exporting-averaging-kernel-layers/867
**Category:** HARP
**Tags:** sentinel-5p, python
**Created:** [December 3, 2024, 7:51pm UTC](https://forum.atmospherictoolbox.org/t/exporting-averaging-kernel-layers/867 "2024-12-03T19:51:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dammoc](https://forum.atmospherictoolbox.org/letter_avatar_proxy/v4/letter/d/f6c823/32.png) [@dammoc](https://forum.atmospherictoolbox.org/u/dammoc)
#### Post date: [December 3, 2024, 7:51pm UTC](https://forum.atmospherictoolbox.org/t/exporting-averaging-kernel-layers/867/1 "2024-12-03T19:51:42Z")

</div>

Hi! 🙂

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?

```auto
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}")

```

---

<div class="post-metadata">

### Author: ![sander.niemeijer](https://forum.atmospherictoolbox.org/user_avatar/forum.atmospherictoolbox.org/sander.niemeijer/32/5_2.png) [@sander.niemeijer](https://forum.atmospherictoolbox.org/u/sander.niemeijer)
#### Post date: [December 4, 2024, 10:37am UTC](https://forum.atmospherictoolbox.org/t/exporting-averaging-kernel-layers/867/2 "2024-12-04T10:37:55Z")

</div>

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](https://forum.atmospherictoolbox.org/t/using-harp-and-cams-aq-forecast-profile-as-an-apriori-to-derive-tropomi-no2-tropospheric-column/240/2) on some background on how to apply averaging kernels.

---

<div class="post-metadata">

### Author: ![dammoc](https://forum.atmospherictoolbox.org/letter_avatar_proxy/v4/letter/d/f6c823/32.png) [@dammoc](https://forum.atmospherictoolbox.org/u/dammoc)
#### Post date: [December 4, 2024, 4:50pm UTC](https://forum.atmospherictoolbox.org/t/exporting-averaging-kernel-layers/867/3 "2024-12-04T16:50:37Z")

</div>

Thank you! After removing bin\_spatial and deriving, it worked.
