source: Roms_tools/Preprocessing_tools/ext_data.m @ 1

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

import Roms_Agrif

File size: 3.6 KB
Line 
1function 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%
34if nargin < 8
35  savefile=2;
36end
37
38disp(['Getting ',dataname,' for time index ',num2str(tindex)])
39%
40default=NaN;
41%
42%
43%
44dl=1;
45lonmin=min(min(lon))-dl;
46lonmax=max(max(lon))+dl;
47latmin=min(min(lat))-dl;
48latmax=max(max(lat))+dl;
49%
50% Open the data file
51%
52ncdat=netcdf(datafile);
53%
54% Get attributes
55%
56missval=ncdat{dataname}.missing_value(:);
57if isempty(missval)
58  missval=nan;
59end
60add_offset=ncdat{dataname}.add_offset(:);
61scale_factor=ncdat{dataname}.scale_factor(:);
62ndims=length(dim(ncdat{dataname}));
63%
64% Get lon,lat,t
65%
66X=ncdat{'X'}(:);
67if isempty(X)
68  X=ncdat{'lon'}(:);
69end
70if isempty(X)
71  X=ncdat{'longitude'}(:);
72end
73if isempty(X)
74  error(['Empty longitude in ',datafile])
75end
76Y=ncdat{'Y'}(:);
77if isempty(Y)
78  Y=ncdat{'lat'}(:);
79end
80if isempty(Y)
81  Y=ncdat{'latitude'}(:);
82end
83if isempty(Y)
84  error(['Empty latitude in ',datafile])
85end
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%
97j=find(Y>=latmin & Y<=latmax);
98i1=find(X-360>=lonmin & X-360<=lonmax);
99i2=find(X>=lonmin & X<=lonmax);
100i3=find(X+360>=lonmin & X+360<=lonmax);
101x=cat(1,X(i1)-360,X(i2),X(i3)+360);
102y=Y(j);
103%
104%  Read data
105%
106if ~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
114else
115  data=[];
116end
117if ~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
125end
126if ~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
134end
135close(ncdat)
136%
137% Perform the extrapolation
138%
139if savefile==2
140  data=get_missing_val(x,y,data,missval,Roa,default,2);
141else
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
147end
148%
149% Interpolation on the ROMS grid
150%
151data=interp2(x,y,data,lon,lat,'linear');
152%
153% Apply offset
154%
155if ~isempty(add_offset)
156  data=add_offset+data*scale_factor;
157end
158%
159% Test for salinity (no negative salinity !)
160%
161if strcmp(dataname,'salinity')
162  disp('salinity test')
163  data(data<2)=2;
164end
165%
166return
Note: See TracBrowser for help on using the repository browser.