subroutine input_nc_initialise(iyr, mb)
use netcdf, only : nf90_open, nf90_nowrite, nf90_inq_varid
integer, intent(in) :: iyr
integer, intent(in) :: mb
integer :: i
logical :: fexists
character(len=path_max_length) :: fpaths(6)
call log_info('input_nc_initialise', 'Opening netCDF files...')
! allocate arrays with subbasin count
allocate(nc_wsum_sb(mb))
nc_wsum_sb = 0
allocate(nc_ncells_sb(mb))
nc_ncells_sb = 0
! read namelist parameters
read(get_config_fid(), nml=NC_CLIMATE_PARAMETERS)
! open nc files, get var ids and compare the dimensions of first file (0), check if others are the same (1)
do i = 1, 6
! check if file exists in input_dir, otherwise interpret as absolute path
INQUIRE(file = trim(input_dir) // trim(NC_FNAMES(i)), EXIST = fexists)
if (fexists) then
fpaths(i) = trim(input_dir) // trim(NC_FNAMES(i))
else
fpaths(i) = trim(NC_FNAMES(i))
endif
if (NC_DEBUG) call log_debug("input_nc_initialise", trim(fpaths(i))//' ('//trim(NC_VNAMES(i))//')')
! open file, get fileID, check dims and attributes
call input_nc_check_error( nf90_open(trim(fpaths(i)), nf90_nowrite, nc_ids(i)) )
call input_nc_check_error( nf90_inq_varid(nc_ids(i), trim(NC_VNAMES(i)), nc_var_ids(i)) )
call input_nc_check_dims(nc_ids(i), i /= 1, trim(NC_VNAMES(i)))
call input_nc_check_attr(i)
enddo
! check ncgrid.dat, count rows (nc_nrows) and max cell per subbasins (nc_mxc)
call input_nc_check_grid(mb)
! allocated arrays with nc_mxc
allocate(nc_weight_sb(mb, nc_mxc))
nc_weight_sb = 0
allocate(nc_lon_sb(mb, nc_mxc))
nc_lon_sb = 0
allocate(nc_lat_sb(mb, nc_mxc))
nc_lat_sb = 0
allocate(nc_x_sb(mb, nc_mxc))
nc_x_sb = 0
allocate(nc_y_sb(mb, nc_mxc))
nc_y_sb = 0
! read ncinfo.dat
call input_nc_read_grid(mb)
! convert lon/lat to x/y indecies
call input_nc_convert_coordinates(mb)
! find time offset
if (NC_REF_YEAR >= 0) then
call input_nc_find_time(iyr) ! resets nc_nday
else if (NC_OFFSET_DAYS /= 0) then
call input_nc_offset_time() ! adds to nc_nday
endif
! allocate nc_var_in to read in
allocate(nc_var_in(nc_nx, nc_ny))
nc_var_in = 0
! check for missing values
if (NC_DEBUG) call input_nc_check_missing(mb)
end subroutine input_nc_initialise