Class Statistics
In: statistics.f90

統計解析関係のルーチン集

Methods

Included Modules

Algebra

Public Instance methods

Subroutine :
x(:) :real, intent(in)
: データ
anor(size(x)) :real, intent(inout)
: 各 x(i) に対応する偏差 anor(i)
error :real, intent(in), optional
: 欠損値が存在するデータセットの場合の欠損値

1 次元データ配列の偏差を返す

[Source]

subroutine Anomaly_1d( x, anor, error )  ! 1 次元データ配列の偏差を返す
  implicit none
  real, intent(in) :: x(:)  ! データ
  real, intent(inout) :: anor(size(x))  ! 各 x(i) に対応する偏差 anor(i)
  real, intent(in), optional :: error  ! 欠損値が存在するデータセットの場合の欠損値
  integer :: i
  integer :: nx  ! データの要素数
  real :: ave

  nx=size(x)

  if(present(error))then
     call Mean_1d( x, ave, error )
     do i=1,nx
        if(x(i)==error)then
           anor(i)=error
        else
           anor(i)=x(i)-ave
        end if
     end do
  else
     call Mean_1d( x, ave )
     do i=1,nx
        anor(i)=x(i)-ave
     end do
  end if

end subroutine Anomaly_1d
Subroutine :
x(:,:) :real, intent(in)
: データ
anor(size(x,1),size(x,2)) :real, intent(inout)
: 各 x(i,j) に対応する偏差 anor(i,j)
error :real, intent(in), optional
: 欠損値が存在するデータセットの場合の欠損値

2 次元データ配列の偏差を返す

[Source]

subroutine Anomaly_2d( x, anor, error )  ! 2 次元データ配列の偏差を返す
  implicit none
  real, intent(in) :: x(:,:)  ! データ
  real, intent(inout) :: anor(size(x,1),size(x,2))  ! 各 x(i,j) に対応する偏差 anor(i,j)
  real, intent(in), optional :: error  ! 欠損値が存在するデータセットの場合の欠損値
  integer :: i, j
  integer :: nx  ! データの要素数 1
  integer :: ny  ! データの要素数 2
  real :: ave

  nx=size(x,1)
  ny=size(x,2)

  if(present(error))then
     call Mean_2d( x, ave, error )
     do j=1,ny
        do i=1,nx
           if(x(i,j)==error)then
              anor(i,j)=error
           else
              anor(i,j)=x(i,j)-ave
           end if
        end do
     end do
  else
     call Mean_2d( x, ave, error )
     do j=1,ny
        do i=1,nx
           anor(i,j)=x(i,j)-ave
        end do
     end do
  end if

end subroutine Anomaly_2d
Subroutine :
x(:,:,:) :real, intent(in)
: データ
anor(size(x,1),size(x,2),size(x,3)) :real, intent(inout)
: 各 x(i,j,k) に対応する偏差 anor(i,j,k)
error :real, intent(in), optional
: 欠損値が存在するデータセットの場合の欠損値

3 次元データ配列の偏差を返す

[Source]

subroutine Anomaly_3d( x, anor, error )  ! 3 次元データ配列の偏差を返す
  implicit none
  real, intent(in) :: x(:,:,:)  ! データ
  real, intent(inout) :: anor(size(x,1),size(x,2),size(x,3))  ! 各 x(i,j,k) に対応する偏差 anor(i,j,k)
  real, intent(in), optional :: error  ! 欠損値が存在するデータセットの場合の欠損値
  integer :: i, j, k
  integer :: nx  ! データの要素数 1
  integer :: ny  ! データの要素数 2
  integer :: nz  ! データの要素数 3
  real :: ave

  nx=size(x,1)
  ny=size(x,2)
  nz=size(x,3)

  if(present(error))then
     call Mean_3d( x, ave, error )
     do k=1,nz
        do j=1,ny
           do i=1,nx
              if(x(i,j,k)==error)then
                 anor(i,j,k)=error
              else
                 anor(i,j,k)=x(i,j,k)-ave
              end if
           end do
        end do
     end do
  else
     call Mean_3d( x, ave, error )
     do k=1,nz
        do j=1,ny
           do i=1,nx
              anor(i,j,k)=x(i,j,k)-ave
           end do
        end do
     end do
  end if

end subroutine Anomaly_3d
Subroutine :
x(:) :real, intent(in)
: データ要素 1
y(size(x)) :real, intent(in)
: データ要素 2
cc :real, intent(inout)
: 相関係数
error :real, intent(in), optional
: 欠損値

