source: altifloat/matlab_toolbox/interp_wind.m @ 148

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

wind interpolation with matlab

File size: 2.6 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
26%Open netcdf
27ncid = netcdf.open(outfile,'WRITE');
28
29%lon/lat
30
31varid = netcdf.inqVarID(ncid,'latitude');
32lat = netcdf.getVar(ncid,varid);
33varid = netcdf.inqVarID(ncid,'longitude');
34lon = netcdf.getVar(ncid,varid);
35
36
37[LAT,LON]=meshgrid(lat,lon); %C'est à l'envers à cause de matlab
38
39timeid = netcdf.inqDimID(ncid,'time');
40
41%Make new dimensions
42netcdf.reDef(ncid);
43latdimid = netcdf.defDim(ncid,'lat_interp',Nlat);
44londimid = netcdf.defDim(ncid,'lon_interp',Nlong);
45
46latid = netcdf.defVar(ncid,'lat_interp','float',latdimid);
47lonid = netcdf.defVar(ncid,'lon_interp','float',londimid);
48
49netcdf.endDef(ncid);
50
51%Make new lat/lon variables in netcdf
52netcdf.putVar(ncid,latid,unique(mg(:,2)));
53netcdf.putVar(ncid,lonid,unique(mg(:,1)));
54
55%for k=1:length(fields)
56for 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 
90end %for k
91
92
93
94netcdf.close(ncid);
95
Note: See TracBrowser for help on using the repository browser.