Solar Spectrum

I wrote some code to generate a solar spectrum chart from official data. Thought I’d share it with you …

ssi.sh:

# Zoe Phin
# 2020/12/20

require() {
    sudo apt-get install curl nco gnuplot
}

download() {
    curl -o ssi.nc https://www.ncei.noaa.gov/data/solar-spectral-irradiance/access/daily/ssi_v02r01_daily_s20190101_e20191231_c20200226.nc
}

yearavg() {
    ncks --trd -HC -v SSI ssi.nc | awk -F '[= ]' '
        { D[$4]+=$6 } END { for (d in D) printf "%.4f %11.9f\n", d/1000, D[d]/365*1000 }
    ' | sort -n | sed 1d | awk '
        BEGIN { pi=atan2(0,-1); c=299792458; h=6.62607015E-34; k=1.38106485E-23 
            r=695700000; d=149597870700 }
        function P(T,w) { return ((2*pi*h*c^2/(w/1e6)^5)/(exp(h*c/(k*T*(w/1e6)))-1))/1e6 }
        { printf "%s %.9f\n", $0, P(5772,$1)*(r/d)^2 }
    ' > ssi.csv
}

plot() {
    echo 'set term png size 740,370
        set xrange [0:2.5]
        set mxtics 5
        set grid xtics ytics
        set ylabel "Radiance (W/m²/μm)"
        set xlabel "Wavelength (μm)"

        plot "ssi.csv" u 1:2 t "Solar Spectrum" w l lw 1 lc rgb "orange",\
             "" u 1:3 t "5772K Blackbody" w l lw 1 lc rgb "black"
    ' | gnuplot > solar.png
}

Run it:

$ source ssi.sh
$ require
$ download
$ yearavg
$ plot

or

$ . ssi.sh; require && download && yearavg && plot

Data and image contained in ssi.csv and solar.png

Enjoy 🙂 and Happy Holidays! -Zoe

Update

I also made a version for nanometer wavelenths rather than microns …

ssi-nm.sh:

# Zoe Phin
# 2020/12/22

require() {
    sudo apt-get install curl nco gnuplot
}

download() {
    curl -o ssi.nc https://www.ncei.noaa.gov/data/solar-spectral-irradiance/access/daily/ssi_v02r01_daily_s20190101_e20191231_c20200226.nc
}

yearavg() {
    ncks --trd -HC -v SSI ssi.nc | awk -F '[= ]' '
        { D[$4]+=$6 } END { for (d in D) printf "%.4f %11.9f\n", d, D[d]/365 }
    ' | sort -n | sed 1d | awk '
        BEGIN { pi=atan2(0,-1); c=299792458; h=6.62607015E-34; k=1.38106485E-23 
            r=695700000; d=149597870700 }
        function P(T,w) { return ((2*pi*h*c^2/(w/1e9)^5)/(exp(h*c/(k*T*(w/1e9)))-1))/1e9 }
        { printf "%s %.9f\n", $0, P(5772,$1)*(r/d)^2 }
    ' > ssi.csv
}

plot() {
    echo 'set term png size 740,370
        set xrange [0:2500]
        set xtics 200; set mxtics 2
        set ytics 0.2; set mytics 2
        set format y "%.1f"
        set grid xtics ytics
        set ylabel "Radiance (W/m²/nm)"
        set xlabel "Wavelength (nm)"

        plot "ssi.csv" u 1:2 t "Solar Spectrum" w l lw 1 lc rgb "orange",\
             "" u 1:3 t "5772K Blackbody" w l lw 1 lc rgb "black"
    ' | gnuplot > ssi-nm.png
}

Run:

$ . ssi-nm.sh; require && download && yearavg && plot

Published by Zoe Phin

https://phzoe.com

5 thoughts on “Solar Spectrum

  1. You are gradually making me a convert. As soon as I finish restoring the OS on a suicidal Mac mini, I’ll be loading Linux/Freaks on a Dell i7 that suffered a debilitating drive stroke. I can no longer stand not getting in on terse, productive code that up and harvests useful data without so much as a by-your-leave. Many thanks–especially for the derivative of the population growth curve. THAT is a treasure! It takes some of the terror out of staring up an almost vertical slope and wondering whether it will flatten before or after the background radiation curve starts looking vertical in an ominously Malthusian sense.

    Liked by 2 people

        1. Wikipedia uses nanometers. 2nd chart matches it perfectly.
          First chart is in microns, both wavelength and radiance.

          Use my code to modify for your needs 🙂

          Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: