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

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

Got some of the code actually working!

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