cfjson/xrdataset.py
def to_dict(self,mapping):
        """
        Dumps the dataset as an ordered dictionary following the same conventions as ncdump.
        """
        res=OrderedDict()
        try:
            res['dimensions']=OrderedDict()
            for dim in self._obj.dims:
                if self._obj.dims[dim]>1:
                    res['dimensions'][dim]=self._obj.dims[dim]
        except:
            print('Failed to export dimensions')
            raise
        
        try:
            res['attributes']=OrderedDict()
            res['attributes'].update(self._obj.attrs)
        except:
            print('Failed to export all global_attribute %s'%(att))
            
        res['variables']=OrderedDict()
        #Put axis variables first
        for special_var in AXIS_VAR:
            if special_var in self._obj.variables.keys():
                res['variables'][special_var]=None
        for var in self._obj.variables:
            try:
                if var=='dum1': #This is a UDS artefact
                    continue
                if var=='time':
                    res['variables']['time']={
                        'shape':['time'],
                        'attributes':{'units':'ISO8601 datetimes'}
                    }
                    continue
                vardims=[d for d in self._obj.variables[var].dims if d in res['dimensions']]
                varout=mapping.get(var,var)
                res['variables'][varout]={'attributes':OrderedDict()}
                if vardims:
                    res['variables'][varout]['shape'] = vardims
                else:
                    res['variables'][varout]['shape'] = []
                for att in self._obj.variables[var].attrs:
                    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
            except:
                print('Failed to export variable %s description or attributes'%(var))
                raise

        for var in self._obj.variables:
            varout=mapping.get(var,var)
            try:
                if var=='dum1':
                    continue
                rawvals=np.atleast_1d(self._obj.variables[var].values.squeeze())
                if var == 'time':
                    vals=[t.strftime('%Y-%m-%dT%H:%M:%SZ') for t in to_datetime(rawvals)]
                    res['variables'][varout]['data']=vals
                else:
                    res['variables'][varout]['data']=rawvals.tolist()
            except:
                  print('Failed to export values for variable %s'%(var))
                  raise

        return res

        Dumps the dataset as an ordered dictionary following the same conventions as ncdump.
        
Dumps the dataset as an ordered dictionary following the same conventions as ncdump.
Dumps the dataset as an ordered dictionary following the same conventions as ncdump .
def to_dict(self,mapping):
        res=OrderedDict()
        try:
            res['dimensions']=OrderedDict()
            for dim in self._obj.dims:
                if self._obj.dims[dim]>1:
                    res['dimensions'][dim]=self._obj.dims[dim]
        except:
            print('Failed to export dimensions')
            raise
        try:
            res['attributes']=OrderedDict()
            res['attributes'].update(self._obj.attrs)
        except:
            print('Failed to export all global_attribute %s'%(att))
        res['variables']=OrderedDict()
        for special_var in AXIS_VAR:
            if special_var in self._obj.variables.keys():
                res['variables'][special_var]=None
        for var in self._obj.variables:
            try:
                if var=='dum1': 
                    continue
                if var=='time':
                    res['variables']['time']={
                        'shape':['time'],
                        'attributes':{'units':'ISO8601 datetimes'}
                    }
                    continue
                vardims=[d for d in self._obj.variables[var].dims if d in res['dimensions']]
                varout=mapping.get(var,var)
                res['variables'][varout]={'attributes':OrderedDict()}
                if vardims:
                    res['variables'][varout]['shape'] = vardims
                else:
                    res['variables'][varout]['shape'] = []
                for att in self._obj.variables[var].attrs:
                    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
            except:
                print('Failed to export variable %s description or attributes'%(var))
                raise
        for var in self._obj.variables:
            varout=mapping.get(var,var)
            try:
                if var=='dum1':
                    continue
                rawvals=np.atleast_1d(self._obj.variables[var].values.squeeze())
                if var == 'time':
                    vals=[t.strftime('%Y-%m-%dT%H:%M:%SZ') for t in to_datetime(rawvals)]
                    res['variables'][varout]['data']=vals
                else:
                    res['variables'][varout]['data']=rawvals.tolist()
            except:
                  print('Failed to export values for variable %s'%(var))
                  raise
        return res
def to_dict ( self , mapping ) : res = OrderedDict ( ) try : res [ 'dimensions' ] = OrderedDict ( ) for dim in self . _obj . dims : if self . _obj . dims [ dim ] > 1 : res [ 'dimensions' ] [ dim ] = self . _obj . dims [ dim ] except : print ( 'Failed to export dimensions' ) raise try : res [ 'attributes' ] = OrderedDict ( ) res [ 'attributes' ] . update ( self . _obj . attrs ) except : print ( 'Failed to export all global_attribute %s' % ( att ) ) res [ 'variables' ] = OrderedDict ( ) for special_var in AXIS_VAR : if special_var in self . _obj . variables . keys ( ) : res [ 'variables' ] [ special_var ] = None for var in self . _obj . variables : try : if var == 'dum1' : continue if var == 'time' : res [ 'variables' ] [ 'time' ] = { 'shape' : [ 'time' ] , 'attributes' : { 'units' : 'ISO8601 datetimes' } } continue vardims = [ d for d in self . _obj . variables [ var ] . dims if d in res [ 'dimensions' ] ] varout = mapping . get ( var , var ) res [ 'variables' ] [ varout ] = { 'attributes' : OrderedDict ( ) } if vardims : res [ 'variables' ] [ varout ] [ 'shape' ] = vardims else : res [ 'variables' ] [ varout ] [ 'shape' ] = [ ] for att in self . _obj . variables [ var ] . attrs : 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 except : print ( 'Failed to export variable %s description or attributes' % ( var ) ) raise for var in self . _obj . variables : varout = mapping . get ( var , var ) try : if var == 'dum1' : continue rawvals = np . atleast_1d ( self . _obj . variables [ var ] . values . squeeze ( ) ) if var == 'time' : vals = [ t . strftime ( '%Y-%m-%dT%H:%M:%SZ' ) for t in to_datetime ( rawvals ) ] res [ 'variables' ] [ varout ] [ 'data' ] = vals else : res [ 'variables' ] [ varout ] [ 'data' ] = rawvals . tolist ( ) except : print ( 'Failed to export values for variable %s' % ( var ) ) raise return res
Dumps the dataset as an ordered dictionary following the same conventions as ncdump.
Dumps the dataset as an ordered dictionary following the same conventions as ncdump .
"""
        Dumps the dataset as an ordered dictionary following the same conventions as ncdump.
        """ #Put axis variables first #This is a UDS artefact
self
mapping