Copy netcdf attributes between different files
Hi everybody, I am using netCDF4 library to read and write from netcdf files. I would like to copy all the attributes of one file to another one, in a way like this: --- from netCDF4 import Dataset as ncdf file1 = ncdf('file1.nc', mode='r', format='NETCDF4_CLASSIC') ... file2 = ncdf('file1.nc', mode='w', format='NETCDF4_CLASSIC') for att in file1.ncattrs(): file2.att = file1.getncatt(att) ... file1.close() file2.close() --- But this will not work as only one attribute named "att" in file2 will be created. How should I do this? Thanks.
On 11/14/11 10:04 AM, Giovanni Plantageneto wrote:
Hi everybody, I am using netCDF4 library to read and write from netcdf files. I would like to copy all the attributes of one file to another one, in a way like this:
---
from netCDF4 import Dataset as ncdf
file1 = ncdf('file1.nc', mode='r', format='NETCDF4_CLASSIC') ... file2 = ncdf('file1.nc', mode='w', format='NETCDF4_CLASSIC') for att in file1.ncattrs(): file2.att = file1.getncatt(att) ... file1.close() file2.close()
---
But this will not work as only one attribute named "att" in file2 will be created. How should I do this? Thanks.
Try this: for att in file1.ncattrs(): setattr(file2,att) = getattr(file1,att) -Jeff -- Jeffrey S. Whitaker Phone : (303)497-6313 Meteorologist FAX : (303)497-6449 NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@noaa.gov 325 Broadway Office : Skaggs Research Cntr 1D-113 Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg
Yes, thanks!
It works, but the syntax is setattr(flle2,att,getattr(file1,att))
2011/11/14 Jeff Whitaker
On 11/14/11 10:04 AM, Giovanni Plantageneto wrote:
Hi everybody, I am using netCDF4 library to read and write from netcdf files. I would like to copy all the attributes of one file to another one, in a way like this:
---
from netCDF4 import Dataset as ncdf
file1 = ncdf('file1.nc', mode='r', format='NETCDF4_CLASSIC') ... file2 = ncdf('file1.nc', mode='w', format='NETCDF4_CLASSIC') for att in file1.ncattrs(): file2.att = file1.getncatt(att) ... file1.close() file2.close()
---
But this will not work as only one attribute named "att" in file2 will be created. How should I do this? Thanks.
Try this:
for att in file1.ncattrs(): setattr(file2,att) = getattr(file1,att)
-Jeff
-- Jeffrey S. Whitaker Phone : (303)497-6313 Meteorologist FAX : (303)497-6449 NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@noaa.gov 325 Broadway Office : Skaggs Research Cntr 1D-113 Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg
In Python you use setattr to set an object's attribute whose name is stored
into a variable:
setattr(file2, att, file1.getncatt(att))
-=- Olivier
2011/11/14 Giovanni Plantageneto
Hi everybody, I am using netCDF4 library to read and write from netcdf files. I would like to copy all the attributes of one file to another one, in a way like this:
---
from netCDF4 import Dataset as ncdf
file1 = ncdf('file1.nc', mode='r', format='NETCDF4_CLASSIC') ... file2 = ncdf('file1.nc', mode='w', format='NETCDF4_CLASSIC') for att in file1.ncattrs(): file2.att = file1.getncatt(att) ... file1.close() file2.close()
---
But this will not work as only one attribute named "att" in file2 will be created. How should I do this? Thanks. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (3)
-
Giovanni Plantageneto
-
Jeff Whitaker
-
Olivier Delalleau