Changeset 26063


Ignore:
Timestamp:
03/10/21 01:09:38 (4 years ago)
Author:
bdef
Message:

CHG: update to netCDF import export

Location:
issm/trunk-jpl/src/m
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py

    r25819 r26063  
    33import time
    44import collections
    5 from inspect import isclass, getmodule
     5from inspect import isclass
    66from os import path, remove
    77
     
    4747    except AttributeError:  #no transient so just one timestep
    4848        StepNum = 1
     49    except TypeError:  #this isnot a result so no results in there
     50        StepNum = 0
    4951    TimeDim = NCData.createDimension('Time', StepNum)  # time is first
    5052    DimDict = {len(TimeDim): 'Time'}
     
    8082        NCgroup = NCData.createGroup(str(group))
    8183        # In each group gather the fields of the class
    82         fields = dict.keys(md.__dict__[group].__dict__)
     84        try:
     85            fields = dict.keys(md.__dict__[group].__dict__)
     86        except AttributeError:
     87            print("WARNING: md.{} as no fields, we skip it.".format(group))
     88            continue
    8389        # looping on fields in each group
    8490        for field in fields:
     
    220226                            if ncvar is not None:
    221227                                FillVar(ncvar, StackedVar)
     228                elif type(md.__dict__[group].__dict__[field]).__name__ == 'dict':
     229                    # designed for a dict in dummy but might be used elsewhere
     230                    # there is no subgroup
     231                    klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__
     232                    NCgroup.__setattr__('classtype', klass)
     233                    Var = md.__dict__[group].__dict__[field]
     234                    Var = SqueezeVar(Var)
     235                    if verbose > 4:
     236                        print("=WW=creating var for {}.{}".format(group, field))
     237                    DimDict, ncvar = CreateVar(NCData, Var, field, NCgroup, DimDict)
     238                    if ncvar is not None:
     239                        FillVar(ncvar, Var)
    222240                else:
    223241                    klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__
     
    258276    # grab dimension
    259277    if val_type in [collections.OrderedDict, dict]:
    260         val_shape = len(var)
     278        val_shape = len(var.keys())
    261279        val_dim = 2
    262280    else:
     
    304322            ncvar = Group.createVariable(str(field), nctype, dimensions=dimensions, zlib=True)
    305323    # treating bool tables and dict as string tables
    306     elif val_type in ['bool', collections.OrderedDict, dict]:
     324    elif val_type in [collections.OrderedDict, dict, 'bool']:
    307325        if val_shape in [(), (0,), 0]:
    308326            ncvar = Group.createVariable(str(field), str, zlib=True)
     
    310328            dimensions, DimDict = GetDim(NCData, val_shape, val_type, DimDict, val_dim)
    311329            ncvar = Group.createVariable(str(field), str, dimensions=dimensions, zlib=True)
    312         # Now dealing with numeric variables
     330    # Now dealing with numeric variables
    313331    elif val_type in [float, 'float64', np.float64, int, 'int64']:
    314332        if val_shape in [(), (0,), 0] and not SupDim:
  • issm/trunk-jpl/src/m/io/loadvars.py

    r25819 r26063  
    8888            #==== First we create the model structure  {{{
    8989            if debug:
    90                 print(' - Now treating classtype {}'.format(mod))
     90                print(' ==== Now treating classtype {}'.format(mod))
    9191            if np.size(classtree[mod]) > 1:
    9292                # this points to a subclass (results.TransientSolution for example)
    9393                curclass = NCFile.groups[classtree[mod][0]].groups[classtree[mod][1]]
    9494                if debug:
    95                     print("===> {} is of class {}".format(mod, classtype[mod]))
     95                    print("    ==> {} is of class {}".format(mod, classtype[mod]))
    9696                if classtype[mod][0] == 'results.solutionstep':  #Treating results {{{
    9797                    keylist = [key for key in curclass.groups]
     
    100100                    #here we have a more NC approach with time being a dimension
    101101                    listtype = split(r'\.', classtype[mod][0])[0]
    102                     print("listtype is {}".format(listtype))
    103102                    if len(NCFile.dimensions['Time']) == 1:
    104103                        nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], listtype)()
     
    126125                else:
    127126                    if debug:
    128                         print("Using the default for md.{}.{}, is that right??".format(classtree[mod][0], classtree[mod][1]))
     127                        print("    Using the default for md.{}.{}, is that right??".format(classtree[mod][0], classtree[mod][1]))
    129128                    try:
    130129                        modulename = split(r'\.', classtype[mod][0])[0]
    131130                        if debug:
    132                             print("trying to import {} from {}".format(classtype[mod][0], modulename))
     131                            print("    trying to import {} from {}".format(classtype[mod][0], modulename))
    133132                        nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], modulename)()
    134133                    except AttributeError:
     
    141140                Tree = nvdict['md'].__dict__[classtree[mod][0]]
    142141            if debug:
    143                 print("for {} Tree is a {}".format(mod, Tree.__class__.__name__))
     142                print("    for {} Tree is a {}".format(mod, Tree.__class__.__name__))
    144143            # }}}
    145144            #==== Then we populate it {{{
     
    152151                for var in listclass.variables:
    153152                    if debug:
    154                         print("treating var {}".format(var))
     153                        print("    ==> treating var {}".format(var))
    155154                    if var not in ['errlog', 'outlog']:
    156155                        varval = listclass.variables[str(var)]
     
    234233                            elif vardim == 2:
    235234                                #dealling with dict
    236                                 if varval.dtype == str:  #that is for toolkits wich needs to be ordered
     235                                if varval.dtype == str:  #that is for dictionaries
    237236                                    if any(varval[:, 0] == 'toolkit'):  #toolkit definition have to be first
    238237                                        Tree.__dict__[str(var)] = OrderedDict([('toolkit', str(varval[np.where(varval[:, 0] == 'toolkit')[0][0], 1]))])
     
    240239                                        strings2 = [str(arg[1]) for arg in varval if arg[0] != 'toolkits']
    241240                                        Tree.__dict__[str(var)].update(list(zip(strings1, strings2)))
     241                                    else:
     242                                        strings1 = [str(arg[0]) for arg in varval]
     243                                        strings2 = [str(arg[1]) for arg in varval]
     244                                        Tree.__dict__[str(var)] = OrderedDict(list(zip(strings1, strings2)))
    242245                                else:
    243246                                    if type(Tree) == list:
     
    264267                for attr in listclass.ncattrs():
    265268                    if debug:
    266                         print("treating attribute {}".format(attr))
     269                        print("      ==> treating attribute {}".format(attr))
    267270                    if attr != 'classtype':  #classtype is for treatment, don't get it back
    268271                        attribute = str(attr).swapcase()  #there is a reason for swapcase, no sure what it isanymore
     
    271274                        if type(Tree) == list and NewFormat:
    272275                            if debug:
    273                                 print("printing with index 0")
     276                                print("        printing with index 0")
    274277                            if listtype == 'dict':
    275278                                Tree[0][attribute] = str(listclass.getncattr(attr))
     
    279282                            t = int(indexlist[i])
    280283                            if debug:
    281                                 print("printing with index {]".format(t))
     284                                print("        printing with index {}".format(t))
    282285                            if listtype == 'dict':
    283286                                Tree[t][attribute] = str(listclass.getncattr(attr))
Note: See TracChangeset for help on using the changeset viewer.