Skip to content

Commit 9126b14

Browse files
authored
Merge pull request #387 from amnp95/istanbulTapisV3
updating Istanbul motions to use tapisv3 app
2 parents df34e9a + 057bf02 commit 9126b14

File tree

3 files changed

+144
-106
lines changed

3 files changed

+144
-106
lines changed
Lines changed: 135 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,161 @@
1-
# %% # noqa: INP001, D100
1+
import os
22
import json
3-
import os
43
import time
54
from datetime import datetime
6-
7-
from agavepy.agave import Agave
5+
from tapipy.tapis import Tapis
86

97
# change the directory to the current directory
10-
os.chdir(os.path.dirname(os.path.realpath(__file__))) # noqa: PTH120
8+
os.chdir(os.path.dirname(os.path.realpath(__file__)))
9+
1110

11+
def Submit_tapis_job(username=None, password=None):
12+
# Initialize Tapis client with credentials
13+
if username is not None and password is not None:
14+
t = Tapis(
15+
base_url='https://designsafe.tapis.io', username=username, password=password
16+
)
17+
t.get_tokens()
18+
else:
19+
# Use existing authentication if no credentials provided
20+
t = Tapis(base_url='https://designsafe.tapis.io')
21+
t.get_tokens()
1222

13-
def Submit_tapis_job(): # noqa: N802, D103
14-
ag = Agave.restore()
15-
with open('TapisFiles/information.json') as file: # noqa: PTH123
23+
with open('TapisFiles/information.json') as file:
1624
information = json.load(file)
1725
file.close()
1826

19-
# %%
20-
profile = ag.profiles.get()
21-
username = profile['username']
22-
savingDirectory = information['directory'] # noqa: N806
23-
if not os.path.exists(savingDirectory): # noqa: PTH110
24-
os.makedirs(savingDirectory) # noqa: PTH103
27+
savingDirectory = information['directory']
28+
if not os.path.exists(savingDirectory):
29+
os.makedirs(savingDirectory)
30+
31+
print('Uploading files to designsafe storage')
32+
try:
33+
t.files.mkdir(
34+
systemId='designsafe.storage.default', path=f'{t.username}/physics_based'
35+
)
36+
except:
37+
# Directory might already exist
38+
pass
39+
40+
try:
41+
t.files.mkdir(
42+
systemId='designsafe.storage.default', path=f'{t.username}/physics_based/Istanbul'
43+
)
44+
except:
45+
# Directory might already exist
46+
pass
2547