2 データの相関係数を計算するルーチン

[Source]

subroutine Cor_Coe( x, y ,cc, error )  ! 2 データの相関係数を計算するルーチン
  implicit none
  real, intent(in) :: x(:)  ! データ要素 1
  real, intent(in) :: y(size(x))  ! データ要素 2
  real, intent(inout) :: cc  ! 相関係数
  real, intent(in), optional :: error  ! 欠損値
  integer :: i
  integer :: nx  ! データ個数
  real :: cov, anor1, anor2

  nx=size(x)

  if(present(error))then
     call covariance( x, y, cov, error )
     call stand_vari( x, anor1, error )
     call stand_vari( y, anor2, error )
  else
     call covariance( x, y, cov )
     call stand_vari( x, anor1 )
     call stand_vari( y, anor2 )
  end if

  cc=cov/(sqrt(anor1)*sqrt(anor2))

end subroutine Cor_Coe
Subroutine :
x(:) :real, intent(in)
: データ要素 1
y(size(x)) :real, intent(in)
: データ要素 2
slope :real, intent(inout)
: 最適な傾き
intercept :real, intent(inout)
: 最適な切片
undef :real, intent(in), optional
: undef

最小二乗法による傾きと切片計算

[Source]

subroutine LSM( x, y, slope, intercept, undef )  ! 最小二乗法による傾きと切片計算
  implicit none
  real, intent(in) :: x(:)  ! データ要素 1
  real, intent(in) :: y(size(x))  ! データ要素 2
  real, intent(inout) :: slope  ! 最適な傾き
  real, intent(inout) :: intercept  ! 最適な切片
  real, intent(in), optional :: undef ! undef 
  real :: u(size(x)), v(size(x))
  integer :: i, j, k
  integer :: nx  ! データ数
  real :: a, b, c, d

  nx=size(x)
  a=0.0
  b=0.0
  c=0.0
  d=0.0

!$omp parallel do shared(u, v, x, y) private(i)
  do i=1,nx
     u(i)=x(i)*x(i)
     v(i)=x(i)*y(i)
  end do
!$omp end parallel do

  call summ(v,a,undef)
  call summ(x,b,undef)
  call summ(y,c,undef)
  call summ(u,d,undef)

  slope=(nx*a-b*c)/(nx*d-b**2)
  intercept=(c*d-a*b)/(nx*d-b**2)

end subroutine LSM
Subroutine :
x(:) :real, intent(in)
: データ要素配列 1
y(size(x)) :real, intent(in)
: データ要素配列 2
a(:) :real, intent(inout)
: 多項式の係数
intercept :real, intent(inout)
: y 切片. a に組み込むと引数を渡すとき, poly_n+1 で渡す必要が あり, 紛らわしいと判断したため, a_0 である y 切片を 独立で引数として渡すことにした.
undef :real, intent(in), optional
: 未定義値.

LSM の多項式近似バージョン. LSM では, F(x)=a_0+a_1x の直線近似を行っていたが, LSM_poly では, F(x)=sum^{N}_{n=0}{a_nx^n} の任意次数の多項式曲線近似を行うことが可能. アルゴリズムは最小二乗法を用いており, 係数のソルバには gausss ルーチンを使用.

[Source]

subroutine LSM_poly( x, y, a, intercept, undef )
! LSM の多項式近似バージョン.
! LSM では, F(x)=a_0+a_1x の直線近似を行っていたが,
! LSM_poly では, F(x)=\sum^{N}_{n=0}{a_nx^n}
! の任意次数の多項式曲線近似を行うことが可能.
! アルゴリズムは最小二乗法を用いており, 係数のソルバには gausss ルーチンを使用.
  use Algebra
  implicit none
  real, intent(in) :: x(:)  ! データ要素配列 1
  real, intent(in) :: y(size(x))  ! データ要素配列 2
  real, intent(inout) :: a(:)  ! 多項式の係数
  real, intent(inout) :: intercept  ! y 切片. 
                         ! a に組み込むと引数を渡すとき, poly_n+1 で渡す必要が
                         ! あり, 紛らわしいと判断したため, a_0 である y 切片を
                         ! 独立で引数として渡すことにした.
  real, intent(in), optional :: undef  ! 未定義値.
  integer :: i, j, k, l, m, n
  integer :: nx  ! データの個数
  integer :: poly_n  ! 近似する曲線の最高次数. 1 なら, LSM と同じ.
  real :: coe(0:size(a)), tmpa_coe(0:size(a),0:size(a)), tmpb_coe(0:size(a))
          ! coe は a_n が入る. tmp_coe はデータの総和が入る.
          ! [注意] : 第一要素が行. 第二要素が列.
  real :: tmp(size(x))  ! べき乗計算の一時配列

  nx=size(x)
  poly_n=size(a)

