1 | function theResult = resize(self, newSize) |
---|
2 | |
---|
3 | % ncdim/resize -- Resize dimension. |
---|
4 | % resize(self, newSize) resizes the length of self, |
---|
5 | % an "ncdim" object. The newSize is a positive |
---|
6 | % integer. The new self is returned. |
---|
7 | |
---|
8 | % Copyright (C) 1998 Dr. Charles R. Denham, ZYDECO. |
---|
9 | % All Rights Reserved. |
---|
10 | % Disclosure without explicit written consent from the |
---|
11 | % copyright owner does not constitute publication. |
---|
12 | |
---|
13 | % Version of 03-Nov-1998 08:52:22. |
---|
14 | % Updated 03-Mar-2003 16:15:48. |
---|
15 | |
---|
16 | if nargin < 1, help(mfilename), return, end |
---|
17 | if nargout > 0, theResult = self; end |
---|
18 | |
---|
19 | % The following is almost identical to "ncvar/resize". |
---|
20 | |
---|
21 | % Check for no-change. |
---|
22 | |
---|
23 | if isequal(ncsize(self), newSize) |
---|
24 | result = self; |
---|
25 | if nargout > 0 |
---|
26 | theResult = result; |
---|
27 | else |
---|
28 | ncans(result) |
---|
29 | end |
---|
30 | return |
---|
31 | end |
---|
32 | |
---|
33 | theItemName = name(self); |
---|
34 | |
---|
35 | % Check for writeability. |
---|
36 | |
---|
37 | f = parent(self); |
---|
38 | thePermission = permission(f); |
---|
39 | theSrcName = name(f); |
---|
40 | |
---|
41 | if isequal(thePermission, 'nowrite') |
---|
42 | disp([' ## NetCDF source file must be writeable.']) |
---|
43 | return |
---|
44 | end |
---|
45 | |
---|
46 | % Check request. |
---|
47 | |
---|
48 | if ~isrecdim(self) & newSize <= 0 |
---|
49 | disp([' ## Dimension "' name(self) '" size requires positive integer.']) |
---|
50 | return |
---|
51 | end |
---|
52 | |
---|
53 | % Create temporary file. |
---|
54 | |
---|
55 | g = []; |
---|
56 | |
---|
57 | i = 0; |
---|
58 | while isempty(g) |
---|
59 | i = i + 1; |
---|
60 | theTmpName = ['tmp_' int2str(i) '.nc']; |
---|
61 | if exist(theTmpName, 'file') ~= 2 |
---|
62 | g = netcdf(theTmpName, 'noclobber'); |
---|
63 | end |
---|
64 | end |
---|
65 | |
---|
66 | theTmpName = name(g); |
---|
67 | |
---|
68 | % Copy the affected dimension first. |
---|
69 | |
---|
70 | d = {self}; |
---|
71 | for i = 1:length(d) |
---|
72 | if isrecdim(d{i}) |
---|
73 | g(name(d{i})) = 0; |
---|
74 | else |
---|
75 | g(name(d{i})) = newSize(i); |
---|
76 | end |
---|
77 | end |
---|
78 | |
---|
79 | % Copy other dimensions. |
---|
80 | |
---|
81 | d = dim(f); |
---|
82 | for i = 1:length(d) |
---|
83 | if isrecdim(d{i}) |
---|
84 | g(name(d{i})) = 0; |
---|
85 | else |
---|
86 | g(name(d{i})) = ncsize(d{i}); |
---|
87 | end |
---|
88 | end |
---|
89 | |
---|
90 | % Copy global attributes. |
---|
91 | |
---|
92 | a = att(f); |
---|
93 | for i = 1:length(a) |
---|
94 | copy(a{i}, g) |
---|
95 | end |
---|
96 | |
---|
97 | % Copy variable definitions and attributes. |
---|
98 | |
---|
99 | v = var(f); |
---|
100 | for i = 1:length(v) |
---|
101 | copy(v{i}, g, 0, 1) |
---|
102 | end |
---|
103 | |
---|
104 | % Copy variable data as minimal rectangular array. |
---|
105 | % Note that the "()" operator is out-of-context |
---|
106 | % inside this method, so we have to build our own |
---|
107 | % calls to "ncvar/subsref" and "ncvar/subsasgn". |
---|
108 | % It might be easier for us to use "virtual" |
---|
109 | % variables instead, which could be transferred |
---|
110 | % with the more intelligent "ncvar/copy" method. |
---|
111 | |
---|
112 | v = var(f); |
---|
113 | w = var(g); |
---|
114 | |
---|
115 | for i = 1:length(v) |
---|
116 | sv = ncsize(v{i}); |
---|
117 | sw = ncsize(w{i}); |
---|
118 | if ~isempty(sw) |
---|
119 | d = dim(w{i}); |
---|
120 | if isrecdim(d{1}) |
---|
121 | if sw(1) == 0 |
---|
122 | if isequal(name(d{1}), theItemName) |
---|
123 | sw(1) = newSize; |
---|
124 | else |
---|
125 | sw(1) = sv(1); |
---|
126 | end |
---|
127 | end |
---|
128 | end |
---|
129 | end |
---|
130 | theMinimalSize = min(sv, sw); |
---|
131 | if prod(theMinimalSize) > 0 |
---|
132 | if isequal(sv, sw) |
---|
133 | copy(v{i}, g, 1) |
---|
134 | else |
---|
135 | theIndices = cell(size(theMinimalSize)); |
---|
136 | for j = 1:length(theIndices) |
---|
137 | theIndices{j} = 1:theMinimalSize(j); |
---|
138 | end |
---|
139 | theStruct.type = '()'; |
---|
140 | theStruct.subs = theIndices; |
---|
141 | theData = subsref(v{i}, theStruct); |
---|
142 | w{i} = subsasgn(w{i}, theStruct, theData); |
---|
143 | end |
---|
144 | end |
---|
145 | end |
---|
146 | |
---|
147 | % Close both files. |
---|
148 | |
---|
149 | f = close(f); |
---|
150 | g = close(g); |
---|
151 | |
---|
152 | % Delete old file. |
---|
153 | |
---|
154 | delete(theSrcName) |
---|
155 | |
---|
156 | % Rename new file to old file name. |
---|
157 | |
---|
158 | fcopy(theTmpName, theSrcName) |
---|
159 | delete(theTmpName) |
---|
160 | |
---|
161 | % Open the new file. |
---|
162 | |
---|
163 | g = netcdf(theSrcName, thePermission); |
---|
164 | |
---|
165 | % Return the resized dimension. |
---|
166 | |
---|
167 | result = g(theItemName); |
---|
168 | |
---|
169 | if nargout > 0 |
---|
170 | theResult = result; |
---|
171 | else |
---|
172 | ncans(result) |
---|
173 | end |
---|