Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions web-client/src/components/SrReqParmsDisplayDlg.vue
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ watch([modelValue, parsedData], () => {
<Tabs v-model:value="activeTab">
<TabList>
<Tab value="0">JSON</Tab>
<Tab value="1">Python Snippet</Tab>
<Tab value="1">Python</Tab>
<Tab value="2">Lua</Tab>
<Tab value="3">Python Example</Tab>
<Tab value="3">Python Code</Tab>
</TabList>

<TabPanels>
Expand All @@ -231,12 +231,12 @@ watch([modelValue, parsedData], () => {
</div>
</TabPanel>

<!-- Python Snippet Tab -->
<!-- Python Tab -->
<TabPanel value="1">
<div class="tab-content">
<div class="button-row">
<Button
label="Copy Snippet"
label="Copy"
size="small"
icon="pi pi-copy"
@click="copyPythonSnippetToClipboard"
Expand Down
74 changes: 41 additions & 33 deletions web-client/src/utils/reqParmsConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,30 @@ export function jsonToPythonDict(data: unknown, indent: number = 0): string {
}

/**
* Maps API endpoints to SlideRule Python client functions
* Maps web client endpoints to SlideRule run() API names (for non-GEDI endpoints)
*/
const apiToFunctionMap: Record<string, { module: string; func: string }> = {
atl06: { module: 'icesat2', func: 'atl06p' },
atl06p: { module: 'icesat2', func: 'atl06p' },
atl06sp: { module: 'icesat2', func: 'atl06sp' },
atl03x: { module: 'icesat2', func: 'atl03sp' },
'atl03x-surface': { module: 'icesat2', func: 'atl03sp' },
'atl03x-phoreal': { module: 'icesat2', func: 'atl03sp' },
atl03sp: { module: 'icesat2', func: 'atl03sp' },
atl03vp: { module: 'icesat2', func: 'atl03vp' },
atl08: { module: 'icesat2', func: 'atl08p' },
atl08p: { module: 'icesat2', func: 'atl08p' },
atl24x: { module: 'icesat2', func: 'atl24x' },
atl13x: { module: 'icesat2', func: 'atl13sp' },
gedi01bp: { module: 'gedi', func: 'gedi01bp' },
gedi02ap: { module: 'gedi', func: 'gedi02ap' },
gedi04ap: { module: 'gedi', func: 'gedi04ap' }
const endpointToRunName: Record<string, string> = {
atl06: 'atl06',
atl06p: 'atl06',
atl06sp: 'atl06',
atl03x: 'atl03x',
'atl03x-surface': 'atl03x',
'atl03x-phoreal': 'atl03x',
atl03sp: 'atl03x',
atl03vp: 'atl03x',
atl08: 'atl08',
atl08p: 'atl08',
atl24x: 'atl24x',
atl13x: 'atl13x'
}

/**
* Maps GEDI endpoints to their gedi module function names
*/
const gediEndpointToFunc: Record<string, string> = {
gedi01bp: 'gedi01bp',
gedi02ap: 'gedi02ap',
gedi04ap: 'gedi04ap'
}

/**
Expand All @@ -107,24 +113,26 @@ function getOutputFormat(jsonData: unknown): string | null {
*/
export function generatePythonClientCode(jsonData: unknown, endpoint: string): string {
const normalizedEndpoint = endpoint.toLowerCase().trim()
const mapping = apiToFunctionMap[normalizedEndpoint] || {
module: 'icesat2',
func: normalizedEndpoint
}

const parmsDict = jsonToPythonDict(jsonData, 0)

// Determine which modules to import
const modules = new Set<string>(['sliderule'])
modules.add(mapping.module)

const importModules = Array.from(modules).join(', ')
// Check if this is a GEDI endpoint
const gediFunc = gediEndpointToFunc[normalizedEndpoint]
const isGedi = !!gediFunc

// Check if output format is specified (returns file path instead of GeoDataFrame)
const outputFormat = getOutputFormat(jsonData)
const hasFileOutput =
outputFormat === 'parquet' || outputFormat === 'geoparquet' || outputFormat === 'csv'

// Determine import and execute statements based on endpoint type
const importStatement = isGedi
? 'from sliderule import sliderule, gedi'
: 'from sliderule import sliderule'

const executeStatement = isGedi
? `gedi.${gediFunc}(parms)`
: `sliderule.run("${endpointToRunName[normalizedEndpoint] || normalizedEndpoint}", parms)`

if (hasFileOutput) {
// When output format is specified, the API returns a file path
const geopandasImport =
Expand All @@ -137,17 +145,17 @@ SlideRule request generated from web client
Endpoint: ${endpoint}
"""

from sliderule import ${importModules}
${importStatement}
${geopandasImport}

# Initialize SlideRule connection
# Initialize SlideRule client
sliderule.init("slideruleearth.io")

# Request parameters
parms = ${parmsDict}

# Execute request (returns file path when output format is specified)
output_file = ${mapping.module}.${mapping.func}(parms)
output_file = ${executeStatement}
print(f"Data saved to: {output_file}")

# Read the output file into a GeoDataFrame
Expand All @@ -165,16 +173,16 @@ SlideRule request generated from web client
Endpoint: ${endpoint}
"""

from sliderule import ${importModules}
${importStatement}

# Initialize SlideRule connection
# Initialize SlideRule client
sliderule.init("slideruleearth.io")

# Request parameters
parms = ${parmsDict}

# Execute request
gdf = ${mapping.module}.${mapping.func}(parms)
gdf = ${executeStatement}

# Display results
print(f"Retrieved {len(gdf)} records")
Expand Down