source: Roms_tools/mexcdf/netcdf_toolbox/netcdf/@ncdim/resize.m @ 1

Last change on this file since 1 was 1, checked in by cholod, 13 years ago

import Roms_Agrif

File size: 3.1 KB
Line 
1function 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
16if nargin < 1, help(mfilename), return, end
17if nargout > 0, theResult = self; end
18
19% The following is almost identical to "ncvar/resize".
20
21% Check for no-change.
22
23if isequal(ncsize(self), newSize)
24        result = self;
25        if nargout > 0
26                theResult = result;
27        else
28                ncans(result)
29        end
30        return
31end
32
33theItemName = name(self);
34
35% Check for writeability.
36
37f = parent(self);
38thePermission = permission(f);
39theSrcName = name(f);
40
41if isequal(thePermission, 'nowrite')
42        disp([' ## NetCDF source file must be writeable.'])
43        return
44end
45
46% Check request.
47
48if ~isrecdim(self) & newSize <= 0
49        disp([' ## Dimension "' name(self) '" size requires positive integer.'])
50        return
51end
52
53% Create temporary file.
54
55g = [];
56
57i = 0;
58while 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
64end
65
66theTmpName = name(g);
67
68% Copy the affected dimension first.
69
70d = {self};
71for 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
77end
78
79% Copy other dimensions.
80
81d = dim(f);
82for 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
88end
89
90% Copy global attributes.
91
92a = att(f);
93for i = 1:length(a)
94        copy(a{i}, g)
95end
96
97% Copy variable definitions and attributes.
98
99v = var(f);
100for i = 1:length(v)
101        copy(v{i}, g, 0, 1)
102end
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
112v = var(f);
113w = var(g);
114
115for 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
145end
146
147% Close both files.
148
149f = close(f);
150g = close(g);
151
152% Delete old file.
153
154delete(theSrcName)
155
156% Rename new file to old file name.
157
158fcopy(theTmpName, theSrcName)
159delete(theTmpName)
160
161% Open the new file.
162
163g = netcdf(theSrcName, thePermission);
164
165% Return the resized dimension.
166
167result = g(theItemName);
168
169if nargout > 0
170        theResult = result;
171else
172        ncans(result)
173end
Note: See TracBrowser for help on using the repository browser.