Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grass.temporal.stds_export: Stds export list write #211

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 32 additions & 37 deletions python/grass/temporal/stds_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import shutil
import tarfile
import tempfile
from pathlib import Path

import grass.script as gs
from grass.exceptions import CalledModuleError
Expand Down Expand Up @@ -402,11 +403,8 @@ def export_stds(
# Write projection and metadata
proj = gs.read_command("g.proj", flags="j")

proj_file = open(proj_file_name, "w")
proj_file.write(proj)
proj_file.close()
Path(proj_file_name).write_text(proj)

init_file = open(init_file_name, "w")
# Create the init string
string = ""
# This is optional, if not present strds will be assumed for backward
Expand All @@ -427,64 +425,61 @@ def export_stds(
string += "%s=%s\n" % ("south", south)
string += "%s=%s\n" % ("east", east)
string += "%s=%s\n" % ("west", west)
init_file.write(string)
init_file.close()
Path(init_file_name).write_text(string)

metadata = gs.read_command("t.info", type=type_, input=sp.get_id())
metadata_file = open(metadata_file_name, "w")
metadata_file.write(metadata)
metadata_file.close()
Path(metadata_file_name).write_text(metadata)

read_file = open(read_file_name, "w")
readme: list[str] = []
if type_ == "strds":
read_file.write(
readme.append(
"This space time raster dataset was exported with "
"t.rast.export of GRASS GIS 8\n"
)
elif type_ == "stvds":
read_file.write(
readme.append(
"This space time vector dataset was exported with "
"t.vect.export of GRASS GIS 8\n"
)
elif type_ == "str3ds":
read_file.write(
readme.append(
"This space time 3D raster dataset was exported "
"with t.rast3d.export of GRASS GIS 8\n"
)
read_file.write("\n")
read_file.write("Files:\n")
readme.extend(("\n", "Files:\n"))
if type_ == "strds":
if format_ == "GTiff":
# 123456789012345678901234567890
read_file.write(" *.tif -- GeoTIFF raster files\n")
read_file.write(" *.color -- GRASS GIS raster color rules\n")
readme.extend(
(
" *.tif -- GeoTIFF raster files\n",
" *.color -- GRASS GIS raster color rules\n",
)
)
elif format_ == "pack":
read_file.write(" *.pack -- GRASS raster files packed with r.pack\n")
readme.append(" *.pack -- GRASS raster files packed with r.pack\n")
elif type_ == "stvds":
# 123456789012345678901234567890
if format_ == "GML":
read_file.write(" *.xml -- Vector GML files\n")
readme.append(" *.xml -- Vector GML files\n")
else:
read_file.write(" *.pack -- GRASS vector files packed with v.pack\n")
readme.append(" *.pack -- GRASS vector files packed with v.pack\n")
elif type_ == "str3ds":
read_file.write(" *.pack -- GRASS 3D raster files packed with r3.pack\n")
read_file.write(
"%13s -- Projection information in PROJ.4 format\n" % (proj_file_name)
)
read_file.write(
"%13s -- GRASS GIS space time %s dataset information\n"
% (init_file_name, sp.get_new_map_instance(None).get_type())
)
read_file.write(
"%13s -- Time series file, lists all maps by name "
"with interval\n" % (list_file_name)
)
read_file.write(
" time stamps in ISO-Format. Field separator is |\n"
readme.append(" *.pack -- GRASS 3D raster files packed with r3.pack\n")
readme.extend(
(
"%13s -- Projection information in PROJ.4 format\n" % proj_file_name,
"%13s -- GRASS GIS space time %s dataset information\n"
% (init_file_name, sp.get_new_map_instance(None).get_type()),
"%13s -- Time series file, lists all maps by name with interval\n"
% list_file_name,
" time stamps in ISO-Format. Field separator is |\n",
"%13s -- The output of t.info\n" % metadata_file_name,
"%13s -- This file\n" % read_file_name,
)
)
read_file.write("%13s -- The output of t.info\n" % (metadata_file_name))
read_file.write("%13s -- This file\n" % (read_file_name))
read_file.close()
with Path(read_file_name).open("w") as read_file_:
read_file_.writelines(readme)

# Append the file list
tar.add(list_file_name)
Expand Down
13 changes: 5 additions & 8 deletions python/grass/temporal/stds_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import os
import os.path
import tarfile
from pathlib import Path

import grass.script as gs
from grass.exceptions import CalledModuleError
Expand Down Expand Up @@ -302,16 +303,12 @@ def import_stds(
# from other programs than g.proj into
# new line format so that the grass file comparison function
# can be used to compare the projections
proj_name_tmp = temp_name + "_in_projection"
proj_file = open(proj_name, "r")
proj_content = proj_file.read()
proj_content = Path(proj_name).read_text()
proj_content = proj_content.replace(" +", "\n+")
proj_content = proj_content.replace("\t+", "\n+")
proj_file.close()

proj_file = open(proj_name_tmp, "w")
proj_file.write(proj_content)
proj_file.close()
proj_name_tmp = f"{temp_name}_in_projection"
Path(proj_name_tmp).write_text(proj_content)

p = gs.start_command("g.proj", flags="j", stdout=temp_file)
p.communicate()
Expand All @@ -336,7 +333,7 @@ def import_stds(
old_env = gs.gisenv()
if location:
try:
proj4_string = open(proj_file_name, "r").read()
proj4_string = Path(proj_file_name).read_text()
gs.create_location(
dbase=old_env["GISDBASE"], location=location, proj4=proj4_string
)
Expand Down
Loading