xarray-contrib/cf-xarray

axis attribute missing for lat/lon when using guess_coord_axis

MuellerSeb opened this issue · 6 comments

Hey there,

when using guess_coord_axis with lat/lon coords, the axis attribute is not added. The CF-Conventions states:

Optionally, the latitude type may be indicated additionally by providing the standard_name attribute with the value latitude, and/or the axis attribute with the value Y.

See: https://cfconventions.org/Data/cf-conventions/cf-conventions-1.10/cf-conventions.html#latitude-coordinate

So, the current behavior is not wrong, but I think it would be nice to have the axis attributes generated for lat/lon as well for consistency.

The issue here is that for analysis, it is useful to distinguish between 1D dimensions or axes of the array, and the actual lat, lon coordinate variables which may be 2D.

In that case, you want to be able to do .mean(dim="X") while .mean(dim="lon") would break. So it's nice to not add axis="X" to a 2D longitude variable (for example)

Of course, I wouldn't add the axis attribute, when lat/lon are auxiliary coordinate variables. But when there is a dim lat and a coordinate lat(lat), the axis attribute makes sense to me as it is mentioned in the cf-convention.

I think the reason I didn't do this was that it makes it hard to write code that generalizes for dimension vs auxiliary lat, lon variables.

Potentially we could have guess_coord_axis(x_is_lon=True, y_is_lat=True) to do what you want. I think I prefer the default to be False because of the above reason

the axis attribute makes sense to me as it is mentioned in the cf-convention.

Oooh can you post a link or the text from the conventions please?

@dcherian it is in the first post:
https://cfconventions.org/Data/cf-conventions/cf-conventions-1.10/cf-conventions.html#latitude-coordinate

If lat (or lon) is a auxiliary coordinate it of course shouldn't hold an axis attribute. But if it is a coordinate lat(lat) (or lon(lon)) it makes perfectly sense to add the axis attribute.

To be clear, it is optional:

Optionally, the latitude type may be indicated additionally by providing the standard_name attribute with the value latitude, and/or the axis attribute with the value Y.

so I still like this approach:

guess_coord_axis(x_is_lon=True, y_is_lat=True)

Yes it is optional. But I would like to get the axis (if present) without first checking if they may be lat/lon. I got a file with lat/lon/z coords but only z gets the axis attributes and lat, lon not. That seems strange to me.