Attachment 'PDS4_replaceStrings.py'
Download 1 #!/usr/bin/env python
2 import os, fnmatch, shutil
3
4 #method to backup files before running findReplace()
5 def findAndBackup(directory, filePattern):
6 for path, dirs, files in os.walk(os.path.abspath(directory)):
7 for filename in fnmatch.filter(files, filePattern):
8 filepath = os.path.join(path, filename)
9 backup = filepath + ".bak"
10 shutil.copy (filepath, backup)
11 print (filepath + " => " + backup)
12
13 #send two arrays for string find and replace
14 def findReplace(directory, find_array, replace_array, filePattern):
15 for path, dirs, files in os.walk(os.path.abspath(directory)):
16 for filename in fnmatch.filter(files, filePattern):
17 filepath = os.path.join(path, filename)
18 with open(filepath) as f:
19 s = f.read()
20 for i in range(len(find_array)):
21 s = s.replace(find_array[i], replace_array[i])
22 #replace XFILENAMEX if found
23 file_noExt = os.path.basename(filename).split('.')[0]
24 file_noExt = file_noExt.lower()
25 s = s.replace("XFILENAMEX",file_noExt)
26 with open(filepath, "w") as f:
27 f.write(s)
28
29 def main():
30
31 #optional to run. Best to backup or zip whole directory though
32 # findAndBackup(".","*.xml")
33
34 #initialize empty arrays
35 in_str=[]
36 re_str=[]
37
38 #update unique logical identifier
39 #urn:nasa:pds:body_mission_instrument_type_author_year:data:vevft201_lot
40 #Note the XFILENAMEX will get replaced above in recursive loop with the filename (no extension)
41 in_str.append("""${LOGICAL_IDENTIFIER}""")
42 re_str.append("""urn:nasa:pds:lunar_lro_lroc_topography_domingue_2022:data:XFILENAMEX""")
43
44 #update investigation area
45 in_str.append("""${INVESTIGATION_AREA_NAME}""")
46 re_str.append("""Lunar Reconnaissance Orbiter""")
47
48 #update investigation area lid reference
49 in_str.append("""${INVESTIGATION_AREA_LID_REFERENCE}""")
50 re_str.append("""urn:nasa:pds:context:instrument_host:spacecraft.lro""")
51
52 #update Observing_System_Component. Also currently GDAL defaults to "spacecraft"
53 # but I think "instrument" would be better here and matches the observing system name used.
54 in_str.append("""${OBSERVING_SYSTEM_NAME}""")
55 re_str.append("""Lunar Reconnaissance Orbiter Camera""")
56 # part 2 of this replacement
57 in_str.append("""<type>Spacecraft</type>""")
58 re_str.append("""<type>Instrument</type>""")
59
60 #update several sections, lower and upper case Moon_2000, ${TARGET_TYPE}, #${target_type}
61
62 #update TARGET_TYPE and lowercase target_type (annoying their is a lowercase version).
63 in_str.append("""Moon_2000""")
64 re_str.append("""Moon""")
65 #part 2 of the replacement
66 in_str.append("""${TARGET_TYPE}""")
67 re_str.append("""Satellite""")
68 #part 3 of the replacement, lower case
69 in_str.append("""moon_2000""")
70 re_str.append("""earth.moon""")
71 # part 4 lower case
72 in_str.append("""${target_type}""")
73 re_str.append("""satellite""")
74
75 #actually run the findReplace fundtion passing the arrays
76 findReplace(".",in_str,re_str,"*.xml")
77
78 if __name__ == "__main__":
79 main()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.