source: LMDZ6/branches/Amaury_dev/libf/misc/lmdz_write_field.f90 @ 5419

Last change on this file since 5419 was 5158, checked in by abarral, 5 months ago

Add missing klon on strataer_emiss_mod.F90
Correct various missing explicit declarations
Replace tabs by spaces (tabs are not part of the fortran charset)
Continue cleaning modules
Removed unused arguments and variables

  • Property copyright set to
    Name of program: LMDZ
    Creation date: 1984
    Version: LMDZ5
    License: CeCILL version 2
    Holder: Laboratoire de m\'et\'eorologie dynamique, CNRS, UMR 8539
    See the license file in the root directory
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 KB
Line 
1module lmdz_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 lmdz_strings, 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
21CONTAINS
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
300END MODULE lmdz_write_field
301 
Note: See TracBrowser for help on using the repository browser.