Model correction difficulty in python

Viewed 44

I have been trying endlessly to figure out why the variable c in the program below is not been returned. Rather, I was getting an error that says NameError: name 'c' is not defined. I have searched for solutions online, including here, but no answer offered a desirable solution yet. How can I print the content of *c *? Any assistance from you will be much appreciated. Thanks.

# obs=observation, p=model data, s=projected data 
def bias_correction(obs, p, s, method='delta', nbins=10, extrapolate=None):
      
    if (method == 'eqm') and (nbins > 1):
        binmid = np.arange((1./nbins)*0.5, 1., 1./nbins)
        qo = mquantiles(obs[np.isfinite(obs)], prob=binmid)
        qp = mquantiles(p[np.isfinite(p)], prob=binmid)
        p2o = interp1d(qp, qo, kind='linear', bounds_error=False)
        c = p2o(s)
        if extrapolate is None:
            c[s > np.max(qp)] = qo[-1]
            c[s < np.min(qp)] = qo[0]
        elif extrapolate == 'constant':
            c[s > np.max(qp)] = s[s > np.max(qp)] + qo[-1] - qp[-1]
            c[s < np.min(qp)] = s[s < np.min(qp)] + qo[0] - qp[0]

    elif method == 'delta':
        c = obs + (np.nanmean(s) - np.nanmean(p))

    elif method == 'scaling_add':
        c = s - np.nanmean(p) + np.nanmean(obs)

    elif method == 'scaling_multi':
        c = (s/np.nanmean(p)) * np.nanmean(obs)

    else:
        raise ValueError("incorrect method, choose from 'delta', 'scaling_add', 'scaling_multi' or 'eqm'")

    return c       # c is the bias-corrected series for s

Here is the image of the full error message. NameError: name 'c' is not defined

1 Answers

Thank you all for your kind responses. Fortunately, a friend just helped me figure it out now, and I taught I should share it. C is from a function, so it is an output term of the function. It isn't a standalone variable.So I need to run the function to produce the output Here is what I ran that produced my desired output.

obs = ref_sliced.tmp
p = mod_interp.tmp
s = future_interp.tmp

bias_correction(obs, p, s, method='delta', 
nbins=10, extrapolate=None)
Related