1 | function data=ext_data(datafile,dataname,tindex,lon,lat,time) |
---|
2 | % |
---|
3 | % Read a data file and extrapole 1 horizontal |
---|
4 | % slice on a ROMS grid |
---|
5 | % |
---|
6 | disp(['Getting ',dataname,' for time index ',num2str(tindex)]) |
---|
7 | % |
---|
8 | % |
---|
9 | % |
---|
10 | ro=1e8; |
---|
11 | default=NaN; |
---|
12 | % |
---|
13 | % |
---|
14 | % |
---|
15 | dl=1; |
---|
16 | lonmin=min(min(lon))-dl; |
---|
17 | lonmax=max(max(lon))+dl; |
---|
18 | latmin=min(min(lat))-dl; |
---|
19 | latmax=max(max(lat))+dl; |
---|
20 | % |
---|
21 | % Open the data file |
---|
22 | % |
---|
23 | ncdat=netcdf(datafile); |
---|
24 | |
---|
25 | % |
---|
26 | % Get attributes |
---|
27 | % |
---|
28 | missval=ncdat{dataname}.missing_value(:); |
---|
29 | if isempty(missval) |
---|
30 | missval=nan; |
---|
31 | end |
---|
32 | add_offset=ncdat{dataname}.add_offset(:); |
---|
33 | scale_factor=ncdat{dataname}.scale_factor(:); |
---|
34 | ndims=length(dim(ncdat{dataname})); |
---|
35 | % |
---|
36 | % Get lon,lat,t |
---|
37 | % |
---|
38 | X=ncdat{'X'}(:); |
---|
39 | Y=ncdat{'Y'}(:); |
---|
40 | T=30*ncdat{'T'}(tindex)-15; |
---|
41 | if T~=time |
---|
42 | disp(['Warning incorrect time :',dataname,... |
---|
43 | ' - ',num2str(tindex),... |
---|
44 | ' - ',num2str(T),... |
---|
45 | ' - ',num2str(time)]) |
---|
46 | end |
---|
47 | % |
---|
48 | % get a subgrid |
---|
49 | % |
---|
50 | j=find(Y>=latmin & Y<=latmax); |
---|
51 | i1=find(X-360>=lonmin & X-360<=lonmax); |
---|
52 | i2=find(X>=lonmin & X<=lonmax); |
---|
53 | i3=find(X+360>=lonmin & X+360<=lonmax); |
---|
54 | x=cat(1,X(i1)-360,X(i2),X(i3)+360); |
---|
55 | y=Y(j); |
---|
56 | % |
---|
57 | % Read data |
---|
58 | % |
---|
59 | if ~isempty(i2) |
---|
60 | if ndims==3 |
---|
61 | data=squeeze(ncdat{dataname}(tindex,j,i2)); |
---|
62 | elseif ndims==4 |
---|
63 | data=squeeze(ncdat{dataname}(tindex,1,j,i2)); |
---|
64 | else |
---|
65 | error(['Bad dimension number ',num2str(ndims)]) |
---|
66 | end |
---|
67 | else |
---|
68 | data=[]; |
---|
69 | end |
---|
70 | if ~isempty(i1) |
---|
71 | if ndims==3 |
---|
72 | data=cat(2,squeeze(ncdat{dataname}(tindex,j,i1)),data); |
---|
73 | elseif ndims==4 |
---|
74 | data=cat(2,squeeze(ncdat{dataname}(tindex,1,j,i1)),data); |
---|
75 | else |
---|
76 | error(['Bad dimension number ',num2str(ndims)]) |
---|
77 | end |
---|
78 | end |
---|
79 | if ~isempty(i3) |
---|
80 | if ndims==3 |
---|
81 | data=cat(2,data,squeeze(ncdat{dataname}(tindex,j,i3))); |
---|
82 | elseif ndims==4 |
---|
83 | data=cat(2,data,squeeze(ncdat{dataname}(tindex,1,j,i3))); |
---|
84 | else |
---|
85 | error(['Bad dimension number ',num2str(ndims)]) |
---|
86 | end |
---|
87 | end |
---|
88 | close(ncdat) |
---|
89 | % |
---|
90 | % Perform the extrapolation |
---|
91 | % |
---|
92 | lon |
---|
93 | lat |
---|
94 | x |
---|
95 | y |
---|
96 | stop |
---|
97 | |
---|
98 | data=get_missing_val(x,y,data,missval,ro,default); |
---|
99 | % |
---|
100 | % Interpolation on the ROMS grid |
---|
101 | % |
---|
102 | pcolor(data) |
---|
103 | |
---|
104 | data=interp2(x,y,data,lon,lat,'linear'); |
---|
105 | % |
---|
106 | % Apply offset |
---|
107 | % |
---|
108 | if ~isempty(add_offset) |
---|
109 | data=add_offset+data*scale_factor; |
---|
110 | end |
---|
111 | % |
---|
112 | return |
---|