!-- gausss に渡しやすいように, 用意した配列に引数を代入.
  if(present(undef))then
     do k=0,poly_n  ! 列成分の計算
        do j=0,poly_n  ! 行成分の計算. 行成分の計算が先に回ることに注意.
           if(j >= k)then  ! 行成分(j)より列成分(k)の要素数が小さい場合, 値を
                           ! まじめに計算する.
              do i=1,nx
                 if(x(i)/=undef)then
                    tmp(i)=x(i)**(j+k)
                 else
                    tmp(i)=undef
                 end if
              end do
              call summ( tmp, tmpa_coe(j,k), undef )
           else  ! 行成分(j)より列成分(k)の要素数が大きい場合, 解く係数行列が
                 ! 対称行列であることから, 値の参照代入のみ行う.
              tmpa_coe(j,k)=tmpa_coe(k,j)  ! 対称成分の代入(すでに計算済み)
           end if
        end do
     end do
     do j=0,poly_n
        do i=1,nx
           if(x(i)/=undef)then
              tmp(i)=y(i)*(x(i)**j)
           else
              tmp(i)=undef
           end if
        end do
        call summ( tmp, tmpb_coe(j), undef )
     end do
  else  ! undef 処理がないとき.
     do k=0,poly_n  ! 列成分の計算
        do j=0,poly_n  ! 行成分の計算. 行成分の計算が先に回ることに注意.
           if(j >= k)then  ! 行成分(j)より列成分(k)の要素数が小さい場合, 値を
                           ! まじめに計算する.
              do i=1,nx
                 tmp(i)=x(i)**(j+k)
              end do
              call summ( tmp, tmpa_coe(j,k), undef )
           else  ! 行成分(j)より列成分(k)の要素数が大きい場合, 解く係数行列が
                 ! 対称行列であることから, 値の参照代入のみ行う.
              tmpa_coe(j,k)=tmpa_coe(k,j)  ! 対称成分の代入(すでに計算済み)
           end if
        end do
     end do
     do j=0,poly_n
        do i=1,nx
           tmp(i)=y(i)*(x(i)**j)
        end do
        call summ( tmp, tmpb_coe(j), undef )
     end do
  end if

!  以上で係数行列に値が入った.

  call gausss( tmpa_coe(0:poly_n,0:poly_n), tmpb_coe(0:poly_n), coe(0:poly_n) )

  do i=1,poly_n
     a(i)=coe(i)
  end do
  intercept=coe(0)

end subroutine
Subroutine :
x(:) :real, intent(in)
: データ
ave :real, intent(inout)
: 計算する平均値
error :real, intent(in), optional
: 欠損値が存在するデータセットの場合の欠損値

1 次元配列平均値計算ルーチン

[Source]

subroutine Mean_1d( x, ave, error )  ! 1 次元配列平均値計算ルーチン
  implicit none
  real, intent(in) :: x(:)  ! データ
  real, intent(inout) :: ave  ! 計算する平均値
  real, intent(in), optional :: error  ! 欠損値が存在するデータセットの場合の欠損値
  integer :: i, nt
  integer :: nx  ! データの要素数
  real :: summ

  summ=0.0
  nt=0
  nx=size(x)

  if(present(error))then
     do i=1,nx
        if(x(i)/=error)then
           summ=summ+x(i)
           nt=1+nt
        end if
     end do

     if(nt/=0)then
        ave=summ/nt
     else
        ave=0.0
     end if

  else

     do i=1,nx
        summ=summ+x(i)
     end do

     ave=summ/nx

  end if

end subroutine Mean_1d
Subroutine :
x(:,:) :real, intent(in)
: データ
ave :real, intent(inout)
: 計算する平均値
error :real, intent(in), optional
: 欠損値が存在するデータセットの場合の欠損値

2 次元配列平均値計算ルーチン

[Source]

subroutine Mean_2d( x, ave, error )  ! 2 次元配列平均値計算ルーチン
  implicit none
  real, intent(in) :: x(:,:)  ! データ
  real, intent(inout) :: ave  ! 計算する平均値
  real, intent(in), optional :: error  ! 欠損値が存在するデータセットの場合の欠損値
  integer :: i, j, nt
  integer :: nx  ! データの要素数 1
  integer :: ny  ! データの要素数 2
  real :: summ, tmp

  summ=0.0
  nt=0
  nx=size(x,1)
  ny=size(x,2)

  if(present(error))then
     do j=1,ny
        do i=1,nx
           if(x(i,j)/=error)then
              summ=summ+x(i,j)
              nt=1+nt
           end if
        end do
     end do

     if(nt/=0)then
        ave=summ/nt
     else
        ave=0.0
     end if

  else

     do j=1,ny
        do i=1,nx
           summ=summ+x(i,j)
        end do
     end do

     ave=summ/(nx*ny)

  end if

end subroutine Mean_2d
Subroutine :
x(:,:,:) :real, intent(in)
: データ
ave :real, intent(inout)
: 計算する平均値
error :real, intent(in), optional
: 欠損値が存在するデータセットの場合の欠損値

3 次元配列平均値計算ルーチン

[Source]

subroutine Mean_3d( x, ave, error )  ! 3 次元配列平均値計算ルーチン
  implicit none
  real, intent(in) :: x(:,:,:)  ! データ
  real, intent(inout) :: ave  ! 計算する平均値
  real, intent(in), optional :: error  ! 欠損値が存在するデータセットの場合の欠損値
  integer :: i, j, k, nt
  integer :: nx  ! データの要素数 1
  integer :: ny  ! データの要素数 2
  integer :: nz  ! データの要素数 2
  real :: summ, tmp

  summ=0.0
  nt=0
  nx=size(x,1)
  ny=size(x,2)
  nz=size(x,3)

  if(present(error))then
     do k=1,nz
        do j=1,ny
           do i=1,nx
              if(x(i,j,k)/=error)then
                 summ=summ+x(i,j,k)
                 nt=1+nt
              end if
           end do
        end do
     end do

     if(nt/=0)then
        ave=summ/nt
     else
        ave=0.0
     end if

  else

     do k=1,nz
        do j=1,ny
           do i=1,nx
              summ=summ+x(i,j,k)
           end do
        end do
     end do

     ave=summ/(nx*ny*nz)

  end if

end subroutine Mean_3d
Subroutine :
x(:) :real, intent(in)
: データ要素 1
y(size(x)) :real, intent(in)
: データ要素 2
slope :real, intent(inout)
: 最適な傾き
intercept :real, intent(inout)
: 最適な切片

LSM を用いて回帰直線の傾き slope と切片 intercept を計算するルーチン

[Source]

subroutine Reg_Line( x, y, slope, intercept )
  ! LSM を用いて回帰直線の傾き slope と切片 intercept を計算するルーチン
  implicit none
  real, intent(in) :: x(:)  ! データ要素 1
  real, intent(in) :: y(size(x))  ! データ要素 2
  real, intent(inout) :: slope  ! 最適な傾き
  real, intent(inout) :: intercept  ! 最適な切片
  real :: u(size(x)), v(size(x))
  integer :: nx  ! データ数

  nx=size(x)

  call Anomaly_1d( x, u )
  call Anomaly_1d( y, v )
  call LSM( u, v, slope, intercept )

end subroutine
Subroutine :
x(:) :real, intent(in)
: データ 1
y(size(x)) :real, intent(in)
: データ 2
cov :real, intent(inout)
: 標準偏差
error :real, intent(in), optional
: 欠損値

2 つの 1 次元データの共分散を計算 共分散$sigma $の定義は, $$sigma =sum^{nx}_{i=1}{(x-\bar{x})(y-\bar{y})} $$

[Source]

subroutine covariance( x, y, cov, error )  ! 2 つの 1 次元データの共分散を計算
  ! 共分散$\sigma $の定義は,
  ! $$\sigma =\sum^{nx}_{i=1}{(x-\bar{x})(y-\bar{y})} $$
  implicit none
  real, intent(in) :: x(:)  ! データ 1
  real, intent(in) :: y(size(x))  ! データ 2
  real, intent(inout) :: cov  ! 標準偏差
  real, intent(in), optional :: error  ! 欠損値
  integer :: i
  integer :: nx  ! データ数
  real :: an1(size(x)), an2(size(x))

  nx=size(x)
  cov=0.0

  if(present(error))then
     call Anomaly_1d( x, an1, error )
     call Anomaly_1d( y, an2, error )
     do i=1,nx
        if(x(i)/=error)then
           cov=cov+an1(i)*an2(i)
        end if
     end do
  else
     call Anomaly_1d( x, an1 )
     call Anomaly_1d( y, an2 )
     do i=1,nx
        cov=cov+an1(i)*an2(i)
     end do
  end if

end subroutine covariance
Subroutine :
x(:) :real, intent(in)
: 漸増配列
point :real, intent(in)
: この点
i :integer, intent(inout)
: point の値を越えない最大の値をもつ要素番号
undeff :real, intent(in), optional
: 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.

漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで, point の前に来る要素番号を出力する.

[Source]

subroutine interpo_search_1d( x, point, i, undeff )
  ! 漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで,
  ! point の前に来る要素番号を出力する.
  implicit none
  real, intent(in) :: x(:)  ! 漸増配列
  real, intent(in) :: point  ! この点
  integer, intent(inout) :: i  ! point の値を越えない最大の値をもつ要素番号
  real, intent(in), optional :: undeff  ! 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.
  integer :: nx, j
  integer :: just

  nx=size(x)
  if(present(undeff))then
     just=int(undeff)
  else
     just=0
  end if

  do j=1,nx
     if(x(1)>point)then
        write(*,*) "****** WARNING ******"
        write(*,*) "searching point was not found."
        write(*,*) "Abort. Exit.!!!"
        i=just
        exit
     end if

     if(present(undeff))then
        if(x(j)/=undeff)then
           if(x(j)<=point)then
              i=j
           else
              exit
           end if
        end if
     else
        if(x(j)<=point)then
           i=j
        else
           exit
        end if
     end if
  end do

end subroutine interpo_search_1d
Subroutine :
x(:) :real, intent(in)
: 漸増配列 x
y(:) :real, intent(in)
: 漸増配列 y
pointx :real, intent(in)
: この点 x
pointy :real, intent(in)
: この点 y
i :integer, intent(inout)
: pointx の値を越えない最大の値をもつ要素番号
j :integer, intent(inout)
: pointy の値を越えない最大の値をもつ要素番号
undeff :real, intent(in), optional
: 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.

漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで, point の前に来る要素番号を出力する.

[Source]

subroutine interpo_search_2d( x, y, pointx, pointy, i, j, undeff )
  ! 漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで,
  ! point の前に来る要素番号を出力する.
  implicit none
  real, intent(in) :: x(:)  ! 漸増配列 x
  real, intent(in) :: y(:)  ! 漸増配列 y
  real, intent(in) :: pointx  ! この点 x
  real, intent(in) :: pointy  ! この点 y
  integer, intent(inout) :: i  ! pointx の値を越えない最大の値をもつ要素番号
  integer, intent(inout) :: j  ! pointy の値を越えない最大の値をもつ要素番号
  real, intent(in), optional :: undeff  ! 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.
  integer :: just

  if(present(undeff))then
     just=int(undeff)
     call interpo_search_1d( x, pointx, i, real(just) )
     call interpo_search_1d( y, pointy, j, real(just) )
  else
     call interpo_search_1d( x, pointx, i )
     call interpo_search_1d( y, pointy, j )
  end if

end subroutine interpo_search_2d
Subroutine :
x(:) :real, intent(in)
: 漸増配列 x
y(:) :real, intent(in)
: 漸増配列 y
z(:) :real, intent(in)
: 漸増配列 z
pointx :real, intent(in)
: この点 x
pointy :real, intent(in)
: この点 y
pointz :real, intent(in)
: この点 z
i :integer, intent(inout)
: pointx の値を越えない最大の値をもつ要素番号
j :integer, intent(inout)
: pointy の値を越えない最大の値をもつ要素番号
k :integer, intent(inout)
: pointz の値を越えない最大の値をもつ要素番号
undeff :real, intent(in), optional
: 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.

漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで, point の前に来る要素番号を出力する.

[Source]

subroutine interpo_search_3d( x, y, z, pointx, pointy, pointz, i, j, k, undeff )
  ! 漸増配列(要素数が増えるごとに値が大きくなる配列)のなかで,
  ! point の前に来る要素番号を出力する.
  implicit none
  real, intent(in) :: x(:)  ! 漸増配列 x
  real, intent(in) :: y(:)  ! 漸増配列 y
  real, intent(in) :: z(:)  ! 漸増配列 z
  real, intent(in) :: pointx  ! この点 x
  real, intent(in) :: pointy  ! この点 y
  real, intent(in) :: pointz  ! この点 z
  integer, intent(inout) :: i  ! pointx の値を越えない最大の値をもつ要素番号
  integer, intent(inout) :: j  ! pointy の値を越えない最大の値をもつ要素番号
  integer, intent(inout) :: k  ! pointz の値を越えない最大の値をもつ要素番号
  real, intent(in), optional :: undeff  ! 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.
  integer :: just

  if(present(undeff))then
     just=int(undeff)
     call interpo_search_1d( x, pointx, i, real(just) )
     call interpo_search_1d( y, pointy, j, real(just) )
     call interpo_search_1d( z, pointz, k, real(just) )
  else
     call interpo_search_1d( x, pointx, i )
     call interpo_search_1d( y, pointy, j )
     call interpo_search_1d( z, pointz, k )
  end if

end subroutine interpo_search_3d
Subroutine :
tmin :real, intent(in)
: 内挿点の左端
tmax :real, intent(in)
: 内挿点の右端
xmin :real, intent(in)
: tmin での値
xmax :real, intent(in)
: tmax での値
point :real, intent(in)
: 内挿点
val :real, intent(inout)
: 内挿点での値

1 次の線形内挿ルーチン

[Source]

subroutine interpolation_1d( tmin, tmax, xmin, xmax, point, val )
  ! 1 次の線形内挿ルーチン
  implicit none
  real, intent(in) :: tmin  ! 内挿点の左端
  real, intent(in) :: tmax  ! 内挿点の右端
  real, intent(in) :: xmin  ! tmin での値
  real, intent(in) :: xmax  ! tmax での値
  real, intent(in) :: point  ! 内挿点
  real, intent(inout) :: val  ! 内挿点での値
  real :: fd, dt

  dt=point-tmin

  fd=(xmax-xmin)/(tmax-tmin)

  val=xmin+dt*fd

end subroutine interpolation_1d
Subroutine :
x(2) :real, intent(in)
: 内挿の空間点 x 方向の左右端
y(2) :real, intent(in)
: 内挿の空間点 y 方向の左右端
z(2,2) :real, intent(in)
: x, y での各点での値, (i,j) について, i<=x, j<=y
point(2) :real, intent(in)
: 内挿点 point(1)<=x 座標, point(2)<=y 座標
val :real, intent(inout)
: 内挿点での値

2 次の重線形内挿ルーチン 本ルーチンは直線直交座標空間でのみ使用可能.

[Source]

subroutine interpolation_2d( x, y, z, point, val )
  ! 2 次の重線形内挿ルーチン
  ! 本ルーチンは直線直交座標空間でのみ使用可能.
  implicit none
  real, intent(in) :: x(2)  ! 内挿の空間点 x 方向の左右端
  real, intent(in) :: y(2)  ! 内挿の空間点 y 方向の左右端
  real, intent(in) :: z(2,2)  ! x, y での各点での値, (i,j) について, i<=x, j<=y
  real, intent(in) :: point(2)  ! 内挿点 point(1)<=x 座標, point(2)<=y 座標
  real, intent(inout) :: val  ! 内挿点での値
  real :: valx(2)

  ! y(1) での x 方向の内挿点での値
  call interpolation_1d( x(1), x(2), z(1,1), z(2,1), point(1), valx(1) )

  ! y(2) での x 方向の内挿点での値
  call interpolation_1d( x(1), x(2), z(1,2), z(2,2), point(1), valx(2) )

  ! x の内挿点からの y 方向の内挿点での値(これが求める内挿点)
  call interpolation_1d( y(1), y(2), valx(1), valx(2), point(2), val )

end subroutine interpolation_2d
Subroutine :
x(2) :real, intent(in)
: 内挿の空間点 x 方向の左右端
y(2) :real, intent(in)
: 内挿の空間点 y 方向の左右端
z(2) :real, intent(in)
: 内挿の空間点 z 方向の左右端
u(2,2,2) :real, intent(in)
: x, y, z での各点での値, (i,j,k) について, i<=x, j<=y, k<=z
point(3) :real, intent(in)
: 内挿点 point(1)<=x 座標, point(2)<=y 座標, point(3)<=z 座標
val :real, intent(inout)
: 内挿点での値

