EMPIRE DA  v1.9.1
Data assimilation codes using EMPIRE communication
 All Classes Files Functions Variables Pages
data_io.f90
Go to the documentation of this file.
1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2 !!! Time-stamp: <2016-10-18 15:23:32 pbrowne>
3 !!!
4 !!! Collection of subroutines to deal with i/o
5 !!! Copyright (C) 2014 Philip A. Browne
6 !!!
7 !!! This program is free software: you can redistribute it and/or modify
8 !!! it under the terms of the GNU General Public License as published by
9 !!! the Free Software Foundation, either version 3 of the License, or
10 !!! (at your option) any later version.
11 !!!
12 !!! This program is distributed in the hope that it will be useful,
13 !!! but WITHOUT ANY WARRANTY; without even the implied warranty of
14 !!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 !!! GNU General Public License for more details.
16 !!!
17 !!! You should have received a copy of the GNU General Public License
18 !!! along with this program. If not, see <http://www.gnu.org/licenses/>.
19 !!!
20 !!! Email: p.browne @ reading.ac.uk
21 !!! Mail: School of Mathematical and Physical Sciences,
22 !!! University of Reading,
23 !!! Reading, UK
24 !!! RG6 6BB
25 !!!
26 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27 
34  use output_empire, only : unit_obs,emp_e
35  use timestep_data
36  use pf_control
37  use sizes
38  implicit none
39  integer, parameter :: rk = kind(1.0d0)
40  integer, intent(in) :: t
41  real(kind=rk), dimension(obs_dim), intent(out) :: y
42  integer :: ios
43  character(14) :: filename
44 
45  write(filename,'(A,i7.7)') 'obs_ts_',tsdata%next_ob_timestep
46 
47  open(unit_obs,file=filename,iostat=ios,action='read',status='old',form='unformatted')
48  if(ios .ne. 0) then
49  write(emp_e,*) 'PARTICLE FILTER DATA ERROR!!!!! Cannot open file ',filename
50  write(emp_e,*) 'Check it exists. I need a lie down.'
51  stop
52  end if
53  read(unit_obs) y
54  close(unit_obs)
55 end subroutine default_get_observation_data
56 
61 subroutine save_observation_data(y)
62  use output_empire, only : unit_obs,emp_e
63  use pf_control
64  use sizes
65  implicit none
66  integer, parameter :: rk = kind(1.0d0)
67  real(kind=rk), dimension(obs_dim), intent(in) :: y
68  integer :: ios
69  character(14) :: filename
70 
71  write(filename,'(A,i7.7)') 'obs_ts_',pf%timestep
72 
73  open(unit_obs,file=filename,iostat=ios,action='write',status='replace',form='unformatted')
74  if(ios .ne. 0) then
75  write(emp_e,*) 'PARTICLE FILTER DATA ERROR!!!!! Cannot open file ',filename
76  write(emp_e,*) 'Very strange that I couldnt open it. Im going to stop now.'
77  stop
78  end if
79  write(unit_obs) y
80  close(unit_obs)
81 
82 end subroutine save_observation_data
83 
87 subroutine get_truth(x)
88  use output_empire, only : unit_truth,emp_e
89  use timestep_data
90  use pf_control
91  use sizes
92  implicit none
93  integer, parameter :: rk = kind(1.0d0)
94  real(kind=rk), dimension(state_dim), intent(out) :: x
95  integer :: ios
96  if(pf%timestep .eq. 0) then
97  print*,'opening pf_truth'
98  open(unit_truth,file='pf_truth',iostat=ios,action='read')
99  if(ios .ne. 0) then
100  write(emp_e,*) 'PARTICLE FILTER DATA ERROR!!!!! Cannot open file pf_truth'
101  write(emp_e,*) 'Very strange that I couldnt open it. Im going to stop now.'
102  stop
103  end if
104  end if
105  read(unit_truth,*) x
106  call flush(unit_truth)
107  !if(pf%timestep .eq. pf%time_obs*pf%time_bwn_obs) then
108  if(tsdata%completed_timesteps .eq. tsdata%total_timesteps) then
109  close(unit_truth)
110  print*,'closing pf_truth'
111  end if
112 end subroutine get_truth
113 
117 subroutine save_truth(x)
118  use output_empire, only : unit_truth,unit_mean,emp_e
119  use timestep_data
120  use pf_control
121  use sizes
122  implicit none
123  integer, parameter :: rk = kind(1.0d0)
124  real(kind=rk), dimension(state_dim), intent(in) :: x
125  integer :: ios
126  if(pf%timestep .eq. 0) then
127  print*,'opening pf_truth'
128  open(unit_truth,file='pf_truth',iostat=ios,action='write',status='replace')
129  if(ios .ne. 0) then
130  write(emp_e,*) 'PARTICLE FILTER DATA ERROR!!!!! Cannot open file pf_truth'
131  write(emp_e,*) 'Very strange that I couldnt open it. Im going to stop now.'
132  stop
133  end if
134  end if
135  write(unit_truth,*) x
136  call flush(unit_truth)
137  !if(pf%timestep .eq. pf%time_obs*pf%time_bwn_obs) then
138  if(tsdata%completed_timesteps .eq. tsdata%total_timesteps) then
139  close(unit_truth)
140  print*,'closing pf_truth'
141  end if
142 end subroutine save_truth
143 
144 
145 
147 subroutine output_from_pf
148  use output_empire, only : unit_weight,unit_mean,emp_e
149  use timestep_data
150  use matrix_pf
151  use pf_control
152  use sizes
153  use comms
154  implicit none
155  include 'mpif.h'
156  real(kind=kind(1.0D0)), dimension(state_dim) :: mean,mtemp
157  integer :: ios,particle,mpi_err
158  character(20) :: filename
159 
160 
161  if(pf%timestep .eq. 0) then
162 
163  if(pf%output_weights) then
164  write(filename,'(A,i2.2)') 'ensemble_weights_',pfrank
165  open(unit_weight,file=filename,iostat=ios,action='write',status='replace')
166  if(ios .ne. 0) then
167  write(emp_e,*) 'PARTICLE FILTER DATA ERROR!!!!! Cannot open file pf_out'
168  write(emp_e,*) 'Very strange that I couldnt open it. Im going to stop now.'
169  stop
170  end if
171  end if !end if output_weights
173  end if
174 
175  if(pf%output_weights) then
176  write(unit_weight,'(i6.6,A)',advance='no') pf%timestep,' '
177  do ios = 1,pf%count-1
178  write(unit_weight,'(i6.6,A,e21.15,A)',advance='no') pf%particles(ios),' ',pf&
179  &%weight(pf%particles(ios)),' '
180  end do
181  write(unit_weight,'(i6.6,A,e21.15)',advance='yes') pf%particles(pf%count),' ',pf&
182  &%weight(pf%particles(pf%count))
183  call flush(unit_weight)
184 
185  if(tsdata%completed_timesteps .eq. tsdata%total_timesteps) close(unit_weight)
186  end if !end if output_weights
187 
188  if(pf%use_mean .and. pf_ens_rank .eq. 0) then
189  if(pf%timestep .eq. 0) then
190  open(unit_mean,file='pf_mean',iostat=ios,action='write',status='replace')
191  if(ios .ne. 0) then
192  write(emp_e,*) 'PARTICLE FILTER DATA ERROR!!!!! Cannot open file pf_mean'
193  write(emp_e,*) 'Very strange that I couldnt open it. Im going to stop now.'
194  stop
195  end if
196  end if
197  end if
198 
199  if(pf%use_mean .or. (pf%use_spatial_rmse .and. .not. pf%gen_data)) then
200  mtemp = 0.0d0
201  do particle = 1,pf%count
202  !mean(:) = mean(:) + pf%psi(:,particle)*exp(-pf&
203  ! &%weight(particles(particle)))
204  mtemp(:) = mtemp(:) + pf%psi(:,particle)/real(pf%nens)
205  end do
206 
207  call mpi_allreduce(mtemp,mean,state_dim,mpi_double_precision,mpi_sum&
208  &,pf_ens_comm,mpi_err)
209 
210  end if
211 
212  if(pf%use_mean .and. pf_ens_rank .eq. 0) then
213  write(unit_mean,*) mean(:)
214  call flush(unit_mean)
215  if(tsdata%completed_timesteps .eq. tsdata%total_timesteps) close(unit_mean)
216  end if
217 
218  if(pf_ens_rank .eq. 0 .and. pf%use_spatial_rmse .and. .not. pf%gen_data) call output_spatial_rmse(mean)
219 
220  if(pf%use_mean .and. pf%use_variance) call output_variance(mean)
221 
222  if(comm_version .ne. 3) then !empire_v3 models will be too large!!!
223  call matrix_pf_output(npfs-1,pf_mpi_comm,state_dim,cnt,pf%psi&
224  &,pf%timestep,tsdata%is_analysis)
225  end if
226 
227 end subroutine output_from_pf
228 
231 subroutine save_state(state,filename)
232  use output_empire, only : unit_state,emp_e
233  use sizes
234  implicit none
235  integer, parameter :: rk = kind(1.0d0)
236  real(kind=rk), dimension(state_dim), intent(in) :: state
238  character(256), intent(in) :: filename
240  integer :: ios
241 
242  open(unit_state,file=trim(filename),iostat=ios,action='write',status='replace'&
243  &,form='unformatted')
244  if(ios .ne. 0) then
245  write(emp_e,*) 'PARTICLE FILTER DATA ERROR!!!!! Cannot open file '&
246  &,filename
247  write(emp_e,*) 'Very strange that I couldnt open it. Im going to stop&
248  & now.'
249  stop 'EMPIRE ERROR in SAVE_STATE'
250  end if
251  write(unit_state) state
252  close(unit_state)
253 end subroutine save_state
254 
255 
258 subroutine get_state(state,filename)
259  use output_empire, only : unit_state,emp_e
260  use sizes
261  implicit none
262  integer, parameter :: rk = kind(1.0d0)
263  real(kind=rk), dimension(state_dim), intent(out) :: state
265  character(256), intent(in) :: filename
267  integer :: ios
268 
269  open(unit_state,file=trim(filename),iostat=ios,action='read',status='old',form='un&
270  &formatted')
271  if(ios .ne. 0) then
272  write(emp_e,*) 'PARTICLE FILTER DATA ERROR!!!!! Cannot open file '&
273  &,filename
274  write(emp_e,*) 'Very strange that I couldnt open it. Im going to stop&
275  & now.'
276  stop
277  end if
278  read(unit_state) state
279  close(unit_state)
280 end subroutine get_state
subroutine save_observation_data(y)
Subroutine to save observation to a file Uses pftimestep to determine which observation to save...
Definition: data_io.f90:61
subroutine default_get_observation_data(y, t)
Subroutine to read observation from a file Uses pftimestep to determine which observation to read...
Definition: data_io.f90:33
Module containing EMPIRE coupling data.
Definition: comms.f90:57
module to deal with generating and outputting pf matrix
Definition: matrix_pf.f90:29
Module that stores the information about the outputting from empire.
Module that stores the information about the timestepping process.
subroutine save_truth(x)
Subroutine to save truth to a file .
Definition: data_io.f90:117
Module that stores the dimension of observation and state spaces.
Definition: sizes.f90:29
subroutine read_matrix_pf_information
subroutine to read namelist to control this output
Definition: matrix_pf.f90:61
subroutine matrix_pf_output(root, comm, n, m, x, time, is_analysis)
subroutine to generate and output matrix Pf
Definition: matrix_pf.f90:124
subroutine output_from_pf
subroutine to output data from the filter
Definition: data_io.f90:147
subroutine get_state(state, filename)
subroutine to read the state vector from a named file as an unformatted fortran file ...
Definition: data_io.f90:258
subroutine get_truth(x)
Subroutine to read truth from the file written by save_truth .
Definition: data_io.f90:87
subroutine output_variance(mean)
subroutine to output ensemble variance
module pf_control holds all the information to control the the main program
Definition: pf_control.f90:29
subroutine save_state(state, filename)
subroutine to save the state vector to a named file as an unformatted fortran file ...
Definition: data_io.f90:231
subroutine output_spatial_rmse(mean)
subroutine to output RMSEs