-
Notifications
You must be signed in to change notification settings - Fork 2
/
esmf_write_grid.m
153 lines (127 loc) · 4.32 KB
/
esmf_write_grid.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
%==========================================================================
% matFVCOM package
% write grid nc for ESMF regrid
%
% input :
%
% output :
%
% Siqi Li, SMAST
% 2023-02-23
%
% Updates:
%
%==========================================================================
function esmf_write_grid(fout, grid_type, varargin)
switch(upper(grid_type))
case 'FVCOM'
lon = varargin{1};
lat = varargin{2};
nv = varargin{3};
write_ugrid_fvcom(fout, lon, lat, nv);
case 'WRF'
lon = varargin{1};
lat = varargin{2};
write_esmf_wrf(fout, lon, lat);
% if length(varargin) > 2
% mask = varargin{3};
% else
% mask = ones(size(lon)-1);
% end
% write_esmf_rectangular(fout, lon, lat, mask);
otherwise
end
end
function write_ugrid_fvcom(fout, lon, lat, nv)
node=length(lon);
nele=length(nv);
% Create output NetCDF
ncid = netcdf.create(fout, 'CLOBBER');
netcdf.putAtt(ncid, netcdf.getConstant('GLOBAL'), 'type', 'UGRID');
% Dimension
dimid_node = netcdf.defDim(ncid, 'node', node);
dimid_nele = netcdf.defDim(ncid, 'nele', nele);
dimid_three = netcdf.defDim(ncid, 'three', 3);
% Variable
% fvcom_mesh
varid_mesh = netcdf.defVar(ncid, 'fvcom_mesh', 'int', []);
netcdf.putAtt(ncid, varid_mesh, 'cf_role', 'mesh_topology');
netcdf.putAtt(ncid, varid_mesh, 'topology_dimension', 2.);
netcdf.putAtt(ncid, varid_mesh, 'node_coordinates', 'lon lat');
netcdf.putAtt(ncid, varid_mesh, 'face_node_connectivity', 'nv');
% nv
varid_nv = netcdf.defVar(ncid, 'nv', 'int', [dimid_three dimid_nele]);
netcdf.putAtt(ncid, varid_nv, 'standard_name', 'face_node_connectivity');
netcdf.putAtt(ncid, varid_nv, 'start_index', 1.);
% lon
varid_lon = netcdf.defVar(ncid, 'lon', 'float', dimid_node);
netcdf.putAtt(ncid, varid_lon, 'standard_name', 'longitude');
netcdf.putAtt(ncid, varid_lon, 'units', 'degrees_east');
% lat
varid_lat = netcdf.defVar(ncid, 'lat', 'float', dimid_node);
netcdf.putAtt(ncid, varid_lat, 'standard_name', 'latitude');
netcdf.putAtt(ncid, varid_lat, 'units', 'degrees_north');
% End define mode
netcdf.endDef(ncid);
% Write data
netcdf.putVar(ncid, varid_nv, nv');
netcdf.putVar(ncid, varid_lat, lat);
netcdf.putVar(ncid, varid_lon, lon);
% Close output NetCDF
netcdf.close(ncid);
end
function write_esmf_wrf(fout, lon, lat)
% function write_esmf_rectangular(fout, lon, lat, mask)
[nlat, nlon] = size(lon);
node = nlon*nlat;
nele = (nlon-1) * (nlat-1);
lon_c = reshape(lon, node, 1);
lat_c = reshape(lat, node, 1);
% mask_c = reshape(mask, nele, 1);
nv = zeros(nele, 4);
for ele = 1 : nele
%
jele = mod(ele, nlat-1);
if (jele==0)
jele = nlat - 1;
end
iele = (ele-jele)/(nlat-1) + 1;
%
nv(ele,1) = (iele-1) *nlat + jele;
nv(ele,2) = (iele+1-1)*nlat + jele;
nv(ele,3) = (iele+1-1)*nlat + jele + 1;
nv(ele,4) = (iele-1) *nlat + jele + 1;
end
% Create the output NetCDF
ncid = netcdf.create(fout, 'CLOBBER');
netcdf.putAtt(ncid, netcdf.getConstant('GLOBAL'), 'type', 'ESMF');
% Dimension
dimid_node = netcdf.defDim(ncid, 'nodeCount', node);
dimid_nele = netcdf.defDim(ncid, 'elementCount', nele);
dimid_four = netcdf.defDim(ncid, 'maxNodePElement', 4);
dimid_coordDim = netcdf.defDim(ncid, 'coordDim', 2);
% Variable
% nodeCoords
varid_nodeCoords = netcdf.defVar(ncid, 'nodeCoords', 'double', [dimid_coordDim dimid_node]);
netcdf.putAtt(ncid, varid_nodeCoords, 'units', 'degrees');
% elementConn
varid_elementConn = netcdf.defVar(ncid, 'elementConn', 'int', [dimid_four dimid_nele]);
netcdf.putAtt(ncid, varid_elementConn, 'long_name', 'Node indices that define the element connectivity');
netcdf.putAtt(ncid, varid_elementConn, 'FillValue', -1.);
% netcdf.defVarFill(ncid,varid_elementConn,false,-1.);
% numElementConn
varid_numElementConn=netcdf.defVar(ncid,'numElementConn','byte',dimid_nele);
netcdf.putAtt(ncid,varid_numElementConn,'long_name','Number of nodes per element');
% % elementMask
% varid_elementMask=netcdf.defVar(ncid,'elementMask','int',dimid_nele);
% netcdf.putAtt(ncid,varid_elementMask,'FillValue',-9999.);
% End define mode
netcdf.endDef(ncid);
% Write data
netcdf.putVar(ncid, varid_nodeCoords, [lon_c lat_c]');
netcdf.putVar(ncid, varid_elementConn, nv');
netcdf.putVar(ncid, varid_numElementConn, ones(1,nele)*4);
% netcdf.putVar(ncid, varid_elementMask, mask_c);
% Close the file
netcdf.close(ncid);
end