Exemple #1
0
 def test_output(self):
     """
     Test full span of crude timezones from UTC-12 to UTC+12. Note the
     degeneracy at +-180.
     """
     longitudes = np.arange(-180, 185, 15)
     expected = np.arange(-12, 13, 1)
     result = set_utc_offset(longitudes)
     self.assertArrayEqual(expected, result)
Exemple #2
0
    def parse_input(self, site_data):
        """
        Perform checks on sitedata input and use it to create a standard format
        sites dictionary.

        Args:
            site_data (dictionary):
                A dictionary of site data for checking and use in constructing
                a consistently formatted site dictionary. This step is to allow
                for input from either a read in file or for definition on the
                command line.

                Contains : Required

                latitudes/longitudes (lists of floats):
                    Lists of coordinates at which to extract data, these define
                    the positions of the SpotData sites.

                Contains : Optional

                altitudes (list of floats):
                    List of altitudes that can optionally be used in defining
                    sites on the fly. If unset the altitudes will be assumed as
                    equivalent to which ever neighbouring grid point is used as
                    a data source.

                utc_offsets (list of floats):
                    List of utc_offsets for calculating local time of the site.

        Returns:
            sites (dict):
                Dictionary of site data.

        Raises:
            KeyError : If longitude or latitude information is not found.

        """
        latitude_entries = [i_site for (i_site, site) in enumerate(site_data)
                            if 'latitude' in site.keys()]
        longitude_entries = [i_site for (i_site, site) in enumerate(site_data)
                             if 'longitude' in site.keys()]

        if not latitude_entries or not longitude_entries:
            raise KeyError('longitude and latitude must be defined for '
                           'site in site_data file')

        # Raise an error if there are an unequal number of latitudes and
        # longitudes as it is indicative of an error in the input data.
        if latitude_entries != longitude_entries:
            raise ValueError(
                'Unequal no. of latitudes ({}) and longitudes ({}).'.format(
                    len(latitude_entries), len(longitude_entries)))
        else:
            valid_entries = latitude_entries

        site_data = [site_data[i_site] for i_site in valid_entries]

        self.latitudes = np.array([site['latitude'] for site in site_data])
        self.longitudes = np.array([site['longitude'] for site in site_data])

        n_sites = len(self.latitudes)

        # If altitudes are unset use np.nan to indicate that they are at the
        # altitude of their neighbouring grid point. Likewise, if sites are
        # wmo sites set wmo_site to wmo_id, else set it to 0.
        self.altitudes = np.full(n_sites, np.nan)
        self.wmo_site = np.full(n_sites, 0, dtype=int)
        for i_site, site in enumerate(site_data):
            if 'altitude' in site.keys() and site['altitude'] is not None:
                self.altitudes[i_site] = site['altitude']
            if 'wmo_id' in site.keys() and site['wmo_id'] is not None:
                self.wmo_site[i_site] = site['wmo_id']

        # Identify UTC offset if it is provided in the input, otherwise set it
        # based upon site longitude.
        self.utc_offsets = np.full(n_sites, np.nan)
        for i_site, site in enumerate(site_data):
            if 'gmtoffset' in site.keys():
                self.utc_offsets[i_site] = site['gmtoffset']
            elif 'utcoffset' in site.keys():
                self.utc_offsets[i_site] = site['utcoffset']
            elif 'utc_offset' in site.keys():
                self.utc_offsets[i_site] = site['utc_offset']

            # If it's not been set, set it with the longitude based method.
            if np.isnan(self.utc_offsets[i_site]):
                self.utc_offsets[i_site], = set_utc_offset([site['longitude']])

        return self.construct_site_dictionary()