In [36]:
import yaml
import netCDF4
import os
from StringIO import StringIO

In [37]:
cd /usgs/data2/notebook/ROMS


/usgs/data2/notebook/ROMS

In [38]:
#map ROMS variables to CF standard_names
cf = {'ocean_time':'time',
      'zeta':'sea_surface_height_above_datum',
      'temp':'sea_water_potential_temperature',
      'salt':'sea_water_salinity',
         'u':'x_sea_water_velocity',
         'v':'y_sea_water_velocity',
      'ubar':'barotropic_x_sea_water_velocity',
      'vbar':'barotropic_y_sea_water_velocity',
     'Hwave':'sea_surface_wave_significant_height'}

In [39]:
x="""

dataset:
    id: "USGS_COAWST_MVCO_CBLAST_Ripples_SWAN_40m"

    title: "USGS-CMG-COAWST Model: CBLAST2007 Ripples with SWAN-40m res"

    summary: "Simulation of hydrodynamics and bottom stress south of Marthas Vineyard, MA using the COAWST modeling system.  These results are from the 40m inner nest of a four-level nested simulation."
    
    creator:
        email: nganju@usgs.gov
        name: Neil Ganju
        url: http://water.usgs.gov/fluxes
        
    
    project:
        - CMG_Portal
        - Sandy_Portal

      
    license: "The data may be used and redistributed for free but is not intended for legal use, since it may contain inaccuracies. Neither the data Contributor, nor the United States Government, nor any of their employees or contractors, makes any warranty, express or implied, including warranties of merchantability and fitness for a particular purpose, or assumes any legal liability for the accuracy, completeness, or usefulness, of this information."
    
    references: 
        - http://www.whoi.edu/science/AOPE/dept/CBLASTmain.html
        - http://water.usgs.gov/fluxes/mvco.html
        - doi:10.1029/2011JC007035

    acknowledgements:
        - USGS-CMGP
        - NSF    

variables: 
    include: 
        - temp
        - salt
 
    exclude: 
        - ubar
        - vbar

aggregation:
    time_var: ocean_time
    dir: /usgs/data0/mvco_ce/mvco_output/spatial_7_ar0fd
    sample_file: his_case7_ar0fd_0001.nc
    pattern: .*/his_case7_ar0fd_[0-9]{4}\.nc$


"""
#
# couldn't get this to work
#stream = open(StringIO(x))

In [40]:
# so read file instead
stream = open("NYB05.yaml", 'r')
a = yaml.load(stream)

In [41]:
a['dataset']


Out[41]:
{'acknowledgements': ['USGS-CMGP'],
 'creator': {'email': 'jcwarner@usgs.gov',
  'name': 'John Warner',
  'url': 'http://woodshole.er.usgs.gov/staffpages/jwarner/'},
 'id': 'USGS_COAWST_Sandy_NYB05_sim6',
 'license': 'The data may be used and redistributed for free but is not intended for legal use, since it may contain inaccuracies. Neither the data Contributor, nor the United States Government, nor any of their employees or contractors, makes any warranty, express or implied, including warranties of merchantability and fitness for a particular purpose, or assumes any legal liability for the accuracy, completeness, or usefulness, of this information.',
 'naming_authority': 'gov.usgs.cmg',
 'project': ['CMG_Portal', 'Sandy_Portal'],
 'publisher': {'email': 'jcwarner@usgs.gov',
  'name': 'John Warner',
  'url': 'http://woodshole.er.usgs.gov/staffpages/jwarner/'},
 'references': ['http://woodshole.er.usgs.gov/operations/modeling/COAWST/',
  'doi:10.1016/j.cageo.2008.02.012'],
 'summary': 'Simulation of hydrodynamics, waves, bottom stress and sediment transport during Hurricane Sandy. These results are from the 700m finest resolutionnest of a three-level nested simulation.',
 'title': 'USGS-CMG-COAWST Model: Hurricane Sandy, NYB05 700m Nest'}

In [42]:
def header():
    str='<?xml version="1.0" encoding="UTF-8"?>\n<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">\n'
    str += str_att('Conventions','CF-1.6, SGRID-0.1, ACDD-1.3')
    str += str_att('cdm_data_type','Grid')
    return str

In [43]:
def footer(str):
    str += '</netcdf>\n'
    return str

In [44]:
def str_att(name,value):
    if isinstance(value, list):
        value = ','.join(value)
    return '  <attribute name="{:s}" type="String" value="{:s}"/>\n'.format(name,value)

In [45]:
def add_global_atts(str,a):
    d = a['dataset']
    for key, value in d.iteritems():
        # handle simple attribute pairs first
        if key in ['id','license','summary','title','project','naming_authority','references','acknowledgements']:
            str += str_att(key,value)
            
        elif key in ['creator','publisher']:

            email = value.get("email", None)
            if email:
                str += str_att('_'.join([key,'email']),email)

            url = value.get("url", None)
            if url:
                str += str_att('_'.join([key,'url']),url)

            name = value.get("name", None)
            if name:
                str += str_att('_'.join([key,'name']),name)
                
        elif key in ['contributor']:
            role = value.get("role", None)
            if email:
                str += str_att('_'.join([key,'role']),role)
            
            email = value.get("email", None)
            if email:
                str += str_att('_'.join([key,'email']),email)

            url = value.get("url", None)
            if url:
                str += str_att('_'.join([key,'url']),url)

            name = value.get("name", None)
            if name:
                str += str_att('_'.join([key,'name']),name)
    return str

In [46]:
ncfile=os.path.join(a['aggregation']['dir'],a['aggregation']['sample_file'])
nc = netCDF4.Dataset(ncfile)
ncv = nc.variables

In [47]:
def add_var_atts(str,a):
    ncfile=os.path.join(a['aggregation']['dir'],a['aggregation']['sample_file'])
    nc = netCDF4.Dataset(ncfile)
    ncv = nc.variables
    
    # get a list of all variables more than 1D
    vars = [var for var, vart in ncv.items() if vart.ndim > 1]
    vars_all = set(vars)
    vars_include = set(a['variables']['include'])
    vars_exclude = set(a['variables']['exclude'])
    if a['variables']['exclude']:
        vars = list(vars_all - vars_all.intersection(vars_exclude))
    else:
        if a['variables']['include']:
            vars = list(vars_all.intersection(vars_include))
        
    rho_vars = [var for var, vart in ncv.items() if 'eta_rho' in 
                vart.dimensions and 'xi_rho' in vart.dimensions]
    u_vars = [var for var, vart in ncv.items() if 'eta_u' in 
              vart.dimensions and 'xi_u' in vart.dimensions]
    v_vars = [var for var, vart in ncv.items() if 'eta_v' in 
              vart.dimensions and 'xi_v' in vart.dimensions]
    
    var = 'ocean_time'
    if var in ncv.keys():
        try:
            str += '\n<variable name="{:s}">\n'.format(var)
            str += str_att('standard_name',cf[var]) 
            str += '</variable>\n\n'
        except:
            pass
    
    for var in vars:
        str += '<variable name="{:s}">\n'.format(var)
        try:
            str += str_att('standard_name',cf[var])
        except:
            pass
        str += str_att('grid','grid')
        str += str_att('content_coverage_type','modelResult')
        if var in rho_vars:
            str += str_att('location','face')
        elif var in u_vars:
            str += str_att('location','edge1')
        elif var in v_vars:
            str += str_att('location','edge2')
        str += '</variable>\n\n'
        
    return str

