-
Notifications
You must be signed in to change notification settings - Fork 0
/
nc_putAttr.F90
233 lines (187 loc) · 8.39 KB
/
nc_putAttr.F90
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
!--------------------------------------------------------------------------------------------
subroutine nc_putAttr(ncid, nd, dimids, xtype, var_name, &
long_name, units, coordinates, missing_real)
use netcdf
implicit none
integer, intent(in) :: ncid, nd, xtype
integer, dimension(6), intent(in) :: dimids
character(len=*), intent(in) :: var_name, long_name, units, coordinates
real, intent(in) :: missing_real
!--Variable id
integer :: varid, md
!--Return rc
integer :: rc
md = nd
if(md > 6) then
write(unit=0, fmt='(a, i6)') "We can only handle data up to 5d. but here nd = ", nd
endif
rc = nf90_def_var(ncid, trim(var_name), xtype, dimids(1:md), varid)
if(rc /= nf90_noerr) then
write(unit=0, fmt='(a, i6)') "ncid: ", ncid
write(unit=0, fmt='(a, 6i6)') "dimids: ", dimids(1:md)
write(unit=0, fmt='(3a)') "Problem to def varid for: <", trim(var_name), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "long_name", trim(long_name))
if(rc /= nf90_noerr) then
write(unit=0, fmt='(3a)') "Problem to write attribute long_name: <", trim(long_name), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "units", trim(units))
if(rc /= nf90_noerr) then
write(unit=0, fmt='(3a)') "Problem to write attribute units: <", trim(units), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "_CoordinateAxes", trim(coordinates))
if(rc /= nf90_noerr) then
write(unit=0, fmt='(3a)') "Problem to write attribute coordinates: <", trim(coordinates), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "_FillValue", missing_real)
if(rc /= nf90_noerr) then
write(unit=0, fmt='(a, f12.2)') "Problem to write attribute missing_real: ", missing_real
write(unit=0, fmt='(3a)') "Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
end subroutine nc_putAttr
!--------------------------------------------------------------------------------------------
subroutine nc_putAttrInt(ncid, nd, dimids, xtype, var_name, &
long_name, units, coordinates, missing_int)
use netcdf
implicit none
integer, intent(in) :: ncid, nd, xtype
integer, dimension(6), intent(in) :: dimids
character(len=*), intent(in) :: var_name, long_name, units, coordinates
integer, intent(in) :: missing_int
!--Variable id
integer :: varid, md
!--Return rc
integer :: rc
md = nd
if(md > 6) then
write(unit=0, fmt='(a, i6)') "We can only handle data up to 5d. but here nd = ", nd
endif
!--Always set the extra dimension unlimited.
! dimids(nd+1) = nf90_unlimited
rc = nf90_def_var(ncid, trim(var_name), xtype, dimids(1:md), varid)
if(rc /= nf90_noerr) then
write(unit=0, fmt='(a, i6)') "ncid: ", ncid
write(unit=0, fmt='(a, 6i6)') "dimids: ", dimids(1:md)
write(unit=0, fmt='(3a)') "Problem to def varid for: <", trim(var_name), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "long_name", trim(long_name))
if(rc /= nf90_noerr) then
write(unit=0, fmt='(3a)') "Problem to write attribute long_name: <", trim(long_name), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "units", trim(units))
if(rc /= nf90_noerr) then
write(unit=0, fmt='(3a)') "Problem to write attribute units: <", trim(units), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "_CoordinateAxes", trim(coordinates))
if(rc /= nf90_noerr) then
write(unit=0, fmt='(3a)') "Problem to write attribute coordinates: <", trim(coordinates), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "_FillValue", missing_int)
if(rc /= nf90_noerr) then
write(unit=0, fmt='(a, f12.2)') "Problem to write attribute missing_int: ", missing_int
write(unit=0, fmt='(3a)') "Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
end subroutine nc_putAttrInt
!--------------------------------------------------------------------------------------------
subroutine nc_putAttrWithChunking(ncid, nd, dimids, chunksizes, xtype, var_name, &
long_name, units, coordinates, missing_real)
use netcdf
implicit none
integer, intent(in) :: ncid, nd, xtype
integer, dimension(6), intent(in) :: dimids, chunksizes
character(len=*), intent(in) :: var_name, long_name, units, coordinates
real, intent(in) :: missing_real
!--Variable id
integer :: varid, md
integer, parameter :: deflate_level = 2
!--Return rc
integer :: rc
md = nd
if(md > 6) then
write(unit=0, fmt='(a, i6)') "We can only handle data up to 5d. but here nd = ", nd
endif
!rc = nf90_def_var(ncid, trim(var_name), xtype, dimids(1:md), varid)
! fletcher32 = .true., endianness = nf90_endian_big,
rc = nf90_def_var(ncid, trim(var_name), xtype, dimids(1:md), varid, &
chunksizes = chunksizes(1:md), shuffle = .true., &
fletcher32 = .true., deflate_level = deflate_level)
if(rc /= nf90_noerr) then
write(unit=0, fmt='(a, i6)') "ncid: ", ncid
write(unit=0, fmt='(a, 6i6)') "dimids: ", dimids(1:md)
write(unit=0, fmt='(3a)') "Problem to def varid for: <", trim(var_name), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "long_name", trim(long_name))
if(rc /= nf90_noerr) then
write(unit=0, fmt='(3a)') "Problem to write attribute long_name: <", trim(long_name), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "units", trim(units))
if(rc /= nf90_noerr) then
write(unit=0, fmt='(3a)') "Problem to write attribute units: <", trim(units), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "_CoordinateAxes", trim(coordinates))
if(rc /= nf90_noerr) then
write(unit=0, fmt='(3a)') "Problem to write attribute coordinates: <", trim(coordinates), ">.", &
"Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
rc = nf90_put_att(ncid, varid, "_FillValue", missing_real)
if(rc /= nf90_noerr) then
write(unit=0, fmt='(a, f12.2)') "Problem to write attribute missing_real: ", missing_real
write(unit=0, fmt='(3a)') "Error rc: ", trim(nf90_strerror(rc))
write(unit=0, fmt='(3a, i4)') &
"Stop in file: <", __FILE__, ">, line: ", __LINE__
stop
endif
end subroutine nc_putAttrWithChunking