3 次の重線形内挿ルーチン 本ルーチンは直線直交座標空間でのみ使用可能.

[Source]

subroutine interpolation_3d( x, y, z, u, point, val )
  ! 3 次の重線形内挿ルーチン
  ! 本ルーチンは直線直交座標空間でのみ使用可能.
  implicit none
  real, intent(in) :: x(2)  ! 内挿の空間点 x 方向の左右端
  real, intent(in) :: y(2)  ! 内挿の空間点 y 方向の左右端
  real, intent(in) :: z(2)  ! 内挿の空間点 z 方向の左右端
  real, intent(in) :: u(2,2,2)  ! x, y, z での各点での値, (i,j,k) について, i<=x, j<=y, k<=z
  real, intent(in) :: point(3)  ! 内挿点 point(1)<=x 座標, point(2)<=y 座標, point(3)<=z 座標
  real, intent(inout) :: val  ! 内挿点での値
  real :: valx(2)

  ! z(1) での x-y 平面での重線形内挿の値
  call interpolation_2d( x, y, u(:,:,1), point(1:2), valx(1) )

  ! z(2) での x 方向の内挿点での値
  call interpolation_2d( x, y, u(:,:,2), point(1:2), valx(2) )

  ! z(1) の内挿点からの z 方向の内挿点での値(これが求める内挿点)
  call interpolation_1d( z(1), z(2), valx(1), valx(2), point(3), val )

end subroutine interpolation_3d
Subroutine :
x(:) :real, intent(in)
: 漸増配列
point :real, intent(in)
: この点
i :integer, intent(inout)
: point の最近傍地点の要素番号
undeff :real, intent(in), optional
: 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.

1 次元最近傍探索ルーチン interpo_search_1d から値を求め, その値と +1 した値の距離を比較して 距離の短い方を選択する.

[Source]

subroutine nearest_search_1d( x, point, i, undeff )
  ! 1 次元最近傍探索ルーチン
  ! interpo_search_1d から値を求め, その値と +1 した値の距離を比較して
  ! 距離の短い方を選択する.
  implicit none
  real, intent(in) :: x(:)  ! 漸増配列
  real, intent(in) :: point  ! この点
  integer, intent(inout) :: i  ! point の最近傍地点の要素番号
  real, intent(in), optional :: undeff  ! 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.
  real :: tmp1, tmp2
  integer :: j

  if(present(undeff))then
     call interpo_search_1d( x, point, j, undeff )
  else
     call interpo_search_1d( x, point, j )
  end if

  tmp1=x(j)
  tmp2=x(j+1)

  if(abs(point-tmp1)>=abs(tmp2-point))then
     if(size(x)>=j+1)then  ! j+1 が配列参照外の場合の処理
        i=j+1
     else
        i=j
     end if
  else
     i=j
  end if

  if(j==0)then
     write(*,*) "WARNING: j is undef value."
  end if
  if(present(undeff))then
     if(j==int(undeff))then
        write(*,*) "WARNING: j is undef value."
     end if
  end if

end subroutine nearest_search_1d
Subroutine :
x(:) :real, intent(in)
: 漸増配列 x
y(:) :real, intent(in)
: 漸増配列 y
pointx :real, intent(in)
: この点 x
pointy :real, intent(in)
: この点 y
i :integer, intent(inout)
: pointx の最近要素番号
j :integer, intent(inout)
: pointy の最近要素番号
undeff :real, intent(in), optional
: 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.

2 次元最近傍探索ルーチン nearest_search_1d から値を求める. 本来, 2 次元であるため, 周囲 4 点の最近を計算する必要があるが, ここでは直交直線座標を考えているので, 各軸方向独立で最近点を計算し, どちらも最近の点が求めたい 2 次元の最近点となる.

[Source]

subroutine nearest_search_2d( x, y, pointx, pointy, i, j, undeff )
  ! 2 次元最近傍探索ルーチン
  ! nearest_search_1d から値を求める.
  ! 本来, 2 次元であるため, 周囲 4 点の最近を計算する必要があるが,
  ! ここでは直交直線座標を考えているので, 各軸方向独立で最近点を計算し,
  ! どちらも最近の点が求めたい 2 次元の最近点となる.
  implicit none
  real, intent(in) :: x(:)  ! 漸増配列 x
  real, intent(in) :: y(:)  ! 漸増配列 y
  real, intent(in) :: pointx  ! この点 x
  real, intent(in) :: pointy  ! この点 y
  integer, intent(inout) :: i  ! pointx の最近要素番号
  integer, intent(inout) :: j  ! pointy の最近要素番号
  real, intent(in), optional :: undeff  ! 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.

  if(present(undeff))then
     call nearest_search_1d( x, pointx, i, undeff )
     call nearest_search_1d( y, pointy, j, undeff )
  else
     call nearest_search_1d( x, pointx, i )
     call nearest_search_1d( y, pointy, j )
  end if

