-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFUN_nc_varget_sub_genStartCount_from_presaved_data_v2.m
More file actions
185 lines (141 loc) · 6.15 KB
/
FUN_nc_varget_sub_genStartCount_from_presaved_data_v2.m
File metadata and controls
185 lines (141 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
function filedim = FUN_nc_varget_sub_genStartCount_from_presaved_data_v2( pregen_info, varname, dim_name, dim_limit, dim_varname )
% filedim = FUN_nc_varget_sub_genStartCount_from_presaved_data_v2( pregen_info, varname, dim_name, dim_limit, dim_varname )
% This is called by FUN_nc_varget_enhanced_region_2_multifile
% -------------------------------------------------------------------------
% INPUT:
% filename [char]: name of the NetCDF file (e.g., 'temp.nc')
% varname [char]: name of the variable (e.g., {'sst','ssh'})
% + varname = [] : This function will return dimensions listed in dim_name.
% dim_name [cell]: name of dimensions, like {'lon','lat'}
% dim_limit [cell]: limit of dimensions. (e.g., {[-85 -55 ], [-inf inf]})
%
% dim_varname [cell, optional]: name of the variable defining the axis at each dimension.
% + by default, each axis is defined by a variable sharing the same name as the dimension.
% + "dim_varname{1} = nan" indicates that the axis is not defined
% not defined by any variable in file. It will be defined
% as 1, 2, 3, ... Nx, where Nx is the length of the dimension.
%
% OUTPUT:
% out_dim : dimension info (e.g., longitude, latitude, if applicable)
% -------------------------------------------------------------------------
% v2.11 by L. Chi, 2025-12-24
% updated to be compatible with new feature: read data from incontinuous index in dim_limit input.
% v2.10 by L. Chi: fix 2 bugs handling v2 data
% v2.01 by L. Chi add error info for nonexisting variables
% v2.00 by L. Chi updated to be compatible with format v2 created by 'FUN_nc_gen_presaved_netcdf_info_v2'
% V1.01 by L. Chi. Fix a bug
% The function may return error unexpectedly when both
% both "dim_name" and "dim_limit" are empty
% V1.00 by L. Chi. This is extracted from "FUN_nc_varget_enhanced_region_2.m"
% (L.Chi.Ocean@outlook.com)
%% ## Set default value
if ischar( dim_name )
dim_name = {dim_name};
end
if ~isempty(dim_limit) && ~iscell( dim_limit )
dim_limit = {dim_limit};
end
if length( dim_name ) == length( dim_limit )
%PASS
else
error('Each dim_name must be assocated with one dim_limit');
end
if ~exist('dim_varname','var')
dim_varname = [];
end
if ~isempty(dim_varname)
is_override_dim = true;
else
is_override_dim = false;
end
%% ## prepare dimensions
% ### Open NetCDF
var_ind = FUN_struct_find_field_ind( pregen_info.var, 'Name', varname );
if length( var_ind ) ~= 1
error('Error in variable name');
end
if ~isempty( varname ) && isnan( var_ind )
error(['Varname: ' varname ' cannot be found!'])
end
if ~isempty( varname )
var_dim_ind = pregen_info.var(var_ind).Dim_ind;
else
for ii = 1:length( dim_name )
var_dim_ind(ii) = find( strcmpi( {pregen_info.dim(:).name}, dim_name{ii} ) );
end
end
% ### Set limit for each dimension
for jj = 1:length( pregen_info.file )
var_dim = struct;
for ii = 1:length( var_dim_ind )
tmp_ind = var_dim_ind(ii);
if pregen_info.dim(tmp_ind).is_dim_merged
var_dim(ii).Name = pregen_info.file(jj).dim.name ;
var_dim(ii).Length = pregen_info.file(jj).dim.length ;
var_dim(ii).varname= pregen_info.file(jj).dim.varname;
var_dim(ii).is_time= pregen_info.file(jj).dim.is_time;
if is_override_dim
dim_or_ind = find( strcmp( var_dim(ii).Name, dim_name ) ); % dim override ind
if ~isempty(dim_or_ind)
dim_or_id_kind = dim_varname{dim_or_ind};
if strcmpi(dim_or_id_kind, var_dim(ii).Name)
dim_or_ind = [];
end
end
end
if ~is_override_dim || isempty(dim_or_ind)
dim_val_now = pregen_info.file(jj).dim.value;
else
error('This should not be applied to the merged dim!')
end
if isscalar(pregen_info.file(jj).dim) && strcmpi(pregen_info.file(jj).dim.name, pregen_info.dim(tmp_ind).name )
else
error('debug error 01')
end
else
var_dim(ii).Name = pregen_info.dim(tmp_ind).name ;
var_dim(ii).Length = pregen_info.dim(tmp_ind).length ;
var_dim(ii).varname= pregen_info.dim(tmp_ind).varname;
var_dim(ii).is_time= pregen_info.dim(tmp_ind).is_time;
if is_override_dim
dim_or_ind = find( strcmp( var_dim(ii).Name, dim_name ) ); % dim override ind
if ~isempty(dim_or_ind)
dim_or_id_kind = dim_varname{dim_or_ind};
if strcmpi(dim_or_id_kind, var_dim(ii).Name)
dim_or_ind = [];
end
end
end
if ~is_override_dim || isempty(dim_or_ind)
dim_val_now = pregen_info.dim(tmp_ind).value ;
elseif isnan(dim_or_id_kind)
dim_val_now = 1:var_dim(ii).Length ;
elseif isnumeric(dim_or_id_kind)
dim_val_now = dim_or_id_kind ;
else
error('Unknown parrameter!')
end
end
var_dim(ii).originalVal = dim_val_now;
% ### apply the loading limits
dim_limit_ind = find( strcmp( var_dim(ii).Name, dim_name ) );
if ~isempty( dim_limit_ind )
[start, count, tem_loc] = FUN_nc_varget_sub_genStartCount( dim_val_now, dim_limit{dim_limit_ind} );
var_dim(ii).start = start;
var_dim(ii).count = count;
var_dim(ii).value = dim_val_now(tem_loc);
var_dim(ii).value_name = var_dim(ii).varname; % value_name is dim_varname_now if it existed, otherwise, value_name is the name of the dimension.
var_dim(ii).ind = tem_loc;
if isempty( var_dim(ii).value_name )
var_dim(ii).value_name = var_dim(ii).Name;
end
else
var_dim(ii).start = 0;
var_dim(ii).count = var_dim(ii).Length;
var_dim(ii).value = dim_val_now;
var_dim(ii).value_name = var_dim(ii).Name ; %
var_dim(ii).ind = [];
end
end
filedim(jj).var_dim = var_dim;
end