readinput.f90 6.13 KB
Newer Older
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
!---------------------------------------------------------------------------------------
! FLEXINVERT : readinput
!---------------------------------------------------------------------------------------
!  Copyright 2013, Rona Thompson
!
!  FLEXINVERT is free software: you can redistribute it and/or modify
!  it under the terms of the GNU General Public License as published by
!  the Free Software Foundation, either version 3 of the License, or
!  (at your option) any later version.
!
!  FLEXINVERT is distributed in the hope that it will be useful,
!  but WITHOUT ANY WARRANTY; without even the implied warranty of
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!  GNU General Public License for more details.
!
!  You should have received a copy of the GNU General Public License
!  along with FLEXINVERT.  If not, see <http://www.gnu.org/licenses/>.
!---------------------------------------------------------------------------------------
!> Reads input data from inversion 
!---------------------------------------------------------------------------------------

subroutine readinput()

  use mod_var

  implicit none

  include '/usr/include/netcdf.inc'

  integer :: i,nid,ierr,xid,yid,zid,nx,ny,nz,varid
  real, dimension(:,:), allocatable :: tmp

    !---------------------------------------------------------------
  ! prior flux at fine resolution
  !---------------------------------------------------------------

  ! open netcdf file
  write(logid,*) 'Reading analysis.nc'
  ierr=nf_open(trim(dirinvert)//'/'//'analysis.nc',NF_NOWRITE,nid)
  if(ierr.ne.0) then
    write(logid,*) 'Error opening analysis.nc'
  endif

  ! inquire about variables
  ierr=nf_inq_dimid(nid,'longitude',xid)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading dimension: longitude'
  endif
  ierr=nf_inq_dimid(nid,'latitude',yid)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading dimension: latitude'
  endif  
  ierr=nf_inq_dimid(nid,'time',zid)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading dimension: time'
  endif

  ! read dimension sizes
  ierr=nf_inq_dimlen(nid,xid,nlon)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading dimension length'
  endif
  ierr=nf_inq_dimlen(nid,yid,nlat)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading dimension length'
  endif
  ierr=nf_inq_dimlen(nid,zid,ntime)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading dimension length'
  endif

  allocate(lon(nlon))
  allocate(lat(nlat))
  allocate(time(ntime))
  allocate(emisprior(nlon,nlat,ntime))

  ! read variables
  ierr=nf_inq_varid(nid,'longitude',varid)
  ierr=nf_get_var_real(nid,varid,lon)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading longitude'
  endif
  ierr=nf_inq_varid(nid,'latitude',varid)
  ierr=nf_get_var_real(nid,varid,lat)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading latitude'
  endif
  ierr=nf_inq_varid(nid,'time',varid)
  ierr=nf_get_var_real(nid,varid,time)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading time'
  endif
  ierr=nf_inq_varid(nid,'fpri',varid)
  ierr=nf_get_var_real(nid,varid,emisprior)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading emis_prior'
  endif

  ierr=nf_close(nid)

  ! multiply by numscale and outheight
  emisprior=emisprior*numscale/outheight

  !CGZ Adjust nemsteps to number of timesteps, no need to set in initialize
  nemsteps=ntime

  !---------------------------------------------------------------
  ! grid definition
  !---------------------------------------------------------------

  ! open netcdf file
  nx=nlon
  nxgrid=nx
  ny=nlat
  nygrid=ny
  write(logid,*) 'Reading nbox_xy.txt'
  !print*, 'nlon:', nlon, ' nx:', nx,'nlat:', nlat, ' ny:', ny
  allocate(nbox_xy(nx,ny))
  allocate(tmp(nx,ny))

  open(100,file=trim(dirinvert)//'/nbox_xy.txt',action='read',status='old',iostat=ierr)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading nbox_xy.txt'
  endif
  do i=1,nx
    read(100,*) tmp(i,:)
    !print*, i, tmp(i,:)
  end do
  nbox_xy=int(tmp)
  nbox=maxval(nbox_xy)

  !print*, nbox_xy
  close(100)
  deallocate(tmp)

  !---------------------------------------------------------------
  ! grid box area
  !---------------------------------------------------------------

  allocate(area_box(nbox))

  open(100,file=trim(dirinvert)//'/area_box.txt',action='read',status='old',iostat=ierr)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading area_box.txt'
  endif
  do i=1,nbox
    read(100,*) area_box(i)
  end do
  !print*, area_box
  close(100)


  !---------------------------------------------------------------
  ! posterior error covariance
  !---------------------------------------------------------------
  write(logid,*) 'Reading cova (izwork.txt)'
  nvar=nbox*nemsteps

  allocate(cova(nvar,nvar))
  open(100,file=trim(dirinvert)//'/izwork.txt',action='read',status='old',iostat=ierr)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading izwork.txt'
163
164
165
166
167
    !CGZ: try reading cova.txt
    open(100,file=trim(dirinvert)//'/cova.txt',action='read',status='old',iostat=ierr)
    if(ierr.ne.0) then
      write(logid,*) 'Error reading cova.txt'
    endif
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
  endif
  do i=1,nvar
    read(100,*) cova(i,:)
   ! print*, cova(i,:)
  end do
  close(100)



  !---------------------------------------------------------------
  ! posterior state vector
  !---------------------------------------------------------------

  allocate(x0(nvar))

  write(logid,*) 'Reading posterior.txt' 
  open(100,file=trim(dirinvert)//'posterior.txt',action='read',status='old',iostat=ierr)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading posterior.txt'
  endif
  write(logid,*), 'nvar:', nvar
  do i=1,nvar
    read(100,*) x0(i)
  end do
  close(100)
  ! multiply by numscale and outheight
  x0=x0*numscale/outheight

  !---------------------------------------------------------------
  ! prior state vector
  !---------------------------------------------------------------

  allocate(xb(nvar))

  write(logid,*) 'Reading prior.txt'
  open(100,file=trim(dirinvert)//'prior.txt',action='read',status='old',iostat=ierr)
  if(ierr.ne.0) then
    write(logid,*) 'Error reading prior.txt'
  endif
  do i=1,nvar
    read(100,*) xb(i)
  end do
  close(100)
  ! multiply by numscale and outheight
  xb=xb*numscale/outheight

end subroutine readinput