Skip to content

Commit

Permalink
Be more explicit about what happens to None attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
bryan-harter committed Feb 19, 2024
1 parent 3694fd4 commit 6e5cf97
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cdflib/cdfwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ def _write_var_attrs(self, f: io.BufferedWriter, varNum: int, var_attrs: Dict[st

for attr, entry in var_attrs.items():
if attr in self.gattrs:
logger.warning(f"Attribute: {attr}" + " already defined as a global attribute... Skip")
logger.warning(f"Attribute: {attr}" + " already defined as a global attribute... Skipping attribute.")
continue

if not (attr in self.attrs):
Expand All @@ -858,6 +858,7 @@ def _write_var_attrs(self, f: io.BufferedWriter, varNum: int, var_attrs: Dict[st
offset = self.attrsinfo[attrNum][2]

if entry is None:
logger.warning(f"Attribute: {attr}" + " is None type, which does not have an equivalent in CDF... Skipping attribute.")
continue

# Check if dataType was provided
Expand Down
10 changes: 8 additions & 2 deletions cdflib/xarray/xarray_to_cdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ def _variable_attribute_checker(dataset: xr.Dataset, epoch_list: List[str], term

for d in data:
for var in d:
# Ensure None of the attributes are given a type of "None"
for key, value in d[var].attrs.items():
if value is None:
_warn_or_except(f"CDF Warning: {key} was given a type of None for variable {var}. CDF does not allow None types, so {key} will be skipped.", terminate_on_warning)


# Check for VAR_TYPE
if "VAR_TYPE" not in d[var].attrs:
_warn_or_except(f"ISTP Compliance Warning: VAR_TYPE is not defined for variable {var}.", terminate_on_warning)
Expand Down Expand Up @@ -718,7 +724,7 @@ def xarray_to_cdf(
)

if os.path.isfile(file_name):
logger.warning(f"{file_name} already exists, cannot create CDF file. Returning...")
_warn_or_except(f"{file_name} already exists, cannot create CDF file. Returning...", terminate_on_warning)
return

# Make a deep copy of the data before continuing
Expand Down Expand Up @@ -810,7 +816,7 @@ def xarray_to_cdf(
var_data = _datetime_to_tt2000(d[var].data)
elif datetime64_to_cdftt2000:
if d[var].dtype.type != np.datetime64:
logger.warning(f"from_datetime64 is set, but datetime64 is not used in the {var} variable")
_warn_or_except(f"from_datetime64 is set, but datetime64 is not used in the {var} variable", terminate_on_warning)
else:
unixtime_from_datetime64 = d[var].data.astype("datetime64[ns]").astype("int64") / 1000000000
var_data = _unixtime_to_tt2000(unixtime_from_datetime64)
Expand Down

0 comments on commit 6e5cf97

Please sign in to comment.