Changeset 25819


Ignore:
Timestamp:
12/03/20 05:51:00 (4 years ago)
Author:
bdef
Message:

CHG: modifying nc IO to suit new results structure

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

Legend:

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

    r25484 r25819  
    7575        if verbose > 1:
    7676            print('===Now treating {}==='.format(group))
     77        if group == 'qmu':
     78            print("qmu is skipped until it is more stable")
     79            continue
    7780        NCgroup = NCData.createGroup(str(group))
    7881        # In each group gather the fields of the class
     
    8891                    #returns False for exotic types (typicaly results)
    8992                    StdList = type(md.__dict__[group].__dict__[field][0]) in typelist
    90                 NCgroup.__setattr__('classtype', md.__dict__[group].__class__.__name__)
     93                klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__
     94                NCgroup.__setattr__('classtype', klass)
    9195                if StdList:  # this is a standard or empty list just proceed
    9296                    if verbose > 4:
    93                         print("=££=creating var for {}.{}".format(group, field))
     97                        print("=££=creating var for {}.{} with classtype : {}".format(group, field, klass))
    9498                    Var = md.__dict__[group].__dict__[field]
    9599                    Var = SqueezeVar(Var)
     
    99103                else:  # this is a list of fields, specific treatment needed (usually results or outputdefinitions)
    100104                    if verbose > 4:
    101                         print("list of fields happens for {}.{}".format(group, field))
     105                        print("list of fields happens for {}.{} with classtype : {}".format(group, field, klass))
    102106                    Listsize = len(md.__dict__[group].__dict__[field])
    103107                    if group == 'results':  #for results we reshape the datas following time rather than subgrouping
     
    105109                        try:
    106110                            #take the class of the first element to define nc class and get the list of variables
    107                             Subgroup.__setattr__('classtype', md.__dict__[group].__dict__[field][0].__class__.__name__)
     111                            klass = type(md.__dict__[group].__dict__[field][0]).__module__ + '.' + type(md.__dict__[group].__dict__[field][0]).__name__
     112                            Subgroup.__setattr__('classtype', klass)
    108113                            subfields = dict.keys(md.__dict__[group].__dict__[field][0].__dict__)
    109114                        except (IndexError, AttributeError):
    110                             Subgroup.__setattr__('classtype', md.__dict__[group].__dict__[field].__class__.__name__)
     115                            klass = type(md.__dict__[group].__dict__[field]).__module__ + '.' + type(md.__dict__[group].__dict__[field]).__name__
     116                            Subgroup.__setattr__('classtype', klass)
    111117                            subfields = dict.keys(md.__dict__[group].__dict__[field].__getitem__(0))
    112118                        for subfield in subfields:
     
    137143                            Subgroupname = str(md.__dict__[group].__dict__[field][listindex].definitionstring)
    138144                            Subgroup = NCgroup.createGroup(Subgroupname)
    139                             Subgroup.__setattr__('classtype', md.__dict__[group].__dict__[field][listindex].__class__.__name__)
     145                            klass = type(md.__dict__[group].__dict__[field][listindex]).__module__ + '.' + type(md.__dict__[group].__dict__[field][listindex]).__name__
     146                            Subgroup.__setattr__('classtype', klass)
    140147                            subfields = dict.keys(md.__dict__[group].__dict__[field][listindex].__dict__)
    141148                            for subfield in subfields:
     
    150157                    else:
    151158                        print("WARNING: unknown treatment for md.{}".format(group))
    152 
    153159            # No subgroup, we directly treat the variable
    154160            elif type(md.__dict__[group].__dict__[field]) in typelist or field == 'bamg':
    155                 NCgroup.__setattr__('classtype', md.__dict__[group].__class__.__name__)
     161                klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__
     162                NCgroup.__setattr__('classtype', klass)
    156163                Var = md.__dict__[group].__dict__[field]
    157164                Var = SqueezeVar(Var)
     
    161168                if ncvar is not None:
    162169                    FillVar(ncvar, Var)
    163             # empty fiel, do nothing
     170            # empty field, do nothing
    164171            elif md.__dict__[group].__dict__[field] is None:
    165172                print('field md.{}.{} is None'.format(group, field))
    166173            # if it is a masked array
    167174            elif type(md.__dict__[group].__dict__[field]) is np.ma.core.MaskedArray:
    168                 NCgroup.__setattr__('classtype', md.__dict__[group].__class__.__name__)
     175                klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__
     176                NCgroup.__setattr__('classtype', klass)
    169177                Var = md.__dict__[group].__dict__[field].data
    170178                Var = SqueezeVar(Var)
     
    176184            # this is an issm class
    177185            elif isclass(type(md.__dict__[group].__dict__[field])):
    178                 classtype = getmodule(type(md.__dict__[group])).__name__
    179                 NCgroup.__setattr__('classtype', classtype)
    180                 Subgroup = NCgroup.createGroup(str(field))
    181                 classname = type(md.__dict__[group].__dict__[field]).__name__
    182                 modulename = getmodule(type(md.__dict__[group].__dict__[field])).__name__
    183                 if classname == modulename:
    184                     #when module and class name have the same name
    185                     classtype = classname
     186                if type(md.__dict__[group].__dict__[field]).__name__ == 'solution':
     187                    #for results we reshape the datas following time rather than subgrouping
     188                    Listsize = len(md.__dict__[group].__dict__[field])
     189                    Subgroup = NCgroup.createGroup(str(field))
     190                    try:
     191                        #take the class of the first element to define nc class and get the list of variables
     192                        klass = type(md.__dict__[group].__dict__[field][0]).__module__ + '.' + type(md.__dict__[group].__dict__[field][0]).__name__
     193                        Subgroup.__setattr__('classtype', klass)
     194                        subfields = dict.keys(md.__dict__[group].__dict__[field][0].__dict__)
     195                    except (IndexError, AttributeError):
     196                        klass = type(md.__dict__[group].__dict__[field]).__module__ + '.' + type(md.__dict__[group].__dict__[field]).__name__
     197                        Subgroup.__setattr__('classtype', klass)
     198                        subfields = dict.keys(md.__dict__[group].__dict__[field].__getitem__(0))
     199                    for subfield in subfields:
     200                        if subfield not in['errlog', 'outlog']:
     201                            StackedVar = ResTable()
     202                            for listindex in range(0, Listsize):
     203                                try:
     204                                    Var = md.__dict__[group].__dict__[field].__getitem__(listindex).__dict__[subfield]
     205                                    lastindex = listindex + 1
     206                                except AttributeError:
     207                                    Var = md.__dict__[group].__dict__[field].__getitem__(listindex)[subfield]
     208                                except KeyError:
     209                                    #Some fields only exist for the first step
     210                                    lastindex = listindex
     211                                    continue
     212                                Var = SqueezeVar(Var)
     213                                StackedVar.update(Var)
     214                            if verbose > 4:
     215                                print("=$$=creating var for {}.{}.{}".format(group, field, subfield))
     216                                print("last index of the list is {}".format(lastindex))
     217                            StackedVar = SqueezeVar(StackedVar.finalize(int(lastindex)))
     218                            DimDict, ncvar = CreateVar(NCData, StackedVar, subfield, Subgroup, DimDict)
     219                            #and fill it up
     220                            if ncvar is not None:
     221                                FillVar(ncvar, StackedVar)
    186222                else:
    187                     classtype = modulename + '.' + classname
    188                 Subgroup.__setattr__('classtype', classtype)
    189                 subfields = dict.keys(md.__dict__[group].__dict__[field].__dict__)
    190                 for subfield in subfields:
    191                     if str(subfield) not in ['errlog', 'outlog']:
    192                         Var = md.__dict__[group].__dict__[field].__dict__[subfield]
    193                         Var = SqueezeVar(Var)
    194                         if verbose > 4:
    195                             print("+==+creating var for {}.{}.{}".format(group, field, subfield))
    196                         DimDict, ncvar = CreateVar(NCData, Var, subfield, Subgroup, DimDict)
    197                         if ncvar is not None:
    198                             FillVar(ncvar, Var)
     223                    klass = type(md.__dict__[group]).__module__ + '.' + type(md.__dict__[group]).__name__
     224                    NCgroup.__setattr__('classtype', klass)
     225                    Subgroup = NCgroup.createGroup(str(field))
     226                    klass = type(md.__dict__[group].__dict__[field]).__module__ + '.' + type(md.__dict__[group].__dict__[field]).__name__
     227                    Subgroup.__setattr__('classtype', klass)
     228                    subfields = dict.keys(md.__dict__[group].__dict__[field].__dict__)
     229                    for subfield in subfields:
     230                        if str(subfield) not in ['errlog', 'outlog']:
     231                            Var = md.__dict__[group].__dict__[field].__dict__[subfield]
     232                            Var = SqueezeVar(Var)
     233                            if verbose > 4:
     234                                print("+==+creating var for {}.{}.{}".format(group, field, subfield))
     235                            DimDict, ncvar = CreateVar(NCData, Var, subfield, Subgroup, DimDict)
     236                            if ncvar is not None:
     237                                FillVar(ncvar, Var)
    199238            else:
    200239                print("WARNING, md.{}.{} is not treated as it does not fall in one of the existing cases.".format(str(group), str(field)))
  • issm/trunk-jpl/src/m/io/loadvars.py

    r25591 r25819  
    9393                curclass = NCFile.groups[classtree[mod][0]].groups[classtree[mod][1]]
    9494                if debug:
    95                     print("===> {} is of class {}".format(mod, classtype[mod][0]))
    96                 if classtype[mod][0] == 'results' and 'Time' in NCFile.dimensions:  #Treating results {{{
     95                    print("===> {} is of class {}".format(mod, classtype[mod]))
     96                if classtype[mod][0] == 'results.solutionstep':  #Treating results {{{
    9797                    keylist = [key for key in curclass.groups]
    9898                    # this is related to the old structure of NC files where every steps of results had its own group
    99                     if len(keylist) > 0:
    100                         #this is kept for compatibility
    101                         #and treatment of list of dicts??
    102                         try:
    103                             #group are named after their step
    104                             steplist = [int(key) for key in curclass.groups]
    105                         except ValueError:
    106                             #or a number is appended at the end of the name
    107                             steplist = [int(findall(r'\d + ', key)[0]) for key in keylist]
    108                         indexlist = [int(index * (len(curclass.groups) - 1) / max(1, max(steplist))) for index in steplist]
    109                         listtype = curclass.groups[keylist[0]].classtype
    110                         #discriminate between dict and results
    111                         if listtype == 'dict':
    112                             nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [OrderedDict() for i in range(max(1, len(curclass.groups)))]
    113                         else:
    114                             if onlylast:   #we load only the last result to save on time and memory
    115                                 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)()]
    116                                 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
    117                             else:
    118                                 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)() for i in range(max(1, len(curclass.groups)))]
    119                                 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:]
     99                    #that is the current treatment
     100                    #here we have a more NC approach with time being a dimension
     101                    listtype = split(r'\.', classtype[mod][0])[0]
     102                    print("listtype is {}".format(listtype))
     103                    if len(NCFile.dimensions['Time']) == 1:
     104                        nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], listtype)()
     105                        Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
    120106                    else:
    121                         #that is the current treatment
    122                         #here we have a more NC approach with time being a dimension
    123                         listtype = curclass.classtype
    124                         if len(NCFile.dimensions['Time']) == 1:
    125                             nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(__import__(listtype), listtype)()
     107                        #Time dimension is in all the variables so we take that as stepnumber for the results
     108                        if onlylast:   #we load only the last result to save on time and memory
     109                            nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(classtype[mod][1], listtype)()]
    126110                            Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
    127111                        else:
    128                             #Time dimension is in all the variables so we take that as stepnumber for the results
    129                             if onlylast:   #we load only the last result to save on time and memory
    130                                 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)()]
    131                                 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
    132                             else:
    133                                 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)() for i in range(max(1, len(NCFile.dimensions['Time'])))]
    134                                 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:]
    135                 #}}}
    136                 elif classtype[mod][0] == 'list':  #Treating the old style of results {{{
    137                     keylist = [key for key in curclass.groups]
    138                     # this is related to the old structure of NC files where every steps of results had its own group
    139                     if len(keylist) > 0:
    140                         #this is kept for compatibility
    141                         #and treatment of list of dicts??
    142                         try:
    143                             #group are named after their step
    144                             steplist = [int(key) for key in curclass.groups]
    145                         except ValueError:
    146                             #or a number is appended at the end of the name
    147                             steplist = [int(findall(r'\d + ', key)[0]) for key in keylist]
    148                         indexlist = [int(index * (len(curclass.groups) - 1) / max(1, max(steplist))) for index in steplist]
    149                         listtype = curclass.groups[keylist[0]].classtype
    150                         #discriminate between dict and results
    151                         if listtype == 'dict':
    152                             nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [OrderedDict() for i in range(max(1, len(curclass.groups)))]
    153                         else:
    154                             if onlylast:   #we load only the last result to save on time and memory
    155                                 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)()]
    156                                 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
    157                             else:
    158                                 nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(__import__(listtype), listtype)() for i in range(max(1, len(curclass.groups)))]
    159                                 Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:]
    160                     else:
    161                         print("WARNING: we don't get the expected format, there might be an issue somewhere")
     112                            #nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = [getattr(classtype[mod][1], listtype)() for i in range(max(1, len(NCFile.dimensions['Time'])))]
     113                            #Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:]
     114                            setattr(nvdict['md'].__dict__[classtree[mod][0]], classtree[mod][1], getattr(classtype[mod][1], 'solution')([]))
     115                            for i in range(max(1, len(NCFile.dimensions['Time']))):
     116                                nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]].steps.append(getattr(classtype[mod][1], 'solutionstep')())
     117                            Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]][:]
     118                            print(nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]].steps[0])
    162119                # }}}
    163120                elif classtype[mod][0] == 'massfluxatgate':  #this is for output definitions {{{
     
    170127                    if debug:
    171128                        print("Using the default for md.{}.{}, is that right??".format(classtree[mod][0], classtree[mod][1]))
    172                     nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], classtype[mod][0])()
     129                    try:
     130                        modulename = split(r'\.', classtype[mod][0])[0]
     131                        if debug:
     132                            print("trying to import {} from {}".format(classtype[mod][0], modulename))
     133                        nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], modulename)()
     134                    except AttributeError:
     135                        print("WARNING: md.{}.{} is not initialized, hopefully that was done in the main group:".format(classtree[mod][0], classtree[mod][1]))
    173136                    Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
    174137            else:
    175138                curclass = NCFile.groups[classtree[mod][0]]
    176                 nvdict['md'].__dict__[mod] = getattr(classtype[mod][1], classtype[mod][0])()
     139                modulename = split(r'\.', classtype[mod][0])[0]
     140                nvdict['md'].__dict__[mod] = getattr(classtype[mod][1], modulename)()
    177141                Tree = nvdict['md'].__dict__[classtree[mod][0]]
    178142            if debug:
     
    351315            for subgroup in NCData.groups[group].groups:
    352316                classe = str(group) + '.' + str(subgroup)
    353                 submodule = str(getattr(NCData.groups[group].groups[subgroup], 'classtype')).split('.')[-1]
    354                 class_dict[classe] = [submodule, ]
     317                grpclass = str(getattr(NCData.groups[group].groups[subgroup], 'classtype'))
     318                class_dict[classe] = [grpclass, ]
    355319                if class_dict[classe][0] not in ['dict', 'list', 'cell']:
    356320                    try:
    357                         class_dict[classe].append(__import__(class_dict[classe][0]))
     321                        modulename = split(r'\.', class_dict[classe][0])[0]
     322                        #class_dict[classe].append(__import__(class_dict[classe][0]))
     323                        class_dict[classe].append(__import__(modulename))
    358324                    except ModuleNotFoundError:
    359325                        #submodule probably has a different name
    360                         modulename = str(getattr(NCData.groups[group].groups[subgroup], 'classtype')).split('.')[0]
     326                        modulename = str(getattr(NCData.groups[group].groups[subgroup], 'classtype'))
     327                        print("WARNING importing {} rather than {}".format(modulename, class_dict[classe][0]))
    361328                        class_dict[classe].append(__import__(modulename))
    362329                class_tree[classe] = [group, subgroup]
     
    366333                class_dict[classe] = [str(getattr(NCData.groups[group], 'classtype')), ]
    367334                if class_dict[classe][0] not in ['dict', 'list', 'cell']:
    368                     class_dict[classe].append(__import__(class_dict[classe][0]))
     335                    modulename = split(r'\.', class_dict[classe][0])[0]
     336                    class_dict[classe].append(__import__(modulename))
    369337                    class_tree[classe] = [group, ]
    370338            except AttributeError:
Note: See TracChangeset for help on using the changeset viewer.