Example #1
0
    def test_ncCreateFile(self):
        """Test nctools creates a file successfully"""
        ncobj = netCDF4.Dataset(self.ncfile,
                                'w',
                                format='NETCDF4',
                                clobber=True)
        # output data.
        press_out = 900. + np.arange(self.nlevs * self.nlats * \
                                     self.nlons, dtype='f')
        press_out.shape = (self.nlevs, self.nlats, self.nlons)
        temp_out = 9. + np.arange(self.nlevs * self.nlats * \
                                  self.nlons, dtype='f')
        temp_out.shape = (self.nlevs, self.nlats, self.nlons)
        # create the lat and lon dimensions.
        nctools.ncCreateDim(ncobj, 'lat', self.lats_out, 'f', self.lat_atts)
        nctools.ncCreateDim(ncobj, 'lon', self.lons_out, 'f', self.lon_atts)
        # create level dimension.
        nctools.ncCreateDim(ncobj, 'level', np.arange(self.nlevs), 'f')
        # create time dimension (record, or unlimited dimension)
        nctools.ncCreateDim(ncobj, 'time', np.arange(self.nrecs), 'f',
                            self.time_atts)
        dimensions = ('time', 'level', 'lat', 'lon')

        press = nctools.ncCreateVar(ncobj, 'pressure', dimensions, 'float64')
        temp = nctools.ncCreateVar(ncobj, 'temperature', dimensions, 'float64')

        press.units = 'hPa'
        temp.units = 'celsius'

        for nrec in range(self.nrecs):
            press[nrec, :, ::] = press_out
            temp[nrec, :, ::] = temp_out
        ncobj.close()
Example #2
0
 def test_ncCreateFile(self):
     """Test nctools creates a file successfully"""
     ncobj = netCDF4.Dataset(self.ncfile, 'w', format='NETCDF4',
                             clobber=True)
     # output data.
     press_out = 900. + np.arange(self.nlevs * self.nlats * \
                                  self.nlons, dtype='f') 
     press_out.shape = (self.nlevs, self.nlats, self.nlons) 
     temp_out = 9. + np.arange(self.nlevs * self.nlats * \
                               self.nlons, dtype='f') 
     temp_out.shape = (self.nlevs, self.nlats, self.nlons) 
     # create the lat and lon dimensions.
     nctools.ncCreateDim(ncobj, 'lat', self.lats_out, 'f', self.lat_atts)
     nctools.ncCreateDim(ncobj, 'lon', self.lons_out, 'f', self.lon_atts)
     # create level dimension.
     nctools.ncCreateDim(ncobj, 'level', np.arange(self.nlevs), 'f')
     # create time dimension (record, or unlimited dimension)
     nctools.ncCreateDim(ncobj, 'time', np.arange(self.nrecs), 'f',
                         self.time_atts)
     dimensions = ('time', 'level', 'lat', 'lon')
     
     press = nctools.ncCreateVar(ncobj, 'pressure',
                                 dimensions, 'float64')
     temp = nctools.ncCreateVar(ncobj, 'temperature',
                                dimensions, 'float64')
     
     press.units =  'hPa'
     temp.units = 'celsius'
     
     for nrec in range(self.nrecs):
         press[nrec,:,::] = press_out
         temp[nrec,:,::] = temp_out
     ncobj.close()
Example #3
0
    def setUp(self):
        self.ncfile = pjoin(unittest_dir, 'test_data', 'pres_temp_4D.nc')

        self.nrecs = 2
        self.nlevs = 2
        self.nlats = 6
        self.nlons = 12
        self.lats_out = -25.0 + 5.0 * np.arange(self.nlats, dtype='float')
        self.lons_out = 125.0 + 5.0 * np.arange(self.nlons, dtype='float')
        self.latrange = [min(self.lats_out), max(self.lats_out)]
        self.lonrange = [min(self.lons_out), max(self.lons_out)]
        self.lat_atts = {
            'units': 'degrees_north',
            'long_name': 'latitude',
            'actual_range': self.latrange
        }
        self.lon_atts = {
            'units': 'degrees_east',
            'long_name': 'longitude',
            'actual_range': self.lonrange
        }
        self.time_atts = {
            'units': 'hours since 2000-01-01 00:00:00',
            'calendar': 'standard'
        }

        press_out = 900. + np.arange(self.nrecs * self.nlevs * \
                                     self.nlats * self.nlons,
                                     dtype='f')
        press_out.shape = (self.nrecs, self.nlevs, self.nlats, self.nlons)
        temp_out = 9. + np.arange(self.nrecs * self.nlevs * \
                                  self.nlats * self.nlons,
                                  dtype='f')
        temp_out.shape = (self.nrecs, self.nlevs, self.nlats, self.nlons)

        self.dimensions = {
            0: {
                'name': 'time',
                'values': np.arange(self.nrecs),
                'dtype': int,
                'atts': self.time_atts
            },
            1: {
                'name': 'level',
                'values': np.arange(self.nlevs),
                'dtype': 'f',
                'atts': {}
            },
            2: {
                'name': 'lat',
                'values': self.lats_out,
                'dtype': 'f',
                'atts': self.lat_atts
            },
            3: {
                'name': 'lon',
                'values': self.lons_out,
                'dtype': 'f',
                'atts': self.lon_atts
            }
        }

        self.variables = {
            0: {
                'name': 'pressure',
                'values': press_out,
                'dtype': 'float64',
                'dims': ('time', 'level', 'lat', 'lon'),
                'atts': {
                    'units': 'hPa',
                    'standard_name': 'air_pressure'
                }
            },
            1: {
                'name': 'temperature',
                'values': temp_out,
                'dtype': 'float64',
                'dims': ('time', 'level', 'lat', 'lon'),
                'atts': {
                    'units': 'degrees_celcius',
                    'standard_name': 'air_temperature'
                }
            }
        }

        ncobj = netCDF4.Dataset(self.ncfile,
                                'w',
                                format='NETCDF4',
                                clobber=True)
        # output data.
        press_out = 900. + np.arange(self.nlevs * self.nlats * \
                                     self.nlons, dtype='f')
        press_out.shape = (self.nlevs, self.nlats, self.nlons)
        temp_out = 9. + np.arange(self.nlevs * self.nlats * \
                                  self.nlons, dtype='f')
        temp_out.shape = (self.nlevs, self.nlats, self.nlons)
        # create the lat and lon dimensions.
        nctools.ncCreateDim(ncobj, 'lat', self.lats_out, 'f', self.lat_atts)
        nctools.ncCreateDim(ncobj, 'lon', self.lons_out, 'f', self.lon_atts)
        # create level dimension.
        nctools.ncCreateDim(ncobj, 'level', np.arange(self.nlevs), 'f')
        # create time dimension (record, or unlimited dimension)
        nctools.ncCreateDim(ncobj, 'time', np.arange(self.nrecs), 'f',
                            self.time_atts)
        dimensions = ('time', 'level', 'lat', 'lon')

        press = nctools.ncCreateVar(ncobj, 'pressure', dimensions, 'float64')
        temp = nctools.ncCreateVar(ncobj, 'temperature', dimensions, 'float64')

        press.units = 'hPa'
        temp.units = 'celsius'

        for nrec in range(self.nrecs):
            press[nrec, :, ::] = press_out
            temp[nrec, :, ::] = temp_out
        ncobj.close()
Example #4
0
    def setUp(self):
        self.ncfile = pjoin(unittest_dir, 'test_data', 'pres_temp_4D.nc')
        
        self.nrecs = 2; self.nlevs = 2; self.nlats = 6; self.nlons = 12
        self.lats_out = -25.0 + 5.0*np.arange(self.nlats, dtype='float')
        self.lons_out = 125.0 + 5.0*np.arange(self.nlons, dtype='float')
        self.latrange = [min(self.lats_out), max(self.lats_out)]
        self.lonrange = [min(self.lons_out), max(self.lons_out)]
        self.lat_atts = {'units': 'degrees_north',
                         'long_name': 'latitude',
                         'actual_range': self.latrange}
        self.lon_atts = {'units': 'degrees_east',
                         'long_name': 'longitude',
                         'actual_range': self.lonrange}
        self.time_atts = {'units': 'hours since 2000-01-01 00:00:00',
                          'calendar': 'standard'}

        press_out = 900. + np.arange(self.nrecs * self.nlevs * \
                                     self.nlats * self.nlons,
                                     dtype='f') 
        press_out.shape = (self.nrecs, self.nlevs,
                           self.nlats, self.nlons) 
        temp_out = 9. + np.arange(self.nrecs * self.nlevs * \
                                  self.nlats * self.nlons,
                                  dtype='f')
        temp_out.shape = (self.nrecs, self.nlevs,
                          self.nlats, self.nlons) 

        
        self.dimensions = {
            0: {
                'name': 'time',
                'values': np.arange(self.nrecs),
                'dtype': int,
                'atts': self.time_atts
                },
            1: {
                'name': 'level',
                'values': np.arange(self.nlevs),
                'dtype': 'f',
                'atts': {}
                },
            2: {
                'name': 'lat',
                'values': self.lats_out,
                'dtype': 'f',
                'atts': self.lat_atts
                },
            3: {
                'name': 'lon',
                'values': self.lons_out,
                'dtype': 'f',
                'atts': self.lon_atts
                }
            }
        
        self.variables = {
            0: {
                'name': 'pressure',
                'values': press_out,
                'dtype': 'float64',
                'dims': ('time', 'level', 'lat', 'lon'),
                'atts': {
                    'units': 'hPa',
                    'standard_name': 'air_pressure'
                    }
                },
            1: {
                'name': 'temperature',
                'values': temp_out,
                'dtype': 'float64',
                'dims': ('time', 'level', 'lat', 'lon'),
                'atts': {
                    'units': 'degrees_celcius',
                    'standard_name': 'air_temperature'
                    }
                }
            }
        
        ncobj = netCDF4.Dataset(self.ncfile, 'w', format='NETCDF4',
                                clobber=True)
        # output data.
        press_out = 900. + np.arange(self.nlevs * self.nlats * \
                                     self.nlons, dtype='f') 
        press_out.shape = (self.nlevs, self.nlats, self.nlons) 
        temp_out = 9. + np.arange(self.nlevs * self.nlats * \
                                  self.nlons, dtype='f') 
        temp_out.shape = (self.nlevs, self.nlats, self.nlons) 
        # create the lat and lon dimensions.
        nctools.ncCreateDim(ncobj, 'lat', self.lats_out, 'f',
                            self.lat_atts)
        nctools.ncCreateDim(ncobj, 'lon', self.lons_out, 'f',
                            self.lon_atts)
        # create level dimension.
        nctools.ncCreateDim(ncobj, 'level', np.arange(self.nlevs), 'f')
        # create time dimension (record, or unlimited dimension)
        nctools.ncCreateDim(ncobj, 'time', np.arange(self.nrecs), 'f',
                            self.time_atts)
        dimensions = ('time', 'level', 'lat', 'lon')
        
        press = nctools.ncCreateVar(ncobj, 'pressure', dimensions, 'float64')
        temp = nctools.ncCreateVar(ncobj, 'temperature', dimensions, 'float64')
        
        press.units =  'hPa'
        temp.units = 'celsius'
        
        for nrec in range(self.nrecs):
            press[nrec,:,::] = press_out
            temp[nrec,:,::] = temp_out
        ncobj.close()