[GRASS-dev] Avoid the addition of @mymapset in output map name

Hi all!
I’m working on the GUI of my module for clouds and shadows detection in Sentinel 2 images (GSoC 2018 project) and I have some trouble with the automatic addition of the mapset name in the output maps.
I have several input bands imported in GRASS that I convert into float value and rescale using r.mapcalc, each output map should be automatically named with the inputs band name and a suffix (e.g. input: nir - output: nir_float).
When I run the script using the GUI I have an error due to the @mapset automatically added to the output map name (e.g. nir@mymapset_float).

At the moment, I solved the problem with a replace() function but I’m not sure this can be the best solution, do you have any hint?

Following you can find the code:

mapset = gscript.gisenv()[‘MAPSET’]
mapset2 = ‘@%s’ % mapset

b1 = options[‘blu’]
b1 = b1.replace(mapset2, “”)
b2 = options[‘green’]
b2 = b2.replace(mapset2, “”)
b3 = options[‘red’]
b3 = b3.replace(mapset2, “”)
b4 = options[‘nir’]
b4 = b4.replace(mapset2, “”)
b5 = options[‘nir8a’]
b5 = b5.replace(mapset2, “”)
b7 = options[‘swir11’]
b7 = b7.replace(mapset2, “”)
b8 = options[‘swir12’]
b8 = b8.replace(mapset2, “”)
f = ‘float’
bands = [b1, b2, b3, b4, b5, b7, b8]

for b in bands:
gscript.mapcalc(‘{r} = 1.0 * ({a})/{c}’.format(r=“%s_%s” % (b, f), a=b, c=scale_fac), overwrite=True)
f_bands.append (“%s_%s” % (b, f))

Thanks in advance,
Roberta

Hi Roberta,

You can try:

b1 = b1.split(‘@’)[0]

Cheers

Stefan

···

Hi all!

I’m working on the GUI of my module for clouds and shadows detection in Sentinel 2 images (GSoC 2018 project) and I have some trouble with the automatic addition of the mapset name in the output maps.

I have several input bands imported in GRASS that I convert into float value and rescale using r.mapcalc, each output map should be automatically named with the inputs band name and a suffix (e.g. input: nir - output: nir_float).

When I run the script using the GUI I have an error due to the @mapset automatically added to the output map name (e.g. nir@mymapset_float).

At the moment, I solved the problem with a replace() function but I’m not sure this can be the best solution, do you have any hint?

Following you can find the code:

mapset = gscript.gisenv()[‘MAPSET’]

mapset2 = ‘@%s’ % mapset

b1 = options[‘blu’]

b1 = b1.replace(mapset2, “”)

b2 = options[‘green’]

b2 = b2.replace(mapset2, “”)

b3 = options[‘red’]

b3 = b3.replace(mapset2, “”)

b4 = options[‘nir’]

b4 = b4.replace(mapset2, “”)

b5 = options[‘nir8a’]

b5 = b5.replace(mapset2, “”)

b7 = options[‘swir11’]

b7 = b7.replace(mapset2, “”)

b8 = options[‘swir12’]

b8 = b8.replace(mapset2, “”)

f = ‘float’

bands = [b1, b2, b3, b4, b5, b7, b8]

for b in bands:

gscript.mapcalc(‘{r} = 1.0 * ({a})/{c}’.format(r=“%s_%s” % (b, f), a=b, c=scale_fac), overwrite=True)

f_bands.append (“%s_%s” % (b, f))

Thanks in advance,

Roberta

Hi Roberta,

Le Tue, 29 May 2018 13:16:48 +0200,
Roberta Fagandini <robifagandini@gmail.com> a écrit :

Hi all!
I'm working on the GUI of my module for clouds and shadows detection
in Sentinel 2 images (GSoC 2018 project) and I have some trouble
with the automatic addition of the mapset name in the output maps.
I have several input bands imported in GRASS that I convert into float
value and rescale using r.mapcalc, each output map should be
automatically named with the inputs band name and a suffix (e.g.
input: nir - output: nir_float).
When I run the script using the GUI I have an error due to the @mapset
automatically added to the output map name (e.g. nir@mymapset_float).

At the moment, I solved the problem with a replace() function but I'm
not sure this can be the best solution, do you have any hint?

A classic solution that you will find in many scripts is using split():

mapname_without_mapset = mapname_with_mapset.split('@')[0]

Moritz

Hi Roberta,

