Changeset 25190


Ignore:
Timestamp:
07/01/20 00:39:40 (5 years ago)
Author:
bdef
Message:

CHG: optimizing nc write for longer runs

File:
1 edited

Legend:

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

    r25063 r25190  
    66
    77
     8class ResTable:
     9
     10    def __init__(self):
     11        self.data = []
     12
     13    def update(self, row):
     14        if len(np.shape(row)) == 0:
     15            self.data.append(row)
     16        else:
     17            for r in row:
     18                self.data.append(r)
     19
     20    def finalize(self, rows):
     21        print("data of len {} with {} rows".format(len(self.data) ,rows))
     22        if len(self.data) > rows:
     23            return np.reshape(self.data, newshape=(rows, int(len(self.data) / rows)))
     24        else:
     25            return np.asarray(self.data)
     26
     27
    828def export_netCDF(md, filename):  # {{{
    9     debug = False
     29    #verbosity of the code, 0 is no messages, 5 is chatty
     30    verbose = 0
    1031    if path.exists(filename):
    1132        print('File {} allready exist'.format(filename))
     
    3556    dimlist = [2, md.mesh.numberofelements, md.mesh.numberofvertices, np.shape(md.mesh.elements)[1]]
    3657    dimnames = ['DictDummy', 'EltNum', 'VertNum', 'VertPerElt']
    37     print('===Creating dimensions ===')
     58    if verbose > 0:
     59        print('===Creating dimensions ===')
    3860    for i, newdim in enumerate(dimlist):
    3961        if newdim not in list(DimDict.keys()):
     
    4870    # get all model classes and create respective groups
    4971    groups = dict.keys(md.__dict__)
    50     print('===Creating and populating groups===')
     72    if verbose > 0:
     73        print('===Creating and populating groups===')
    5174    for group in groups:
     75        if verbose > 1:
     76            print('===Now treating {}==='.format(group))
    5277        NCgroup = NCData.createGroup(str(group))
    5378        # In each group gather the fields of the class
     
    6590                NCgroup.__setattr__('classtype', md.__dict__[group].__class__.__name__)
    6691                if StdList:  # this is a standard or empty list just proceed
     92                    if verbose > 4:
     93                        print("=££=creating var for {}.{}".format(group, field))
    6794                    Var = md.__dict__[group].__dict__[field]
    6895                    Var = SqueezeVar(Var)
    69                     if debug:
    70                         print("=££=creating var for {}.{}".format(group, field))
    7196                    DimDict, ncvar = CreateVar(NCData, Var, field, NCgroup, DimDict)
    7297                    if ncvar is not None:
    7398                        FillVar(ncvar, Var)
    7499                else:  # this is a list of fields, specific treatment needed
    75                     if debug:
     100                    if verbose > 4:
    76101                        print("list of fields happens for {}.{}".format(group, field))
    77102                    Listsize = len(md.__dict__[group].__dict__[field])
     
    81106                        Subgroup.__setattr__('classtype', md.__dict__[group].__dict__[field][0].__class__.__name__)
    82107                        subfields = dict.keys(md.__dict__[group].__dict__[field][0].__dict__)
    83                     except IndexError:
     108                    except (IndexError, AttributeError):
    84109                        Subgroup.__setattr__('classtype', md.__dict__[group].__dict__[field].__class__.__name__)
    85                     except AttributeError:
    86110                        subfields = dict.keys(md.__dict__[group].__dict__[field].__getitem__(0))
    87111
    88112                    for subfield in subfields:
    89113                        if subfield not in['errlog', 'outlog']:
    90                             try:
    91                                 Var = md.__dict__[group].__dict__[field].__getitem__(0).__dict__[subfield]
    92                             except AttributeError:
    93                                 Var = md.__dict__[group].__dict__[field].__getitem__(0)[subfield]
    94                             StackedVar = SqueezeVar(Var)
    95 
    96                             for listindex in range(1, Listsize):
     114                            StackedVar = ResTable()
     115                            for listindex in range(0, Listsize):
    97116                                try:
    98117                                    Var = md.__dict__[group].__dict__[field].__getitem__(listindex).__dict__[subfield]
     118                                    lastindex = listindex + 1
    99119                                except AttributeError:
    100120                                    Var = md.__dict__[group].__dict__[field].__getitem__(listindex)[subfield]
    101121                                except KeyError:
    102122                                    #Some fields only exist for the first step
     123                                    lastindex = listindex
    103124                                    continue
    104125                                Var = SqueezeVar(Var)
    105                                 StackedVar = np.vstack((StackedVar, Var))
    106                             if debug:
     126                                StackedVar.update(Var)
     127                            if verbose > 4:
    107128                                print("=$$=creating var for {}.{}.{}".format(group, field, subfield))
    108                             StackedVar = SqueezeVar(StackedVar)
     129                                print(lastindex)
     130                            StackedVar = SqueezeVar(StackedVar.finalize(int(lastindex)))
    109131                            DimDict, ncvar = CreateVar(NCData, StackedVar, subfield, Subgroup, DimDict)
    110132                            #and fill it up
     
    117139                Var = md.__dict__[group].__dict__[field]
    118140                Var = SqueezeVar(Var)
    119                 if debug:
     141                if verbose > 4:
    120142                    print("====creating var for {}.{}".format(group, field))
    121143                DimDict, ncvar = CreateVar(NCData, Var, field, NCgroup, DimDict)
     
    130152                Var = md.__dict__[group].__dict__[field].data
    131153                Var = SqueezeVar(Var)
    132                 if debug:
     154                if verbose > 4:
    133155                    print("=++=creating var for {}.{}".format(group, field))
    134156                DimDict, ncvar = CreateVar(NCData, Var, field, NCgroup, DimDict)
     
    144166                        Var = md.__dict__[group].__dict__[field].__dict__[subfield]
    145167                        Var = SqueezeVar(Var)
    146                         if debug:
     168                        if verbose > 4:
    147169                            print("+==+creating var for {}.{}.{}".format(group, field, subfield))
    148170                        DimDict, ncvar = CreateVar(NCData, Var, subfield, Subgroup, DimDict)
     
    273295        except TypeError:  # type does not accept nan, get vallue of the variable
    274296            naned = invar
     297
    275298        if UnlimIndex:
    276299            if len(val_shape) == 0:
     
    328351    return Var
    329352# }}}
     353
     354
     355def grow(self, row):  # {{{
     356    np.append(self.data, row)
     357# }}}
Note: See TracChangeset for help on using the changeset viewer.