Difference between revisions of "Geopsy: Set header"

From GeopsyWiki
Jump to navigation Jump to search
Line 192: Line 192:
  
 
List files and directories in your home directory
 
List files and directories in your home directory
 +
  print(system("ls /home/username"));
 +
 +
=== Example 13: time arithmetic ===
 +
 +
Time specification is a string of characters with format 'YYYYMMddhhmmssz'. For example: 20200213123057.456, which stands for February 13th 2020 at 12:30:57.456.
 +
 +
Modify starting time of a signal
 +
  sig=signals[i];
 +
  sig.startTime=addSeconds(sig.startTime, 2.456);
  
   print(system("ls /home/username"));
+
Similar functions exist to add months and years: addMonths and addYears.
 +
 
 +
Get the time delay between two signal starting times
 +
   dt=elapsedSeconds(signals[i].startTime, signals[i+1].startTime);

Revision as of 13:46, 13 February 2020

This section explains how to modify header information stored in a database in a very efficient way. Any information about signals can be viewed in a table. Signals can be modified one by one by cell editing (Ctrl+K). For huge number of signals this is boring and time consuming. Instead, you can create small scripts and apply them with one single click. The syntax for these scripts is rather simple. They are written in Javascript like waveform script language.

General presentation

This help refers to the latest development release (geopsypack>=3.0.0)

Dialog box to editing header information

From any active signal viewer (which is by definition a list of signals), click on menu item Edit/Set headers. A dialog box will appear. You can type any script in the left editor. A basic loop to assign a new name to all signals is proposed as the default script. Comment or remove this script and enter your own code.

All statements are separated by ; (like in C or C++). If a line begins with //, all text is ignored until the end of the line (like C or C++ comments). In the same way, all text included between /* and */ is considered as a comment. Commenting parts of a script may be interesting to solve errors.

Upon starting the script, an array called signals contains the list of all signals of the current viewer. Each signal is indexed by its position in the current viewer, starting at index 0. For instance,

signals[i]

Refers to the (i+1)th signal in the current viewer. In a table, index are displayed on the left side starting at 1. Be careful that they start at 0 in array signals. The number of signals in the current viewer is given by expression

signals.length

Hence, the basic loop to assign values to signal headers will have a structure similar to this one:

for(i=0; i<signals.length; i++) {
  signals[i].samplingPeriod=0.01;
}

In this example, samplingPeriod is set to 0.01 seconds or a sampling frequency of 100 Hz. Available signal attributes are those listed here, replacing the first capital letter by its corresponding lower case:

ShortFileName becomes shortFileName 

print function might be useful to solve errors in scripts. Print output and errors are all directed to the main log message window.

for(i=0; i<signals.length; i++) {
  print("Signal index "+i+" had a sampling period of "+
        signals[i].samplingPeriod);
}

Which results into the following message in the log window:

[SetHeader]Signal index 0 had a sampling period of 0.004
[SetHeader]Signal index 1 had a sampling period of 0.004

To execute the current script click on Apply. The content of the editor can be saved or restored to a .headequ file (a simple text file in fact) by clicking on the button Save and Load respectively.

Using functions

Usually, you would like to keep several handy scripts easily accessible. You can write each one in a separate function and comment/uncomment the function to be run.

 function set_samplingFrequency(f)
 {
   for(i=0; i<signals.length; i++) {
     signals[i].samplingFrequency=f;
   }
 }
 function set_receiver(dx)
 {
   for(i=0; i<signals.length; i++) {
     signals[i].receiverX=i*dx;
   }
 }
 //set_samplingFrequency(100);
 set_receiver(2);

Trouble shooting

A common mistake is a typo error for signal property name. For instance

 signals[i].smplingFrequency=100;

neither produces the expected effect nor generates an error message. To check the effect of this statement, add prints:

 print("before smplingFrequency "+signals[i].smplingFrequency);
 signals[i].smplingFrequency=100;
 print("after smplingFrequency "+signals[i].smplingFrequency);

Note that instead of altering the SamplingFrequency, this script is simply adding a new property to the object signal[i].

Examples

Example 1: modify signal names

ShortFileName is the original file name without its complete path. The name of the signal is extracted from the ShortFileName.

for(i=0; i<signals.length; i++) {
  signals[i].name=signals[i].shortFileName.substring(3,8)
}

substring(s, e) returns the characters starting at index s (counting from 0) and stops just before index e.

Example 2: set sampling frequency to 100 Hz

Set sampling frequency to 100 Hz. Can be useful when loading signal from text files without header information. The sampling frequency is the minimum field required to display signals.

for(i=0; i<signals.length; i++) {
  signals[i].samplingFrequency=100;
}

Example 3: set components

Set the component from the signal file name. ShortFileName is the original file name without its complete path. We assume that the file name contains a keyword indicating the component.

for(i=0; i<signals.length; i++) {
  switch(signals[i].shortFileName.substring(12,13)) {
  case "E":
    signals[i].component="East";
    break;
  case "N":
    signals[i].component="North";
    break;
  case "Z":
    signals[i].component="Vertical";
    break;
  }
}

Example 4: set name from the right end of shorFileName

Set the signal name from shortFileName. ShortFileName is the original file name without its complete path. We assume that the file name ends with a file number written with 3 digits (i.e. typical files out of CityShark II).

for(i=0; i<signals.length; i++) {
  s=signals[i].shortFileName;
  signals[i].name=s.substring(s.length-3,s.length);
}

Example 5: set comments

The comments contains here the name of the array.

for(i=0; i<signals.length; i++) {
  signals[i].comments="RING01"
}

The application of scripts shown is the examples No 1, 2, 3 and 4 modify the headers as follow:

Modified headers after applying the equations shown in examples 1 to 4

Example 6: round T0 to sampling period

for(i=0; i<signals.length; i++) {
  f=signals[i].samplingFrequency;
  signals[i].t0=Math.round(signals[i].t0*f)/f;
}

Example 7: shift T0 by 3h20

for(i=0; i<signals.length; i++) {
  signals[i].t0+=3*3600+20*60;
}

One may also use this expression based on a more human format for time:

for(i=0; i<signals.length; i++) {
  signals[i].t0String+="3h20m";
}

Be careful, this expression works only for all positive times. The '+=" is actually performing a string concatenation of the current value and the new string. For instance, if t0 is '2h51m', the resulting t0 will be "2h51m3h20m" which converts well to "6h11m". To do arithmetic operations in general prefer the first expression.

Example 8: set T0 from time of TimeReference

This example can be used to fix signals imported by mistake without any reference to absolute time

for(i=0; i<signals.length; i++) {
  s=signals[i].timeReference;
  signals[i].t0String+=s.substring(s.length-8, s.length);
  signals[i].timeReference=s.substring(0, 10)+" 00:00:00";
}

Example 9: playing with time picks and comments

 for(i=0; i<signals.length; i++) {
   signals[i].setTimePick("Vp", 5.78)
   signals[i].comments="Vp set to "+signals[i].timePick("Vp")+" s";
 }

Example 10: debug helper

Print messages to the message console.

 for(i=0; i<signals.length; i++) {
   print("Vp set to "+signals[i].timePick("Vp")+" s");
 }

Example 11: right or left justification

Build a signal name with 0 padding.

 for(i=0; i<signals.length; i++) {
   signals[i].name="WA_WAU"+rightJustified(i+1, 2, "0");
 }

Example 12: start a system command

List files and directories in your home directory

 print(system("ls /home/username"));

Example 13: time arithmetic

Time specification is a string of characters with format 'YYYYMMddhhmmssz'. For example: 20200213123057.456, which stands for February 13th 2020 at 12:30:57.456.

Modify starting time of a signal

 sig=signals[i];
 sig.startTime=addSeconds(sig.startTime, 2.456);

Similar functions exist to add months and years: addMonths and addYears.

Get the time delay between two signal starting times

 dt=elapsedSeconds(signals[i].startTime, signals[i+1].startTime);