Skip to content

Instantly share code, notes, and snippets.

@mappingvermont
Created September 17, 2018 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mappingvermont/e09c63fb99e94671d801cee1fe60eded to your computer and use it in GitHub Desktop.
Save mappingvermont/e09c63fb99e94671d801cee1fe60eded to your computer and use it in GitHub Desktop.
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