Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 227 additions & 0 deletions PFIO_tools_compare.ipynb

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions fort_io/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# PF_FORT_IO

Python wrapped Fortran PF IO codes

## Prerequisites

* Fortran compiler
* Numpy

## Install

Recommend that this tool is installed in it's own virtual environment, like a conda environment, rather than at the system level.

```
git clone https://github.com/arezaii/pf_tools.git
cd pf_tools/fort_io
pip install .
```

## Usage

Brief overview of the pf_read usage

```
>>>import pf_pytools.pf_fort_io as pf_fort_io
>>>import numpy as np
>>>pfb_data = np.asfortranarray(np.zeros((3342,1888,17)))
>>>pf_fort_io.pfb_read(pfb_data,'filename')
```

138 changes: 138 additions & 0 deletions fort_io/pf_fort_io.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
subroutine pfb_read(value,fname,nx,ny,nz)
implicit none
real*8 value(nx,ny,nz)
real*8 dx, dy, dz, x1, y1, z1
integer*4 i,j,k, nni, nnj, nnk, ix, iy, iz, &
ns, rx, ry, rz,nx,ny,nz, nnx, nny, nnz,is
character*100 fname

open(100,file=trim(adjustl(fname)),form='unformatted', &
access='stream',convert='BIG_ENDIAN') !binary outputfile of Parflow
! Read in header info

! Start: reading of domain spatial information
read(100) x1 !X
read(100) y1 !Y
read(100) z1 !Z

read(100) nx !NX
read(100) ny !NY
read(100) nz !NZ

read(100) dx !DX
read(100) dy !DY
read(100) dz !DZ

read(100) ns !num_subgrids
! End: reading of domain spatial information

! Start: loop over number of sub grids
do is = 0, (ns-1)

! Start: reading of sub-grid spatial information
read(100) ix
read(100) iy
read(100) iz

read(100) nnx
read(100) nny
read(100) nnz
read(100) rx
read(100) ry
read(100) rz

! End: reading of sub-grid spatial information

! Start: read in saturation data from each individual subgrid
! do k=iz +1 , iz + nnz
! do j=iy +1 , iy + nny
! do i=ix +1 , ix + nnx
read(100) value((ix+1):(ix+nnx),(iy+1):(iy+nny),(iz+1):(iz+nnz))
! read(100) value(i,j,k)
! end do
! end do
! end do
! End: read in saturation data from each individual subgrid

! End: read in saturation data from each individual subgrid

end do
! End: loop over number of sub grids



close(100)
end subroutine


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Subroutine to write output as PFB
subroutine pfb_write(value,fname,nx,ny,nz,x1,y1,z1,dx,dy,dz)
implicit none
real*8 value(nx,ny,nz)
real*8 dx,dy,dz,x1,y1,z1
integer*4 i,j,k,nni,nnj,nnk,ix,iy,iz, &
ns,rx,ry,rz,nx,ny,nz,nnx,nny,nnz,is
character*200 fname

! ifort
! open(100,file=trim(fname),form='unformatted',accconvert='BIG_ENDIAN',status='unknown')

! gfortran
open(100,file=trim(fname),form='unformatted',access='stream',convert='BIG_ENDIAN',status='unknown')

nnx=nx; nny=ny; nnz=nz
ns=1
ix=0;iy=0;iz=0
rx=0;ry=0;rz=0

! Start: writing of domain spatial information
write(100) x1 !X
write(100) y1 !Y
write(100) z1 !Z

write(100) nx !NX
write(100) ny !NY
write(100) nz !NZ

write(100) dx !DX
write(100) dy !DY
write(100) dz !DZ

write(100) ns !num_subgrids
! End: writing of domain spatial information

! Start: loop over number of sub grids
do is = 0, (ns-1)

! Start: writing of sub-grid spatial information
write(100) ix
write(100) iy
write(100) iz
write(100) nnx
write(100) nny
write(100) nnz
write(100) rx
write(100) ry
write(100) rz
! End: writing of sub-grid spatial information

! Start: write in data from each individual subgrid
!write(100) value((ix+1):(ix+nnx),(iy+1):(iy+nny),(iz+1):(iz+nnz))
write(100) value
! do k=iz +1 , iz + nnz
! do j=iy +1 , iy + nny
! do i=ix +1 , ix + nnx
! write(100) value(i,j,k)
! end do
! end do
! end do

! End: write data from each individual subgrid

end do
! End: loop over number of sub grids

close(100)
end subroutine

30 changes: 30 additions & 0 deletions fort_io/pf_fort_io.pyf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
! -*- f90 -*-
! Note: the context of this file is case sensitive.

python module pf_fort_io ! in
interface ! in :pf_fort_io
subroutine pfb_read(value,fname,nx,ny,nz) ! in :pf_fort_io:pf_fort_io.f90
real*8 intent(inout),dimension(nx,ny,nz) :: value
character*100 intent(in) :: fname
integer*4 intent(in),optional,check(shape(value,0)==nx),depend(value) :: nx=shape(value,0)
integer*4 intent(in),optional,check(shape(value,1)==ny),depend(value) :: ny=shape(value,1)
integer*4 intent(in),optional,check(shape(value,2)==nz),depend(value) :: nz=shape(value,2)
end subroutine pfb_read
subroutine pfb_write(value,fname,nx,ny,nz,x1,y1,z1,dx,dy,dz) ! in :pf_fort_io:pf_fort_io.f90
real*8 dimension(nx,ny,nz) :: value
character*200 :: fname
integer*4, optional,check(shape(value,0)==nx),depend(value) :: nx=shape(value,0)
integer*4, optional,check(shape(value,1)==ny),depend(value) :: ny=shape(value,1)
integer*4, optional,check(shape(value,2)==nz),depend(value) :: nz=shape(value,2)
real*8 :: x1
real*8 :: y1
real*8 :: z1
real*8 :: dx
real*8 :: dy
real*8 :: dz
end subroutine pfb_write
end interface
end python module pf_fort_io

! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/
10 changes: 10 additions & 0 deletions fort_io/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

def configuration(parent_package='', top_path=None):
from numpy.distutils.misc_util import Configuration
config = Configuration('pf_pytools',parent_package, top_path)
config.add_extension('pf_fort_io', sources=['pf_fort_io.pyf','pf_fort_io.f90'])
return config

if __name__ == '__main__':
from numpy.distutils.core import setup
setup(**configuration(top_path='').todict())
Loading