#string that the fortran code automatically uses. Not sure how to #change this at the moment. #initializing variable to store output file name string #will probably need to be incremented as model is run outputfile_iter = 0 outputfile = 'workrslt.{:0>3d}'.format(outputfile_iter) #need to write out outputfile_iter as a text file required by the #fortran code f = open('filenum.dat', 'w') f.write('{:0>3d}'.format(outputfile_iter)) f.close() #Evaluate model using fortran code to get synthetic observation interface.exec_rtmod() #Read result file into python syn_obs = observation.Synthetic(filename = outputfile) #below is testing ... remove when find_best_model.py is finished initial_csq = compare.calc_chisq(datacuts, syn_obs) print "Chi-square: {}".format(initial_csq) #diagnostic: real = observation.concat(datacuts) syn = syn_obs.concat(3) #remove temporary working model files
def calc_deriv(atm_model, synobs): """ Calculate derivatives (changes in synthetic observation) produced by changes introduced by adjusting the parameters. """ #determine number of parameters nparams = atm_model.nflags #create synthetic observation I/F data vector (i.e. 1 numpy array combining #all spectral channels) syn_vec = synobs.concat(synobs.nangles) #initialize derivative array deriv_array = np.array([]) #create copy of input model for testing tmp_model = copy.deepcopy(atm_model) epsilon = 0.01 #parse input atmospheric model for flags, change parameter one at a time #for each atmospheric layer for i in range(tmp_model.nlayers): #first, extract said layer l = getattr(tmp_model, 'l'+str(i+1)) #parse layer for flags #determine index locations flaglocs = [a for a in range(len(l.flaglist)) \ if type(l.flaglist[a]) == str] #source #http://stackoverflow.com/questions/7270321/finding-the-index-of-elements-based-on-a-condition-using-python-list-comprehensi for j in flaglocs: #when encountering flag: #determine old value old_param_val = getattr(l, l.param_names[j]) #adjust parameter by epsilon #note, this is currently ALWAYS a positive change #since this is only calculating derivative, not adjusting the model towards #a best-fit. l.adj_param_epsilon(l.param_names[j]) #determine change in parameter new_param_val = getattr(l, l.param_names[j]) diff_param = new_param_val - old_param_val #replace modified layer in atmosphere object # i = layer number # l = new layer object tmp_model.replace_layer(i+1, l) #run model with changed parameter #set up temporary files for output / RT model execution f = open('filenum.dat', 'w') f.write('{:0<3d}'.format(999)) #999 = code for tmp/fleeting file in radtran model f.close() #write out temp model for RT code tmp_model.write_ft_input(filename='workmodl999') #Evaluate model using fortran code to get synthetic observation interface.exec_rtmod() #Read result file into python tmp_syn = observation.Synthetic(filename = 'workrslt.999') #create tmp synthetic observation concatenated vector tmp_syn_vec = tmp_syn.concat(synobs.nangles) #determine change in I/F diff = (tmp_syn_vec - syn_vec)/(diff_param) #store in derivative array if deriv_array.size == 0: deriv_array = np.append(deriv_array, diff) else: deriv_array = np.vstack((deriv_array, diff)) #restore tmp_model to the old state tmp_model = copy.deepcopy(atm_model) #delete temporary working model os.system('rm -f workmodl999') return deriv_array