def __init__(self,
                 ctl_name,
                 binary_name,
                 super_ctl_name=None,
                 undef=-1e15):
        self.super_ctl_name = super_ctl_name
        self.ctl_name = ctl_name
        self.binary_name = binary_name

        self._x = self._y = self._z = self._times = None
        self.var_nls = {}

        if super_ctl_name and self.ctl_name.startswith('^'):
            self._super_ctl_dir = path.dirname(super_ctl_name)
            self._ctl_expanded = path.join(self._super_ctl_dir,
                                           self.ctl_name.replace('^', ''))
        else:
            self._ctl_expanded = self.ctl_name
        if self.binary_name.startswith('^'):
            ctl_dir = path.dirname(self._ctl_expanded)
            self._bin_expanded = path.join(ctl_dir,
                                           self.binary_name.replace('^', ''))
        else:
            self._bin_expanded = self.binary_name
        self.undef = undef
        self._vars_3d = set()
        self._def_done = False
        self.var_list = []
        self._closed = False
        self._general_attrs = namelist.Namelist('gattr')
 def as_namelist(self):
     nl = namelist.Namelist('vertical')
     if self.number_of_levels() > 0:
         nl.put('number_of_levels', self.number_of_levels())
         nl.put(self.__class__.values_label, ' '.join('%g' % x for x in self.values))
     nl.put('vertical_method', self.__class__.vertical_method.upper())
     if self.__class__.level_type:
         nl.put('level_type', self.__class__.level_type.upper())
     return nl
    def as_namelist(self):
        nlevs = self.number_of_levels()
        nl = namelist.Namelist('vertical')

        if nlevs > 0:
            nl.put('number_of_levels', nlevs)
            for i in range(nlevs):
                  nl.put(self.__class__.values_label, '%d %g %g' %(i,a_half[i], b_half[i]))
            nl.put(self.__class__.values_top_label, '%d %g %g' %(i,a_half[nlevs+1], b_half[nlevs+1]))

        nl.put('vertical_method', self.__class__.vertical_method.upper())
        if self.__class__.level_type:
            nl.put('level_type', self.__class__.level_type.upper())
        return nl
 def add(self, varname, reader, attrs={}):
     z = None if reader.ndims() == 2 else reader.z()
     times = self.times or reader.t()
     var_nl = namelist.Namelist(varname)
     var_nl.set('describe', reader.describe())
     for key, val in attrs.items():
         var_nl.set(key, val)
     new_var = self.writer.add_var(varname, times, reader.x(), reader.y(),
                                   z, var_nl)
     self._readers[varname] = reader
     if self.writer.undef is None:
         self.writer.undef = reader.undef()
     elif not util.almost_eq(self.writer.undef, reader.undef()):
         raise ValueError(
             'Cannot have different undef for several varibles')
 def add_var(self, varname, times, x, y, z=None, var_nl=None):
     """
     Add a variable. For 2D variables, set z = None. A namelist is given to be included
     in the super ctl.
     """
     if self._def_done:
         raise ValueError('Not in definition mode')
     if varname in self.var_nls:
         raise ValueError('Variable already defined: %s' % varname)
     self._set_xy('x', x)
     self._set_xy('y', y)
     self._set_time(times)
     if z is not None:
         self._set_z(z)
         self._vars_3d.add(varname)
     if var_nl is not None:
         self.var_nls[varname] = var_nl
     else:
         self.var_nls[varname] = namelist.Namelist(varname)
     self.var_list.append(varname)
 def as_namelist(self):
     hash = {'grid_type' : self.grid_type,
             'grid_title' : '',
             'lon_s_pole' : self.proj.south_pole[0],
             'lat_s_pole' : self.proj.south_pole[1],
             'nx' : self.nx,
             'ny' : self.ny,
             'dx' : self.dx,
             'dy' : self.dy,
             'resol_flag' : 128,
             'ifReduced' : 0,
             'wind_component' : 0,
             'reduced_nbr_str' : 0,
             'lat_pole_stretch' : 0.0,
             'lon_pole_stretch' : 0.0,
             'lon_start' : self.x0,
             'lat_start' : self.y0
             }
     nl = namelist.Namelist('grid')
     for key, val in hash.iteritems():
         nl.put(key, val)
     return nl