How to apply e.g., '-1' in coda.fetch on S5P L2 products

I was trying out coda.fetch on a S5P L2 file, following the documentation at CODA Python. Unfortunately I don’t fully grasp the documentation, especially the optional use of the integers and a bit the terminology.

Would “datasetname” correspond to a netCDF variable name ?

I got the following commands to work:

s5p_rec = coda.fetch(path2file) # fetch entire file

s5p_rec_product = coda.fetch(path2file, "PRODUCT") # fetch everything below group PRODUCT

s5p_rec_cf = coda.fetch(path2file, "PRODUCT/cloud_fraction") # fetch 1 variable. Output is a numpy array.

s5p_rec_cf0 = coda.fetch(path2file, "PRODUCT/cloud_fraction[0]") # get 1 value of that variable

However, I don’t got the examples with the following syntax to work

coda.fetch(pf, "datasetname", 0)

coda.fetch(pf, "datasetname", -1, "dsr_time")

E.g., the following

s5p_rec_cf0 = coda.fetch(path2file, "PRODUCT/cloud_fraction", 0)

coda.fetch(pf, "PRODUCT/cloud_fraction", -1, 'scanline', 0) # Inspired by example here: Problem with HARP/Python within a loop - #3 by corisanucci

both return an error ValueError: number of specified indices does not match the dimensionality of the array

Part of the problem might be that I don’t know how the terms ‘dataset’, ‘field’, ‘dimension’ translate to the S5P L2 file.

If you could give working examples that would help my understanding of coda!

To know how to order the fields and array indices it is often best to inspect the file first with a codadump list command. This will show you that the cloud_fraction is stored using e.g.
/PRODUCT/cloud_fraction[1,4172,450].
Whenever there is a multi-dimensional array you will also have to provide a multidimensional index. So you can say coda.fetch(pf, "PRODUCT/cloud_fraction", [0,0,-1]) to fetch to first scanline.

Thanks! This is much more clear now.

codadump list is a commandline command. Does there exist a python interface for it that would output to e.g., a python tuple or list?

There is no function in the coda python interface that provides such a list, but you would be able to create such a function yourself by creating a coda.Cursor, inspect the type using the coda API functions, and recursively navigate through the product. This would be a nice coding exercise if you want to get more familiar with the CODA api.

Trying to.

With coda.get_fieldnames(cursor) I can get all nodes below the cursor, but how do I differentiate between variables and groups ?

By checking whether the field itself is a record or an array. Groups are records and datasets are arrays.