Skip to content

Commit 82e834d

Browse files
committed
simplify error statements
1 parent 953ffbd commit 82e834d

File tree

7 files changed

+35
-67
lines changed

7 files changed

+35
-67
lines changed

src/attributes.f90

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
module procedure write_attribute
1010
integer :: varid, ier
1111

12-
if(.not.self%is_open) error stop 'ERROR:nc4fortran:write_attribute: file handle not open'
12+
if(.not.self%is_open) error stop 'nc4fortran:write_attribute: file handle not open'
1313

1414
ier = nf90_inq_varid(self%ncid, dname, varid)
1515

@@ -29,18 +29,15 @@
2929
endif
3030

3131
if (present(ierr)) ierr = ier
32-
if (check_error(ier, dname)) then
33-
if (present(ierr)) return
34-
error stop
35-
endif
32+
if (check_error(ier, dname) .and. .not. present(ierr)) error stop 'nc4fortran:attributes: failed to write' // attrname
3633

3734
end procedure write_attribute
3835

3936

4037
module procedure read_attribute
4138
integer :: varid, ier
4239

43-
if(.not.self%is_open) error stop 'ERROR:nc4fortran:read_attribute: file handle not open'
40+
if(.not.self%is_open) error stop 'nc4fortran:read_attribute: file handle not open'
4441

4542
ier = nf90_inq_varid(self%ncid, dname, varid)
4643

@@ -60,13 +57,9 @@
6057
endif
6158

6259
if (present(ierr)) ierr = ier
63-
if (check_error(ier, dname)) then
64-
if (present(ierr)) return
65-
error stop
66-
endif
60+
if (check_error(ier, dname) .and. .not. present(ierr)) error stop 'nc4fortran:attributes: failed to read' // attrname
6761

6862
end procedure read_attribute
6963

7064

71-
7265
end submodule attributes

