1 | function data=ext_data(datafile,dataname,tindex,lon,lat,time,Roa,savefile) |
---|
2 | % |
---|
3 | % Read a data file and extrapole 1 horizontal |
---|
4 | % slice on a ROMS grid |
---|
5 | % |
---|
6 | % |
---|
7 | % Further Information: |
---|
8 | % http://www.brest.ird.fr/Roms_tools/ |
---|
9 | % |
---|
10 | % This file is part of ROMSTOOLS |
---|
11 | % |
---|
12 | % ROMSTOOLS is free software; you can redistribute it and/or modify |
---|
13 | % it under the terms of the GNU General Public License as published |
---|
14 | % by the Free Software Foundation; either version 2 of the License, |
---|
15 | % or (at your option) any later version. |
---|
16 | % |
---|
17 | % ROMSTOOLS is distributed in the hope that it will be useful, but |
---|
18 | % WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | % GNU General Public License for more details. |
---|
21 | % |
---|
22 | % You should have received a copy of the GNU General Public License |
---|
23 | % along with this program; if not, write to the Free Software |
---|
24 | % Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
25 | % MA 02111-1307 USA |
---|
26 | % |
---|
27 | % Copyright (c) 2001-2006 by Pierrick Penven |
---|
28 | % e-mail:Pierrick.Penven@ird.fr |
---|
29 | % |
---|
30 | % Updated 5-Oct-2006 by Pierrick Penven (test for negative salinity) |
---|
31 | % |
---|
32 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
33 | % |
---|
34 | if nargin < 8 |
---|
35 | savefile=2; |
---|
36 | end |
---|
37 | |
---|
38 | disp(['Getting ',dataname,' for time index ',num2str(tindex)]) |
---|
39 | % |
---|
40 | default=NaN; |
---|
41 | % |
---|
42 | % |
---|
43 | % |
---|
44 | dl=1; |
---|
45 | lonmin=min(min(lon))-dl; |
---|
46 | lonmax=max(max(lon))+dl; |
---|
47 | latmin=min(min(lat))-dl; |
---|
48 | latmax=max(max(lat))+dl; |
---|
49 | % |
---|
50 | % Open the data file |
---|
51 | % |
---|
52 | ncdat=netcdf(datafile); |
---|
53 | % |
---|
54 | % Get attributes |
---|
55 | % |
---|
56 | missval=ncdat{dataname}.missing_value(:); |
---|
57 | if isempty(missval) |
---|
58 | missval=nan; |
---|
59 | end |
---|
60 | add_offset=ncdat{dataname}.add_offset(:); |
---|
61 | scale_factor=ncdat{dataname}.scale_factor(:); |
---|
62 | ndims=length(dim(ncdat{dataname})); |
---|
63 | % |
---|
64 | % Get lon,lat,t |
---|
65 | % |
---|
66 | X=ncdat{'X'}(:); |
---|
67 | if isempty(X) |
---|
68 | X=ncdat{'lon'}(:); |
---|
69 | end |
---|
70 | if isempty(X) |
---|
71 | X=ncdat{'longitude'}(:); |
---|
72 | end |
---|
73 | if isempty(X) |
---|
74 | error(['Empty longitude in ',datafile]) |
---|
75 | end |
---|
76 | Y=ncdat{'Y'}(:); |
---|
77 | if isempty(Y) |
---|
78 | Y=ncdat{'lat'}(:); |
---|
79 | end |
---|
80 | if isempty(Y) |
---|
81 | Y=ncdat{'latitude'}(:); |
---|
82 | end |
---|
83 | if isempty(Y) |
---|
84 | error(['Empty latitude in ',datafile]) |
---|
85 | end |
---|
86 | |
---|
87 | %T=30*ncdat{'T'}(tindex); |
---|
88 | %if T~=time |
---|
89 | % disp(['Warning incorrect time :',dataname,... |
---|
90 | % ' - ',num2str(tindex),... |
---|
91 | % ' - ',num2str(T),... |
---|
92 | % ' - ',num2str(time)]) |
---|
93 | %end |
---|
94 | % |
---|
95 | % get a subgrid |
---|
96 | % |
---|
97 | j=find(Y>=latmin & Y<=latmax); |
---|
98 | i1=find(X-360>=lonmin & X-360<=lonmax); |
---|
99 | i2=find(X>=lonmin & X<=lonmax); |
---|
100 | i3=find(X+360>=lonmin & X+360<=lonmax); |
---|
101 | x=cat(1,X(i1)-360,X(i2),X(i3)+360); |
---|
102 | y=Y(j); |
---|
103 | % |
---|
104 | % Read data |
---|
105 | % |
---|
106 | if ~isempty(i2) |
---|
107 | if ndims==3 |
---|
108 | data=squeeze(ncdat{dataname}(tindex,j,i2)); |
---|
109 | elseif ndims==4 |
---|
110 | data=squeeze(ncdat{dataname}(tindex,1,j,i2)); |
---|
111 | else |
---|
112 | error(['Bad dimension number ',num2str(ndims)]) |
---|
113 | end |
---|
114 | else |
---|
115 | data=[]; |
---|
116 | end |
---|
117 | if ~isempty(i1) |
---|
118 | if ndims==3 |
---|
119 | data=cat(2,squeeze(ncdat{dataname}(tindex,j,i1)),data); |
---|
120 | elseif ndims==4 |
---|
121 | data=cat(2,squeeze(ncdat{dataname}(tindex,1,j,i1)),data); |
---|
122 | else |
---|
123 | error(['Bad dimension number ',num2str(ndims)]) |
---|
124 | end |
---|
125 | end |
---|
126 | if ~isempty(i3) |
---|
127 | if ndims==3 |
---|
128 | data=cat(2,data,squeeze(ncdat{dataname}(tindex,j,i3))); |
---|
129 | elseif ndims==4 |
---|
130 | data=cat(2,data,squeeze(ncdat{dataname}(tindex,1,j,i3))); |
---|
131 | else |
---|
132 | error(['Bad dimension number ',num2str(ndims)]) |
---|
133 | end |
---|
134 | end |
---|
135 | close(ncdat) |
---|
136 | % |
---|
137 | % Perform the extrapolation |
---|
138 | % |
---|
139 | if savefile==2 |
---|
140 | data=get_missing_val(x,y,data,missval,Roa,default,2); |
---|
141 | else |
---|
142 | if tindex==1 |
---|
143 | data=get_missing_val(x,y,data,missval,Roa,default,1); |
---|
144 | else |
---|
145 | data=get_missing_val(x,y,data,missval,Roa,default,0); |
---|
146 | end |
---|
147 | end |
---|
148 | % |
---|
149 | % Interpolation on the ROMS grid |
---|
150 | % |
---|
151 | data=interp2(x,y,data,lon,lat,'linear'); |
---|
152 | % |
---|
153 | % Apply offset |
---|
154 | % |
---|
155 | if ~isempty(add_offset) |
---|
156 | data=add_offset+data*scale_factor; |
---|
157 | end |
---|
158 | % |
---|
159 | % Test for salinity (no negative salinity !) |
---|
160 | % |
---|
161 | if strcmp(dataname,'salinity') |
---|
162 | disp('salinity test') |
---|
163 | data(data<2)=2; |
---|
164 | end |
---|
165 | % |
---|
166 | return |
---|