source: nappy/trunk/nappy/nc_interface/na_to_nc.py @ 3991

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/nc_interface/na_to_nc.py@3991
Revision 3991, 3.0 KB checked in by astephen, 12 years ago (diff)

Converted all global_atts to lists of key,value pairs so order can be maintained.

RevLine 
[3329]1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6na_to_nc.py
7===========
8
9Contains the NAToNC class for converting a NASA Ames file to a NetCDF file.
10
11"""
12
13# Imports from python standard library
14
[3405]15# Imports from external packages
[3966]16try:
17    import cdms2 as cdms
18except:
19    try:
20        import cdms
21    except:
22        raise Exception("Could not import third-party software. Nappy requires the CDMS and Numeric packages to be installed to convert to CDMS and NetCDF.")
[3405]23
[3966]24
[3329]25# Import from nappy package
26import nappy.nc_interface.na_to_cdms
27from nappy.na_error import na_error
28
[3405]29
30class NAToNC(nappy.nc_interface.na_to_cdms.NADictToCdmsObjects):
[3329]31    """
32    Converts a NASA Ames file to a NetCDF file.
33    """
34   
[3405]35    def __init__(self, na_file, variables=None, aux_variables=None,
[3991]36                 global_attributes=[("Conventions","CF-1.0")],
[3329]37                 time_units=None, time_warning=True, 
38                 rename_variables={}):
39        """
40        Sets up instance variables. Note that the argument 'na_file' has a relaxes definition
41        and can be either a NASA Ames file object or the name of a NASA AMES file.
42        Typical usage is:
43        >>>    import nappy.nc_interface.na_to_nc as na_to_nc
[3391]44        >>>    c = na_to_nc.NAToNC("old_file.na")
[3329]45        >>>    c.convert()
[3391]46        >>>    c.writeNCFile("new_file.nc")         
[3329]47        """
48        # First open na_file if it is a file rather than an na_file object
[3382]49        na_file_obj = na_file
50        if type(na_file_obj) == type("string"):
51            na_file_obj = nappy.openNAFile(na_file_obj)
[3329]52
[3405]53        nappy.nc_interface.na_to_cdms.NADictToCdmsObjects.__init__(self, na_file_obj, variables=variables, 
[3329]54                 aux_variables=aux_variables,
55                 global_attributes=global_attributes,
56                 time_units=time_units, time_warning=time_warning, 
57                 rename_variables=rename_variables)
58
59
[3391]60    def writeNCFile(self, file_name, mode="w"):
[3329]61        """
62        Writes the NASA Ames content that has been converted into CDMS objects to a
63        NetCDF file of name 'file_name'. Note that mode can be set to append so you
64        can add the data to an existing file.
65        """
66        if not self.converted:
67            self.convert()
68
69        # Create CDMS output file object
70        fout = cdms.open(file_name, mode=mode)
71
72        # Write main variables
73        for var in self.cdms_variables:
74            fout.write(var)
75
76        # Write aux variables
77        for avar in self.cdms_aux_variables:
78            fout.write(avar)
79
80        # Write global attributes
[3991]81        for (att, value) in self.global_attributes:
[3329]82            setattr(fout, att, value)
83       
84        fout.close()
85        print "NetCDF file '%s' written successfully." % file_name
[3405]86        return True
Note: See TracBrowser for help on using the repository browser.