model {
# No noise for ni_gam_decomp
    for (i in 1:n_obs) {
    # Main likelihood
      y[i] ~ dnorm(mu_y[i], sigmasq_all[i]^-1)
      sigmasq_all[i] <- sigma_y^2 + y_err[i]^2

    # Mean structure
      mu_y[i] <- r[i] + g_z_x[i] + h_z_x[i]
      #mu_y[i] <-  g_z_x[i] + h_z_x[i]

    # Site specific vertical offset
      h_z_x[i] <- intercept[site[i]]

    # Linear Local component
      g_z_x[i] <- t[i] * b_g[site[i]]

    # Linear Local component + Site specific vertical offset
      g_h_z_x[i] <- h_z_x[i] + g_z_x[i]
    }

    # Regional term
    r <- B_t %*% b_t
    for (j in 1:n_knots_t) {
      b_t[j] ~ dnorm(0, sigma_beta_r^-2)
      #b_t[j] ~ dnorm(0, tau_t)
    }

    # Spline in time penalised
    #r <- B_t %*% b_t
    #b_t[1] ~ dnorm(0,1)
    #b_t[1] ~ dnorm(0,0.00001)
    #for (j in 2:(n_knots_t-1)) {
    #for (j in 1:(n_knots_t-1)) {
      # #b_t[j] ~ dnorm(b_t[j-1], tau_t)
       #b_t[j] ~ dnorm(b_t[j-1], sigma_t^-2)
     #  b_t[j] ~ dnorm(b_t[j-1], sigma_t^-2)
    #}
    #---Sum to 0 constraint on the final knot---
    #b_t[n_knots_t] <- -sum(b_t[1:(n_knots_t-1)])

    # Prior on intercept
    for (j in 1:n_sites) {
      intercept[j] ~ dnorm(0, sigma_beta_h^-2)
    }

    # Linear Local component
    for (j in 1:n_sites) {
      b_g[j] ~ dnorm(linear_rate[j], linear_rate_err[j]^-2)
    }

    # Priors
    sigma_beta_r~ dt(0, 1^-2, 1)T(0,)
    sigma_y ~ dt(0, 1^-2, 1)T(0,)

    sigma_beta_h ~ dt(2.5, 2^-2, 1)T(0,)# old one
    #sigma_beta_h ~ dt(2.5, 1^-2, 1)T(0,)# wider variance
    #sigma_beta_h ~ dt(0, 0.1^-2, 1)T(0,)# Not tuned at all
    #sigma_h ~ dt(0, 0.001^-2, 1)T(0,)
    #sigma_h ~ dt(0, 1^-2, 1)T(0,)


  }
