-
Notifications
You must be signed in to change notification settings - Fork 0
/
grdwrite2p.m
executable file
·184 lines (173 loc) · 6.36 KB
/
grdwrite2p.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
function grdwrite2p(x,y,z,file);
%GRDWRITE2P Write a GMT grid file
%
% Uses built-in NetCDF capability (MATLAB R2008b or later) to
% write a COARDS-compliant netCDF grid file
% Duplicates (some) functionality of the program grdwrite (which requires
% compilation as a mexfile-based function on each architecture) using
% Matlab 2008b (and later) built-in NetCDF functionality
% instead of GMT libraries.
%
% GRDWRITE2P(X,Y,Z,'filename') will create a grid file containing the
% data in the matrix Z. X and Y should be either vectors with
% dimensions that match the size of Z or two-component vectors
% containing the max and min values for each.
%
% See also GRDWRITE2, GRDREAD2, GRDINFO2
%
% Contains changes to adapt it to Octave by plattner-at-alumni.ethz.ch, 3/6/2015
%
% For more information on GMT grid file formats, see:
% http://www.soest.hawaii.edu/gmt/gmt/doc/gmt/html/GMT_Docs/node70.html
% Details on Matlab's native netCDF capabilities are at:
% http://www.mathworks.com/access/helpdesk/help/techdoc/ref/netcdf.html
%
% GMT (Generic Mapping Tools, <http://gmt.soest.hawaii.edu>)
% was developed by Paul Wessel and Walter H. F. Smith
%
% Original by
%
% Kelsey Jordahl
% Marymount Manhattan College
% http://marymount.mmm.edu/faculty/kjordahl/software.html
%
% Time-stamp: <Tue Jul 19 16:28:24 EDT 2011>
%
% Version 1.1.2, 19-Jul-2011
% Available at MATLAB Central
% <http://www.mathworks.com/matlabcentral/fileexchange/26290-grdwrite2>
%warning('You are using grdwrite2.m written by Kelsey Jordahl, Marymount Manhattan College')
%warning('Plattner has adapted this software for Octave but has not written the original!!!')
%warning('Original available from http://www.mathworks.com/matlabcentral/fileexchange/26290-grdwrite2')
if nargin < 4,
help(mfilename);
return,
end
% plattner-at-alumni.ethz.ch, 3/6/2015:
% I adapted it to work with Octave's netcdf
% % check for appropriate Matlab version (>=7.7)
% V=regexp(version,'[ \.]','split');
% if (str2num(V{1})<7) | (str2num(V{1})==7 & str2num(V{2})<7),
% ver
% error('grdread2: Requires Matlab R2008b or later!');
% end
runningoctave=0;
if exist('OCTAVE_VERSION','builtin')
pkg load netcdf
runningoctave=1;
end
if runningoctave
ncid = netcdf_create(file, 'NC_SHARE');
else
ncid = netcdf.create(file, 'NC_SHARE');
end
if isempty(ncid),
return,
end
% set descriptive variables
conv='COARDS/CF-1.0';
title=file;
history='File written by MATLAB function grdwrite2.m';
desc=['Created ' datestr(now)];
vers='4.x'; % is "x" OK?
% check X and Y
if (~isvector(x) | ~isvector(y)),
error('X and Y must be vectors!');
end
if (length(x) ~= size(z,2)), % number of columns don't match size of x
minx=min(x); maxx=max(x);
dx=(maxx-minx)/(size(z,2)-1);
x=minx:dx:maxx; % write as a vector
end
if (length(y) ~= size(z,1)), % number of rows don't match size of y
miny=min(y); maxy=max(y);
dy=(maxy-miny)/(size(z,1)-1);
y=miny:dy:maxy; % write as a vector
end
% match Matlab class to NetCDF data type
switch class(z)
case 'single'
nctype='NC_FLOAT';
nanfill=single(NaN);
case 'double'
nctype='NC_DOUBLE';
nanfill=double(NaN);
case 'int8'
nctype='NC_BYTE';
nanfill=intmin(class(z));
disp(['Warning: ''No data'' fill value set to ' num2str(nanfill)])
case 'int16'
nctype='NC_SHORT';
nanfill=intmin(class(z));
disp(['Warning: ''No data'' fill value set to ' num2str(nanfill)])
case 'int32'
nctype='NC_INT';
nanfill=intmin(class(z));
disp(['Warning: ''No data'' fill value set to ' num2str(nanfill)])
otherwise
error(['Don''t know how to handle data of class ''' class(z) '''. Try converting to a supported data type (int8, int16, int32, single or double).'])
end
% global
if runningoctave % plattner-at-alumni.ethz.ch 3/6/2015 I added this to allow it running in Octave
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'),'Conventions',conv);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'),'title',title);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'),'history',history);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'),'description',desc);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'),'GMT_version',vers);
% X
dimid = netcdf_defDim(ncid,'x',length(x));
varid = netcdf_defVar(ncid,'x','double',dimid);
netcdf_putAtt(ncid,varid,'long_name','x');
netcdf_putAtt(ncid,varid,'actual_range',[min(x) max(x)]);
netcdf_endDef(ncid);
netcdf_putVar(ncid,varid,x);
% Y
netcdf_reDef(ncid);
dimid = netcdf_defDim(ncid,'y',length(y));
varid = netcdf_defVar(ncid,'y','double',dimid);
netcdf_putAtt(ncid,varid,'long_name','y');
netcdf_putAtt(ncid,varid,'actual_range',[min(y) max(y)]);
netcdf_endDef(ncid);
netcdf_putVar(ncid,varid,y);
% Z
netcdf_reDef(ncid);
varid = netcdf_defVar(ncid,'z',nctype,[0 1]);
netcdf_putAtt(ncid,varid,'long_name','z');
netcdf_putAtt(ncid,varid,'_FillValue',nanfill);
netcdf_putAtt(ncid,varid,'actual_range',[min(z(:)) max(z(:))]);
netcdf_endDef(ncid);
netcdf_putVar(ncid,varid,z');
% close file
netcdf_close(ncid);
else % The original Matlab version
netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Conventions',conv);
netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'title',title);
netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'history',history);
netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'description',desc);
netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'GMT_version',vers);
% X
dimid = netcdf.defDim(ncid,'x',length(x));
varid = netcdf.defVar(ncid,'x','double',dimid);
netcdf.putAtt(ncid,varid,'long_name','x');
netcdf.putAtt(ncid,varid,'actual_range',[min(x) max(x)]);
netcdf.endDef(ncid);
netcdf.putVar(ncid,varid,x);
% Y
netcdf.reDef(ncid);
dimid = netcdf.defDim(ncid,'y',length(y));
varid = netcdf.defVar(ncid,'y','double',dimid);
netcdf.putAtt(ncid,varid,'long_name','y');
netcdf.putAtt(ncid,varid,'actual_range',[min(y) max(y)]);
netcdf.endDef(ncid);
netcdf.putVar(ncid,varid,y);
% Z
netcdf.reDef(ncid);
varid = netcdf.defVar(ncid,'z',nctype,[0 1]);
netcdf.putAtt(ncid,varid,'long_name','z');
netcdf.putAtt(ncid,varid,'_FillValue',nanfill);
netcdf.putAtt(ncid,varid,'actual_range',[min(z(:)) max(z(:))]);
netcdf.endDef(ncid);
netcdf.putVar(ncid,varid,z');
% close file
netcdf.close(ncid);
end