Changeset 26566
- Timestamp:
- 11/09/21 06:56:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py
r26178 r26566 8 8 9 9 class ResTable: 10 11 10 def __init__(self): 12 11 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: 19 25 self.data.append(r) 20 26 21 27 def finalize(self, rows): 28 #we have more scalars than steps, so we have an array 22 29 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 24 56 else: 25 57 return np.asarray(self.data) … … 125 157 if subfield not in['errlog', 'outlog']: 126 158 StackedVar = ResTable() 159 #first loop over the field (result type) to find the index of the last subfield (variable) 127 160 for listindex in range(0, Listsize): 128 161 try: … … 135 168 lastindex = listindex 136 169 continue 170 #Add the subfield at the current step 137 171 Var = SqueezeVar(Var) 138 172 StackedVar.update(Var)
Note:
See TracChangeset
for help on using the changeset viewer.