# Provide the table of all wind power projects in Vietnam.

"""
Created on Fri Jan 22 08:43:32 2021

@author: minh.haduong@gmail.com
"""

from math import isnan
from pandas import read_csv
from numpy import nan

# The database maintained as a Calc file should be Saved As | Text CSV
data = read_csv(
    'All VN wind power projects.csv',
    skiprows=6,
    sep='\t',
    usecols=[
        'Status',
        'Project stage',
        'Project name',
        'Location type',
        'Capacity (MWp)',
        'Turbines',
        'Investment\n(billion VND)',
        'Investment\n(million USD)'],
    index_col='Project name')


def unformat(cell):
    """Remove units suffixes ' MW' and ' M$' then convert to float."""
    try:
        return float(cell[:-3].replace("\xa0", ""))
    except Exception:
        return nan


data['capacity'] = data['Capacity (MWp)'].map(unformat)
data['USD'] = data['Investment\n(million USD)'].map(unformat)
data['VND'] = data['Investment\n(billion VND)'].map(float)


def as_usd(usd, vnd):
    """Merge the costs fields, priority to VND."""
    exchange_rate = 23    # billion VND per million USD
    if not isnan(vnd):
        return vnd / exchange_rate
    if not isnan(usd):
        return usd
    return nan


data['investment'] = [as_usd(usd, vnd)
                      for usd, vnd in zip(data['USD'], data['VND'])]
