write_pedestrian_wind_heights_nc Subroutine

public subroutine write_pedestrian_wind_heights_nc(ncid, heights)

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: ncid
real, intent(in) :: heights(:)

Calls

proc~~write_pedestrian_wind_heights_nc~~CallsGraph proc~write_pedestrian_wind_heights_nc write_pedestrian_wind_heights_nc nf90_def_dim nf90_def_dim proc~write_pedestrian_wind_heights_nc->nf90_def_dim nf90_def_var nf90_def_var proc~write_pedestrian_wind_heights_nc->nf90_def_var nf90_enddef nf90_enddef proc~write_pedestrian_wind_heights_nc->nf90_enddef nf90_get_var nf90_get_var proc~write_pedestrian_wind_heights_nc->nf90_get_var nf90_inq_dimid nf90_inq_dimid proc~write_pedestrian_wind_heights_nc->nf90_inq_dimid nf90_inq_varid nf90_inq_varid proc~write_pedestrian_wind_heights_nc->nf90_inq_varid nf90_inquire_dimension nf90_inquire_dimension proc~write_pedestrian_wind_heights_nc->nf90_inquire_dimension nf90_inquire_variable nf90_inquire_variable proc~write_pedestrian_wind_heights_nc->nf90_inquire_variable nf90_put_att nf90_put_att proc~write_pedestrian_wind_heights_nc->nf90_put_att nf90_put_var nf90_put_var proc~write_pedestrian_wind_heights_nc->nf90_put_var nf90_redef nf90_redef proc~write_pedestrian_wind_heights_nc->nf90_redef nf90_sync nf90_sync proc~write_pedestrian_wind_heights_nc->nf90_sync proc~nchandle_error nchandle_error proc~write_pedestrian_wind_heights_nc->proc~nchandle_error nf90_strerror nf90_strerror proc~nchandle_error->nf90_strerror

Called by

proc~~write_pedestrian_wind_heights_nc~~CalledByGraph proc~write_pedestrian_wind_heights_nc write_pedestrian_wind_heights_nc proc~stats_define_pedestrian_wind stats_define_pedestrian_wind proc~stats_define_pedestrian_wind->proc~write_pedestrian_wind_heights_nc proc~stats_createnc_tavg stats_createnc_tavg proc~stats_createnc_tavg->proc~stats_define_pedestrian_wind proc~stats_createnc_tkslice stats_createnc_tkslice proc~stats_createnc_tkslice->proc~stats_define_pedestrian_wind proc~stats_prepare stats_prepare proc~stats_prepare->proc~stats_createnc_tavg proc~stats_prepare->proc~stats_createnc_tkslice proc~stats_main stats_main proc~stats_main->proc~stats_prepare program~udales uDALES program~udales->proc~stats_main

Source Code

  subroutine write_pedestrian_wind_heights_nc(ncid, heights)
    implicit none
    integer, intent(in) :: ncid
    real, intent(in) :: heights(:)
    integer :: status, height_dimid, height_varid, wind_varid, xt_dimid, yt_dimid, time_dimid
    integer :: ndims, stored_count, dimids(NF90_MAX_VAR_DIMS)
    real, allocatable :: stored_heights(:)
    logical :: height_exists

    status = nf90_inq_varid(ncid, 'receptor_height', height_varid)
    height_exists = status == NF90_NOERR
    if (height_exists) then
      status = nf90_inquire_variable(ncid, height_varid, ndims=ndims, dimids=dimids)
      call nchandle_error(status)
      if (ndims /= 1) then
        write(0,*) 'ERROR: Existing receptor_height is scalar; cannot use multiple heights.'
        stop 1
      end if
      height_dimid = dimids(1)
      status = nf90_inquire_dimension(ncid, height_dimid, len=stored_count)
      call nchandle_error(status)
      if (stored_count /= size(heights)) then
        write(0,*) 'ERROR: Existing receptor_height count differs from configured count.'
        stop 1
      end if
      allocate(stored_heights(stored_count))
      status = nf90_get_var(ncid, height_varid, stored_heights)
      call nchandle_error(status)
      if (any(abs(stored_heights-heights) > &
          100.*epsilon(1.)*max(1.,abs(stored_heights),abs(heights)))) then
        write(0,*) 'ERROR: Existing receptor_height values differ from configured heights.'
        stop 1
      end if
    else if (status /= NF90_ENOTVAR) then
      call nchandle_error(status)
    end if

    status = nf90_inq_dimid(ncid, 'xt', xt_dimid)
    call nchandle_error(status)
    status = nf90_inq_dimid(ncid, 'yt', yt_dimid)
    call nchandle_error(status)
    status = nf90_inq_dimid(ncid, 'time', time_dimid)
    call nchandle_error(status)

    status = nf90_redef(ncid)
    call nchandle_error(status)
    if (.not. height_exists) then
      status = nf90_def_dim(ncid, 'receptor_height', size(heights), height_dimid)
      call nchandle_error(status)
      status = nf90_def_var(ncid, 'receptor_height', NF90_FLOAT, (/height_dimid/), height_varid)
      call nchandle_error(status)
    end if
    status = nf90_put_att(ncid, height_varid, 'longname', 'Heights above model ground used for ws_local')
    call nchandle_error(status)
    status = nf90_put_att(ncid, height_varid, 'standard_name', 'height')
    call nchandle_error(status)
    status = nf90_put_att(ncid, height_varid, 'units', 'm')
    call nchandle_error(status)
    status = nf90_put_att(ncid, height_varid, 'positive', 'up')
    call nchandle_error(status)

    status = nf90_inq_varid(ncid, 'ws_local', wind_varid)
    if (status == NF90_ENOTVAR) then
      status = nf90_def_var(ncid, 'ws_local', NF90_FLOAT, &
                            (/xt_dimid,yt_dimid,height_dimid,time_dimid/), wind_varid)
      call nchandle_error(status)
      status = nf90_put_att(ncid, wind_varid, '_FillValue', nc_fillvalue)
      call nchandle_error(status)
    else
      call nchandle_error(status)
      status = nf90_inquire_variable(ncid, wind_varid, ndims=ndims, dimids=dimids)
      call nchandle_error(status)
      if (ndims /= 4) then
        write(0,*) 'ERROR: Existing ws_local has incompatible dimensions for multiple heights.'
        stop 1
      end if
      if (any(dimids(1:4) /= (/xt_dimid,yt_dimid,height_dimid,time_dimid/))) then
        write(0,*) 'ERROR: Existing ws_local dimension order differs from configured output.'
        stop 1
      end if
    end if
    status = nf90_put_att(ncid, wind_varid, 'longname', &
                          'Time-mean horizontal wind speed at configured heights above model ground')
    call nchandle_error(status)
    status = nf90_put_att(ncid, wind_varid, 'units', 'm/s')
    call nchandle_error(status)
    status = nf90_put_att(ncid, wind_varid, 'coordinates', 'receptor_height')
    call nchandle_error(status)
    status = nf90_enddef(ncid)
    call nchandle_error(status)
    status = nf90_put_var(ncid, height_varid, heights)
    call nchandle_error(status)
    status = nf90_sync(ncid)
    call nchandle_error(status)
  end subroutine write_pedestrian_wind_heights_nc