source: altifloat/matlab_toolbox/interp_wind.m @ 199

Last change on this file since 199 was 160, checked in by jbrlod, 9 years ago

finalize wind effect

File size: 3.3 KB
Line 
1function [] = interp_wind (windfile,meshfile,outfile)
2%[] = interp_wind (windfile,meshfile,outfile)
3%outfile is a netcdf file
4
5
6%Test the function
7%windfile = '../../data/wind/wind_20130825-20130930.nc';
8%meshfile = '../exp_dan_lr/meshgrid_aviso.dat';
9%outfile  = '../exp_dan_lr/wind_interp.nc';
10
11ufield = 'u10';
12vfield = 'v10';
13
14fields = {ufield,vfield};
15
16copyfile (windfile,outfile);
17
18%mehsgrid
19mg=load(meshfile);
20Nlat=length(unique(mg(:,2))); %Phi
21Nlong=length(unique(mg(:,1))); %Lambda
22GR_long=reshape(mg(:,1),Nlong,Nlat);
23GR_lat=reshape(mg(:,2),Nlong,Nlat);
24
25%Grid point size
26R_earth=6371229; %in meters
27delta{1}=zeros(Nlong,Nlat); %deltax
28delta{2}=zeros(Nlong,Nlat); %deltay (should correspond to fields order)
29
30for ilon=1:Nlong-1;
31    for jlat=1:Nlat-1;
32       
33    delta{1}(ilon,jlat)=R_earth*(2*pi/360)*(GR_long(ilon+1,jlat)-GR_long(ilon,jlat))...
34        .*cos(2*pi*GR_lat(ilon,jlat)/360);
35    delta{2}(ilon,jlat)=R_earth*(2*pi/360)*(GR_lat(ilon,jlat+1)-GR_lat(ilon,jlat)); 
36   
37   
38end
39end
40
41
42
43delta{1}(Nlong,1:Nlat)=delta{1}(Nlong-1,1:Nlat);
44delta{2}(Nlong,1:Nlat)=delta{2}(Nlong-1,1:Nlat);
45
46
47delta{1}(1:Nlong,Nlat)=delta{1}(1:Nlong,Nlat-1);
48delta{2}(1:Nlong,Nlat)=delta{2}(1:Nlong,Nlat-1);
49
50
51%Open netcdf
52ncid = netcdf.open(outfile,'WRITE');
53
54%lon/lat
55
56varid = netcdf.inqVarID(ncid,'latitude');
57lat = netcdf.getVar(ncid,varid);
58varid = netcdf.inqVarID(ncid,'longitude');
59lon = netcdf.getVar(ncid,varid);
60
61
62[LAT,LON]=meshgrid(lat,lon); %C'est à l'envers à cause de matlab
63
64timeid = netcdf.inqDimID(ncid,'time');
65
66%Make new dimensions
67netcdf.reDef(ncid);
68latdimid = netcdf.defDim(ncid,'lat_interp',Nlat);
69londimid = netcdf.defDim(ncid,'lon_interp',Nlong);
70
71latid = netcdf.defVar(ncid,'lat_interp','float',latdimid);
72lonid = netcdf.defVar(ncid,'lon_interp','float',londimid);
73
74netcdf.endDef(ncid);
75
76%Make new lat/lon variables in netcdf
77netcdf.putVar(ncid,latid,unique(mg(:,2)));
78netcdf.putVar(ncid,lonid,unique(mg(:,1)));
79
80for k=1:length(fields)
81%for k=1:1
82 
83  %Read var and attributes
84  varid = netcdf.inqVarID(ncid,fields{k});
85  data = netcdf.getVar(ncid,varid); %time is the last dimension
86  scale_factor = netcdf.getAtt(ncid,varid,'scale_factor');
87  add_offset = netcdf.getAtt(ncid,varid,'add_offset');
88  FillValue = netcdf.getAtt(ncid,varid,'_FillValue');
89  missing_value = netcdf.getAtt(ncid,varid,'missing_value');
90  %units = netcdf.getAtt(ncid,varid,'units');
91  %long_name = netcdf.getAtt(ncid,varid,'long_name');
92 
93  %Convert to floating point
94  data_f = double(data)*scale_factor + add_offset ;
95  data_f(data == FillValue | data == missing_value) = nan ;
96 
97  %interp on meshgrid
98  data_fi = nan*ones(Nlong,Nlat,size(data,3));
99  for it=1:size(data,3)
100  data_fi(:,:,it) = interp2(LAT,LON,data_f(:,:,it),GR_lat,GR_long,'nearest')./delta{k};
101  end
102 
103  % Save in the ncfile
104  netcdf.reDef(ncid);
105  dimids = [londimid latdimid timeid];
106  newid = netcdf.defVar(ncid,[fields{k} '_f'],'float',dimids);
107  %netcdf.copyAtt(ncid,varid,'units',ncid,newid);
108  netcdf.putAtt(ncid,newid,'units','gridpoint per second');
109  netcdf.copyAtt(ncid,varid,'long_name',ncid,newid);
110  netcdf.putAtt(ncid,newid,'Description',['Convert to floating point and interpolated using' ...
111                      meshfile ' file - ' datestr(now)]);
112  netcdf.endDef(ncid);
113  netcdf.putVar(ncid,newid,data_fi);
114 
115 
116end %for k
117
118
119
120netcdf.close(ncid);
121
Note: See TracBrowser for help on using the repository browser.