@@ -22,7 +22,7 @@ export type EpicParams = z.infer<typeof epicParamsSchema>;
2222 * @param collection The collection type (natural or enhanced)
2323 * @returns Formatted results with summary and image data
2424 */
25- function processEpicResults ( epicData : any [ ] , collection : string ) {
25+ async function processEpicResults ( epicData : any [ ] , collection : string ) {
2626 if ( ! Array . isArray ( epicData ) || epicData . length === 0 ) {
2727 return {
2828 summary : "No EPIC data available for the specified parameters." ,
@@ -39,37 +39,79 @@ function processEpicResults(epicData: any[], collection: string) {
3939 const [ year , month , day ] = dateStr . split ( '-' ) ;
4040
4141 // Format each image and register it as a resource
42- const images = epicData . map ( img => {
42+ const images = [ ] ;
43+
44+ for ( const img of epicData ) {
4345 // Construct the image URL according to NASA's format
4446 const imageUrl = `${ EPIC_IMAGE_BASE_URL } /${ collection } /${ year } /${ month } /${ day } /png/${ img . image } .png` ;
4547
4648 // Create a unique resource URI for this image
4749 const resourceUri = `nasa://epic/image/${ collection } /${ img . identifier } ` ;
4850
49- // Register this image as a resource
50- addResource ( resourceUri , {
51- name : `NASA EPIC Earth Image - ${ img . identifier } ` ,
52- mimeType : "image/png" ,
53- text : JSON . stringify ( {
54- id : img . identifier ,
55- date : img . date ,
51+ try {
52+ // Fetch the actual image data
53+ const imageResponse = await axios ( {
54+ url : imageUrl ,
55+ responseType : 'arraybuffer' ,
56+ timeout : 30000
57+ } ) ;
58+
59+ // Register this image as a resource with binary data
60+ addResource ( resourceUri , {
61+ name : `NASA EPIC Earth Image - ${ img . identifier } ` ,
62+ mimeType : "image/png" ,
63+ // Store metadata as text
64+ text : JSON . stringify ( {
65+ id : img . identifier ,
66+ date : img . date ,
67+ caption : img . caption || "Earth view from DSCOVR satellite" ,
68+ imageUrl : imageUrl ,
69+ centroid_coordinates : img . centroid_coordinates ,
70+ dscovr_j2000_position : img . dscovr_j2000_position ,
71+ lunar_j2000_position : img . lunar_j2000_position ,
72+ sun_j2000_position : img . sun_j2000_position ,
73+ attitude_quaternions : img . attitude_quaternions
74+ } ) ,
75+ // Store actual image data as blob
76+ blob : Buffer . from ( imageResponse . data )
77+ } ) ;
78+
79+ images . push ( {
80+ identifier : img . identifier ,
5681 caption : img . caption || "Earth view from DSCOVR satellite" ,
5782 imageUrl : imageUrl ,
58- centroid_coordinates : img . centroid_coordinates ,
59- dscovr_j2000_position : img . dscovr_j2000_position ,
60- lunar_j2000_position : img . lunar_j2000_position ,
61- sun_j2000_position : img . sun_j2000_position ,
62- attitude_quaternions : img . attitude_quaternions
63- } )
64- } ) ;
65-
66- return {
67- identifier : img . identifier ,
68- caption : img . caption || "Earth view from DSCOVR satellite" ,
69- imageUrl : imageUrl ,
70- resourceUri : resourceUri
71- } ;
72- } ) ;
83+ resourceUri : resourceUri
84+ } ) ;
85+ } catch ( error ) {
86+ console . error ( `Error fetching EPIC image ${ img . identifier } :` , error ) ;
87+
88+ // If fetch fails, register with just the metadata
89+ addResource ( resourceUri , {
90+ name : `NASA EPIC Earth Image - ${ img . identifier } ` ,
91+ mimeType : "image/png" ,
92+ text : JSON . stringify ( {
93+ id : img . identifier ,
94+ date : img . date ,
95+ caption : img . caption || "Earth view from DSCOVR satellite" ,
96+ imageUrl : imageUrl ,
97+ centroid_coordinates : img . centroid_coordinates ,
98+ dscovr_j2000_position : img . dscovr_j2000_position ,
99+ lunar_j2000_position : img . lunar_j2000_position ,
100+ sun_j2000_position : img . sun_j2000_position ,
101+ attitude_quaternions : img . attitude_quaternions ,
102+ fetch_error : ( error as Error ) . message
103+ } )
104+ } ) ;
105+
106+ images . push ( {
107+ identifier : img . identifier ,
108+ caption : img . caption || "Earth view from DSCOVR satellite" ,
109+ imageUrl : imageUrl ,
110+ resourceUri : resourceUri ,
111+ error : "Failed to fetch image data"
112+ } ) ;
113+ }
114+ }
73115
74116 return {
75117 summary : `EPIC Earth imagery from ${ date } - Collection: ${ collection } - ${ images . length } images available` ,
@@ -100,7 +142,7 @@ export async function nasaEpicHandler(params: EpicParams) {
100142
101143 // Process the results
102144 if ( epicData && epicData . length > 0 ) {
103- const results = processEpicResults ( epicData , collection ) ;
145+ const results = await processEpicResults ( epicData , collection ) ;
104146
105147 return {
106148 content : [
0 commit comments