示例#1
0
    def open_new_file(self,
                      file_name,
                      info=None,
                      var_name='UNKNOWN',
                      dtype='float32',
                      VERBOSE=False,
                      MAKE_RTI=True,
                      MAKE_BOV=False):

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite(file_name)
        self.file_name = file_name

        #---------------------------------------
        # Check and store the grid information
        #---------------------------------------
        self.check_and_store_info(file_name, info, var_name, dtype, MAKE_RTI,
                                  MAKE_BOV)

        #------------------------------------
        # Try to open new RTS file to write
        #------------------------------------
        try:
            if (VERBOSE):
                print 'Preparing to write new RTS file:'
                print '   ' + file_name
            self.rts_unit = open(file_name, 'wb')
            return True
        except:
            return False
示例#2
0
    def open_new_file(self, file_name, info=None,
                      var_name='UNKNOWN',
                      dtype='float32',
                      VERBOSE=False,
                      MAKE_RTI=True, MAKE_BOV=False):

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite( file_name )
        self.file_name = file_name
        
        #---------------------------------------
        # Check and store the grid information
        #---------------------------------------
        self.check_and_store_info( file_name, info, var_name,
                                   dtype, MAKE_RTI, MAKE_BOV )
        
        #------------------------------------
        # Try to open new RTS file to write
        #------------------------------------
        try:            
            if (VERBOSE):
                print 'Preparing to write new RTS file:'
                print '   ' + file_name  
            self.rts_unit = open(file_name, 'wb')
            return True
        except:
            return False
示例#3
0
    def open_new_file(self,
                      file_name,
                      var_names=['X'],
                      dtype='float64',
                      time_units='minutes'):

        #-----------------------------------------------------------
        # Note:  The "dtype" argument is included to match similar
        #        routines, but is not currently used. (9/18/14)
        #-----------------------------------------------------------

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite(file_name)
        self.file_name = file_name

        self.var_names = var_names
        self.time_units = time_units

        try:
            self.ts_unit = open(file_name, 'w')
            self.write_header(var_names)
            return True
        except:
            print 'SORRY, Could not open new multi-column text file:'
            print '   ' + file_name
            self.ts_unit = False
            return False
示例#4
0
    def open_new_file( self, file_name,
                       var_names=['X'],
                       dtype='float64',
                       time_units='minutes' ):

        #-----------------------------------------------------------
        # Note:  The "dtype" argument is included to match similar
        #        routines, but is not currently used. (9/18/14)
        #-----------------------------------------------------------
        
        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite( file_name )
        self.file_name = file_name
        
        self.var_names  = var_names
        self.time_units = time_units
        
        try:
            self.ts_unit = open( file_name, 'w' )
            self.write_header( var_names )
            return True
        except:
            print 'SORRY, Could not open new multi-column text file:'
            print '   ' + file_name
            self.ts_unit = False
            return False
    def open_new_file(
            self,
            file_name,
            info=None,
            var_name='X',
            long_name=None,
            units_name='None',
            dtype='float32',
            ### dtype='float64'
            time_units='minutes',
            comment='',
            MAKE_RTI=True,
            MAKE_BOV=False):

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite(file_name)
        self.file_name = file_name

        #---------------------------------------
        # Check and store the grid information
        #---------------------------------------
        self.check_and_store_info(file_name, info, var_name, dtype, MAKE_RTI,
                                  MAKE_BOV)
        if (long_name is None): long_name = var_name
        self.long_name = long_name
        self.units_name = units_name
        self.dtype = dtype

        #-----------------------------------
        # Save the two-char data type code
        #-----------------------------------
        dtype_map = self.get_dtype_map()
        dtype_code = dtype_map[dtype.lower()]
        self.dtype_code = dtype_code

        #-------------------------------------
        # Open a new netCDF file for writing
        #-----------------------------------------------------------------
        # Format options are:  NETCDF3_CLASSIC, NETCDF3_64BIT_OFFSET,
        # NETCDF3_64BIT_DATA, NETCDF4_CLASSIC, and NETCDF4
        #-----------------------------------------------------------------
        # NETCDF3_CLASSIC results in MUCH SMALLER filesizes than using
        # NETCDF4_CLASSIC or NETCDF4.
        #   NETCDF3_CLASSIC, June_20_67_2D-Q.nc, 813,312 bytes
        #   NETCDF4_CLASSIC, June_20_67_2D-Q.nc, 5,030,256 bytes
        # The 2nd one is 6.2 TIMES BIGGER.
        #-----------------------------------------------------------------
        # For more info see:  http://unidata.github.io/netcdf4-python/
        #-----------------------------------------------------------------
        # The "nccopy" utility can convert between these formats.
        #-----------------------------------------------------------------
        try:
            format = 'NETCDF3_CLASSIC'
            ## format = 'NETCDF4'
            ## format = 'NETCDF4_CLASSIC'
            ncgs_unit = nc.Dataset(file_name, mode='w', format=format)
            OK = True
        except:
            OK = False
            return OK

        #------------------------------------------------------------
        # Option to pre-fill with fill values
        # Set fill_value for a var with "var._Fill_Value = number"
        # For Nio was:  opt.PreFill = False # (for efficiency)
        #------------------------------------------------------------
        ncgs_unit.set_fill_off()
        # ncgs_unit.set_fill_on()

        #-------------------------------------
        # Prepare and save a history string
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        history = "Created using netCDF4 " + nc.__version__ + " on "
        history = history + time.asctime() + ". "
        history = history + comment
        ncgs_unit.history = history
        # print 'MADE IT PAST history BLOCK'

        ##        print 'nx =', self.info.ncols
        ##        print 'ny =', self.info.nrows
        ##        print 'dx =', self.info.xres
        ##        print 'dy =', self.info.yres
        ##        print ' '

        #----------------------------------------------
        # Create grid dimensions nx and ny, plus time
        #----------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        #----------------------------------------------
        ncgs_unit.createDimension('nx', int(self.info.ncols))
        ncgs_unit.createDimension('ny', int(self.info.nrows))
        ncgs_unit.createDimension('time', None)  # (unlimited dimension)
        # print 'MADE IT PAST create_dimension CALLS.'

        #-------------------------
        # Create a time variable
        #------------------------------------------
        #('d' = float64; must match in add_grid()
        # In netCDF4 package, use 'f8' vs. 'd'.
        #------------------------------------------
        tvar = ncgs_unit.createVariable('time', 'f8', ('time', ))
        tvar.units = time_units
        ### ncgs_unit.variables['time'].units = time_units

        #--------------------------------
        # Create a variable in the file
        #--------------------------------
        var = ncgs_unit.createVariable(var_name, dtype_code,
                                       ('time', 'ny', 'nx'))

        #----------------------------------
        # Specify a "nodata" fill value ?
        #----------------------------------
        # var._Fill_Value = -9999.0    ## Used for pre-fill above ?

        #-------------------------------------------
        # Create a separate, scalar "time stamp" ?
        #-------------------------------------------
        # t = nc_unit.createVariable('time', dtype_code, ('time'))

        #----------------------------------
        # Specify a "nodata" fill value ?
        #----------------------------------
        #         var._FillValue = -9999.0    ## Was used for Nio.

        #------------------------------------
        # Create attributes of the variable
        #------------------------------------
        ncgs_unit.variables[var_name].long_name = long_name
        ncgs_unit.variables[var_name].units = units_name
        ncgs_unit.variables[var_name].dx = self.info.xres
        ncgs_unit.variables[var_name].dy = self.info.yres  ### (12/2/09)
        ncgs_unit.variables[var_name].y_south_edge = self.info.y_south_edge
        ncgs_unit.variables[var_name].y_north_edge = self.info.y_north_edge
        ncgs_unit.variables[var_name].x_west_edge = self.info.x_west_edge
        ncgs_unit.variables[var_name].x_east_edge = self.info.x_east_edge

        self.ncgs_unit = ncgs_unit
        return OK
