def parse_args()

in analysis/webservice/algorithms/Tomogram.py [0:0]


    def parse_args(self, compute_options):
        ds, parameter = super().parse_args(compute_options)

        def get_required_float(name):
            val = compute_options.get_float_arg(name, None)

            if val is None:
                raise NexusProcessingException(reason=f'Missing required parameter: {name}', code=400)

            return val

        def get_required_int(name):
            val = compute_options.get_int_arg(name, None)

            if val is None:
                raise NexusProcessingException(reason=f'Missing required parameter: {name}', code=400)

            return val

        longitude = compute_options.get_argument('longitude', None)
        output = compute_options.get_content_type().lower()

        if longitude is None:
            raise NexusProcessingException(reason='Missing required parameter: longitude', code=400)
        if re.match(FLOAT_PATTERN, longitude):
            longitude = float(longitude)

            if longitude < -180 or longitude > 180:
                raise NexusProcessingException(reason='Invalid longitude: out of range', code=400)

            longitude = [longitude]
        elif ',' in longitude:
            if output == 'png':
                raise NexusProcessingException(reason='Cannot use PNG output with multi-longitude slicing', code=400)

            try:
                longitude = list(sorted([float(l) for l in longitude.split(',')]))
            except ValueError:
                raise NexusProcessingException(reason='Invalid longitude: invalid format', code=400)

            if any([l < -180 or l > 180 for l in longitude]):
                raise NexusProcessingException(reason='Invalid longitude: out of range', code=400)
        elif ':' in longitude:
            if output == 'png':
                raise NexusProcessingException(reason='Cannot use PNG output with multi-longitude slicing', code=400)

            try:
                longitude = slice(*(float(p) if p else None for p in longitude.split(':')))
            except:
                raise NexusProcessingException(reason='Invalid longitude: invalid format', code=400)

            if longitude.start is not None and longitude.stop is not None and longitude.start >= longitude.stop:
                raise NexusProcessingException(reason='Invalid longitude: out of order', code=400)

            # Some helpers for a < b / a > b where a could be None and return false in that case
            def lt(a, b):
                return a is not None and a < b

            def gt(a, b):
                return a is not None and a > b

            if (lt(longitude.start, -180) or gt(longitude.start, 180) or
                    lt(longitude.stop, -180) or gt(longitude.stop, 180)):
                raise NexusProcessingException(reason='Invalid longitude: out of range', code=400)
        elif longitude == '*':
            if output == 'png':
                raise NexusProcessingException(reason='Cannot use PNG output with multi-longitude slicing', code=400)

            longitude = slice(None, None, None)
        else:
            raise NexusProcessingException(reason='Invalid longitude argument', code=400)

        min_lat = get_required_float('minLat')
        max_lat = get_required_float('maxLat')
        min_elevation = get_required_int('minElevation')
        max_elevation = get_required_int('maxElevation')

        horizontal_margin = compute_options.get_float_arg('horizontalMargin', 0.001)

        ch_ds = compute_options.get_argument('canopy_ds', None)
        g_ds = compute_options.get_argument('ground_ds', None)

        if ch_ds is not None:
            ch_ds = [v.strip() for v in ch_ds.split(',')]
        else:
            ch_ds = []

        if len(ch_ds) > 2:
            raise NexusProcessingException(
                reason='Too many datasets provided for canopy_ds', code=400
            )

        if g_ds is not None:
            g_ds = [v.strip() for v in g_ds.split(',')]
        else:
            g_ds = []

        if len(g_ds) > 2:
            raise NexusProcessingException(
                reason='Too many datasets provided for ground_ds', code=400
            )

        percentiles = compute_options.get_boolean_arg('elevPercentiles')

        if percentiles and (ch_ds is None or g_ds is None):
            raise NexusProcessingException(
                code=400,
                reason='\'elevPercentiles\' argument requires \'canopy_ds\' and \'ground_ds\' be set'
            )

        peaks = compute_options.get_boolean_arg('peaks') and not percentiles

        cmap = mpl.colormaps.get(
            compute_options.get_argument('cmap', 'viridis'),
            mpl.colormaps['viridis']
        )

        return (ds, parameter, longitude, min_lat, max_lat, min_elevation, max_elevation, horizontal_margin, peaks,
                ch_ds, g_ds, percentiles, cmap)