def fitStat(xdata, ydata, p, cov, infodict): """ Function fitStat ================ Purpose: Derive statistical data from least square fit of TUV data for MCM parameterisations. Variables: I/O: xdata,ydata: x-,y-data for curve fit (sza and j values) p: optimised parameters from least square fit cov: covariance matrix from least square fit infodict: further statistical data from least square fit rsquared: correlation coefficient ss_tot: standard deviation rmse: root mean square error el,em,en: confidence of parameters internal: ss_err: for calculation of R2 and RMSE dof: degrees of freedom for calculation of RMSE s_sq: for calculation of confidences of parameters err: list of all confidences Dependencies: uses: residuals,datfcn.order,numpy called from: datfcn.xydat """ # load functions import numpy as np from datfcn import order # R2: ss_err = (infodict['fvec']**2).sum() ss_tot = ((ydata - ydata.mean())**2).sum() rsquared = 1 - (ss_err / ss_tot) # RMSE: dof = len(xdata) - len(p) rmse = np.sqrt(ss_err / dof) # Confidence: if (len(ydata) > len(p)) and cov is not None: s_sq = (residuals(p, xdata, ydata)**2).sum() / (len(ydata) - len(p)) cov = cov * s_sq else: cov = inf err = np.sqrt(np.diag(cov)) el = err[0] / order(p[0])[1] em = err[1] en = err[2] return rsquared, ss_tot, rmse, el, em, en #______________________________________________________________________________________________
def gpscat(gpfile, rxn, data): """ Function gnuinp =============== Purpose: Write gnuplot input file with data for scatter plots of TUV data. Variables: I/O: gpfile: gnuplot file with data for scatter plots rxn: matrix with indices and labels of available photoreactions data: matrix with sza-dependent j values from TUV model om: matrix with all orders of magnitudes (for sza column 0 dummy inserted) internal: h,x,y: counters/indices o1,o2: order of magnitude of maximum value in each j data column Dependencies: uses: numpy, datfcn.order called from: photMCM (main) """ # initilise data import numpy as np from datfcn import order om = [] # open text file with open(gpfile, 'w+') as gnu: # write header gnu.write("# sza / rad ") for h in range(len(rxn)): gnu.write("\t%s" % (rxn[h][1])) gnu.write("\n") # determine order of magnitude from maximum (first) value # for nicer output for y in range(len(rxn)): o1, o2 = order(data[0][y + 1]) om = np.append(om, o2) om = np.insert(om, 0, 1.) # write data matrix for x in range(len(data)): for y in range(len(rxn) + 1): if (y == 0): data[x][0] = np.deg2rad(data[x][0]) gnu.write("\t%.3f" % (data[x][y] / om[y])) gnu.write("\n") return rxn, data, om
def fitTUV(xdata, ydata): """ Function fitTUV =============== Purpose: Curve fit of TUV data to parameterisation for photolysis in MCM.8///////////////////8=74 j = l*(cos(x))^m*exp(-n*sec(x)) Variables: I/O: xdata,ydata: x-,y-data for curve fit (sza and j values) p: optimised parameters from least square fit cov: covariance matrix from least square fit infodict: further statistical data from least square fit mesg, ier: not used (see description of leastsq function) internal: Param list with parameters for curve fitting p_guess: initial parameters for curve fit Dependencies: uses: residuals,numpy,scipy.optimize.leastsq, collections.namedtuple called from: datfcn.xydat """ # load functions import numpy as np import collections # from scipy.optimize import leastsq # for curve fit from datfcn import order # curve fitting Param = collections.namedtuple('Param', 'l m n') p_guess = Param(l=ydata[0] * np.exp(-0.8), m=0.8, n=0.25) p, cov, infodict, mesg, ier = leastsq(residuals, p_guess, args=(xdata, ydata), full_output=True) p = Param(*p) l = p[0] ol, ml = order(l) l = l / ml m = p[1] n = p[2] return p, l, ol, m, n, cov, infodict
def scatdat(rxn, data): """ Function scatdat ================ Purpose: Derive matrix with plot data and vector with associated photoreactions. Variables: I/O rxn: matrix with indices and labels of available photoreactions data: matrix with sza-dependent j values from TUV model om: matrix with all orders of magnitudes (for sza column 0 dummy inserted) internal: h,x,y: counters/indices o1,o2: order of magnitude of maximum value in each j data column Dependencies: uses: numpy, datfcn.order called from: photMCM (main) """ # initilise data import numpy as np from datfcn import order om = [] # determine order of magnitude from maximum (first) value for nicer output for y in range(len(rxn)): o1, o2 = order(data[0][y + 1]) om = np.append(om, o2) om = np.insert(om, 0, 1.) # write data matrix for x in range(len(data)): for y in range(len(rxn) + 1): if (y == 0): data[x][0] = np.deg2rad(data[x][0]) return rxn, data, om