write_ncml_file.py 4.96 KB
Newer Older
1 2 3 4 5 6 7 8 9
import numpy as np
import sys

import PyFVCOM as pf

fvcom_file_loc = sys.argv[1]
output_file = sys.argv[2]
write_vars = sys.argv[3].split(',')

10 11 12
write_vars.append('h')
write_vars.append('mask')

13 14 15 16 17 18 19 20 21
all_attributes = {'lon':{'var_name':'longitude','standard_name':'longitude', 'units':'degrees_east', 'long_name':'longitude', 'shape':'longitude'},
            'lat':{'var_name':'latitude','standard_name':'latitude', 'units':'degrees_north', 'long_name':'latitude', 'shape':'latitude'},
            'depth':{'standard_name':'depth', 'units':'m', 'long_name':'depth, measured downwards from free surface', 'axis':'Z', 'positive':'down', 'shape':'depth'},
            'time':{'standard_name':'time', 'units':'days since 1858-11-17 00:00:00', 'long_name':'time', 'axis':'T','shape':'time', 'time_zone':'UTC', 'format':'modified julian day (MJD)'},
            'h':{'standard_name':'h', 'units':'m', 'long_name':'model bathymetry depth, measured downwards from geoid', 'axis':'Z', 'positive':'down', 'shape':'latitude longitude'},
            'temp':{'var_name':'thetao','standard_name':'sea_water_potential_temperature','units':'C','missing_value':-32768, 'long_name':'Sea Water Potential Temperature', 'shape':'time depth latitude longitude'} ,
            'salinity':{'var_name':'so','standard_name':'sea_water_salinity','units':'psu', 'missing_value':-32768, 'long_name':'Sea Water Salinity', 'shape':'time depth latitude longitude'},
            'u':{'var_name':'uo', 'standard_name':'eastward_sea_water_velocity','units':'m s-1', 'missing_value':-32768, 'long_name':'Eastward Current Velocity', 'shape':'time depth latitude longitude'},
            'v':{'var_name':'vo','standard_name':'northward_sea_water_velocity','units':'m s-1', 'missing_value':-32768, 'long_name':'Northward Current Velocity', 'shape':'time depth latitude longitude'},
22 23
            'zeta':{'var_name':'zos','standard_name':'sea_surface_height_above_geoid','units':'m', 'missing_value':-32768,'long_name':'Sea surface height above geoid', 'shape':'time latitude longitude'},
            'mask':{'standard_name':'sea_binary_mask','units':'1', 'long_name':'Land-sea mask: sea = 1 ; land = 0', 'shape':'depth latitude longitude'},
24 25
            'h':{'standard_name':'h', 'units':'m', 'long_name':'model bathymetry depth, measured downwards from geoid', 'axis':'Z', 'positive':'down', 'shape':'latitude longitude' },
            'ww':{'var_name':'wo', 'standard_name':'upward_sea_water_velocity','units':'m s-1', 'missing_value':-32768, 'long_name':'Upward Current Velocity', 'shape':'time depth latitude longitude'}
26
            }
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

all_type = {'add_offset':'float', 'scale_factor':'float', '_FillValue':'double'}

all_dimensions = {'longitude':'lon', 'latitude':'lat', 'depth':'depth', 'time':'time'} 

with open(output_file, 'a') as out_file:
    out_file.write("<?xml version='1.0' encoding='UTF-8'?>\n")
    out_file.write("<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2'\n")
    out_file.write("\t location='{}' >\n".format(fvcom_file_loc))

    out_file.write('\t<explicit />\n')

    for this_dim, this_dim_orig in all_dimensions.items():
        if this_dim == this_dim_orig:
            out_file.write('\t<dimension name="{}"/>\n'.format(this_dim))
        else:
            out_file.write('\t<dimension name="{}" orgName="{}"/>\n'.format(this_dim, this_dim_orig))

    for this_var in all_dimensions.values():
        this_atts = all_attributes[this_var]
        if 'var_name' in this_atts:
            out_file.write('\t<variable name="{}" orgName="{}" shape="{}" >\n'.format(this_atts['var_name'], this_var, this_atts['shape']))
        else:
            out_file.write('\t<variable name="{}" shape="{}" >\n'.format(this_var, this_atts['shape']))

        for this_att, this_val in this_atts.items():
            if this_att not in ['var_name', 'shape']:
                if this_att not in all_type.keys():
                    out_file.write('\t\t<attribute name="{}" value="{}"/>\n'.format(this_att, this_val))
                else:
                    out_file.write('\t\t<attribute name="{}" value="{}" type="{}"/>\n'.format(this_att, this_val, all_type[this_att]))

        out_file.write('\t</variable>\n')

    for this_var in write_vars:
        this_atts = all_attributes[this_var]
        if 'var_name' in this_atts:
            out_file.write('\t<variable name="{}" orgName="{}" shape="{}" >\n'.format(this_atts['var_name'], this_var, this_atts['shape']))
        else:
            out_file.write('\t<variable name="{}" shape="{}" >\n'.format(this_var, this_atts['shape']))

        for this_att, this_val in this_atts.items():
            if this_att not in ['var_name', 'shape']:
                if this_att not in all_type.keys():
                    out_file.write('\t\t<attribute name="{}" value="{}"/>\n'.format(this_att, this_val))
                else:
                    out_file.write('\t\t<attribute name="{}" value="{}" type="{}"/>\n'.format(this_att, this_val, all_type[this_att]))

        out_file.write('\t</variable>\n')

    out_file.write('</netcdf>')