Created
June 11, 2021 01:07
-
-
Save richpsharp/74b02aaee9f5e6a34a9a4520365b056a to your computer and use it in GitHub Desktop.
Convert ISIMPI2B netcat files to geotiffs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Convert isimib2 to geotiff.""" | |
import os | |
import glob | |
import xarray as xr | |
from osgeo import gdal | |
from osgeo import osr | |
LOCAL_RASTER_CREATION_OPTIONS = ( | |
'TILED=YES', 'BIGTIFF=YES', 'COMPRESS=LZW', | |
'BLOCKXSIZE=256', 'BLOCKYSIZE=256') | |
CLIMATE_MODEL = 'multi-model-median' | |
TEMPERATURE_CHANGE = 3.5 | |
GEOTRANSFORM = [-179.75, .5, 0, 89.75, 0, -.5] | |
if __name__ == '__main__': | |
driver = gdal.GetDriverByName('GTiff') | |
for netcdf_path in glob.glob('*/*/mean_vals/*.nc'): | |
print(netcdf_path) | |
target_raster_path = f'{os.path.basename(os.path.splitext(netcdf_path)[0])}_{CLIMATE_MODEL}_del{TEMPERATURE_CHANGE}C.tif' | |
ds = xr.open_dataset(netcdf_path) | |
array = (ds.sel(impact_model=CLIMATE_MODEL, climate_model=CLIMATE_MODEL, temperature_change=TEMPERATURE_CHANGE)).to_array().data[0] | |
n_rows, n_cols = array.shape | |
print(array.dtype) | |
target_raster = driver.Create( | |
target_raster_path, n_cols, n_rows, 1, gdal.GDT_Float32, | |
options=LOCAL_RASTER_CREATION_OPTIONS) | |
target_raster.SetProjection(osr.SRS_WKT_WGS84_LAT_LONG) | |
target_raster.SetGeoTransform(GEOTRANSFORM) | |
target_band = target_raster.GetRasterBand(1) | |
target_band.WriteArray(array) | |
target_band = None | |
target_raster = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment