@@ -716,6 +716,8 @@ end subroutine check_dimorder
716716 ! --------------------------------
717717 subroutine check_variable_attributes (ncid , varid , unit , direction , missing_val )
718718
719+ use , intrinsic :: ieee_arithmetic
720+
719721 implicit none
720722 integer , intent (in ) :: ncid
721723 integer , intent (in ) :: varid
@@ -724,14 +726,18 @@ subroutine check_variable_attributes(ncid, varid, unit, direction, missing_val)
724726 ! local parameters
725727 integer :: num_atts,i
726728 character (len= 100 ) :: name,value
727- real (kind= CUSTOM_REAL) :: fill_val
729+ real (kind= CUSTOM_REAL) :: fill_val,val_read
728730 logical :: has_fill_value,has_missing_value
729731
732+ ! note: isNaN() function is a GNU extension, thus not all compilers might have it.
733+ ! using a simple val /= val check can already work.
734+ ! here, we double-check with the IEEE intrinsic function ieee_is_nan() which should be Fortran2003 standard.
735+
730736 ! initializes
731737 unit = 0
732738 direction = 0
733- missing_val = 9999 .0_CUSTOM_REAL
734- fill_val = 9999 .0_CUSTOM_REAL
739+ missing_val = 99999 .0_CUSTOM_REAL ! initializes to some ficticious value
740+ fill_val = 99999 .0_CUSTOM_REAL
735741
736742 has_missing_value = .false.
737743 has_fill_value = .false.
@@ -804,22 +810,47 @@ subroutine check_variable_attributes(ncid, varid, unit, direction, missing_val)
804810 endif
805811
806812 else if (trim (name) == ' missing_value' ) then
813+ ! initialize to avoid issues with netcdf function call
814+ val_read = 0.0_CUSTOM_REAL
807815 ! assigns value for invalid entries
808- call check_status(nf90_get_att(ncid, varid, name, missing_val))
809- if (VERBOSE) print * ,' missing value: ' ,missing_val
816+ call check_status(nf90_get_att(ncid, varid, name, val_read))
817+ if (VERBOSE) print * ,' missing value: ' ,val_read
818+ ! to avoid compiler running into floating-point invalid errors, we double-check if value is not-a-number
810819 has_missing_value = .true.
820+ ! isNaN check
821+ if (val_read /= val_read) then
822+ ! NaN value
823+ has_missing_value = .false.
824+ else
825+ has_missing_value = .true.
826+ endif
827+ ! double-check with ieee function
828+ if (ieee_support_standard(val_read)) then
829+ if (ieee_is_nan(val_read)) has_missing_value = .false.
830+ endif
831+ ! only assign if not NaN
832+ if (has_missing_value) missing_val = val_read
811833
812834 else if (trim (name) == ' _FillValue' .or. trim (name) == ' FillValue' ) then
835+ ! initialize to avoid issues with netcdf function call
836+ val_read = 0.0_CUSTOM_REAL
813837 ! assigns value for invalid entries
814- call check_status(nf90_get_att(ncid, varid, name, fill_val))
815- if (VERBOSE) print * ,' fill value: ' ,fill_val
838+ call check_status(nf90_get_att(ncid, varid, name, val_read))
839+ if (VERBOSE) print * ,' fill value: ' ,val_read
840+ ! to avoid compiler running into floating-point invalid errors, we double-check if value is not-a-number
841+ has_fill_value = .true.
816842 ! isNaN check
817- if (fill_val /= fill_val ) then
843+ if (val_read /= val_read ) then
818844 ! NaN value
819845 has_fill_value = .false.
820846 else
821847 has_fill_value = .true.
822848 endif
849+ ! double-check with ieee function
850+ if (ieee_support_standard(val_read)) then
851+ if (ieee_is_nan(val_read)) has_fill_value = .false.
852+ endif
853+ if (has_fill_value) fill_val = val_read
823854
824855 else if (trim (name) == ' long_name' .or. trim (name) == ' _long_name' ) then
825856 ! assigns value for invalid entries
@@ -2887,6 +2918,22 @@ subroutine read_emc_model()
28872918 double precision , parameter :: RCMB_ = 3480000.d0
28882919 double precision , parameter :: R_EARTH_ = 6371000.d0
28892920
2921+ ! initializes
2922+ dir_dep = 0
2923+ dir = 0
2924+
2925+ missing_val_vp = 99999.0_CUSTOM_REAL ! initializes to some ficticious value
2926+ missing_val_vs = 99999.0_CUSTOM_REAL
2927+ missing_val_rho = 99999.0_CUSTOM_REAL
2928+ missing_val_dep = 99999.0_CUSTOM_REAL
2929+
2930+ missing_val_vpv = 99999.0_CUSTOM_REAL
2931+ missing_val_vph = 99999.0_CUSTOM_REAL
2932+ missing_val_vsv = 99999.0_CUSTOM_REAL
2933+ missing_val_vsh = 99999.0_CUSTOM_REAL
2934+ missing_val_eta = 99999.0_CUSTOM_REAL
2935+ missing_val_qmu = 99999.0_CUSTOM_REAL
2936+
28902937 if (VERBOSE) print * ,' reading EMC model:'
28912938
28922939 ! Open netcdf file with write access
0 commit comments