Changeset 9040 for trunk/anuga_work/development/gareth/experimental
- Timestamp:
- Dec 10, 2013, 12:31:52 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/anuga_work/development/gareth/experimental/bal_and/plot_utils.py
r9035 r9040 494 494 495 495 496 496 ## FOLLOWING FUNCTION MODIFIED FROM STACK OVERFLOW 497 def make_grid(data, lats, lons, fileName, EPSG_CODE=None): 498 """ 499 Convert data,lats,lons to a georeferenced raster tif 500 INPUT: data -- array with desired raster cell values 501 lats -- 1d array with 'latitude' or 'y' range 502 lons -- 1D array with 'longitude' or 'x' range 503 fileName -- name of file to write to 504 EPSG_CODE -- Integer code with projection information in EPSG format 505 """ 506 try: 507 import gdal 508 import osr 509 except: 510 raise Exception, 'Cannot find gdal and/or osr python modules' 511 512 #data = scipy.random.rand(5,6) 513 #lats = scipy.array([-5.5, -5.0, -4.5, -4.0, -3.5]) 514 #lons = scipy.array([135.0, 135.5, 136.0, 136.5, 137.0, 137.5]) 515 #import pdb 516 #pdb.set_trace() 517 xres = lons[1] - lons[0] 518 yres = lats[1] - lats[0] 519 520 ysize = len(lats) 521 xsize = len(lons) 522 523 # Assume data/lats/longs refer to cell centres, as per gdal 524 #ulx = lons[0] - (xres / 2.) 525 #uly = lats[-1] - (yres / 2.) 526 llx = lons[0] - (xres / 2.) 527 lly = lats[0] - (yres / 2.) 528 529 driver = gdal.GetDriverByName('GTiff') 530 ds = driver.Create(fileName, xsize, ysize, 1, gdal.GDT_Float32) 531 532 # this assumes the projection is Geographic lat/lon WGS 84 533 srs = osr.SpatialReference() 534 srs.ImportFromEPSG(EPSG_CODE) 535 ds.SetProjection(srs.ExportToWkt()) 536 537 #gt = [ulx, xres, 0, uly, 0, yres ] 538 gt = [llx, xres, 0, lly, 0, yres ] 539 ds.SetGeoTransform(gt) 540 541 outband = ds.GetRasterBand(1) 542 outband.WriteArray(data) 543 544 ds = None 545 return 546 547 ################################################################################## 548 549 def Make_Geotif(swwFile, output_quantity='depth', 550 myTimeStep=1, CellSize=5.0, 551 lower_left=None, upper_right=None, 552 EPSG_CODE=None, 553 velocity_extrapolation=True, 554 min_allowed_height=1.0e-05, 555 output_dir='TIFS', 556 verbose=False): 557 """ 558 Make a georeferenced tif by nearest-neighbour interpolation of sww file outputs. 559 INPUTS: swwFile -- name of sww file 560 output_quantity -- quantity to plot ('depth' or 'velocity' or 'stage' or 'elevation') 561 myTimeStep -- time-index of swwFile to plot, or 'last', or 'max' 562 CellSize -- approximate pixel size for output raster [adapted to fit lower_left / upper_right] 563 lower_left -- [x0,y0] of lower left corner. If None, use extent of swwFile. 564 upper_right -- [x1,y1] of upper right corner. If None, use extent of swwFile. 565 EPSG_CODE -- Projection information as an integer EPSG code (e.g. 3123 for PRS92 Zone 3). 566 Google for info on EPSG Codes 567 velocity_extrapolation -- Compute velocity assuming the code extrapolates with velocity (instead of momentum)? 568 min_allowed_height -- Minimum allowed height from ANUGA 569 output_dir -- Write outputs to this directory 570 571 """ 572 try: 573 import gdal 574 import osr 575 import scipy.io 576 import scipy.interpolate 577 import anuga 578 from anuga.utilities import plot_utils as util 579 import os 580 except: 581 raise Exception, 'Required modules not installed for mkgeotif' 582 583 584 if(EPSG_CODE is None): 585 raise Exception, 'Must specify an integer EPSG_CODE describing the file projection' 586 ### 587 ## INPUTS 588 ### 589 #swwFile='taguig_rainfall_ondoy.sww' 590 #myTimeStep=40 # Index of time-slice in sWW to plot 591 ## Plot extents 592 #lower_left=None #[x0,y0] 593 #upper_right=None #[x1,y1] 594 ## Projection as an epsg code 595 #EPSG_CODE=3123 596 ## CellSize for output raster 597 #CellSize=5.0 # APPROXIMATE CellSize 598 ## ANUGA Parameters 599 #min_allowed_height=1.0e-05 600 #velocity_extrapolation=True 601 602 #output_quantity='depth' # 'velocity' 'depth' 'stage' 603 #output_dir='TEST' 604 605 ## 606 # END INPUTS 607 ## 608 609 # Make output_dir 610 try: 611 os.mkdir(output_dir) 612 except: 613 pass 614 615 # Read in ANUGA outputs 616 # FIXME: It would be good to support reading of data subsets 617 if(verbose): 618 print 'Reading sww File ...' 619 p=util.get_output(swwFile,min_allowed_height) 620 p2=util.get_centroids(p,velocity_extrapolation) 621 swwIn=scipy.io.netcdf_file(swwFile) 622 xllcorner=swwIn.xllcorner 623 yllcorner=swwIn.yllcorner 624 625 if(myTimeStep=='last'): 626 myTimeStep=len(p.time)-1 627 628 629 if(verbose): 630 print 'Extracting required data ...' 631 # Get ANUGA points 632 swwX=p2.x+xllcorner 633 swwY=p2.y+yllcorner 634 635 if(myTimeStep!='max'): 636 swwStage=p2.stage[myTimeStep,:] 637 swwHeight=p2.height[myTimeStep,:] 638 swwVel=p2.vel[myTimeStep,:] 639 timestepString=str(round(p2.time[myTimeStep])) 640 else: 641 swwStage=p2.stage.max(axis=0) 642 swwHeight=p2.height.max(axis=0) 643 swwVel=p2.vel.max(axis=0) 644 timestepString='max' 645 646 #import pdb 647 #pdb.set_trace() 648 649 # Make name for output file 650 output_name=output_dir+'/'+os.path.splitext(os.path.basename(swwFile))[0] + '_'+\ 651 output_quantity+'_'+timestepString+\ 652 '_'+str(myTimeStep)+'.tif' 653 654 if(verbose): 655 print 'Computing grid of output locations...' 656 # Get points where we want raster cells 657 if(lower_left is None): 658 lower_left=[swwX.min(),swwY.min()] 659 if(upper_right is None): 660 upper_right=[swwX.max(),swwY.max()] 661 662 nx=round((upper_right[0]-lower_left[0])*1.0/(1.0*CellSize)) + 1 663 xres=(upper_right[0]-lower_left[0])*1.0/(1.0*(nx-1)) 664 desiredX=scipy.arange(lower_left[0], upper_right[0],xres ) 665 ny=round((upper_right[1]-lower_left[1])*1.0/(1.0*CellSize)) + 1 666 yres=(upper_right[1]-lower_left[1])*1.0/(1.0*(ny-1)) 667 desiredY=scipy.arange(lower_left[1], upper_right[1], yres) 668 669 gridX, gridY=scipy.meshgrid(desiredX,desiredY) 670 671 # Make functions to interpolate quantities at points 672 if(verbose): 673 print 'Making interpolation functions...' 674 swwXY=scipy.array([swwX[:],swwY[:]]).transpose() 675 #swwXY=scipy.array([scipy.concatenate(gridX), scipy.concatenate(gridY)]).transpose() 676 if(output_quantity=='stage'): 677 qFun=scipy.interpolate.NearestNDInterpolator(swwXY,swwStage.transpose()) 678 elif(output_quantity=='velocity'): 679 qFun=scipy.interpolate.NearestNDInterpolator(swwXY,swwVel.transpose()) 680 elif(output_quantity=='elevation'): 681 qFun=scipy.interpolate.NearestNDInterpolator(swwXY,p2.elev.transpose()) 682 elif(output_quantity=='depth'): 683 qFun=scipy.interpolate.NearestNDInterpolator(swwXY,swwHeight.transpose()) 684 else: 685 raise Exception, 'output_quantity not available -- check if it is spelled correctly' 686 687 if(verbose): 688 print 'Interpolating' 689 gridq=qFun(scipy.array([scipy.concatenate(gridX),scipy.concatenate(gridY)]).transpose()).transpose() 690 gridq.shape=(len(desiredY),len(desiredX)) 691 #gridq=gridq.transpose() 692 693 if(verbose): 694 print 'Making raster ...' 695 make_grid(gridq,desiredY,desiredX, output_name,EPSG_CODE)
Note: See TracChangeset
for help on using the changeset viewer.