from dedalus.extras import flow_tools
from dedalus.tools import post
from dedalus.tools.config import config

from logic.output import initialize_rotating_output
from logic.checkpointing import Checkpoint
from logic.ae_tools import BoussinesqAESolver
from logic.extras import global_noise
from logic.parsing import construct_BC_dict, construct_out_dir

logger = logging.getLogger(__name__)
args = docopt(__doc__)

### 1. Read in command-line args, set up data directory
bc_dict = construct_BC_dict(args,
                            default_T_BC='TT',
                            default_u_BC='FS',
                            default_M_BC=None)
data_dir = construct_out_dir(args,
                             bc_dict,
                             base_flags=['Ek', 'Ra', 'Pr', 'a'],
                             label_flags=['Nu_ICs', 'AE', 'TT_to_FT'],
                             resolution_flags=['nx', 'ny', 'nz'])
logger.info("saving run in: {}".format(data_dir))

run_time_buoy = args['--run_time_buoy']
run_time_therm = args['--run_time_therm']
run_time_wall = float(args['--run_time_wall'])
if run_time_buoy is not None:
    run_time_buoy = float(run_time_buoy)
if run_time_therm is not None:
    run_time_therm = float(run_time_therm)
args = docopt(__doc__)
if args['<config>'] is not None:
    config_file = Path(args['<config>'])
    config = ConfigParser()
    config.read(str(config_file))
    for n, v in config.items('parameters'):
        for k in args.keys():
            if k.split('--')[-1].lower() == n:
                if v == 'true': v = True
                args[k] = v

### 1. Read in command-line args, set up data directory
threeD = args['--3D']
bc_dict = construct_BC_dict(args,
                            default_T_BC='TT',
                            default_u_BC='NS',
                            default_M_BC='MC')
if args['--MI']:
    bc_dict['MC'] = False
    bc_dict['MI'] = True
if args['--FT']:
    bc_dict['TT'] = False
    bc_dict['FT'] = True
elif args['--FF']:
    bc_dict['TT'] = False
    bc_dict['FF'] = True
if args['--FS']:
    bc_dict['NS'] = False
    bc_dict['FS'] = True

if threeD: resolution_flags = ['nx', 'ny', 'nz']