26-
print('Uploading files to designsafe storage') # noqa: T201
27-
ag.files.manage(
48+
# Upload Istanbul.py
49+
with open('TapisFiles/Istanbul.py', 'rb') as file:
50+
contents = file.read()
51+
result = t.files.insert(
2852
systemId='designsafe.storage.default',
29-
filePath=f'{username}/',
30-
body={'action': 'mkdir', 'path': 'physics_based'},
53+
path=f'{t.username}/physics_based/Istanbul/Istanbul.py',
54+
file=contents,
3155
)
32-
ag.files.manage(
56+
57+
# Upload information.json
58+
with open('TapisFiles/information.json', 'rb') as file:
59+
contents = file.read()
60+
result = t.files.insert(
3361
systemId='designsafe.storage.default',
34-
filePath=f'{username}/physics_based',
35-
body={'action': 'mkdir', 'path': 'Istanbul'},
62+
path=f'{t.username}/physics_based/Istanbul/information.json',
63+
file=contents,
3664
)
37-
# ag.files_mkdir(systemId="designsafe.storage.default", filePath=f"{username}/physics_based/Istanbul2")
38-
with open('TapisFiles/Istanbul.py', 'rb') as file: # noqa: PTH123
39-
result = ag.files.importData(
40-
filePath=f'{username}/physics_based/Istanbul/',
41-
fileToUpload=file,
42-
systemId='designsafe.storage.default',
43-
)
44-
with open('TapisFiles/information.json', 'rb') as file: # noqa: PTH123
45-
result = ag.files.importData(
46-
filePath=f'{username}/physics_based/Istanbul/',
47-
fileToUpload=file,
48-
systemId='designsafe.storage.default',
49-
)
50-
with open('TapisFiles/selectedSites.csv', 'rb') as file: # noqa: PTH123
51-
result = ag.files.importData( # noqa: F841
52-
filePath=f'{username}/physics_based/Istanbul/',
53-
fileToUpload=file,
54-
systemId='designsafe.storage.default',
55-
)
5665

57-
# %%
66+
# Upload selectedSites.csv
67+
with open('TapisFiles/selectedSites.csv', 'rb') as file:
68+
contents = file.read()
69+
result = t.files.insert(
70+
systemId='designsafe.storage.default',
71+
path=f'{t.username}/physics_based/Istanbul/selectedSites.csv',
72+
file=contents,
73+
)
74+
75+
# Define inputs
76+
input_Directory = (
77+
f'tapis://designsafe.storage.default/{t.username}/physics_based/Istanbul'
78+
)
79+
fileInputs = [{'name': 'Input Directory', 'sourceUrl': input_Directory}]
80+
81+
# Define parameterSet
82+
input_filename = 'Istanbul.py'
83+
parameterSet = {
84+
'envVariables': [
85+
{'key': 'inputScript', 'value': input_filename},
86+
]
87+
}
88+
89+
# Generate a timestamp to append to the job name
90+
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
91+
jobname = f'PhysicsBasedMotion_Istanbul_{t.username}_{timestamp}'
92+
5893
jobdict = {
59-
'name': '',
60-
'appId': 'physicsBasedMotionApp-0.0.1',
94+
'name': jobname,
95+
'appId': 'SimCenter-DesignSafeVM',
96+
'appVersion': '0.0.1',
97+
'execSystemId': 'wma-exec-01',
6198
'nodeCount': 1,
62-
'processorsPerNode': 1,
63-
'archive': True,
99+
'coresPerNode': 1,
100+
'maxMinutes': 30,
64101
'archiveOnAppError': True,
65-
'inputs': {'inputDirectory': ''},
66-
'parameters': {'inputScript': 'Istanbul.py'},
67-
'maxRunTime': '00:01:00',
68-
'memoryPerNode': '1GB',
69-
'archiveSystem': 'designsafe.storage.default',
102+
'archiveSystemId': 'designsafe.storage.default',
103+
'archiveSystemDir': f'{t.username}/physics_based/Istanbul/',
104+
'fileInputs': fileInputs,
105+
'parameterSet': parameterSet,
106+
'tags': ['portalName: DESIGNSAFE'],
70107
}
71108

72-
# Generate a timestamp to append to the job name an
73-
timestamp = datetime.now().strftime('%Y%m%d%H%M%S') # noqa: DTZ005
74-
jobname = f'PhysicsBasedMotion_Istanbul_{username}_{timestamp}'
109+
print('Submitting job')
110+
res = t.jobs.submitJob(**jobdict)
111+
job_uuid = res.uuid
75112

76-
print('Submitting job') # noqa: T201
77-
# submit the job
78-
jobdict['name'] = jobname
79-
jobdict['inputs']['inputDirectory'] = (
80-
f'agave://designsafe.storage.default/{username}/physics_based/Istanbul/'
81-
)
82-
83-
# %%
84-
res = ag.jobs.submit(body=jobdict)
85-
jobid = res['id']
86-
status = ''
87-
last_status = ''
88-
count = 0
89-
while status != 'FINISHED':
90-
status = ag.jobs.getStatus(jobId=jobid)['status']
91-
if count == 0:
92-
last_status = status
93-
print('Job status: ', status) # noqa: T201
94-
count += 1
95-
if last_status != status:
96-
print('Job status: ', status) # noqa: T201
97-
last_status = status
98-
if status == 'FAILED':
99-
print('Job failed') # noqa: T201
113+
# Monitor job status
114+
tlapse = 10
115+
status = t.jobs.getJobStatus(jobUuid=job_uuid).status
116+
previous = status
117+
print(f'Job status: {status}')
118+
119+
while True:
120+
if status in ['FINISHED', 'FAILED', 'STOPPED', 'CANCELLED']:
100121
break
122+
123+
time.sleep(tlapse)
124+
status = t.jobs.getJobStatus(jobUuid=job_uuid).status
125+
126+
if status != previous:
127+
print(f'Job status: {status}')
128+
previous = status
129+
130+
# Download results
131+
print('Downloading extracted motions')
132+
archivePath = t.jobs.getJob(jobUuid=job_uuid).archiveSystemDir
133+
archivePath = f'{archivePath}/Istanbul/Events'
134+
135+
try:
136+
files = t.files.listFiles(
137+
systemId='designsafe.storage.default', path=archivePath
138+
)
101139

102-
time.sleep(10)
140+
if len(files) <= 1:
141+
print('No files in the archive')
142+
else:
143+
for file in files:
144+
filename = file.name
145+
if filename == '.':
146+
continue
147+
148+
path = f'{archivePath}/{filename}'
149+
res = t.files.getContents(
150+
systemId='designsafe.storage.default', path=path
151+
)
103152

104-
# %%
105-
print('Downloading extracted motions') # noqa: T201
106-
archivePath = ag.jobs.get(jobId=jobid)['archivePath'] # noqa: N806
107-
archivePath = f'{archivePath}/Istanbul/Events/' # noqa: N806
153+
with open(f'{savingDirectory}/{filename}', 'w') as f:
154+
f.write(res.decode('utf-8'))
155+
156+
print('Files downloaded')
157+
print('Please check the directory for the extracted motions')
158+
except Exception as e:
159+
print(f'Error downloading files: {e}')
108160

109-
files = ag.files.list(
110-
filePath=archivePath, systemId='designsafe.storage.default'
111-
)
112-
# %%
113-
if len(files) <= 1:
114-
print('No files in the archive') # noqa: T201
115-
else:
116-
for file in files:
117-
filename = file['name']
118-
if filename == '.':
119-
continue
120-
path = f'{archivePath}/{filename}'
121-
res = ag.files.download(
122-
filePath=path, systemId='designsafe.storage.default'
123-
)
124-
with open(f'{savingDirectory}/{filename}', 'wb') as f: # noqa: PTH123
125-
f.write(res.content)
126-
# %%
161+
return res

modules/createEVENT/Istanbul/IstanbulRun.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import argparse # noqa: INP001, D100
1+
import argparse
22
import os
33

44
import IstanbulApp2
@@ -31,6 +31,9 @@
3131
parser.add_argument(
3232
'-o', '--output', help='number of realizations', required=False
3333
)
34+
# Add username and password args for tapipy
35+
parser.add_argument('--username', help='username', required=False)
36+
parser.add_argument('--password', help='password', required=False)
3437
args = parser.parse_args()
3538

3639
if args.lat:
@@ -45,5 +48,5 @@
4548
# change the directory to the file location
4649
os.chdir(os.path.dirname(os.path.realpath(__file__))) # noqa: PTH120
4750
IstanbulStations.getStations(information, plot=False, show=False)
48-
IstanbulApp2.Submit_tapis_job()
49-
exit() # noqa: PLR1722
51+
IstanbulApp2.Submit_tapis_job(args.username, args.password)
52+
exit()

modules/createEVENT/Istanbul/TapisFiles/Istanbul.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json # noqa: E402
99

1010
import numpy as np # noqa: E402
11-
import pandas as pd # noqa: E402
11+
import pandas as pd # noqa: E402
1212

1313

1414
def Istanbul(information): # noqa: N802, D103
@@ -35,10 +35,10 @@ def Istanbul(information): # noqa: N802, D103
3535

3636
if 'TopoFlag':
3737
# IstanbulDirectory = '/corral-repl/projects/NHERI/published/PRJ-3712/GM_data/GM_topo/'
38-
IstanbulDirectory = '/home/jovyan/work/projects/PRJ-3712/GM_data/GM_topo/' # noqa: N806
38+
IstanbulDirectory = '/home/NHERI-Published/PRJ-3712/GM_data/GM_topo' # noqa: N806
3939
else:
4040
# IstanbulDirectory = '/corral-repl/projects/NHERI/published/PRJ-3712/GM_data/GM_flat/'
41-
IstanbulDirectory = '/home/jovyan/work/projects/PRJ-3712/GM_data/GM_flat/' # noqa: N806
41+
IstanbulDirectory = '/home/NHERI-Published/PRJ-3712/GM_data/GM_flat' # noqa: N806
4242

4343
# print number of cites
4444
print(f'Number of sites: {len(gdf)}') # noqa: T201

0 commit comments

Comments
 (0)