def mb_inf(src_xyz, src_fmt=168): """generate an info (.inf) file from a src_xyz file using MBSystem. Args: src_xyz (port): an open xyz port or generator src_fmt (int): the datalists format of the source data Returns: dict: xyz infos dictionary mb_inf_parse(inf_file) """ utils.run_cmd('mbdatalist -O -F{} -I{}'.format(src_fmt, src_xyz), verbose=False) return (mb_inf_parse('{}.inf'.format(src_xyz)))
def gmt_yield_entry(entry, region=None, verbose=False, z_region=None): """yield the xyz data from the xyz datalist entry Args: entry (list): a datalist entry region (list): a region list [xmin, xmax, ymin, ymax] verbose (bool): increase verbosity z_region (list): a z-regin [zmin, zmax] Yields: list: [x, y, z, <w, ...>] """ ln = 0 delim = ' ' if z_region is not None: z_region = ['-' if x is None else str(x) for x in z_region] out, status = utils.run_cmd('gmt gmtset IO_COL_SEPARATOR = SPACE', verbose=False) for line in utils.yield_cmd('gmt gmtselect {} {} {}\ '.format(entry[0], '' if region is None else regions.region_format(region, 'gmt'), '' if z_region is None else '-Z{}'.format('/'.join(z_region))), data_fun=None, verbose=False): ln += 1 yield (line) if verbose: utils.echo_msg('read {} data points from {}'.format(ln, entry[0]))
def las_inf2(src_las): '''scan an xyz file and find it's min/max values and write an associated inf file for the src_xyz file. returns region [xmin, xmax, ymin, ymax, zmin, zmax] of the src_xyz file.''' minmax = [] out, status = utils.run_cmd( 'lasinfo -nc -nv -stdout -i {}'.format(src_las), verbose=False) for i in out.split('\n'): if 'min x y z' in i: xyz_min = [ float(y) for y in [x.strip() for x in i.split(':')][1].split() ] if 'max x y z' in i: xyz_max = [ float(y) for y in [x.strip() for x in i.split(':')][1].split() ] minmax = [ xyz_min[0], xyz_max[0], xyz_min[1], xyz_max[1], xyz_min[2], xyz_max[2] ] with open('{}.inf'.format(src_las), 'w') as inf: utils.echo_msg('generating inf file for {}'.format(src_las)) inf.write('{}\n'.format(' '.join([str(x) for x in minmax]))) return (minmax)
def gmt_gmtinfo(src_xyz, verbose=False): """gather infos about src_xyz using GMT gmtinfo Args: src_xyz (str): a pathname to an XYZ file verbose (bool): increase verbosity Returns: list: an info list of `src_xyz` """ out, status = utils.run_cmd('gmt gmtset IO_COL_SEPARATOR = SPACE', verbose=verbose) gmtinfo_cmd = ('gmt gmtinfo {} -C'.format(src_xyz)) out, status = utils.run_cmd(gmtinfo_cmd, verbose=verbose) remove_glob('gmt.conf') if status == 0: return (out.split()) else: return (None)
def gmt_inf(src_xyz): """generate an info (.inf) file from a src_xyz file using GMT. Args: src_xyz (str): source xyz file pathname Returns: list: [cmd-output, cmd-return-code] """ return (utils.run_cmd('gmt gmtinfo {} -C > {}.inf'.format( src_xyz, src_xyz), verbose=False))
def gmt_grd_inf(src_grd): """generate an info (.inf) file from a src_gdal file using GMT. Args: src_grd (str): a pathname to a grid file Returns: list: [cmd-output, cmd-return-code] """ return (utils.run_cmd('gmt grdinfo {} -C > {}.inf'.format( src_grd, src_grd), verbose=False))
def gmt_slope(src_dem, dst_slp, verbose=False): """generate a Slope grid from a DEM with GMT Args: src_dem (str): pathname to a source DEM grid file dst_slp (str): pathname to a destination SLOPE grid file verbose (bool): increase verbosity Returns: int: status code (0 == success) """ o_b_name = '{}'.format(src_dem.split('.')[0]) slope_cmd0 = ('gmt grdgradient -V -fg {} -S{}_pslp.grd -D -R{}\ '.format(src_dem, o_b_name, src_dem)) out, status = utils.run_cmd(slope_cmd0, verbose=verbose) if status == 0: slope_cmd1 = ( 'gmt grdmath -V {}_pslp.grd ATAN PI DIV 180 MUL = {}=gd+n-9999:GTiff\ '.format(o_b_name, dst_slp)) out, status = utils.run_cmd(slope_cmd1, verbose=verbose) remove_glob('{}_pslp.grd'.format(o_b_name)) return (status)
def vdatum_get_version(vd_config=_vd_config): '''run vdatum and attempt to get it's version return the vdatum version or None''' if vd_config['jar'] is None: vd_config['jar'] = vdatum_locate_jar() if vd_config['jar'] is not None: out, status = utils.run_cmd('java -jar {} {}'.format( vd_config['jar'], '-'), verbose=self.verbose) for i in out.decode('utf-8').split('\n'): if '- v' in i.strip(): return (i.strip().split('v')[-1]) return (None)
def gmt_select_split(o_xyz, sub_region, sub_bn, verbose=False): """split an xyz file into an inner and outer region. Args: o_xyz (str): a pathname to an xyz file sub_region (list): a region list [xmin, xmax, ymin, ymax] sub_bn (str): a basename for the selected data verbose (bool): increase verbosity Returns: list: [inner_region, outer_region] """ out_inner = None out_outer = None gmt_s_inner = 'gmt gmtselect -V {} {} > {}_inner.xyz'.format( o_xyz, regions.region_format(sub_region, 'gmt'), sub_bn) out, status = utils.run_cmd(gmt_s_inner, verbose=verbose) if status == 0: out_inner = '{}_inner.xyz'.format(sub_bn) gmt_s_outer = 'gmt gmtselect -V {} {} -Ir > {}_outer.xyz'.format( o_xyz, regions.region_format(sub_region, 'gmt'), sub_bn) out, status = utils.run_cmd(gmt_s_outer, verbose=verbose) if status == 0: out_outer = '{}_outer.xyz'.format(sub_bn) return ([out_inner, out_outer])
def gmt_num_msk(num_grd, dst_msk, verbose=False): """generate a num-msk from a NUM grid using GMT grdmath Args: num_grd (str): pathname to a source `num` grid file dst_msk (str): pathname to a destination `msk` grid file verbose (bool): increase verbosity Returns: list: [cmd-output, cmd-return-code] """ num_msk_cmd = ('gmt grdmath -V {} 0 MUL 1 ADD 0 AND = {}\ '.format(num_grd, dst_msk)) return (utils.run_cmd(num_msk_cmd, verbose=verbose))
def gmt_grdcut(src_grd, src_region, dst_grd, verbose=False): """cut `src_grd` to `src_region` using GMT grdcut Args: src_grd (str): pathname to a source grid file src_region (list): a region list of the source grid [xmin, xmax, ymin, ymax] dst_grd (str): pathname to a destination grid file verbose (bool): increase verbosity Returns: list: [cmd-output, cmd-return-code] """ cut_cmd1 = ('gmt grdcut -V {} -G{} {}'.format(src_grd, dst_grd, src_region.gmt)) return (utils.run_cmd(cut_cmd1, verbose=True))
def gmt_sample_gnr(src_grd, verbose=False): """resamele src_grd to toggle between grid-node and pixel-node grid registration. Args: src_grd (str): pathname to a source grid file verbose (bool): increase verbosity Returns: int: status code (0 == success) """ out, status = utils.run_cmd( 'gmt grdsample -T {} -G_tmp.tif=gd+n-9999:GTiff'.format(src_grd), verbose=verbose) if status == 0: os.rename('_tmp.tif', '{}'.format(src_grd)) return (status)
def gmt_grdfilter(src_grd, dst_grd, dist='3s', node='pixel', verbose=False): """filter `src_grd` using GMT grdfilter Args: src_grd (str): pathname to a source grid file dst_grd (str): pathname to a destination grid file dist (str): a GMT string increment to use in filter node (str): `pixel` or `grid`; the grid-node verbose (bool): increase verbosity Returns: list: [cmd-output, cmd-return-code] """ #ft_cmd1 = ('gmt grdfilter -V {} -G{} -R{} -Fc{} -D1{}'.format(src_grd, dst_grd, src_grd, dist, ' -r' if node == 'pixel' else '')) ft_cmd1 = ('gmt grdfilter -V {} -G{} -Fc{} -D1{}'.format( src_grd, dst_grd, dist, ' -r' if node == 'pixel' else '')) return (utils.run_cmd(ft_cmd1, verbose=verbose))
def gmt_nan2zero(src_grd, node='pixel', verbose=False): """convert nan values in `src_grd` to zero Args: src_grd (str): pathname to a source grid file node (str): `pixel` or `grid`; the grid-node verbose (bool): increase verbosity Returns: int: status code (0 == success) """ num_msk_cmd = ( 'gmt grdmath -V {} 0 MUL 1 ADD 0 AND = _tmp.tif=gd+n-9999:GTiff'. format(src_grd)) out, status = utils.run_cmd(num_msk_cmd, verbose=True) if status == 0: os.rename('_tmp.tif', '{}'.format(src_grd)) return (status)
def run_vdatum(src_fn, vd_config=_vd_config): '''run vdatum on src_fn which is an XYZ file use vd_config to set vdatum parameters. returns [command-output, command-return-code]''' if vd_config['jar'] is None: vd_config['jar'] = vdatum_locate_jar()[0] if vd_config['jar'] is not None: vdc = 'ihorz:{} ivert:{} ohorz:{} overt:{} -nodata -file:txt:{},{},skip{}:{}:{} region:{}\ ' .format(vd_config['ihorz'], vd_config['ivert'], vd_config['ohorz'], vd_config['overt'], \ vd_config['delim'], vd_config['xyzl'], vd_config['skip'], src_fn, vd_config['result_dir'], vd_config['region']) #return(utils.run_cmd('java -jar {} {}'.format(vd_config['jar'], vdc), verbose = True)) return (utils.run_cmd( 'java -Djava.awt.headless=true -jar {} {}'.format( vd_config['jar'], vdc), verbose=vd_config['verbose'])) else: return ([], -1)
def gmt_sample_inc(src_grd, inc=1, verbose=False): """resamele src_grd to increment `inc` using GMT grdsample Args: src_grd (str): pathname to a source grid file inc (float): the increment to sample to verbose (bool): increase verbosity Returns int: status code (0 == success) """ out, status = utils.run_cmd( 'gmt grdsample -I{:.10f} {} -R{} -G_tmp.tif=gd+n-9999:GTiff\ '.format(inc, src_grd, src_grd), verbose=verbose) if status == 0: os.rename('_tmp.tif', '{}'.format(src_grd)) return (status)
def gmt_grdcut(src_grd, region, verbose=False): """cut a grid to region using GMT grdcut Args: src_grd (str): pathname to a source grid file region (list): a region list [xmin, xmax, ymin, ymax] verbose (bool): increase verbosity Returns: int: status code (0 == success) """ cut_cmd = ('gmt grdcut -V {} -G_tmp.grd {}\ '.format(src_grd, region_format(region, 'gmt'))) out, status = utils.run_cmd(cut_cmd, verbose=True) if status == 0: remove_glob(src_grd) os.rename('_tmp.grd', '{}'.format(src_grd)) return (status)
def vdatum_xyz(xyz, vd_config=_vd_config): '''run vdatum on an xyz list [x, y, z] returns the transformed xyz list''' if vd_config['jar'] is None: vd_config['jar'] = vdatum_locate_jar()[0] if vd_config['jar'] is not None: vdc = 'ihorz:{} ivert:{} ohorz:{} overt:{} -nodata -pt:{},{},{} region:{}\ ' .format(vd_config['ihorz'], vd_config['ivert'], vd_config['ohorz'], vd_config['overt'], \ xyz[0], xyz[1], xyz[2], vd_config['region']) out, status = utils.run_cmd( 'java -Djava.awt.headless=false -jar {} {}'.format( vd_config['jar'], vdc), verbose=False) for i in out.split('\n'): if 'Height/Z' in i: z = float(i.split()[2]) break return ([xyz[0], xyz[1], z]) else: return (xyz)
def gmt_grd2gdal(src_grd, dst_fmt='GTiff', epsg=4326, verbose=False): """convert the grd file to tif using GMT Args: src_grd (str): a pathname to a grid file dst_fmt (str): the output GDAL format string epsg (int): an EPSG code verbose (bool): increase verbosity Returns: str: the gdal file name or None """ dst_gdal = '{}.{}'.format( os.path.basename(src_grd).split('.')[0], utils.gdal_fext(dst_fmt)) grd2gdal_cmd = ('gmt grdconvert {} {}=gd+n-9999:{} -V\ '.format(src_grd, dst_gdal, dst_fmt)) out, status = utils.run_cmd(grd2gdal_cmd, verbose=verbose) if status == 0: return (dst_gdal) else: return (None)
sys.stderr.write('{}\n'.format(_version)) sys.exit(1) elif elev is None: elev = arg elif src_ply is None: src_ply = arg else: sys.stderr.write(_usage) sys.exit(1) i = i + 1 if elev is None or not os.path.exists(elev): sys.stderr.write(_usage) utils.echo_error_msg('you must enter a valid input raster file') sys.exit(1) if src_ply is None or not os.path.exists(src_ply): sys.stderr.write(_usage) utils.echo_error_msg('you must enter a valid input vector file') sys.exit(1) if want_output: elev2 = elev.split('.')[0] + '_clip.tif' out, status = utils.run_cmd('gdal_translate {} {}'.format(elev, elev2), verbose=True) else: elev2 = elev out, status = gdalfun.gdal_clip(elev2, src_ply, want_invert) ### End