Skip to content

Commit

Permalink
ceno stuff updated
Browse files Browse the repository at this point in the history
  • Loading branch information
basaks committed Sep 25, 2023
1 parent 63fccc3 commit 5915dd5
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 7 deletions.
110 changes: 103 additions & 7 deletions scripts/ceno_stuff.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
import rasterio as rio
near_rough = "/g/data/jl14/new_ceno_inputs/KD_8000_80m.tif"
far_smooth = "/g/data/jl14/new_ceno_inputs/KD_3500_80m.tif"

far_smooth = "/g/data/jl14/new_ceno_inputs/KD_8000_80m.tif"
near_rough = "/g/data/jl14/new_ceno_inputs/KD_3500_new_80m.tif"
scale = "/g/data/jl14/80m_covarites/proximity/P_Dist_Meso.tif"

src = rio.open(far_smooth)
Expand All @@ -10,9 +11,12 @@
Z = rio.open(scale).read(masked=True)
mask = far.mask

greater = Z > 0.43
less = Z < 0.05
W = (0.43 - Z)/(0.43 - 0.05)
lower_end = 0.02
higher_end = 0.1

greater = Z > higher_end
less = Z < lower_end
W = (higher_end - Z) / (higher_end - lower_end)
W[greater] = 0
W[less] = 1
W = np.ma.MaskedArray(data=W, mask=far.mask)
Expand All @@ -22,7 +26,8 @@
profile = src.profile
profile.update({'driver': 'COG', 'BIGTIFF': 'YES'})

with rio.open(f'kd_8000_3500_dist_meso_weighted_average_quadratic_bigtiff.tif', 'w', ** profile, compress='lzw') as dst:
with rio.open(f'kd_3500_new_8000_dist_meso_weighted_average_quadratic_bigtiff.tif', 'w', **profile,
compress='lzw') as dst:
dst.write(output)


Expand Down Expand Up @@ -60,4 +65,95 @@

# for the DEM use /g/data/jl14/80m_covarites/proximity/P_Dist_Meso.tif to determing the weights and transition to combine the two DEM grids.
# Use the values 0.01 to 0.15 i.e. < 0.01 100% grid 1 and > 0.15 100% of grid 2
# grid 1 == /g/data/jl14/80m_covarites/terrain/T_DEM_S.tif and grid 2 == /g/data/jl14/new_ceno_inputs/Spline_DEM_80.tif
# grid 1 == /g/data/jl14/80m_covarites/terrain/T_DEM_S.tif and grid 2 == /g/data/jl14/new_ceno_inputs/Spline_DEM_80.tif


import numpy as np
import rasterio as rio
near_rough = "DEM_fill_smooth_mag_2_cog_nan.tif"
far_smooth = "/g/data/jl14/new_ceno_inputs/zero_blend.tif"
scale = "/g/data/jl14/80m_covarites/proximity/P_Dist_Meso.tif"

src = rio.open(near_rough)
far = rio.open(far_smooth).read(masked=True)
near = rio.open(near_rough).read(masked=True)
Z = rio.open(scale).read(masked=True)
mask = far.mask

lower_end = 0.01
higher_end = 0.08

greater = Z > higher_end
less = Z < lower_end
W = (higher_end - Z)/(higher_end - lower_end)
W[greater] = 0
W[less] = 1
W = np.ma.MaskedArray(data=W, mask=far.mask)

output = far * (1 - W ** 2) + near * W ** 2

profile = src.profile
profile.update({'driver': 'COG', 'BIGTIFF': 'YES', 'compress': 'lzw'})

with rio.open(f'dem_fill_mag2_zero_blend_dist_meso_weighted_average_quadratic_bigtiff.tif', 'w', ** profile) as dst:
dst.write(output)


import numpy as np
import rasterio as rio
near_rough = "/g/data/jl14/80m_covarites/terrain/T_MRVBF_S.tif"
# far_smooth = "/g/data/jl14/new_ceno_inputs/five_blend_float.tif"
# far_smooth = "/g/data/jl14/new_ceno_inputs/six_blend_float.tif"
far_smooth = "/g/data/jl14/new_ceno_inputs/8_5_blend_float.tif"
scale = "/g/data/jl14/80m_covarites/proximity/P_Dist_Meso.tif"

src = rio.open(near_rough)
far = rio.open(far_smooth).read(masked=True)
near = rio.open(near_rough).read(masked=True)
Z = rio.open(scale).read(masked=True)
mask = far.mask

lower_end = 0.01
higher_end = 0.08

greater = Z > higher_end
less = Z < lower_end
W = (higher_end - Z)/(higher_end - lower_end)
W[greater] = 0
W[less] = 1
W = np.ma.MaskedArray(data=W, mask=far.mask)

output = far * (1 - W ** 2) + near * W ** 2

profile = src.profile
profile.update({'driver': 'COG', 'BIGTIFF': 'YES', 'compress': 'lzw'})

