def GenerateSudoku(): """ Generates a full Sudoku with all numbers Returns: 3D-Array """ succesfull = 0 countTries = 0 while succesfull < 1: Sudoku = ArrayTools.nullBased2DArray(9) for x in range (0,9): for y in range(0,9): cands = CandidateChecker.getCandidates(Sudoku, x, y) #Check if there are no more candidates if(len(cands) == 0): succesfull = -1 countTries = countTries + 1 break else: Sudoku[x][y] = RandomGenerator.GenerateRandom(cands) #Check if succesfull if((x == 8) & (y == 8)): succesfull = 1 print("Generation succesfull with " + str(countTries) + " Tries!") if(succesfull == -1 | succesfull == 1): break return Sudoku
def getCandidates(Sudoku, posX, posY): """ Returns all possible candidates for the given position in the given Sudoku If a value == 0, it is seen as empty """ #List of Candidates candidates = [] #Adds the numbers 1 - 9 to the array Numbers = [i for i in range(1, 10)] #Check vertical numbers candidates = ArrayTools.substractArray(Numbers, Sudoku[posX]) #Check horizontal Numbers candidatesVert = ArrayTools.substractArray(Numbers, ArrayTools.getRow(Sudoku, posX)) candidates = ArrayTools.onlyKeepSameNumbers(candidates, candidatesVert) #Check sqaure Numbers candidatesSqr = ArrayTools.substractArray( Numbers, ArrayTools.getSquare(Sudoku, posX, posY)) candidates = ArrayTools.onlyKeepSameNumbers(candidates, candidatesSqr) return candidates
def Map(self,mapfunction): """Maps the grid using Cartesian coordinates This method can be used to map the grid values along with their coordinates. 'mapfunction' is expected to take the arguments, 'mapfunction(' *gridvalue* , *coordinate* ')' where coordinate is a NumPy array containing the Cartesian coordinates corresponding to the grid point. """ import ArrayTools return ArrayTools.Map(mapfunction,self.GetArray(),self.GetCartesianCoordinates())
def solveSudoku(Sudoku): """ Solves the given Sudoku, if there is a unique solution Returns the solved sudoku """ while True: lowest_Candidates = 10 lowest_posX = 0 lowestposY = 0 solved = True Candidates = ArrayTools.nullBased3DArray(9) #Get candidates for all fields for x in range(0, 9): for y in range(0, 9): cands = CandidateChecker.getCandidates(Sudoku, x, y) Candidates[x][y] = cands #Checks if the current field has the minimum numbers of candidates curCandCount = len(cands) #print(curCandCount) if (curCandCount > 0 & curCandCount < lowest_Candidates): lowest_Candidates = curCandCount lowest_posX = x lowestposY = y #If there is any field which is not filled, the sudoku is not solved if (Sudoku[x][y] == 0): solved = False #Check if the sudoku is already solved if (solved == True): print("Sudoku was succesfully solved!") return Sudoku print(lowest_Candidates) #Checks if the Sudoku is solvable if (lowest_Candidates == 1): solution = Candidates[lowest_posX][lowestposY][0] Sudoku[lowest_posX][lowestposY] = solution print("Got number " + solution + " as right number for (" + lowest_posX + "/" + lowestposY + ")") continue else: print("Sudoko is not solvable!") sys.exit(-1)
def getSudokoFromInput(): Sudoku = ArrayTools.nullBased2DArray(9) for y in range(0, 9): eingabe = input("Bitte geben Sie die " + str(y + 1) + ". Zeile ein: \n") #Check for valid input if (len(eingabe) != 9 | is_number(eingabe) == False): print("Could not convert the given string to a Sudoko!") sys.exit(-1) else: for x in range(0, 9): number = int(eingabe[x]) Sudoku[x][y] = number return Sudoku
def TranslateCoordinates(self,translation,type): """Returns a translated grid This method can be used to translate the coordinates of a grid. Two different types of translation exist * 'active' : Translates every grid point according to 'translation' * 'passive' : Translates the origin of the grid according to 'translation' This method only supports a translation vector that matches the grid coordinates and for this reason 'translation' should be specified in terms of these coordinates. """ import ArrayTools from Numeric import asarray from Vector import Vector import copy # finding the output grid translategrid=copy.copy(self) # preparing for translation and calculating origin #trunctranslation=self._GetTruncatedTranslation(translation) if type=='active': translationnumbers=self._GetTranslationNumbers(-asarray(translation)) # origin translategrid.SetOrigin(self.GetOrigin()) elif type=='passive': translationnumbers=self._GetTranslationNumbers(asarray(translation)) # origin transorigin=Vector(space=self.GetOrigin().GetSpace()) transorigin.SetCartesianCoordinates(translategrid.GetSpace().CartesianCoordinatesFromCoordinates(self.CoordinatesFromGridCoordinates(translation))) translategrid.SetOrigin(self.GetOrigin()+transorigin) else: raise ValueError, 'translation type not defined' # finding the output array translatearray=ArrayTools.Translate(self.GetArray(),translationnumbers) translategrid.SetArray(translatearray) return translategrid
import itertools, ArrayTools combs = [] tot = 0 def isPal(i): c = str(i) if c == c[::-1]: return True return False for i in itertools.permutations(range(100, 1000), 2): c = sorted(i) combs.append(tuple(c)) combs = set(combs) lgst = 0 for i in combs: tmpProd = ArrayTools.listProd(i) if tmpProd > lgst and isPal(tmpProd) == True: lgst = tmpProd print lgst
def start_calculations(dask_client, _routines, dask_threads, process): import ArrayTools import Calculation import Routines import PyPostTools logger = PyPostTools.pyPostLogger() try: start = os.environ["PYTHON_POST_FIRSTTIME"] postDir = os.environ["PYTHON_POST_DIR"] targetDir = os.environ["PYTHON_POST_TARG_DIR"] except KeyError: process.terminate() logger.write("***FAIL*** Could not locate environment variables set by the original application (DIRS), check the logs to ensure it is being done.") logger.close() sys.exit("Failed to find environmental variable (DIRS), check original application to ensure it is being set.") return -1 # Get the list of files logger.write(" - Collecting files from target directory (" + postDir + ").") fList = sorted(glob.glob(postDir + "wrfout*")) logger.write(" - " + str(len(fList)) + " files have been found.") logger.write(" - Checking target directory if this job has been done?") fList2 = sorted(glob.glob(targetDir + "WRFPRS_F*")) if(len(fList) == len(fList2)): logger.write(" - " + str(len(fList2)) + " WRFPRS files have been found, calculations are already completed, skipping step.") return None logger.write(" - No.") if(start == "" or targetDir == ""): logger.write("Cannot run calculations, missing important information") return -1 for ncFile_Name in fList: logger.write("Running calculation routines on " + str(ncFile_Name)) startTime = datetime.strptime(start, '%Y%m%d%H') daskArray = xarray.open_mfdataset(ncFile_Name, parallel=False, combine='by_coords') logger.write(" > DEBUG: ncFile Opened\n\n" + str(daskArray) + "\n\n") forecastTime_str = ncFile_Name[-19:] forecastTime = datetime.strptime(forecastTime_str, '%Y-%m-%d_%H_%M_%S') elapsedTime = forecastTime - startTime elapsedHours = elapsedTime.days*24 + elapsedTime.seconds//3600 # Grab the vertical interpolation levels logger.write(" > DEBUG: Fetch vertical interpolation levels") p_vert = Calculation.get_full_p(daskArray, omp_threads=dask_threads) p_vert.persist() logger.write(" > DEBUG: P:\n" + str(p_vert) + "\n") z_vert = Calculation.get_height(daskArray, omp_threads=dask_threads) z_vert.persist() logger.write(" > DEBUG: Z:\n" + str(z_vert) + "\n") logger.write(" > DEBUG: Done.") # Our end goal is to create a new xArray saving only what we need to it. Start by creaying a "blank" xarray logger.write(" > DEBUG: Create new xarray dataset object") xrOut = ArrayTools.make_dataset(daskArray, start, elapsedHours) logger.write(" > DEBUG: Done.") # Now, calculate the variables. ## ## - MSLP if(_routines.need_mslp): logger.write(" > DEBUG: MSLP - " + str(ncFile_Name)) calc = Calculation.get_slp(daskArray, omp_threads=dask_threads) mslp = calc.compute(num_workers=dask_nodes) xrOut["MSLP"] = (('south_north', 'west_east'), mslp) del(calc) del(mslp) ## ## - Simulated Radar Reflectivity if(_routines.need_sim_dbz): logger.write(" > DEBUG: SDBZ - " + str(ncFile_Name)) calc = Calculation.get_dbz(daskArray, use_varint=False, use_liqskin=False, omp_threads=dask_threads) dbz = calc.compute(num_workers=dask_nodes) xrOut["DBZ"] = (('south_north', 'west_east'), dbz[0]) del(calc) del(dbz) ## ## - Precipitation Type if(_routines.need_ptype): # Need to make a routine for this. logger.write(" > DEBUG: ***WARNING*** Precipitation type is currently unsupported, ignoring.") ## ## - Total Accumulated Precipitation if(_routines.need_acum_pcp): logger.write(" > DEBUG: APCP - " + str(ncFile_Name)) calc = Calculation.get_accum_precip(daskArray, omp_threads=dask_threads) acum_pcp = calc.compute(num_workers=dask_nodes) xrOut["ACUM_PCP"] = (('south_north', 'west_east'), acum_pcp) del(calc) del(acum_pcp) ## ## - Total Accumulated Snowfall if(_routines.need_acum_sno): logger.write(" > DEBUG: ASNO - " + str(ncFile_Name)) calc = ArrayTools.fetch_variable(daskArray, "SNOWNC") acum_sno = calc.compute(num_workers=dask_nodes) xrOut["ACUM_SNO"] = (('south_north', 'west_east'), acum_sno) del(calc) del(acum_sno) ## ## - Precipitable Water if(_routines.need_prec_wat): logger.write(" > DEBUG: PWAT - " + str(ncFile_Name)) calc = Calculation.get_pw(daskArray, omp_threads=dask_threads) prec_wat = calc.compute(num_workers=dask_nodes) xrOut["PW"] = (('south_north', 'west_east'), prec_wat) del(calc) del(prec_wat) ## ## - Dewpoint Temperature if(_routines.need_dewpoint): logger.write(" > DEBUG: TDPT - " + str(ncFile_Name)) calc = Calculation.get_dewpoint(daskArray, omp_threads=dask_threads) td = calc.compute(num_workers=dask_nodes) xrOut["TD"] = (('south_north', 'west_east'), td[0]) del(calc) del(td) ## ## - Relative Humidity if(_routines.need_RH): logger.write(" > DEBUG: RELH - " + str(ncFile_Name)) calc = Calculation.get_rh(daskArray, omp_threads=dask_threads) rh = calc.compute(num_workers=dask_nodes) for l in _routines.rh_levels: if(l == 0): xrOut["SFC_RH"] = (('south_north', 'west_east'), rh[0]) else: rh_level = ArrayTools.wrapped_interplevel(rh, p_vert, l, omp_threads=dask_threads) xrOut["RH_" + str(l)] = (('south_north', 'west_east'), rh_level[0]) del(rh_level) del(calc) del(rh) ## ## - Air Temperature if(_routines.need_Temp): logger.write(" > DEBUG: AIRT - " + str(ncFile_Name)) calc = Calculation.get_tk(daskArray, omp_threads=dask_threads) tk = calc.compute(num_workers=dask_nodes) for l in _routines.temp_levels: if(l == 0): xrOut["SFC_T"] = (('south_north', 'west_east'), tk[0]) else: tk_level = ArrayTools.wrapped_interplevel(tk, p_vert, l, omp_threads=dask_threads) xrOut["T_" + str(l)] = (('south_north', 'west_east'), tk_level[0]) del(tk_level) del(calc) del(tk) ## ## - U/V Wind Components if(_routines.need_winds): logger.write(" > DEBUG: WIND - " + str(ncFile_Name)) for l in _routines.winds_levels: # We can handle the 0 as surface here because this function defaults to surface winds when requested_top == 0 u, v = Calculation.get_winds_at_level(daskArray, vertical_field=p_vert, requested_top=l) uComp = u.compute(num_workers=dask_nodes) vComp = v.compute(num_workers=dask_nodes) if(l == 0): xrOut["SFC_U"] = (('south_north', 'west_east'), uComp) xrOut["SFC_V"] = (('south_north', 'west_east'), vComp) else: xrOut["U_" + str(l)] = (('south_north', 'west_east'), uComp[0]) xrOut["V_" + str(l)] = (('south_north', 'west_east'), vComp[0]) del(u) del(v) del(uComp) del(vComp) ## ## - Equivalent Potential Temperature (Theta_E) if(_routines.need_theta_e): logger.write(" > DEBUG: THTE - " + str(ncFile_Name)) calc = Calculation.get_eth(daskArray, omp_threads=dask_threads) eth = calc.compute(num_workers=dask_nodes) for l in _routines.theta_e_levels: if(l == 0): xrOut["SFC_THETA_E"] = (('south_north', 'west_east'), eth[0]) else: eth_level = ArrayTools.wrapped_interplevel(eth, p_vert, l, omp_threads=dask_threads) xrOut["THETA_E_" + str(l)] = (('south_north', 'west_east'), eth_level[0]) del(eth_level) del(eth) del(calc) ## ## - Omega if(_routines.need_omega): logger.write(" > DEBUG: OMGA - " + str(ncFile_Name)) calc = Calculation.get_omega(daskArray, omp_threads=dask_threads) omega = calc.compute(num_workers=dask_nodes) xrOut["OMEGA"] = (('south_north', 'west_east'), omega[0]) del(omega) del(calc) ## ## - Max Surface Wind Gust (AFWA Diagnostic) if(_routines.need_sfc_max_winds): logger.write(" > DEBUG: MWND - " + str(ncFile_Name)) maxWind = ArrayTools.fetch_variable(daskArray, "WSPD10MAX") xrOut["MAX_WIND_SFC"] = (('south_north', 'west_east'), maxWind) del(maxWind) ## ## - Geopotential Height if(_routines.need_geoht): logger.write(" > DEBUG: GHGT - " + str(ncFile_Name)) for l in _routines.geoht_levels: z = z_vert.compute(num_workers=dask_nodes) p = p_vert.compute(num_workers=dask_nodes) z_level = ArrayTools.wrapped_interplevel(z, p, l, omp_threads=dask_threads) xrOut["GEOHT_" + str(l)] = (('south_north', 'west_east'), z_level[0]) del(z_level) del(z) del(p) ## ## - 500 mb Relative Vorticity if(_routines.need_relvort): logger.write(" > DEBUG: RLVT - " + str(ncFile_Name)) calc = Calculation.get_rvor(daskArray, omp_threads=dask_threads) rvo = calc.compute(num_workers=dask_nodes) rvo_500 = ArrayTools.wrapped_interplevel(rvo, p_vert, 500, omp_threads=dask_threads) xrOut["RVO_500"] = (('south_north', 'west_east'), rvo_500[0]) del(calc) del(rvo) del(rvo_500) ## ## - Convective Available Potential Energy (3D) & Convective Inhibition (3D) if(_routines.need_3d_cape or _routines.need_3d_cin): logger.write(" > DEBUG: 3DCAPE - " + str(ncFile_Name)) calc = Calculation.get_cape3d(daskArray, omp_threads=dask_threads) cape3d = calc.compute(num_workers=dask_nodes) cape = cape3d[0] cin = cape3d[1] if(_routines.need_3d_cape): xrOut["CAPE_3D"] = (('bottom_top', 'south_north', 'west_east'), cape) if(_routines.need_3d_cin): xrOut["CIN_3D"] = (('bottom_top', 'south_north', 'west_east'), cin) del(calc) del(cape3d) del(cape) del(cin) ## ## - Maximum Cape (MUCAPE, 2D), Maximum CIN (MUCIN, 2D), Lifting Condensation Level (LCL), Level of Free Convection (LFC) if(_routines.need_mucape or _routines.need_mucin or _routines.need_lcl or _routines.need_lfc): logger.write(" > DEBUG: 2DCAPE - " + str(ncFile_Name)) cape2d = Calculation.get_cape2d(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) mucape = cape2d[0] mucin = cape2d[1] lcl = cape2d[2] lfc = cape2d[3] if(_routines.need_mucape): xrOut["MUCAPE"] = (('south_north', 'west_east'), mucape) if(_routines.need_mucin): xrOut["MUCIN"] = (('south_north', 'west_east'), mucin) if(_routines.need_lcl): xrOut["LCL"] = (('south_north', 'west_east'), lcl) if(_routines.need_lfc): xrOut["LFC"] = (('south_north', 'west_east'), lfc) del(cape2d) del(mucape) del(mucin) del(lcl) del(lfc) ## ## - Storm Relative Helicity if(_routines.need_srh): logger.write(" > DEBUG: SRH - " + str(ncFile_Name)) for l in _routines.srh_levels: calc = Calculation.get_srh(daskArray, top=l, omp_threads=dask_threads) srh = calc.compute(num_workers=dask_nodes) xrOut["SRH_" + str(l)] = (('south_north', 'west_east'), srh) del(srh) del(calc) ## ## - Updraft Helicity if(_routines.need_uphel): logger.write(" > DEBUG: UPHL - " + str(ncFile_Name)) if(len(_routines.updft_helcy_levels) % 2 != 0): logger.write("***WARNING*** Error in updraft helicity levels, list must have a divisible number of 2 (values are pairs).") else: lows = _routines.updft_helcy_levels[0::2] highs = _routines.updft_helcy_levels[1::2] for i in range(0, len(lows)): calc = Calculation.get_udhel(daskArray, bottom=lows[i], top=highs[i], omp_threads=dask_threads) uphel = calc.compute(num_workers=dask_nodes) xrOut["UPHEL_" + str(lows[i]) + "_" + str(highs[i])] = (('south_north', 'west_east'), uphel) del(uphel) del(calc) ## ## - Wind Shear if(_routines.need_shear): logger.write(" > DEBUG: WSHR - " + str(ncFile_Name)) for l in _routines.shear_levels: uS, vS, spd = Calculation.get_wind_shear(daskArray, top=l, omp_threads=dask_threads, z=z_vert) uComp = uS.compute(num_workers=dask_nodes) vComp = vS.compute(num_workers=dask_nodes) sComp = spd.compute(num_workers=dask_nodes) xrOut["SHEAR_U_" + str(l)] = (('south_north', 'west_east'), uComp[0]) xrOut["SHEAR_V_" + str(l)] = (('south_north', 'west_east'), vComp[0]) xrOut["SHEAR_MAG_" + str(l)] = (('south_north', 'west_east'), sComp[0]) del(uS) del(vS) del(spd) del(uComp) del(vComp) del(sComp) ## ## - AFWA Hail Diagnostic if(_routines.need_afwa_hail): afwaHail = ArrayTools.fetch_variable(daskArray, "AFWA_HAIL") xrOut["AFWA_HAIL"] = (('south_north', 'west_east'), afwaHail) del(afwaHail) ## ## - AFWA Tornado Diagnostic if(_routines.need_afwa_tor): afwaTor = ArrayTools.fetch_variable(daskArray, "AFWA_TORNADO") xrOut["AFWA_TORNADO"] = (('south_north', 'west_east'), afwaTor) del(afwaTor) ## ## - Done Calculations ## # Save our variables to the output file. logger.write(" > DEBUG: Saving output file.") timeOut = "0" + str(elapsedHours) if elapsedHours < 10 else str(elapsedHours) xrOut.to_netcdf(targetDir + "/WRFPRS_F" + timeOut + ".nc") logger.write("Calculations completed, file saved as " + targetDir + "/WRFPRS_F" + timeOut + ".nc") #Done. return True
g3.append(temp) allSets = [] for row in xrange(len(g3)): Tset = GridOps.GoDiagLR(g3, row)[0] allSets.append(Tset) Tset = GridOps.GoDiagLR(g3, row)[1] allSets.append(Tset) Tset = GridOps.GoDiagRL(g3, row)[0] allSets.append(Tset) Tset = GridOps.GoDiagRL(g3, row)[1] allSets.append(Tset) for i in xrange(len(g3)): Tset = GridOps.GoHorz(g3, i) allSets.append(Tset) Tset = GridOps.GoVert(g3, i) allSets.append(Tset) AllSubs = [] for nums in allSets: if len(nums) > 4: Subs = ArrayTools.subarrays(nums, 4) AllSubs.append(Subs) AllTotals = [] for sets in AllSubs: for four in sets: AllTotals.append(ArrayTools.listProd(four)) AllTotals = sorted(AllTotals) print AllTotals[-1]
def run_calculation_routines(callObject): ncFile_Name = callObject['filename'] start = callObject['start'] targetDir = callObject['tDir'] _routines = callObject['routines'] dask_threads = callObject['dask_threads'] logger = PyPostTools.pyPostLogger() if (start == "" or targetDir == ""): logger.write("Cannot run calculations, missing important information") return -1 startTime = datetime.strptime(start, '%Y%m%d%H') daskArray = xarray.open_mfdataset(ncFile_Name, parallel=True) forecastTime_str = ncFile_Name[-19:] forecastTime = datetime.strptime(forecastTime_str, '%Y-%m-%d_%H_%M_%S') elapsedTime = forecastTime - startTime elapsedHours = elapsedTime.days * 24 + elapsedTime.seconds // 3600 # Grab the vertical interpolation levels p_vert = Calculation.get_full_p(daskArray) z_vert = Calculation.get_height(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) # Our end goal is to create a new xArray saving only what we need to it. Start by creaying a "blank" xarray xrOut = xarray.Dataset() # Start with important attributes xrOut.attrs["MOAD_CEN_LAT"] = daskArray.MOAD_CEN_LAT xrOut.attrs["CEN_LON"] = daskArray.CEN_LON xrOut.attrs["TRUELAT1"] = daskArray.TRUELAT1 xrOut.attrs["TRUELAT2"] = daskArray.TRUELAT2 xrOut.attrs["MAP_PROJ"] = daskArray.MAP_PROJ xrOut.attrs["DX"] = daskArray.DX xrOut.attrs["DY"] = daskArray.DY xrOut.attrs["STARTTIME"] = start xrOut.attrs["FORECASTHOUR"] = elapsedHours # Copy map information first xrOut.coords["XLAT"] = (('south_north', 'west_east'), daskArray["XLAT"][0]) xrOut.coords["XLONG"] = (('south_north', 'west_east'), daskArray["XLONG"][0]) # Now, calculate the variables. ## ## - MSLP if (_routines.need_mslp): print("MSLP - " + str(ncFile_Name)) mslp = Calculation.get_slp(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["MSLP"] = (('south_north', 'west_east'), mslp) del (mslp) ## ## - Simulated Radar Reflectivity if (_routines.need_sim_dbz): print("SDBZ - " + str(ncFile_Name)) dbz = Calculation.get_dbz(daskArray, use_varint=False, use_liqskin=False, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["DBZ"] = (('south_north', 'west_east'), dbz[0]) del (dbz) ## ## - Precipitation Type if (_routines.need_ptype): # Need to make a routine for this. print( "***WARNING*** Precipitation type is currently unsupported, ignoring." ) ## ## - Total Accumulated Precipitation if (_routines.need_acum_pcp): print("APCP - " + str(ncFile_Name)) acum_pcp = Calculation.get_accum_precip(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["ACUM_PCP"] = (('south_north', 'west_east'), acum_pcp) del (acum_pcp) ## ## - Total Accumulated Snowfall if (_routines.need_acum_sno): print("ASNO - " + str(ncFile_Name)) acum_sno = ArrayTools.fetch_variable(daskArray, "SNOWNC") xrOut["ACUM_SNO"] = (('south_north', 'west_east'), acum_sno) del (acum_sno) ## ## - Precipitable Water if (_routines.need_prec_wat): print("PWAT - " + str(ncFile_Name)) prec_wat = Calculation.get_pw(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["PW"] = (('south_north', 'west_east'), prec_wat) del (prec_wat) ## ## - Dewpoint Temperature if (_routines.need_dewpoint): print("TDPT - " + str(ncFile_Name)) td = Calculation.get_dewpoint(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["TD"] = (('south_north', 'west_east'), td[0]) del (td) ## ## - Relative Humidity if (_routines.need_RH): print("RELH - " + str(ncFile_Name)) rh = Calculation.get_rh(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) for l in _routines.rh_levels: if (l == 0): xrOut["SFC_RH"] = (('south_north', 'west_east'), rh[0]) else: rh_level = ArrayTools.wrapped_interplevel( rh, p_vert, l, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["RH_" + str(l)] = (('south_north', 'west_east'), rh_level[0]) del (rh_level) del (rh) ## ## - Air Temperature if (_routines.need_Temp): print("AIRT - " + str(ncFile_Name)) tk = Calculation.get_tk(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) for l in _routines.temp_levels: if (l == 0): xrOut["SFC_T"] = (('south_north', 'west_east'), tk[0]) else: tk_level = ArrayTools.wrapped_interplevel( tk, p_vert, l, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["T_" + str(l)] = (('south_north', 'west_east'), tk_level[0]) del (tk_level) del (tk) ## ## - U/V Wind Components if (_routines.need_winds): print("WIND - " + str(ncFile_Name)) for l in _routines.winds_levels: # We can handle the 0 as surface here because this function defaults to surface winds when requested_top == 0 u, v = Calculation.get_winds_at_level(daskArray, vertical_field=p_vert, requested_top=l) if (l == 0): xrOut["SFC_U"] = (('south_north', 'west_east'), u) xrOut["SFC_V"] = (('south_north', 'west_east'), v) else: xrOut["U_" + str(l)] = (('south_north', 'west_east'), u[0]) xrOut["V_" + str(l)] = (('south_north', 'west_east'), v[0]) ## ## - Equivalent Potential Temperature (Theta_E) if (_routines.need_theta_e): print("THTE - " + str(ncFile_Name)) eth = Calculation.get_eth(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) for l in _routines.theta_e_levels: if (l == 0): xrOut["SFC_THETA_E"] = (('south_north', 'west_east'), eth[0]) else: eth_level = ArrayTools.wrapped_interplevel( eth, p_vert, l, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["THETA_E_" + str(l)] = (('south_north', 'west_east'), eth_level[0]) del (eth_level) del (eth) ## ## - Omega if (_routines.need_omega): print("OMGA - " + str(ncFile_Name)) omega = Calculation.get_omega(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["OMEGA"] = (('south_north', 'west_east'), omega[0]) del (omega) ## ## - Max Surface Wind Gust (AFWA Diagnostic) if (_routines.need_sfc_max_winds): print("MWND - " + str(ncFile_Name)) maxWind = ArrayTools.fetch_variable(daskArray, "WSPD10MAX") xrOut["MAX_WIND_SFC"] = (('south_north', 'west_east'), maxWind) del (maxWind) ## ## - Geopotential Height if (_routines.need_geoht): print("GHGT - " + str(ncFile_Name)) for l in _routines.geoht_levels: z_level = ArrayTools.wrapped_interplevel(z_vert, p_vert, l, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["GEOHT_" + str(l)] = (('south_north', 'west_east'), z_level[0]) del (z_level) ## ## - 500 mb Relative Vorticity if (_routines.need_relvort): print("RLVT - " + str(ncFile_Name)) rvo = Calculation.get_rvor(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) rvo_500 = ArrayTools.wrapped_interplevel(rvo, p_vert, 500, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["RVO_500"] = (('south_north', 'west_east'), rvo_500[0]) del (rvo) del (rvo_500) ## ## - Convective Available Potential Energy (3D) & Convective Inhibition (3D) if (_routines.need_3d_cape or _routines.need_3d_cin): print("3DCAPE - " + str(ncFile_Name)) cape3d = Calculation.get_cape3d(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) cape = cape3d[0] cin = cape3d[1] if (_routines.need_3d_cape): xrOut["CAPE_3D"] = (('bottom_top', 'south_north', 'west_east'), cape) if (_routines.need_3d_cin): xrOut["CIN_3D"] = (('bottom_top', 'south_north', 'west_east'), cin) del (cape3d) del (cape) del (cin) ## ## - Maximum Cape (MUCAPE, 2D), Maximum CIN (MUCIN, 2D), Lifting Condensation Level (LCL), Level of Free Convection (LFC) if (_routines.need_mucape or _routines.need_mucin or _routines.need_lcl or _routines.need_lfc): print("2DCAPE - " + str(ncFile_Name)) cape2d = Calculation.get_cape2d(daskArray, omp_threads=dask_threads, num_workers=dask_nodes) mucape = cape2d[0] mucin = cape2d[1] lcl = cape2d[2] lfc = cape2d[3] if (_routines.need_mucape): xrOut["MUCAPE"] = (('south_north', 'west_east'), mucape) if (_routines.need_mucin): xrOut["MUCIN"] = (('south_north', 'west_east'), mucin) if (_routines.need_lcl): xrOut["LCL"] = (('south_north', 'west_east'), lcl) if (_routines.need_lfc): xrOut["LFC"] = (('south_north', 'west_east'), lfc) del (cape2d) del (mucape) del (mucin) del (lcl) del (lfc) ## ## - Storm Relative Helicity if (_routines.need_srh): print("SRH - " + str(ncFile_Name)) for l in _routines.srh_levels: srh = Calculation.get_srh(daskArray, top=l, omp_threads=dask_threads, num_workers=dask_nodes) xrOut["SRH_" + str(l)] = (('south_north', 'west_east'), srh) del (srh) ## ## - Updraft Helicity if (_routines.need_uphel): print("UPHL - " + str(ncFile_Name)) if (len(_routines.updft_helcy_levels) % 2 != 0): logger.write( "***WARNING*** Error in updraft helicity levels, list must have a divisible number of 2 (values are pairs)." ) else: lows = _routines.updft_helcy_levels[0::2] highs = _routines.updft_helcy_levels[1::2] for i in range(0, len(lows)): uphel = Calculation.get_udhel(daskArray, bottom=lows[i], top=highs[i], omp_threads=dask_threads, num_workers=dask_nodes) xrOut["UPHEL_" + str(lows[i]) + "_" + str(highs[i])] = (('south_north', 'west_east'), uphel) del (uphel) ## ## - Wind Shear if (_routines.need_shear): print("WSHR - " + str(ncFile_Name)) for l in _routines.shear_levels: uS, vS, spd = Calculation.get_wind_shear(daskArray, top=l, omp_threads=dask_threads, num_workers=dask_nodes, z=z_vert) xrOut["SHEAR_U_" + str(l)] = (('south_north', 'west_east'), uS[0]) xrOut["SHEAR_V_" + str(l)] = (('south_north', 'west_east'), vS[0]) xrOut["SHEAR_MAG_" + str(l)] = (('south_north', 'west_east'), spd[0]) del (uS) del (vS) del (spd) ## ## - AFWA Hail Diagnostic if (_routines.need_afwa_hail): afwaHail = ArrayTools.fetch_variable(daskArray, "AFWA_HAIL") xrOut["AFWA_HAIL"] = (('south_north', 'west_east'), afwaHail) del (afwaHail) ## ## - AFWA Tornado Diagnostic if (_routines.need_afwa_tor): afwaTor = ArrayTools.fetch_variable(daskArray, "AFWA_TORNADO") xrOut["AFWA_TORNADO"] = (('south_north', 'west_east'), afwaTor) del (afwaTor) ## ## - Done Calculations ## # Save our variables to the output file. print("Saving output file.") timeOut = "0" + str(elapsedHours) if elapsedHours < 10 else str( elapsedHours) xrOut.to_netcdf(targetDir + "/WRFPRS_F" + timeOut + ".nc") #Done. return 0