示例#6
0
    def open_new_file(
            self,
            file_name,
            info=None,
            var_name='X',
            long_name=None,
            units_name='None',
            dtype='float64',
            ### dtype='float64'
            time_units='minutes',
            comment='',
            shape=(1, 1, 1),
            res=(1., 1., 1.),
            MAKE_RTI=True,
            MAKE_BOV=False):

        #--------------------------------------------------
        # Try to import the Nio module from PyNIO package
        #--------------------------------------------------
        Nio = self.import_nio()
        if not Nio:
            return False

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite(file_name)
        self.file_name = file_name

        #---------------------------------------
        # Check and store the grid information
        #---------------------------------------
        self.format = 'nccs'
        self.file_name = file_name
        self.time_index = 0
        self.var_name = var_name
        self.shape = shape
        self.res = res

        if (long_name is None):
            long_name = var_name
        self.long_name = long_name
        self.units_name = units_name
        self.dtype = dtype

        #-----------------------------------
        # Get Nio type code for this dtype
        #------------------------------------
        nio_type_map = self.get_nio_type_map()
        nio_type_code = nio_type_map[dtype.lower()]
        self.nio_type_code = nio_type_code

        #-------------------------------------
        # Open a new netCDF file for writing
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        opt = Nio.options()
        opt.PreFill = False  # (for efficiency)
        opt.HeaderReserveSpace = 4000  # (4000 bytes, for efficiency)
        history = "Created using PyNIO " + Nio.__version__ + " on "
        history = history + time.asctime() + ". "
        history = history + comment
        # print 'MADE IT PAST history BLOCK'

        try:
            nccs_unit = Nio.open_file(file_name,
                                      mode="w",
                                      options=opt,
                                      history=history)
            OK = True
        except:
            OK = False
            return OK

        #----------------------------------------------
        # Create grid dimensions nx and ny, plus time
        #----------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        #----------------------------------------------
        nccs_unit.create_dimension("nz", self.shape[0])
        nccs_unit.create_dimension("ny", self.shape[1])
        nccs_unit.create_dimension("nx", self.shape[2])
        nccs_unit.create_dimension("time", None)  # (unlimited dimension)
        # print 'MADE IT PAST create_dimension CALLS.'

        #-------------------------
        # Create a time variable
        #------------------------------------------
        #('d' = float64; must match in add_cube()
        #------------------------------------------
        tvar = nccs_unit.create_variable('time', 'd', ("time", ))
        nccs_unit.variables['time'].units = time_units

        #--------------------------------
        # Create a variable in the file
        #----------------------------------
        # Returns "var" as a PyNIO object
        #----------------------------------
        var = nccs_unit.create_variable(var_name, nio_type_code,
                                        ("time", "nz", "ny", "nx"))

        #----------------------------------
        # Specify a "nodata" fill value ?
        #----------------------------------
        var._FillValue = -9999.0  ## Does this jive with Prefill above ??

        #------------------------------------
        # Create attributes of the variable
        #------------------------------------
        nccs_unit.variables[var_name].long_name = long_name
        nccs_unit.variables[var_name].units = units_name
        nccs_unit.variables[var_name].dz = self.res[0]
        nccs_unit.variables[var_name].dy = self.res[1]
        nccs_unit.variables[var_name].dx = self.res[2]
        nccs_unit.variables[var_name].y_south_edge = 0.
        nccs_unit.variables[
            var_name].y_north_edge = self.res[1] * self.shape[1]
        nccs_unit.variables[var_name].x_west_edge = 0.
        nccs_unit.variables[var_name].x_east_edge = self.res[2] * self.shape[2]
        nccs_unit.variables[var_name].z_bottom_edge = 0.
        nccs_unit.variables[var_name].z_top_edge = self.res[0] * self.shape[0]

        self.nccs_unit = nccs_unit
        return OK
示例#7
0
    def open_new_file(self, file_name,
                      z_values=np.arange(10),
                      z_units='m',
                      var_names=['X'],
                      long_names=[None],
                      units_names=['None'],
                      dtypes=['float64'],
                      time_units='minutes',
                      comment=''):

        #----------------------------------------------------
        # Notes: It might be okay to have "nz" be an
        #        unlimited dimension, like "time".  This
        #        would mean replacing "int(profile_length)"
        #        with "None".
        #----------------------------------------------------

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite( file_name )
        self.file_name = file_name
        
        #---------------------------------------
        # Check and store the time series info
        #---------------------------------------
        self.format     = 'ncps'
        self.file_name  = file_name
        self.time_index = 0
        if (long_names[0] == None):
            long_names = var_names
        #-------------------------------------------            
        self.z_values  = z_values
        self.z_units   = z_units
        nz             = np.size(z_values)
        #-------------------------------------------
        # We may not need to save these in self.
        # I don't think they're used anywhere yet.
        #-------------------------------------------
        self.var_names   = var_names 
        self.long_names  = long_names
        self.units_names = units_names
        self.dtypes      = dtypes

        #---------------------------------------------
        # Create array of Nio type codes from dtypes
        #---------------------------------------------
        dtype_map   = self.get_dtype_map()
        dtype_codes = []
        if (len(dtypes) == len(var_names)):
            for dtype in dtypes:
               dtype_code = dtype_map[ dtype.lower() ]
               dtype_codes.append( dtype_code )
        else:
            dtype = dtypes[0]
            dtype_code = dtype_map[ dtype.lower() ]
            for k in xrange(len(var_names)):
                dtype_codes.append( dtype_code )                
        self.dtype_codes = dtype_codes        
            
        #-------------------------------------
        # Open a new netCDF file for writing
        #-------------------------------------
        try:
            ## format = 'NETCDF4'
            format = 'NETCDF4_CLASSIC'
            ncps_unit = nc.Dataset(file_name, mode='w', format=format)
            OK = True
        except:
            OK = False
            return OK

        #------------------------------------------------------------
        # Option to pre-fill with fill values
        # Set fill_value for a var with "var._Fill_Value = number"
        # For Nio was:  opt.PreFill = False # (for efficiency)
        #------------------------------------------------------------
        ncgs_unit.set_fill_off()
        # ncgs_unit.set_fill_on()
        
        #-------------------------------------
        # Prepare and save a history string
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        history = "Created using netCDF4 " + nc.__version__ + " on "
        history = history + time.asctime() + ". " 
        history = history + comment
        ncps_unit.history = history
        # print 'MADE IT PAST history BLOCK'
       
        #------------------------------------------------
        # Create an unlimited time dimension (via None)
        #------------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        #------------------------------------------------
        ncps_unit.createDimension('nz', int(nz))
        ncps_unit.createDimension('time', None)

        #-------------------------
        # Create a time variable
        #---------------------------------------------------
        #('f' = float32; must match in add_values_at_IDs()
        #---------------------------------------------------
        # NB! Can't use "time" vs. "tvar" here unless we
        #     add "import time" inside this function.
        #---------------------------------------------------
        tvar = ncps_unit.createVariable('time', 'f8', ('time',))
        ncps_unit.variables['time'].units = time_units

        #--------------------------------------
        # Create a distance/depth variable, z
        #--------------------------------------
        zvar = ncps_unit.createVariable('z', 'd', ('z',))
        zvar[ : ] = z_values  # (store the z-values)
        ncps_unit.variables['z'].units = z_units
        
        #-----------------------------------
        # Create variables using var_names
        #-----------------------------------
        # Returns "var" as a PyNIO object
        #---------------------------------------------------
        # NB! The 3rd argument here (dimension), must be a
        #     tuple.  If there is only one dimension, then
        #     we need to add a comma, as shown.
        #---------------------------------------------------
        for k in xrange(len(var_names)):
            var_name = var_names[k]
            var = ncps_unit.create_variable(var_name, dtype_codes[k],
                                            ("time", "nz"))
        
            #------------------------------------
            # Create attributes of the variable
            #------------------------------------
            ncps_unit.variables[var_name].long_name = long_names[k]
            ncps_unit.variables[var_name].units     = units_names[k]        

			#----------------------------------
			# Specify a "nodata" fill value ?
			#----------------------------------
			# var._Fill_Value = -9999.0    ## Used for pre-fill above ?
            
        self.ncps_unit = ncps_unit
        return OK
示例#8
0
    def open_new_file(self, file_name,
                      z_values=numpy.arange(10),
                      z_units='m',
                      var_names=['X'],
                      long_names=[None],
                      units_names=['None'],
                      dtypes=['float64'],
                      time_units='minutes',
                      comment=''):

        #----------------------------------------------------
        # Notes: It might be okay to have "nz" be an
        #        unlimited dimension, like "time".  This
        #        would mean replacing "int(profile_length)"
        #        with "None".
        #----------------------------------------------------
        
        #--------------------------------------------------
        # Try to import the Nio module from PyNIO package
        #--------------------------------------------------
        Nio = self.import_nio()
        if not(Nio): return False

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite( file_name )
        self.file_name = file_name
        
        #---------------------------------------
        # Check and store the time series info
        #---------------------------------------
        self.format     = 'ncps'
        self.file_name  = file_name
        self.time_index = 0
        if (long_names[0] == None):
            long_names = var_names
        #-------------------------------------------            
        self.z_values  = z_values
        self.z_units   = z_units
        nz             = numpy.size(z_values)
        #-------------------------------------------
        # We may not need to save these in self.
        # I don't think they're used anywhere yet.
        #-------------------------------------------
        self.var_names   = var_names 
        self.long_names  = long_names
        self.units_names = units_names
        self.dtypes      = dtypes

        #---------------------------------------------
        # Create array of Nio type codes from dtypes
        #---------------------------------------------
        nio_type_map   = self.get_nio_type_map()
        nio_type_codes = []
        if (len(dtypes) == len(var_names)):
            for dtype in dtypes:
               nio_type_code = nio_type_map[ dtype.lower() ]
               nio_type_codes.append( nio_type_code )
        else:
            dtype = dtypes[0]
            nio_type_code = nio_type_map[ dtype.lower() ]
            for k in xrange(len(var_names)):
                nio_type_codes.append( nio_type_code )                
        self.nio_type_codes = nio_type_codes        
            
        #-------------------------------------
        # Open a new netCDF file for writing
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        opt = Nio.options()
        opt.PreFill = False            # (for efficiency)
        opt.HeaderReserveSpace = 4000  # (4000 bytes, for efficiency)
        history = "Created using PyNIO " + Nio.__version__ + " on "
        history = history + time.asctime() + ". " 
        history = history + comment

        try:
            ncps_unit = Nio.open_file(file_name, mode="w",
                                      options=opt, history=history )
            OK = True
        except:
            OK = False
            return OK
        
        #------------------------------------------------
        # Create an unlimited time dimension (via None)
        #------------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        #------------------------------------------------
        ncps_unit.create_dimension("nz", int(nz))
        ncps_unit.create_dimension("time", None)

        #-------------------------
        # Create a time variable
        #---------------------------------------------------
        #('f' = float32; must match in add_values_at_IDs()
        #---------------------------------------------------
        # NB! Can't use "time" vs. "tvar" here unless we
        #     add "import time" inside this function.
        #---------------------------------------------------
        tvar = ncps_unit.create_variable('time', 'd', ("time",))
        ncps_unit.variables['time'].units = time_units

        #--------------------------------------
        # Create a distance/depth variable, z
        #--------------------------------------
        zvar = ncps_unit.create_variable('z', 'd', ("nz",))
        zvar[ : ] = z_values  # (store the z-values)
        ncps_unit.variables['z'].units = z_units
        
        #-----------------------------------
        # Create variables using var_names
        #-----------------------------------
        # Returns "var" as a PyNIO object
        #---------------------------------------------------
        # NB! The 3rd argument here (dimension), must be a
        #     tuple.  If there is only one dimension, then
        #     we need to add a comma, as shown.
        #---------------------------------------------------
        for k in xrange(len(var_names)):
            var_name = var_names[k]
            var = ncps_unit.create_variable(var_name, nio_type_codes[k],
                                            ("time", "nz"))
        
            #------------------------------------
            # Create attributes of the variable
            #------------------------------------
            ncps_unit.variables[var_name].long_name = long_names[k]
            ncps_unit.variables[var_name].units     = units_names[k]        

            #----------------------------------
            # Specify a "nodata" fill value ?
            #----------------------------------
            var._FillValue = -9999.0    ## Does this jive with Prefill above ??
            
        self.ncps_unit = ncps_unit
        return OK
示例#9
0
    def open_new_file(self, file_name, info=None,
                      var_name='X',
                      long_name=None,
                      units_name='None',
                      dtype='float64',
                      time_units='minutes',
                      comment='',
                      shape=(1,1,1),
                      res=(1.,1.,1.),
                      MAKE_RTI=True, MAKE_BOV=False):

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite( file_name )
        self.file_name = file_name
        
        #---------------------------------------
        # Check and store the grid information
        #---------------------------------------
        self.format     = 'nccs'
        self.file_name  = file_name
        self.time_index = 0
        self.var_name   = var_name
        self.shape      = shape
        self.res        = res
        
        if (long_name is None):
            long_name = var_name
        self.long_name  = long_name
        self.units_name = units_name
        self.dtype      = dtype

        #-----------------------------------
        # Save the two-char data type code
        #-----------------------------------
        dtype_map  = self.get_dtype_map()        
        dtype_code = dtype_map[ dtype.lower() ]        
        self.dtype_code = dtype_code
        
        #-------------------------------------
        # Open a new netCDF file for writing
        #-------------------------------------   
        try:
            ## format = 'NETCDF4'
            format = 'NETCDF4_CLASSIC'
            nccs_unit = nc.Dataset(file_name, mode='w', format=format)
            OK = True
        except:
            OK = False
            return OK

        #------------------------------------------------------------
        # Option to pre-fill with fill values
        # Set fill_value for a var with "var._Fill_Value = number"
        # For Nio was:  opt.PreFill = False # (for efficiency)
        #------------------------------------------------------------
        ncgs_unit.set_fill_off()
        # ncgs_unit.set_fill_on()

        #-------------------------------------
        # Prepare and save a history string
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        history = "Created using netCDF4 " + nc.__version__ + " on "
        history = history + time.asctime() + ". " 
        history = history + comment
        nccs_unit.history = history
        # print 'MADE IT PAST history BLOCK'
        
        #----------------------------------------------
        # Create grid dimensions nx and ny, plus time
        #----------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        #----------------------------------------------
        nccs_unit.createDimension('nz', self.shape[0])
        nccs_unit.createDimension('ny', self.shape[1])
        nccs_unit.createDimension('nx', self.shape[2])
        nccs_unit.createDimension('time', None)   # (unlimited dimension)
        # print 'MADE IT PAST create_dimension CALLS.'
        
        #-------------------------
        # Create a time variable
        #------------------------------------------
        #('d' = float64; must match in add_cube()
        #------------------------------------------
        tvar = nccs_unit.createVariable ('time', 'f8', ('time',))
        nccs_unit.variables['time'].units = time_units
        
        #--------------------------------
        # Create a variable in the file
        #----------------------------------
        # Returns "var" as a PyNIO object
        #----------------------------------
        var = nccs_unit.createVariable (var_name, dtype_code,
                                         ('time', 'nz', 'ny', 'nx'))

        #----------------------------------
        # Specify a "nodata" fill value ?
        #----------------------------------
        # var._Fill_Value = -9999.0    ## Used for pre-fill above ?
        
        #------------------------------------
        # Create attributes of the variable
        #------------------------------------
        nccs_unit.variables[var_name].long_name = long_name
        nccs_unit.variables[var_name].units = units_name
        #----------------------------------------------------
        nccs_unit.variables[var_name].dz = self.res[0]
        nccs_unit.variables[var_name].dy = self.res[1]
        nccs_unit.variables[var_name].dx = self.res[2]
        nccs_unit.variables[var_name].y_south_edge = 0.
        nccs_unit.variables[var_name].y_north_edge = self.res[1]*self.shape[1]
        nccs_unit.variables[var_name].x_west_edge = 0.
        nccs_unit.variables[var_name].x_east_edge = self.res[2]*self.shape[2]
        nccs_unit.variables[var_name].z_bottom_edge = 0.
        nccs_unit.variables[var_name].z_top_edge = self.res[0]*self.shape[0]
        #--------------------------------------------
        # This is how it is done in ncgs_files.py.
        #-------------------------------------------
#         ncgs_unit.variables[var_name].dx           = self.info.xres
#         ncgs_unit.variables[var_name].dy           = self.info.yres  ### (12/2/09)
#         ncgs_unit.variables[var_name].y_south_edge = self.info.y_south_edge
#         ncgs_unit.variables[var_name].y_north_edge = self.info.y_north_edge
#         ncgs_unit.variables[var_name].x_west_edge  = self.info.x_west_edge
#         ncgs_unit.variables[var_name].x_east_edge  = self.info.x_east_edge  
                
        self.nccs_unit = nccs_unit
        return OK
示例#10
0
    def open_new_file(self,
                      file_name,
                      z_values=numpy.arange(10),
                      z_units='m',
                      var_names=['X'],
                      long_names=[None],
                      units_names=['None'],
                      dtypes=['float64'],
                      time_units='minutes',
                      comment=''):

        #----------------------------------------------------
        # Notes: It might be okay to have "nz" be an
        #        unlimited dimension, like "time".  This
        #        would mean replacing "int(profile_length)"
        #        with "None".
        #----------------------------------------------------

        #--------------------------------------------------
        # Try to import the Nio module from PyNIO package
        #--------------------------------------------------
        Nio = self.import_nio()
        if not (Nio): return False

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite(file_name)
        self.file_name = file_name

        #---------------------------------------
        # Check and store the time series info
        #---------------------------------------
        self.format = 'ncps'
        self.file_name = file_name
        self.time_index = 0
        if (long_names[0] is None):
            long_names = var_names
        #-------------------------------------------
        self.z_values = z_values
        self.z_units = z_units
        nz = numpy.size(z_values)
        #-------------------------------------------
        # We may not need to save these in self.
        # I don't think they're used anywhere yet.
        #-------------------------------------------
        self.var_names = var_names
        self.long_names = long_names
        self.units_names = units_names
        self.dtypes = dtypes

        #---------------------------------------------
        # Create array of Nio type codes from dtypes
        #---------------------------------------------
        nio_type_map = self.get_nio_type_map()
        nio_type_codes = []
        if (len(dtypes) == len(var_names)):
            for dtype in dtypes:
                nio_type_code = nio_type_map[dtype.lower()]
                nio_type_codes.append(nio_type_code)
        else:
            dtype = dtypes[0]
            nio_type_code = nio_type_map[dtype.lower()]
            for k in xrange(len(var_names)):
                nio_type_codes.append(nio_type_code)
        self.nio_type_codes = nio_type_codes

        #-------------------------------------
        # Open a new netCDF file for writing
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        opt = Nio.options()
        opt.PreFill = False  # (for efficiency)
        opt.HeaderReserveSpace = 4000  # (4000 bytes, for efficiency)
        history = "Created using PyNIO " + Nio.__version__ + " on "
        history = history + time.asctime() + ". "
        history = history + comment

        try:
            ncps_unit = Nio.open_file(file_name,
                                      mode="w",
                                      options=opt,
                                      history=history)
            OK = True
        except:
            OK = False
            return OK

        #------------------------------------------------
        # Create an unlimited time dimension (via None)
        #------------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        #------------------------------------------------
        ncps_unit.create_dimension("nz", int(nz))
        ncps_unit.create_dimension("time", None)

        #-------------------------
        # Create a time variable
        #---------------------------------------------------
        #('f' = float32; must match in add_values_at_IDs()
        #---------------------------------------------------
        # NB! Can't use "time" vs. "tvar" here unless we
        #     add "import time" inside this function.
        #---------------------------------------------------
        tvar = ncps_unit.create_variable('time', 'd', ("time", ))
        ncps_unit.variables['time'].units = time_units

        #--------------------------------------
        # Create a distance/depth variable, z
        #--------------------------------------
        zvar = ncps_unit.create_variable('z', 'd', ("nz", ))
        zvar[:] = z_values  # (store the z-values)
        ncps_unit.variables['z'].units = z_units

        #-----------------------------------
        # Create variables using var_names
        #-----------------------------------
        # Returns "var" as a PyNIO object
        #---------------------------------------------------
        # NB! The 3rd argument here (dimension), must be a
        #     tuple.  If there is only one dimension, then
        #     we need to add a comma, as shown.
        #---------------------------------------------------
        for k in xrange(len(var_names)):
            var_name = var_names[k]
            var = ncps_unit.create_variable(var_name, nio_type_codes[k],
                                            ("time", "nz"))

            #------------------------------------
            # Create attributes of the variable
            #------------------------------------
            ncps_unit.variables[var_name].long_name = long_names[k]
            ncps_unit.variables[var_name].units = units_names[k]

            #----------------------------------
            # Specify a "nodata" fill value ?
            #----------------------------------
            var._FillValue = -9999.0  ## Does this jive with Prefill above ??

        self.ncps_unit = ncps_unit
        return OK
示例#11
0
    def open_new_file(self, file_name, info=None,
                      var_name='X',
                      long_name=None,
                      units_name='None',
                      dtype='float32',
                      ### dtype='float64'
                      time_units='minutes',
                      comment='',
                      MAKE_RTI=True, MAKE_BOV=False):
     
        #--------------------------------------------------
        # Try to import the Nio module from PyNIO package
        #--------------------------------------------------
        Nio = self.import_nio()
        if not(Nio): return False

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite( file_name )
        self.file_name = file_name
        
        #---------------------------------------
        # Check and store the grid information
        #---------------------------------------
        self.check_and_store_info( file_name, info, var_name,
                                   dtype, MAKE_RTI, MAKE_BOV )
        if (long_name == None): long_name = var_name
        self.long_name  = long_name
        self.units_name = units_name
        self.dtype      = dtype

        #-------------------------
        # Save the Nio type code
        #-------------------------
        nio_type_map  = self.get_nio_type_map()
        nio_type_code = nio_type_map[ dtype.lower() ]
        self.nio_type_code = nio_type_code
        
        #-------------------------------------
        # Open a new netCDF file for writing
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        opt = Nio.options()
        opt.PreFill = False            # (for efficiency)
        opt.HeaderReserveSpace = 4000  # (4000 bytes, for efficiency)
        history = "Created using PyNIO " + Nio.__version__ + " on "
        history = history + time.asctime() + ". " 
        history = history + comment
        # print 'MADE IT PAST history BLOCK'
        
        try:
            ncgs_unit = Nio.open_file(file_name, mode="w",
                                      options=opt, history=history )
            OK = True
        except:
            OK = False
            return OK

