1 | module write_field |
---|
2 | USE netcdf, ONLY: nf90_sync, nf90_put_var, nf90_enddef, nf90_def_dim, nf90_unlimited, & |
---|
3 | nf90_clobber, nf90_create, nf90_def_var |
---|
4 | USE lmdz_cppkeys_wrapper, ONLY: nf90_format |
---|
5 | USE strings_mod, ONLY: int2str |
---|
6 | |
---|
7 | IMPLICIT NONE; PRIVATE |
---|
8 | PUBLIC WriteField |
---|
9 | |
---|
10 | integer, parameter :: MaxWriteField = 100 |
---|
11 | integer, dimension(MaxWriteField), save :: FieldId |
---|
12 | integer, dimension(MaxWriteField), save :: FieldVarId |
---|
13 | integer, dimension(MaxWriteField), save :: FieldIndex |
---|
14 | character(len = 255), dimension(MaxWriteField) :: FieldName |
---|
15 | |
---|
16 | integer, save :: NbField = 0 |
---|
17 | |
---|
18 | interface WriteField |
---|
19 | module procedure WriteField3d, WriteField2d, WriteField1d |
---|
20 | end interface WriteField |
---|
21 | contains |
---|
22 | |
---|
23 | function GetFieldIndex(name) |
---|
24 | implicit none |
---|
25 | integer :: GetFieldindex |
---|
26 | character(len = *) :: name |
---|
27 | |
---|
28 | character(len = 255) :: TrueName |
---|
29 | integer :: i |
---|
30 | |
---|
31 | TrueName = TRIM(ADJUSTL(name)) |
---|
32 | |
---|
33 | GetFieldIndex = -1 |
---|
34 | do i = 1, NbField |
---|
35 | if (TrueName==FieldName(i)) then |
---|
36 | GetFieldIndex = i |
---|
37 | exit |
---|
38 | endif |
---|
39 | enddo |
---|
40 | end function GetFieldIndex |
---|
41 | |
---|
42 | subroutine WriteField3d(name, Field) |
---|
43 | implicit none |
---|
44 | character(len = *) :: name |
---|
45 | real, dimension(:, :, :) :: Field |
---|
46 | integer, dimension(3) :: Dim |
---|
47 | |
---|
48 | Dim = shape(Field) |
---|
49 | CALL WriteField_gen(name, Field, Dim(1), Dim(2), Dim(3)) |
---|
50 | |
---|
51 | end subroutine WriteField3d |
---|
52 | |
---|
53 | subroutine WriteField2d(name, Field) |
---|
54 | implicit none |
---|
55 | character(len = *) :: name |
---|
56 | real, dimension(:, :) :: Field |
---|
57 | integer, dimension(2) :: Dim |
---|
58 | |
---|
59 | Dim = shape(Field) |
---|
60 | CALL WriteField_gen(name, Field, Dim(1), Dim(2), 1) |
---|
61 | |
---|
62 | end subroutine WriteField2d |
---|
63 | |
---|
64 | subroutine WriteField1d(name, Field) |
---|
65 | implicit none |
---|
66 | character(len = *) :: name |
---|
67 | real, dimension(:) :: Field |
---|
68 | integer, dimension(1) :: Dim |
---|
69 | |
---|
70 | Dim = shape(Field) |
---|
71 | CALL WriteField_gen(name, Field, Dim(1), 1, 1) |
---|
72 | |
---|
73 | end subroutine WriteField1d |
---|
74 | |
---|
75 | subroutine WriteField_gen(name, Field, dimx, dimy, dimz) |
---|
76 | implicit none |
---|
77 | character(len = *) :: name |
---|
78 | integer :: dimx, dimy, dimz |
---|
79 | real, dimension(dimx, dimy, dimz) :: Field |
---|
80 | integer, dimension(dimx * dimy * dimz) :: ndex |
---|
81 | integer :: status |
---|
82 | integer :: index |
---|
83 | integer :: start(4) |
---|
84 | integer :: count(4) |
---|
85 | |
---|
86 | Index = GetFieldIndex(name) |
---|
87 | if (Index==-1) then |
---|
88 | CALL CreateNewField(name, dimx, dimy, dimz) |
---|
89 | Index = GetFieldIndex(name) |
---|
90 | else |
---|
91 | FieldIndex(Index) = FieldIndex(Index) + 1. |
---|
92 | endif |
---|
93 | |
---|
94 | start(1) = 1 |
---|
95 | start(2) = 1 |
---|
96 | start(3) = 1 |
---|
97 | start(4) = FieldIndex(Index) |
---|
98 | |
---|
99 | count(1) = dimx |
---|
100 | count(2) = dimy |
---|
101 | count(3) = dimz |
---|
102 | count(4) = 1 |
---|
103 | |
---|
104 | status = nf90_put_var(FieldId(Index), FieldVarId(Index), Field, start, count) |
---|
105 | status = nf90_sync(FieldId(Index)) |
---|
106 | |
---|
107 | end subroutine WriteField_gen |
---|
108 | |
---|
109 | subroutine CreateNewField(name, dimx, dimy, dimz) |
---|
110 | implicit none |
---|
111 | character(len = *) :: name |
---|
112 | integer :: dimx, dimy, dimz |
---|
113 | integer :: TabDim(4) |
---|
114 | integer :: status |
---|
115 | |
---|
116 | NbField = NbField + 1 |
---|
117 | FieldName(NbField) = TRIM(ADJUSTL(name)) |
---|
118 | FieldIndex(NbField) = 1 |
---|
119 | |
---|
120 | status = nf90_create(TRIM(ADJUSTL(name)) // '.nc', nf90_clobber, FieldId(NbField)) |
---|
121 | status = nf90_def_dim(FieldId(NbField), 'X', dimx, TabDim(1)) |
---|
122 | status = nf90_def_dim(FieldId(NbField), 'Y', dimy, TabDim(2)) |
---|
123 | status = nf90_def_dim(FieldId(NbField), 'Z', dimz, TabDim(3)) |
---|
124 | status = nf90_def_dim(FieldId(NbField), 'iter', nf90_unlimited, TabDim(4)) |
---|
125 | status = nf90_def_var(FieldId(NbField), FieldName(NbField), nf90_format, TabDim, FieldVarId(NbField)) |
---|
126 | status = nf90_enddef(FieldId(NbField)) |
---|
127 | |
---|
128 | end subroutine CreateNewField |
---|
129 | |
---|
130 | subroutine write_field1D(name, Field) |
---|
131 | implicit none |
---|
132 | |
---|
133 | integer, parameter :: MaxDim = 1 |
---|
134 | character(len = *) :: name |
---|
135 | real, dimension(:) :: Field |
---|
136 | real, dimension(:), allocatable :: New_Field |
---|
137 | character(len = 20) :: str |
---|
138 | integer, dimension(MaxDim) :: Dim |
---|
139 | integer :: i, nb |
---|
140 | integer, parameter :: id = 10 |
---|
141 | integer, parameter :: NbCol = 4 |
---|
142 | integer :: ColumnSize |
---|
143 | integer :: pos |
---|
144 | character(len = 255) :: form |
---|
145 | character(len = 255) :: MaxLen |
---|
146 | |
---|
147 | open(unit = id, file = name // '.field', form = 'formatted', status = 'replace') |
---|
148 | write (id, '("----- Field ' // name // '",//)') |
---|
149 | Dim = shape(Field) |
---|
150 | MaxLen = int2str(len(trim(int2str(Dim(1))))) |
---|
151 | ColumnSize = 20 + 6 + 3 + len(trim(int2str(Dim(1)))) |
---|
152 | Nb = 0 |
---|
153 | Pos = 2 |
---|
154 | do i = 1, Dim(1) |
---|
155 | nb = nb + 1 |
---|
156 | |
---|
157 | if (MOD(nb, NbCol)==0) then |
---|
158 | form = '(t' // trim(int2str(pos)) // ',i' // trim(MaxLen) // '," ---> ",g22.16,/)' |
---|
159 | Pos = 2 |
---|
160 | else |
---|
161 | form = '(t' // trim(int2str(pos)) // ',i' // trim(MaxLen) // '," ---> ",g22.16," | ",)' |
---|
162 | Pos = Pos + ColumnSize |
---|
163 | endif |
---|
164 | write (id, form, advance = 'no') i, Field(i) |
---|
165 | enddo |
---|
166 | |
---|
167 | close(id) |
---|
168 | |
---|
169 | end subroutine write_field1D |
---|
170 | |
---|
171 | subroutine write_field2D(name, Field) |
---|
172 | implicit none |
---|
173 | |
---|
174 | integer, parameter :: MaxDim = 2 |
---|
175 | character(len = *) :: name |
---|
176 | real, dimension(:, :) :: Field |
---|
177 | real, dimension(:, :), allocatable :: New_Field |
---|
178 | character(len = 20) :: str |
---|
179 | integer, dimension(MaxDim) :: Dim |
---|
180 | integer :: i, j, nb |
---|
181 | integer, parameter :: id = 10 |
---|
182 | integer, parameter :: NbCol = 4 |
---|
183 | integer :: ColumnSize |
---|
184 | integer :: pos, offset |
---|
185 | character(len = 255) :: form |
---|
186 | character(len = 255) :: spacing |
---|
187 | |
---|
188 | open(unit = id, file = name // '.field', form = 'formatted', status = 'replace') |
---|
189 | write (id, '("----- Field ' // name // '",//)') |
---|
190 | |
---|
191 | Dim = shape(Field) |
---|
192 | offset = len(trim(int2str(Dim(1)))) + len(trim(int2str(Dim(2)))) + 3 |
---|
193 | ColumnSize = 20 + 6 + 3 + offset |
---|
194 | |
---|
195 | spacing = '(t2,"' // repeat('-', ColumnSize * NbCol) // '")' |
---|
196 | |
---|
197 | do i = 1, Dim(2) |
---|
198 | nb = 0 |
---|
199 | Pos = 2 |
---|
200 | do j = 1, Dim(1) |
---|
201 | nb = nb + 1 |
---|
202 | |
---|
203 | if (MOD(nb, NbCol)==0) then |
---|
204 | form = '(t' // trim(int2str(pos)) // & |
---|
205 | ',"(' // trim(int2str(j)) // ',' & |
---|
206 | // trim(int2str(i)) // ')",t' & |
---|
207 | // trim(int2str(pos + offset)) & |
---|
208 | // '," ---> ",g22.16,/)' |
---|
209 | Pos = 2 |
---|
210 | else |
---|
211 | form = '(t' // trim(int2str(pos)) // & |
---|
212 | ',"(' // trim(int2str(j)) // ',' & |
---|
213 | // trim(int2str(i)) // ')",t' & |
---|
214 | // trim(int2str(pos + offset)) & |
---|
215 | // '," ---> ",g22.16," | ")' |
---|
216 | Pos = Pos + ColumnSize |
---|
217 | endif |
---|
218 | write (id, form, advance = 'no') Field(j, i) |
---|
219 | enddo |
---|
220 | if (MOD(nb, NbCol)==0) then |
---|
221 | write (id, spacing) |
---|
222 | else |
---|
223 | write (id, '("")') |
---|
224 | write (id, spacing) |
---|
225 | endif |
---|
226 | enddo |
---|
227 | |
---|
228 | end subroutine write_field2D |
---|
229 | |
---|
230 | subroutine write_field3D(name, Field) |
---|
231 | implicit none |
---|
232 | |
---|
233 | integer, parameter :: MaxDim = 3 |
---|
234 | character(len = *) :: name |
---|
235 | real, dimension(:, :, :) :: Field |
---|
236 | real, dimension(:, :, :), allocatable :: New_Field |
---|
237 | integer, dimension(MaxDim) :: Dim |
---|
238 | integer :: i, j, k, nb |
---|
239 | integer, parameter :: id = 10 |
---|
240 | integer, parameter :: NbCol = 4 |
---|
241 | integer :: ColumnSize |
---|
242 | integer :: pos, offset |
---|
243 | character(len = 255) :: form |
---|
244 | character(len = 255) :: spacing |
---|
245 | |
---|
246 | open(unit = id, file = name // '.field', form = 'formatted', status = 'replace') |
---|
247 | write (id, '("----- Field ' // name // '"//)') |
---|
248 | |
---|
249 | Dim = shape(Field) |
---|
250 | offset = len(trim(int2str(Dim(1)))) + len(trim(int2str(Dim(2)))) + len(trim(int2str(Dim(3)))) + 4 |
---|
251 | ColumnSize = 22 + 6 + 3 + offset |
---|
252 | |
---|
253 | ! open(unit=id,file=name,form=formatted |
---|
254 | |
---|
255 | spacing = '(t2,"' // repeat('-', ColumnSize * NbCol) // '")' |
---|
256 | |
---|
257 | do i = 1, Dim(3) |
---|
258 | |
---|
259 | do j = 1, Dim(2) |
---|
260 | nb = 0 |
---|
261 | Pos = 2 |
---|
262 | |
---|
263 | do k = 1, Dim(1) |
---|
264 | nb = nb + 1 |
---|
265 | |
---|
266 | if (MOD(nb, NbCol)==0) then |
---|
267 | form = '(t' // trim(int2str(pos)) // & |
---|
268 | ',"(' // trim(int2str(k)) // ',' & |
---|
269 | // trim(int2str(j)) // ',' & |
---|
270 | // trim(int2str(i)) // ')",t' & |
---|
271 | // trim(int2str(pos + offset)) & |
---|
272 | // '," ---> ",g22.16,/)' |
---|
273 | Pos = 2 |
---|
274 | else |
---|
275 | form = '(t' // trim(int2str(pos)) // & |
---|
276 | ',"(' // trim(int2str(k)) // ',' & |
---|
277 | // trim(int2str(j)) // ',' & |
---|
278 | // trim(int2str(i)) // ')",t' & |
---|
279 | // trim(int2str(pos + offset)) & |
---|
280 | // '," ---> ",g22.16," | ")' |
---|
281 | ! dépend de l'implémention, sur compaq, c'est necessaire |
---|
282 | ! Pos=Pos+ColumnSize |
---|
283 | endif |
---|
284 | write (id, form, advance = 'no') Field(k, j, i) |
---|
285 | enddo |
---|
286 | if (MOD(nb, NbCol)==0) then |
---|
287 | write (id, spacing) |
---|
288 | else |
---|
289 | write (id, '("")') |
---|
290 | write (id, spacing) |
---|
291 | endif |
---|
292 | enddo |
---|
293 | write (id, spacing) |
---|
294 | enddo |
---|
295 | |
---|
296 | close(id) |
---|
297 | |
---|
298 | end subroutine write_field3D |
---|
299 | |
---|
300 | end module write_field |
---|
301 | |
---|