Commit e7314e09 authored by R.K.Garcia's avatar R.K.Garcia
Browse files

Autoscale utility for CRISPCA files or other simple netcdf float arrays

parent 84442b88
Pipeline #36876 passed with stage
in 7 minutes and 43 seconds
......@@ -76,8 +76,8 @@ class Autoscale:
def attrs(self):
""" CF attributes and fill value
"""
return {'scale': np.array(self.scale, dtype=self.dtype),
'offset': np.array(self.offset, dtype=self.dtype),
return {'scale_factor': np.array(self.scale, dtype=self.dtype),
'add_offset': np.array(self.offset, dtype=self.dtype),
'_FillValue': self.vnan
}
......
#!/usr/bin/env python3
import netCDF4 as nc4
from pathlib import Path
from autoscale import Autoscale
def copy(input_fn: Path, output_fn: Path):
nci = nc4.Dataset(input_fn, 'r')
nco = nc4.Dataset(output_fn, 'w')
for dname, dextent in nci.dimensions.items():
nco.createDimension(dname, len(dextent))
for gname in nci.ncattrs():
nco.setncattr(gname, getattr(nci, gname))
for vname, vdata in nci.variables.items():
nd = vdata[:]
sc = Autoscale(nd)
sca = sc.attrs
scale = sca['scale_factor']
offset = sca['add_offset']
print(f"scaling {vname} as m={scale} b={offset}")
ovdata = nco.createVariable(vname, sc.vdtype, vdata.dimensions, fill_value=sc.vnan, zlib=True)
ovdata.set_auto_scale(False)
ovdata.setncattr('scale_factor', scale)
ovdata.setncattr('add_offset', offset)
for attr_name in vdata.ncattrs():
if attr_name != '_FillValue':
setattr(ovdata, attr_name, vdata[attr_name])
ovdata[:] = sc.convert(nd)
nco.close()
nci.close()
if __name__ == '__main__':
import sys
ip = Path(sys.argv[1])
op = Path(sys.argv[2])
assert not op.exists()
copy(ip, op)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment