Changeset 26566


Ignore:
Timestamp:
11/09/21 06:56:00 (3 years ago)
Author:
bdef
Message:

NEW: update to deal with AMR models

File:
1 edited

Legend:

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

    r26178 r26566  
    88
    99class ResTable:
    10 
    1110    def __init__(self):
    1211        self.data = []
    13 
    14     def update(self, row):
    15         if len(np.shape(row)) == 0:
    16             self.data.append(row)
    17         else:
    18             for r in row:
     12        self.sizes = []
     13
     14    def update(self, stepvar):
     15        #if we have a scalar we just add it to the en
     16        #we save the size of the current step for further treatment
     17        if len(np.shape(stepvar)) == 0:
     18            self.sizes.append(1)
     19            self.data.append(stepvar)
     20        # if it is an array we add the values one by one
     21        #we save the size of the current step for further treatment
     22        else:
     23            self.sizes.append(len(stepvar))
     24            for r in stepvar:
    1925                self.data.append(r)
    2026
    2127    def finalize(self, rows):
     28        #we have more scalars than steps, so we have an array
    2229        if len(self.data) > rows:
    23             return np.reshape(self.data, newshape=(rows, int(len(self.data) / rows)))
     30            #first check if all steps are the same size
     31            SameSize = np.sum(np.asarray(self.sizes) - self.sizes[0]) == 0
     32            if SameSize:
     33                #same size for all steps, just reshape
     34                return np.reshape(self.data, newshape=(rows, int(len(self.data) / rows)))
     35            else:
     36                #different sizes at each steps, first create a table big enough for the biggest step
     37                startpoint = 0
     38                datadim = len(np.shape(self.data))
     39                if datadim == 1:
     40                    outdat = np.nan * np.ones((rows, np.nanmax(self.sizes)))
     41                    for step in range(rows):
     42                        curlen = self.sizes[step]
     43                        outdat[step, :curlen] = self.data[startpoint: startpoint + curlen]
     44                        startpoint += curlen
     45                elif datadim == 2:
     46                    outdat = np.nan * np.ones((rows, np.nanmax(self.sizes), np.shape(self.data)[1]))
     47                    for step in range(rows):
     48                        curlen = self.sizes[step]
     49                        outdat[step, :curlen, :] = self.data[startpoint: startpoint + curlen]
     50                        startpoint += curlen
     51
     52                else:
     53                    print("ERROR, reult treatment cant cope with dimensions above 2")
     54                return outdat
     55        #as much scalars as stpes (or less) so just one value per step
    2456        else:
    2557            return np.asarray(self.data)
     
    125157                            if subfield not in['errlog', 'outlog']:
    126158                                StackedVar = ResTable()
     159                                #first loop over the field (result type) to find the index of the last subfield (variable)
    127160                                for listindex in range(0, Listsize):
    128161                                    try:
     
    135168                                        lastindex = listindex
    136169                                        continue
     170                                    #Add the  subfield at the current step
    137171                                    Var = SqueezeVar(Var)
    138172                                    StackedVar.update(Var)
Note: See TracChangeset for help on using the changeset viewer.