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

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