예제 #1
0
def new_mtg_factory(parameters, metamer_factory=adel_metamer, leaf_sectors=1,
                    leaves=None, stand=None, axis_dynamics=None,
                    add_elongation=False,
                    topology=('plant', 'axe_id', 'numphy'), split=False,
                    aborting_tiller_reduction=1.0, leaf_db=None):
    """A 'clone' of mtg_factory that uses mtg_edition functions
    """

    if leaf_db is not None:
        raise AdelDeprecationError(
            'leaf_db argument is deprecated, use leaves argument instead')

    if leaves is None:
        dynamic_leaf_db = {0: False}
        leaves = {0: None}
    else:
        dynamic_leaf_db = {k: leaves[k].dynamic for k in leaves}

    g = MTG()

    # buffers
    # for detection of newplant/newaxe
    prev_plant = 0
    prev_axe = -1

    dp = parameters
    nrow = len(dp['plant'])
    species = 0
    for i in range(nrow):
        plant, num_metamer = [int(convert(dp.get(x)[i], undef=None)) for x in
                              [topology[e] for e in [0, 2]]]
        axe = dp.get(topology[1])[i]
        args = properties_from_dict(dp, i, exclude=topology)

        # Add plant if new
        if plant != prev_plant:
            if axe != 'MS':
                raise ValueError('Main stem is expected first when a new plant '
                                 'is declared')

            position, azimuth = (0, 0, 0), 0
            if stand and len(stand) >= plant:
                position, azimuth = stand[plant - 1]
            plant_properties = {'position': position, 'azimuth': azimuth,
                'refplant_id': args.get('refplant_id'),
                                        'species': species}

            timetable = None
            if axis_dynamics:
                timetable = axis_dynamics[str(plant)][str(axe)]
            mainstem_properties = dict(timetable=timetable,
                                       HS_final=args.get('HS_final'),
                                       nff=args.get('nff'),
                                       hasEar=args.get('hasEar'),
                                       azimuth=args.get('az_insertion'))

            add_plant(g, plant_number=plant, plant_properties=plant_properties,
                      axis_properties=mainstem_properties)
            prev_plant = plant
        # Add axis
        if axe != prev_axe and axe != 'MS':
            timetable = None
            if axis_dynamics:
                timetable = axis_dynamics[str(plant)][str(axe)]
            axe_properties = dict(timetable=timetable,
                                  HS_final=args.get('HS_final'),
                                  nff=args.get('nff'),
                                  hasEar=args.get('hasEar'),
                                  azimuth=args.get('az_insertion'))
            add_axe(g, axe, plant, axis_properties=axe_properties)
            prev_axe = axe
        # Add metamer
        assert num_metamer > 0
        # args are added to metamers only if metamer_factory is none,
        # otherwise compute metamer components
        components = []
        if metamer_factory:
            xysr_key = None
            if leaves[species] is not None and 'LcType' in args and 'LcIndex' in args:
                lctype = int(args['LcType'])
                lcindex = int(args['LcIndex'])
                if lctype != -999 and lcindex != -999:
                    age = None
                    if dynamic_leaf_db[species]:
                        age = float(args[
                                        'rph']) - 0.3  # age_db = HS - rank + 1 = ph - 1.3 - rank +1 = rph - .3
                        if age != 'NA':
                            age = max(0, int(float(age)))
                    xysr_key = leaves[species].get_leaf_key(lctype, lcindex, age)

            elongation = None
            if add_elongation:
                startleaf = -.4
                endleaf = 1.6
                stemleaf = 1.2
                startE = endleaf
                endE = startE + (endleaf - startleaf) / stemleaf
                endBlade = endleaf
                if args['Gl'] > 0:
                    endBlade = args['Ll'] / args['Gl'] * (endleaf - startleaf)
                elongation = {'startleaf': startleaf, 'endBlade': endBlade,
                              'endleaf': endleaf, 'endE': endE}
            if not 'ntop' in args:
                args.update({'ntop': None})
            if not 'Gd' in args:
                args.update({'Gd': 0.19})
            args.update({'split': split})
            if args.get('HS_final') < args.get('nff'):
                for what in (
                        'Ll', 'Lv', 'Lr', 'Lsen', 'L_shape', 'Lw_shape', 'Gl',
                        'Gv', 'Gsen', 'Gd', 'El', 'Ev', 'Esen', 'Ed'):
                    args.update(
                        {what: args.get(what) * aborting_tiller_reduction})
            components = metamer_factory(Lsect=leaf_sectors, shape_key=xysr_key,
                                         elongation=elongation, leaves=leaves[species],
                                         **args)
            args = {'L_shape': args.get('L_shape')}
        #
        metamer_properties = args
        internode, sheath, blade = None, None, None
        elts = {k: [] for k in ('internode', 'sheath', 'blade')}
        if len(components) > 0:
            internode, sheath, blade = components
            internode.pop('label')
            sheath.pop('label')
            blade.pop('label')
            elts['internode'] = internode.pop('elements')
            elts['sheath'] = sheath.pop('elements')
            elts['blade'] = blade.pop('elements')

        vid_metamer = add_vegetative_metamer(g, plant, axe,
                                             metamer_properties=metamer_properties,
                                             internode_properties=internode,
                                             sheath_properties=sheath,
                                             blade_properties=blade)
        for organ in g.components(vid_metamer):
            label = g.property('label')[organ]
            if label in elts and len(elts[label]) > 0:
                insert_elements(g, organ, elts[label])
    return fat_mtg(g)