end subroutine nearest_search_2d
Subroutine :
x(:) :real, intent(in)
: 漸増配列 x
y(:) :real, intent(in)
: 漸増配列 y
z(:) :real, intent(in)
: 漸増配列 z
pointx :real, intent(in)
: この点 x
pointy :real, intent(in)
: この点 y
pointz :real, intent(in)
: この点 z
i :integer, intent(inout)
: pointx の最近要素番号
j :integer, intent(inout)
: pointy の最近要素番号
k :integer, intent(inout)
: pointz の最近要素番号
undeff :real, intent(in), optional
: 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.

2 次元最近傍探索ルーチン nearest_search_1d から値を求める. 本来, 2 次元であるため, 周囲 4 点の最近を計算する必要があるが, ここでは直交直線座標を考えているので, 各軸方向独立で最近点を計算し, どちらも最近の点が求めたい 2 次元の最近点となる.

[Source]

subroutine nearest_search_3d( x, y, z, pointx, pointy, pointz, i, j, k, undeff )
  ! 2 次元最近傍探索ルーチン
  ! nearest_search_1d から値を求める.
  ! 本来, 2 次元であるため, 周囲 4 点の最近を計算する必要があるが,
  ! ここでは直交直線座標を考えているので, 各軸方向独立で最近点を計算し,
  ! どちらも最近の点が求めたい 2 次元の最近点となる.
  implicit none
  real, intent(in) :: x(:)  ! 漸増配列 x
  real, intent(in) :: y(:)  ! 漸増配列 y
  real, intent(in) :: z(:)  ! 漸増配列 z
  real, intent(in) :: pointx  ! この点 x
  real, intent(in) :: pointy  ! この点 y
  real, intent(in) :: pointz  ! この点 z
  integer, intent(inout) :: i  ! pointx の最近要素番号
  integer, intent(inout) :: j  ! pointy の最近要素番号
  integer, intent(inout) :: k  ! pointz の最近要素番号
  real, intent(in), optional :: undeff  ! 探索範囲の配列要素より小さい値を探索しようとした際, undef を返すが, その undef 値を設定する. default では 0.
  integer :: just

  if(present(undeff))then
     call nearest_search_1d( x, pointx, i, undeff )
     call nearest_search_1d( y, pointy, j, undeff )
     call nearest_search_1d( z, pointz, k, undeff )
  else
     call nearest_search_1d( x, pointx, i )
     call nearest_search_1d( y, pointy, j )
     call nearest_search_1d( z, pointz, k )
  end if

end subroutine nearest_search_3d
Subroutine :
x(:) :real, intent(in)
: データ
anor :real, intent(inout)
: 標準偏差
error :real, intent(in), optional
: 欠損値

1 次元データの標準偏差を計算 標準偏差$sigma $の定義は, $$sigma =sum^{nx}_{i=1}{epsilon ^2} $$ ただし, $epsilon $は平均値からのずれ$x-\bar{x}$である.

[Source]

subroutine stand_vari( x, anor, error )  ! 1 次元データの標準偏差を計算
  ! 標準偏差$\sigma $の定義は,
  ! $$\sigma =\sum^{nx}_{i=1}{epsilon ^2} $$
  ! ただし, $\epsilon $は平均値からのずれ$x-\bar{x}$である.
  implicit none
  real, intent(in) :: x(:)  ! データ
  real, intent(inout) :: anor  ! 標準偏差
  real, intent(in), optional :: error  ! 欠損値
  integer :: i
  integer :: nx  ! データ数
  real :: an(size(x))

  nx=size(x)
  anor=0.0

  if(present(error))then
     call Anomaly_1d( x, an, error )
     do i=1,nx
        if(x(i)/=error)then
           anor=anor+an(i)**2
        end if
     end do
  else
     call Anomaly_1d( x, an )
     do i=1,nx
        anor=anor+an(i)**2
     end do
  end if

end subroutine stand_vari