|
21 | 21 | from . import converters
|
22 | 22 | from . import schemas
|
23 | 23 |
|
| 24 | +import numpy as np |
| 25 | + |
24 | 26 | LOGGER = logging.getLogger(__name__)
|
25 | 27 |
|
26 | 28 | router = APIRouter()
|
@@ -215,8 +217,92 @@ async def get_property_surface_resampled_to_statistical_static_surface(
|
215 | 217 | @router.post("/well_intersection_reals_from_user_session")
|
216 | 218 | async def well_intersection_reals_from_user_session(
|
217 | 219 | request: Request,
|
218 |
| - polyline: schemas.FencePolyline = Body(embed=True), |
| 220 | + ensemble_ident: schemas.EnsembleIdent = Body(embed=True), |
| 221 | + realizations_surface_set_spec: schemas.RealizationsSurfaceSetSpec = Body(embed=True), |
| 222 | + surface_fence_spec: schemas.SurfaceFenceSpec = Body(embed=True), |
219 | 223 | authenticated_user: AuthenticatedUser = Depends(AuthHelper.get_authenticated_user),
|
220 | 224 | ) -> List[schemas.SurfaceIntersectionPoints]:
|
221 | 225 | response = await proxy_to_user_session(request, authenticated_user)
|
222 | 226 | return response
|
| 227 | + |
| 228 | + |
| 229 | +@router.post("/well_intersection_statistics") |
| 230 | +async def well_intersection_statistics( |
| 231 | + request: Request, |
| 232 | + ensemble_ident: schemas.EnsembleIdent = Body(embed=True), |
| 233 | + statistical_surface_set_spec: schemas.StatisticalSurfaceSetSpec = Body(embed=True), |
| 234 | + surface_fence_spec: schemas.SurfaceFenceSpec = Body(embed=True), |
| 235 | + authenticated_user: AuthenticatedUser = Depends(AuthHelper.get_authenticated_user), |
| 236 | +) -> List[schemas.SurfaceIntersectionPoints]: |
| 237 | + access = await SurfaceAccess.from_case_uuid( |
| 238 | + authenticated_user.get_sumo_access_token(), ensemble_ident.case_uuid, ensemble_ident.ensemble_name |
| 239 | + ) |
| 240 | + surfaces = [] |
| 241 | + |
| 242 | + async def fetch_surface(statistic, surface_name): |
| 243 | + surface = await access.get_statistical_surface_data_async( |
| 244 | + statistic_function=StatisticFunction.from_string_value(statistic), |
| 245 | + name=surface_name, |
| 246 | + attribute=statistical_surface_set_spec.surface_attribute, |
| 247 | + realizations=statistical_surface_set_spec.realization_nums, |
| 248 | + ) |
| 249 | + surface.name = surface_name + "_" + statistic |
| 250 | + return surface |
| 251 | + |
| 252 | + async def fetch_all_surfaces(): |
| 253 | + tasks = [] |
| 254 | + for statistic in statistical_surface_set_spec.statistic_function: |
| 255 | + for surface_name in statistical_surface_set_spec.surface_names: |
| 256 | + # Create a task for each combination of statistic and surface_name |
| 257 | + task = fetch_surface(statistic, surface_name) |
| 258 | + tasks.append(task) |
| 259 | + |
| 260 | + # Run all the tasks concurrently |
| 261 | + tmp_surfaces = await asyncio.gather(*tasks) |
| 262 | + return tmp_surfaces |
| 263 | + |
| 264 | + # for statistic in statistical_surface_set_spec.statistic_function: |
| 265 | + # for surface_name in statistical_surface_set_spec.surface_names: |
| 266 | + # surface = await access.get_statistical_surface_data_async( |
| 267 | + # statistic_function=StatisticFunction.from_string_value(statistic), |
| 268 | + # name=surface_name, |
| 269 | + # attribute=statistical_surface_set_spec.surface_attribute, |
| 270 | + # realizations=statistical_surface_set_spec.realization_nums, |
| 271 | + # ) |
| 272 | + # surface.name = surface_name + "_" + statistic |
| 273 | + # surfaces.append(surface) |
| 274 | + surfaces = await fetch_all_surfaces() |
| 275 | + |
| 276 | + fence_arr = np.array( |
| 277 | + [ |
| 278 | + surface_fence_spec.x_points, |
| 279 | + surface_fence_spec.y_points, |
| 280 | + np.zeros(len(surface_fence_spec.y_points)), |
| 281 | + surface_fence_spec.cum_length, |
| 282 | + ] |
| 283 | + ).T |
| 284 | + intersections = await make_intersections(surfaces, fence_arr) |
| 285 | + print(intersections) |
| 286 | + return intersections |
| 287 | + |
| 288 | + |
| 289 | +from concurrent.futures import ThreadPoolExecutor |
| 290 | +import asyncio |
| 291 | + |
| 292 | + |
| 293 | +async def make_intersections(surfaces, fence_arr): |
| 294 | + def make_intersection(surf): |
| 295 | + line = surf.get_randomline(fence_arr) |
| 296 | + intersection = schemas.SurfaceIntersectionPoints( |
| 297 | + name=f"{surf.name}", |
| 298 | + cum_length=line[:, 0].tolist(), |
| 299 | + z_array=line[:, 1].tolist(), |
| 300 | + ) |
| 301 | + return intersection |
| 302 | + |
| 303 | + loop = asyncio.get_running_loop() |
| 304 | + |
| 305 | + with ThreadPoolExecutor() as executor: |
| 306 | + tasks = [loop.run_in_executor(executor, make_intersection, surf) for surf in surfaces] |
| 307 | + intersections = await asyncio.gather(*tasks) |
| 308 | + return intersections |
0 commit comments