touchstone 0.12.1

Touchstone (s2p, etc.) file parser, plotter, and more
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use std::{fs, path::Path};

// The paths are relative to this .rs file
pub(crate) static PLOTLY_JS: &str = include_str!("assets/js/plotly-3.3.0.min.js");
// pub (crate) static PLOTLY_SRC_LINE: &str = "./js/plotly-3.3.0.min.js";
pub(crate) static TAILWIND_CSS: &str = include_str!("assets/js/tailwindcss-3.4.17.js");
// pub (crate) static TAILWIND_SRC_LINE: &str = "./js/tailwindcss-3.4.17.js";

pub(crate) fn get_plotly_js() -> &'static str {
    PLOTLY_JS
}

pub(crate) fn get_tailwind_css() -> &'static str {
    TAILWIND_CSS
}

pub(crate) fn write_plot_html(file_path: &str, html_content: &str) -> std::io::Result<()> {
    use std::fs::File;
    use std::io::prelude::*;
    use std::path::Path;

    let path = Path::new(file_path);

    // delete existing file
    if path.exists() {
        let _ = fs::remove_file(path);
    }

    // open file in write mode
    let mut file = File::create(path)?;
    file.write_all(html_content.as_bytes())?;
    Ok(())
}

pub fn generate_two_port_plot_html(
    output_path: &str,
    network_names: &[String],
    frequency_data: &[String],
    s11_data: &[String],
    s21_data: &[String],
    s12_data: &[String],
    s22_data: &[String],
) -> std::io::Result<()> {
    // this only works if a relative path or full path is given.
    // the unwrap fails if "ntwk1.s2p" is given instead of "./ntwk1.s2p"
    // this is handled befroby main.rs::get_file_path_config
    // Attempt to get parent; if None, default to "." (current dir)
    let folder_path = Path::new(output_path)
        .parent()
        .map(|p| {
            if p.as_os_str().is_empty() {
                Path::new(".")
            } else {
                p
            }
        })
        .unwrap_or(Path::new("."));
    std::fs::create_dir_all(folder_path)?;

    let mut html_content = include_str!("assets/template_2port.html").to_string();

    // Format arrays for JS injection
    let format_js_string_array = |arr: &[String]| -> String {
        let items: Vec<String> = arr.iter().map(|s| format!("'{}'", s)).collect();
        format!("[{}]", items.join(", "))
    };

    let format_js_data_array = |arr: &[String]| -> String { format!("[{}]", arr.join(", ")) };

    html_content = html_content.replace(
        "{{ network_names }}",
        &format_js_string_array(network_names),
    );
    html_content = html_content.replace(
        "{{ frequency_data }}",
        &format_js_data_array(frequency_data),
    );
    html_content = html_content.replace("{{ s11_data }}", &format_js_data_array(s11_data));
    html_content = html_content.replace("{{ s21_data }}", &format_js_data_array(s21_data));
    html_content = html_content.replace("{{ s12_data }}", &format_js_data_array(s12_data));
    html_content = html_content.replace("{{ s22_data }}", &format_js_data_array(s22_data));

    write_plot_html(output_path, &html_content)?;

    let js_assets_path = format!(
        "{}/js",
        std::path::Path::new(output_path)
            .parent()
            .unwrap()
            .to_str()
            .unwrap()
    );
    std::fs::create_dir_all(&js_assets_path)?;
    let plotly_js_path = format!("{}/plotly-3.3.0.min.js", js_assets_path);
    let tailwind_js_path = format!("{}/tailwindcss-3.4.17.js", js_assets_path);
    std::fs::write(plotly_js_path, get_plotly_js())?;
    std::fs::write(tailwind_js_path, get_tailwind_css())?;
    Ok(())
}

pub fn generate_one_port_plot_html(
    output_path: &str,
    network_names: &[String],
    frequency_data: &[String],
    s11_data: &[String],
) -> std::io::Result<()> {
    let folder_path = Path::new(output_path)
        .parent()
        .map(|p| {
            if p.as_os_str().is_empty() {
                Path::new(".")
            } else {
                p
            }
        })
        .unwrap_or(Path::new("."));
    std::fs::create_dir_all(folder_path)?;

    let mut html_content = include_str!("assets/template_1port.html").to_string();

    // Format arrays for JS injection
    let format_js_string_array = |arr: &[String]| -> String {
        let items: Vec<String> = arr.iter().map(|s| format!("'{}'", s)).collect();
        format!("[{}]", items.join(", "))
    };

    let format_js_data_array = |arr: &[String]| -> String { format!("[{}]", arr.join(", ")) };

    html_content = html_content.replace(
        "{{ network_names }}",
        &format_js_string_array(network_names),
    );
    html_content = html_content.replace(
        "{{ frequency_data }}",
        &format_js_data_array(frequency_data),
    );
    html_content = html_content.replace("{{ s11_data }}", &format_js_data_array(s11_data));

    write_plot_html(output_path, &html_content)?;

    let js_assets_path = format!(
        "{}/js",
        std::path::Path::new(output_path)
            .parent()
            .unwrap()
            .to_str()
            .unwrap()
    );
    std::fs::create_dir_all(&js_assets_path)?;
    let plotly_js_path = format!("{}/plotly-3.3.0.min.js", js_assets_path);
    let tailwind_js_path = format!("{}/tailwindcss-3.4.17.js", js_assets_path);
    std::fs::write(plotly_js_path, get_plotly_js())?;
    std::fs::write(tailwind_js_path, get_tailwind_css())?;
    Ok(())
}

pub fn generate_plot_from_networks(
    networks: &[crate::Network],
    output_path: &str,
) -> std::io::Result<()> {
    // Check if all networks have the same rank
    if networks.is_empty() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "No networks provided for plotting",
        ));
    }

    let rank = networks[0].rank;
    tracing::debug!(
        num_networks = networks.len(),
        rank,
        output_path,
        "Generating plot"
    );

    // Verify all networks have the same rank
    for network in networks {
        if network.rank != rank {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                format!(
                    "All networks must have the same rank. Found {} and {}",
                    rank, network.rank
                ),
            ));
        }
    }

    // Handle different ranks
    match rank {
        1 => {
            // 1-port network plotting
            let mut network_names = Vec::new();
            let mut frequency_data_list = Vec::new();
            let mut s11_data_list = Vec::new();

            for network in networks {
                network_names.push(network.name.clone());

                let freq = network
                    .f
                    .iter()
                    .map(|f| f.to_string())
                    .collect::<Vec<String>>()
                    .join(", ");
                frequency_data_list.push(format!("[{}]", freq));

                let s11 = network
                    .s_db(1, 1)
                    .iter()
                    .map(|s| s.s_db.decibel().to_string())
                    .collect::<Vec<String>>()
                    .join(", ");
                s11_data_list.push(format!("[{}]", s11));
            }

            generate_one_port_plot_html(
                output_path,
                &network_names,
                &frequency_data_list,
                &s11_data_list,
            )
        }
        2 => {
            // 2-port network plotting (existing code)
            let mut network_names = Vec::new();
            let mut frequency_data_list = Vec::new();
            let mut s11_data_list = Vec::new();
            let mut s21_data_list = Vec::new();
            let mut s12_data_list = Vec::new();
            let mut s22_data_list = Vec::new();

            for network in networks {
                network_names.push(network.name.clone());

                let freq = network
                    .f
                    .iter()
                    .map(|f| f.to_string())
                    .collect::<Vec<String>>()
                    .join(", ");
                frequency_data_list.push(format!("[{}]", freq));

                let s11 = network
                    .s_db(1, 1)
                    .iter()
                    .map(|s| s.s_db.decibel().to_string())
                    .collect::<Vec<String>>()
                    .join(", ");
                s11_data_list.push(format!("[{}]", s11));

                let s21 = network
                    .s_db(2, 1)
                    .iter()
                    .map(|s| s.s_db.decibel().to_string())
                    .collect::<Vec<String>>()
                    .join(", ");
                s21_data_list.push(format!("[{}]", s21));

                let s12 = network
                    .s_db(1, 2)
                    .iter()
                    .map(|s| s.s_db.decibel().to_string())
                    .collect::<Vec<String>>()
                    .join(", ");
                s12_data_list.push(format!("[{}]", s12));

                let s22 = network
                    .s_db(2, 2)
                    .iter()
                    .map(|s| s.s_db.decibel().to_string())
                    .collect::<Vec<String>>()
                    .join(", ");
                s22_data_list.push(format!("[{}]", s22));
            }

            generate_two_port_plot_html(
                output_path,
                &network_names,
                &frequency_data_list,
                &s11_data_list,
                &s21_data_list,
                &s12_data_list,
                &s22_data_list,
            )
        }
        _ => {
            // N-port where N > 2: Not yet supported for plotting
            Err(std::io::Error::new(
                std::io::ErrorKind::Unsupported,
                format!(
                    "Plotting for {}-port networks is not yet supported. \
                     Currently only 1-port and 2-port networks can be plotted. \
                     For {}-port networks, you can still parse and access S-parameters programmatically, \
                     but interactive HTML plots are not available yet.",
                    rank, rank
                ),
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use crate::Network;

    use super::*;
    use std::path::PathBuf;

    fn setup_test_dir(name: &str) -> PathBuf {
        let mut path = std::env::temp_dir();
        path.push("touchstone_tests");
        path.push(name);
        path.push(format!(
            "{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&path).unwrap();
        path
    }

    #[test]
    fn test_get_plotly_js_not_empty() {
        let js = get_plotly_js();
        assert!(!js.is_empty());
        assert!(js.len() > 1000); // Plotly is a large library
    }

    #[test]
    fn test_get_tailwind_css_not_empty() {
        let css = get_tailwind_css();
        assert!(!css.is_empty());
        assert!(css.len() > 1000);
    }

    #[test]
    fn test_write_plot_html() {
        let test_dir = setup_test_dir("test_write_plot_html");
        let output_path = test_dir.join("output.html");
        let output_str = output_path.to_str().unwrap();
        let content = "<html><body>test</body></html>";

        write_plot_html(output_str, content).unwrap();

        let read_back = fs::read_to_string(&output_path).unwrap();
        assert_eq!(read_back, content);
    }

    #[test]
    fn test_generate_one_port_plot_html() {
        let test_dir = setup_test_dir("test_generate_one_port_plot_html");
        let s1p_path = test_dir.join("test.s1p");
        fs::copy("files/hfss_oneport.s1p", &s1p_path).unwrap();

        let network = Network::new(s1p_path.to_str().unwrap().to_string());
        let output_path = test_dir.join("oneport_plot.html");
        let output_str = output_path.to_str().unwrap().to_string();

        let freq_data = vec![network
            .f
            .iter()
            .map(|f| f.to_string())
            .collect::<Vec<String>>()
            .join(", ")];
        let freq_data = vec![format!("[{}]", freq_data[0])];
        let s11_data: Vec<String> = vec![format!(
            "[{}]",
            network
                .s_db(1, 1)
                .iter()
                .map(|s| s.s_db.decibel().to_string())
                .collect::<Vec<String>>()
                .join(", ")
        )];

        generate_one_port_plot_html(
            &output_str,
            &[network.name.clone()],
            &freq_data,
            &s11_data,
        )
        .unwrap();

        assert!(output_path.exists());
        let html = fs::read_to_string(&output_path).unwrap();
        assert!(html.contains("plotly"));
        assert!(test_dir.join("js").exists());
    }

    #[test]
    fn test_generate_plot_from_networks_empty() {
        let test_dir = setup_test_dir("test_empty_networks");
        let output_path = test_dir.join("empty.html");
        let result = generate_plot_from_networks(&[], output_path.to_str().unwrap());
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().kind(), std::io::ErrorKind::InvalidInput);
    }

    #[test]
    fn test_generate_plot_from_networks_rank_mismatch() {
        let n1 = Network::new("files/hfss_oneport.s1p".to_string());
        let n2 = Network::new("files/ntwk1.s2p".to_string());
        let test_dir = setup_test_dir("test_rank_mismatch");
        let output_path = test_dir.join("mismatch.html");
        let result = generate_plot_from_networks(&[n1, n2], output_path.to_str().unwrap());
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
        assert!(err.to_string().contains("same rank"));
    }

    #[test]
    fn test_generate_plot_from_networks_one_port() {
        let network = Network::new("files/hfss_oneport.s1p".to_string());
        let test_dir = setup_test_dir("test_plot_one_port");
        let output_path = test_dir.join("oneport.html");
        generate_plot_from_networks(&[network], output_path.to_str().unwrap()).unwrap();
        assert!(output_path.exists());
        assert!(test_dir.join("js").exists());
    }

    #[test]
    fn test_generate_plot_from_networks_multi_two_port() {
        let n1 = Network::new("files/ntwk1.s2p".to_string());
        let n2 = Network::new("files/ntwk2.s2p".to_string());
        let test_dir = setup_test_dir("test_plot_multi_two_port");
        let output_path = test_dir.join("overlay.html");
        generate_plot_from_networks(&[n1, n2], output_path.to_str().unwrap()).unwrap();
        assert!(output_path.exists());
        let html = fs::read_to_string(&output_path).unwrap();
        assert!(html.contains("ntwk1"));
        assert!(html.contains("ntwk2"));
    }

    #[test]
    fn test_generate_two_port_plot_html() {
        let test_dir = setup_test_dir("test_generate_two_port_plot_html");
        let s2p_path = test_dir.join("test_plot.s2p");
        fs::copy("files/test_plot/test_plot.s2p", &s2p_path).unwrap();

        let network = Network::new(s2p_path.to_str().unwrap().to_string());

        // network.name is derived from filename, so it will be "test_plot.s2p" (or similar depending on implementation)
        // Network::new uses parser::read_file which sets name.
        // If name is full path, this might be tricky.
        // Let's check Network::new implementation or parser.
        // Assuming name is just filename or derived from it.
        // But wait, output_path is constructed here.
        // If network.name is "test_plot.s2p", output_path is "test_plot.s2p.html".
        // But we want it in the test_dir.

        // Network::new takes a path.
        // parser::read_file probably sets name to filename.
        // Let's assume network.name is just the name.

        // We need to ensure output_path is in test_dir.
        // generate_plot_from_two_port_network takes output_path.
        // If output_path is relative, it puts it relative to CWD?
        // No, generate_two_port_plot_html uses output_path parent.

        // So we should construct output_path to be in test_dir.
        let output_path = s2p_path.with_extension("s2p.html");
        let output_path_str = output_path.to_str().unwrap().to_string();

        println!("{}", output_path_str);

        let output_path_as_path = Path::new(&output_path_str);

        // delete existing file
        if output_path_as_path.exists() {
            let _ = fs::remove_file(output_path_as_path);
        }

        let _ = generate_plot_from_networks(&[network], &output_path_str);

        assert!(std::path::Path::new(&output_path_str).exists());
        assert!(test_dir.join("js").exists());

        // clean up
        // let _remove_test_plot_file = fs::remove_file(output_path_as_path);
        // let _remove_tests_js_folder = fs::remove_dir_all(test_dir.join("js"));
    }
}