In [48]:
def write_grid_var(str):
    grid_var="""<variable name="grid" type="int">
        <attribute name="cf_role" value="grid_topology"/>
        <attribute name="topology_dimension" type="int" value="2"/>
        <attribute name="node_dimensions" value="xi_psi eta_psi"/>
        <attribute name="face_dimensions"
            value="xi_rho: xi_psi (padding: both) eta_rho: eta_psi (padding: both)"/>
        <attribute name="edge1_dimensions" value="xi_u: xi_psi eta_u: eta_psi (padding: both)"/>
        <attribute name="edge2_dimensions" value="xi_v: xi_psi (padding: both) eta_v: eta_psi"/>
        <attribute name="node_coordinates" value="lon_psi lat_psi"/>
        <attribute name="face_coordinates" value="lon_rho lat_rho"/>
        <attribute name="edge1_coordinates" value="lon_u lat_u"/>
        <attribute name="edge2_coordinates" value="lon_v lat_v"/>
        <attribute name="vertical_dimensions" value="s_rho: s_w (padding: none)"/>
    </variable>\n """
    str += grid_var
    return str

In [49]:
def add_aggregation_scan(str,a):
    agg = a['aggregation']
#    <aggregation dimName="ocean_time" type="joinExisting">
#        <scan location="." regExp=".*his_case7_ar0fd_[0-9]{4}\.nc$" subdirs="false"/>
#    </aggregation>
    str += '<aggregation dimName="{:s}" type="joinExisting">\n'.format(agg['time_var'])
    str += '<scan location="." regExp="{:s}" subdirs="false"/>\n</aggregation>\n'.format(agg['pattern'])
    return str

In [50]:
str = header()
str = add_global_atts(str,a)
str = add_var_atts(str,a)
str = write_grid_var(str)
str = add_aggregation_scan(str,a)
str = footer(str)

In [51]:
print str


<?xml version="1.0" encoding="UTF-8"?>
<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">
  <attribute name="Conventions" type="String" value="CF-1.6, SGRID-0.1, ACDD-1.3"/>
  <attribute name="cdm_data_type" type="String" value="Grid"/>
  <attribute name="publisher_email" type="String" value="jcwarner@usgs.gov"/>
  <attribute name="publisher_url" type="String" value="http://woodshole.er.usgs.gov/staffpages/jwarner/"/>
  <attribute name="publisher_name" type="String" value="John Warner"/>
  <attribute name="license" type="String" value="The data may be used and redistributed for free but is not intended for legal use, since it may contain inaccuracies. Neither the data Contributor, nor the United States Government, nor any of their employees or contractors, makes any warranty, express or implied, including warranties of merchantability and fitness for a particular purpose, or assumes any legal liability for the accuracy, completeness, or usefulness, of this information."/>
  <attribute name="creator_email" type="String" value="jcwarner@usgs.gov"/>
  <attribute name="creator_url" type="String" value="http://woodshole.er.usgs.gov/staffpages/jwarner/"/>
  <attribute name="creator_name" type="String" value="John Warner"/>
  <attribute name="title" type="String" value="USGS-CMG-COAWST Model: Hurricane Sandy, NYB05 700m Nest"/>
  <attribute name="summary" type="String" value="Simulation of hydrodynamics, waves, bottom stress and sediment transport during Hurricane Sandy. These results are from the 700m finest resolutionnest of a three-level nested simulation."/>
  <attribute name="project" type="String" value="CMG_Portal,Sandy_Portal"/>
  <attribute name="references" type="String" value="http://woodshole.er.usgs.gov/operations/modeling/COAWST/,doi:10.1016/j.cageo.2008.02.012"/>
  <attribute name="acknowledgements" type="String" value="USGS-CMGP"/>
  <attribute name="id" type="String" value="USGS_COAWST_Sandy_NYB05_sim6"/>
  <attribute name="naming_authority" type="String" value="gov.usgs.cmg"/>

<variable name="ocean_time">
  <attribute name="standard_name" type="String" value="time"/>
</variable>

<variable name="bvstrcwmax">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="dep_net">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="v_stokes">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="grain_density">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bvstrc">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bvstrw">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bed_age">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="lon_psi">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
</variable>

<variable name="bedload_Vsand_03">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="bedload_Vsand_02">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="bedload_Vsand_01">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="bed_biodiff">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bedload_Vsand_06">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="bedload_Vsand_05">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="bedload_Vsand_04">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="active_layer_thickness">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="Pwave_top">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="h">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="f">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="vbar">
  <attribute name="standard_name" type="String" value="barotropic_y_sea_water_velocity"/>
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="wetdry_mask_psi">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
</variable>

