@@ -22,7 +22,7 @@ export type EpicParams = z.infer<typeof epicParamsSchema>;
22
22
* @param collection The collection type (natural or enhanced)
23
23
* @returns Formatted results with summary and image data
24
24
*/
25
- function processEpicResults ( epicData : any [ ] , collection : string ) {
25
+ async function processEpicResults ( epicData : any [ ] , collection : string ) {
26
26
if ( ! Array . isArray ( epicData ) || epicData . length === 0 ) {
27
27
return {
28
28
summary : "No EPIC data available for the specified parameters." ,
@@ -39,37 +39,79 @@ function processEpicResults(epicData: any[], collection: string) {
39
39
const [ year , month , day ] = dateStr . split ( '-' ) ;
40
40
41
41
// 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 ) {
43
45
// Construct the image URL according to NASA's format
44
46
const imageUrl = `${ EPIC_IMAGE_BASE_URL } /${ collection } /${ year } /${ month } /${ day } /png/${ img . image } .png` ;
45
47
46
48
// Create a unique resource URI for this image
47
49
const resourceUri = `nasa://epic/image/${ collection } /${ img . identifier } ` ;
48
50
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 ,
56
81
caption : img . caption || "Earth view from DSCOVR satellite" ,
57
82
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
+ }
73
115
74
116
return {
75
117
summary : `EPIC Earth imagery from ${ date } - Collection: ${ collection } - ${ images . length } images available` ,
@@ -100,7 +142,7 @@ export async function nasaEpicHandler(params: EpicParams) {
100
142
101
143
// Process the results
102
144
if ( epicData && epicData . length > 0 ) {
103
- const results = processEpicResults ( epicData , collection ) ;
145
+ const results = await processEpicResults ( epicData , collection ) ;
104
146
105
147
return {
106
148
content : [
0 commit comments