1 | function [field,interp_flag]=get_missing_val(lon,lat,field,missvalue,ro,default,savefile) |
---|
2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
3 | % |
---|
4 | % function field=get_missing_val(lon,lat,field,missvalue,ro,default) |
---|
5 | % |
---|
6 | % Perform an objective analysis to fill |
---|
7 | % the missing points of an horizontal gridded slice |
---|
8 | % |
---|
9 | % |
---|
10 | % input: |
---|
11 | % lon : longitude |
---|
12 | % lat : latitude |
---|
13 | % field : input 2D field |
---|
14 | % missvalue: value of the bad points (e.g. -99.999) |
---|
15 | % ro : oa decorrelation scale |
---|
16 | % default : default value given if there is only missing data |
---|
17 | % |
---|
18 | % output: |
---|
19 | % field : output 2D field |
---|
20 | % |
---|
21 | % Further Information: |
---|
22 | % http://www.brest.ird.fr/Roms_tools/ |
---|
23 | % |
---|
24 | % This file is part of ROMSTOOLS |
---|
25 | % |
---|
26 | % ROMSTOOLS is free software; you can redistribute it and/or modify |
---|
27 | % it under the terms of the GNU General Public License as published |
---|
28 | % by the Free Software Foundation; either version 2 of the License, |
---|
29 | % or (at your option) any later version. |
---|
30 | % |
---|
31 | % ROMSTOOLS is distributed in the hope that it will be useful, but |
---|
32 | % WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
33 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
34 | % GNU General Public License for more details. |
---|
35 | % |
---|
36 | % You should have received a copy of the GNU General Public License |
---|
37 | % along with this program; if not, write to the Free Software |
---|
38 | % Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
39 | % MA 02111-1307 USA |
---|
40 | % |
---|
41 | % Copyright (c) 2001-2006 by Pierrick Penven |
---|
42 | % e-mail:Pierrick.Penven@ird.fr |
---|
43 | % |
---|
44 | % Contributions from Patrick Marchesiello (IRD) and Jerome Lefevre (IRD) |
---|
45 | % |
---|
46 | % Updated 6-Sep-2006 by Pierrick Penven |
---|
47 | % Updated 2-Oct-2006 by Xavier Capet and Pierrick Penven |
---|
48 | % (add the 'savefile option') |
---|
49 | % Menkes 2007 correct for Ro |
---|
50 | % |
---|
51 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
52 | interp_flag=1; |
---|
53 | if nargin<4 |
---|
54 | oa_interp=0; |
---|
55 | missvalue=NaN; |
---|
56 | default=0; |
---|
57 | ro=0; |
---|
58 | elseif nargin<5 |
---|
59 | oa_interp=0; |
---|
60 | default=0; |
---|
61 | ro=0; |
---|
62 | elseif nargin<6 |
---|
63 | default=0; |
---|
64 | end |
---|
65 | % |
---|
66 | % get a masking matrix and the good data matrix |
---|
67 | % |
---|
68 | if isnan(missvalue) |
---|
69 | ismask=isnan(field); |
---|
70 | else |
---|
71 | ismask=(field==missvalue); |
---|
72 | end |
---|
73 | isdata=1-ismask; |
---|
74 | [M,L]=size(field); |
---|
75 | % |
---|
76 | if sum(size(lon))==(length(squeeze(lon))+1) |
---|
77 | [lon,lat]=meshgrid(lon,lat); |
---|
78 | end |
---|
79 | % |
---|
80 | % Check dimensions |
---|
81 | % |
---|
82 | if size(lon)~=size(field) |
---|
83 | error('GET-MISSING_VALUE: sizes do not correspond') |
---|
84 | end |
---|
85 | |
---|
86 | % |
---|
87 | % test if there are any data |
---|
88 | % |
---|
89 | if (sum(sum(isdata))==0) |
---|
90 | % disp('no data') |
---|
91 | disp(['GET_MISSING_VAL: No data point -> using default value:',... |
---|
92 | num2str(default)]) |
---|
93 | field=zeros(M,L)+default; |
---|
94 | interp_flag=0; |
---|
95 | return |
---|
96 | elseif (sum(sum(isdata))<6) |
---|
97 | % default=min(field(isdata==1)); |
---|
98 | default=mean(field(isdata==1)); |
---|
99 | disp(['GET_MISSING_VAL: only ',num2str(sum(sum(isdata))),... |
---|
100 | ' data points: using the mean value:',num2str(default)]) |
---|
101 | field=zeros(M,L)+default; |
---|
102 | interp_flag=0; |
---|
103 | return |
---|
104 | end |
---|
105 | if (sum(sum(ismask))==0) |
---|
106 | % disp('no mask') |
---|
107 | return |
---|
108 | end |
---|
109 | |
---|
110 | if ro>0 |
---|
111 | %--------------------------------------------------------------- |
---|
112 | % Objective Analysis |
---|
113 | %--------------------------------------------------------------- |
---|
114 | if (sum(sum(ismask))==1) |
---|
115 | % disp('1 mask') |
---|
116 | [j,i]=find(ismask==1); |
---|
117 | lat0=lat(j,i); |
---|
118 | lon0=lon(j,i); |
---|
119 | if j>1 |
---|
120 | od1=1./spheric_dist(lat0,lat(j-1,i),lon0,lon(j-1,i)); |
---|
121 | f1=field(j-1,i); |
---|
122 | else |
---|
123 | od1=0; |
---|
124 | f1=0; |
---|
125 | end |
---|
126 | if j<M |
---|
127 | od2=1./spheric_dist(lat0,lat(j+1,i),lon0,lon(j+1,i)); |
---|
128 | f2=field(j+1,i); |
---|
129 | else |
---|
130 | od2=0; |
---|
131 | f2=0; |
---|
132 | end |
---|
133 | if i>1 |
---|
134 | od3=1./spheric_dist(lat0,lat(j,i-1),lon0,lon(j,i-1)); |
---|
135 | f3=field(j,i-1); |
---|
136 | else |
---|
137 | od3=0; |
---|
138 | f3=0; |
---|
139 | end |
---|
140 | if i<L |
---|
141 | od4=1./spheric_dist(lat0,lat(j,i+1),lon0,lon(j,i+1)); |
---|
142 | f4=field(j,i+1); |
---|
143 | else |
---|
144 | od4=0; |
---|
145 | f4=0; |
---|
146 | end |
---|
147 | field(j,i)=(od1.*f1+od2.*f2+od3.*f3+od4.*f4)./... |
---|
148 | (od1+od2+od3+od4); |
---|
149 | return |
---|
150 | end |
---|
151 | % |
---|
152 | % perform the oa (if savefile is given, store the distances matrices |
---|
153 | % (r1 and r2) in a tmp.mat file) |
---|
154 | % |
---|
155 | if nargin == 7 |
---|
156 | field(ismask)=oainterp(lon(~ismask),lat(~ismask),field(~ismask),... |
---|
157 | lon(ismask),lat(ismask),ro,savefile); |
---|
158 | else |
---|
159 | field(ismask)=oainterp(lon(~ismask),lat(~ismask),field(~ismask),... |
---|
160 | lon(ismask),lat(ismask),ro,2); |
---|
161 | end |
---|
162 | else |
---|
163 | %--------------------------------------------------------------- |
---|
164 | % Extrapolation using nearest values |
---|
165 | %-------------------------------------------------------------- |
---|
166 | field(ismask)=griddata(lon(~ismask),lat(~ismask),field(~ismask),... |
---|
167 | lon(ismask),lat(ismask),'nearest'); |
---|
168 | end |
---|
169 | return |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | |
---|