Index: ../trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py =================================================================== --- ../trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py (revision 26565) +++ ../trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py (revision 26566) @@ -7,20 +7,52 @@ class ResTable: - def __init__(self): self.data = [] + self.sizes = [] - def update(self, row): - if len(np.shape(row)) == 0: - self.data.append(row) + def update(self, stepvar): + #if we have a scalar we just add it to the en + #we save the size of the current step for further treatment + if len(np.shape(stepvar)) == 0: + self.sizes.append(1) + self.data.append(stepvar) + # if it is an array we add the values one by one + #we save the size of the current step for further treatment else: - for r in row: + self.sizes.append(len(stepvar)) + for r in stepvar: self.data.append(r) def finalize(self, rows): + #we have more scalars than steps, so we have an array if len(self.data) > rows: - return np.reshape(self.data, newshape=(rows, int(len(self.data) / rows))) + #first check if all steps are the same size + SameSize = np.sum(np.asarray(self.sizes) - self.sizes[0]) == 0 + if SameSize: + #same size for all steps, just reshape + return np.reshape(self.data, newshape=(rows, int(len(self.data) / rows))) + else: + #different sizes at each steps, first create a table big enough for the biggest step + startpoint = 0 + datadim = len(np.shape(self.data)) + if datadim == 1: + outdat = np.nan * np.ones((rows, np.nanmax(self.sizes))) + for step in range(rows): + curlen = self.sizes[step] + outdat[step, :curlen] = self.data[startpoint: startpoint + curlen] + startpoint += curlen + elif datadim == 2: + outdat = np.nan * np.ones((rows, np.nanmax(self.sizes), np.shape(self.data)[1])) + for step in range(rows): + curlen = self.sizes[step] + outdat[step, :curlen, :] = self.data[startpoint: startpoint + curlen] + startpoint += curlen + + else: + print("ERROR, reult treatment cant cope with dimensions above 2") + return outdat + #as much scalars as stpes (or less) so just one value per step else: return np.asarray(self.data) @@ -124,6 +156,7 @@ for subfield in subfields: if subfield not in['errlog', 'outlog']: StackedVar = ResTable() + #first loop over the field (result type) to find the index of the last subfield (variable) for listindex in range(0, Listsize): try: Var = md.__dict__[group].__dict__[field].__getitem__(listindex).__dict__[subfield] @@ -134,6 +167,7 @@ #Some fields only exist for the first step lastindex = listindex continue + #Add the subfield at the current step Var = SqueezeVar(Var) StackedVar.update(Var) if verbose > 4: