#!/usr/bin/env python # coding: utf-8 # In[82]: import f90nml as nml import numpy as np import xarray as xr import salishsea_cmd.api import yaml # In[ ]: # 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) # In[58]: 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' # In[ ]: reference_nml = f90nml.read(reference_namelist) # creates a dictionary-like object with contents of namelist # In[73]: 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 # In[71]: isinstance(nml['nampismezo']['zz_rate_mesozoo_sumpeakwid'], list) # In[92]: 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) # In[96]: run_desc # In[95]: 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')