[MMTK] How to create new NetCDF file from template?

Jose Borreguero borreguero at gmail.com
Thu Nov 6 16:01:40 UTC 2008


I finally got the function to clone a NetCDF file:

from copy import deepcopy
from Scientific.IO.NetCDF import NetCDFFile

def cloneNetCDF(outfile,templatefile):
    "return NetCDF object based on a templatefile"""
    pin=NetCDFFile(templatefile, 'r')
    pout=NetCDFFile(outfile, 'w')
    #clone dimensions
    for name,size in pin.dimensions.items(): pout.createDimension(name,size)
    #clone variables
    for name,var in pin.variables.items():
        datatype=var.typecode()
        dimensions=var.dimensions
        pout.createVariable(name, datatype, dimensions)
        pout.variables[name][:]=deepcopy(pin.variables[name][:])
    #clone global attributes
    for attributeName in dir(pin):
        if attributeName not in dir(pout):
            setattr(pout,attributeName,getattr(pin,attributeName) )
    return pout




On Thu, Nov 6, 2008 at 3:23 AM, Konrad Hinsen <hinsen at cnrs-orleans.fr>wrote:

> On 06.11.2008, at 00:44, Jose Borreguero wrote:
>
>  Dear MMTK users,
>>
>> I want to create a fresh new NetCDF file with the same structure as some
>> existing NetCDF file. I have trouble cloning the variables:
>>
>> def cloneNetCDF(outfile,template):
>>    pin=NetCDFFile(template, 'r')   #this is the existing file
>>    pout=NetCDFFile(outfile, 'w')    #this is the new file
>>    for name,size in pin.dimensions: pout.createDimension(name,size)
>>    for name,var in template.variables: I-have-no-clue()
>>   return pout
>>
>
> for name, var in template.variables.items():
>        ...
>
> template.variables is an ordinary Python dictionary.
>
> Konrad.
> --
> ---------------------------------------------------------------------
> Konrad Hinsen
> Centre de Biophysique Moléculaire, CNRS Orléans
> Synchrotron Soleil - Division Expériences
> Saint Aubin - BP 48
> 91192 Gif sur Yvette Cedex, France
> Tel. +33-1 69 35 97 15
> E-Mail: hinsen at cnrs-orleans.fr
> ---------------------------------------------------------------------
>
>
>
>


-- 
Jose M. Borreguero
Postdoctoral Associate
Oak Ridge National Laboratory
P.O. Box 2008, M.S. 6164
Oak Ridge, TN 37831
phone: 865-241-3071 fax: 865-576-5491
Email: borreguerojm at ornl.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://starship.python.net/pipermail/mmtk/attachments/20081106/839cd1e5/attachment.htm>


More information about the mmtk mailing list