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

Field/domain print fails when dimension corodinate has no data #174

Closed
davidhassell opened this issue Feb 28, 2022 · 1 comment · Fixed by #175
Closed

Field/domain print fails when dimension corodinate has no data #174

davidhassell opened this issue Feb 28, 2022 · 1 comment · Fixed by #175
Assignees
Labels
bug Something isn't working
Milestone

Comments

@davidhassell
Copy link
Contributor

(First reported over at NCAS-CMS/cf-python#326 - see there for full details)

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:

>>> import cfdm
>>> f = cfdm.example_field(2)
>>> t = f.construct('time')
>>> t.del_data()
<Data(36): [1959-12-16 12:00:00, ..., 1962-11-16 00:00:00]>
>>> print(f)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-fc0364975534> in <module>
----> 1 print(f)

~/miniconda3.9/lib/python3.9/site-packages/cfdm/field.py in __str__(self)
    239             string.append(f"Field ancils    : {field_ancils}")
    240 
--> 241         string.append(str(self.domain))
    242 
    243         return "\n".join(string)

~/miniconda3.9/lib/python3.9/site-packages/cfdm/domain.py in __str__(self)
    194                 if construct_data_axes[cid] == (axis_cid,):
    195                     name = dim.identity(default=f"key%{0}")
--> 196                     y = f"{name}({dim.get_data().size})"
    197                     if y != axis_names[axis_cid]:
    198                         y = f"{name}({axis_names[axis_cid]})"

~/miniconda3.9/lib/python3.9/site-packages/cfdm/core/abstract/propertiesdata.py in get_data(self, default, _units, _fill_value)
    290                 return
    291 
--> 292             return self._default(
    293                 default, message=f"{self.__class__.__name__} has no data"
    294             )

~/miniconda3.9/lib/python3.9/site-packages/cfdm/core/abstract/container.py in _default(self, default, message)
    145                 default = type(default)(message)
    146 
--> 147             raise default
    148 
    149         return default

ValueError: DimensionCoordinate has no data

>>>

Something sensible should be displayed, instead.

@davidhassell davidhassell added the bug Something isn't working label Feb 28, 2022
@davidhassell davidhassell self-assigned this Feb 28, 2022
@davidhassell davidhassell changed the title Bug: Field/domain print fails when dimension corodinate has no data Feb 28, 2022
@davidhassell
Copy link
Contributor Author

Of the possible solutions described at NCAS-CMS/cf-python#326, I favour:

>>> print(f)
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) = 
                : latitude(5) = [-75.0, ..., 75.0] degrees_north
                : longitude(8) = [22.5, ..., 337.5] degrees_east
                : air_pressure(1) = [850.0] hPa

as it is the one which identifies the construct as "time", but doesn't introduce any other notions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant