@@ -64,24 +64,30 @@ export function jsonToPythonDict(data: unknown, indent: number = 0): string {
6464}
6565
6666/**
67- * Maps API endpoints to SlideRule Python client functions
67+ * Maps web client endpoints to SlideRule run() API names (for non-GEDI endpoints)
6868 */
69- const apiToFunctionMap : Record < string , { module : string ; func : string } > = {
70- atl06 : { module : 'icesat2' , func : 'atl06p' } ,
71- atl06p : { module : 'icesat2' , func : 'atl06p' } ,
72- atl06sp : { module : 'icesat2' , func : 'atl06sp' } ,
73- atl03x : { module : 'icesat2' , func : 'atl03sp' } ,
74- 'atl03x-surface' : { module : 'icesat2' , func : 'atl03sp' } ,
75- 'atl03x-phoreal' : { module : 'icesat2' , func : 'atl03sp' } ,
76- atl03sp : { module : 'icesat2' , func : 'atl03sp' } ,
77- atl03vp : { module : 'icesat2' , func : 'atl03vp' } ,
78- atl08 : { module : 'icesat2' , func : 'atl08p' } ,
79- atl08p : { module : 'icesat2' , func : 'atl08p' } ,
80- atl24x : { module : 'icesat2' , func : 'atl24x' } ,
81- atl13x : { module : 'icesat2' , func : 'atl13sp' } ,
82- gedi01bp : { module : 'gedi' , func : 'gedi01bp' } ,
83- gedi02ap : { module : 'gedi' , func : 'gedi02ap' } ,
84- gedi04ap : { module : 'gedi' , func : 'gedi04ap' }
69+ const endpointToRunName : Record < string , string > = {
70+ atl06 : 'atl06' ,
71+ atl06p : 'atl06' ,
72+ atl06sp : 'atl06' ,
73+ atl03x : 'atl03x' ,
74+ 'atl03x-surface' : 'atl03x' ,
75+ 'atl03x-phoreal' : 'atl03x' ,
76+ atl03sp : 'atl03x' ,
77+ atl03vp : 'atl03x' ,
78+ atl08 : 'atl08' ,
79+ atl08p : 'atl08' ,
80+ atl24x : 'atl24x' ,
81+ atl13x : 'atl13x'
82+ }
83+
84+ /**
85+ * Maps GEDI endpoints to their gedi module function names
86+ */
87+ const gediEndpointToFunc : Record < string , string > = {
88+ gedi01bp : 'gedi01bp' ,
89+ gedi02ap : 'gedi02ap' ,
90+ gedi04ap : 'gedi04ap'
8591}
8692
8793/**
@@ -107,24 +113,26 @@ function getOutputFormat(jsonData: unknown): string | null {
107113 */
108114export function generatePythonClientCode ( jsonData : unknown , endpoint : string ) : string {
109115 const normalizedEndpoint = endpoint . toLowerCase ( ) . trim ( )
110- const mapping = apiToFunctionMap [ normalizedEndpoint ] || {
111- module : 'icesat2' ,
112- func : normalizedEndpoint
113- }
114-
115116 const parmsDict = jsonToPythonDict ( jsonData , 0 )
116117
117- // Determine which modules to import
118- const modules = new Set < string > ( [ 'sliderule' ] )
119- modules . add ( mapping . module )
120-
121- const importModules = Array . from ( modules ) . join ( ', ' )
118+ // Check if this is a GEDI endpoint
119+ const gediFunc = gediEndpointToFunc [ normalizedEndpoint ]
120+ const isGedi = ! ! gediFunc
122121
123122 // Check if output format is specified (returns file path instead of GeoDataFrame)
124123 const outputFormat = getOutputFormat ( jsonData )
125124 const hasFileOutput =
126125 outputFormat === 'parquet' || outputFormat === 'geoparquet' || outputFormat === 'csv'
127126
127+ // Determine import and execute statements based on endpoint type
128+ const importStatement = isGedi
129+ ? 'from sliderule import sliderule, gedi'
130+ : 'from sliderule import sliderule'
131+
132+ const executeStatement = isGedi
133+ ? `gedi.${ gediFunc } (parms)`
134+ : `sliderule.run("${ endpointToRunName [ normalizedEndpoint ] || normalizedEndpoint } ", parms)`
135+
128136 if ( hasFileOutput ) {
129137 // When output format is specified, the API returns a file path
130138 const geopandasImport =
@@ -137,17 +145,17 @@ SlideRule request generated from web client
137145Endpoint: ${ endpoint }
138146"""
139147
140- from sliderule import ${ importModules }
148+ ${ importStatement }
141149${ geopandasImport }
142150
143- # Initialize SlideRule connection
151+ # Initialize SlideRule client
144152sliderule.init("slideruleearth.io")
145153
146154# Request parameters
147155parms = ${ parmsDict }
148156
149157# Execute request (returns file path when output format is specified)
150- output_file = ${ mapping . module } . ${ mapping . func } (parms)
158+ output_file = ${ executeStatement }
151159print(f"Data saved to: {output_file}")
152160
153161# Read the output file into a GeoDataFrame
@@ -165,16 +173,16 @@ SlideRule request generated from web client
165173Endpoint: ${ endpoint }
166174"""
167175
168- from sliderule import ${ importModules }
176+ ${ importStatement }
169177
170- # Initialize SlideRule connection
178+ # Initialize SlideRule client
171179sliderule.init("slideruleearth.io")
172180
173181# Request parameters
174182parms = ${ parmsDict }
175183
176184# Execute request
177- gdf = ${ mapping . module } . ${ mapping . func } (parms)
185+ gdf = ${ executeStatement }
178186
179187# Display results
180188print(f"Retrieved {len(gdf)} records")
0 commit comments