Created
September 17, 2018 19:41
-
-
Save mappingvermont/e09c63fb99e94671d801cee1fe60eded to your computer and use it in GitHub Desktop.
This file contains 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
import subprocess | |
import xarray as xr | |
# convert input tifs to netcdf | |
subprocess.check_call(['gdal_translate', '-of', 'NetCDF', 'biomass.tif', 'biomass.nc']) | |
subprocess.check_call(['gdal_translate', '-of', 'NetCDF', 'extent.tif', 'extent.nc']) | |
# rename bands to match data | |
subprocess.check_call(['ncrename', '-v', 'Band1,biomass', 'biomass.nc']) | |
subprocess.check_call(['ncrename', '-v', 'Band1,extent', 'extent.nc']) | |
# run a netcdf append command to add biomass data to the extent netcdf | |
subprocess.check_call(['ncks', '-A', 'biomass.nc', 'extent.nc']) | |
# open extent and then write it directly to CSV | |
# unfortunately there doesn't seem to be a fast CLI tool for this | |
ds = xr.open_dataset('extent.nc') | |
df = ds.to_dataframe().reset_index() | |
print df.head() | |
df.to_csv('netcdf_output.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment