1 | function [] = 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 | |
---|
11 | ufield = 'u10'; |
---|
12 | vfield = 'v10'; |
---|
13 | |
---|
14 | fields = {ufield,vfield}; |
---|
15 | |
---|
16 | copyfile (windfile,outfile); |
---|
17 | |
---|
18 | %mehsgrid |
---|
19 | mg=load(meshfile); |
---|
20 | Nlat=length(unique(mg(:,2))); %Phi |
---|
21 | Nlong=length(unique(mg(:,1))); %Lambda |
---|
22 | GR_long=reshape(mg(:,1),Nlong,Nlat); |
---|
23 | GR_lat=reshape(mg(:,2),Nlong,Nlat); |
---|
24 | |
---|
25 | %Grid point size |
---|
26 | R_earth=6371229; %in meters |
---|
27 | delta{1}=zeros(Nlong,Nlat); %deltax |
---|
28 | delta{2}=zeros(Nlong,Nlat); %deltay (should correspond to fields order) |
---|
29 | |
---|
30 | for 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 | |
---|
38 | end |
---|
39 | end |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | delta{1}(Nlong,1:Nlat)=delta{1}(Nlong-1,1:Nlat); |
---|
44 | delta{2}(Nlong,1:Nlat)=delta{2}(Nlong-1,1:Nlat); |
---|
45 | |
---|
46 | |
---|
47 | delta{1}(1:Nlong,Nlat)=delta{1}(1:Nlong,Nlat-1); |
---|
48 | delta{2}(1:Nlong,Nlat)=delta{2}(1:Nlong,Nlat-1); |
---|
49 | |
---|
50 | |
---|
51 | %Open netcdf |
---|
52 | ncid = netcdf.open(outfile,'WRITE'); |
---|
53 | |
---|
54 | %lon/lat |
---|
55 | |
---|
56 | varid = netcdf.inqVarID(ncid,'latitude'); |
---|
57 | lat = netcdf.getVar(ncid,varid); |
---|
58 | varid = netcdf.inqVarID(ncid,'longitude'); |
---|
59 | lon = netcdf.getVar(ncid,varid); |
---|
60 | |
---|
61 | |
---|
62 | [LAT,LON]=meshgrid(lat,lon); %C'est à l'envers à cause de matlab |
---|
63 | |
---|
64 | timeid = netcdf.inqDimID(ncid,'time'); |
---|
65 | |
---|
66 | %Make new dimensions |
---|
67 | netcdf.reDef(ncid); |
---|
68 | latdimid = netcdf.defDim(ncid,'lat_interp',Nlat); |
---|
69 | londimid = netcdf.defDim(ncid,'lon_interp',Nlong); |
---|
70 | |
---|
71 | latid = netcdf.defVar(ncid,'lat_interp','float',latdimid); |
---|
72 | lonid = netcdf.defVar(ncid,'lon_interp','float',londimid); |
---|
73 | |
---|
74 | netcdf.endDef(ncid); |
---|
75 | |
---|
76 | %Make new lat/lon variables in netcdf |
---|
77 | netcdf.putVar(ncid,latid,unique(mg(:,2))); |
---|
78 | netcdf.putVar(ncid,lonid,unique(mg(:,1))); |
---|
79 | |
---|
80 | for 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 | |
---|
116 | end %for k |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | netcdf.close(ncid); |
---|
121 | |
---|