def test(self, emis, out_paths):
        ''' Master Testing Method.

            Compare the final NetCDF output file emissions to the original EMFAC2014 input files.
            PMEDS or CSE files will by compared by county, date, and EIC.

            emis format: emis.data[region][date string] = EmissionsTable
                             EmissionsTable[eic][poll] = value
        '''
        # Fail politely if not EIC14
        if self.precision != MAX_EIC_PRECISION:
            print(
                '    + Output diurnal profiles can only be tested on EIC14 files.'
            )
            return

        # Build top-emitting EIC list for all pollutants
        eics = EmfacTxtDiurnalTester.find_top_eics(emis)

        # test outputs for each dates
        for date in self.dates:
            d = date[5:]
            if d not in out_paths:
                print(
                    '    + No output text files found for testing on date: ' +
                    d)
                continue
            cse_files = [
                f for f in out_paths[d] if f.rstrip('.gz').endswith('.cse')
            ]
            pmeds_files = [
                f for f in out_paths[d] if f.rstrip('.gz').endswith('.pmeds')
            ]
            if not pmeds_files and not cse_files:
                print(
                    '    + No output text files found for testing on date: ' +
                    d)
                continue

            # find the day-of-week
            if date in find_holidays(self.base_year):
                dow = 'holi'
            else:
                by_date = str(self.base_year) + '-' + d
                dow = DOW[dt.strptime(by_date, self.date_format).weekday()]

            for file_path in pmeds_files:
                output_profs = self._output_pmeds_profiles(file_path, eics)
                self._write_profile_comparision(output_profs, date, dow)

            for file_path in cse_files:
                output_profs = self._output_cse_profiles(file_path, eics)
                self._write_profile_comparision(output_profs, date, dow)
Example #2
0
    def test(self, emis, out_paths):
        ''' Master Testing Method.
            Compare the final NetCDF output file emissions to the original EMFAC2014 input files.
            PMEDS or CSE files will by compared by county, date, and EIC.

            emis format: emis.data[region][date string] = EmissionsTable
                            EmissionsTable[eic][poll] = value
        '''
        # reduce input EIC precision, if necessary
        self._reduce_emissions_eics(emis)

        # loop through each date
        for d in self.dates:
            date = d[5:]
            if date not in out_paths:
                print(
                    '    + No output text files found for testing on date: ' +
                    str(date))
                continue

            # find the day-of-week
            if date in find_holidays(self.base_year):
                dow = 'holi'
            else:
                by_date = str(self.base_year) + '-' + date
                dow = DOW[dt.strptime(by_date, self.date_format).weekday()]

            # test output pmeds, if any
            cse_files = [
                f for f in out_paths[date] if f.rstrip('.gz').endswith('.cse')
            ]
            pmeds_files = [
                f for f in out_paths[date]
                if f.rstrip('.gz').endswith('.pmeds')
            ]
            if pmeds_files or cse_files:
                self._read_and_compare_txt(pmeds_files, cse_files, date, emis,
                                           dow)
