<commit_before>import grass.script as grass

accum_thresh = 30

tertiary = 'B60'

# make sure the region is set correctly
grass.run_command('g.region', rast='srtm-dem')

# create catchment mask
grass.run_command('v.extract',
                  flags = '-o',
                  input = 'sa_quaternaries',
                  output = '%s_py' % tertiary,
                  where="TERTIARY = '%s'" % tertiary)

grass.run_command('v.dissolve',
                  flags = '-o',
                  input = '%s_py' % tertiary,
                  output = '%s_dissolve_py' % tertiary,
                  column='TERTIARY')

grass.run_command('v.buffer',
                  flags = '-o',
                  input = '%s_dissolve_py' % tertiary,
                  output = '%s_buffer_py' % tertiary,
                  distance='5000.0')

grass.run_command('v.to.rast',
                  flags = '-o',
                  input = '%s_buffer_py' % tertiary,
                  out = '%s_mask_py' % tertiary,
                  use = 'val')

# use mask to extract catchment from DEM
grass.run_command('r.mask', input='%s_mask_py' % tertiary)
grass.mapcalc('%s_dem=srtm_dem' % tertiary, overwrite=True)
grass.run_command('r.mask', flags='r')

# obtain parameters from DEM
grass.run_command('r.slope.aspect',
                  flags = '-o',
                  elevation = '%s_dem' % tertiary,
                  slope = '%s_slope' % tertiary)

grass.run_command('r.watershed',
                  flags = '-o',
                  elevation = '%s_dem' % tertiary,
                  accumulation = '%s_accum' % tertiary,
                  drainage = '%s_dir' % tertiary,
                  stream = '%s_str' % tertiary,
                  threshold=250)

# the stream raster usually requires thinning
grass.run_command('r.thin',
                  flags = '-o',
                  input = '%s_str' % tertiary,
                  out = '%s_str_thin' % tertiary)

grass.run_command('r.to.vect',
                  input = '%s_str_thin' % tertiary,
                  out = '%s_streams' % tertiary,
                  feature = 'line')

grass.mapcalc('%s_accum_thresh=abs(%s_accum) >= %s' % (tertiary, tertiary, accum_thresh),<commit_msg>ENH: Find the outlet point on catchment boundary<commit_after>import numpy as np
import grass.script as grass

accum_thresh = 30

tertiary = 'B60'

# make sure the region is set correctly
grass.run_command('g.region', rast='srtm-dem')

# create catchment mask
grass.run_command('v.extract',
                  flags = '-o',
                  input = 'sa_quaternaries',
                  output = '%s_py' % tertiary,
                  where="TERTIARY = '%s'" % tertiary)

grass.run_command('v.dissolve',
                  flags = '-o',
                  input = '%s_py' % tertiary,
                  output = '%s_dissolve_py' % tertiary,
                  column='TERTIARY')

grass.run_command('v.buffer',
                  flags = '-o',
                  input = '%s_dissolve_py' % tertiary,
                  output = '%s_buffer_py' % tertiary,
                  distance='5000.0')

grass.run_command('v.to.rast',
                  flags = '-o',
                  input = '%s_buffer_py' % tertiary,
                  out = '%s_mask_py' % tertiary,
                  use = 'val')

# use mask to extract catchment from DEM
grass.run_command('r.mask', input='%s_mask_py' % tertiary)
grass.mapcalc('%s_dem=srtm_dem' % tertiary, overwrite=True)
grass.run_command('r.mask', flags='r')

# obtain parameters from DEM
grass.run_command('r.slope.aspect',
                  flags = '-o',
                  elevation = '%s_dem' % tertiary,
                  slope = '%s_slope' % tertiary)

grass.run_command('r.watershed',
                  flags = '-o',
                  elevation = '%s_dem' % tertiary,
                  accumulation = '%s_accum' % tertiary,
                  drainage = '%s_dir' % tertiary,
                  stream = '%s_str' % tertiary,
                  threshold=250)

# the stream raster usually requires thinning
grass.run_command('r.thin',
                  flags = '-o',
                  input = '%s_str' % tertiary,
                  out = '%s_str_thin' % tertiary)

grass.run_command('r.to.vect',
                  flags = '-o',
                  input = '%s_str_thin' % tertiary,
                  out = '%s_streams' % tertiary,
                  feature = 'line')

# Find outlet point and upstream cells
grass.run_command('v.patch',
                  flags = '-o',
                  input = '%s_streams,%s_dissolve_py' % (tertiary, tertiary),
                  out = '%s_streams_catch' % tertiary)

grass.run_command('v.clean',
                  flags = '-o',
                  input = '%s_streams_catch' % tertiary,
                  out = '%s_streams_catch_clean' % tertiary,
                  tool = 'break',
                  error = '%s_catch_outlet' % tertiary)

grass.run_command('v.out.ascii',
                  flags = '-o',
                  input = '%s_catch_outlet' % tertiary,
                  out = '%s_catch_outlet.txt' % tertiary,
                  format = 'point',
                  fs = 'space')

easting, northing = np.loadtxt('%s_catch_outlet.txt' % tertiary)

grass.run_command('r.water.outlet',
                  drain = '%s_dir' % tertiary,
                  east = easting,
                  north = northing,
                  basin = '%s_catch_mask' % tertiary)

grass.run_command('r.null',
                  map = '%s_catch_mask' % tertiary,
                  setnull = '0')

grass.mapcalc('%s_accum_thresh=abs(%s_accum) >= %s' % (tertiary, tertiary, accum_thresh),
{'fraction_non_alphanumeric': 0.13164466642882625, 'fraction_numerical': 0.004459507670353193, 'mean_word_length': 1.677650429799427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5690559', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1626, 'n_words': 346}