def test(theseargs): parser = argparse.ArgumentParser(description='Download bulk information from Ofgem to produce an Excel spreadsheet') parser.add_argument('--start', action='store', required=True, help='Period to start from (MMM-YYYY)') parser.add_argument('--end', action='store', required=True, help='Period to finish on (MMM-YYYY)') parser.add_argument('--scheme', action='store', default='RO', help='Scheme to get certificates for') parser.add_argument('--filename', action='store', default='certificates.xls', help='Filename to export to') parser.add_argument('--name', action='store', default=None, help='Part of a name of generation station (or name fragments for several stations, separated by commas)') args = parser.parse_args(args=theseargs) (start_year, start_month) = get_period(args.start) (end_year, end_month) = get_period(args.end) if not args.filename.endswith('.xls'): args.filename += '.xls' periods = [] for yy in range(start_year, end_year + 1): mm = start_month if start_year == yy else 1 mm2 = end_month if end_year == yy else 12 for m in range(mm, mm2+1): periods.append(date(yy,m,1).strftime("%b-%Y")) station = args.name if station is None: stations = [] while (True): station = input("Enter a station name (or blank to finish)") if station.strip() == '': break if ',' in station: for s in station.strip().split(','): s = s.strip() if s in stations: continue stations.append(s) else: station = station.strip() if station in stations: continue stations.append(station) else: stations = station.strip().split(',') if len(stations) == 0: print("No stations to process. Exiting...") sys.exit(0) wb = Workbook() ws = wb.add_sheet('Certificate Data', cell_overwrite_ok=True) ws.write(PERIOD_START - 1,0,"Period") for i in range(0, len(periods)): ws.write(PERIOD_START + i, 0, periods[i]) al = Alignment() al.horz = Alignment.HORZ_CENTER al.vert = Alignment.VERT_CENTER title_style = XFStyle() title_style.alignment = al print("\nTotal of %d stations to process\n" % len(stations)) for i in range(0, len(stations)): s = stations[i] col = 1 + 5 * i print(" "+ s) # add headers ws.write(PERIOD_START - 1, col, "Installed Capacity") ws.write(PERIOD_START - 1, col + 1, "RO Certificates") ws.write(PERIOD_START - 1, col + 2, "RO Factor") ws.write(PERIOD_START - 1, col + 3, "REGO Certificates") ws.write(PERIOD_START - 1, col + 4, "REGO Factor") capacity = {} for scheme in ['RO','REGO']: offset = 1 if scheme == 'RO' else 3 ss = StationSearch() ss.filter_scheme(scheme) ss.filter_name(s) if not ss.get_data(): print("Unable to find any station with a name %s" % s) continue station = None # if more than one generator has matched, use the first WIND one if len(ss.stations) > 1: print('Several stations found that match %s:' % s) print(list(st.name for st in ss.stations)) print('The first wind station will be selected') for st in ss.stations: if 'wind' in st.technology.lower(): station = st break else: station = ss.stations[0] if station is None: print("Unable to get station data for '%s'" % s) continue # Write name ws.write_merge(PERIOD_START - 4, PERIOD_START - 4, col, col + 4, station.name, title_style) # add accreditation # if scheme == 'RO': ws.write_merge(PERIOD_START - 2, PERIOD_START - 2, col, col + 4, 'RO: ' + station.accreditation + ' [' + station.commission_dt.strftime("%d %b %Y") + ']', title_style) elif scheme == 'REGO': ws.write_merge(PERIOD_START - 3, PERIOD_START - 3, col, col + 4, 'REGO: ' + station.accreditation + ' [' + station.commission_dt.strftime("%d %b %Y") + ']', title_style) cs = CertificateSearch() cs.set_start_month(start_month) cs.set_start_year(start_year) cs.set_finish_month(end_month) cs.set_finish_year(end_year) cs.filter_accreditation(station.accreditation) #cs.filter_scheme(scheme) # seems to work ok without this, and REGOs break with it #cs.filter_status(['Issued','Redeemed','Expired']) # this doesn't work. And arguable whether expired should count if not cs.get_data(): print("Unable to get any certificate data :-(") continue data = {} for c in cs.certificates: # print(c.as_string()) if c.status in ['Revoked','Retired','Expired']: continue row = periods.index(c.period) + PERIOD_START if not c.period in capacity: ws.write(row, col, c.capacity) capacity[c.period] = True data[c.period] = data.get(c.period,0) + c.certs ws.write(row, col + offset + 1, c.factor) for p,val in viewitems(data): row = periods.index(p) + PERIOD_START ws.write(row, col + offset, val) print("\nComplete. Excel spreadsheet %s" % args.filename) wb.save(args.filename)
def main(): parser = commandline_parser('Search ofgem database for matching stations') parser.add_argument('--generator', action='store', help='Generator ID to search for') parser.add_argument('--organisation', action='store', help='Organisation to search for') args = parser.parse_args() if args.station is None and \ args.generator is None and \ args.organisation is None: print("You must specify either a name, generator id or organisation") sys.exit(0) setup_logging(args.debug, request_logging=args.request_debug) print("Connecting with Ofgem website and preparing the search...") osd = StationSearch() osd.start() print("Setting up filters:") if args.station is not None: osd.filter_name(args.station) print(" - station name contains {}".format(args.station)) if args.organisation: osd.filter_organisation(args.organisation) print(" - organisation contains {}".format(args.organisation)) if args.generator: if args.generator.upper()[0] in ['R', 'P']: osd.filter_scheme('RO') elif args.generator.upper()[0] == 'G': osd.filter_scheme('REGO') osd.filter_generator_id(args.generator.upper()) print(" - generator ID is {}".format(args.generator.upper())) print("\nGetting results from Ofgem...\n") if osd.get_data() is False: print("Search returned no data") sys.exit(0) print("Query returned {} result{}".format(len(osd), '' if len(osd) == 0 else 's')) fmt = StdoutFormatter("35s", "13s", "12.2f", "20s", "20s", "15s") print( fmt.titles("Station Name", "Commission Dt", "Capacity", "Technology", "Country", "Generator ID")) for stat in osd.rows(): # The rows() output is intended for exporting, so fields will have '@' prepended # if they are attributes. # This could also be done by using osd.stations station = stat.get('Station') if station is None: continue cdt = station.get('@commission_dt') print( fmt.row(station.get('@name'), cdt.strftime("%Y-%m-%d") if cdt else 'n/a', station.get('@capacity'), station.get('@technology'), station.get('@country'), station.get('@generator_id')))
help='Scheme to search (ignored for accreditation) [default REGO]') parser.add_argument('--organisation', action='store', help='Organisation to search for') parser.add_argument('--output', action='store', help='Filename to output data into (as CSV)') args = parser.parse_args() print("Connecting with Ofgem website and preparing the search...") osd = StationSearch() crit = "Searching for Ofgem Stations where:\n\tscheme is %s" % args.scheme osd.filter_scheme(args.scheme) if args.name: osd.filter_name(args.name) crit += "\n\tname contains '%s'" % args.name if args.organisation: osd['organisation'] = args.organisation crit += "\n\torganisation is '%s'" % args.organisation if args.generator: if args.generator.upper()[0] == 'R': osd.filter_scheme('RO') elif args.generator.upper()[0] == 'G': osd.filter_scheme('REGO') osd.filter_generator_id(args.generator.upper())
def main(): parser = commandline_parser('Search ofgem database for matching stations') parser.add_argument('--generator', action='store', help='Generator ID to search for') parser.add_argument('--organisation', action='store', help='Organisation to search for') args = parser.parse_args() if args.station is None and \ args.generator is None and \ args.organisation is None: print("You must specify either a name, generator id or organisation") sys.exit(0) setup_logging(args.debug, request_logging=args.request_debug) print("Connecting with Ofgem website and preparing the search...") osd = StationSearch() osd.start() print("Setting up filters:") if args.station is not None: osd.filter_name(args.station) print(" - station name contains {}".format(args.station)) if args.organisation: osd.filter_organisation(args.organisation) print(" - organisation contains {}".format(args.organisation)) if args.generator: if args.generator.upper()[0] in ['R', 'P']: osd.filter_scheme('RO') elif args.generator.upper()[0] == 'G': osd.filter_scheme('REGO') osd.filter_generator_id(args.generator.upper()) print(" - generator ID is {}".format(args.generator.upper())) print("\nGetting results from Ofgem...\n") if osd.get_data() is False: print("Search returned no data") sys.exit(0) print("Query returned {} result{}".format(len(osd), '' if len(osd) == 0 else 's')) fmt = StdoutFormatter("35s", "13s", "12.2f", "20s", "20s", "15s") print(fmt.titles("Station Name", "Commission Dt", "Capacity", "Technology", "Country", "Generator ID")) for stat in osd.rows(): # The rows() output is intended for exporting, so fields will have '@' prepended # if they are attributes. # This could also be done by using osd.stations station = stat.get('Station') if station is None: continue cdt = station.get('@commission_dt') print(fmt.row(station.get('@name'), cdt.strftime("%Y-%m-%d") if cdt else 'n/a', station.get('@capacity'), station.get('@technology'), station.get('@country'), station.get('@generator_id')))