##        print 'nx =', self.info.ncols
##        print 'ny =', self.info.nrows
##        print 'dx =', self.info.xres
##        print 'dy =', self.info.yres
##        print ' '
        
        #----------------------------------------------
        # Create grid dimensions nx and ny, plus time
        #----------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        #----------------------------------------------
        ncgs_unit.create_dimension("nx", int(self.info.ncols))
        ncgs_unit.create_dimension("ny", int(self.info.nrows))
        ncgs_unit.create_dimension("time", None)   # (unlimited dimension)
        # print 'MADE IT PAST create_dimension CALLS.'
        
        #-------------------------
        # Create a time variable
        #------------------------------------------
        #('d' = float64; must match in add_grid()
        #------------------------------------------
        tvar = ncgs_unit.create_variable('time', 'd', ("time",))
        ncgs_unit.variables['time'].units = time_units
        
        #--------------------------------
        # Create a variable in the file
        #----------------------------------
        # Returns "var" as a PyNIO object
        #----------------------------------
        var = ncgs_unit.create_variable(var_name, nio_type_code,
                                        ("time", "ny", "nx"))
        ## var = nc_unit.create_variable(var_name, nio_type_code,
        ##            ("time", "nx", "ny"))

        #-------------------------------------------
        # Create a separate, scalar "time stamp" ?
        #-------------------------------------------
        # t = nc_unit.create_variable("time", nio_type_code, ("time"))
        
        #----------------------------------
        # Specify a "nodata" fill value ?
        #----------------------------------
        var._FillValue = -9999.0    ## Does this jive with Prefill above ??
        
        #------------------------------------
        # Create attributes of the variable
        #------------------------------------
        ncgs_unit.variables[var_name].long_name = long_name
        ncgs_unit.variables[var_name].units     = units_name
        ncgs_unit.variables[var_name].dx        = self.info.xres
        ncgs_unit.variables[var_name].dy        = self.info.yres  ### (12/2/09)
##        ncgs_unit.variables[var_name].dx        = dx
##        ncgs_unit.variables[var_name].dy        = dy  ### (10/15/09)
        ncgs_unit.variables[var_name].y_south_edge = self.info.y_south_edge
        ncgs_unit.variables[var_name].y_north_edge = self.info.y_north_edge
        ncgs_unit.variables[var_name].x_west_edge  = self.info.x_west_edge
        ncgs_unit.variables[var_name].x_east_edge  = self.info.x_east_edge        
        
        self.ncgs_unit = ncgs_unit
        return OK
示例#12
0
    def open_new_file(self, file_name, info=None,
                      var_name='X',
                      long_name=None,
                      units_name='None',
                      dtype='float64',
                      ### dtype='float64'
                      time_units='minutes',
                      comment='',
                      shape=(1,1,1),
                      res=(1.,1.,1.),
                      MAKE_RTI=True, MAKE_BOV=False):
            
        #--------------------------------------------------
        # Try to import the Nio module from PyNIO package
        #--------------------------------------------------
        Nio = self.import_nio ()
        if not Nio:
            return False

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite( file_name )
        self.file_name = file_name
        
        #---------------------------------------
        # Check and store the grid information
        #---------------------------------------
        self.format     = 'nccs'
        self.file_name  = file_name
        self.time_index = 0
        self.var_name   = var_name
        self.shape      = shape
        self.res        = res
        
        if (long_name is None):
            long_name = var_name
        self.long_name  = long_name
        self.units_name = units_name
        self.dtype      = dtype

        #-----------------------------------
        # Get Nio type code for this dtype
        #------------------------------------
        nio_type_map  = self.get_nio_type_map()        
        nio_type_code = nio_type_map[ dtype.lower() ]        
        self.nio_type_code = nio_type_code
        
        #-------------------------------------
        # Open a new netCDF file for writing
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        opt = Nio.options()
        opt.PreFill = False            # (for efficiency)
        opt.HeaderReserveSpace = 4000  # (4000 bytes, for efficiency)
        history = "Created using PyNIO " + Nio.__version__ + " on "
        history = history + time.asctime() + ". " 
        history = history + comment
        # print 'MADE IT PAST history BLOCK'
        
        try:
            nccs_unit = Nio.open_file (file_name, mode="w",
                                       options=opt, history=history)
            OK = True
        except:
            OK = False
            return OK

        #----------------------------------------------
        # Create grid dimensions nx and ny, plus time
        #----------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        #----------------------------------------------
        nccs_unit.create_dimension("nz", self.shape[0])
        nccs_unit.create_dimension("ny", self.shape[1])
        nccs_unit.create_dimension("nx", self.shape[2])
        nccs_unit.create_dimension("time", None)   # (unlimited dimension)
        # print 'MADE IT PAST create_dimension CALLS.'
        
        #-------------------------
        # Create a time variable
        #------------------------------------------
        #('d' = float64; must match in add_cube()
        #------------------------------------------
        tvar = nccs_unit.create_variable ('time', 'd', ("time",))
        nccs_unit.variables['time'].units = time_units
        
        #--------------------------------
        # Create a variable in the file
        #----------------------------------
        # Returns "var" as a PyNIO object
        #----------------------------------
        var = nccs_unit.create_variable (var_name, nio_type_code,
                                         ("time", "nz", "ny", "nx"))

        #----------------------------------
        # Specify a "nodata" fill value ?
        #----------------------------------
        var._FillValue = -9999.0    ## Does this jive with Prefill above ??
        
        #------------------------------------
        # Create attributes of the variable
        #------------------------------------
        nccs_unit.variables[var_name].long_name = long_name
        nccs_unit.variables[var_name].units = units_name
        nccs_unit.variables[var_name].dz = self.res[0]
        nccs_unit.variables[var_name].dy = self.res[1]
        nccs_unit.variables[var_name].dx = self.res[2]
        nccs_unit.variables[var_name].y_south_edge = 0.
        nccs_unit.variables[var_name].y_north_edge = self.res[1]*self.shape[1]
        nccs_unit.variables[var_name].x_west_edge = 0.
        nccs_unit.variables[var_name].x_east_edge = self.res[2]*self.shape[2]
        nccs_unit.variables[var_name].z_bottom_edge = 0.
        nccs_unit.variables[var_name].z_top_edge = self.res[0]*self.shape[0]
        
        self.nccs_unit = nccs_unit
        return OK
示例#13
0
    def open_new_file(
        self,
        file_name,
        info=None,
        var_name="X",
        long_name=None,
        units_name="None",
        dtype="float32",
        ### dtype='float64'
        time_units="minutes",
        comment="",
        MAKE_RTI=True,
        MAKE_BOV=False,
    ):

        # ----------------------------
        # Does file already exist ?
        # ----------------------------
        file_name = file_utils.check_overwrite(file_name)
        self.file_name = file_name

        # ---------------------------------------
        # Check and store the grid information
        # ---------------------------------------
        self.check_and_store_info(file_name, info, var_name, dtype, MAKE_RTI, MAKE_BOV)
        if long_name == None:
            long_name = var_name
        self.long_name = long_name
        self.units_name = units_name
        self.dtype = dtype

        # -----------------------------------
        # Save the two-char data type code
        # -----------------------------------
        dtype_map = self.get_dtype_map()
        dtype_code = dtype_map[dtype.lower()]
        self.dtype_code = dtype_code

        # -------------------------------------
        # Open a new netCDF file for writing
        # -------------------------------------
        try:
            ## format = 'NETCDF4'
            format = "NETCDF4_CLASSIC"
            ncgs_unit = nc.Dataset(file_name, mode="w", format=format)
            OK = True
        except:
            OK = False
            return OK

        # ------------------------------------------------------------
        # Option to pre-fill with fill values
        # Set fill_value for a var with "var._Fill_Value = number"
        # For Nio was:  opt.PreFill = False # (for efficiency)
        # ------------------------------------------------------------
        ncgs_unit.set_fill_off()
        # ncgs_unit.set_fill_on()

        # -------------------------------------
        # Prepare and save a history string
        # -------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        # -------------------------------------
        history = "Created using netCDF4 " + nc.__version__ + " on "
        history = history + time.asctime() + ". "
        history = history + comment
        ncgs_unit.history = history
        # print 'MADE IT PAST history BLOCK'

        ##        print 'nx =', self.info.ncols
        ##        print 'ny =', self.info.nrows
        ##        print 'dx =', self.info.xres
        ##        print 'dy =', self.info.yres
        ##        print ' '

        # ----------------------------------------------
        # Create grid dimensions nx and ny, plus time
        # ----------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        # ----------------------------------------------
        ncgs_unit.createDimension("nx", int(self.info.ncols))
        ncgs_unit.createDimension("ny", int(self.info.nrows))
        ncgs_unit.createDimension("time", None)  # (unlimited dimension)
        # print 'MADE IT PAST create_dimension CALLS.'

        # -------------------------
        # Create a time variable
        # ------------------------------------------
        # ('d' = float64; must match in add_grid()
        # In netCDF4 package, use 'f8' vs. 'd'.
        # ------------------------------------------
        tvar = ncgs_unit.createVariable("time", "f8", ("time",))
        tvar.units = time_units
        ### ncgs_unit.variables['time'].units = time_units

        # --------------------------------
        # Create a variable in the file
        # --------------------------------
        var = ncgs_unit.createVariable(var_name, dtype_code, ("time", "ny", "nx"))

        # ----------------------------------
        # Specify a "nodata" fill value ?
        # ----------------------------------
        # var._Fill_Value = -9999.0    ## Used for pre-fill above ?

        # -------------------------------------------
        # Create a separate, scalar "time stamp" ?
        # -------------------------------------------
        # t = nc_unit.createVariable('time', dtype_code, ('time'))

        # ----------------------------------
        # Specify a "nodata" fill value ?
        # ----------------------------------
        #         var._FillValue = -9999.0    ## Was used for Nio.

        # ------------------------------------
        # Create attributes of the variable
        # ------------------------------------
        ncgs_unit.variables[var_name].long_name = long_name
        ncgs_unit.variables[var_name].units = units_name
        ncgs_unit.variables[var_name].dx = self.info.xres
        ncgs_unit.variables[var_name].dy = self.info.yres  ### (12/2/09)
        ncgs_unit.variables[var_name].y_south_edge = self.info.y_south_edge
        ncgs_unit.variables[var_name].y_north_edge = self.info.y_north_edge
        ncgs_unit.variables[var_name].x_west_edge = self.info.x_west_edge
        ncgs_unit.variables[var_name].x_east_edge = self.info.x_east_edge

        self.ncgs_unit = ncgs_unit
        return OK
示例#14
0
    def open_new_file(
        self,
        file_name,
        var_names=["X"],
        long_names=[None],
        units_names=["None"],
        dtypes=["float32"],
        ### dtypes=['float64'],
        time_units="minutes",
        comment="",
    ):

        # ----------------------------
        # Does file already exist ?
        # ----------------------------
        file_name = file_utils.check_overwrite(file_name)

        # ---------------------------------------
        # Check and store the time series info
        # ---------------------------------------
        self.format = "ncts"
        self.file_name = file_name
        self.time_index = 0
        if long_names[0] == None:
            long_names = var_names
        # -------------------------------------------
        # We may not need to save these in self.
        # I don't think they're used anywhere yet.
        # -------------------------------------------
        self.var_names = var_names
        self.long_names = long_names
        self.units_names = units_names
        self.time_units = time_units
        self.dtypes = dtypes

        # ---------------------------------------------
        # Create array of dtype codes from dtypes
        # for multiple time series (i.e. columns).
        # ---------------------------------------------
        dtype_map = self.get_dtype_map()
        dtype_codes = []
        if len(dtypes) == len(var_names):
            for dtype in dtypes:
                dtype_code = dtype_map[dtype.lower()]
                dtype_codes.append(dtype_code)
        else:
            dtype = dtypes[0]
            dtype_code = dtype_map[dtype.lower()]
            for k in xrange(len(var_names)):
                dtype_codes.append(dtype_code)
        self.dtype_codes = dtype_codes

        # -------------------------------------
        # Open a new netCDF file for writing
        # -------------------------------------
        try:
            ## format = 'NETCDF4'
            format = "NETCDF4_CLASSIC"
            ncts_unit = nc.Dataset(file_name, mode="w", format=format)
            OK = True
        except:
            OK = False
            return OK

        # ------------------------------------------------------------
        # Option to pre-fill with fill values
        # Set fill_value for a var with "var._Fill_Value = number"
        # For Nio was:  opt.PreFill = False # (for efficiency)
        # ------------------------------------------------------------
        ncts_unit.set_fill_off()
        # ncts_unit.set_fill_on()

        # -------------------------------------
        # Prepare and save a history string
        # -------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        # -------------------------------------
        history = "Created using netCDF4 " + nc.__version__ + " on "
        history = history + time.asctime() + ". "
        history = history + comment
        ncts_unit.history = history

        # ------------------------------------------------
        # Create an unlimited time dimension (via None)
        # ------------------------------------------------
        # Without using "int()" for length, we get this:
        #     TypeError: size must be None or integer
        # ------------------------------------------------
        ncts_unit.createDimension("time", None)

        # -------------------------
        # Create a time variable
        # ---------------------------------------------------
        # ('f' = float32; must match in add_values_at_IDs()
        # ---------------------------------------------------
        # NB! Can't use "time" vs. "tvar" here unless we
        #     add "import time" inside this function.
        # ---------------------------------------------------
        tvar = ncts_unit.createVariable("time", "f8", ("time",))
        ncts_unit.variables["time"].units = time_units

        # -----------------------------------
        # Create variables using var_names
        # -----------------------------------
        # Returns "var" as a PyNIO object
        # ---------------------------------------------------
        # NB! The 3rd argument here (dimension), must be a
        #     tuple.  If there is only one dimension, then
        #     we need to add a comma, as shown.
        # ---------------------------------------------------
        for k in xrange(len(var_names)):
            var_name = var_names[k]
            var = ncts_unit.createVariable(var_name, dtype_codes[k], ("time",))

            # ------------------------------------
            # Create attributes of the variable
            # ------------------------------------
            ncts_unit.variables[var_name].long_name = long_names[k]
            ncts_unit.variables[var_name].units = units_names[k]

            # ----------------------------------
            # Specify a "nodata" fill value ?
            # ----------------------------------
            # var._Fill_Value = -9999.0    ## Used for pre-fill above ?

        self.ncts_unit = ncts_unit
        return OK
示例#15
0
    def open_new_file(self,
                      file_name,
                      info=None,
                      var_name='X',
                      long_name=None,
                      units_name='None',
                      dtype='float64',
                      time_units='minutes',
                      comment='',
                      shape=(1, 1, 1),
                      res=(1., 1., 1.),
                      MAKE_RTI=True,
                      MAKE_BOV=False):

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite(file_name)
        self.file_name = file_name

        #---------------------------------------
        # Check and store the grid information
        #---------------------------------------
        self.format = 'nccs'
        self.file_name = file_name
        self.time_index = 0
        self.var_name = var_name
        self.shape = shape
        self.res = res

        if (long_name is None):
            long_name = var_name
        self.long_name = long_name
        self.units_name = units_name
        self.dtype = dtype

        #-----------------------------------
        # Save the two-char data type code
        #-----------------------------------
        dtype_map = self.get_dtype_map()
        dtype_code = dtype_map[dtype.lower()]
        self.dtype_code = dtype_code

        #-------------------------------------
        # Open a new netCDF file for writing
        #-------------------------------------
        try:
            ## format = 'NETCDF4'
            format = 'NETCDF4_CLASSIC'
            nccs_unit = nc.Dataset(file_name, mode='w', format=format)
            OK = True
        except:
            OK = False
            return OK

        #------------------------------------------------------------
        # Option to pre-fill with fill values
        # Set fill_value for a var with "var._Fill_Value = number"
        # For Nio was:  opt.PreFill = False # (for efficiency)
        #------------------------------------------------------------
        ncgs_unit.set_fill_off()
        # ncgs_unit.set_fill_on()

        #-------------------------------------
        # Prepare and save a history string
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        history = "Created using netCDF4 " + nc.__version__ + " on "
        history = history + time.asctime() + ". "
        history = history + comment
        nccs_unit.history = history
        # print 'MADE IT PAST history BLOCK'

        #----------------------------------------------
        # Create grid dimensions nx and ny, plus time
        #----------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        #----------------------------------------------
        nccs_unit.createDimension('nz', self.shape[0])
        nccs_unit.createDimension('ny', self.shape[1])
        nccs_unit.createDimension('nx', self.shape[2])
        nccs_unit.createDimension('time', None)  # (unlimited dimension)
        # print 'MADE IT PAST create_dimension CALLS.'

        #-------------------------
        # Create a time variable
        #------------------------------------------
        #('d' = float64; must match in add_cube()
        #------------------------------------------
        tvar = nccs_unit.createVariable('time', 'f8', ('time', ))
        nccs_unit.variables['time'].units = time_units

        #--------------------------------
        # Create a variable in the file
        #----------------------------------
        # Returns "var" as a PyNIO object
        #----------------------------------
        var = nccs_unit.createVariable(var_name, dtype_code,
                                       ('time', 'nz', 'ny', 'nx'))

        #----------------------------------
        # Specify a "nodata" fill value ?
        #----------------------------------
        # var._Fill_Value = -9999.0    ## Used for pre-fill above ?

        #------------------------------------
        # Create attributes of the variable
        #------------------------------------
        nccs_unit.variables[var_name].long_name = long_name
        nccs_unit.variables[var_name].units = units_name
        #----------------------------------------------------
        nccs_unit.variables[var_name].dz = self.res[0]
        nccs_unit.variables[var_name].dy = self.res[1]
        nccs_unit.variables[var_name].dx = self.res[2]
        nccs_unit.variables[var_name].y_south_edge = 0.
        nccs_unit.variables[
            var_name].y_north_edge = self.res[1] * self.shape[1]
        nccs_unit.variables[var_name].x_west_edge = 0.
        nccs_unit.variables[var_name].x_east_edge = self.res[2] * self.shape[2]
        nccs_unit.variables[var_name].z_bottom_edge = 0.
        nccs_unit.variables[var_name].z_top_edge = self.res[0] * self.shape[0]
        #--------------------------------------------
        # This is how it is done in ncgs_files.py.
        #-------------------------------------------
        #         ncgs_unit.variables[var_name].dx           = self.info.xres
        #         ncgs_unit.variables[var_name].dy           = self.info.yres  ### (12/2/09)
        #         ncgs_unit.variables[var_name].y_south_edge = self.info.y_south_edge
        #         ncgs_unit.variables[var_name].y_north_edge = self.info.y_north_edge
        #         ncgs_unit.variables[var_name].x_west_edge  = self.info.x_west_edge
        #         ncgs_unit.variables[var_name].x_east_edge  = self.info.x_east_edge

        self.nccs_unit = nccs_unit
        return OK
示例#16
0
    def open_new_file(self,
                      file_name,
                      z_values=np.arange(10),
                      z_units='m',
                      var_names=['X'],
                      long_names=[None],
                      units_names=['None'],
                      dtypes=['float64'],
                      time_units='minutes',
                      comment=''):

        #----------------------------------------------------
        # Notes: It might be okay to have "nz" be an
        #        unlimited dimension, like "time".  This
        #        would mean replacing "int(profile_length)"
        #        with "None".
        #----------------------------------------------------

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite(file_name)
        self.file_name = file_name

        #---------------------------------------
        # Check and store the time series info
        #---------------------------------------
        self.format = 'ncps'
        self.file_name = file_name
        self.time_index = 0
        if (long_names[0] == None):
            long_names = var_names
        #-------------------------------------------
        self.z_values = z_values
        self.z_units = z_units
        nz = np.size(z_values)
        #-------------------------------------------
        # We may not need to save these in self.
        # I don't think they're used anywhere yet.
        #-------------------------------------------
        self.var_names = var_names
        self.long_names = long_names
        self.units_names = units_names
        self.dtypes = dtypes

        #---------------------------------------------
        # Create array of Nio type codes from dtypes
        #---------------------------------------------
        dtype_map = self.get_dtype_map()
        dtype_codes = []
        if (len(dtypes) == len(var_names)):
            for dtype in dtypes:
                dtype_code = dtype_map[dtype.lower()]
                dtype_codes.append(dtype_code)
        else:
            dtype = dtypes[0]
            dtype_code = dtype_map[dtype.lower()]
            for k in xrange(len(var_names)):
                dtype_codes.append(dtype_code)
        self.dtype_codes = dtype_codes

        #-------------------------------------
        # Open a new netCDF file for writing
        #-------------------------------------
        try:
            ## format = 'NETCDF4'
            format = 'NETCDF4_CLASSIC'
            ncps_unit = nc.Dataset(file_name, mode='w', format=format)
            OK = True
        except:
            OK = False
            return OK

        #------------------------------------------------------------
        # Option to pre-fill with fill values
        # Set fill_value for a var with "var._Fill_Value = number"
        # For Nio was:  opt.PreFill = False # (for efficiency)
        #------------------------------------------------------------
        ncgs_unit.set_fill_off()
        # ncgs_unit.set_fill_on()

        #-------------------------------------
        # Prepare and save a history string
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        history = "Created using netCDF4 " + nc.__version__ + " on "
        history = history + time.asctime() + ". "
        history = history + comment
        ncps_unit.history = history
        # print 'MADE IT PAST history BLOCK'

        #------------------------------------------------
        # Create an unlimited time dimension (via None)
        #------------------------------------------------
        # Without using "int()" here, we get this:
        #     TypeError: size must be None or integer
        #------------------------------------------------
        ncps_unit.createDimension('nz', int(nz))
        ncps_unit.createDimension('time', None)

        #-------------------------
        # Create a time variable
        #---------------------------------------------------
        #('f' = float32; must match in add_values_at_IDs()
        #---------------------------------------------------
        # NB! Can't use "time" vs. "tvar" here unless we
        #     add "import time" inside this function.
        #---------------------------------------------------
        tvar = ncps_unit.createVariable('time', 'f8', ('time', ))
        ncps_unit.variables['time'].units = time_units

        #--------------------------------------
        # Create a distance/depth variable, z
        #--------------------------------------
        zvar = ncps_unit.createVariable('z', 'd', ('z', ))
        zvar[:] = z_values  # (store the z-values)
        ncps_unit.variables['z'].units = z_units

        #-----------------------------------
        # Create variables using var_names
        #-----------------------------------
        # Returns "var" as a PyNIO object
        #---------------------------------------------------
        # NB! The 3rd argument here (dimension), must be a
        #     tuple.  If there is only one dimension, then
        #     we need to add a comma, as shown.
        #---------------------------------------------------
        for k in xrange(len(var_names)):
            var_name = var_names[k]
            var = ncps_unit.create_variable(var_name, dtype_codes[k],
                                            ("time", "nz"))

            #------------------------------------
            # Create attributes of the variable
            #------------------------------------
            ncps_unit.variables[var_name].long_name = long_names[k]
            ncps_unit.variables[var_name].units = units_names[k]

