def calc()

in analysis/webservice/algorithms/doms/subsetter.py [0:0]


    def calc(self, request, **args):
        primary_ds_name, matchup_ds_names, parameter_s, start_time, end_time, \
        bounding_polygon, depth_min, depth_max, platforms = self.parse_arguments(request)

        min_lat = max_lat = min_lon = max_lon = None
        if bounding_polygon:
            min_lat = bounding_polygon.bounds[1]
            max_lat = bounding_polygon.bounds[3]
            min_lon = bounding_polygon.bounds[0]
            max_lon = bounding_polygon.bounds[2]

        self.log.info('Fetching tile ids in bounds')

        tile_service = self._get_tile_service()

        tiles = tile_service.find_tiles_in_box(
            min_lat=min_lat, max_lat=max_lat, min_lon=min_lon,
            max_lon=max_lon, ds=primary_ds_name, start_time=start_time,
            end_time=end_time, fetch_data=False
        )

        self.log.info(f'Fetched {len(tiles)} tile ids')
        self.log.info('Processing satellite tiles')

        # Satellite
        data = []
        data_dict = {}
        for i in range(len(tiles)-1, -1, -1):
            tile = tiles.pop(i)

            self.log.debug(f'Processing tile {tile.tile_id}')

            tile = tile_service.fetch_data_for_tiles(tile)[0]

            tile = tile_service.mask_tiles_to_bbox(min_lat, max_lat, min_lon, max_lon, [tile])
            tile = tile_service.mask_tiles_to_time_range(start_time, end_time, tile)

            if len(tile) == 0:
                self.log.debug(f'Skipping empty tile')
                continue

            tile = tile[0]

            def get_best_name(variable):
                if variable.standard_name:
                    return variable.standard_name
                elif variable.variable_name:
                    return variable.variable_name
                else:
                    raise NexusProcessingException(
                        reason=f'Variable in subsetted data does not have defined name',
                        code=500
                    )

            for nexus_point in tile.nexus_point_generator():
                if tile.is_multi:
                    data_points = {
                        get_best_name(tile.variables[idx]): nexus_point.data_vals[idx]
                        for idx in range(len(tile.variables))
                    }
                else:
                    data_points = {get_best_name(tile.variables[0]): nexus_point.data_vals}
                data.append({
                    'latitude': nexus_point.latitude,
                    'longitude': nexus_point.longitude,
                    'time': nexus_point.time,
                    'data': data_points
                })

        data_dict[primary_ds_name] = data

        self.log.info('Finished satellite subsetting')
        self.log.info(f'Processed tiles to {len(data)} points')

        # In-situ
        non_data_fields = [
            'meta',
            'platform',
            'job_id',
            'latitude',
            'longitude',
            'time',
        ]
        for insitu_dataset in matchup_ds_names:
            data = []
            edge_response = query_insitu(
                dataset=insitu_dataset,
                variable=parameter_s,
                start_time=start_time,
                end_time=end_time,
                bbox=','.join(list(map(str, [min_lon, min_lat, max_lon, max_lat]))),
                platform=platforms,
                depth_min=depth_min,
                depth_max=depth_max
            )
            for result in edge_response['results']:
                data_points = {
                    key: value for key, value in result.items()
                    if value is not None and key not in non_data_fields
                }
                data.append({
                    'latitude': result['latitude'],
                    'longitude': result['longitude'],
                    'id': result['meta'],
                    'time': (datetime.strptime(result['time'], '%Y-%m-%dT%H:%M:%SZ') - datetime.fromtimestamp(0)).total_seconds(),
                    'data': data_points
                })
            data_dict[insitu_dataset] = data

        self.log.info('Finished Insitu Subsetting')

        if len(tiles) > 0:
            meta = [tile.get_summary() for tile in tiles]
        else:
            meta = None

        self.log.info('Subsetting complete - creating result')

        result = SubsetResult(
            results=data_dict,
            meta=meta
        )

        result.extendMeta(min_lat, max_lat, min_lon, max_lon, '', start_time, end_time)

        return result