-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a "Put Horizon" jupyter notebook
- Loading branch information
1 parent
69fb8e3
commit 66bbb79
Showing
2 changed files
with
316 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,315 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "40a17e1f-53f0-4ec9-b2a4-2718acf2a59e", | ||
"metadata": {}, | ||
"source": [ | ||
"The very first step is to download and install the FETPAPI wheel (which depends on FESAPI) : pip install fetpapi" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a16222c5-064e-431d-9dae-d3d6c535a6b9", | ||
"metadata": {}, | ||
"source": [ | ||
"Once FETPAPI wheel installed, you need to first import FESAPI (required dependency) and then fetpapi it in order to be able to call its API." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "fe2772db-0f12-4ba3-bd7c-17890092dc63", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import fesapi\n", | ||
"import fetpapi" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "0e0f0067-7c3e-4b4f-9da9-b6a4da7f8929", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's now connect to an ETP server by defining some few initialization parameters such as the URL and the authorization" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "6d5cda5b-7627-4405-a4c0-ced8edbe9257", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import uuid\n", | ||
"\n", | ||
"etp_server_url = \"wss://.../\"\n", | ||
"authorization = \"Bearer ...\"\n", | ||
"initialization_params = fetpapi.InitializationParameters(str(uuid.uuid4()), etp_server_url)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d7d5c21e-8b14-4bd6-a78f-d0bef5014e4f", | ||
"metadata": {}, | ||
"source": [ | ||
"Some ETP servers such as the OSDU RDDMS one require some extra header fields. Let's add them" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "5c3e7a41-5270-4a06-9125-af784c097f17", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"additionalHeaderField = fetpapi.MapStringString()\n", | ||
"additionalHeaderField[\"data-partition-id\"] = \"osdu\"\n", | ||
"initialization_params.setAdditionalHandshakeHeaderFields(additionalHeaderField)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "dcfbaeab-a787-47ff-b323-437bb3b556df", | ||
"metadata": {}, | ||
"source": [ | ||
"We can now create the ETP session." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "a987ec54-86a8-424f-9539-48b87a9e8dea", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"client_session = fetpapi.createClientSession(initialization_params, authorization)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b564949a-03b8-4988-b4d8-c779b2f3fe2a", | ||
"metadata": {}, | ||
"source": [ | ||
"And we can finally run the session in parallel not to block the main thread" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "c6245f4b-5acd-454b-921d-99e767c36c79", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def start_etp_server(client_session):\n", | ||
" client_session.run()\n", | ||
"\n", | ||
"from threading import Thread\n", | ||
"t = Thread(target=start_etp_server, args=(client_session,), daemon=True)\n", | ||
"t.start()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "0580e85e-0040-423c-8825-ebfb511ba26f", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's wait for the connection to be effective" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b5d289e6-c699-4d88-a870-3dae2baf14ee", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from time import sleep, perf_counter\n", | ||
"start_time = perf_counter()\n", | ||
"while client_session.isEtpSessionClosed() and perf_counter() - start_time < 5:\n", | ||
" sleep(0.25)\t\n", | ||
"if client_session.isEtpSessionClosed():\n", | ||
" print(\"The ETP session could not be established in 5 seconds.\")\n", | ||
"else:\n", | ||
" print(\"Now connected to ETP Server\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "abbc43cb", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's create a dataspace" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "76b6025a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"dataspace = fetpapi.Dataspace()\n", | ||
"dataspace.path = \"demo/PutHorizon\"\n", | ||
"dataspace.uri = \"eml:///dataspace('\" + dataspace.path + \"')\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3687c127", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's put the dataspace to the ETP store (optional if it already exists)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "5496dd41", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"dataspaces = fetpapi.MapStringDataspace()\n", | ||
"dataspaces[\"0\"] = dataspace\n", | ||
"successKeys = client_session.putDataspaces(dataspaces)\n", | ||
"if (len(successKeys) == 1):\n", | ||
" print(\"Dataspace has been put\")\n", | ||
"else:\n", | ||
" print(\"Error when putting dataspace\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "01ff657b", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's create a RESQML horizon in order to put it to the ETP store" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"id": "60b670a8", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Create a DataObjectRepository in order to store Energistics dataobjects in-memory.\n", | ||
"repo = fesapi.DataObjectRepository()\n", | ||
"# Create an unknown local depth oriented 3D CRS in meters with no offset and no rotation\n", | ||
"local_3d_crs = repo.createLocalDepth3dCrs(\"b2129512-b8f9-4721-8a70-1abac53ef406\", \"Default CRS\",\n", | ||
" 0.0, 0.0, 0.0, 0.0,\n", | ||
" fesapi.eml20__LengthUom_m, 5215,\n", | ||
" fesapi.eml20__LengthUom_m, \"Unknown\",\n", | ||
" False)\n", | ||
"repo.setDefaultCrs(local_3d_crs)\n", | ||
"repo.setHdfProxyFactory(fetpapi.FesapiHdfProxyFactory(client_session));\n", | ||
"hdf_proxy = repo.createHdfProxy(\"f8160b8f-0517-4c55-ab6e-ed8bcdc87111\", \"Hdf Proxy\",\n", | ||
" \".\", \"fake.h5\",\n", | ||
" fesapi.DataObjectRepository.openingMode_OVERWRITE)\n", | ||
"hdf_proxy.setUriSource(dataspace.uri)\n", | ||
"repo.setDefaultHdfProxy(hdf_proxy)\n", | ||
"# We can now create plenty of Energistics dataobjects. Let's try a Horizon 2d grid.\n", | ||
"horizon_feature = repo.createHorizon(\"c0f12836-41f4-44a8-a3fd-95ac78f6232d\", \"My horizon feature\")\n", | ||
"horizon_interpretation = repo.createHorizonInterpretation(horizon_feature, \"dc217b29-8ceb-4b77-bdcc-6bcfd9cd3baf\", \"My horizon interpretation\")\n", | ||
"horizon_grid_2d_representation = repo.createGrid2dRepresentation(horizon_interpretation, \"7721fb3c-39ba-4d59-ba0b-f9451706a94c\", \"My horizon representation\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "cebaf539", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's write the DataArray of the horizon directly on the ETP store" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"id": "b87cb468", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Create first a transaction in order to be able to rollback if necessary and to easier resolve dependencies to put\n", | ||
"dataspacesToLock = fetpapi.StringVector()\n", | ||
"dataspacesToLock.append(dataspace.uri)\n", | ||
"client_session.startTransaction(dataspacesToLock)\n", | ||
"# And let's set a 2x3 geometry on this 2d grid where the Z values equal hundred times the node index.\n", | ||
"# The first (aka I) axis of this 2d grid is aligned on X and the second (aka J) axis of this 2d grid is aligned on Y.\n", | ||
"# The spacing on the first axis between two node is 25m and the spacing on the second axis between two node is 50m\n", | ||
"resqml_points = fesapi.DoubleArray(24)\n", | ||
"for i in range(6):\n", | ||
" resqml_points.setitem(i, i*100)\n", | ||
"horizon_grid_2d_representation.setGeometryAsArray2dOfExplicitZ(resqml_points, 2, 3, hdf_proxy,\n", | ||
" 0.0, 0.0, 0.0,\n", | ||
" 1.0, 0.0, 0.0, 25.0,\n", | ||
" 0.0, 1.0, 0.0, 50.0)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5836e70c", | ||
"metadata": {}, | ||
"source": [ | ||
"At this time, the data array has been put on the ETP store, we still need to put the XML parts before to commit the transaction" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e3b57d4c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"dataobjects = fetpapi.MapStringDataObject()\n", | ||
"allUuids = repo.getUuids()\n", | ||
"for i, uuid in enumerate(allUuids):\n", | ||
" dataObj = repo.getDataObjectByUuid(uuid)\n", | ||
" dataObj.setUriSource(dataspace.uri)\n", | ||
" dataobjects[str(i)] = fetpapi.buildEtpDataObjectFromEnergisticsObject(dataObj)\n", | ||
"client_session.putDataObjects(dataobjects)\n", | ||
"# Finally commit the tansaction now that we have put everything we wanted\n", | ||
"client_session.commitTransaction()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "60dc75ab", | ||
"metadata": {}, | ||
"source": [ | ||
"We can now close the session" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"id": "693c0fcf", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"client_session.close();" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters