コード例 #1
0
 def from_HDF5(self, filename, **kwargs):
     """
     Read a harmonics object from a HDF5 file
     Inputs: full path of input HDF5 file
     Options:
         HDF5 file contains date information
         keyword arguments for HDF5 reader
     """
     #-- set filename
     self.case_insensitive_filename(filename)
     #-- set default parameters
     kwargs.setdefault('date', True)
     kwargs.setdefault('verbose', False)
     kwargs.setdefault('compression', None)
     #-- read data from HDF5 file
     Ylms = hdf5_read_stokes(self.filename,
                             DATE=kwargs['date'],
                             COMPRESSION=kwargs['compression'])
     #-- copy variables to harmonics object
     self.clm = Ylms['clm'].copy()
     self.slm = Ylms['slm'].copy()
     self.l = Ylms['l'].copy()
     self.m = Ylms['m'].copy()
     self.lmax = np.max(Ylms['l'])
     self.mmax = np.max(Ylms['m'])
     if kwargs['date']:
         self.time = Ylms['time'].copy()
         #-- adjust months to fix special cases if necessary
         self.month = adjust_months(Ylms['month'])
     #-- assign shape and ndim attributes
     self.update_dimensions()
     return self
コード例 #2
0
 def from_list(self, object_list, **kwargs):
     """
     Build a sorted harmonics object from a list of other harmonics objects
     Inputs: list of harmonics object to be merged
     Options:
         harmonics objects contain date information
         sort harmonics objects by date information
         clear the harmonics list from memory
     """
     #-- set default keyword arguments
     kwargs.setdefault('date', True)
     kwargs.setdefault('sort', True)
     kwargs.setdefault('clear', False)
     #-- number of harmonic objects in list
     n = len(object_list)
     #-- indices to sort data objects if harmonics list contain dates
     if kwargs['date'] and kwargs['sort']:
         list_sort = np.argsort([d.time for d in object_list], axis=None)
     else:
         list_sort = np.arange(n)
     #-- truncate to maximum degree and order
     self.lmax = np.min([d.lmax for d in object_list])
     self.mmax = np.min([d.mmax for d in object_list])
     #-- output degree and order
     self.l = np.arange(self.lmax + 1)
     self.m = np.arange(self.mmax + 1)
     #-- create output harmonics
     self.clm = np.zeros((self.lmax + 1, self.mmax + 1, n))
     self.slm = np.zeros((self.lmax + 1, self.mmax + 1, n))
     #-- create list of files
     self.filename = []
     #-- output dates
     if kwargs['date']:
         self.time = np.zeros((n))
         self.month = np.zeros((n), dtype=np.int64)
     #-- for each indice
     for t, i in enumerate(list_sort):
         self.clm[:, :,
                  t] = object_list[i].clm[:self.lmax + 1, :self.mmax + 1]
         self.slm[:, :,
                  t] = object_list[i].slm[:self.lmax + 1, :self.mmax + 1]
         if kwargs['date']:
             self.time[t] = np.atleast_1d(object_list[i].time)
             self.month[t] = np.atleast_1d(object_list[i].month)
         #-- append filename to list
         if getattr(object_list[i], 'filename'):
             self.filename.append(object_list[i].filename)
     #-- adjust months to fix special cases if necessary
     if kwargs['date']:
         self.month = adjust_months(self.month)
     #-- assign shape and ndim attributes
     self.update_dimensions()
     #-- clear the input list to free memory
     if kwargs['clear']:
         object_list = None
     #-- return the single harmonic object
     return self
コード例 #3
0
 def from_HDF5(self, filename, date=True, **kwargs):
     """
     Read a spatial object from a HDF5 file
     Inputs: full path of input HDF5 file
     Options:
         HDF5 file contains date information
         keyword arguments for HDF5 reader
     """
     #-- set filename
     self.case_insensitive_filename(filename)
     #-- set default parameters
     kwargs.setdefault('verbose', False)
     kwargs.setdefault('compression', None)
     kwargs.setdefault('varname', 'z')
     kwargs.setdefault('lonname', 'lon')
     kwargs.setdefault('latname', 'lat')
     kwargs.setdefault('timename', 'time')
     #-- read data from HDF5 file
     data = hdf5_read(self.filename,
                      DATE=date,
                      COMPRESSION=kwargs['compression'],
                      VARNAME=kwargs['varname'],
                      LONNAME=kwargs['lonname'],
                      LATNAME=kwargs['latname'],
                      TIMENAME=kwargs['timename'])
     #-- copy variables to spatial object
     self.data = data['data'].copy()
     if '_FillValue' in data['attributes']['data'].keys():
         self.fill_value = data['attributes']['_FillValue']
     self.mask = np.zeros(self.data.shape, dtype=bool)
     self.lon = data['lon'].copy()
     self.lat = data['lat'].copy()
     #-- if the HDF5 file contains date variables
     if date:
         self.time = data['time'].copy()
         self.month = calendar_to_grace(self.time)
         #-- adjust months to fix special cases if necessary
         self.month = adjust_months(self.month)
     #-- update attributes
     self.attributes.update(data['attributes'])
     #-- get spacing and dimensions
     self.update_spacing()
     self.update_extents()
     self.update_dimensions()
     self.update_mask()
     return self
コード例 #4
0
 def from_gfc(self, filename, **kwargs):
     """
     Read a harmonics object from a gfc gravity model file
     Inputs: full path of input gfc file
     Options:
         keyword arguments for gfc input
     """
     #-- set filename
     self.case_insensitive_filename(filename)
     #-- set default parameters
     kwargs.setdefault('date', False)
     kwargs.setdefault('tide', None)
     kwargs.setdefault('verbose', False)
     #-- read data from gfc file
     if kwargs['date']:
         Ylms = read_gfc_harmonics(self.filename, TIDE=kwargs['tide'])
     else:
         Ylms = read_ICGEM_harmonics(self.filename, TIDE=kwargs['tide'])
     #-- Output file information
     logging.info(self.filename)
     logging.info(list(Ylms.keys()))
     #-- copy variables for gravity model
     self.clm = Ylms['clm'].copy()
     self.slm = Ylms['slm'].copy()
     self.lmax = np.int64(Ylms['max_degree'])
     self.mmax = np.int64(Ylms['max_degree'])
     self.l = np.arange(self.lmax + 1)
     self.m = np.arange(self.mmax + 1)
     #-- copy date variables
     if kwargs['date']:
         self.time = Ylms['time'].copy()
         #-- adjust months to fix special cases if necessary
         self.month = adjust_months(Ylms['month'])
     #-- geophysical parameters of gravity model
     self.GM = np.float64(Ylms['earth_gravity_constant'])
     self.R = np.float64(Ylms['radius'])
     self.tide = Ylms['tide_system']
     #-- assign shape and ndim attributes
     self.update_dimensions()
     return self
コード例 #5
0
 def from_list(self, object_list, **kwargs):
     """
     Build a sorted spatial object from a list of other spatial objects
     Inputs: list of spatial object to be merged
     Options:
         spatial objects contain date information
         sort spatial objects by date information
         clear the spatial list from memory
     """
     #-- set default keyword arguments
     kwargs.setdefault('date', True)
     kwargs.setdefault('sort', True)
     kwargs.setdefault('clear', False)
     #-- number of spatial objects in list
     n = len(object_list)
     #-- indices to sort data objects if spatial list contain dates
     if kwargs['date'] and kwargs['sort']:
         list_sort = np.argsort([d.time for d in object_list], axis=None)
     else:
         list_sort = np.arange(n)
     #-- extract dimensions and grid spacing
     self.spacing = object_list[0].spacing
     self.extent = object_list[0].extent
     self.shape = object_list[0].shape
     #-- create output spatial grid and mask
     self.data = np.zeros((self.shape[0], self.shape[1], n))
     self.mask = np.zeros((self.shape[0], self.shape[1], n), dtype=bool)
     self.fill_value = object_list[0].fill_value
     self.lon = object_list[0].lon.copy()
     self.lat = object_list[0].lat.copy()
     #-- create list of files and attributes
     self.filename = []
     self.attributes = []
     #-- output dates
     if kwargs['date']:
         self.time = np.zeros((n))
         self.month = np.zeros((n), dtype=np.int64)
     #-- for each indice
     for t, i in enumerate(list_sort):
         self.data[:, :, t] = object_list[i].data[:, :].copy()
         self.mask[:, :, t] |= object_list[i].mask[:, :]
         if kwargs['date']:
             self.time[t] = np.atleast_1d(object_list[i].time)
             self.month[t] = np.atleast_1d(object_list[i].month)
         #-- append filename to list
         if getattr(object_list[i], 'filename'):
             self.filename.append(object_list[i].filename)
         #-- append attributes to list
         if getattr(object_list[i], 'attributes'):
             self.attributes.append(object_list[i].attributes)
     #-- adjust months to fix special cases if necessary
     if kwargs['date']:
         self.month = adjust_months(self.month)
     #-- update the dimensions
     self.update_dimensions()
     self.update_mask()
     #-- clear the input list to free memory
     if kwargs['clear']:
         object_list = None
     #-- return the single spatial object
     return self
コード例 #6
0
 def from_ascii(self, filename, date=True, **kwargs):
     """
     Read a spatial object from an ascii file
     Inputs: full path of input ascii file
     Options:
         ascii file contains date information
         keyword arguments for ascii input
     """
     #-- set filename
     self.case_insensitive_filename(filename)
     #-- set default parameters
     kwargs.setdefault('verbose', False)
     kwargs.setdefault('compression', None)
     kwargs.setdefault('columns', ['lon', 'lat', 'data', 'time'])
     kwargs.setdefault('header', 0)
     #-- open the ascii file and extract contents
     logging.info(self.filename)
     if (kwargs['compression'] == 'gzip'):
         #-- read input ascii data from gzip compressed file and split lines
         with gzip.open(self.filename, 'r') as f:
             file_contents = f.read().decode('ISO-8859-1').splitlines()
     elif (kwargs['compression'] == 'zip'):
         #-- read input ascii data from zipped file and split lines
         base, _ = os.path.splitext(self.filename)
         with zipfile.ZipFile(self.filename) as z:
             file_contents = z.read(base).decode('ISO-8859-1').splitlines()
     elif (kwargs['compression'] == 'bytes'):
         #-- read input file object and split lines
         file_contents = self.filename.read().splitlines()
     else:
         #-- read input ascii file (.txt, .asc) and split lines
         with open(self.filename, 'r') as f:
             file_contents = f.read().splitlines()
     #-- compile regular expression operator for extracting numerical values
     #-- from input ascii files of spatial data
     regex_pattern = r'[-+]?(?:(?:\d*\.\d+)|(?:\d+\.?))(?:[EeD][+-]?\d+)?'
     rx = re.compile(regex_pattern, re.VERBOSE)
     #-- output spatial dimensions
     if (None not in self.extent):
         self.lat = np.linspace(self.extent[3], self.extent[2],
                                self.shape[0])
         self.lon = np.linspace(self.extent[0], self.extent[1],
                                self.shape[1])
     else:
         self.lat = np.zeros((self.shape[0]))
         self.lon = np.zeros((self.shape[1]))
     #-- output spatial data
     self.data = np.zeros((self.shape[0], self.shape[1]))
     self.mask = np.zeros((self.shape[0], self.shape[1]), dtype=bool)
     #-- remove time from list of column names if not date
     columns = [c for c in kwargs['columns'] if (c != 'time')]
     #-- extract spatial data array and convert to matrix
     #-- for each line in the file
     header = kwargs['header']
     for line in file_contents[header:]:
         #-- extract columns of interest and assign to dict
         #-- convert fortran exponentials if applicable
         d = {
             c: r.replace('D', 'E')
             for c, r in zip(columns, rx.findall(line))
         }
         #-- convert line coordinates to integers
         ilon = np.int64(np.float64(d['lon']) / self.spacing[0])
         ilat = np.int64((90.0 - np.float64(d['lat'])) // self.spacing[1])
         self.data[ilat, ilon] = np.float64(d['data'])
         self.mask[ilat, ilon] = False
         self.lon[ilon] = np.float64(d['lon'])
         self.lat[ilat] = np.float64(d['lat'])
         #-- if the ascii file contains date variables
         if date:
             self.time = np.array(d['time'], dtype='f')
             self.month = calendar_to_grace(self.time)
     #-- if the ascii file contains date variables
     if date:
         #-- adjust months to fix special cases if necessary
         self.month = adjust_months(self.month)
     #-- get spacing and dimensions
     self.update_spacing()
     self.update_extents()
     self.update_dimensions()
     self.update_mask()
     return self
コード例 #7
0
 def from_ascii(self, filename, **kwargs):
     """
     Read a harmonics object from an ascii file
     Inputs: full path of input ascii file
     Options:
         ascii file contains date information
         keyword arguments for ascii input
     """
     #-- set filename
     self.case_insensitive_filename(filename)
     #-- set default parameters
     kwargs.setdefault('date', True)
     kwargs.setdefault('verbose', False)
     kwargs.setdefault('compression', None)
     #-- open the ascii file and extract contents
     logging.info(self.filename)
     if (kwargs['compression'] == 'gzip'):
         #-- read input ascii data from gzip compressed file and split lines
         with gzip.open(self.filename, 'r') as f:
             file_contents = f.read().decode('ISO-8859-1').splitlines()
     elif (kwargs['compression'] == 'zip'):
         #-- read input ascii data from zipped file and split lines
         base, _ = os.path.splitext(self.filename)
         with zipfile.ZipFile(self.filename) as z:
             file_contents = z.read(base).decode('ISO-8859-1').splitlines()
     elif (kwargs['compression'] == 'bytes'):
         #-- read input file object and split lines
         file_contents = self.filename.read().splitlines()
     else:
         #-- read input ascii file (.txt, .asc) and split lines
         with open(self.filename, 'r') as f:
             file_contents = f.read().splitlines()
     #-- compile regular expression operator for extracting numerical values
     #-- from input ascii files of spherical harmonics
     regex_pattern = r'[-+]?(?:(?:\d*\.\d+)|(?:\d+\.?))(?:[EeD][+-]?\d+)?'
     rx = re.compile(regex_pattern, re.VERBOSE)
     #-- find maximum degree and order of harmonics
     self.lmax = 0
     self.mmax = 0
     #-- for each line in the file
     for line in file_contents:
         if kwargs['date']:
             l1, m1, clm1, slm1, time = rx.findall(line)
         else:
             l1, m1, clm1, slm1 = rx.findall(line)
         #-- convert line degree and order to integers
         l1, m1 = np.array([l1, m1], dtype=np.int64)
         self.lmax = np.copy(l1) if (l1 > self.lmax) else self.lmax
         self.mmax = np.copy(m1) if (m1 > self.mmax) else self.mmax
     #-- output spherical harmonics dimensions array
     self.l = np.arange(self.lmax + 1)
     self.m = np.arange(self.mmax + 1)
     #-- output spherical harmonics data
     self.clm = np.zeros((self.lmax + 1, self.mmax + 1))
     self.slm = np.zeros((self.lmax + 1, self.mmax + 1))
     #-- if the ascii file contains date variables
     if kwargs['date']:
         self.time = np.float64(time)
         self.month = np.int64(calendar_to_grace(self.time))
         #-- adjust months to fix special cases if necessary
         self.month = adjust_months(self.month)
     #-- extract harmonics and convert to matrix
     #-- for each line in the file
     for line in file_contents:
         if kwargs['date']:
             l1, m1, clm1, slm1, time = rx.findall(line)
         else:
             l1, m1, clm1, slm1 = rx.findall(line)
         #-- convert line degree and order to integers
         ll, mm = np.array([l1, m1], dtype=np.int64)
         #-- convert fortran exponentials if applicable
         self.clm[ll, mm] = np.float64(clm1.replace('D', 'E'))
         self.slm[ll, mm] = np.float64(slm1.replace('D', 'E'))
     #-- assign shape and ndim attributes
     self.update_dimensions()
     return self