import f90nml as nml
import numpy as np
import xarray as xr
import salishsea_cmd.api
import yaml
# Used to estimate a distance between two model outputs (with the same coordinate space)
def SmeltResultDif(result_1_dir, result_2_dir, tracer_filename = 'SS5x5_1h_20041019_20041023_ptrc_T.nc'):
ds1 = xr.open_dataset(result_1_dir + '/' +tracer_filename)
ds2 = xr.open_dataset(result_2_dir +'/' + tracer_filename)
#Picked MICZ pretty much arbitrarily
dif = ((np.square(ds1.MICZ.values - ds2.MICZ.values)).sum())**0.5 #RMS difference (should probably normalise by number of data points)
return(dif)
reference_namelist = '/data/jpetrie/MEOPAR/SalishSea/results/d9f69a38-160e-11e6-9b98-0025909a8461/namelist_pisces_cfg'
new_namelist_directory = '/data/jpetrie/MEOPAR/SS-run-sets/SS-SMELT/namelists/modified_namelists/'
reference_result = '/data/jpetrie/MEOPAR/SalishSea/results/d9f69a38-160e-11e6-9b98-0025909a8461'
reference_nml = f90nml.read(reference_namelist) # creates a dictionary-like object with contents of namelist
grad = dict.fromkeys(reference_nml, 0) # create dictionary filled with zeros
step_size = 1e-6
# Calculate gradient
for paramtype in reference_nml:
for param in reference_nml[paramtype]:
print(paramtype + " " + param)
if(not isinstance(reference_nml[paramtype][param],list)):
#some values are lists- need something different for them
patch_nml = {paramtype: {param: reference_nml[paramtype][param] + step_size}} # change parameter slightly
modified_nml_file = new_namelist_directory + 'mod_' + paramtype + '_' + param
# creates a new namelist file identical to original, but with the value specified by patch_nml changed
f90nml.patch(reference_namelist,patch_nml, modified_nml_file)
#### Call Salishsea using new namelist
#dif = SmeltResultDif(reference_result, new_result)
#grad[paramtype][param] = dif/step_size
nrdttrc zz_rate_r_diat zz_rate_r_myri zz_rate_r_nano zz_rate_maxtemp_diat zz_rate_maxtemp_myri zz_rate_maxtemp_nano zz_rate_temprange_diat zz_rate_temprange_myri zz_rate_temprange_nano zz_rate_iopt_diat zz_rate_iopt_myri zz_rate_iopt_nano zz_rate_gamma_diat zz_rate_gamma_myri zz_rate_gamma_nano zz_rate_k_si_diat zz_rate_k_si_myri zz_rate_k_si_nano zz_rate_gamma_o_diat zz_rate_gamma_o_myri zz_rate_gamma_o_nano zz_rate_kapa_diat zz_rate_kapa_myri zz_rate_kapa_nano zz_rate_k_diat zz_rate_k_myri zz_rate_k_nano zz_rate_n_x_diat zz_rate_n_x_myri zz_rate_n_x_nano zz_rate_n_o_diat zz_rate_n_o_myri zz_rate_n_o_nano zz_rate_si_ratio_diat zz_rate_si_ratio_myri zz_rate_si_ratio_nano zz_rate_pico_rm zz_frac_waste_fnm_nh zz_frac_waste_fnm_don zz_frac_waste_fnm_pon zz_frac_waste_fnm_ref zz_frac_waste_fnm_bsi zz_rate_pico_si_ratio zz_rate_myri_rm zz_frac_waste_nnm_nh zz_frac_waste_nnm_don zz_frac_waste_nnm_pon zz_frac_waste_nnm_ref zz_frac_waste_nnm_bsi zz_rate_myri_si_ratio zz_rate_micro_rm zz_frac_waste_dnm_nh zz_frac_waste_dnm_don zz_frac_waste_dnm_pon zz_frac_waste_dnm_ref zz_frac_waste_dnm_bsi zz_rate_micro_si_ratio zz_rate_mesorub_r zz_rate_mesorub_picopredslope zz_rate_mesorub_picohalfsat zz_rate_mesorub_eff zz_frac_waste_fen_nh zz_frac_waste_fen_don zz_frac_waste_fen_pon zz_frac_waste_fen_bsi zz_rate_mesozoo_winterconc zz_rate_mesozoo_summerconc zz_rate_mesozoo_sumpeakval zz_rate_mesozoo_sumpeakpos zz_rate_mesozoo_sumpeakwid zz_rate_mesozoo_alpha zz_rate_mesozoo_r zz_rate_mesozoo_rm zz_rate_mesozoo_excr zz_rate_mesozoo_predslope zz_rate_mesozoo_halfsat zz_rate_mesozoo_micropref zz_rate_mesozoo_micropredslope zz_rate_mesozoo_microhalfsat zz_rate_mesozoo_nanopref zz_rate_mesozoo_nanopredslope zz_rate_mesozoo_nanohalfsat zz_rate_mesozoo_picopref zz_rate_mesozoo_picopredslope zz_rate_mesozoo_picohalfsat zz_rate_mesozoo_pon_pref zz_rate_mesozoo_pon_predslope zz_rate_mesozoo_pon_halfsat zz_rate_mesozoo_z_pref zz_rate_mesozoo_z_predslope zz_rate_mesozoo_z_halfsat zz_rate_mesozoo_eff zz_frac_waste_mnm_nh zz_frac_waste_mnm_don zz_frac_waste_mnm_pon zz_frac_waste_mnm_bsi zz_frac_waste_mex_nh zz_frac_waste_mex_don zz_frac_waste_mex_pon zz_frac_waste_mex_bsi zz_frac_waste_pem_nh zz_frac_waste_pem_don zz_frac_waste_pem_pon zz_frac_waste_pem_bsi zz_frac_waste_dem_nh zz_frac_waste_dem_don zz_frac_waste_dem_pon zz_frac_waste_dem_bsi zz_frac_waste_fem_nh zz_frac_waste_fem_don zz_frac_waste_fem_pon zz_frac_waste_fem_bsi zz_frac_waste_zem_nh zz_frac_waste_zem_don zz_frac_waste_zem_pon zz_frac_waste_zem_bsi zz_frac_waste_nem_nh zz_frac_waste_nem_don zz_frac_waste_nem_pon zz_frac_waste_nem_bsi zz_rate_micro_si_ratio zz_rate_nano_si_ratio zz_rate_pico_si_ratio zz_rate_uzoo_rm zz_rate_uzoo_excr zz_frac_waste_znm_nh zz_frac_waste_zex_nh zz_rate_uzoo_predslope zz_rate_uzoo_halfsat zz_rate_uzoo_micropref zz_rate_uzoo_micropredslope zz_rate_uzoo_microhalfsat zz_rate_uzoo_nanopref zz_rate_uzoo_nanopredslope zz_rate_uzoo_nanohalfsat zz_rate_uzoo_picopref zz_rate_uzoo_picopredslope zz_rate_uzoo_picohalfsat zz_rate_uzoo_pon_pref zz_rate_uzoo_pon_predslope zz_rate_uzoo_pon_halfsat zz_rate_uzoo_z_pref zz_rate_uzoo_z_predslope zz_rate_uzoo_z_halfsat zz_rate_uzoo_r zz_frac_waste_pez_nh zz_frac_waste_dez_nh zz_frac_waste_nez_nh zz_frac_waste_fez_nh zz_frac_waste_zez_nh zz_rate_uzoo_eff zz_frac_waste_znm_don zz_frac_waste_zex_don zz_frac_waste_dez_don zz_frac_waste_fez_don zz_frac_waste_nez_don zz_frac_waste_pez_don zz_frac_waste_zez_don zz_frac_waste_znm_pon zz_frac_waste_zex_pon zz_frac_waste_dez_pon zz_frac_waste_fez_pon zz_frac_waste_nez_pon zz_frac_waste_pez_pon zz_frac_waste_zez_pon zz_frac_waste_znm_bsi zz_frac_waste_zex_bsi zz_frac_waste_dez_bsi zz_frac_waste_fez_bsi zz_frac_waste_nez_bsi zz_frac_waste_pez_bsi zz_frac_waste_zez_bsi zz_rate_micro_si_ratio zz_rate_nano_si_ratio zz_rate_pico_si_ratio zz_remin_nh zz_remin_d_don zz_remin_d_pon zz_remin_d_bsi ln_pisdmp ln_check_mass zzq_o zzalbedo zzialpha zzibeta zzigamma zzisigma zzitheta zzidl zzn2chl zz_w_sink_pmicro_min zz_w_sink_pmicro_max zz_w_sink_d_pon zz_w_sink_d_bsi
isinstance(nml['nampismezo']['zz_rate_mesozoo_sumpeakwid'], list)
True
stream = open('/data/jpetrie/MEOPAR/SS-run-sets/SS-SMELT/jpetrie/SMELT5x5test.yaml', 'r') # 'document.yaml' contains a single YAML document.
run_desc = yaml.load(stream)
run_desc
{'MPI decomposition': '1x1', 'config_name': 'SOG5x5test', 'email': 'jpetrie@eos.ubc.ca', 'forcing': {'initial_green': {'link to': 'initial_green/'}, 'initial_strat': {'link to': 'initial_strat/'}}, 'grid': {'bathymetry': 'bathy_downonegrid.nc', 'coordinates': 'coordinates_seagrid_SalishSea.nc'}, 'namelists': {'namelist_cfg': ['/data/jpetrie/MEOPAR/SS-run-sets/SS-SMELT/namelists/namelist_cfg_5x5_NewIC'], 'namelist_pisces_cfg': ['/data/jpetrie/MEOPAR/SS-run-sets/SS-SMELT/namelists/namelist_pisces_cfg_5x5_NewIC'], 'namelist_top_cfg': ['/data/jpetrie/MEOPAR/SS-run-sets/SS-SMELT/namelists/namelist_top_cfg_5x5_NewIC']}, 'output': {'XIOS servers': 1, 'domain': '/data/jpetrie/MEOPAR/SS-run-sets/SS-SMELT/iofiles/domain_def.xml', 'fields': '/data/jpetrie/MEOPAR/SS-run-sets/SS-SMELT/iofiles/field_def.xml', 'separate XIOS server': True}, 'paths': {'NEMO-code': '/data/jpetrie/MEOPAR/NEMO-3.6-code/', 'XIOS': '/data/jpetrie/MEOPAR/XIOS/', 'forcing': '/data/jpetrie/MEOPAR/NEMO-forcing/', 'runs directory': '/data/jpetrie/MEOPAR/SalishSea/results/'}, 'run_id': 'test5x5', 'walltime': 108000}
salishsea_cmd.api.run_in_subprocess("test_salishsea_api", run_desc, '/data/jpetrie/MEOPAR/SS-run-sets/SS-SMELT/iofiles/iodef_bio_1hr.xml', '/data/jpetrie/MEOPAR/SalishSea/results/test_salishsea_api')
salishsea_cmd.api ERROR: subprocess ['salishsea', 'run', 'test_salishsea_api_subprocess_run.yaml', '/data/jpetrie/MEOPAR/SS-run-sets/SS-SMELT/iofiles/iodef_bio_1hr.xml', '/data/jpetrie/MEOPAR/SalishSea/results/test_salishsea_api'] failed with return code 1 salishsea_cmd.api ERROR: salishsea_cmd.run INFO: Created run directory /data/jpetrie/MEOPAR/SalishSea/results/bf311930-170d-11e6-8bbe-10c37b4d98e4 salishsea_cmd.api ERROR: salishsea ERROR: [Errno 2] No such file or directory: 'qsub'