module fileio

use sep

implicit none

contains
!=====================================================
subroutine io(v,m,nz,nx,nw,nreal)
character*2	:: v
integer		:: nx,nz
integer, optional	:: nw,nreal
real		:: m(:)

if(v.eq."mf") then
  call to_history("n1",nw,"m");call to_history("d1",1.,"m");call to_history("o1",1.,"m")
  call to_history("n2",nz,"m");call to_history("d2",1.,"m");call to_history("o2",1.,"m")
  call to_history("n3",nx,"m");call to_history("d3",1.,"m");call to_history("o3",1.,"m")
  call to_history("n4",nreal,"m");call to_history("d4",1.,"m");call to_history("o4",1.,"m")
  call to_history("label1","freq","m");call to_history("label2","z","m")
  call to_history("label3","x","m");call to_history("label4","realization","m")
  call srite("m",m,4*nw*nx*nz*nreal)
else if(v.eq."m0") then
  call to_history("n1",nw,"m0");call to_history("d1",1.,"m0");call to_history("o1",1.,"m0")
  call to_history("n2",nz,"m0");call to_history("d2",1.,"m0");call to_history("o2",1.,"m0")
  call to_history("n3",nx,"m0");call to_history("d3",1.,"m0");call to_history("o3",1.,"m0")
  call to_history("n4",nreal,"m0");call to_history("d4",1.,"m0");call to_history("o4",1.,"m0")
  call to_history("label1","freq","m0");call to_history("label2","z","m0")
  call to_history("label3","x","m0");call to_history("label4","realization","m0")
  call srite("m0",m,4*nw*nx*nz*nreal)
else if(v.eq."df") then
  call to_history("n1",nx*nz,"data");call to_history("d1",1.,"data");call to_history("o1",1.,"data");
  call to_history("n2",nx*nz,"data");call to_history("d2",1.,"data");call to_history("o2",1.,"data");
  call to_history("n3",1,"data");call to_history("d3",1.,"data");call to_history("o3",1.,"data");
  call srite("data",m,4*nx*nz*nx*nz)
else if(v.eq."d0") then
  call to_history("n1",nx*nz,"data0");call to_history("d1",1.,"data0");call to_history("o1",1.,"data0");
  call to_history("n2",nx*nz,"data0");call to_history("d2",1.,"data0");call to_history("o2",1.,"data0");
  call to_history("n3",1,"data0");call to_history("d3",1.,"data0");call to_history("o3",1.,"data0");
  call srite("data0",m,4*nx*nz*nx*nz)
end if

end subroutine
end module
