Skip to content

Commit

Permalink
Merge pull request #32 from metocean/missing_values
Browse files Browse the repository at this point in the history
Missing values and data types
  • Loading branch information
aportagain authored Aug 11, 2023
2 parents 21a3d2e + b2e5fa0 commit 70bf362
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions cfjson/xrdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CFJSONinterface(object):
def __init__(self, xarray_obj):
self._obj=xarray_obj

def to_dict(self,mapping):
def to_dict(self,mapping,**kwargs):
"""
Dumps the dataset as an ordered dictionary following the same conventions as ncdump.
"""
Expand Down Expand Up @@ -63,14 +63,24 @@ def to_dict(self,mapping):
res['variables'][varout]['shape'] = vardims
else:
res['variables'][varout]['shape'] = []

if kwargs.get("infer_dtype", False) is True:
try:
res['variables'][varout]['type'] = type(np.zeros(1, self._obj.dtypes[var].name).item()).__name__
except:
print(f"Failed to export dtype of {var}")

for att in self._obj.variables[var].attrs:
newatt=self._obj.variables[var].attrs[att]
if att not in SPECIAL_ATTRS:
newatt=self._obj.variables[var].attrs[att]
try:
newatt=float(newatt)
except:
newatt=str(newatt)
res['variables'][varout]['attributes'][att]=newatt
else:
if kwargs.get("output_special_attrs", False) is True:
res['variables'][varout][att]=newatt
except:
print('Failed to export variable %s description or attributes'%(var))
raise
Expand All @@ -92,12 +102,12 @@ def to_dict(self,mapping):

return res

def json_dumps(self, indent=2, separators=None, mapping={}, attributes={}):
def json_dumps(self, indent=2, separators=None, mapping={}, attributes={}, **kwargs):
"""
Dumps a JSON representation of the Dataset following the same conventions as ncdump.
Assumes the Dataset is CF complient.
"""
dico=self.to_dict(mapping)
dico=self.to_dict(mapping, **kwargs)
try:
dico['attributes'].update(attributes)
except:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup

NAME = "cfjson"
VERSION = "0.3.7"
VERSION = "0.4.0"

# To install the library, run the following
#
Expand Down

0 comments on commit 70bf362

Please sign in to comment.