-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFUN_nc_varget_enhanced_region.m
135 lines (112 loc) · 4.57 KB
/
FUN_nc_varget_enhanced_region.m
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
function data = FUN_nc_varget_enhanced_region(filename, varname, start, counts, stride )
% data = FUN_nc_varget_enhanced_region( filename, varname, start, counts, stride)
% data = FUN_nc_varget_enhanced_region( filename, varname)
%
% Read the selected region of a variable.
%
% scale, offset and missing values will be corrected automatically.
% -------------------------------------------------------------------------
% INPUT:
% filename: name of the nc file
% varname : name of the variable will be read from the nc file
% start, count, stride: see doc netcdf.getVar
% -------------------------------------------------------------------------
% OUTPUT:
% data: data from the nc files
% -------------------------------------------------------------------------
% Example
% data2 = FUN_nc_varget_enhanced( 'TEST.nc', 'tempearture_3D', [ 20, 16, 30],[10, 15, 20], [1, 1, 1]);
% V1.24 by L. Chi, 2021-08-10: filename can be a 1x1 struct (e.g., results from dir('a.nc') )
% V1.23 by L. Chi, 2018-01-21: Add mask_value
% V1.22 by L. Chi, 2016-07-30: The function can be called by 2
% parameters like this: FUN_nc_varget_enhanced_region(filename,varname)
% V1.21 by L. Chi, 2015-11-30: Make sure output data will always be a double variable.
% Support add_offset
% V1.20 by L. Chi, 2015-11-11: Add auto scale_factor; fix a bug for auto-nan
% V1.12 by L. Chi, 2015-11-02: return to V1.10
% V1.11 by L. Chi, 2015-11-02: Fix a bug: double( data ) before data(nanloc) = nan;
% V1.10 by L. Chi, 2015-08-04. (L.Chi.Ocean@outlook.com)
if ~exist('stride','var') || isempty( stride )
stride = ones( size( counts ) );
end
% read path from strucutre (if applicable)
if isstruct( filename )
if isfield( filename, 'folder' ) && isfield( filename, 'name' )
filename = fullfile( filename.folder, filename.name );
elseif isfield( filename, 'name' )
filename = filename.name;
else
error('Unknown input filename format')
end
end
ncid = netcdf.open(filename,'NOWRITE');
varid = netcdf.inqVarID(ncid,varname);
count_inf_ind = find( isinf( counts ) );
if ~isempty( count_inf_ind )
% replace inf to the actuall length
[~,~,dimids,~] = netcdf.inqVar( ncid, varid );
dim_len = nan( 1, length(count_inf_ind) );
for jj = 1:length( count_inf_ind )
[~, dim_len(jj) ] = netcdf.inqDim( ncid, dimids( count_inf_ind(jj) ) );
dim_len(jj) = dim_len(jj) - start(count_inf_ind(jj));
end
counts( count_inf_ind ) = dim_len;
end
if nargin == 2
data = netcdf.getVar(ncid, varid);
elseif nargin ==4 || nargin == 5 % stride is optional
data = netcdf.getVar(ncid, varid, start, counts, stride );
else
error
end
% get the format of data ( single or double )
data_format = whos('data');
data_format = data_format.class;
%% deal with Nans
var_info = ncinfo(filename,varname);
Nan_loc = false( size(data) ) ;
% If the data is single & FillValue is double, then the FillValue must be
% converted into signle format to make sure nan can be detected correctly.
%
scale_factor = 1;
for ii = 1:length(var_info.Attributes)
if strcmp( var_info.Attributes(ii).Name, 'FillValue')
nan_val = netcdf.getAtt(ncid,varid,'FillValue');
eval( ['nan_val = ' data_format '(nan_val);'] );
Nan_loc = ( Nan_loc | data == nan_val );
clear nan_val
elseif strcmp( var_info.Attributes(ii).Name, '_FillValue')
nan_val = netcdf.getAtt(ncid,varid,'_FillValue');
eval( ['nan_val = ' data_format '(nan_val);'] );
Nan_loc = ( Nan_loc | data == nan_val );
clear nan_val
elseif strcmp( var_info.Attributes(ii).Name, 'missing_value')
nan_val = netcdf.getAtt(ncid,varid,'missing_value');
eval( ['nan_val = ' data_format '(nan_val);'] )
Nan_loc = ( Nan_loc | data == nan_val );
clear nan_val
elseif strcmp( var_info.Attributes(ii).Name, 'mask_value')
nan_val = netcdf.getAtt(ncid,varid,'mask_value');
eval( ['nan_val = ' data_format '(nan_val);'] )
Nan_loc = ( Nan_loc | data == nan_val );
clear nan_val
elseif strcmp( var_info.Attributes(ii).Name, 'scale_factor')
scale_factor = netcdf.getAtt(ncid,varid,'scale_factor');
end
end
clear ii
%%
data = double(data);
data( Nan_loc ) = nan;
data = data .* double( scale_factor );
%% Add offset
offset = 0 ;
for ii = 1:length(var_info.Attributes)
if strcmp( var_info.Attributes(ii).Name, 'add_offset')
offset = netcdf.getAtt(ncid,varid,'add_offset');
end
end
clear ii
data = data + double( offset ) ;
%% return
netcdf.close(ncid)