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

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

Replace print with logging

Line 
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
14import logging
15
16# Imports from external packages
17try:
18    import cdms2 as cdms
19except:
20    try:
21        import cdms
22    except:
23        raise Exception("Could not import third-party software. Nappy requires the CDMS and Numeric packages to be installed to convert to CDMS and NetCDF.")
24
25
26# Import from nappy package
27import nappy.nc_interface.na_to_cdms
28from nappy.na_error import na_error
29
30logging.basicConfig()
31log = logging.getLogger(__name__)
32
33class NAToNC(nappy.nc_interface.na_to_cdms.NADictToCdmsObjects):
34    """
35    Converts a NASA Ames file to a NetCDF file.
36    """
37   
38    def __init__(self, na_file, variables=None, aux_variables=None,
39                 global_attributes=[("Conventions","CF-1.0")],
40                 time_units=None, time_warning=True, 
41                 rename_variables={}):
42        """
43        Sets up instance variables. Note that the argument 'na_file' has a relaxes definition
44        and can be either a NASA Ames file object or the name of a NASA AMES file.
45        Typical usage is:
46        >>>    import nappy.nc_interface.na_to_nc as na_to_nc
47        >>>    c = na_to_nc.NAToNC("old_file.na")
48        >>>    c.convert()
49        >>>    c.writeNCFile("new_file.nc")         
50        """
51        # First open na_file if it is a file rather than an na_file object
52        na_file_obj = na_file
53        if type(na_file_obj) == type("string"):
54            na_file_obj = nappy.openNAFile(na_file_obj)
55
56        nappy.nc_interface.na_to_cdms.NADictToCdmsObjects.__init__(self, na_file_obj, variables=variables, 
57                 aux_variables=aux_variables,
58                 global_attributes=global_attributes,
59                 time_units=time_units, time_warning=time_warning, 
60                 rename_variables=rename_variables)
61
62
63    def writeNCFile(self, file_name, mode="w"):
64        """
65        Writes the NASA Ames content that has been converted into CDMS objects to a
66        NetCDF file of name 'file_name'. Note that mode can be set to append so you
67        can add the data to an existing file.
68        """
69        if not self.converted:
70            self.convert()
71
72        # Create CDMS output file object
73        fout = cdms.open(file_name, mode=mode)
74
75        # Write main variables
76        for var in self.cdms_variables:
77            fout.write(var)
78
79        # Write aux variables
80        for avar in self.cdms_aux_variables:
81            fout.write(avar)
82
83        # Write global attributes
84        for (att, value) in self.global_attributes:
85            setattr(fout, att, value)
86       
87        fout.close()
88        log.info("NetCDF file '%s' written successfully." % file_name)
89        return True
Note: See TracBrowser for help on using the repository browser.