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
RevLine 
[26740]1Index: ../trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py
2===================================================================
3--- ../trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py (revision 26565)
4+++ ../trunk-jpl/src/m/contrib/defleurian/netCDF/export_netCDF.py (revision 26566)
5@@ -7,20 +7,52 @@
6
7
8 class ResTable:
9-
10 def __init__(self):
11 self.data = []
12+ self.sizes = []
13
14- def update(self, row):
15- if len(np.shape(row)) == 0:
16- self.data.append(row)
17+ def update(self, stepvar):
18+ #if we have a scalar we just add it to the en
19+ #we save the size of the current step for further treatment
20+ if len(np.shape(stepvar)) == 0:
21+ self.sizes.append(1)
22+ self.data.append(stepvar)
23+ # if it is an array we add the values one by one
24+ #we save the size of the current step for further treatment
25 else:
26- for r in row:
27+ self.sizes.append(len(stepvar))
28+ for r in stepvar:
29 self.data.append(r)
30
31 def finalize(self, rows):
32+ #we have more scalars than steps, so we have an array
33 if len(self.data) > rows:
34- return np.reshape(self.data, newshape=(rows, int(len(self.data) / rows)))
35+ #first check if all steps are the same size
36+ SameSize = np.sum(np.asarray(self.sizes) - self.sizes[0]) == 0
37+ if SameSize:
38+ #same size for all steps, just reshape
39+ return np.reshape(self.data, newshape=(rows, int(len(self.data) / rows)))
40+ else:
41+ #different sizes at each steps, first create a table big enough for the biggest step
42+ startpoint = 0
43+ datadim = len(np.shape(self.data))
44+ if datadim == 1:
45+ outdat = np.nan * np.ones((rows, np.nanmax(self.sizes)))
46+ for step in range(rows):
47+ curlen = self.sizes[step]
48+ outdat[step, :curlen] = self.data[startpoint: startpoint + curlen]
49+ startpoint += curlen
50+ elif datadim == 2:
51+ outdat = np.nan * np.ones((rows, np.nanmax(self.sizes), np.shape(self.data)[1]))
52+ for step in range(rows):
53+ curlen = self.sizes[step]
54+ outdat[step, :curlen, :] = self.data[startpoint: startpoint + curlen]
55+ startpoint += curlen
56+
57+ else:
58+ print("ERROR, reult treatment cant cope with dimensions above 2")
59+ return outdat
60+ #as much scalars as stpes (or less) so just one value per step
61 else:
62 return np.asarray(self.data)
63
64@@ -124,6 +156,7 @@
65 for subfield in subfields:
66 if subfield not in['errlog', 'outlog']:
67 StackedVar = ResTable()
68+ #first loop over the field (result type) to find the index of the last subfield (variable)
69 for listindex in range(0, Listsize):
70 try:
71 Var = md.__dict__[group].__dict__[field].__getitem__(listindex).__dict__[subfield]
72@@ -134,6 +167,7 @@
73 #Some fields only exist for the first step
74 lastindex = listindex
75 continue
76+ #Add the subfield at the current step
77 Var = SqueezeVar(Var)
78 StackedVar.update(Var)
79 if verbose > 4:
Note: See TracBrowser for help on using the repository browser.