Skip to content

Commit

Permalink
Merge pull request #7 from cd10kfsu/feature/cda/add_ncio_create_nc
Browse files Browse the repository at this point in the history
add funcs to create nc files in m_ncio.f90
  • Loading branch information
cd10kfsu committed Jul 7, 2024
2 parents fbdc315 + ff723e6 commit 9355e92
Showing 1 changed file with 101 additions and 1 deletion.
102 changes: 101 additions & 1 deletion libsrc/fast/nc/m_ncio.f90
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@ MODULE m_ncio
IMPLICIT NONE
PRIVATE

! PUBLIC :: NF90_DOUBLE, NF90_FLOAT, NF90_INT, NF90_SHORT, NF90_BYTE
PUBLIC :: NF90_DOUBLE, NF90_FLOAT, NF90_INT, NF90_SHORT, NF90_BYTE

! open/close file
PUBLIC :: nc_get_fid, nc_close_fid

! Create new file/dim/var
PUBLIC :: nc_create_file, nc_create_dim, nc_create_var

! End def mode
PUBLIC :: nc_end_create

! Read Attributes
PUBLIC :: nc_rdatt

! low-level read Attributes
PUBLIC :: nc_rdatt_str, nc_rdatt_i1, nc_rdatt_i2, nc_rdatt_i4, &
nc_rdatt_r4, nc_rdatt_r8

! Get dimension
PUBLIC :: nc_rddim

Expand Down Expand Up @@ -133,6 +140,10 @@ MODULE m_ncio
INTEGER(i1) :: attval = 4
INTEGER(i1) :: dimid = 5
INTEGER(i1) :: dimval = 6
INTEGER(i1) :: genfile = 7
INTEGER(i1) :: gendim = 8
INTEGER(i1) :: genvar = 9
INTEGER(i1) :: endgen = 10
INTEGER(i1) :: undef = 127 ! max positive for signed 8-byte int
END TYPE
TYPE(t_errcode),SAVE,PRIVATE:: errcode
Expand All @@ -153,6 +164,45 @@ SUBROUTINE mystop(errcode)
END SUBROUTINE



!--------------------------------------------------------------------------------
! end def mode
!--------------------------------------------------------------------------------
SUBROUTINE nc_end_create(fid)
IMPLICIT NONE

INTEGER(i4),INTENT(IN) :: fid

INTEGER(i4) :: istat

istat = nf90_enddef(fid)
if (istat /= NF90_NOERR) then
write(lout_log,*) "[err] nc_end_create: cannot close def mode for fid:", fid
call mystop(errcode%endgen)
end if

END SUBROUTINE


!--------------------------------------------------------------------------------
! create nc file
!--------------------------------------------------------------------------------
SUBROUTINE nc_create_file(filename, fid)
IMPLICIT NONE

CHARACTER(*),INTENT(IN) :: filename
INTEGER(i4), INTENT(OUT) :: fid

INTEGER(i4) :: istat

istat = nf90_create(trim(filename), NF90_CLOBBER, fid)
if (istat /= NF90_NOERR) then
write(lout_log,*) "[err] nc_create_file: cannot get fid for file", trim(filename)
call mystop(errcode%genfile)
end if

END SUBROUTINE nc_create_file

!--------------------------------------------------------------------------------
! open/close nc file
!--------------------------------------------------------------------------------
Expand Down Expand Up @@ -196,6 +246,29 @@ SUBROUTINE nc_close_fid(fid)

END SUBROUTINE nc_close_fid


!--------------------------------------------------------------------------------
! create vars
!--------------------------------------------------------------------------------
SUBROUTINE nc_create_var(fid, varname, vartype, varsize, varid)
IMPLICIT NONE

INTEGER(i4), INTENT(IN) :: fid
CHARACTER(*),INTENT(IN) :: varname
INTEGER(i4), INTENT(IN) :: vartype
INTEGER(i4), INTENT(IN) :: varsize(:)
INTEGER(i4), INTENT(OUT) :: varid

INTEGER(i4) :: istat

istat = nf90_def_var(fid, trim(varname), vartype, varsize, varid)
if (istat /= NF90_NOERR) then
write(lout_log,*) "[err] nc_create_vars: cannot create var:", trim(varname)
call mystop(errcode%genvar)
end if

END SUBROUTINE

!--------------------------------------------------------------------------------
! read 1D
!--------------------------------------------------------------------------------
Expand Down Expand Up @@ -605,6 +678,33 @@ SUBROUTINE nc_rdatt_r8(fid, varname, attname, attval)
include "nc_rdatt.f90.inc"
END SUBROUTINE

!--------------------------------------------------------------------------------
! define dimension
!--------------------------------------------------------------------------------
SUBROUTINE nc_create_dim(fid, dimname, dimsize, lunlimited, dimid)
IMPLICIT NONE

INTEGER(i4), INTENT(IN) :: fid
CHARACTER(*),INTENT(IN) :: dimname
INTEGER(i4), INTENT(IN) :: dimsize
LOGICAL, INTENT(IN) :: lunlimited
INTEGER(i4), INTENT(OUT) :: dimid

INTEGER :: istat

if (lunlimited) then
istat = nf90_def_dim(fid, trim(dimname), NF90_UNLIMITED, dimid)
else
istat = nf90_def_dim(fid, trim(dimname), dimsize, dimid)
end if
if (istat /= NF90_NOERR) then
write(lout_log,*) "[err] nc_create_dim: cannot create dim:"//trim(dimname)
call mystop(errcode%gendim)
end if


END SUBROUTINE


!--------------------------------------------------------------------------------
! read dimension
Expand Down

0 comments on commit 9355e92

Please sign in to comment.