1- # %% # noqa: INP001, D100
1+ import os
22import json
3- import os
43import time
54from 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
0 commit comments