0

I'm a student dealing with this weird old program that takes only files as input and outputs directly to terminal.

I have a python script, that makes this input file, and writes the output to a text file. However, on each run I have to change the variables manually in the python script myself. There are five instances of variables, that are all in a constant format. Is there a waY I can loop/array this?

My python script :

from subprocess import check_output

#time is in UTC format, ie. IST -5.30 hrs
with open("INPUT", 'w') as f_in:                   #INPUT is the input file
    f_in.write("&INPUT\n")
    f_in.write("WLINF = 0.250\n")               #lower frequency value
    f_in.write("WLSUP = 4.0\n")                 #highest frequency value
    f_in.write("WLINC = 0.5\n")                     #wavelength increment
    f_in.write("IDAY = 289\n")                  #computing for a specific day
    f_in.write("ALAT = 23.0329\n")          #latitude of the location
    f_in.write("ALON = 72.5517\n")          #longitude of the location
    f_in.write("IDATM = 3\n")                   #atmopsheric model 2 - mid latitude summer
    f_in.write("ISALB = 5\n")                         #surface albedo feature
    f_in.write("IAER = 5\n")                          #boundary layer aerosol type selection - 5 - user defined spectral dependance of BLA
    f_in.write("WLBAER = .500,.675,.870,.936,1.02\n") #wavelenght points for IAER
    f_in.write("WBAER = 5*0.9\n")                      #single scattering albedo
    f_in.write("GBAER = 5*0.8\n")                      #assymetric factor used with IAER
    f_in.write("TIME = 3\n")                       #Time in IST format (-5.30hr)
    f_in.write("QBAER = 0.553,0.258,0.344,0.31,0.276\n") #extinction efficiency percentage
    f_in.write("ZOUT = 0.0,15.0\n")                         #TOA defining
    f_in.write("/\n")

check_output('slarrt >> output1.csv',shell=True)  #slarrt is the program, and ouytput.csv is the output file

This script

Takes these values that need to be input, writes them to the input file, runs the other program(that takes the inputs from input file), and writes the output to a csv file. then i change the values in the script.

The variables ALAT, ALON, and QBAER and TIME need to be changed on each run, and the values are in an excel sheet. Is there anyway I can do them in one shot? SO FAR, I have been editing each line manually.

Hre is what the data looks like

https://i.stack.imgur.com/YKcb9.jpg

wwii
  • 23,232
  • 7
  • 37
  • 77
samhain
  • 11
  • 3
  • The field values in your code example does not match any of the example data. How are `QBAER` and `Time` derived from the data? – wwii Jun 24 '18 at 14:03
  • Please [don't post images of code/data/Tracebacks](https://meta.stackoverflow.com/a/285557/2823755). Just copy the text, paste it in your question and format it as code. – wwii Jun 24 '18 at 14:12
  • Hey, so heres the finalish thing?, but it doesn't input any of the files into the INPUT, it just looks TIME = QBAER = ,,,, – samhain Jun 24 '18 at 15:43
  • Thank you so much for your help, I've posted a new question here https://stackoverflow.com/questions/51011742/python-program-doesnt-write-to-output-csv-everything-else-seems-to-work-correc – samhain Jun 24 '18 at 16:17

1 Answers1

0

Export the spreadsheet to a csv file; use a csv reader to iterate over the rows; use the row values to update your variables.

Assuming the csv has column headers and QBAER is just the AOT xxxx fields concantenated:

import csv, operator
extinction_pct = operator.itemgetter('AOT 500','AOT 675','AOT 870','AOT 936','AOT 1020')

with open('csv_export.csv') as f_csv, open("INPUT", 'w') as f_in:
    reader = csv.DictReader(f_csv)
    for row in reader:
        f_in.write("&INPUT\n")
        f_in.write("WLINF = 0.250\n")               #lower frequency value
        f_in.write("WLSUP = 4.0\n")                 #highest frequency value
        f_in.write("WLINC = 0.5\n")                     #wavelength increment
        f_in.write("IDAY = 289\n")                  #computing for a specific day
        #f_in.write("ALAT = {Lat}\n".format(**row))    # for Python versions less than 3.6
        f_in.write(f"ALAT = {row['Lat']}\n")          #latitude of the location
        #f_in.write("ALON = {Long}\n".format(**row))    # for Python versions less than 3.6
        f_in.write(f"ALON = {row['Long']}\n")          #longitude of the location
        f_in.write("IDATM = 3\n")                   #atmopsheric model 2 - mid latitude summer
        f_in.write("ISALB = 5\n")                         #surface albedo feature
        f_in.write("IAER = 5\n")                          #boundary layer aerosol type selection - 5 - user defined spectral dependance of BLA
        f_in.write("WLBAER = .500,.675,.870,.936,1.02\n") #wavelenght points for IAER
        f_in.write("WBAER = 5*0.9\n")                      #single scattering albedo
        f_in.write("GBAER = 5*0.8\n")                      #assymetric factor used with IAER
        #f_in.write("TIME = {Time]}\n".format(**row))    # for Python versions less than 3.6
        f_in.write(f"TIME = {row['Time']}\n")                       #Time in IST format (-5.30hr)
        #f_in.write("QBAER = {}\n".format(','.join(extinction_pct(row)))    # for Python versions less than 3.6
        f_in.write(f"QBAER = {','.join(extinction_pct(row))}\n") #extinction efficiency percentage
        f_in.write("ZOUT = 0.0,15.0\n")                         #TOA defining
        f_in.write("/\n")

Uses f-strings which requires Python verion 3.6+.
It isn't clear from the question how the Time and QBAER fields are derived from the data.
There are packages that will work with the excel files directly but the process is the same.


If you want to create a new file for each row of data and check it, move the file creation inside the reader loop. To make things a bit more readable, I've put the text construction into a function.

def file_text(row):
    # String concatenation isn't the most efficient but it does preserve the comment annotations
    s = ''
    s += "&INPUT\n"
    s += "WLINF = 0.250\n"                             #lower frequency value
    s += "WLSUP = 4.0\n"                               #highest frequency value
    s += "WLINC = 0.5\n"                               #wavelength increment
    s += "IDAY = 289\n"                                #computing for a specific day
    #s += "ALAT = {Lat}\n".format(**row)               # for Python versions less than 3.6
    s += f"ALAT = {row['Lat']}\n"                      #latitude of the location
    #s += "ALON = {Long}\n".format(**row)              # for Python versions less than 3.6
    s += f"ALON = {row['Long']}\n"                     #longitude of the location
    s += "IDATM = 3\n"                                 #atmopsheric model 2 - mid latitude summer
    s += "ISALB = 5\n"                                 #surface albedo feature
    s += "IAER = 5\n"                                  #boundary layer aerosol type selection - 5 - user defined spectral dependance of BLA
    s += "WLBAER = .500,.675,.870,.936,1.02\n"         #wavelenght points for IAER
    s += "WBAER = 5*0.9\n"                             #single scattering albedo
    s += "GBAER = 5*0.8\n"                             #assymetric factor used with IAER
    #s += "TIME = {Time]}\n".format(**row)             # for Python versions less than 3.6
    s += f"TIME = {row['Time']}\n"                     #Time in IST format (-5.30hr)
    #s += "QBAER = {}\n".format(','.join(extinction_pct(row)))    # for Python versions less than 3.6
    s += f"QBAER = {','.join(extinction_pct(row))}\n"  #extinction efficiency percentage
    s += "ZOUT = 0.0,15.0\n"                           #TOA defining
    s += "/\n"
    return s

with open('csv_export.csv') as f_csv:
    reader = csv.DictReader(f_csv)
    for row in reader:
        with open("INPUT", 'w') as f_in:
            f_in.write(file_text(row))
        check_output('slarrt >> output1.csv',shell=True)  #slarrt is the program, and ouytput.csv is the output file
wwii
  • 23,232
  • 7
  • 37
  • 77
  • Thank you! I'm on python 2.7, I'm updating to python 3.6, and will update then Yes, the QBAER field is just the AOT field values seperated by commas. Time is simply the first decimal digit of the time field, I shall change those. – samhain Jun 24 '18 at 14:17
  • @samhain - See edit for Python v2.7 string formating. – wwii Jun 24 '18 at 14:35
  • THANK YOU SO MUCH!!! I updated to 3.6 and now it works, but it makes a huuge input file with every single input appended one after the other. How do I make it do each INput file, record the output, and then do the next? – samhain Jun 24 '18 at 15:05
  • @samhain - please edit your question to include all of your specifications, or ask another question for the new problem you are having. Please don't continually add specifications and morph the question. – wwii Jun 24 '18 at 15:59