#----------------------------------
# Specify a "nodata" fill value ?
#----------------------------------
# var._Fill_Value = -9999.0    ## Used for pre-fill above ?

        self.ncps_unit = ncps_unit
        return OK
示例#17
0
    def open_new_file(self, file_name,
                      var_names=['X'],
                      long_names=[None],
                      units_names=['None'],
                      dtypes=['float32'],
                      ### dtypes=['float64'],
                      time_units='minutes',
                      comment=''):

        #----------------------------
        # Does file already exist ?
        #----------------------------
        file_name = file_utils.check_overwrite( file_name )
        
        #---------------------------------------
        # Check and store the time series info
        #---------------------------------------
        self.format     = 'ncts'
        self.file_name  = file_name
        self.time_index = 0
        if (long_names[0] is None):
            long_names = var_names
        #-------------------------------------------
        # We may not need to save these in self.
        # I don't think they're used anywhere yet.
        #-------------------------------------------
        self.var_names   = var_names           
        self.long_names  = long_names
        self.units_names = units_names
        self.time_units  = time_units
        self.dtypes      = dtypes

        #---------------------------------------------
        # Create array of dtype codes from dtypes
        # for multiple time series (i.e. columns).
        #---------------------------------------------
        dtype_map   = self.get_dtype_map()
        dtype_codes = []
        if (len(dtypes) == len(var_names)):
            for dtype in dtypes:
               dtype_code = dtype_map[ dtype.lower() ]
               dtype_codes.append( dtype_code )
        else:
            dtype = dtypes[0]
            dtype_code = dtype_map[ dtype.lower() ]
            for k in xrange(len(var_names)):
                dtype_codes.append( dtype_code )                
        self.dtype_codes = dtype_codes
            
        #-------------------------------------
        # Open a new netCDF file for writing
        #--------------------------------------------------------------
        # Format options are:  NETCDF3_CLASSIC, NETCDF3_64BIT_OFFSET,
        # NETCDF3_64BIT_DATA, NETCDF4_CLASSIC, and NETCDF4
        #-----------------------------------------------------------------
        # NETCDF3_CLASSIC results in MUCH SMALLER filesizes than using
        # NETCDF4_CLASSIC or NETCDF4.
        #   NETCDF3_CLASSIC, June_20_67_0D-Q.nc, 5200 bytes
        #   NETCDF4_CLASSIC, June_20_67_0D-Q.nc, 4217537 Bytes
        # The 2nd one is 811 TIMES BIGGER, even after setting chunksize.
        #-----------------------------------------------------------------
        # For more info see:  http://unidata.github.io/netcdf4-python/
        #-----------------------------------------------------------------
        # The "nccopy" utility can convert between these formats.
        #-----------------------------------------------------------------
        try:
            format = 'NETCDF3_CLASSIC'
            ### format = 'NETCDF4'
            ### format = 'NETCDF4_CLASSIC'  # (before 2/19/17)
            ncts_unit = nc.Dataset(file_name, mode='w', format=format)
            OK = True
        except:
            OK = False
            return OK

        #------------------------------------------------------------
        # Option to pre-fill with fill values
        # Set fill_value for a var with "var._Fill_Value = number"
        # For Nio was:  opt.PreFill = False # (for efficiency)
        #------------------------------------------------------------
        ncts_unit.set_fill_off()
        # ncts_unit.set_fill_on()
         
        #-------------------------------------
        # Prepare and save a history string
        #-------------------------------------
        # Sample output from time.asctime():
        #     "Thu Oct  8 17:10:18 2009"
        #-------------------------------------
        history = "Created using netCDF4 " + nc.__version__ + " on "
        history = history + time.asctime() + ". " 
        history = history + comment
        ncts_unit.history = history
                
        #------------------------------------------------
        # Create an unlimited time dimension (via None)
        #------------------------------------------------
        # Without using "int()" for length, we get this:
        #     TypeError: size must be None or integer
        #------------------------------------------------
        ncts_unit.createDimension("time", None)

        #-------------------------
        # Create a time variable
        #---------------------------------------------------
        #('f' = float32; must match in add_values_at_IDs()
        #---------------------------------------------------
        # NB! Can't use "time" vs. "tvar" here unless we
        #     add "import time" inside this function.
        #---------------------------------------------------
        tvar = ncts_unit.createVariable('time', 'f8', ("time",))
        ncts_unit.variables['time'].units = time_units
        
        #-----------------------------------
        # Create variables using var_names
        #-----------------------------------
        # Returns "var" as a PyNIO object
        #----------------------------------------------------------
        # NB! The 3rd argument here (dimension), must be a tuple.
        # If there is only one dimension, then we need to add a
        # comma, as shown.
        #-----------------------------------------------------------
        # (2/19/17) For "0D" netCDF files created by TF, the files
        # are much too large with the default chunksize.  By using
        # chunksizes=[1], filesize for Treynor is reduced by a
        # factor of 6.9 (4.25 MB vs. 29.38 MB).
        #-----------------------------------------------------------
        # But even this is 287.9 times bigger than the TXT file!
        #-----------------------------------------------------------
        # Default chunksize in NetCDF 4.4.1.1 = 4MB.
        #-----------------------------------------------------------
        n_vars = len( var_names )
        for k in xrange( n_vars ):
            var_name = var_names[k]
            ## var = ncts_unit.createVariable(var_name, dtype_codes[k], ("time",))
            ## var = ncts_unit.createVariable(var_name, dtype_codes[k], ("time",), chunksizes=[512])
            ## var = ncts_unit.createVariable(var_name, dtype_codes[k], ("time",), chunksizes=[1])
            ## var = ncts_unit.createVariable(var_name, dtype_codes[k], ("time",), chunksizes=[4000])
            var = ncts_unit.createVariable(var_name, dtype_codes[k], ("time",), chunksizes=[n_vars])
        
            #------------------------------------
            # Create attributes of the variable
            #------------------------------------
            ncts_unit.variables[var_name].long_name = long_names[k]
            ncts_unit.variables[var_name].units     = units_names[k]        

			#----------------------------------
			# Specify a "nodata" fill value ?
			#----------------------------------
			# var._Fill_Value = -9999.0    ## Used for pre-fill above ?
            
        self.ncts_unit = ncts_unit
        return OK