开发者问题收集

如何在运行 GPT 命令时将变量作为输入

2020-01-25
593

我想用 Python 中的 for 循环处理多张图片。

当我尝试对一个文件执行此操作时,它可以工作,例如:

!gpt "Method/gpt_scripts/s1_cal_deb.xml" -t "result_step1/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip/s1_cal_deb/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip_Cal_deb_script.dim" "./Rader Images/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"

但是我想对许多文件执行此操作,所以我在 Python 3 中测试了这一点。

deb = (f'"{"Method/gpt_scripts/s1_cal_deb.xml"}"') 
deb = deb + " -t "

url = (f'"{"result_step1/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"}')
url5 = (f'{"S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"}')       
url2 = (f'{"/s1_cal_deb/"}')
url3 = (f'{url5}')
url4 = (f'{"_Cal_deb_script.dim"}"')
source = (f'"{"./Rader Images/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"}"')

output = url + url2 + url3 + url4

final = deb + output + " " + source

final

输出:

'"Method/gpt_scripts/s1_cal_deb.xml" -t "result_step1/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip/s1_cal_deb/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip_Cal_deb_script.dim" "./Rader Images/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"'

最终字符串与第一个字符串完全相同。但我收到以下错误:

INFO: org.esa.snap.core.gpf.operators.tooladapter.ToolAdapterIO: Initializing external tool adapters
INFO: org.esa.snap.core.util.EngineVersionCheckActivator: Please check regularly for new updates for the best SNAP experience.
java.lang.IllegalStateException: Operator SPI not found for operator [final]
    at org.esa.snap.core.gpf.annotations.ParameterDescriptorFactory.getOpSpi(ParameterDescriptorFactory.java:262)
    at org.esa.snap.core.gpf.annotations.ParameterDescriptorFactory.createMapBackedOperatorPropertyContainer(ParameterDescriptorFactory.java:67)
    at org.esa.snap.core.gpf.main.CommandLineTool.convertParameterMap(CommandLineTool.java:400)
    at org.esa.snap.core.gpf.main.CommandLineTool.runOperator(CommandLineTool.java:296)
    at org.esa.snap.core.gpf.main.CommandLineTool.runGraphOrOperator(CommandLineTool.java:284)
    at org.esa.snap.core.gpf.main.CommandLineTool.run(CommandLineTool.java:188)
    at org.esa.snap.core.gpf.main.CommandLineTool.run(CommandLineTool.java:121)
    at org.esa.snap.core.gpf.main.GPT.run(GPT.java:54)
    at org.esa.snap.core.gpf.main.GPT.main(GPT.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.esa.snap.runtime.Launcher.lambda$run$0(Launcher.java:55)
    at org.esa.snap.runtime.Engine.runClientCode(Engine.java:189)
    at org.esa.snap.runtime.Launcher.run(Launcher.java:51)
    at org.esa.snap.runtime.Launcher.main(Launcher.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.exe4j.runtime.LauncherEngine.launch(LauncherEngine.java:65)
    at com.exe4j.runtime.WinLauncher.main(WinLauncher.java:101)
    at com.install4j.runtime.launcher.WinLauncher.main(WinLauncher.java:26)

Error: Operator SPI not found for operator [final]

我在这里遗漏了一些非常明显的东西?我对 Python 中的 F 字符串还不熟悉,因此非常感谢您的帮助。

一些示例:

deb = (f'"{"Method/gpt_scripts/s1_cal_deb.xml"}"') # This is the script to run
    deb = deb + " -t " 

url = (f'"{"result_step1/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"}') # output folder
url5 = (f'{"S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"}')  # input file      
url2 = (f'{"/s1_cal_deb/"}') # output folder
url3 = (f'{url5}') # filename
url4 = (f'{"_Cal_deb_script.dim"}"') # output file extention
source = (f'"{"./Rader Images/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"}"') # input file 

output = url + url2 + url3 + url4

final = deb + output + " " + source

final

这将创建以下字符串:

输出:

'"Method/gpt_scripts/s1_cal_deb.xml" -t "result_step1/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip/s1_cal_deb/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip_Cal_deb_script.dim" "./Rader Images/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"'

但我想要以下字符串:

 "Method/gpt_scripts/s1_cal_deb.xml" -t "result_step1/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip/s1_cal_deb/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip_Cal_deb_script.dim" "./Rader Images/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"

我想要转换:

'"Method/gpt... to "Method/gpt... on both ends of the string

然后我可以按如下方式运行 !gpt 命令:

!gpt final

我希望这更清楚。

非常感谢,

Ishack

2个回答

我已设法在 Python 中找到解决方案。

#example command

command = 'cmd /k "gpt Method/gpt_scripts/s1_cal_deb.xml -t ../s1_pc/s1/deb/unit/S1A_IW_SLC__1SDV_20190224T003144_20190224T003211_026064_02E80B_0ECE_cal_deb.dim ../s1_pc/s1/s1_slc/unit1/S1A_IW_SLC__1SDV_20190224T003144_20190224T003211_026064_02E80B_0ECE.zip"'

os.system(command) # Start GPT command in the Jupyter Notebook cmd
Ishack Marshook
2020-02-03

这不是一个完整的答案,因为正如您将在下面看到的,问题可能出在 gpt 上(我对此不太熟悉)。但在试图弄清楚的过程中,我发现了一些可能对您有帮助的东西。 首先,您的代码不必要地复杂,并且 f 字符串(在这种情况下)不执行任何操作,只会使代码混乱。我会像这样重写它:

deb = '"Method/gpt_scripts/s1_cal_deb.xml"' + " -t " 
url = "result_step1/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"
url2 = "/s1_cal_deb/"
url3 = "S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"
url4 = "_Cal_deb_script.dim" 

source = "./Rader Images/S1A_IW_SLC__1SDV_20190928T003154_20190928T003221_029214_035175_65FA.zip"
output = '"'+url+url2+url3+url4+'"'
final = deb + output + " " + '"'+source+'"'

这将使您得到与问题中相同的 final 。 至于字符串的问题:这是 python 的工作方式:

hi = 'hi there!'
print(hi) #using the print method
hi #without using the print method

输出:

hi there!   
'hi there!'

您注意到了差异吗?单引号实际上不是字符串的一部分,只是当您不发出打印命令时屏幕输出的一部分。这就是我说这不是一个完整答案的原因:我不知道如何强制 gpt 接受字符串本身作为参数。您可能希望将该特定问题(使用简化的“hi”示例)作为单独的问题发布。可能(很可能)比我聪明的人可以解决这个问题。

抱歉最后一部分;希望这对您有用....

Jack Fleeting
2020-01-25