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 | |
---|
26 | %Open netcdf |
---|
27 | ncid = netcdf.open(outfile,'WRITE'); |
---|
28 | |
---|
29 | %lon/lat |
---|
30 | |
---|
31 | varid = netcdf.inqVarID(ncid,'latitude'); |
---|
32 | lat = netcdf.getVar(ncid,varid); |
---|
33 | varid = netcdf.inqVarID(ncid,'longitude'); |
---|
34 | lon = netcdf.getVar(ncid,varid); |
---|
35 | |
---|
36 | |
---|
37 | [LAT,LON]=meshgrid(lat,lon); %C'est à l'envers à cause de matlab |
---|
38 | |
---|
39 | timeid = netcdf.inqDimID(ncid,'time'); |
---|
40 | |
---|
41 | %Make new dimensions |
---|
42 | netcdf.reDef(ncid); |
---|
43 | latdimid = netcdf.defDim(ncid,'lat_interp',Nlat); |
---|
44 | londimid = netcdf.defDim(ncid,'lon_interp',Nlong); |
---|
45 | |
---|
46 | latid = netcdf.defVar(ncid,'lat_interp','float',latdimid); |
---|
47 | lonid = netcdf.defVar(ncid,'lon_interp','float',londimid); |
---|
48 | |
---|
49 | netcdf.endDef(ncid); |
---|
50 | |
---|
51 | %Make new lat/lon variables in netcdf |
---|
52 | netcdf.putVar(ncid,latid,unique(mg(:,2))); |
---|
53 | netcdf.putVar(ncid,lonid,unique(mg(:,1))); |
---|
54 | |
---|
55 | %for k=1:length(fields) |
---|
56 | for k=1:1 |
---|
57 | |
---|
58 | %Read var and attributes |
---|
59 | varid = netcdf.inqVarID(ncid,fields{k}); |
---|
60 | data = netcdf.getVar(ncid,varid); %time is the last dimension |
---|
61 | scale_factor = netcdf.getAtt(ncid,varid,'scale_factor'); |
---|
62 | add_offset = netcdf.getAtt(ncid,varid,'add_offset'); |
---|
63 | FillValue = netcdf.getAtt(ncid,varid,'_FillValue'); |
---|
64 | missing_value = netcdf.getAtt(ncid,varid,'missing_value'); |
---|
65 | %units = netcdf.getAtt(ncid,varid,'units'); |
---|
66 | %long_name = netcdf.getAtt(ncid,varid,'long_name'); |
---|
67 | |
---|
68 | %Convert to floating point |
---|
69 | data_f = double(data)*scale_factor + add_offset ; |
---|
70 | data_f(data == FillValue | data == missing_value) = nan ; |
---|
71 | |
---|
72 | %interp on meshgrid |
---|
73 | data_fi = nan*ones(Nlong,Nlat,size(data,3)); |
---|
74 | for it=1:size(data,3) |
---|
75 | data_fi(:,:,it) = interp2(LAT,LON,data_f(:,:,it),GR_lat,GR_long,'nearest'); |
---|
76 | end |
---|
77 | |
---|
78 | % Save in the ncfile |
---|
79 | netcdf.reDef(ncid); |
---|
80 | dimids = [londimid latdimid timeid]; |
---|
81 | newid = netcdf.defVar(ncid,[fields{k} '_f'],'float',dimids); |
---|
82 | netcdf.copyAtt(ncid,varid,'units',ncid,newid); |
---|
83 | netcdf.copyAtt(ncid,varid,'long_name',ncid,newid); |
---|
84 | netcdf.putAtt(ncid,newid,'Description',['Convert to floating point and interpolated using' ... |
---|
85 | meshfile ' file - ' datestr(now)]); |
---|
86 | netcdf.endDef(ncid); |
---|
87 | netcdf.putVar(ncid,newid,data_fi); |
---|
88 | |
---|
89 | |
---|
90 | end %for k |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | netcdf.close(ncid); |
---|
95 | |
---|