with rio.open(f'MRVBF_S_8_5_blend_dist_meso_weighted_average_quadratic_bigtiff.tif', 'w', ** profile) as dst:
dst.write(output)


# [Yesterday 17:31] John Wilford
#
# Im preparing another blend like the example below near_rough = "DEM_fill_smooth_mag_2_cog_nan.tif"
#
# far_smooth = "/g/data/jl14/new_ceno_inputs/zero_blend.tif"
#
# scale = "/g/data/jl14/80m_covarites/proximity/P_Dist_Meso.tif"
#
# [Yesterday 17:32] John Wilford
#
# near ==/g/data/jl14/80m_covarites/terrain/T_MRVBF_S.tif
#
# [Yesterday 17:52] John Wilford
#
# far_smooth == /g/data/jl14/new_ceno_inputs/five_blend_float.tif
#
# [Yesterday 17:52] John Wilford
#
# scale = "/g/data/jl14/80m_covarites/proximity/P_Dist_Meso.tif"
#
# [Yesterday 17:53] John Wilford
#
# also please try it again with far_smooth == /g/data/jl14/new_ceno_inputs/six_blend_float.tif
#
# like 1
101 changes: 101 additions & 0 deletions scripts/clahe_rasters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""
Currently only supports single band raster.
"""
from optparse import OptionParser
import numpy as np
from osgeo import gdal, gdalconst
from skimage.exposure import equalize_adapthist, rescale_intensity


def apply_clahe(input_raster, output_raster,
in_range="image", kernel_size=200, clip_limit=0.1):
# don't change the next two lines

if (in_range is not None) and (in_range != "image"):
in_range = tuple([float(s) for s in in_range.split()])
else:
in_range = 'image'

src = gdal.Open(input_raster, gdalconst.GA_ReadOnly)
data = src.GetRasterBand(1).ReadAsArray()
nodata = getattr(np, str(data.dtype))(
src.GetRasterBand(1).GetNoDataValue())
mask = data == nodata
output_nodata = src.GetRasterBand(1).GetNoDataValue()

datatype = src.GetRasterBand(1).DataType
# import IPython; IPython.embed()

# rescale the data as the adapthist expects data in (-1, 1)
# specify in_range=(min_value_to_limit, max_value_to_limit),
# in_range="image" to use min and max in the image values.
rescaled_data = rescale_intensity(data.astype(np.float32),
in_range=in_range,
out_range=(0, 1))

# kernel_size: integer or list-like, optional
# Defines the shape of contextual regions used in the algorithm.
# If iterable is passed, it must have the same number of elements
# as image.ndim (without color channel).
# If integer, it is broadcasted to each image dimension. By default,
# kernel_size is 1/8 of image height by 1/8 of its width.
#
# clip_limit : float, optional
# Clipping limit, normalized between 0 and 1 (higher values give more contrast).
#
# nbins : int, optional
# Number of gray bins for histogram (“data range”).

stretched_data = equalize_adapthist(rescaled_data,
kernel_size=kernel_size,
clip_limit=clip_limit,
nbins=256)
# don't change anything below this
driver = gdal.GetDriverByName('GTiff')
out_ds = driver.Create(output_raster,
xsize=src.RasterXSize,
ysize=src.RasterYSize,
bands=1,
eType=datatype
)
out_ds.SetGeoTransform(src.GetGeoTransform())
out_ds.SetProjection(src.GetProjection())
rescaled_data[mask] = output_nodata
out_ds.GetRasterBand(1).WriteArray(stretched_data)
# might have to change dtype manually
out_ds.GetRasterBand(1).SetNoDataValue(output_nodata)
out_ds.FlushCache()


if __name__ == '__main__':
parser = OptionParser(usage='%prog -i input_raster -o output_raster \n'
'-r input_range -k kernel_size \n'
'-c clip_limit')
parser.add_option('-i', '--input_raster', type=str, dest='input_raster',
help='name of input raster file')

parser.add_option('-o', '--output_raster', type=str, dest='output_raster',
help='name of output raster file')

parser.add_option('-r', '--input_range', type=str, dest='input_range',
help="input range of the input raster to use."
" Can use tuple like '125 3000'")

parser.add_option('-k', '--kernel_size', type=int, dest='kernel_size',
default=400,
help='kernel size to use')

parser.add_option('-c', '--clip_limit', type=float, dest='clip_limit',
default='0.01',
help='clip limit used in clahe')

options, args = parser.parse_args()

if not options.input_raster: # if filename is not given
parser.error('Input raster filename must be provided.')

if not options.output_raster: # if filename is not given
parser.error('Output raster filename must be provided.')

apply_clahe(options.input_raster, options.output_raster,
options.input_range, options.kernel_size, options.clip_limit)

0 comments on commit 5915dd5

Please sign in to comment.