Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing construct data prevents str representation of field #326

Closed
sadielbartholomew opened this issue Feb 18, 2022 · 1 comment
Closed
Labels
enhancement New feature or request question General question
Milestone

Comments

@sadielbartholomew
Copy link
Member

When a field has a construct which can have data, e.g. a coordinate, but that construct doesn't have data, the str() representation hits a ValueError regarding the lack of data on that construct, e.g:

>>> f = cf.example_fields()
>>> u = f[2]
>>> print(u)
Field: air_potential_temperature (ncvar%air_potential_temperature)
------------------------------------------------------------------
Data            : air_potential_temperature(time(36), latitude(5), longitude(8)) K
Cell methods    : area: mean
Dimension coords: time(36) = [1959-12-16 12:00:00, ..., 1962-11-16 00:00:00]
                : latitude(5) = [-75.0, ..., 75.0] degrees_north
                : longitude(8) = [22.5, ..., 337.5] degrees_east
                : air_pressure(1) = [850.0] hPa
>>>
>>> u.get_construct('dimensioncoordinate0').del_data()  # delete data from a coordinate
<CF Data(36): [1959-12-16 12:00:00, ..., 1962-11-16 00:00:00]>
>>> u  # the repr is still available to view
<CF Field: air_potential_temperature(time(36), latitude(5), longitude(8)) K>
>>> print(u)  # but now try to print the field out...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sadie/anaconda3/envs/cf-env/lib/python3.8/site-packages/cfdm/field.py", line 241, in __str__
    string.append(str(self.domain))
  File "/home/sadie/anaconda3/envs/cf-env/lib/python3.8/site-packages/cfdm/domain.py", line 194, in __str__
    y = f"{name}({dim.get_data().size})"
  File "/home/sadie/anaconda3/envs/cf-env/lib/python3.8/site-packages/cf/mixin/propertiesdata.py", line 4919, in get_data
    return super().get_data(
  File "/home/sadie/anaconda3/envs/cf-env/lib/python3.8/site-packages/cfdm/core/abstract/propertiesdata.py", line 292, in get_data
    return self._default(
  File "/home/sadie/anaconda3/envs/cf-env/lib/python3.8/site-packages/cfdm/core/abstract/container.py", line 147, in _default
    raise default
ValueError: DimensionCoordinate has no data

(note the repr() still works). Whilst the error is logically correct, I feel it is not appropriate to be raised in the case of an object string representation, where it would be much more useful to display the usual output but with some indicator that there is no data for a given construct, perhaps something as simple as [] in place of the missing data, or a dedicated marker such as <no data>, e.g. for the example above:

>>> print(u)
Field: air_potential_temperature (ncvar%air_potential_temperature)
------------------------------------------------------------------
Data            : air_potential_temperature(time(36), latitude(5), longitude(8)) K
Cell methods    : area: mean
Dimension coords: time(time(36)) = []
                : latitude(5) = [-75.0, ..., 75.0] degrees_north
                : longitude(8) = [22.5, ..., 337.5] degrees_east
                : air_pressure(1) = [850.0] hPa

@davidhassell what do you think? (It may even be that the issue described is a bug?)

Interestingly, if instead of deleting the data on u, we set it to [None], I see the following:

>>> u.get_construct('dimensioncoordinate0').set_data([None])
>>> u
<CF Field: air_potential_temperature(time(36), latitude(5), longitude(8)) K>
>>> print(u)
Field: air_potential_temperature (ncvar%air_potential_temperature)
------------------------------------------------------------------
Data            : air_potential_temperature(time(36), latitude(5), longitude(8)) K
Cell methods    : area: mean
Dimension coords: time(time(36)) = 
                : latitude(5) = [-75.0, ..., 75.0] degrees_north
                : longitude(8) = [22.5, ..., 337.5] degrees_east
                : air_pressure(1) = [850.0] hPa

which may also be a bug...

@sadielbartholomew
Copy link
Member Author

Fixed by the closure of NCAS-CMS/cfdm#174.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question General question
Projects
None yet
Development

No branches or pull requests

2 participants