#
# Copyright 2013 Simone Campagna
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

__author__ = 'Simone Campagna'

from structparser.core import *

class FileTypeEnum(EnumType):
    """Enumeration for file types:
 * RAW-C:       C raw file
 * RAW-FORTRAN: FORTRAN raw file
 * ASCII:       ASCII file
"""
    __items__ = ('RAW-C', 'RAW-FORTRAN', 'ASCII')

class FileType(Enum):
    """wraps the FileTypeEnum enumeration"""
    __type__ = FileTypeEnum

class Params(Struct):
    __fields__ = (
        FieldType("alpha",
            field_type=Float,
            description="the alpha coefficient"),
        FieldType("beta",
            field_type=Float,
            description="the beta coefficient"),
        FieldType("order",
            field_type=PositiveInt,
            default=1,
            description="the equation order"),
        FieldType("coefficients",
            field_type=FloatList(BIND.order + 1),
            description="the equation coefficients"),
    )

class ParamsSubFile(BaseSubFile):
    __subtype__ = Params

class ExperimentalParams(Struct):
    __fields__ = (
        FieldType("gamma_0",
            field_type=Float,
            default=0.95,
            description="gamma_0 coefficient"),
        FieldType("gamma_1",
            field_type=Float,
            default=-0.95,
            description="gamma_1 coefficient"),
        FieldType("label",
            field_type=Str,
            default="exp-0",
            description="label"),
    )

class Dataset(Struct):
    __fields__ = (
        FieldType("version",
            field_type=Str,
            default="r1",
            description="version number"),
        FieldType("r1",
            field_type=Int,
            description="r1 value"),
        FieldType("num_vars",
            field_type=NonNegativeInt,
            description="number of variables"),
        FieldType("num_tsteps",
            field_type=NonNegativeInt,
            description="number of time steps"),
        FieldType("spacing",
            field_type=FloatList(3),
            description="x, y, z spacing"),
        FieldType("var_names",
            field_type=StrList(BIND.num_vars),
            description="name of the variables"),
        FieldType("tsteps",
            field_type=FloatList(BIND.num_tsteps),
            description="value of the time steps"),
        FieldType("file_type",
            field_type=FileType,
            default=FileTypeEnum.ASCII,
            description="file type: {{{0}}}".format(', '.join(item.label for item in FileTypeEnum))),
        FieldType("files",
            field_type=PathList(BIND.num_vars * BIND.num_tsteps),
            description="var data files"),
        FieldType("mix_files",
            field_type=PathList(BIND.num_vars ** 2),
            description="mix var data files"),
        FieldType("experimental_flag",
            field_type=Switch,
            description="enable experimental mode; it switches on/off the 'experimental_params' section"),
        FieldType("experimental_params",
            field_type=Switched(ExperimentalParams, BIND.experimental_flag),
            description="experimental mode parameters; they all have a default value"),
        FieldType("params_file",
            field_type=ParamsSubFile,
            description="parameters file"),
    )


