cfjson/xrdataset.py
def from_json(self, js):
        """Convert CF-JSON string or dictionary to xarray Dataset
        Example:
        import xarray as xr
        from cfjson import xrdataset
        cfjson_string = '{"dimensions": {"time": 1}, "variables": {"x": {"shape": ["time"], "data": [1], "attributes": {}}}}'
        dataset = xr.Dataset()
        dataset.cfjson.from_json(cfjson_string)

        """

        if isinstance(js, six.string_types):
            try:
                dico = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(js)
            except:
                print('Could not decode JSON string')
                raise
        else:
            dico = js

        if 'attributes' in dico.keys():
            # Copy global attributes
            logging.debug('copying global attributes: {}'.format(dico['attributes'].items()))
            for k,v in six.iteritems(dico['attributes']):
                self._obj.attrs[k] = v
        else:
            logging.debug('no global attributes found')

        # Copy variables and their attributes and dimensions
        for varname,var in six.iteritems(dico['variables']):
            logging.debug('copying variable "{}" data'.format(varname))
            if "shape" not in var.keys():
                logging.warning("missing shape (required by CF-JSON spec >=0.2)")
                var["shape"] = []
            # Ideally we'd use udunits to find "time" variables, but tricky in
            # Python (cf_units doesn't seem to provide utScan or utIsTime)...
            if 'units' in var['attributes'] and 'ISO8601' in var['attributes']['units']:
                logging.debug('found "ISO8601" in units string, guessing time variable, converting to datetime64')
                time_strings = var['data']
                time_dt = [dateutil.parser.parse(tx) for tx in time_strings]
                # If timezone information was provided (e.g., "Z")
                if any([t.utcoffset() is not None for t in time_dt]):
                    if all([t.utcoffset() == dt.timedelta(0) for t in time_dt]):
                        # Timezone-aware, even if they're all the same timezone, would lead to dtype=object
                        time_dt = [t.replace(tzinfo=None) for t in time_dt]
                    else:
                        logging.warning('Mixed timezones (or mixed naive / aware) in input, may lead to dtype=object in output')
                self._obj[varname] = (var['shape'], time_dt)
                logging.debug('copying variable "{}" attributes: {}'.format(varname, var['attributes'].items()))
                self._obj[varname].attrs = var['attributes']
                self._obj[varname].attrs['units'] = 'Python datetime64 objects'
            else:
                if var['shape']:
                    # shape=['dim1'] (in contrast to shape=[])
                    self._obj[varname] = (var['shape'], var['data'])
                else:
                    # shape=[] is allowed, but a bit more tricky...
                    if isinstance(var['data'], list):
                        if len(var['data']) > 1:
                            msg = 'len(data) > 1 not allowed with empty / missing shape; varname: {}'.format(varname)
                            raise Exception(msg)
                        # shape=[] with data=[1.2] (in contrast to data=1.2)
                        self._obj[varname] = (var['shape'], var['data'][0])
                    else:
                        # shape=[] with data=1.2 (in contrast to data=[1.2])
                        self._obj[varname] = (var['shape'], var['data'])
                        # TODO: is shape=[] with data=[] allowed and needs to be handled?
                logging.debug('copying variable "{}" attributes: {}'.format(varname, var['attributes'].items()))
                # Some cases result in a dtype=object array with None elements,
                # but if this is just due to a mix of "null" and numeric values,
                # we can avoid some downstream problems by casting now, which
                # should also convert any None values to numpy NaN.
                if self._obj[varname].dtype == 'O':
                    dtype_set = set([type(el) for el in self._obj[varname].data.flatten()])
                    if str not in dtype_set:
                        if float not in dtype_set:
                            logging.warning('casting variable "{}" to float to preserve None / NaN, but no floats in original data'.format(varname))
                        self._obj[varname] = self._obj[varname].astype(float)

                self._obj[varname].attrs = var['attributes']
Convert CF-JSON string or dictionary to xarray Dataset
        Example:
        import xarray as xr
        from cfjson import xrdataset
        cfjson_string = '{"dimensions": {"time": 1}, "variables": {"x": {"shape": ["time"], "data": [1], "attributes": {}}}}'
        dataset = xr.Dataset()
        dataset.cfjson.from_json(cfjson_string)

        
Convert CF-JSON string or dictionary to xarray Dataset
Convert CF - JSON string or dictionary to xarray Dataset
def from_json(self, js):
        if isinstance(js, six.string_types):
            try:
                dico = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(js)
            except:
                print('Could not decode JSON string')
                raise
        else:
            dico = js
        if 'attributes' in dico.keys():
            logging.debug('copying global attributes: {}'.format(dico['attributes'].items()))
            for k,v in six.iteritems(dico['attributes']):
                self._obj.attrs[k] = v
        else:
            logging.debug('no global attributes found')
        for varname,var in six.iteritems(dico['variables']):
            logging.debug('copying variable "{}" data'.format(varname))
            if "shape" not in var.keys():
                logging.warning("missing shape (required by CF-JSON spec >=0.2)")
                var["shape"] = []
            if 'units' in var['attributes'] and 'ISO8601' in var['attributes']['units']:
                logging.debug('found "ISO8601" in units string, guessing time variable, converting to datetime64')
                time_strings = var['data']
                time_dt = [dateutil.parser.parse(tx) for tx in time_strings]
                if any([t.utcoffset() is not None for t in time_dt]):
                    if all([t.utcoffset() == dt.timedelta(0) for t in time_dt]):
                        time_dt = [t.replace(tzinfo=None) for t in time_dt]
                    else:
                        logging.warning('Mixed timezones (or mixed naive / aware) in input, may lead to dtype=object in output')
                self._obj[varname] = (var['shape'], time_dt)
                logging.debug('copying variable "{}" attributes: {}'.format(varname, var['attributes'].items()))
                self._obj[varname].attrs = var['attributes']
                self._obj[varname].attrs['units'] = 'Python datetime64 objects'
            else:
                if var['shape']:
                    self._obj[varname] = (var['shape'], var['data'])
                else:
                    if isinstance(var['data'], list):
                        if len(var['data']) > 1:
                            msg = 'len(data) > 1 not allowed with empty / missing shape; varname: {}'.format(varname)
                            raise Exception(msg)
                        self._obj[varname] = (var['shape'], var['data'][0])
                    else:
                        self._obj[varname] = (var['shape'], var['data'])
                logging.debug('copying variable "{}" attributes: {}'.format(varname, var['attributes'].items()))
                if self._obj[varname].dtype == 'O':
                    dtype_set = set([type(el) for el in self._obj[varname].data.flatten()])
                    if str not in dtype_set:
                        if float not in dtype_set:
                            logging.warning('casting variable "{}" to float to preserve None / NaN, but no floats in original data'.format(varname))
                        self._obj[varname] = self._obj[varname].astype(float)
                self._obj[varname].attrs = var['attributes']
