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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/nc_interface/na_to_nc.py@3351
Revision 3351, 2.6 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        if type(na_file) == type("string"):
39            na_file_obj = nappy.openNAFile(na_file)
40
41        nappy.nc_interface.na_to_cdms.NAToCdms(self, na_file_obj, variables=variables, 
42                 aux_variables=aux_variables,
43                 global_attributes=global_attributes,
44                 time_units=time_units, time_warning=time_warning, 
45                 rename_variables=rename_variables)
46
47
48    def write(self, file_name, mode="w"):
49        """
50        Writes the NASA Ames content that has been converted into CDMS objects to a
51        NetCDF file of name 'file_name'. Note that mode can be set to append so you
52        can add the data to an existing file.
53        """
54        if not self.converted:
55            self.convert()
56
57        # Create CDMS output file object
58        fout = cdms.open(file_name, mode=mode)
59
60        # Write main variables
61        for var in self.cdms_variables:
62            fout.write(var)
63
64        # Write aux variables
65        for avar in self.cdms_aux_variables:
66            fout.write(avar)
67
68        # Write global attributes
69        for (att, value) in self.global_attributes.items():
70            setattr(fout, att, value)
71       
72        fout.close()
73        print "NetCDF file '%s' written successfully." % file_name
74        return True
Note: See TracBrowser for help on using the repository browser.