2
############################################################################
5
# AUTHOR(S): Hamish Bowman, Otago University, New Zealand
6
# Converted to Python by Martin Landa <landa.martin gmail.com>
7
# PURPOSE: Pack up a raster map, collect raster map elements => gzip
8
# COPYRIGHT: (C) 2004-2013 by the GRASS Development Team
10
# This program is free software under the GNU General
11
# Public License (>=v2). Read the file COPYING that
12
# comes with GRASS for details.
14
#############################################################################
17
#% description: Packs up a raster map and support files for copying.
22
#%option G_OPT_R_INPUT
23
#% description: Name of raster map to pack up
25
#%option G_OPT_F_OUTPUT
26
#% description: Name for output file (default is <input>.pack)
31
#% description: Switch the compression off
40
from grass.script.utils import try_rmdir, try_remove
41
from grass.script import core as grass
47
infile = options['input']
48
compression_off = flags['c']
51
infile, mapset = infile.split('@')
54
outfile_path, outfile_base = os.path.split(os.path.abspath(options['output']))
56
outfile_path, outfile_base = os.path.split(os.path.abspath(infile + ".pack"))
58
outfile = os.path.join(outfile_path, outfile_base)
62
tmp_dir = os.path.join(tmp, infile)
64
grass.debug('tmp_dir = %s' % tmp_dir)
66
gfile = grass.find_file(name = infile, element = 'cell', mapset = mapset)
68
grass.fatal(_("Raster map <%s> not found") % infile)
70
if os.path.exists(outfile):
71
if os.getenv('GRASS_OVERWRITE'):
72
grass.warning(_("Pack file <%s> already exists and will be overwritten") % outfile)
75
grass.fatal(_("option <output>: <%s> exists.") % outfile)
77
grass.message(_("Packing <%s> to <%s>...") % (gfile['fullname'], outfile))
78
basedir = os.path.sep.join(os.path.normpath(gfile['file']).split(os.path.sep)[:-2])
82
for element in ['cats', 'cell', 'cellhd', 'colr', 'fcell', 'hist']:
83
path = os.path.join(basedir, element, infile)
84
if os.path.exists(path):
85
grass.debug('copying %s' % path)
87
os.path.join(tmp_dir, element))
89
if os.path.exists(os.path.join(basedir, 'cell_misc', infile)):
90
shutil.copytree(os.path.join(basedir, 'cell_misc', infile),
91
os.path.join(tmp_dir, 'cell_misc'))
93
if not os.listdir(tmp_dir):
94
grass.fatal(_("No raster map components found"))
96
# copy projection info
97
# (would prefer to use g.proj*, but this way is 5.3 and 5.7 compat)
98
gisenv = grass.gisenv()
99
for support in ['INFO', 'UNITS', 'EPSG']:
100
path = os.path.join(gisenv['GISDBASE'], gisenv['LOCATION_NAME'],
101
'PERMANENT', 'PROJ_' + support)
102
if os.path.exists(path):
103
shutil.copyfile(path, os.path.join(tmp_dir, 'PROJ_' + support))
108
tar = tarfile.TarFile.open(name = outfile_base, mode = 'w:')
110
tar = tarfile.TarFile.open(name = outfile_base, mode = 'w:gz')
111
tar.add(infile, recursive = True)
114
shutil.move(outfile_base, outfile)
115
except shutil.Error as e:
120
grass.verbose(_("Raster map saved to '%s'" % outfile))
123
if __name__ == "__main__":
124
options, flags = grass.parser()
125
atexit.register(cleanup)