def Draw(self,x,y,time): if self.Time2Exit==None: self.Time2Exit=time if time-self.Time2Exit>10.0: Bladex.Quit() elif time-self.Time2Exit>5.0: self.CurrentBitmap = self.Bitmap2 Raster.SetPosition(0,0) Raster.DrawImage(640,480,"BGR","Stretch",self.CurrentBitmap.GetData()) self.DefDraw(x,y,time)
def __init__(self,Parent,Menudesc,StackMenu,VertPos=0): #print "MenuTree.__init__()" #print "MenuTree initial refcount",sys.getrefcount(self) Width,Height=Raster.GetSize() try: Width,Height=Menudesc["Size"] except KeyError: pass B_MenuFrameWidget.__init__(self,Parent,"MenuTree"+Menudesc["Name"],Width,Height,VertPos) self.Menudesc=Menudesc ValidIndex = 0 isValidIndex = 0 for i in Menudesc["ListDescr"]: m_class=B_MenuItemTextNoFX try: m_class=i["Kind"] except KeyError: pass vsep=0 try: vsep=i["VSep"] except KeyError: pass wSubMenu=m_class(self,i,StackMenu) if not isValidIndex: if wSubMenu.AcceptsFocus(): isValidIndex = 1 else: ValidIndex = ValidIndex+1 HPos=0.5 HIndicator=BUIx.B_FrameWidget.B_FR_HRelative HAnchor=BUIx.B_FrameWidget.B_FR_HCenter try: PosDscr=i["Position"] HPos=PosDscr[0] HIndicator=PosDscr[1] HAnchor=PosDscr[2] except KeyError: pass try: PosDscr=i["PositionEx"] HPos=PosDscr[0] HIndicator=PosDscr[1] HAnchor=PosDscr[2] except KeyError: B_MenuFrameWidget.AddMenuElement(self,wSubMenu,vsep,HPos,HIndicator,HAnchor) if Menudesc.has_key("iFocus"): self.SetFocus_Idx(Menudesc["iFocus"]) else: self.SetFocus_Idx(ValidIndex)
def clip_cloud(self): # Clip # Cloud reprojected file rc = os.path.join(self.tmp_dir, f"rc{self.scene_file_name}.tif") r = R.Raster(img_path=rc) # Only clip raster if it intersects limit of project if r.intersects_trace_outline_aoi(): c = CR.ClipRaster( img_path=rc, tmp_dir=self.tmp_dir, scene_file_name=self.scene_file_name, output_dir=self.output_dir, output_file_name=f"fmask_{self.output_file_name}") c.clip_raster_by_mask() else: dst = os.path.join(self.output_dir, f"fmask_{self.scene_file_name}") shutil.move(src=rc, dst=dst)
# for k, S in enumerate(mesh.surfaces()): # mesh = MT.TSurf.make(*S.as_arrays()) # for k, S in enumerate(mesh.surfaces()): # mesh = MT.TSurf.make(*S.as_arrays()) # MT.to_vtu(mesh, "surface%03d.vtu" % k) print("adding dtm") filename = "raster2750.txt" import Raster info = Raster.RasterInfo(filename) raw_data = np.loadtxt(filename, skiprows=6) data = np.ma.array(raw_data, mask=raw_data == float(info.nodata)) centers = info.centers() xyz = np.transpose(np.vstack([a.ravel() for a in centers] + [raw_data.ravel(),])) xyz = xyz[raw_data.ravel() != float(info.nodata)] # center dtm xyz[:, 0] -= 1010922 xyz[:, 1] -= 6810940 # x = np.linspace(xmin-2*Lh, xmax+2*Lh) # y = np.linspace(ymin-2*Lh, ymax+2*Lh)
def Draw(self,x,y,time): Raster.SetPosition(0,0) Raster.DrawImage(640,480,"BGR","Stretch",self.Bitmap.GetData()) self.DefDraw(x,y,time)
def Draw(self,x,y,time): import string import GotoMapVars import Menu char = Bladex.GetEntity("Player1") x,y = Raster.GetSize() Raster.SetPosition((x - 640)/2, (y - 480)/2) Map = string.lower(Bladex.GetCurrentMap()) Specials = 1 Inventory = char.GetInventory() HaveCrush = 0 for i in range(Inventory.nWeapons): Weapon = Inventory.GetWeapon(i) RootWeapon = Bladex.GetEntity(Weapon) if RootWeapon.Kind == "CrushHammer": HaveCrush = 1 if Map in GotoMapVars.BackLevelNames: Specials = 0 else: if (Map in GotoMapVars.LevelNames and GotoMapVars.LevelNames.index(Map) > 6) or ((char.Kind == "Dwarf_N") and HaveCrush): Specials = 0 # Horizontal buttons range checking if self.image < Specials: self.image = (self.NumImages - (1 + self.addone)) if self.image > (self.NumImages - (1 + self.addone)): self.image = Specials # Vertical -Text- options range checking if self.Text < 0: self.Text = self.NumTexts - 1 if self.Text > self.NumTexts - 1: self.Text = 0 # Set logic conditions if self.image == 0: Raster.DrawImage(640,480,"BGR","Normal",self.Specials.GetData()) if self.image == 1: Raster.DrawImage(640,480,"BGR","Normal",self.Weapons.GetData()) if self.image == 2: Raster.DrawImage(640,480,"BGR","Normal",self.Habilities.GetData()) if self.image == 3: Raster.DrawImage(640,480,"BGR","Normal",self.Items.GetData()) if self.image == 4: Raster.DrawImage(640,480,"BGR","Normal",self.MapText.GetData()) if self.image == 5: if self.Text == 0: Raster.DrawImage(640,480,"BGR","Normal",self.Tablets.GetData()) if self.Text == 1: Raster.DrawImage(640,480,"BGR","Normal",self.Text1.GetData()) if self.Text == 2: Raster.DrawImage(640,480,"BGR","Normal",self.Text2.GetData()) if self.Text == 3: Raster.DrawImage(640,480,"BGR","Normal",self.Text3.GetData()) if ((self.image == 5)): # and # ( # (self.TextsAvail[1]) or # (self.TextsAvail[2]) or # (self.TextsAvail[3]) # )): Menu.TBUDSoundAble = 1 else: Menu.TBUDSoundAble = 0 self.DefDraw(x,y,time)
def __init__(self,Parent,Menudesc,StackMenu,VertPos=0): import Language import GotoMapVars self.image = 1 self.NumImages = 6 self.Text = 0 self.NumTexts = 4 self.TextsAvail = [0, 0, 0, 0] self.vidw = 1 self.vidh = 1 self.addone = 0 char = Bladex.GetEntity("Player1") print char.Kind Raster.UnifyRenderBuffers() if "ORCMURAL" in GotoMapVars.BaList: self.TextsAvail[1] = 1 if "ISLANDMURAL" in GotoMapVars.BaList: self.TextsAvail[2] = 1 if "NEJEVMURAL" in GotoMapVars.BaList: self.TextsAvail[3] = 1 # Range Vars Inventory = Bladex.GetEntity("Player1").GetInventory() if (not Inventory.nTablets > 0) and not (1 in GotoMapVars.PlacedTablets) and not (self.TextsAvail[1] or self.TextsAvail[2] or self.TextsAvail[3]): self.addone = 1 if "SALATABLILLAS" in GotoMapVars.BaList or (1 in GotoMapVars.PlacedTablets) or (Inventory.nTablets > 0): self.addone = 0 self.TextsAvail[0] = 1 self.Specials = BBLib.B_BitMap24() self.Specials.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current + "\\" + char.Kind + "\\plantillaspecials.jpg") self.Items = BBLib.B_BitMap24() self.Items.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current +"\\Items\\plantillaGitems.jpg") self.Weapons = BBLib.B_BitMap24() self.Weapons.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current + "\\" + char.Kind + "\\plantillaweapons.jpg") self.Habilities = BBLib.B_BitMap24() self.Habilities.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current + "\\" + char.Kind + "\\plantillahabilities.jpg") self.MapText = BBLib.B_BitMap24() if not self.MapText.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current + "\\" + char.Kind + "\\" + Bladex.GetCurrentMap() + ".jpg"): self.MapText.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current + "\\" + Bladex.GetCurrentMap() + ".jpg") if (Bladex.GetCurrentMap() == "Tower_m16"): inv = Bladex.GetEntity("Player1").GetInventory() for i in range(inv.nWeapons): if (Bladex.GetEntity(inv.GetWeapon(i)).Kind == "BladeSword2") or (Bladex.GetEntity(inv.GetWeapon(i)).Kind == "BladeSword2Barbarian"): self.MapText.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current + "\\" + "conTower_m16.jpg") else: self.MapText.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current + "\\" + "sinTower_m16.jpg") self.Tablets = BBLib.B_BitMap24() if (Inventory.nTablets > 0) or (1 in GotoMapVars.PlacedTablets): self.Tablets.ReadFromFile("..\\..\\Data\\TB\\"+ Language.Current + "\\tablillas.jpg") self.Text1 = BBLib.B_BitMap24() if self.TextsAvail[1]: self.Text1.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current + "\\muralorc.jpg") self.Text2 = BBLib.B_BitMap24() if self.TextsAvail[2]: self.Text2.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current + "\\muralisla1.jpg") self.Text3 = BBLib.B_BitMap24() if self.TextsAvail[3]: self.Text3.ReadFromFile("..\\..\\Data\\TB\\" + Language.Current + "\\muralnejev.jpg") BUIx.B_RectWidget.__init__(self,Parent,Menudesc["Name"],self.vidw,self.vidh) self.Selected=0 self.Solid=0 self.Border=0 self.SetDrawFunc(self.Draw) self.SndCorreGema=Bladex.CreateSound("..\\..\\Sounds\\golpe-2.wav","Chanje") self.SndCorreGema.Volume=0.5 self.SndCorreGema.MinDistance=1000000.0 self.SndCorreGema.MaxDistance=2000000 if self.addone == 0: while not self.TextsAvail[self.Text]: self.Text = self.Text + 1 if self.Text > self.NumTexts - 1: self.Text = 0
def RunCalibration(ConceptualModel, Paths, Basic_inputs, SpatialVarFun, SpatialVarArgs, OF, OF_args, Q_obs, OptimizationArgs, printError=None): """ ======================================================================= RunCalibration(ConceptualModel, Paths, p2, Q_obs, UB, LB, SpatialVarFun, lumpedParNo, lumpedParPos, objective_function, printError=None, *args): ======================================================================= this function runs the calibration algorithm for the conceptual distributed hydrological model Inputs: ---------- 1-ConceptualModel: [function] conceptual model and it should contain a function called simulate 1-Paths: 1-PrecPath: [String] path to the Folder contains precipitation rasters 2-Evap_Path: [String] path to the Folder contains Evapotranspiration rasters 3-TempPath: [String] path to the Folder contains Temperature rasters 4-FlowAccPath: [String] path to the Flow Accumulation raster of the catchment (it should include the raster name and extension) 5-FlowDPath: [String] path to the Flow Direction raster of the catchment (it should include the raster name and extension) 2-Basic_inputs: 1-p2: [List] list of unoptimized parameters p2[0] = tfac, 1 for hourly, 0.25 for 15 min time step and 24 for daily time step p2[1] = catchment area in km2 2-init_st: [list] initial values for the state variables [sp,sm,uz,lz,wc] in mm 3-UB: [Numeric] upper bound of the values of the parameters 4-LB: [Numeric] Lower bound of the values of the parameters 3-Q_obs: [Numeric] Observed values of discharge 6-lumpedParNo: [int] nomber of lumped parameters, you have to enter the value of the lumped parameter at the end of the list, default is 0 (no lumped parameters) 7-lumpedParPos: [List] list of order or position of the lumped parameter among all the parameters of the lumped model (order starts from 0 to the length of the model parameters), default is [] (empty), the following order of parameters is used for the lumped HBV model used [ltt, utt, rfcf, sfcf, ttm, cfmax, cwh, cfr, fc, beta, e_corr, etf, lp, c_flux, k, k1, alpha, perc, pcorr, Kmuskingum, Xmuskingum] 8-objective_function: [function] objective function to calculate the performance of the model and to be used in the calibration 9-*args: other arguments needed on the objective function Outputs: ---------- 1- st: [4D array] state variables 2- q_out: [1D array] calculated Discharge at the outlet of the catchment 3- q_uz: [3D array] Distributed discharge for each cell Example: ---------- PrecPath = prec_path="meteodata/4000/calib/prec" Evap_Path = evap_path="meteodata/4000/calib/evap" TempPath = temp_path="meteodata/4000/calib/temp" FlowAccPath = "GIS/4000/acc4000.tif" FlowDPath = "GIS/4000/fd4000.tif" ParPath = "meteodata/4000/"+"parameters.txt" p2=[1, 227.31] st, q_out, q_uz_routed = RunModel(PrecPath,Evap_Path,TempPath,DemPath, FlowAccPath,FlowDPath,ParPath,p2) """ ### inputs validation # data type assert len(Paths) == 5, "Paths should include 5 folder pathes " +str(len(Paths))+" paths are only provided" PrecPath=Paths[0] Evap_Path=Paths[1] TempPath=Paths[2] # DemPath=Paths[3] FlowAccPath=Paths[3] FlowDPath=Paths[4] assert type(PrecPath)== str, "PrecPath input should be string type" assert type(Evap_Path)== str, "Evap_Path input should be string type" assert type(TempPath)== str, "TempPath input should be string type" # assert type(DemPath)== str, "DemPath input should be string type" assert type(FlowAccPath)== str, "FlowAccPath input should be string type" assert type(FlowDPath)== str, "FlowDPath input should be string type" # input values # dem_ext=DemPath[-4:] # assert dem_ext == ".tif", "please add the extension at the end of the DEM raster path input" acc_ext=FlowAccPath[-4:] assert acc_ext == ".tif", "please add the extension at the end of the Flow accumulation raster path input" fd_ext=FlowDPath[-4:] assert fd_ext == ".tif", "please add the extension at the end of the Flow Direction path input" # check wether the path exists or not assert os.path.exists(PrecPath), PrecPath + " you have provided does not exist" assert os.path.exists(Evap_Path), Evap_Path+" path you have provided does not exist" assert os.path.exists(TempPath), TempPath+" path you have provided does not exist" # assert os.path.exists(DemPath), DemPath+ " you have provided does not exist" assert os.path.exists(FlowAccPath), FlowAccPath + " you have provided does not exist" assert os.path.exists(FlowDPath), FlowDPath+ " you have provided does not exist" # check wether the folder has the rasters or not assert len(os.listdir(PrecPath)) > 0, PrecPath+" folder you have provided is empty" assert len(os.listdir(Evap_Path)) > 0, Evap_Path+" folder you have provided is empty" assert len(os.listdir(TempPath)) > 0, TempPath+" folder you have provided is empty" # basic inputs # check if all inputs are included assert all(["p2","init_st","UB","LB","snow "][i] in Basic_inputs.keys() for i in range(4)), "Basic_inputs should contain ['p2','init_st','UB','LB'] " p2 = Basic_inputs['p2'] init_st = Basic_inputs["init_st"] UB = Basic_inputs['UB'] LB = Basic_inputs['LB'] snow = Basic_inputs['snow'] assert len(UB)==len(LB), "length of UB should be the same like LB" # check objective_function assert callable(OF) , "second argument should be a function" if OF_args== None : OF_args=[] # read data ### meteorological data prec=Raster.ReadRastersFolder(PrecPath) evap=Raster.ReadRastersFolder(Evap_Path) temp=Raster.ReadRastersFolder(TempPath) print("meteorological data are read successfully") #### GIS data # dem= gdal.Open(DemPath) acc=gdal.Open(FlowAccPath) fd=gdal.Open(FlowDPath) print("GIS data are read successfully") ### optimization # get arguments store_history=OptimizationArgs[0] history_fname=OptimizationArgs[1] # check optimization arguement assert store_history !=0 or store_history != 1,"store_history should be 0 or 1" assert type(history_fname) == str, "history_fname should be of type string " assert history_fname[-4:] == ".txt", "history_fname should be txt file please change extension or add .txt ad the end of the history_fname" print('Calibration starts') ### calculate the objective function def opt_fun(par): try: # parameters klb=float(par[-2]) kub=float(par[-1]) par=par[:-2] par_dist=SpatialVarFun(par,*SpatialVarArgs,kub=kub,klb=klb) #run the model _, q_out, q_uz_routed, q_lz_trans=Wrapper.Dist_model(ConceptualModel, acc, fd, prec, evap, temp, par_dist, p2, snow , init_st) # calculate performance of the model try: error=OF(Q_obs,q_out,q_uz_routed,q_lz_trans,*OF_args) except TypeError: # if no of inputs less than what the function needs assert 1==5, "the objective function you have entered needs more inputs please enter then in a list as *args" # print error if printError != 0: print(error) print(par) fail = 0 except: error = np.nan fail = 1 return error, [], fail ### define the optimization components opt_prob = Optimization('HBV Calibration', opt_fun) for i in range(len(LB)): opt_prob.addVar('x{0}'.format(i), type='c', lower=LB[i], upper=UB[i]) print(opt_prob) opt_engine = ALHSO(etol=0.0001,atol=0.0001,rtol=0.0001, stopiters=10, hmcr=0.5,par=0.5) #,filename='mostafa.out' Optimizer.__init__(opt_engine,def_options={ 'hms':[int,9], # Memory Size [1,50] 'hmcr':[float,0.95], # Probability rate of choosing from memory [0.7,0.99] 'par':[float,0.99], # Pitch adjustment rate [0.1,0.99] 'dbw':[int,2000], # Variable Bandwidth Quantization 'maxoutiter':[int,2e3], # Maximum Number of Outer Loop Iterations (Major Iterations) 'maxinniter':[int,2e2], # Maximum Number of Inner Loop Iterations (Minor Iterations) 'stopcriteria':[int,1], # Stopping Criteria Flag 'stopiters':[int,20], # Consecutively Number of Outer Iterations for which the Stopping Criteria must be Satisfied 'etol':[float,0.0001], # Absolute Tolerance for Equality constraints 'itol':[float,0.0001], # Absolute Tolerance for Inequality constraints 'atol':[float,0.0001], # Absolute Tolerance for Objective Function 1e-6 'rtol':[float,0.0001], # Relative Tolerance for Objective Function 'prtoutiter':[int,0], # Number of Iterations Before Print Outer Loop Information 'prtinniter':[int,0], # Number of Iterations Before Print Inner Loop Information 'xinit':[int,0], # Initial Position Flag (0 - no position, 1 - position given) 'rinit':[float,1.0], # Initial Penalty Factor 'fileout':[int,store_history], # Flag to Turn On Output to filename 'filename':[str,'parameters.txt'], # We could probably remove fileout flag if filename or fileinstance is given 'seed':[float,0.5], # Random Number Seed (0 - Auto-Seed based on time clock) 'scaling':[int,1], # Design Variables Scaling Flag (0 - no scaling, 1 - scaling between [-1,1]) }) res = opt_engine(opt_prob) return res
def Draw(self, x, y, time): if self.GetVisible() == 0: return #self.SetAlpha(0.5) foc = self.GetHasFocus() if foc != self._foc: self.SelectionFilterUpdated = 0 self.FilterIncX = 0 self.FilterUpdated = 0 self._foc = foc w, h = self.GetSize() if foc: if self.ActionName: if ActionDescriptor.has_key(self.ActionName): netwidgets.LabelName = MenuText.GetMenuText( ActionDescriptor[self.ActionName]) else: netwidgets.LabelName = " " if self.ChangingKey: mult = math.cos(time * 2) self.SetColor(80 * mult + 150, 80 * mult + 150, 45 * mult + 105) self.DefDraw(x, y, time) return elif self.SelectionFilterUpdated: Raster.SetPosition(x - self.FilterIncX, y - self.FilterIncY) Raster.DrawImage(w + 2 * self.FilterIncX, h + 2 * self.FilterIncY, "RGB", "Native", self.SelectionFilter.GetImageBuffer()) self.SetColor(252, 247, 167) self.DefDraw(x, y, time) self.SetColor(207, 144, 49) return else: ## if self.Filter is None: ## self.CreateBlur(x,y,time) ## Raster.SetPosition(x-2,y-2) ## Raster.DrawImage(w+4,h+4,"RGB","Native",self.Filter.GetImageBuffer()) ## if Raster.GetClipActive(): ## print self.Name(),"ClipActive:",Raster.GetClipWindow() ## else: ## print self.Name(),"ClipNoActive:" self.DefDraw(x, y, time) return if foc: #self.CreateDistorsion(x,y,time) #if self.Filter is None: # self.CreateBlur(x,y,time) # self.FilterUpdated=1 #sfw,sfh=self.Filter.GetDimension() #self.SelectionFilter.CopySubBuffer(self.FilterIncX-2,0,sfw,sfh,self.Filter.GetImageBuffer(),"AddInc") #self.SelectionFilter.GetFromBuffer2("Substract") #Raster.SetPosition(x-self.FilterIncX,y-self.FilterIncY) #Raster.DrawImage(w+2*self.FilterIncX,h+2*self.FilterIncY,"RGB","Native",self.SelectionFilter.GetImageBuffer()) self.SetColor(252, 247, 167) self.DefDraw(x, y, time) self.SetColor(207, 144, 49) if self.FilterIncX > 25: self.SelectionFilterUpdated = 1 else: self.SelectionFilterUpdated = 0
output_img_path = os.path.join(tmp_dir, f"r{output_file_name}.TIF") rprj = RR.RasterReproject(input_img_path, output_img_path) rprj.reproject_raster_to_epsg4674() # Clip img_path = f"{tmp_dir}/r{output_file_name}.TIF" c = CR.ClipRaster( img_path=img_path, tmp_dir=tmp_dir, scene_file_name=parameter_satellite["scene_file_name"], output_dir=output_dir, output_file_name=output_file_name) r = R.Raster(img_path=img_path) if r.intersects_trace_outline_aoi(): c.clip_raster_by_mask(band_order=[1, 2, 3]) else: import RasterTranslate as RT rt = RT.RasterTranslate(img_path=img_path, output_dir=output_dir, output_file_name=output_file_name) rt.translate_8bit(band_order=[1, 2, 3]) # Make pyramid img_path = os.path.join(output_dir, f"{output_file_name}.TIF") PR.PyramidRaster(img_path=img_path).create_img_pyramid() # Save when file was processed in postgres database