On Tue, May 29, 2018 at 1:41 PM, Moritz Lennert <mlennert@club.worldonline.be> wrote:

Hi Roberta,

Le Tue, 29 May 2018 13:16:48 +0200,
Roberta Fagandini <robifagandini@gmail.com> a écrit :

Hi all!
I’m working on the GUI of my module for clouds and shadows detection
in Sentinel 2 images (GSoC 2018 project) and I have some trouble
with the automatic addition of the mapset name in the output maps.
I have several input bands imported in GRASS that I convert into float
value and rescale using r.mapcalc, each output map should be
automatically named with the inputs band name and a suffix (e.g.
input: nir - output: nir_float).
When I run the script using the GUI I have an error due to the @mapset
automatically added to the output map name (e.g. nir@mymapset_float).

At the moment, I solved the problem with a replace() function but I’m
not sure this can be the best solution, do you have any hint?

A classic solution that you will find in many scripts is using split():

mapname_without_mapset = mapname_with_mapset.split(‘@’)[0]

there is find_file() in lib/python/script/core.py with returns a dictionary of keys/values including the name without mapset

Markus M

Moritz


grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev

2018-05-29 14:09 GMT+02:00 Markus Metz <markus.metz.giswork@gmail.com>:

Hi Roberta,

On Tue, May 29, 2018 at 1:41 PM, Moritz Lennert <
mlennert@club.worldonline.be> wrote:
>
> Hi Roberta,
>
> Le Tue, 29 May 2018 13:16:48 +0200,
> Roberta Fagandini <robifagandini@gmail.com> a écrit :
>
> > Hi all!
> > I'm working on the GUI of my module for clouds and shadows detection
> > in Sentinel 2 images (GSoC 2018 project) and I have some trouble
> > with the automatic addition of the mapset name in the output maps.
> > I have several input bands imported in GRASS that I convert into float
> > value and rescale using r.mapcalc, each output map should be
> > automatically named with the inputs band name and a suffix (e.g.
> > input: nir - output: nir_float).
> > When I run the script using the GUI I have an error due to the @mapset
> > automatically added to the output map name (e.g. nir@mymapset_float).
> >
> > At the moment, I solved the problem with a replace() function but I'm
> > not sure this can be the best solution, do you have any hint?
>
> A classic solution that you will find in many scripts is using split():
>
> mapname_without_mapset = mapname_with_mapset.split('@')[0]

there is find_file() in lib/python/script/core.py with returns a
dictionary of keys/values including the name without mapset

Markus M

With find_file it should work also for maps in different mapset.
Roberta can you test it?

R

>
> Moritz
> _______________________________________________
> grass-dev mailing list
> grass-dev@lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/grass-dev

_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev

2018-05-31 15:08 GMT+02:00 Roberto Marzocchi <roberto.marzocchi@gmail.com>:

2018-05-29 14:09 GMT+02:00 Markus Metz <markus.metz.giswork@gmail.com>:

Hi Roberta,

On Tue, May 29, 2018 at 1:41 PM, Moritz Lennert <
mlennert@club.worldonline.be> wrote:
>
> Hi Roberta,
>
> Le Tue, 29 May 2018 13:16:48 +0200,
> Roberta Fagandini <robifagandini@gmail.com> a écrit :
>
> > Hi all!
> > I'm working on the GUI of my module for clouds and shadows detection
> > in Sentinel 2 images (GSoC 2018 project) and I have some trouble
> > with the automatic addition of the mapset name in the output maps.
> > I have several input bands imported in GRASS that I convert into float
> > value and rescale using r.mapcalc, each output map should be
> > automatically named with the inputs band name and a suffix (e.g.
> > input: nir - output: nir_float).
> > When I run the script using the GUI I have an error due to the @mapset
> > automatically added to the output map name (e.g. nir@mymapset_float).
> >
> > At the moment, I solved the problem with a replace() function but I'm
> > not sure this can be the best solution, do you have any hint?
>
> A classic solution that you will find in many scripts is using split():
>
> mapname_without_mapset = mapname_with_mapset.split('@')[0]

there is find_file() in lib/python/script/core.py with returns a
dictionary of keys/values including the name without mapset

Markus M

With find_file it should work also for maps in different mapset.
Roberta can you test it?

R

Yes, it works!
Thank you all for your help!

Roberta

>
> Moritz
> _______________________________________________
> grass-dev mailing list
> grass-dev@lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/grass-dev

_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev