source: branches/GRISLIv3/SOURCES/readinput.f90 @ 474

Last change on this file since 474 was 449, checked in by aquiquet, 9 months ago

Cleaning branch: use only statement for netcdf modules

File size: 7.3 KB
Line 
1
2!> subroutine dat2netcdf
3!! convertion of a ZBL file to an netcdf one
4!! @param  nxx                dimension along x
5!! @param  nyy                dimension along y
6!! @param  filename           
7!! @return Tab                the value of Tab(nxx,nyy): Warning it is a real
8
9subroutine dat2netcdf(nxx,nyy,numcol,base_name,filename,fil_sortie)
10
11  use netcdf, only: nf90_create,nf90_write,nf90_close
12  use io_netcdf_grisli,only: write_ncdf_var
13
14  implicit none
15  integer,intent(in)  :: nxx                    !< dimension along x
16  integer,intent(in)  :: nyy                    !< dimension along y
17  integer,intent(in)  :: numcol                 !< column number
18  character(len=*), intent(in) :: base_name     !< base name of the variable to write
19  character(len=*),intent(in) :: filename       !< name of the file
20  character(len=*),intent(in) :: fil_sortie     !< output filename
21
22  ! local variables
23  character(len=20) :: base_name1               !< base name of the variable to write
24  real*8,dimension(:,:),pointer :: Tab1,Tab2,Tab3    !< the array that is read and returned
25  integer  :: i,j                !< working integers
26  integer  :: lx,ly              !< nxx, nyy read in the file
27  real,dimension(3) :: a         !< working tab
28  logical,save :: init
29  integer ncid1,status1
30  character(len=20),dimension(2) :: dimnames2d        !< dimensions pour netcdf pour tableau 2d
31
32  if ((index(filename,'no') .eq. 0)) then
33
34     if (.not. init) then
35        status1  = nf90_create(trim(fil_sortie),NF90_WRITE,ncid1)    ! ouverture du fichier
36        status1  = nf90_close(ncid1)                                 ! fermeture
37        call init_ncdf(nxx,nyy,fil_sortie,dimnames2d)
38        init= .true.
39     end if
40
41
42     open(22,file=trim(filename))
43     read(22,*) lx,ly
44
45     if ((lx.ne.nxx).or.(ly.ne.nyy)) then
46        write(6,*) 'wrong dimension of the file', filename
47        write(6,*) 'in the file',lx,ly
48        write(6,*) 'should be',nxx,nyy
49        stop
50     end if
51
52     if (numcol .eq. 1) then
53        allocate(Tab1(nxx,nyy))
54        do j=1,nyy
55           do i=1,nxx
56              read(22,*) a(1)
57              Tab1(i,j)=a(1)
58           end do
59        end do
60        base_name1= trim(base_name)//'_1'
61        call Write_Ncdf_var(base_name1,dimnames2d,trim(fil_sortie),Tab1,'double')
62        deallocate(Tab1)
63     endif
64
65     if (numcol .eq. 2) then
66        allocate(Tab1(nxx,nyy),Tab2(nxx,nyy))
67        do j=1,nyy
68           do i=1,nxx
69              read(22,*) a(1),a(2)
70              Tab1(i,j)=a(1)
71              Tab2(i,j)=a(2)
72           end do
73        end do
74        base_name1= trim(base_name)//'_1'
75        call Write_Ncdf_var(base_name1,dimnames2d,trim(fil_sortie),Tab1,'double')
76        base_name1=trim(base_name)//'_2'
77        call Write_Ncdf_var(base_name1,dimnames2d,trim(fil_sortie),Tab2,'double')
78        deallocate(Tab1,Tab2)
79     endif
80
81     if (numcol .eq. 3) then
82        allocate(Tab1(nxx,nyy),Tab2(nxx,nyy),Tab3(nxx,nyy))
83        do j=1,nyy
84           do i=1,nxx
85              read(22,*) a(1),a(2),a(3)
86              Tab1(i,j)=a(1)
87              Tab2(i,j)=a(2)
88              Tab3(i,j)=a(3)
89           end do
90        end do
91        base_name1= trim(base_name)//'_1'
92        call Write_Ncdf_var(base_name1,dimnames2d,trim(fil_sortie),Tab1,'double')
93        base_name1=trim(base_name)//'_2'
94        call Write_Ncdf_var(base_name1,dimnames2d,trim(fil_sortie),Tab2,'double')
95        base_name1=trim(base_name)//'_3'
96        call Write_Ncdf_var(base_name1,dimnames2d,trim(fil_sortie),Tab3,'double')
97        deallocate(Tab1,Tab2,Tab3)
98     endif
99
100     close(22)
101  end if
102
103end subroutine dat2netcdf
104
105!> subroutine init_ncdf
106!! Initialise the netcdf file
107!! @param nxx                dimension along x
108!! @param nyy                dimension along y
109!! @param fil_sortie         name of the file to initialise
110!! @ param dimnames2d        dimensions for netcdf
111
112subroutine init_ncdf(nxx,nyy,fil_sortie,dimnames2d)
113  use io_netcdf_grisli, only: write_ncdf_dim
114  implicit none
115  integer,intent(in)  :: nxx                    !< dimension along x
116  integer,intent(in)  :: nyy                    !< dimension along y
117  character(len=*),intent(in)::fil_sortie
118  character(len=20),dimension(2),intent(out) :: dimnames2d  !< dimensions pour netcdf pour tableau 2d
119  ! initialisation
120  call write_ncdf_dim('x',trim(fil_sortie),nxx)               ! dimensions des tableaux
121  call write_ncdf_dim('y',trim(fil_sortie),nyy)
122  ! ecriture d'un tableau tab 2D
123  dimnames2d(1)='x'
124  dimnames2d(2)='y'
125end subroutine init_ncdf
126
127
128!> subroutine lect_ncfile
129!! Read the nc file given
130!! @param varname              The name of the variable to read
131!! @param  filename            File to read from
132!! @return Tab                 The value of Tab: Warning it is a real
133
134subroutine lect_ncfile(varname,Tab,filename)
135  use io_netcdf_grisli, only: read_ncdf_var
136  implicit none
137
138  character(*),intent(in) :: varname          !< Var name to read
139  real,dimension(:,:),intent(inout) :: Tab    !< Array to read in
140  character(*),intent(in) :: filename         !< File to read from
141  real*8, dimension(:,:), pointer :: tabvar
142
143  if(.not. associated(tabvar)) then
144     allocate(tabvar(size(Tab,1),size(Tab,2))) 
145  else  ! to nullify the pointer if undefined
146     if (any(shape(tabvar).ne.(/size(Tab,1),size(Tab,2)/)) ) then
147        tabvar => null()
148     end if
149  end if
150
151  call read_ncdf_var(varname,filename,tabvar)
152  Tab(:,:)= tabvar(:,:)
153  !write(6,*) filename,varname,Tab(154,123)
154end subroutine lect_ncfile
155
156
157!> subroutine lect_input
158!! Read the file given
159!! @param numcol               Column number
160!! @param basename             Base name of the variable to read
161!! @param col                  The column to read
162!! @param  filename            File to read from
163!! @param ncfileout            File out in case of .dat file to read
164!! @return tabvar              The value of Tab(nxx,nyy): Warning it is a real
165
166
167
168subroutine lect_input (numcol,basename,col,tabvar,filename,ncfileout) 
169
170  implicit none
171  integer,intent(in)  :: numcol                 !< column number
172  character(len=*), intent(in) :: basename      !< base name of the variable to read
173  integer,intent(in)  :: col                    !< column   1->average, 2->minval 3->maxval 
174  !          ??? double emploi avec numcol
175  real,dimension(:,:),intent(inout) :: tabvar   !< array to read in the variable
176  character(len=*),intent(in) :: filename       !< name of the file to read
177  character(len=*)::ncfileout                   !< name of the nc file to create
178
179  interface     !<<<<<<<<<<<<<<<<<<<
180     subroutine lect_ncfile(varname,Tab,filename)           
181       character(*),intent(in) :: varname
182       real,dimension(:,:),intent(inout) :: Tab
183       character(*),intent(in) :: filename
184       real*8, dimension(:,:), pointer :: tabvar
185     end subroutine lect_ncfile
186  end interface !<<<<<<<<<<<<<<<<<<<
187
188
189  character(1)  xcol
190  write( xcol,'(i1)')  col
191
192  if (index(filename,'.dat')  .ne.0) then    ! file in is a .dat
193
194     call dat2netcdf(size(tabvar,1),size(tabvar,2),numcol,basename,filename,ncfileout)
195     call lect_ncfile(trim(basename)//'_'//xcol,tabvar,ncfileout)               
196  else
197
198     if(index(filename,'.grd') .ne.0) then   ! file in is a .grd
199
200        call lect_ncfile('z',tabvar,filename)
201     else
202        ! file in is a .nc
203        if(index(filename,'.nc') .ne.0) then
204           call lect_ncfile(basename,tabvar,filename)
205        endif
206     endif
207  endif
208
209
210end subroutine lect_input
211
Note: See TracBrowser for help on using the repository browser.