In [39]:
from osgeo import gdal
In [40]:
raster = r'/usgs/data0/bathy/sandy/zip3/big.nc'
ofile = r'/usgs/data2/notebook/data/sandy_3sb.ncml'
title = 'sandy3s'
In [41]:
ds=gdal.Open(raster)
gt=ds.GetGeoTransform()
print(gt)
In [42]:
ncml = '''<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2"
location="/usgs/data0/bathy/srtm30plus_v1.nc">
<dimension name="lon" orgName="x"/>
<dimension name="lat" orgName="y"/>
<variable name="lon" shape="lon" type="double">
<attribute name="units" value="degrees_east"/>
<values start="-180.00" increment="+0.008333333333333333"/>
</variable>
<variable name="lat" shape="lat" type="double">
<attribute name="units" value="degrees_north"/>
<values start="90.00" increment="-0.008333333333333333"/>
</variable>
<variable name="topo" orgName="Band1">
<attribute name="units" value="meters"/>
<attribute name="long_name" value="elevation"/>
<attribute name="standard_name" value="height_above_geopotential_surface"/>
<attribute name="grid_mapping" value="crs"/>
</variable>
<variable name="crs">
<attribute name="grid_mapping_name" value="latitude_longitude"/>
<attribute name="longitude_of_prime_meridian" type="float" value="0.0"/>
<attribute name="semi_major_axis" type="float" value="6378137.0"/>
<attribute name="inverse_flattening" type="float" value="298.257223563"/>
<attribute name="geopotential_datum_name" value="NAVD88"/>
<attribute name="crs_wkt" value="COMPD_CS[\\\"NAD83 + NAVD88 height\\\",GEOGCS[\\\"NAD83\\\",DATUM[\\\"North_American_Datum_1983\\\",SPHEROID[\\\"GRS 1980\\\",6378137,298.257222101,AUTHORITY[\\\"EPSG\\\",\\\"7019\\\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\\\"EPSG\\\",\\\"6269\\\"]],PRIMEM[\\\"Greenwich\\\",0,AUTHORITY[\\\"EPSG\\\",\\\"8901\\\"]],UNIT[\\\"degree\\\",0.0174532925199433,AUTHORITY[\\\"EPSG\\\",\\\"9122\\\"]],AUTHORITY[\\\"EPSG\\\",\\\"4269\\\"]],VERT_CS[\\\"NAVD88 height\\\",VERT_DATUM[\\\"North American Vertical Datum 1988\\\",2005,AUTHORITY[\\\"EPSG\\\",\\\"5103\\\"],EXTENSION[\\\"PROJ4_GRIDS\\\",\\\"g2012a_conus.gtx,g2012a_alaska.gtx,g2012a_guam.gtx,g2012a_hawaii.gtx,g2012a_puertorico.gtx,g2012a_samoa.gtx\\\"]],UNIT[\\\"metre\\\",1,AUTHORITY[\\\"EPSG\\\",\\\"9001\\\"]],AXIS[\\\"Up\\\",UP],AUTHORITY[\\\"EPSG\\\",\\\"5703\\\"]],AUTHORITY[\\\"EPSG\\\",\\\"5498\\\"]]"/>
</variable>
<attribute name="Conventions" value="CF-1.0"/>
<attribute name="title" value="SRTM30_v1"/>
</netcdf>'''
In [43]:
ncml = '''<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2"
location="/usgs/data0/bathy/srtm30plus_v1.nc">
<dimension name="lon" orgName="x"/>
<dimension name="lat" orgName="y"/>
<variable name="lon" shape="lon" type="double">
<attribute name="units" value="degrees_east"/>
<values start="-180.00" increment="+0.008333333333333333"/>
</variable>
<variable name="lat" shape="lat" type="double">
<attribute name="units" value="degrees_north"/>
<values start="90.00" increment="-0.008333333333333333"/>
</variable>
<variable name="topo" orgName="Band1">
<attribute name="units" value="meters"/>
<attribute name="long_name" value="elevation"/>
<attribute name="standard_name" value="height_above_geopotential_surface"/>
<attribute name="grid_mapping" value="crs"/>
</variable>
<variable name="crs" type="int">
<attribute name="grid_mapping_name" value="latitude_longitude"/>
<attribute name="longitude_of_prime_meridian" type="float" value="0.0"/>
<attribute name="semi_major_axis" type="float" value="6378137.0"/>
<attribute name="inverse_flattening" type="float" value="298.257223563"/>
<attribute name="geopotential_datum_name" value="NAVD88"/>
<attribute name="crs_wkt" value="COMPD_CS["NAD83 + NAVD88 height",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],VERT_CS["NAVD88 height",VERT_DATUM["North American Vertical Datum 1988",2005,AUTHORITY["EPSG","5103"],EXTENSION["PROJ4_GRIDS","g2012a_conus.gtx,g2012a_alaska.gtx,g2012a_guam.gtx,g2012a_hawaii.gtx,g2012a_puertorico.gtx,g2012a_samoa.gtx"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Up",UP],AUTHORITY["EPSG","5703"]],AUTHORITY["EPSG","5498"]]"/>
</variable>
<attribute name="Conventions" value="CF-1.0"/>
<attribute name="title" value="SRTM30_v1"/>
</netcdf>'''
In [44]:
#replace title
ncml = ncml.replace('SRTM30_v1',title)
#replace lon_min
ncml = ncml.replace('-180.00',str(gt[0]))
#replace lon_increment
ncml = ncml.replace('+0.008333333333333333',str(gt[1]))
#replace lat_max
ncml = ncml.replace('90.00',str(gt[3]))
#replace lat_increment
ncml = ncml.replace('-0.008333333333333333',str(gt[5]))
#replace file location
ncml = ncml.replace(r'/usgs/data0/bathy/srtm30plus_v1.nc',raster)
print(ncml)
In [45]:
with open(ofile, "w") as text_file:
text_file.write("{}".format(ncml))
In [46]:
value="COMPD_CS[\\\"NAD83 + NAVD88 height\\\",GEOGCS[\\\"NAD83\\\",DATUM[\\\"North_American_Datum_1983\\\",SPHEROID[\\\"GRS 1980\\\",6378137,298.257222101,AUTHORITY[\\\"EPSG\\\",\\\"7019\\\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\\\"EPSG\\\",\\\"6269\\\"]],PRIMEM[\\\"Greenwich\\\",0,AUTHORITY[\\\"EPSG\\\",\\\"8901\\\"]],UNIT[\\\"degree\\\",0.0174532925199433,AUTHORITY[\\\"EPSG\\\",\\\"9122\\\"]],AUTHORITY[\\\"EPSG\\\",\\\"4269\\\"]],VERT_CS[\\\"NAVD88 height\\\",VERT_DATUM[\\\"North American Vertical Datum 1988\\\",2005,AUTHORITY[\\\"EPSG\\\",\\\"5103\\\"],EXTENSION[\\\"PROJ4_GRIDS\\\",\\\"g2012a_conus.gtx,g2012a_alaska.gtx,g2012a_guam.gtx,g2012a_hawaii.gtx,g2012a_puertorico.gtx,g2012a_samoa.gtx\\\"]],UNIT[\\\"metre\\\",1,AUTHORITY[\\\"EPSG\\\",\\\"9001\\\"]],AXIS[\\\"Up\\\",UP],AUTHORITY[\\\"EPSG\\\",\\\"5703\\\"]],AUTHORITY[\\\"EPSG\\\",\\\"5498\\\"]]"
In [47]:
value
Out[47]:
In [48]:
value="COMPD_CS[\"NAD83 + NAVD88 height\",GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\",SPHEROID[\"GRS 1980\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"7019\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",\"6269\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4269\"]],VERT_CS[\"NAVD88 height\",VERT_DATUM[\"North American Vertical Datum 1988\",2005,AUTHORITY[\"EPSG\",\"5103\"],EXTENSION[\"PROJ4_GRIDS\",\"g2012a_conus.gtx,g2012a_alaska.gtx,g2012a_guam.gtx,g2012a_hawaii.gtx,g2012a_puertorico.gtx,g2012a_samoa.gtx\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Up\",UP],AUTHORITY[\"EPSG\",\"5703\"]],AUTHORITY[\"EPSG\",\"5498\"]]"
In [49]:
value
Out[49]:
In [ ]: