source: issm/oecreview/Archive/25834-26739/ISSM-26565-26566.diff

Last change on this file was 26740, checked in by Mathieu Morlighem, 3 years ago

CHG: added 25834-26739

File size: 3.7 KB
  • ../trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py

     
    77
    88
    99class ResTable:
    10 
    1110    def __init__(self):
    1211        self.data = []
     12        self.sizes = []
    1313
    14     def update(self, row):
    15         if len(np.shape(row)) == 0:
    16             self.data.append(row)
     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
    1722        else:
    18             for r in row:
     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)
    2658
     
    124156                        for subfield in subfields:
    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:
    129162                                        Var = md.__dict__[group].__dict__[field].__getitem__(listindex).__dict__[subfield]
     
    134167                                        #Some fields only exist for the first step
    135168                                        lastindex = listindex
    136169                                        continue
     170                                    #Add the  subfield at the current step
    137171                                    Var = SqueezeVar(Var)
    138172                                    StackedVar.update(Var)
    139173                                if verbose > 4:
Note: See TracBrowser for help on using the repository browser.