source: LMDZ6/branches/Amaury_dev/libf/misc/write_field.F90 @ 5113

Last change on this file since 5113 was 5113, checked in by abarral, 4 months ago

Rename modules in misc from *_mod > lmdz_*
Put cbrt.f90, ch*.f90, pch*.f90 in new lmdz_libmath_pch.f90

  • 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.6 KB
RevLine 
[631]1module write_field
[5088]2  USE netcdf, ONLY: nf90_sync, nf90_put_var, nf90_enddef, nf90_def_dim, nf90_unlimited, &
[5103]3          nf90_clobber, nf90_create, nf90_def_var
[5090]4  USE lmdz_cppkeys_wrapper, ONLY: nf90_format
[5103]5  USE strings_mod, ONLY: int2str
[631]6
[5103]7  IMPLICIT NONE; PRIVATE
8  PUBLIC WriteField
[5075]9
[631]10  integer, parameter :: MaxWriteField = 100
[5103]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
[631]18  interface WriteField
[5103]19    module procedure WriteField3d, WriteField2d, WriteField1d
[761]20  end interface WriteField
[5103]21contains
22
23  function GetFieldIndex(name)
[5113]24    IMPLICIT NONE
[5103]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)
[5113]43    IMPLICIT NONE
[5103]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)
[5113]54    IMPLICIT NONE
[5103]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)
[5113]65    IMPLICIT NONE
[5103]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)
[5113]76    IMPLICIT NONE
[5103]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)
[631]85
[5103]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
[631]93
[5103]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)
[5113]110    IMPLICIT NONE
[5103]111    character(len = *) :: name
112    integer :: dimx, dimy, dimz
113    integer :: TabDim(4)
114    integer :: status
[631]115
[5103]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)
[5113]131    IMPLICIT NONE
[5103]132
133    integer, parameter :: MaxDim = 1
134    character(len = *) :: name
[631]135    real, dimension(:) :: Field
[5103]136    real, dimension(:), allocatable :: New_Field
137    character(len = 20) :: str
[631]138    integer, dimension(MaxDim) :: Dim
[5103]139    integer :: i, nb
140    integer, parameter :: id = 10
141    integer, parameter :: NbCol = 4
142    integer :: ColumnSize
[631]143    integer :: pos
[5103]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
[631]160      else
[5103]161        form = '(t' // trim(int2str(pos)) // ',i' // trim(MaxLen) // '," ---> ",g22.16," | ",)'
162        Pos = Pos + ColumnSize
[631]163      endif
[5103]164      write (id, form, advance = 'no') i, Field(i)
[631]165    enddo
[5103]166
[631]167    close(id)
168
169  end subroutine write_field1D
170
[5103]171  subroutine write_field2D(name, Field)
[5113]172    IMPLICIT NONE
[5103]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
[631]179    integer, dimension(MaxDim) :: Dim
[5103]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
[631]187
[5103]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
[631]210        else
[5103]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
[631]217        endif
[5103]218        write (id, form, advance = 'no') Field(j, i)
[631]219      enddo
[5103]220      if (MOD(nb, NbCol)==0) then
221        write (id, spacing)
[631]222      else
[5103]223        write (id, '("")')
224        write (id, spacing)
[631]225      endif
226    enddo
[5103]227
[631]228  end subroutine write_field2D
[5103]229
230  subroutine write_field3D(name, Field)
[5113]231    IMPLICIT NONE
[5103]232
233    integer, parameter :: MaxDim = 3
234    character(len = *) :: name
235    real, dimension(:, :, :) :: Field
236    real, dimension(:, :, :), allocatable :: New_Field
[631]237    integer, dimension(MaxDim) :: Dim
[5103]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
[631]245
[5103]246    open(unit = id, file = name // '.field', form = 'formatted', status = 'replace')
247    write (id, '("----- Field ' // name // '"//)')
[631]248
[5103]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
[631]274          else
[5103]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
[631]283          endif
[5103]284          write (id, form, advance = 'no') Field(k, j, i)
[631]285        enddo
[5103]286        if (MOD(nb, NbCol)==0) then
287          write (id, spacing)
[631]288        else
[5103]289          write (id, '("")')
290          write (id, spacing)
[631]291        endif
292      enddo
[5103]293      write (id, spacing)
[631]294    enddo
[5103]295
[631]296    close(id)
297
[5103]298  end subroutine write_field3D
299
[631]300end module write_field
301 
Note: See TracBrowser for help on using the repository browser.