Difference between revisions of "Python example: editing HV results"
(Created page with "Import SciFigs module and create a blank sheet import GeopsyPySciFigs as sf s=sf.newSheet() Open the .page file saved from Geopsy H/V module s.fileOpen_2("hv-before-9.10...") |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | This script works only with GeopsyPack>=3.6 | ||
+ | |||
Import SciFigs module and create a blank sheet | Import SciFigs module and create a blank sheet | ||
import GeopsyPySciFigs as sf | import GeopsyPySciFigs as sf | ||
Line 4: | Line 6: | ||
Open the .page file saved from Geopsy H/V module | Open the .page file saved from Geopsy H/V module | ||
s.fileOpen_2("hv-before-9.10.page") | s.fileOpen_2("hv-before-9.10.page") | ||
− | Import a few Python functions to manipulate graphical objects. | + | Import a few Python functions to manipulate graphical objects. |
− | import hvedit | + | import [[Media:hvedit.py|hvedit]] |
+ | Change the color and the width of the average and stddev curves | ||
+ | hvedit.setRed(s) | ||
+ | Save the average and stddev layers | ||
+ | hvedit.saveLayers(s) | ||
+ | Open other results with the same stations, change the width of curves and compare the results | ||
+ | s.fileNew() | ||
+ | s.fileOpen_2("hv-after-9.10.page") | ||
+ | hvedit.setWidth(s) | ||
+ | hvedit.addLayer(s) | ||
+ | === Python code === | ||
+ | The legend retrieved from a ''LineLayer'' is just a local copy of the internal properties of the ''LineLayer''. Modifying the obtained legend has no effect before calling ''setLegend''. Childs and values of a legend are references through XML tags and attributes. To get a list of the available tags, untar a .legend file and inspect the file ''contents.xml''. | ||
+ | |||
def setWidth(s): | def setWidth(s): | ||
− | |||
n=s.numberOfObjects() | n=s.numberOfObjects() | ||
− | |||
for i in range(0, n): | for i in range(0, n): | ||
g=s.object_1(i) | g=s.object_1(i) | ||
Line 20: | Line 32: | ||
leg=l.legend() | leg=l.legend() | ||
pen=leg.child("LegendItem index='0'").child("Pen") | pen=leg.child("LegendItem index='0'").child("Pen") | ||
− | pen.setValue("width", | + | pen.setValue("width", 0.3) |
l.setLegend(leg) | l.setLegend(leg) | ||
# stddev | # stddev | ||
Line 27: | Line 39: | ||
leg=l.legend() | leg=l.legend() | ||
pen=leg.child("LegendItem index='0'").child("Pen") | pen=leg.child("LegendItem index='0'").child("Pen") | ||
− | pen.setValue("width", | + | pen.setValue("width", 0.3) |
pen=leg.child("LegendItem index='1'").child("Pen") | pen=leg.child("LegendItem index='1'").child("Pen") | ||
− | pen.setValue("width", | + | pen.setValue("width", 0.3) |
l.setLegend(leg) | l.setLegend(leg) | ||
def setRed(): | def setRed(): | ||
n=s.numberOfObjects() | n=s.numberOfObjects() | ||
− | |||
for i in range(0, n): | for i in range(0, n): | ||
g=s.object_1(i) | g=s.object_1(i) | ||
Line 44: | Line 55: | ||
pen=leg.child("LegendItem index='0'").child("Pen") | pen=leg.child("LegendItem index='0'").child("Pen") | ||
pen.setValue("color", '#FF0000') | pen.setValue("color", '#FF0000') | ||
− | pen.setValue("width", | + | pen.setValue("width", 0.3) |
l.setLegend(leg) | l.setLegend(leg) | ||
# stddev | # stddev | ||
Line 52: | Line 63: | ||
pen=leg.child("LegendItem index='0'").child("Pen") | pen=leg.child("LegendItem index='0'").child("Pen") | ||
pen.setValue("color", '#FF0000') | pen.setValue("color", '#FF0000') | ||
− | pen.setValue("width", | + | pen.setValue("width", 0.3) |
pen=leg.child("LegendItem index='1'").child("Pen") | pen=leg.child("LegendItem index='1'").child("Pen") | ||
pen.setValue("color", '#FF0000') | pen.setValue("color", '#FF0000') | ||
− | pen.setValue("width", | + | pen.setValue("width", 0.3) |
l.setLegend(leg) | l.setLegend(leg) | ||
+ | |||
+ | C++ supports overload of functions and operators. A function name may be defined several times with distinct argument number and types. Pyhton does not. To solve this issue, the Python interface renames the C++ functions appearing with the same name by adding a numbered suffix. To get the possible variants of function ''saveLayers'': | ||
+ | gc=s.object_1(0).object(1).graphContents() | ||
+ | help(gc) | ||
+ | Version ''_3'' accepts two arguments: the file name to save to and the name of the layer to save. Only one layer is saved unlike other variants that save all layers. | ||
def saveLayers(s): | def saveLayers(s): | ||
− | |||
n=s.numberOfObjects() | n=s.numberOfObjects() | ||
− | |||
for i in range(0, n): | for i in range(0, n): | ||
g=s.object_1(i) | g=s.object_1(i) | ||
Line 69: | Line 83: | ||
gc.saveLayers_3(g.objectName()+"-average.layer", "Average") | gc.saveLayers_3(g.objectName()+"-average.layer", "Average") | ||
gc.saveLayers_3(g.objectName()+"-stddev.layer", "Stddev") | gc.saveLayers_3(g.objectName()+"-stddev.layer", "Stddev") | ||
− | + | ||
def addLayer(s): | def addLayer(s): | ||
− | |||
n=s.numberOfObjects() | n=s.numberOfObjects() | ||
− | |||
for i in range(0, n): | for i in range(0, n): | ||
g=s.object_1(i) | g=s.object_1(i) |
Latest revision as of 09:12, 22 October 2024
This script works only with GeopsyPack>=3.6
Import SciFigs module and create a blank sheet
import GeopsyPySciFigs as sf s=sf.newSheet()
Open the .page file saved from Geopsy H/V module
s.fileOpen_2("hv-before-9.10.page")
Import a few Python functions to manipulate graphical objects.
import hvedit
Change the color and the width of the average and stddev curves
hvedit.setRed(s)
Save the average and stddev layers
hvedit.saveLayers(s)
Open other results with the same stations, change the width of curves and compare the results
s.fileNew() s.fileOpen_2("hv-after-9.10.page") hvedit.setWidth(s) hvedit.addLayer(s)
Python code
The legend retrieved from a LineLayer is just a local copy of the internal properties of the LineLayer. Modifying the obtained legend has no effect before calling setLegend. Childs and values of a legend are references through XML tags and attributes. To get a list of the available tags, untar a .legend file and inspect the file contents.xml.
def setWidth(s): n=s.numberOfObjects() for i in range(0, n): g=s.object_1(i) print(g.objectName()) # Average l=g.object(1).graphContents().layer(3) if l: leg=l.legend() pen=leg.child("LegendItem index='0'").child("Pen") pen.setValue("width", 0.3) l.setLegend(leg) # stddev l=g.object(1).graphContents().layer(4) if l: leg=l.legend() pen=leg.child("LegendItem index='0'").child("Pen") pen.setValue("width", 0.3) pen=leg.child("LegendItem index='1'").child("Pen") pen.setValue("width", 0.3) l.setLegend(leg)
def setRed(): n=s.numberOfObjects() for i in range(0, n): g=s.object_1(i) print(g.objectName()) # Average l=g.object(1).graphContents().layer(3) if l: leg=l.legend() pen=leg.child("LegendItem index='0'").child("Pen") pen.setValue("color", '#FF0000') pen.setValue("width", 0.3) l.setLegend(leg) # stddev l=g.object(1).graphContents().layer(4) if l: leg=l.legend() pen=leg.child("LegendItem index='0'").child("Pen") pen.setValue("color", '#FF0000') pen.setValue("width", 0.3) pen=leg.child("LegendItem index='1'").child("Pen") pen.setValue("color", '#FF0000') pen.setValue("width", 0.3) l.setLegend(leg)
C++ supports overload of functions and operators. A function name may be defined several times with distinct argument number and types. Pyhton does not. To solve this issue, the Python interface renames the C++ functions appearing with the same name by adding a numbered suffix. To get the possible variants of function saveLayers:
gc=s.object_1(0).object(1).graphContents() help(gc)
Version _3 accepts two arguments: the file name to save to and the name of the layer to save. Only one layer is saved unlike other variants that save all layers.
def saveLayers(s): n=s.numberOfObjects() for i in range(0, n): g=s.object_1(i) print(g.objectName()) gc=g.object(1).graphContents() if gc: gc.saveLayers_3(g.objectName()+"-average.layer", "Average") gc.saveLayers_3(g.objectName()+"-stddev.layer", "Stddev")
def addLayer(s): n=s.numberOfObjects() for i in range(0, n): g=s.object_1(i) print(g.objectName()) gc=g.object(1).graphContents() if gc: gc.insertLayers(g.objectName()+"-average.layer", "Stddev") gc.insertLayers(g.objectName()+"-stddev.layer", "Stddev")