def from_json ( self , js ) : if isinstance ( js , six . string_types ) : try : dico = json . JSONDecoder ( object_pairs_hook = OrderedDict ) . decode ( js ) except : print ( 'Could not decode JSON string' ) raise else : dico = js if 'attributes' in dico . keys ( ) : logging . debug ( 'copying global attributes: {}' . format ( dico [ 'attributes' ] . items ( ) ) ) for k , v in six . iteritems ( dico [ 'attributes' ] ) : self . _obj . attrs [ k ] = v else : logging . debug ( 'no global attributes found' ) for varname , var in six . iteritems ( dico [ 'variables' ] ) : logging . debug ( 'copying variable "{}" data' . format ( varname ) ) if "shape" not in var . keys ( ) : logging . warning ( "missing shape (required by CF-JSON spec >=0.2)" ) var [ "shape" ] = [ ] if 'units' in var [ 'attributes' ] and 'ISO8601' in var [ 'attributes' ] [ 'units' ] : logging . debug ( 'found "ISO8601" in units string, guessing time variable, converting to datetime64' ) time_strings = var [ 'data' ] time_dt = [ dateutil . parser . parse ( tx ) for tx in time_strings ] if any ( [ t . utcoffset ( ) is not None for t in time_dt ] ) : if all ( [ t . utcoffset ( ) == dt . timedelta ( 0 ) for t in time_dt ] ) : time_dt = [ t . replace ( tzinfo = None ) for t in time_dt ] else : logging . warning ( 'Mixed timezones (or mixed naive / aware) in input, may lead to dtype=object in output' ) self . _obj [ varname ] = ( var [ 'shape' ] , time_dt ) logging . debug ( 'copying variable "{}" attributes: {}' . format ( varname , var [ 'attributes' ] . items ( ) ) ) self . _obj [ varname ] . attrs = var [ 'attributes' ] self . _obj [ varname ] . attrs [ 'units' ] = 'Python datetime64 objects' else : if var [ 'shape' ] : self . _obj [ varname ] = ( var [ 'shape' ] , var [ 'data' ] ) else : if isinstance ( var [ 'data' ] , list ) : if len ( var [ 'data' ] ) > 1 : msg = 'len(data) > 1 not allowed with empty / missing shape; varname: {}' . format ( varname ) raise Exception ( msg ) self . _obj [ varname ] = ( var [ 'shape' ] , var [ 'data' ] [ 0 ] ) else : self . _obj [ varname ] = ( var [ 'shape' ] , var [ 'data' ] ) logging . debug ( 'copying variable "{}" attributes: {}' . format ( varname , var [ 'attributes' ] . items ( ) ) ) if self . _obj [ varname ] . dtype == 'O' : dtype_set = set ( [ type ( el ) for el in self . _obj [ varname ] . data . flatten ( ) ] ) if str not in dtype_set : if float not in dtype_set : logging . warning ( 'casting variable "{}" to float to preserve None / NaN, but no floats in original data' . format ( varname ) ) self . _obj [ varname ] = self . _obj [ varname ] . astype ( float ) self . _obj [ varname ] . attrs = var [ 'attributes' ]
Convert CF-JSON string or dictionary to xarray Dataset
Convert CF - JSON string or dictionary to xarray Dataset
"""Convert CF-JSON string or dictionary to xarray Dataset
        Example:
        import xarray as xr
        from cfjson import xrdataset
        cfjson_string = '{"dimensions": {"time": 1}, "variables": {"x": {"shape": ["time"], "data": [1], "attributes": {}}}}'
        dataset = xr.Dataset()
        dataset.cfjson.from_json(cfjson_string)

        """ # Copy global attributes # Copy variables and their attributes and dimensions # Ideally we'd use udunits to find "time" variables, but tricky in # Python (cf_units doesn't seem to provide utScan or utIsTime)... # If timezone information was provided (e.g., "Z") # Timezone-aware, even if they're all the same timezone, would lead to dtype=object # shape=['dim1'] (in contrast to shape=[]) # shape=[] is allowed, but a bit more tricky... # shape=[] with data=[1.2] (in contrast to data=1.2) # shape=[] with data=1.2 (in contrast to data=[1.2]) # TODO: is shape=[] with data=[] allowed and needs to be handled? # Some cases result in a dtype=object array with None elements, # but if this is just due to a mix of "null" and numeric values, # we can avoid some downstream problems by casting now, which # should also convert any None values to numpy NaN.
self
js
examples
None