src/interface.f90

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,14 @@ subroutine nc_initialize(self,filename,ierr, status,action,comp_lvl,verbose,debu
526526
end select
527527

528528
if (present(ierr)) ierr = ier
529-
if (ier /= NF90_NOERR) then
530-
write(stderr,*) 'ERROR:initialize ' // filename // ' could not be created'
531-
if (present(ierr)) return
532-
error stop
529+
if (ier == NF90_NOERR) then
530+
self%is_open = .true.
531+
return
533532
endif
534533

535-
self%is_open = .true.
534+
write(stderr,*) 'ERROR:initialize ' // filename // ' could not be created'
535+
if (present(ierr)) return
536+
error stop
536537

537538
end subroutine nc_initialize
538539

@@ -588,15 +589,11 @@ logical function is_contig(self, dname)
588589
integer :: ier, varid
589590

590591
ier = nf90_inq_varid(self%ncid, dname, varid)
591-
if (ier/=NF90_NOERR) then
592-
write(stderr,*) 'ERROR:nc4fortran:is_contig: cannot find variable: ' // dname
593-
error stop
594-
endif
592+
if (ier/=NF90_NOERR) error stop 'nc4fortran:is_contig: cannot find variable: ' // dname
593+
595594
ier = nf90_inquire_variable(self%ncid, varid, contiguous=is_contig)
596-
if (ier/=NF90_NOERR) then
597-
write(stderr,*) 'ERROR:nc4fortran:is_contig: cannot get variable properties'
598-
error stop
599-
endif
595+
if (ier/=NF90_NOERR) error stop 'nc4fortran:is_contig: cannot get variable properties' // dname
596+
600597
end function is_contig
601598

602599

@@ -606,15 +603,11 @@ logical function is_chunked(self, dname)
606603
integer :: ier, varid
607604

608605
ier = nf90_inq_varid(self%ncid, dname, varid)
609-
if (ier/=NF90_NOERR) then
610-
write(stderr,*) 'ERROR:nc4fortran:is_chunked: cannot find variable: ' // dname
611-
error stop
612-
endif
606+
if (ier/=NF90_NOERR) error stop 'nc4fortran:is_chunked: cannot find variable: ' // dname
607+
613608
ier = nf90_inquire_variable(self%ncid, varid, contiguous=is_chunked)
614-
if (ier/=NF90_NOERR) then
615-
write(stderr,*) 'ERROR:nc4fortran:is_chunked: cannot get variable properties'
616-
error stop
617-
endif
609+
if (ier/=NF90_NOERR) error stop 'nc4fortran:is_chunked: cannot get variable properties' // dname
610+
618611
is_chunked = .not.is_chunked
619612
end function is_chunked
620613

@@ -629,18 +622,15 @@ subroutine get_chunk(self, dname, chunk_size)
629622
chunk_size = -1
630623

631624
i = nf90_inq_varid(self%ncid, dname, varid)
632-
if (i/=NF90_NOERR) then
633-
write(stderr,*) 'ERROR:nc4fortran:chunk: cannot find variable: ' // dname
634-
return
635-
endif
625+
if (i/=NF90_NOERR) error stop 'nc4fortran:chunk: cannot find variable: ' // dname
626+
636627
i = nf90_inquire_variable(self%ncid, varid, contiguous=contig)
637-
if (i/=NF90_NOERR) then
638-
write(stderr,*) 'nc4fortran:chunk: cannot get variable properties'
639-
return
640-
endif
628+
if (i/=NF90_NOERR) error stop 'nc4fortran:chunk: cannot get variable properties' // dname
629+
641630
if(contig) return
642631
i = nf90_inquire_variable(self%ncid, varid, chunksizes=chunk_size)
643-
if (i/=NF90_NOERR) write(stderr,*) 'nc4fortran:chunk: cannot get variable properties'
632+
if (i/=NF90_NOERR) error stop 'nc4fortran:chunk: cannot get variable properties' // dname
633+
644634
end subroutine get_chunk
645635

646636

src/read.f90

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99
module procedure get_ndims
1010
integer :: varid, ierr
1111

12-
if(.not.self%is_open) error stop 'ERROR:nc4fortran:read: file handle not open'
12+
if(.not.self%is_open) error stop 'nc4fortran:read: file handle not open'
1313

1414
drank = -1
1515

1616
ierr = nf90_inq_varid(self%ncid, dname, varid)
17-
if(ierr/=NF90_NOERR) then
18-
write(stderr,*) 'nc4fortran:get_ndims: could not get variable ID for ' // dname
19-
error stop
20-
endif
17+
if(ierr/=NF90_NOERR) error stop 'nc4fortran:get_ndims: could not get variable ID for ' // dname
2118

2219
ierr = nf90_inquire_variable(self%ncid, varid, ndims=drank)
2320

@@ -34,16 +31,10 @@
3431
allocate(dimids(N), dims(N))
3532

3633
ier = nf90_inq_varid(self%ncid, dname, varid)
37-
if(check_error(ier, dname)) then
38-
write(stderr,*) 'nc4fortran:get_shape: could not get variable ID for: '//dname
39-
error stop
40-
endif
34+
if(check_error(ier, dname)) error stop 'nc4fortran:get_shape: could not get variable ID for: ' // dname
4135

4236
ier = nf90_inquire_variable(self%ncid, varid, dimids = dimids)
43-
if(check_error(ier, dname)) then
44-
write(stderr,*) 'nc4fortran:get_shape: could not get dimension IDs for: '//dname
45-
error stop
46-
endif
37+
if(check_error(ier, dname)) error stop 'nc4fortran:get_shape: could not get dimension IDs for: ' // dname
4738

4839
if (present(dimnames)) allocate(tempnames(N))
4940

src/reader.in.f90

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
module procedure nc_read_scalar
99
integer :: varid, ier
1010

11-
if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open'
11+
if(.not.self%is_open) error stop 'nc4fortran:reader file handle not open'
1212

1313
ier = nf90_inq_varid(self%ncid, dname, varid)
1414

@@ -40,10 +40,8 @@
4040
endif
4141

4242
if (present(ierr)) ierr = ier
43-
if (check_error(ier, dname)) then
44-
if (present(ierr)) return
45-
error stop
46-
endif
43+
if (check_error(ier, dname) .and. .not. present(ierr)) error stop 'nc4fortran:read failed ' // dname
44+
4745
end procedure nc_read_scalar
4846

4947

src/reader_template.in.f90

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
integer :: varid, ier, drank
22

3-
4-
if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open'
3+
if(.not.self%is_open) error stop 'nc4fortran:reader file handle not open'
54

65
ier = nf90_inq_varid(self%ncid, dname, varid)
76

@@ -30,7 +29,4 @@
3029
endif
3130

3231
if (present(ierr)) ierr = ier
33-
if (check_error(ier, dname)) then
34-
if (present(ierr)) return
35-
error stop
36-
endif
32+
if (check_error(ier, dname) .and. .not. present(ierr)) error stop 'nc4fortran:read failed to read ' // dname

src/write.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
integer :: i, L
1111
character(NF90_MAX_NAME) :: name
1212

13-
if(.not.self%is_open) error stop 'ERROR:nc4fortran:write: file handle not open'
13+
if(.not.self%is_open) error stop 'nc4fortran:write: file handle not open'
1414

1515
do i=1,size(dims)
1616
if (present(dimnames)) then

src/writer.in.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
module procedure nc_write_scalar_char
1111
integer :: varid, ier, lenid
1212

13-
if(.not.self%is_open) error stop 'ERROR:nc4fortran:writer: file handle not open'
13+
if(.not.self%is_open) error stop 'nc4fortran:writer: file handle not open'
1414

1515
!! uses string prefill method
1616
!! https://www.unidata.ucar.edu/software/netcdf/docs-fortran/f90-variables.html#f90-reading-and-writing-character-string-values

0 commit comments

Comments
 (0)