def doAREMCAnalyssis(stnr, startDate, endDate): """ Preforms a Ragnar Ekker variant of Monte Carlo (or whatever). This metod runs an analysis of several conbinations of gammadistributions for precipitation and temperature affect on cloudcover. Results are written to a sqllite database for further study. :param stnr: eKlima station where there is cloudcover for testing method. :param startDate: simulations and referance data from this data :param endDate: simulations and referance data to this data :return: """ wsTemp = getMetData(stnr, 'TAM', startDate, endDate, 0, 'list') wsPrec = getMetData(stnr, 'RR', startDate, endDate, 0, 'list') wsCC = getMetData(stnr, 'NNM', startDate, endDate, 0, 'list') temp, date = strip_metadata(wsTemp, get_dates=True) prec = strip_metadata(wsPrec, False) clouds = strip_metadata(wsCC, False) loggdate = datetime.datetime.now() period = '{0} to {1}'.format(startDate,endDate) cs = [1] # cloudshift the observed data one day forward psh = [2.8, 3.0, 3.3] # prec gamma shapefactor psc = [2.0] # prec gamma scalefactor pdb = [0.2, 0.3, 0.5, 0.7] # prec days back pap = [0.3, 0.4, 0.5] # prec amplitude factor tsh = [4.3, 4.5, 4.8, 5.0] # temp gamma shapefactor tsc = [1.0] # temp gamma scalefactor tdb = [4.8, 5.0, 5.5] # temp days back tap = [0.02, 0.03, 0.05] # temp amplitude factor estimateSimulations = len(cs)*len(psh)*len(psc)*len(pdb)*len(pap)*len(tsh)*len(tsc)*len(tdb)*len(tap) doneSimulations = 0 for a in cs: cloudsShifted = __shiftClouds(clouds, a) for b in psh: for c in psc: for d in pdb: for e in pap: for f in tsh: for g in tsc: for h in tdb: for i in tap: estClouds = dgc.cc_gamma_prec_and_temp_change(prec, temp, [b, c, d, e], [f, g, h, i]) # What is the root mean square of estimatet vs observed clouds? rms = np.sqrt(((np.array(estClouds) - np.array(cloudsShifted)) ** 2).mean()) __writeRMC2database(database_location, a, b, c, d, e, f, g, h, i, rms, loggdate, stnr, period) doneSimulations = doneSimulations + 1 print('beregning {0} av {1}'.format(doneSimulations, estimateSimulations)) return
def testCloudMaker(stnr, startDate, endDate, method): """Gets data from a eKlima station and smooths the data depending on which method we wish to test. We find the nash-sutcliffe to observations of clouds. And we plot and save the result to Plots folder. :param stnr: eKlima station where there is cloud cover for testing method. :param startDate: simulations and reference data from this data :param endDate: simulations and reference data to this data :param method: specify which method to be tested :return: Available methods to test: cc_from_prec ccFromAveragePrec ccFromAverageObsCc ccGammaPrec ccGammaTemp ccGammaPrecAndTemp""" wsTemp = getMetData(stnr, 'TAM', startDate, endDate, 0, 'list') wsPrec = getMetData(stnr, 'RR', startDate, endDate, 0, 'list') wsCC = getMetData(stnr, 'NNM', startDate, endDate, 0, 'list') temp, date = strip_metadata(wsTemp, get_dates=True) prec = strip_metadata(wsPrec) clouds = strip_metadata(wsCC) dayShift = 1 clouds = __shiftClouds(clouds, dayShift) if method == 'ccFromRandomThomas': estClouds = dpz.clouds_from_precipitation(prec, method='Random Thomas') gammaFigtext = 'No gamma smoothing and dayshift = {0}'.format(dayShift) elif method == 'ccFromPrec': estClouds = dpz.clouds_from_precipitation(prec, method='Binary') gammaFigtext = 'No gamma smoothing and dayshift = {0}'.format(dayShift) elif method == 'ccFromAveragePrec': estClouds = dpz.clouds_from_precipitation(prec, method='Average') gammaFigtext = 'No gamma smoothing and dayshift = {0}'.format(dayShift) elif method == 'ccFromPrecAndAveragePrec': estClouds = dpz.clouds_from_precipitation(prec, method='Binary and average') gammaFigtext = ''.format() elif method == 'ccFromAverageObsCc': estClouds = [sum(clouds)/float(len(clouds))] * len(clouds) gammaFigtext = ''.format() elif method == 'ccGammaPrec': gammaPrec = [2.8, 2., 0.3, 0.4] estClouds = dpz.clouds_from_precipitation(prec, method='Binary') estClouds = dgc.cc_gamma_smoothing(estClouds, gammaPrec) gammaFigtext = "gamma smoothing prec = {0} and dayshift = {1}".format(gammaPrec, dayShift) elif method == 'ccGammaTempChange': gammaTemp = [4.8, 1., 5., 0.03] estClouds = dgc.cc_gamma_temp(temp, gammaTemp) gammaFigtext = "gamma smoothing temp = {0} and dayshift = {1}".format(gammaTemp, dayShift) elif method == 'ccGammaPrecAndTempChange': gammaPrec = [2.8, 2., 0.3, 0.4] gammaTemp = [4.8, 1., 5., 0.03] estClouds = dgc.cc_gamma_prec_and_temp_change(prec, temp, gammaPrec, gammaTemp) gammaFigtext = "prec = {0} and temp = {1} and dayshift = {2}".format(gammaPrec, gammaTemp, dayShift) fileName = "{3} {0} {1} {2}.png".format(stnr, startDate[0:7], endDate[0:7], method) # What is the root mean square of estimatet vs observed clouds? # rms = np.sqrt(((np.array(estClouds) - np.array(clouds)) ** 2).mean()) # Nash–Sutcliffe model efficiency coefficient from # https://en.wikipedia.org/wiki/Nash%E2%80%93Sutcliffe_model_efficiency_coefficient numerator = 0 denominator = 0 mean_clouds = sum(clouds)/float(len(clouds)) for i in range(0, len(clouds), 1): numerator += (clouds[i] - estClouds[i])**2 denominator += (clouds[i] - mean_clouds)**2 nash_sutcliffe = 1 - numerator/denominator # Figure dimensions fsize = (16, 10) plt.figure(figsize=fsize) plt.clf() # plot total snowdepth on land plt.bar(date, prec, width=1, color="0.4") # plot the estimated cloud cover for i in range(0, len(estClouds) - 1, 1): if estClouds[i] > 0: plt.hlines(max(prec) * 1.2, date[i], date[i + 1], lw=45, color=str(-(estClouds[i] - 1.))) elif estClouds[i] == None: plt.hlines(max(prec) * 1.2, date[i], date[i + 1], lw=45, color="pink") else: plt.hlines(max(prec) * 1.2, date[i], date[i + 1], lw=45, color=str(-(estClouds[i] - 1.))) # plot cloud cover from met for i in range(0, len(clouds) - 1, 1): if clouds[i] > 0: plt.hlines(max(prec) * 1.1, date[i], date[i + 1], lw=45, color=str(-(clouds[i] - 1.))) elif clouds[i] == None: plt.hlines(max(prec) * 1.1, date[i], date[i + 1], lw=45, color="pink") else: plt.hlines(max(prec) * 1.1, date[i], date[i + 1], lw=45, color=str(-(clouds[i] - 1.))) # this plots temperature on separate right side axis plt.twinx() temp_pluss = [] temp_minus = [] for i in range(0, len(temp), 1): if temp[i] >= 0: temp_pluss.append(temp[i]) temp_minus.append(np.nan) else: temp_minus.append(temp[i]) temp_pluss.append(np.nan) plt.plot(date, temp, "black") plt.plot(date, temp_pluss, "red") plt.plot(date, temp_minus, "blue") # title and text fields plt.title("{3} {0} {1} {2}".format(stnr, startDate[0:7], endDate[0:7], method)) plt.text(date[len(date)/2], min(temp)*1.2, 'gamma smoothing [shape, scale, days back, amplification]') plt.text(date[len(date)/2], min(temp)*1.3, gammaFigtext) # this is a scatter plot of modelled and estimated cloud cover xfrac = 0.15 yfrac = (float(fsize[0])/float(fsize[1])) * xfrac xpos = 0.95-xfrac ypos = 0.42-yfrac a = plt.axes([xpos, ypos, xfrac, yfrac]) a.scatter(clouds, estClouds) plt.setp(a, xticks=[0, 0.5, 1], yticks=[0, 0.5, 1]) plt.text(0.0, 0.1, 'na_su = {0}'.format(round(nash_sutcliffe, 2)), color='yellow', bbox={'facecolor':'black'}) plt.savefig("{0}{1}".format(plot_folder, fileName)) return nash_sutcliffe