Example #3
0
    def scale(self, emissions, spatial_surr, temp_surr):
        """ Master method to scale emissions using spatial and temporal surrogates.
            INPUT FORMATS:
            Emissions: EMFAC2014EmissionsData
                            emis_data[region][date string] = EmissionsTable
                            EmissionsTable[EIC][pollutant] = value
            Spatial Surrogates: SpatialSurrogateData[region][veh][act] = SpatialSurrogate()
            Temporal Surrogates: {'diurnal': {}, 'dow': {}}
            OUTPUT FORMAT:
            ScaledEmissions: data[region][date][hr][eic] = SparseEmissions
                                SparseEmissions[pollutant][(grid, cell)] = value
            NOTE: This function is a generator and will `yield` emissions file-by-file.
        """
        today = deepcopy(self.base_start_date)

        # loop through all the dates in the period
        while today <= self.base_end_date:
            # find the DOW
            date = today.strftime(self.date_format)
            today += timedelta(days=1)
            if date[5:] in find_holidays(self.base_year):
                dow_num = 7
                dow = 'holi'
            else:
                by_date = str(self.base_year) + date[4:]
                dow_num = dt.strptime(by_date, self.date_format).weekday()
                dow = DOW[dt.strptime(by_date, self.date_format).weekday()]

            # if not by sub-region, create emissions object
            if not self.by_region:
                e = ScaledEmissions()

            for region in self.regions:
                if date not in emissions.data[region]:
                    continue

                # if by sub-area, create emissions object
                if self.by_region:
                    e = ScaledEmissions()

                # apply DOW factors (this line is long for performance reasons)
                emis_table = self._apply_factors(
                    deepcopy(emissions.data[region][date]),
                    temp_surr['dow'][region][dow])

                # find diurnal factors by hour
                factors_by_hour = temp_surr['diurnal'][region][dow]

                # pull today's spatial surrogate
                spatial_surrs = spatial_surr.data[region]

                # loop through each hour of the day
                for hr in xrange(24):
                    # apply diurnal, then spatial profiles (this line long for performance reasons)
                    emis_dict = self._apply_spatial_surrs(
                        self._apply_factors(deepcopy(emis_table),
                                            factors_by_hour[hr]),
                        spatial_surrs, region, dow_num, hr)

                    for eic, sparse_emis in emis_dict.iteritems():
                        e.set(region, date, hr + 1, self.eic_reduce(eic),
                              sparse_emis)

                # yield, if by sub-area
                if self.by_region:
                    yield e

            # yield, if not by sub-area
            if not self.by_region:
                yield e
Example #4
0
    def scale(self, emissions, spatial_surr, temp_surr):
        """ Master method to scale emissions using spatial and temporal surrogates.
            INPUT FORMATS:
            Emissions: EMFAC2014EmissionsData
                       emis_data[region][date string] = EmissionsTable
                       EmissionsTable[EIC][pollutant] = value
            Spatial Surrogates: SpatialSurrogateData[region][label] = SpatialSurrogate()
            Temporal Surrogates: {'diurnal': {}, 'dow': {}}
            OUTPUT FORMAT:
            ScaledEmissions: data[region][date][hr][eic] = SparseEmissions
                             SparseEmissions[pollutant][(grid, cell)] = value
            NOTE: This function is a generator and will `yield` emissions file-by-file.
        """
        self._load_species(emissions)

        # find start date
        today = dt(self.base_year, self.start_date.month, self.start_date.day)

        # loop through all the dates in the period
        while today <= self.base_end_date:
            # find the DOW
            date = today.strftime(self.date_format)
            today += timedelta(days=1)
            if date[5:] in find_holidays(self.base_year):
                dow = 'holi'
            else:
                by_date = str(self.base_year) + date[4:]
                dow = DOW[dt.strptime(by_date, self.date_format).weekday()]

            # create a statewide emissions object
            e = self._prebuild_scaled_emissions(date)

            for region in self.regions:
                if date not in emissions.data[region]:
                    continue

                # use the speciation from the correct season
                if self.month2season[region][today.month] == 's':
                    self.gsref = self.summer_gsref
                else:
                    self.gsref = self.winter_gsref

                # handle region bounding box (limits are inclusive: {'lat': (51, 92), 'lon': (156, 207)})
                box = self.region_boxes[region]

                # apply DOW factors (this line is long for performance reasons)
                emis_table = self._apply_factors(
                    deepcopy(emissions.data[region][date]),
                    temp_surr['dow'][region][dow])

                # find diurnal factors by hour
                factors_by_hour = temp_surr['diurnal'][region][dow]

                # pull today's spatial surrogate
                spatial_surrs = spatial_surr.data[region]

                # loop through each hour of the day
                for hr in xrange(24):
                    sparse_emis = self._apply_spatial_surrs(
                        self._apply_factors(emis_table, factors_by_hour[hr]),
                        spatial_surrs, region, box, hr)
                    e.add_subgrid_nocheck(-999, date, hr + 1, -999,
                                          sparse_emis, box)

            yield e