NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.
To learn about what's going on, see About the Archive Site.
Howdy,Would some kind python user hit me with the clue-by-four here as to how to make scale_factor work with python-netcdf4 and the ushort type? I am using current conda-forge on python2.7. Here's my minimal example:
"""scale_factor.py""" from __future__ import print_function import netCDF4 import numpy as np def create_file(): """Create""" nc = netCDF4.Dataset('test.nc', 'w') nc.createDimension('x', 10) ncvar = nc.createVariable('ncvar', np.ushort, ('x', )) ncvar.scale_factor = 100. ncvar.add_offset = 0. nc.close() def write_data(): """Write""" nc = netCDF4.Dataset('test.nc', 'a') nc.set_auto_maskandscale(True) nc.variables['ncvar'][5] = 1.5 nc.close() nc = netCDF4.Dataset('test.nc', 'a') nc.set_auto_maskandscale(False) nc.variables['ncvar'][6] = 150.5 nc.close() def read_file(): """Read""" nc = netCDF4.Dataset('test.nc', 'r') nc.set_auto_maskandscale(True) print("read[5] resulted in %s" % (nc.variables['ncvar'][5],)) print("read[6] resulted in %s" % (nc.variables['ncvar'][6],)) nc.close() nc = netCDF4.Dataset('test.nc', 'r') nc.set_auto_maskandscale(False) print("readv2[5] resulted in %s" % (nc.variables['ncvar'][5],)) print("readv2[6] resulted in %s" % (nc.variables['ncvar'][6],)) nc.close() def main(): """Go Main""" create_file() write_data() read_file() if __name__ == '__main__': main() This results in $ python scale_factor.py read[5] resulted in 0.0 read[6] resulted in 15000.0 readv2[5] resulted in 0 readv2[6] resulted in 150if I switch to np.float32 as the netcdf variable type, I get proper results
$ python scale_factor.py read[5] resulted in 1.49999996647 read[6] resulted in 15050.0 readv2[5] resulted in 0.015 readv2[6] resulted in 150.5 thanks! daryl
python-users
archives: