Skip to content

Commit 9355e92

Browse files
authored
Merge pull request #7 from cd10kfsu/feature/cda/add_ncio_create_nc
add funcs to create nc files in m_ncio.f90
2 parents fbdc315 + ff723e6 commit 9355e92

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

libsrc/fast/nc/m_ncio.f90

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ MODULE m_ncio
1717
IMPLICIT NONE
1818
PRIVATE
1919

20-
! PUBLIC :: NF90_DOUBLE, NF90_FLOAT, NF90_INT, NF90_SHORT, NF90_BYTE
20+
PUBLIC :: NF90_DOUBLE, NF90_FLOAT, NF90_INT, NF90_SHORT, NF90_BYTE
2121

2222
! open/close file
2323
PUBLIC :: nc_get_fid, nc_close_fid
2424

25+
! Create new file/dim/var
26+
PUBLIC :: nc_create_file, nc_create_dim, nc_create_var
27+
28+
! End def mode
29+
PUBLIC :: nc_end_create
30+
2531
! Read Attributes
2632
PUBLIC :: nc_rdatt
2733

2834
! low-level read Attributes
2935
PUBLIC :: nc_rdatt_str, nc_rdatt_i1, nc_rdatt_i2, nc_rdatt_i4, &
3036
nc_rdatt_r4, nc_rdatt_r8
37+
3138
! Get dimension
3239
PUBLIC :: nc_rddim
3340

@@ -133,6 +140,10 @@ MODULE m_ncio
133140
INTEGER(i1) :: attval = 4
134141
INTEGER(i1) :: dimid = 5
135142
INTEGER(i1) :: dimval = 6
143+
INTEGER(i1) :: genfile = 7
144+
INTEGER(i1) :: gendim = 8
145+
INTEGER(i1) :: genvar = 9
146+
INTEGER(i1) :: endgen = 10
136147
INTEGER(i1) :: undef = 127 ! max positive for signed 8-byte int
137148
END TYPE
138149
TYPE(t_errcode),SAVE,PRIVATE:: errcode
@@ -153,6 +164,45 @@ SUBROUTINE mystop(errcode)
153164
END SUBROUTINE
154165

155166

167+
168+
!--------------------------------------------------------------------------------
169+
! end def mode
170+
!--------------------------------------------------------------------------------
171+
SUBROUTINE nc_end_create(fid)
172+
IMPLICIT NONE
173+
174+
INTEGER(i4),INTENT(IN) :: fid
175+
176+
INTEGER(i4) :: istat
177+
178+
istat = nf90_enddef(fid)
179+
if (istat /= NF90_NOERR) then
180+
write(lout_log,*) "[err] nc_end_create: cannot close def mode for fid:", fid
181+
call mystop(errcode%endgen)
182+
end if
183+
184+
END SUBROUTINE
185+
186+
187+
!--------------------------------------------------------------------------------
188+
! create nc file
189+
!--------------------------------------------------------------------------------
190+
SUBROUTINE nc_create_file(filename, fid)
191+
IMPLICIT NONE
192+
193+
CHARACTER(*),INTENT(IN) :: filename
194+
INTEGER(i4), INTENT(OUT) :: fid
195+
196+
INTEGER(i4) :: istat
197+
198+
istat = nf90_create(trim(filename), NF90_CLOBBER, fid)
199+
if (istat /= NF90_NOERR) then
200+
write(lout_log,*) "[err] nc_create_file: cannot get fid for file", trim(filename)
201+
call mystop(errcode%genfile)
202+
end if
203+
204+
END SUBROUTINE nc_create_file
205+
156206
!--------------------------------------------------------------------------------
157207
! open/close nc file
158208
!--------------------------------------------------------------------------------
@@ -196,6 +246,29 @@ SUBROUTINE nc_close_fid(fid)
196246

197247
END SUBROUTINE nc_close_fid
198248

249+
250+
!--------------------------------------------------------------------------------
251+
! create vars
252+
!--------------------------------------------------------------------------------
253+
SUBROUTINE nc_create_var(fid, varname, vartype, varsize, varid)
254+
IMPLICIT NONE
255+
256+
INTEGER(i4), INTENT(IN) :: fid
257+
CHARACTER(*),INTENT(IN) :: varname
258+
INTEGER(i4), INTENT(IN) :: vartype
259+
INTEGER(i4), INTENT(IN) :: varsize(:)
260+
INTEGER(i4), INTENT(OUT) :: varid
261+
262+
INTEGER(i4) :: istat
263+
264+
istat = nf90_def_var(fid, trim(varname), vartype, varsize, varid)
265+
if (istat /= NF90_NOERR) then
266+
write(lout_log,*) "[err] nc_create_vars: cannot create var:", trim(varname)
267+
call mystop(errcode%genvar)
268+
end if
269+
270+
END SUBROUTINE
271+
199272
!--------------------------------------------------------------------------------
200273
! read 1D
201274
!--------------------------------------------------------------------------------
@@ -605,6 +678,33 @@ SUBROUTINE nc_rdatt_r8(fid, varname, attname, attval)
605678
include "nc_rdatt.f90.inc"
606679
END SUBROUTINE
607680

681+
!--------------------------------------------------------------------------------
682+
! define dimension
683+
!--------------------------------------------------------------------------------
684+
SUBROUTINE nc_create_dim(fid, dimname, dimsize, lunlimited, dimid)
685+
IMPLICIT NONE
686+
687+
INTEGER(i4), INTENT(IN) :: fid
688+
CHARACTER(*),INTENT(IN) :: dimname
689+
INTEGER(i4), INTENT(IN) :: dimsize
690+
LOGICAL, INTENT(IN) :: lunlimited
691+
INTEGER(i4), INTENT(OUT) :: dimid
692+
693+
INTEGER :: istat
694+
695+
if (lunlimited) then
696+
istat = nf90_def_dim(fid, trim(dimname), NF90_UNLIMITED, dimid)
697+
else
698+
istat = nf90_def_dim(fid, trim(dimname), dimsize, dimid)
699+
end if
700+
if (istat /= NF90_NOERR) then
701+
write(lout_log,*) "[err] nc_create_dim: cannot create dim:"//trim(dimname)
702+
call mystop(errcode%gendim)
703+
end if
704+
705+
706+
END SUBROUTINE
707+
608708

609709
!--------------------------------------------------------------------------------
610710
! read dimension

0 commit comments

Comments
 (0)