10478
|
1 function ncarray_example_file(filename,data) |
|
2 |
11998
|
3 if ~isempty(which('nccreate')) && ~isempty(which('ncwriteatt')) && ... |
|
4 ~isempty(which('ncwrite')) |
|
5 % use matlab netcdf high level interface |
10478
|
6 |
12193
|
7 %dtype = 'single'; |
|
8 dtype = 'double'; |
|
9 |
11998
|
10 % Variables |
12193
|
11 nccreate(filename,'lon','Format','classic','Datatype',dtype,... |
11998
|
12 'Dimensions',{'x',220, 'y',144}); |
|
13 ncwriteatt(filename,'lon','long_name','Longitude') |
|
14 ncwriteatt(filename,'lon','units','degrees_east') |
|
15 |
12193
|
16 nccreate(filename,'lat','Datatype',dtype,'Dimensions',{'x',220, 'y',144}); |
11998
|
17 ncwriteatt(filename,'lat','long_name','Latitude') |
|
18 ncwriteatt(filename,'lat','units','degrees_north') |
|
19 |
12193
|
20 nccreate(filename,'time','Datatype',dtype,'Dimensions',{'time',1}); |
11998
|
21 ncwriteatt(filename,'time','long_name','Time') |
|
22 ncwriteatt(filename,'time','units','days since 1858-11-17 00:00:00 GMT') |
|
23 |
12193
|
24 nccreate(filename,'SST','Datatype',dtype,'Dimensions',... |
11998
|
25 {'x',220, 'y',144, 'time',1}); |
|
26 ncwriteatt(filename,'SST','missing_value',single(9999)) |
|
27 ncwriteatt(filename,'SST','_FillValue',single(9999)) |
|
28 ncwriteatt(filename,'SST','units','degC') |
|
29 ncwriteatt(filename,'SST','long_name','Sea Surface Temperature') |
|
30 ncwriteatt(filename,'SST','coordinates','lat lon') |
10478
|
31 |
11998
|
32 ncwrite(filename,'SST',data); |
|
33 else |
|
34 % use octcdf interface |
10478
|
35 |
11998
|
36 nc = netcdf(filename,'c'); |
10478
|
37 |
11998
|
38 % dimensions |
|
39 |
|
40 nc('x') = size(data,1); |
|
41 nc('y') = size(data,2); |
|
42 nc('time') = size(data,3); |
|
43 |
|
44 % variables |
|
45 |
12193
|
46 nc{'lon'} = ncdouble('y','x'); % 31680 elements |
11998
|
47 nc{'lon'}.long_name = ncchar('Longitude'); |
|
48 nc{'lon'}.units = ncchar('degrees_east'); |
|
49 |
12193
|
50 nc{'lat'} = ncdouble('y','x'); % 31680 elements |
11998
|
51 nc{'lat'}.long_name = ncchar('Latitude'); |
|
52 nc{'lat'}.units = ncchar('degrees_north'); |
10478
|
53 |
12193
|
54 nc{'time'} = ncdouble('time'); % 1 elements |
11998
|
55 nc{'time'}.long_name = ncchar('Time'); |
|
56 nc{'time'}.units = ncchar('days since 1858-11-17 00:00:00 GMT'); |
|
57 |
12193
|
58 nc{'SST'} = ncdouble('time','y','x'); % 31680 elements |
|
59 nc{'SST'}.missing_value = ncdouble(9999); |
|
60 nc{'SST'}.FillValue_ = ncdouble(9999); |
11998
|
61 nc{'SST'}.units = ncchar('degC'); |
|
62 nc{'SST'}.long_name = ncchar('Sea Surface Temperature'); |
|
63 nc{'SST'}.coordinates = ncchar('lat lon'); |
10478
|
64 |
11998
|
65 % global attributes |
|
66 |
|
67 nc{'SST'}(:) = permute(data,[3 2 1]); |
|
68 close(nc) |
|
69 end |
10478
|
70 |
11998
|
71 % Copyright (C) 2012,2013 Alexander Barth <barth.alexander@gmail.com> |
10535
|
72 % |
|
73 % This program is free software; you can redistribute it and/or modify |
|
74 % it under the terms of the GNU General Public License as published by |
|
75 % the Free Software Foundation; either version 2 of the License, or |
|
76 % (at your option) any later version. |
|
77 % |
|
78 % This program is distributed in the hope that it will be useful, |
|
79 % but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
80 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
81 % GNU General Public License for more details. |
|
82 % |
|
83 % You should have received a copy of the GNU General Public License |
|
84 % along with this program; If not, see <http://www.gnu.org/licenses/>. |
|
85 |