Changeset 26128


Ignore:
Timestamp:
03/23/21 05:38:37 (4 years ago)
Author:
bdef
Message:

BUG: fix to netCDF load to deal with old files

File:
1 edited

Legend:

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

    r26127 r26128  
    166166            if len(curclass.groups) > 0:  #that is presumably only for old style NC where each result step had its own group
    167167                if onlylast:
    168                     listclass = curclass.groups[keylist[len(curclass.groups) - 1]]
     168                    groupclass = [curclass.groups[keylist[len(curclass.groups) - 1]]]
    169169                else:
    170                     listclass = curclass.groups[keylist[0]]
     170                    groupclass = [curclass.groups[key] for key in keylist]
    171171            else:
    172                 listclass = curclass
     172                groupclass = [curclass]
    173173            #==== We deal with Variables {{{
    174             for var in listclass.variables:
    175                 if debug:
    176                     print("    ==> treating var {}".format(var))
    177                 if var not in ['errlog', 'outlog']:
    178                     varval = listclass.variables[str(var)]
    179                     vardim = varval.ndim
    180                     #There is a special treatment for results to account for its specific structure
    181                     #that is the new export version where time is a named dimension
    182                     NewFormat = 'Time' in NCFile.dimensions
    183                     if type(Tree) == list:  # and NewFormat:
    184                         if onlylast:
    185                             if NewFormat:
    186                                 if vardim == 1:
    187                                     Tree[0].__dict__[str(var)] = varval[-1].data
    188                                 elif vardim == 2:
    189                                     Tree[0].__dict__[str(var)] = varval[-1, :].data
    190                                 elif vardim == 3:
    191                                     Tree[0].__dict__[str(var)] = varval[-1, :, :].data
     174            for groupindex, listclass in enumerate(groupclass):
     175                for var in listclass.variables:
     176                    if debug:
     177                        print("    ==> treating var {}".format(var))
     178                    if var not in ['errlog', 'outlog']:
     179                        varval = listclass.variables[str(var)]
     180                        vardim = varval.ndim
     181                        #There is a special treatment for results to account for its specific structure
     182                        #that is the new export version where time is a named dimension
     183                        NewFormat = 'Time' in NCFile.dimensions
     184                        if type(Tree) == list:  # and NewFormat:
     185                            if onlylast:
     186                                if NewFormat:
     187                                    if vardim == 1:
     188                                        Tree[0].__dict__[str(var)] = varval[-1].data
     189                                    elif vardim == 2:
     190                                        Tree[0].__dict__[str(var)] = varval[-1, :].data
     191                                    elif vardim == 3:
     192                                        Tree[0].__dict__[str(var)] = varval[-1, :, :].data
     193                                    else:
     194                                        print('table dimension greater than 3 not implemented yet')
     195                                else:  #old format had step sorted in difeerent group so last group is last time
     196                                    Tree[0].__dict__[str(var)] = varval[:].data
     197                            else:
     198                                if NewFormat:
     199                                    incomplete = 'Time' not in varval.dimensions and NewFormat
     200                                    if incomplete:
     201                                        chosendim = varval.dimensions[0]
     202                                        timelist = np.arange(0, len(NCFile.dimensions[chosendim]))
     203                                        print('WARNING, {} is not present on every times, we chose {}({}) as the dimension to write it with'.format(var, chosendim, len(NCFile.dimensions[chosendim])))
     204                                    else:
     205                                        timelist = np.arange(0, len(NCFile.dimensions['Time']))
     206                                    for t in timelist:
     207                                        if debug:
     208                                            print("filing step {} for {}".format(t, var))
     209                                        if vardim == 0:
     210                                            Tree[t].__dict__[str(var)] = varval[:].data
     211                                        elif vardim == 1:
     212                                            Tree[t].__dict__[str(var)] = varval[t].data
     213                                        elif vardim == 2:
     214                                            Tree[t].__dict__[str(var)] = varval[t, :].data
     215                                        elif vardim == 3:
     216                                            Tree[t].__dict__[str(var)] = varval[t, :, :].data
     217                                        else:
     218                                            print('table dimension greater than 3 not implemented yet')
    192219                                else:
    193                                     print('table dimension greater than 3 not implemented yet')
    194                             else:  #old format had step sorted in difeerent group so last group is last time
    195                                 Tree[0].__dict__[str(var)] = varval[:].data
     220                                    if debug:
     221                                        print("filing step {} for {}".format(groupindex, var))
     222                                    Tree[groupindex].__dict__[str(var)] = varval[:].data
    196223                        else:
    197                             incomplete = 'Time' not in varval.dimensions and NewFormat
    198                             if incomplete:
    199                                 chosendim = varval.dimensions[0]
    200                                 timelist = np.arange(0, len(NCFile.dimensions[chosendim]))
    201                                 print('WARNING, {} is not present on every times, we chose {}({}) as the dimension to write it with'.format(var, chosendim, len(NCFile.dimensions[chosendim])))
    202                             elif not NewFormat:
    203                                 timelist = len(curclass.groups)
     224                            if vardim == 0:  #that is a scalar
     225                                if str(varval[0]) == '':  #no value
     226                                    Tree.__dict__[str(var)] = []
     227                                elif varval[0] == 'True':  #treatin bool
     228                                    Tree.__dict__[str(var)] = True
     229                                elif varval[0] == 'False':  #treatin bool
     230                                    Tree.__dict__[str(var)] = False
     231                                else:
     232                                    Tree.__dict__[str(var)] = varval[0].item()
     233
     234                            elif vardim == 1:  #that is a vector
     235                                if varval.dtype == str:
     236                                    if varval.shape[0] == 1:
     237                                        Tree.__dict__[str(var)] = [str(varval[0]), ]
     238                                    elif 'True' in varval[:] or 'False' in varval[:]:
     239                                        Tree.__dict__[str(var)] = np.asarray([V == 'True' for V in varval[:]], dtype=bool)
     240                                    else:
     241                                        Tree.__dict__[str(var)] = [str(vallue) for vallue in varval[:]]
     242                                else:
     243                                    try:
     244                                        #some thing specifically require a list
     245                                        mdtype = type(Tree.__dict__[str(var)])
     246                                    except KeyError:
     247                                        mdtype = float
     248                                    if mdtype == list:
     249                                        Tree.__dict__[str(var)] = [mdval for mdval in varval[:]]
     250                                    else:
     251                                        Tree.__dict__[str(var)] = varval[:].data
     252
     253                            elif vardim == 2:
     254                                #dealling with dict
     255                                if varval.dtype == str:  #that is for dictionaries
     256                                    if any(varval[:, 0] == 'toolkit'):  #toolkit definition have to be first
     257                                        Tree.__dict__[str(var)] = OrderedDict([('toolkit', str(varval[np.where(varval[:, 0] == 'toolkit')[0][0], 1]))])
     258                                        strings1 = [str(arg[0]) for arg in varval if arg[0] != 'toolkits']
     259                                        strings2 = [str(arg[1]) for arg in varval if arg[0] != 'toolkits']
     260                                        Tree.__dict__[str(var)].update(list(zip(strings1, strings2)))
     261                                    else:
     262                                        strings1 = [str(arg[0]) for arg in varval]
     263                                        strings2 = [str(arg[1]) for arg in varval]
     264                                        Tree.__dict__[str(var)] = OrderedDict(list(zip(strings1, strings2)))
     265                            elif vardim == 3:
     266                                Tree.__dict__[str(var)] = varval[:, :, :].data
    204267                            else:
    205                                 timelist = np.arange(0, len(NCFile.dimensions['Time']))
    206                             for t in timelist:
    207                                 print("filing step {} for {}".format(t, var))
    208                                 if vardim == 0:
    209                                     Tree[t].__dict__[str(var)] = varval[:].data
    210                                 if vardim == 1:
    211                                     Tree[t].__dict__[str(var)] = varval[t].data
    212                                 elif vardim == 2:
    213                                     Tree[t].__dict__[str(var)] = varval[t, :].data
    214                                 elif vardim == 3:
    215                                     Tree[t].__dict__[str(var)] = varval[t, :, :].data
    216                                 else:
    217                                     print('table dimension greater than 3 not implemented yet')
    218                     else:
    219                         if vardim == 0:  #that is a scalar
    220                             if str(varval[0]) == '':  #no value
    221                                 Tree.__dict__[str(var)] = []
    222                             elif varval[0] == 'True':  #treatin bool
    223                                 Tree.__dict__[str(var)] = True
    224                             elif varval[0] == 'False':  #treatin bool
    225                                 Tree.__dict__[str(var)] = False
     268                                print('table dimension greater than 3 not implemented yet')
     269                # }}}
     270                #==== And with atribute {{{
     271                for attr in listclass.ncattrs():
     272                    if debug:
     273                        print("      ==> treating attribute {}".format(attr))
     274                    if attr != 'classtype':  #classtype is for treatment, don't get it back
     275                        attribute = str(attr).swapcase()  #there is a reason for swapcase, no sure what it isanymore
     276                        if attr == 'VARNAME':
     277                            attribute = 'name'
     278                        if type(Tree) == list:
     279                            if debug:
     280                                print("        printing with index 0")
     281                            if listtype == 'dict':
     282                                Tree[0][attribute] = str(listclass.getncattr(attr))
    226283                            else:
    227                                 Tree.__dict__[str(var)] = varval[0].item()
    228 
    229                         elif vardim == 1:  #that is a vector
    230                             if varval.dtype == str:
    231                                 if varval.shape[0] == 1:
    232                                     Tree.__dict__[str(var)] = [str(varval[0]), ]
    233                                 elif 'True' in varval[:] or 'False' in varval[:]:
    234                                     Tree.__dict__[str(var)] = np.asarray([V == 'True' for V in varval[:]], dtype=bool)
    235                                 else:
    236                                     Tree.__dict__[str(var)] = [str(vallue) for vallue in varval[:]]
    237                             else:
    238                                 try:
    239                                     #some thing specifically require a list
    240                                     mdtype = type(Tree.__dict__[str(var)])
    241                                 except KeyError:
    242                                     mdtype = float
    243                                 if mdtype == list:
    244                                     Tree.__dict__[str(var)] = [mdval for mdval in varval[:]]
    245                                 else:
    246                                     Tree.__dict__[str(var)] = varval[:].data
    247 
    248                         elif vardim == 2:
    249                             #dealling with dict
    250                             if varval.dtype == str:  #that is for dictionaries
    251                                 if any(varval[:, 0] == 'toolkit'):  #toolkit definition have to be first
    252                                     Tree.__dict__[str(var)] = OrderedDict([('toolkit', str(varval[np.where(varval[:, 0] == 'toolkit')[0][0], 1]))])
    253                                     strings1 = [str(arg[0]) for arg in varval if arg[0] != 'toolkits']
    254                                     strings2 = [str(arg[1]) for arg in varval if arg[0] != 'toolkits']
    255                                     Tree.__dict__[str(var)].update(list(zip(strings1, strings2)))
    256                                 else:
    257                                     strings1 = [str(arg[0]) for arg in varval]
    258                                     strings2 = [str(arg[1]) for arg in varval]
    259                                     Tree.__dict__[str(var)] = OrderedDict(list(zip(strings1, strings2)))
    260                         elif vardim == 3:
    261                             Tree.__dict__[str(var)] = varval[:, :, :].data
     284                                Tree[0].__dict__[attribute] = str(listclass.getncattr(attr))
    262285                        else:
    263                             print('table dimension greater than 3 not implemented yet')
    264                 # }}}
    265             #==== And with atribute {{{
    266             for attr in listclass.ncattrs():
    267                 if debug:
    268                     print("      ==> treating attribute {}".format(attr))
    269                 if attr != 'classtype':  #classtype is for treatment, don't get it back
    270                     attribute = str(attr).swapcase()  #there is a reason for swapcase, no sure what it isanymore
    271                     if attr == 'VARNAME':
    272                         attribute = 'name'
    273                     if type(Tree) == list:
    274                         if debug:
    275                             print("        printing with index 0")
    276                         if listtype == 'dict':
    277                             Tree[0][attribute] = str(listclass.getncattr(attr))
    278                         else:
    279                             Tree[0].__dict__[attribute] = str(listclass.getncattr(attr))
    280                     else:
    281                         Tree.__dict__[attribute] = str(listclass.getncattr(attr))
    282                         if listclass.getncattr(attr) == 'True':
    283                             Tree.__dict__[attribute] = True
    284                         elif listclass.getncattr(attr) == 'False':
    285                             Tree.__dict__[attribute] = False
     286                            Tree.__dict__[attribute] = str(listclass.getncattr(attr))
     287                            if listclass.getncattr(attr) == 'True':
     288                                Tree.__dict__[attribute] = True
     289                            elif listclass.getncattr(attr) == 'False':
     290                                Tree.__dict__[attribute] = False
    286291                # }}}
    287292            # }}}
Note: See TracChangeset for help on using the changeset viewer.