Skip to content

Commit 263a28a

Browse files
committed
Changes from main, and removal of output kalpana directory
1 parent 5ff951a commit 263a28a

2 files changed

Lines changed: 76 additions & 23 deletions

File tree

kalpana/downscaling.py

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ def runStatic(ncFile, levels, epsgOut, pathOut, grassVer, pathRasFiles, rasterF
656656
same path of the extracted shape file (pathout).
657657
createGrassLocation: boolean. DEFAULT True
658658
True for creating a new location and loading DEMs, false to use an existing location with DEMs already imported
659-
createLocMethod: str. DEFAULT 'from_raster'
659+
createLocMethod: str. DEFAULT 'from_raster'
660660
Two options "from_epsg" (default) or "from_raster" otherwise an error will be thrown.
661661
attrCol: str. DEFAULT 'avgVal'
662662
name of the attribute column
@@ -762,7 +762,7 @@ def runStatic(ncFile, levels, epsgOut, pathOut, grassVer, pathRasFiles, rasterF
762762
logger.info(f'Output files saved on: {pathaux}') # Changed
763763

764764
def meshRepLen2raster(fort14, epsgIn, epsgOut, pathOut, grassVer, pathRasFiles, rasterFiles, subDomain=None,
765-
nameGrassLocation=None, createGrassLocation=True, createLocMethod='from_raster'):
765+
nameGrassLocation=None, createGrassLocation=True, createLocMethod='from_raster', exportDEM=False):
766766
''' Function to rasterize mesh shapefile created from the fort.14 file
767767
Parameters
768768
fort14: str
@@ -786,10 +786,10 @@ def meshRepLen2raster(fort14, epsgIn, epsgOut, pathOut, grassVer, pathRasFiles,
786786
same path of the extracted shape file (pathout).
787787
createGrassLocation: boolean. DEFAULT True
788788
True for creating a new location and loading DEMs, false to use an existing location with DEMs already imported
789-
createLocMethod: str. DEFAULT 'from_raster'
790-
Two options "from_epsg" (default) or "from_raster" otherwise an error will be thrown.
789+
createLocMethod: str. DEFAULT 'from_raster'
790+
Two options "from_epsg" (default) or "from_raster" otherwise an error will be thrown.
791791
Returns
792-
NOne
792+
None
793793
'''
794794
## create gdf from fort14 file with elements as geometries
795795
t0 = time.time()
@@ -829,6 +829,9 @@ def meshRepLen2raster(fort14, epsgIn, epsgOut, pathOut, grassVer, pathRasFiles,
829829
## setup grass env
830830
setGrassEnv(grassVer, pathGrassLocation, createGrassLocation, gs, gsetup,
831831
pathRasFiles, rasterFiles, createLocMethod, epsgOut)
832+
if exportDEM == True:
833+
gs.run_command('r.out.gdal', input = 'dem', flags = 'cm', format = 'GTiff', nodata = -9999,
834+
output = os.path.join(os.path.dirname(pathOut), 'downscaling_dem.tif'))
832835
## get minimum area
833836
minArea = gdfMesh.elemArea.min()
834837
t0 = time.time()
@@ -907,3 +910,46 @@ def reprojectRas(filein, pathout, epsgOut=None, res='same'):
907910
rasOut.rio.to_raster(os.path.join(pathout, bname + f'_epsg{epsgOut}_res{res}.tif'), driver="COG") # Changed
908911
logger.info('Wrote '+bname+' to COG') # Changed
909912

913+
def mergeDEMs(grassVer, pathRasFiles, rasterFiles, pathOut, epsgOut,
914+
nameGrassLocation=None, createGrassLocation=True, createLocMethod='from_raster'):
915+
''' Function to rasterize mesh shapefile created from the fort.14 file
916+
Parameters
917+
grassVer: float
918+
Version of the grass software (The code was writen for v8.0).
919+
pathRasters: str
920+
path of the raster files
921+
rasterFiles: list or str
922+
name(s) of the raster file(s). If 'all' is input, all files in pathRasters are used
923+
pathOut: str
924+
full path of the output DEM
925+
nameGrassLocation: str. DEFAULT None
926+
path and name of the grass location. If None the grass location will be called 'grassLoc' and save in the
927+
same path of the extracted shape file (pathout).
928+
createGrassLocation: boolean. DEFAULT True
929+
True for creating a new location and loading DEMs, false to use an existing location with DEMs already imported
930+
createLocMethod: str. DEFAULT 'from_raster'
931+
Two options "from_epsg" (default) or "from_raster" otherwipathOut, epsgOut,se an error will be thrown.
932+
Returns
933+
None
934+
'''
935+
pathaux = os.path.dirname(pathOut)
936+
## add grass to the environment variables
937+
grassEnvVar(grassVer)
938+
## import grass
939+
import grass.script as gs
940+
import grass.script.setup as gsetup
941+
## grass location path
942+
if nameGrassLocation == None:
943+
pathGrassLocation = os.path.join(pathaux, 'grassLoc')
944+
else:
945+
pathGrassLocation = os.path.join(pathaux, nameGrassLocation)
946+
947+
if rasterFiles == 'all':
948+
rasterFiles = os.listdir(pathRasFiles)
949+
950+
## setup grass env
951+
setGrassEnv(grassVer, pathGrassLocation, createGrassLocation, gs, gsetup,
952+
pathRasFiles, rasterFiles, createLocMethod, epsgOut)
953+
954+
gs.run_command('r.out.gdal', input = 'dem', flags = 'cm', format = 'GTiff', nodata = -9999,
955+
output = pathOut)

kalpana/kalpana.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,16 @@ def main(args):
235235
if not os.path.exists(outputDir):
236236
mode = 0o777
237237
os.makedirs(outputDir, mode, exist_ok=True)
238+
logger.info('Made directory '+outputDir)
239+
else:
240+
logger.info('Directory '+outputDir+' already made.')
241+
242+
if not os.path.exists(finalDir):
243+
mode = 0o777
238244
os.makedirs(finalDir, mode, exist_ok=True)
239-
logger.info('Made directories '+outputDir+ ' and '+finalDir+'.')
245+
logger.info('Made directory '+finalDir)
240246
else:
241-
logger.info('Directories '+outputDir+' and '+finalDir+' already made.')
247+
logger.info('Directory '+finalDir+' already made.')
242248

243249
# log start of runStatic run
244250
logger.info('Start runScript with the following inputs: '+runScript+', '+str(epsgIn)+', '+str(epsgOut)+', '+pathOut+', '+grassVer+', '+ncFile+', '+meshFile+', '+conLevelsLog+', '+vUnitIn+', '+vUnitOut+', '+adcircVar+', '+conType+', '+str(subDomain)+', '+str(epsgSubDom)+', '+str(exportMesh)+', '+dzFile+', '+str(zeroDif)+', '+nameGrassLocation+', '+str(createGrassLocation)+', '+createLocMethod+', '+attrCol+', '+str(repLenGrowing)+', '+str(compAdcirc2dem)+', '+str(floodDepth)+', '+clumpThreshold+', '+str(perMinElemArea)+', '+str(ras2vec)+', '+str(exportOrg))
@@ -250,15 +256,16 @@ def main(args):
250256
compAdcirc2dem, floodDepth, clumpThreshold, perMinElemArea, ras2vec, exportOrg)
251257

252258
# move cog tiff to final directory
253-
for finalPathFile in glob.glob(outputDir+'*_epsg4326.tif'):
254-
try:
255-
shutil.move(finalPathFile, finalDir)
256-
logger.info('Moved cog file '+finalPathFile.split("/")[-1]+' to '+finalDir+' directory.')
257-
shutil.rmtree('/data/'+modelRunID+'/kalpana')
258-
logger.info('Removed TIFF file /data/'+modelRunID+'/kalpana.')
259-
except OSError as err:
260-
logger.error('Failed to move cog file '+finalPathFile.split("/")[-1]+' to '+finalDir+' directory.')
261-
sys.exit(1)
259+
finalPathFile = glob.glob(outputDir+'*_epsg4326.tif')[0]
260+
logger.info('The length of the finalPathFile is: '+str(len(finalPathFile)))
261+
try:
262+
shutil.move(finalPathFile, finalDir)
263+
logger.info('Moved cog file '+finalPathFile+' to '+finalDir+' directory.')
264+
shutil.rmtree('/data/'+modelRunID+'/kalpana', ignore_errors=True)
265+
logger.info('Removed TIFF file /data/'+modelRunID+'/kalpana.')
266+
except OSError as err:
267+
logger.error(err)
268+
sys.exit(1)
262269
else:
263270
logger.info('ncFile '+ncFile+' does not use the NCSC_SAB_v1.22 grid or the hsofs grid, it uses the '+grid+' grid, so do not process')
264271

@@ -374,13 +381,13 @@ def main(args):
374381

375382
# create cog
376383
# move cog tiff to final directory
377-
for finalPathFile in glob.glob(outputDir+'*_epsg4326.tif'):
378-
try:
379-
shutil.move(finalPathFiles, finalDir)
380-
logger.info('Created cog file '+finalPathFiles.split("/")[-1]+' and move to '+finalDir+' directory.')
381-
except OSError as err:
382-
logger.error('Failed to create cog file '+finalPathFiles.split("/")[-1]+' and move to '+finalDir+' directory.')
383-
sys.exit(1)
384+
finalPathFile = glob.glob(outputDir+'*_epsg4326.tif')[0]
385+
try:
386+
shutil.move(finalPathFile, finalDir)
387+
logger.info('Created cog file '+finalPathFile+' and move to '+finalDir+' directory.')
388+
except OSError as err:
389+
logger.error(err)
390+
sys.exit(1)
384391

385392
if __name__ == "__main__":
386393
''' Takes argparse inputs and passes theme to the main function

0 commit comments

Comments
 (0)