Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Editing Fortran .ini configuration files from Python by José Fonseca

Pycon ZA
October 05, 2017

Editing Fortran .ini configuration files from Python by José Fonseca

A Lightning talk at PyConZA 2017

Pycon ZA

October 05, 2017
Tweet

More Decks by Pycon ZA

Other Decks in Programming

Transcript

  1. Lightning talk 5 Oct October 8, 2017 1 Editing Fortran

    .ini configuration files from Python 1.1 Jos´ e Fonseca 1.1.1 University of the Western Cape 1.1.2 5th Oct 2017 @PyConZA 1.1.3 slack zefonseca In [1]: #What I need to import import os from configobj import ConfigObj import numpy as np import matplotlib.pyplot as plt %matplotlib inline As a cosmologist I use CAMB sources - a code written in fortran - to make my Fisher Forecasts (fancy name to access performance of experiments). But that means I need to run the code with one sligth change dozens of hundreds of times. By hand it can be a mission. So really nice that configobj exists (and os as well). configobj is a parser that imports the configuration file and transforms all id=something from the configuration file into a dictionary with id as keys and something as ‘value’. In [2]: ### Example original=’template.ini’ filename=’PyConZA_example.ini’ os.system(’cp ’+original+’ ’+filename) inifile=ConfigObj(filename) In [3]: inifile.keys() Out[3]: [’output root’, ’get scalar cls’, ’get vector cls’, ’get tensor cls’, ’get transfer’, ’do lensing’, ’do nonlinear’, ’l max scalar’, ’l max tensor’, ’k eta max tensor’, ’use physical’, ’hubble’, 1
  2. ’fNL’, ’normtrans’, ’eps WL’, ’eps GR’, ’eps IM’, ’eps gal’,

    ’w’, ’cs2 lam’, ’omega baryon’, ’omega cdm’, ’omega lambda’, ’omega neutrino’, ’temp cmb’, ’helium fraction’, ’massless neutrinos’, ’nu mass eigenstates’, ’massive neutrinos’, ’share delta neff’, ’nu mass fractions’, ’nu mass degeneracies’, ’initial power num’, ’pivot scalar’, ’pivot tensor’, ’scalar amp(1)’, ’scalar spectral index(1)’, ’scalar nrun(1)’, ’scalar nrunrun(1)’, ’tensor spectral index(1)’, ’tensor nrun(1)’, ’tensor parameterization’, ’initial ratio(1)’, ’reionization’, ’re use optical depth’, ’re optical depth’, ’re redshift’, ’re delta redshift’, ’re ionization frac’, ’re helium redshift’, ’re helium delta redshift’, ’RECFAST fudge’, ’RECFAST fudge He’, ’RECFAST Heswitch’, ’RECFAST Hswitch’, ’initial condition’, ’initial vector’, ’vector mode’, ’COBE normalize’, ’CMB outputscale’, ’transfer high precision’, ’transfer kmax’, ’transfer k per logint’, ’transfer num redshifts’, ’transfer interp matterpower’, ’transfer redshift(1)’, 2
  3. ’transfer filename(1)’, ’transfer matterpower(1)’, ’transfer power var’, ’scalar output file’,

    ’vector output file’, ’tensor output file’, ’total output file’, ’lensed output file’, ’lensed total output file’, ’lens potential output file’, ’FITS filename’, ’do lensing bispectrum’, ’do primordial bispectrum’, ’bispectrum nfields’, ’bispectrum slice base L’, ’bispectrum ndelta’, ’bispectrum delta(1)’, ’bispectrum delta(2)’, ’bispectrum delta(3)’, ’bispectrum do fisher’, ’bispectrum fisher noise’, ’bispectrum fisher noise pol’, ’bispectrum fisher fwhm arcmin’, ’bispectrum full output file’, ’bispectrum full output sparse’, ’bispectrum export alpha beta’, ’feedback level’, ’output file headers’, ’derived parameters’, ’lensing method’, ’accurate BB’, ’massive nu approx’, ’accurate polarization’, ’accurate reionization’, ’do tensor neutrinos’, ’do late rad truncation’, ’halofit version’, ’number of threads’, ’high accuracy default’, ’accuracy boost’, ’l accuracy boost’, ’l sample boost’, ’want CMB’, ’want CMB lensing’, ’limber windows’, ’use mK’, ’line basic’, ’line distortions’, ’line extra’, ’line phot quadrupole’, ’line phot dipole’, ’line reionization’, ’DoRedshiftLensing’, ’counts density’, 3
  4. ’counts redshift’, ’counts radial’, ’counts timedelay’, ’counts ISW’, ’counts velocity’,

    ’counts potential’, ’counts evolve’, ’evolve delta xe’, ’Do21cm’, ’transfer 21cm cl’, ’num redshiftwindows’, ’redshift(1)’, ’redshift sigma(1)’, ’redshift kind(1)’, ’redshift wintype(1)’, ’redshift smooth(1)’, ’redshift bias(1)’, ’redshift dlog10Ndm(1)’, ’redshift dNdz(1)’] In [4]: new_folder=’pyconza’ root=’example’ inifile[’output_root’]=new_folder+’/’+root os.system(’mkdir ’+new_folder) inifile[’get_scalar_cls’]=’T’ inifile[’num_redshiftwindows’] = 1 inifile[’redshift(1)’] = 1.0 inifile[’redshift_sigma(1)’] = 0.1 inifile[’redshift_kind(1)’] = ’counts’ inifile[’redshift_wintype(1)’] = ’gaussian’ inifile[’redshift_bias(1)’] = 1.5 inifile[’redshift_dlog10Ndm(1)’] = 0.6 inifile.write() In [5]: os.system(’./camb ’+filename) cl_raw=np.loadtxt(new_folder+’/’+root+’_scalCovCls.dat’)[:,-1] l=np.loadtxt(new_folder+’/’+root+’_scalCovCls.dat’)[:,0] cl=cl_raw*2*np.pi/(l*(l+1)) In [6]: plt.style.use(’ggplot’) plt.plot(l,cl,lw=3) plt.xlabel(r’$\ell$’) plt.ylabel(r’$C_\ell$’) plt.yscale(’log’) plt.show() 4
  5. In [7]: w=[-1.3,-1.15,-1,-0.85,-0.7] cores=’kbrgy’ for i in range(len(w)): w_name=str(w[i]) inifile[’output_root’]=new_folder+’/’+root+’_’+w_name

    inifile[’w’]=w_name inifile.write() os.system(’./camb ’+filename) cl_raw=np.loadtxt(new_folder+’/’+root+’_’+w_name+’_scalCovCls.dat’)[:,-1] l=np.loadtxt(new_folder+’/’+root+’_’+w_name+’_scalCovCls.dat’)[:,0] cl=cl_raw*2*np.pi/(l*(l+1)) plt.plot(l,cl,lw=1.5,color=cores[i]) plt.xlabel(r’$\ell$’) plt.ylabel(r’$C_\ell$’) plt.yscale(’log’) plt.ylim([1e-6,1e-5]) plt.xlim([10,300]) plt.show() inifile[’w’]=str(-1) inifile.write() 5