<variable name="bstrcwmax">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="u_stokes">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="bustrcwmax">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandfrac_02">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandfrac_03">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandfrac_01">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandfrac_06">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandfrac_04">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandfrac_05">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bustrc">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bustrw">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bedload_Usand_02">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="bedload_Usand_03">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="bedload_Usand_01">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="bedload_Usand_06">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="bedload_Usand_04">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="bedload_Usand_05">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="sandmass_06">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandmass_04">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandmass_05">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandmass_02">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandmass_03">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sandmass_01">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="w">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="uWave">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="lon_rho">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="mask_v">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="mask_u">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="mask_rho">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="lat_psi">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
</variable>

<variable name="zeta">
  <attribute name="standard_name" type="String" value="sea_surface_height_above_datum"/>
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="angle">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="erosion_stress">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="wetdry_mask_v">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="wetdry_mask_u">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="pn">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bed_porosity">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="ripple_length">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="Lwave">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="settling_vel">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="temp">
  <attribute name="standard_name" type="String" value="sea_water_potential_temperature"/>
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="wetdry_mask_rho">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="ubar">
  <attribute name="standard_name" type="String" value="barotropic_x_sea_water_velocity"/>
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="v">
  <attribute name="standard_name" type="String" value="y_sea_water_velocity"/>
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="pm">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="vWave">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bed_inund_depth">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="Pwave_bot">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="Zo_def">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sand_02">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sand_03">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sand_01">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sand_06">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sand_04">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="sand_05">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="Lwavep">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="Dwave">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="Hwave">
  <attribute name="standard_name" type="String" value="sea_surface_wave_significant_height"/>
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="grain_diameter">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="lon_v">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="lon_u">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="lat_rho">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="mask_psi">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
</variable>

<variable name="lat_v">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge2"/>
</variable>

<variable name="lat_u">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="Zo_app">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bed_thickness">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="bed_wave_amp">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="u">
  <attribute name="standard_name" type="String" value="x_sea_water_velocity"/>
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="edge1"/>
</variable>

<variable name="Uwave_rms">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="salt">
  <attribute name="standard_name" type="String" value="sea_water_salinity"/>
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="ripple_height">
  <attribute name="grid" type="String" value="grid"/>
  <attribute name="content_coverage_type" type="String" value="modelResult"/>
  <attribute name="location" type="String" value="face"/>
</variable>

<variable name="grid" type="int">
        <attribute name="cf_role" value="grid_topology"/>
        <attribute name="topology_dimension" type="int" value="2"/>
        <attribute name="node_dimensions" value="xi_psi eta_psi"/>
        <attribute name="face_dimensions"
            value="xi_rho: xi_psi (padding: both) eta_rho: eta_psi (padding: both)"/>
        <attribute name="edge1_dimensions" value="xi_u: xi_psi eta_u: eta_psi (padding: both)"/>
        <attribute name="edge2_dimensions" value="xi_v: xi_psi (padding: both) eta_v: eta_psi"/>
        <attribute name="node_coordinates" value="lon_psi lat_psi"/>
        <attribute name="face_coordinates" value="lon_rho lat_rho"/>
        <attribute name="edge1_coordinates" value="lon_u lat_u"/>
        <attribute name="edge2_coordinates" value="lon_v lat_v"/>
        <attribute name="vertical_dimensions" value="s_rho: s_w (padding: none)"/>
    </variable>
 <aggregation dimName="ocean_time" type="joinExisting">
<scan location="." regExp=".*ocean_NYB05_his_[0-9]{4}\.nc$" subdirs="false"/>
</aggregation>
</netcdf>


In [52]:
#with open('{:s}/test6.ncml'.format(a['aggregation']['dir']),'w') as text_file:
#    text_file.write("{:s}".format(str))

In [52]: