Ignore:
Timestamp:
09/05/22 02:05:50 (3 years ago)
Author:
bdef
Message:

CHG:Some netCDF IO modifications

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/io/loadvars.py

    r26968 r27264  
    5555
    5656    timeindex = False
     57    SteadySols = ['ThermalSolution', 'HydrologySolution', 'StressbalanceSolution']
    5758
    5859    for key, value in kwargs.items():
     
    105106                    #here we have a more NC approach with time being a dimension
    106107                    listtype = split(r'\.', classtype[mod][0])[1]
    107                     print(listtype)
    108                     if len(NCFile.dimensions['Time']) == 1:
     108                    try:
     109                        soltype = str(getattr(curclass, 'SolutionType'))
     110                    except AttributeError:
     111                        #might be an older format try that instead :
     112                        soltype = str(getattr(curclass, 'sOLUTIONtYPE'))
     113                    if len(NCFile.dimensions['Time']) == 1 or soltype in SteadySols:
    109114                        nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], listtype)()
    110115                        Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
     
    148153                    Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[defname][defindex - 1]
    149154                #}}}
     155                elif classtype[mod][0] == 'collections.OrderedDict':  #Treating multiple toolkits {{{
     156                    nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]] = getattr(classtype[mod][1], 'OrderedDict')
     157                    Tree = nvdict['md'].__dict__[classtree[mod][0]].__dict__[classtree[mod][1]]
    150158                else:
    151159                    if verbose > 0:
     
    184192            else:
    185193                groupclass = [curclass]
    186             #==== We deal with Variables {{{
    187194            for groupindex, listclass in enumerate(groupclass):
     195                try:
     196                    soltype = str(getattr(listclass, 'SolutionType'))
     197                except AttributeError:
     198                    soltype = 'NoSol'
     199                #==== We deal with Variables {{{
    188200                for var in listclass.variables:
    189201                    if not resname or var == resname:
     
    217229                                        else:
    218230                                            print('table dimension greater than 3 not implemented yet')
     231                                    elif soltype in SteadySols:
     232                                        Tree.__dict__[str(var)] = varval[:].data
    219233                                    else:  #old format had step sorted in difeerent group so last group is last time
    220234                                        Tree[0].__dict__[str(var)] = varval[:].data
    221235                                else:
    222236                                    if NewFormat:
    223                                         incomplete = 'Time' not in varval.dimensions
     237                                        incomplete = 'Time' not in varval.dimensions and soltype not in SteadySols
    224238                                        if incomplete:
    225239                                            try:
     
    230244                                                #just one step, so no dimension, we just put it on the first solutionstep
    231245                                                timelist = [0]
     246                                        elif soltype in SteadySols:
     247                                            timelist = [0]
    232248                                        else:
    233249                                            timelist = np.arange(0, len(NCFile.dimensions['Time']))
    234                                         for t in timelist:
    235                                             if verbose > 5:
    236                                                 print("filing step {} for {}".format(t, var))
    237                                             if vardim == 0:
    238                                                 Tree[t].__dict__[str(var)] = varval[:].data
    239                                             elif vardim == 1:
    240                                                 stepval = ma.masked_array(varval[t].data, mask=np.where(np.isnan(varval[t]), 1, 0))
    241                                                 Tree[t].__dict__[str(var)] = ma.compressed(stepval)
    242                                             elif vardim == 2:
    243                                                 stepval = ma.masked_array(varval[t, :].data, mask=np.where(np.isnan(varval[t, :]), 1, 0))
    244                                                 Tree[t].__dict__[str(var)] = ma.compressed(stepval)
    245                                             elif vardim == 3:
    246                                                 stepval = ma.masked_array(varval[t, :, :].data, mask=np.where(np.isnan(varval[t, :, :]), 1, 0))
    247                                                 Tree[t].__dict__[str(var)] = ma.compressed(stepval).reshape((stepval.count(0)[0], stepval.count(1)[0]))
    248                                             else:
    249                                                 print('table dimension greater than 3 not implemented yet')
     250                                        if soltype in SteadySols:
     251                                            Tree.__dict__[str(var)] = varval[:].data
     252                                        else:
     253                                            for t in timelist:
     254                                                if verbose > 5:
     255                                                    print("filing step {} for {}".format(t, var))
     256                                                if vardim == 0:
     257                                                    Tree[t].__dict__[str(var)] = varval[:].data
     258                                                elif vardim == 1:
     259                                                    stepval = ma.masked_array(varval[t].data, mask=np.where(np.isnan(varval[t]), 1, 0))
     260                                                    Tree[t].__dict__[str(var)] = ma.compressed(stepval)
     261                                                elif vardim == 2:
     262                                                    stepval = ma.masked_array(varval[t, :].data, mask=np.where(np.isnan(varval[t, :]), 1, 0))
     263                                                    Tree[t].__dict__[str(var)] = ma.compressed(stepval)
     264                                                elif vardim == 3:
     265                                                    stepval = ma.masked_array(varval[t, :, :].data, mask=np.where(np.isnan(varval[t, :, :]), 1, 0))
     266                                                    Tree[t].__dict__[str(var)] = ma.compressed(stepval).reshape((stepval.count(0)[0], stepval.count(1)[0]))
     267                                                else:
     268                                                    print('table dimension greater than 3 not implemented yet')
    250269                                    else:
    251270                                        if verbose > 0:
     
    315334                                            Tree.__dict__[str(var)] = varval[:, :].data
    316335                                elif vardim == 3:
    317                                     Tree.__dict__[str(var)] = varval[:, :, :].data
     336                                    if varval.dtype == "|S1":  #that is for matlab chararcter arrays
     337                                        #most likely that is a toolkit dictionar so should be treated as such
     338                                        #first we convert the character table to strings
     339                                        stringtable = []
     340                                        for i in range(np.shape(varval)[0]):
     341                                            stringtable.append([chartostring(varval[i, 0, :]), chartostring(varval[i, 1, :])])
     342                                        stringtable = np.asarray(stringtable, dtype=str)
     343                                        Tree.__dict__[str(var)] = OrderedDict([('toolkit', str(varval[np.where(stringtable[:, 0] == 'toolkit')[0][0], 1]))])
     344                                        strings1 = [str(arg[0]) for arg in stringtable if arg[0] != 'toolkits']
     345                                        strings2 = [str(arg[1]) for arg in stringtable if arg[0] != 'toolkits']
     346                                        Tree.__dict__[str(var)].update(list(zip(strings1, strings2)))
     347                                    else:
     348                                        Tree.__dict__[str(var)] = varval[:, :, :].data
    318349                                else:
    319350                                    print('table dimension greater than 3 not implemented yet')
     
    387418                if class_dict[classe][0] not in ['dict', 'list', 'cell']:
    388419                    modulename = split(r'\.', class_dict[classe][0])[0]
    389                     if modulename == "giaivins":
    390                         print("WARNING: module {} does not exist anymore and is skipped".format(modulename))
    391                     else:
     420                    try:
    392421                        class_dict[classe].append(import_module(modulename))
    393422                        class_tree[classe] = [group, ]
     423                    except ModuleNotFoundError:
     424                        print("WARNING: module {} does not exist anymore and is skipped".format(modulename))
     425
    394426            except AttributeError:
    395427                print(('group {} is empty'.format(group)))
Note: See TracChangeset for help on using the changeset viewer.