Created
November 2, 2019 23:00
-
-
Save lkiesow/17c3c4984408e580fb0c4345ff6ba002 to your computer and use it in GitHub Desktop.
Filter sentinel-5p netCDF files for testing
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 netCDF4 | |
import sys | |
USED_VARS = set([ | |
'carbonmonoxide_total_column', | |
'longitude', | |
'latitude', | |
'qa_value', | |
'delta_time']) | |
def main(): | |
if len(sys.argv) != 3: | |
print(f'{sys.argv[0]} infile outfile') | |
sys.exit() | |
infile = sys.argv[1] | |
outfile = sys.argv[2] | |
with netCDF4.Dataset(infile, 'r') as read_f: | |
p_grp = read_f.groups['PRODUCT'] | |
with netCDF4.Dataset(outfile, 'w') as f: | |
f.time_reference = read_f.time_reference | |
products = f.createGroup('/PRODUCT') | |
for d in p_grp.dimensions.values(): | |
products.createDimension(d.name, d.size) | |
for v in p_grp.variables.values(): | |
if v.name in USED_VARS: | |
print(f'importing {v.name}') | |
else: | |
print(f'skipping {v.name}') | |
var = products.createVariable(v.name, v.dtype, v.dimensions) | |
for a in v.ncattrs(): | |
if not a.startswith('_'): | |
var.setncattr(a, v.getncattr(a)) | |
var[:] = v[:] | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment