Extract information from specific lines - awk

In the below, the purpose is to extract information from specific lines, I output the desired file with my code, but i have small problem in the selection of.
/Live Seis/{s=s","substr($0,12,1500)}
I selected the range of 12-1500 fields, because in my code I select always the last field, but in this line this option does not work.
Is is possible to improve or change the code to other more efficient.
Input file
SCI TB Timestamp Local : 1/2/10 9:54:37.780 AM
File # : 5000
Is Raw : false
Shot # : 754
Swath Name : 1000
Tape # : 2240
Tape Label : X5
Tape Time : 5/4/18 9:55:01.619 AM
Comment : N/A
Line Name : 40559.0
Point Number : 38741.0
Point Index : 1
Easting (m) : 0
Northing (m) : 0
Elevation (m) : 0.0
Source Number : 6
Source ID : 1
SFL : 0
SFN : 0
Spread : 754
Process Type : 0
Nb of Aux : 4
Exploitation Mode : 0
Acq : 1
Correl Pilot : CorrelWith
Type of Test : N/A
Record Type : NORMAL
Acq Length (ms) : 0
Sweep Length (ms) : 0
Record Length (ms) : 0
SR : SR_2_MS
Filter Type : 0
SF : 1
Dump SF : 1
Live Seis : 40560:38466-39172/2(1-354) 40572:38466-39172/2(355-708) 40584:38466-39172/2(709-1062) 40596:38466-39172/2(1063-1416) 40608:38466-39172/2(1417-1770) 40620:38466-39172/2(1771-2124) 40632:38466-39172/2(2125-2478) 40644:38466-39172/2(2479-2832) 40656:38466-39172/2(2833-3186) 40668:38466-39172/2(3187-3540) 40680:38466-39172/2(3541-3894) 40692:38466-39172/2(3895-4248) 40704:38466-39172/2(4249-4602) 40716:38466-39172/2(4603-4956) 40728:38466-39172/2(4957-5310) 40740:38466-39172/2(5311-5664) 40752:38466-39172/2(5665-6018) 40764:38466-39172/2(6019-6372) 40776:38466-39172/2(6373-6726) 40788:38466-39172/2(6727-7080) 40800:38466-39172/2(7081-7434) 40812:38466-39172/2(7435-7788) 40824:38466-39172/2(7789-8142) 40836:38466-39172/2(8143-8496) 40848:38466-39172/2(8497-8850) 40860:38466-39172/2(8851-9204) 40872:38466-39172/2(9205-9558) 40884:38466-39172/2(9559-9912) 40896:38466-39172/2(9913-10266) 40908:38466-39172/2(10267-10620) 40920:38466-39172/2(10621-10974) 40932:38466-39172/2(10975-11328) 40944:38466-39172/2(11329-11682) 40956:38466-39172/2(11683-12036) 40968:38466-39172/2(12037-12390) 40980:38466-39172/2(12391-12744)
Live Aux : 1-2
Dead Seis : N/A
Missing Seis : N/A
Acq Error : N/A
ITB : N/A
CTB (?s) : N/A
Uphole Time (?s) : N/A
User Header : N/A
Jday : 124
Nb Of Seis : 12746
SCI TB Timestamp GPS Time : 1209452095780002
COG State : COG_ACTUAL
Deviation : 0.5
Cog Easting (m) : 0
Cog Northing (m) : 0
Cog Elevation (m) : 0
Traces Total : 00000
Nb Of Live Seis : 00000
Nb Of Live Aux : 2
Nb Of Dead Seis : 0
Nb Of Missing Seis : 0
Max Of Max Seis : 7.25919534678016E15
Hist Zeroing Length : N/A
________________________________________________________________________
SCI TB Timestamp Local : 2/1/10 9:15:51.580 PM
File # : 5001
Is Raw : false
Shot # : 50
Swath Name : 1000
Tape # : 2240
Tape Label : X5
Tape Time : 5/4/18 9:16:15.011 PM
Comment : N/A
Line Name : 40549.0
Point Number : 39053.0
Point Index : 1
Easting (m) : 0
Northing (m) : 0
Elevation (m) : 0.0
Source Number : 4
Source ID : 1
SFL : 0
SFN : 0
Spread : 50
Process Type : 0
Nb of Aux : 4
Exploitation Mode : 0
Acq : 1
Correl Pilot : 0
Type of Test : N/A
Record Type : NORMAL
Acq Length (ms) : 0
Sweep Length (ms) : 0
Record Length (ms) : 0
SR : SR_2_MS
Filter Type : 0
SF : 1
Dump SF : 1
Live Seis : 40560:38622-39484/2(1-432) 40572:38622-39484/2(433-864) 40584:38622-39484/2(865-1296) 40596:38622-39484/2(1297-1728) 40608:38622-39484/2(1729-2160) 40620:38622-39484/2(2161-2592) 40632:38622-39484/2(2593-3024) 40644:38622-39484/2(3025-3456) 40656:38622-39484/2(3457-3888) 40668:38622-39484/2(3889-4320) 40680:38622-39484/2(4321-4752) 40692:38622-39484/2(4753-5184) 40704:38622-39484/2(5185-5616) 40716:38622-39484/2(5617-6048) 40728:38622-39484/2(6049-6480) 40740:38622-39484/2(6481-6912) 40752:38622-39484/2(6913-7344) 40764:38622-39484/2(7345-7776) 40776:38622-39484/2(7777-8208) 40788:38622-39484/2(8209-8640) 40800:38622-39484/2(8641-9072) 40812:38622-39484/2(9073-9504) 40824:38622-39484/2(9505-9936) 40836:38622-39484/2(9937-10368) 40848:38622-39484/2(10369-10800) 40860:38622-39484/2(10801-11232) 40872:38622-39484/2(11233-11664) 40884:38622-39484/2(11665-12096) 40896:38622-39484/2(12097-12528) 40908:38622-39484/2(12529-12960) 40920:38622-39484/2(12961-13392) 40932:38622-39484/2(13393-13824) 40944:38622-39484/2(13825-14256) 40956:38622-39484/2(14257-14688) 40968:38622-39484/2(14689-15120) 40980:38622-39484/2(15121-15552)
Live Aux : 1-2
Dead Seis : N/A
Missing Seis : N/A
Acq Error : N/A
ITB : N/A
CTB (?s) : N/A
Uphole Time (?s) : N/A
User Header : N/A
Jday : 124
Nb Of Seis : 15554
SCI TB Timestamp GPS Time : 1209492969580002
COG State : COG_ACTUAL
Deviation : 3.0
Cog Easting (m) : 0
Cog Northing (m) : 0
Cog Elevation (m) : 178.4
Traces Total : 0000
Nb Of Live Seis : 0000
Nb Of Live Aux : 2
Nb Of Dead Seis : 0
Nb Of Missing Seis : 0
Max Of Max Seis : -1.13516329566208E14
Hist Zeroing Length : N/A
________________________________________________________________________
Output file
21,1000,2240,X5,40559.0,38741.0, 40560:38466-39172/2(1-354) 40572:38466-39172/2(355-708) 40584:38466-39172/2(709-1062) 40596:38466-39172/2(1063-1416) 40608:38466-39172/2(1417-1770) 40620:38466-39172/2(1771-2124) 40632:38466-39172/2(2125-2478) 40644:38466-39172/2(2479-2832) 40656:38466-39172/2(2833-3186) 40668:38466-39172/2(3187-3540) 40680:38466-39172/2(3541-3894) 40692:38466-39172/2(3895-4248) 40704:38466-39172/2(4249-4602) 40716:38466-39172/2(4603-4956) 40728:38466-39172/2(4957-5310) 40740:38466-39172/2(5311-5664) 40752:38466-39172/2(5665-6018) 40764:38466-39172/2(6019-6372) 40776:38466-39172/2(6373-6726) 40788:38466-39172/2(6727-7080) 40800:38466-39172/2(7081-7434) 40812:38466-39172/2(7435-7788) 40824:38466-39172/2(7789-8142) 40836:38466-39172/2(8143-8496) 40848:38466-39172/2(8497-8850) 40860:38466-39172/2(8851-9204) 40872:38466-39172/2(9205-9558) 40884:38466-39172/2(9559-9912) 40896:38466-39172/2(9913-10266) 40908:38466-39172/2(10267-10620) 40920:38466-39172/2(10621-10974) 40932:38466-39172/2(10975-11328) 40944:38466-39172/2(11329-11682) 40956:38466-39172/2(11683-12036) 40968:38466-39172/2(12037-12390) 40980:38466-39172/2(12391-12744) ,1209452095780002
1853,1000,2240,X5,40549.0,39053.0, 40560:38622-39484/2(1-432) 40572:38622-39484/2(433-864) 40584:38622-39484/2(865-1296) 40596:38622-39484/2(1297-1728) 40608:38622-39484/2(1729-2160) 40620:38622-39484/2(2161-2592) 40632:38622-39484/2(2593-3024) 40644:38622-39484/2(3025-3456) 40656:38622-39484/2(3457-3888) 40668:38622-39484/2(3889-4320) 40680:38622-39484/2(4321-4752) 40692:38622-39484/2(4753-5184) 40704:38622-39484/2(5185-5616) 40716:38622-39484/2(5617-6048) 40728:38622-39484/2(6049-6480) 40740:38622-39484/2(6481-6912) 40752:38622-39484/2(6913-7344) 40764:38622-39484/2(7345-7776) 40776:38622-39484/2(7777-8208) 40788:38622-39484/2(8209-8640) 40800:38622-39484/2(8641-9072) 40812:38622-39484/2(9073-9504) 40824:38622-39484/2(9505-9936) 40836:38622-39484/2(9937-10368) 40848:38622-39484/2(10369-10800) 40860:38622-39484/2(10801-11232) 40872:38622-39484/2(11233-11664) 40884:38622-39484/2(11665-12096) 40896:38622-39484/2(12097-12528) 40908:38622-39484/2(12529-12960) 40920:38622-39484/2(12961-13392) 40932:38622-39484/2(13393-13824) 40944:38622-39484/2(13825-14256) 40956:38622-39484/2(14257-14688) 40968:38622-39484/2(14689-15120) 40980:38622-39484/2(15121-15552) ,1209492969580002
My code
awk '/File/{if(s){print s;s=$NF+0}else{s=$NF+0}}
/Swath Name/{s=s","$NF}
/Tape #/{s=s","$NF}
/Tape Label /{s=s","$NF}
/Line Name/{s=s","$NF}
/Point Number/{s=s","$NF}
/Live Seis/{s=s","substr($0,12,1500)}
/SCI TB Timestamp GPS Time/{s=s","$NF}
END{print s}' file
Thanks in advance

Related

'NA' does not work with and 'AND' logical pine script

My script does not working as expected when I put a 'NA' variable in 'AND' logical.
// ON/OFF Indicator
// Buy Condition
MACDonoffbuy = useMACD ? macdLine > 0 : na
ADXonfoffbuy = useADX ? ADX > thradx : na
RSIonoffbuy = useRSI ? rsi >= overbought : na
Williamonoffbuy = useWilliam ? percentR >= -25 : na
Squeezeronoffbuy = useSqueezer ? sqzOff : na
// Sell Condition
MACDonoffsell = useMACD ? macdLine < 0 : na
ADXonfoffsell = useADX ? ADX > thradx : na
RSIonoffsell = useRSI ? rsi <= overbought : na
Williamonoffsell = useWilliam ? percentR <= -25 : na
Squeezeronoffsell = useSqueezer ? sqzOff : na
// Final Combination
buy = MACDonoffbuy and ADXonfoffbuy and RSIonoffbuy and Williamonoffbuy and Squeezeronoffbuy
sell = MACDonoffsell and ADXonfoffsell and RSIonoffsell and Williamonoffsell and Squeezeronoffsell
My expectation is if one of expression is 'NA' on that 'AND' logical, the other expression is still calculated.

Dynamic lower/upper bound of Pyomo Variable for every iteration

I want to adjust the lower and upper bound of three pyo.Var() depending on the outcome of another variable.
the variable model.inverter_power should be in the i-th iteration bigger than model.inverter_power[i].lb + model.fcr_power[i] but also smaller than model.inverter_power[i].ub - model.fcr_power[i])
How can i implement this? Unfortuntately my idea is not working....
def fcr_inverter_reduction(model, i):
return (model.inverter_power[i] >= model.inverter_power[i].lb + model.fcr_power[i],
model.inverter_power[i] <= model.inverter_power[i].ub - model.fcr_power[i])
model.fcr_inverter_rule = pyo.Constraint(model.i, rule = fcr_inverter_reduction)
I tried various versions of this code, not only is this code not linear anymore, so I used ipopt as a solver but no solution can be found, i got this error message:
File "D:\.conda\envs\PythonEnviromentV2\lib\site-packages\pyomo\opt\base\solvers.py", line 596, in solve
raise ApplicationError(
pyomo.common.errors.ApplicationError: Solver (ipopt) did not exit normally
This is very doable if you reformulate just a bit. Also, I don't think it is possible to return a tuple of 2 constraints like you are doing with that function, so you should break it up... it is clearer as well.
You probably could access the upper/lower bound and use that within the constraint because they are fixed/constant with respect to the solver, but I think it is probably clearer to break out your min/max values as parameters. Some variation of this works.
(also, in the future, you are more likely to get better help/results if you post a fully minimal-reproducible example instead of just 1-line.)
Code:
import pyomo.environ as pyo
model = pyo.ConcreteModel()
model.I = pyo.Set(initialize=[1,2,3])
# power parameters...
model.min_inverter = pyo.Param(model.I, initialize={1:10, 2:15, 3:22})
model.max_inverter = pyo.Param(model.I, initialize={1:55, 2:45, 3:80})
# vars...
model.inverter_power = pyo.Var(model.I)
model.fcr_power = pyo.Var(model.I)
def fcr_inverter_min(model, i):
return model.inverter_power[i] >= model.min_inverter[i] + model.fcr_power[i]
model.fcr_inverter_rule_min = pyo.Constraint(model.I, rule=fcr_inverter_min)
def fcr_inverter_max(model, i):
return model.inverter_power[i] <= model.max_inverter[i] - model.fcr_power[i]
model.fcr_inverter_rule_max = pyo.Constraint(model.I, rule=fcr_inverter_max)
model.pprint()
Output:
1 Set Declarations
I : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 3 : {1, 2, 3}
2 Param Declarations
max_inverter : Size=3, Index=I, Domain=Any, Default=None, Mutable=False
Key : Value
1 : 55
2 : 45
3 : 80
min_inverter : Size=3, Index=I, Domain=Any, Default=None, Mutable=False
Key : Value
1 : 10
2 : 15
3 : 22
2 Var Declarations
fcr_power : Size=3, Index=I
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 : None : None : None : False : True : Reals
2 : None : None : None : False : True : Reals
3 : None : None : None : False : True : Reals
inverter_power : Size=3, Index=I
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 : None : None : None : False : True : Reals
2 : None : None : None : False : True : Reals
3 : None : None : None : False : True : Reals
2 Constraint Declarations
fcr_inverter_rule_max : Size=3, Index=I, Active=True
Key : Lower : Body : Upper : Active
1 : -Inf : inverter_power[1] - (55 - fcr_power[1]) : 0.0 : True
2 : -Inf : inverter_power[2] - (45 - fcr_power[2]) : 0.0 : True
3 : -Inf : inverter_power[3] - (80 - fcr_power[3]) : 0.0 : True
fcr_inverter_rule_min : Size=3, Index=I, Active=True
Key : Lower : Body : Upper : Active
1 : -Inf : 10 + fcr_power[1] - inverter_power[1] : 0.0 : True
2 : -Inf : 15 + fcr_power[2] - inverter_power[2] : 0.0 : True
3 : -Inf : 22 + fcr_power[3] - inverter_power[3] : 0.0 : True

How to extract variables that equal a certain value in pyomo?

I am building a linear optimization model and trying to extract key information from my decision variable values. instance.x.display() displays the following sample
x : Size=121, Index=x_index
Key : Lower : Value : Upper : Fixed : Stale : Domain
(1, 1) : 0 : None : 1 : False : True : Binary
(1, 2) : 0 : 0.0 : 1 : False : False : Binary
(1, 3) : 0 : 0.0 : 1 : False : False : Binary
(1, 4) : 0 : 1.0 : 1 : False : False : Binary
(1, 5) : 0 : 0.0 : 1 : False : False : Binary
(1, 6) : 0 : 0.0 : 1 : False : False : Binary
(1, 7) : 0 : 0.0 : 1 : False : False : Binary
(1, 8) : 0 : 0.0 : 1 : False : False : Binary
(1, 9) : 0 : 0.0 : 1 : False : False : Binary
(1, 10) : 0 : 0.0 : 1 : False : False : Binary
(1, 11) : 0 : 0.0 : 1 : False : False : Binary
(2, 1) : 0 : 0.0 : 1 : False : False : Binary
(2, 2) : 0 : None : 1 : False : True : Binary
(2, 3) : 0 : 0.0 : 1 : False : False : Binary
(2, 4) : 0 : 0.0 : 1 : False : False : Binary
(2, 5) : 0 : 0.0 : 1 : False : False : Binary
I want to extract the values that are equal to 1, such as x(1,4) which is equal to 1.
I have tried the following code: instance.x.display(value(model.x[i,j] == 1)) which gives me the error message ValueError: Error retrieving component x[11,11]: The component has not been constructed. I am thinking that this is because the value for this is 'None' just like x(1,1) and x(2,2) above.
Any ideas on how to code this to display something like this:
(1,4) -- 1
(2,3) -- 1
(3,5) -- 1
(4,2) -- 1
(5,4) -- 1
(6,7) -- 1
You can extract all values to a more user-friendly format by:
x_dic = x.get_values()
for i in x_dic.keys():
if r[i]==1:
print(i)
I am sure there is a nicer way but this should do the trick :)
Cheers

is pyomo constraint with multiple indexes of one variable possible?

Is there a way to set multiple indexes to a set value within one constraint without having to type out the same variable for each indexed time. I have provided an example, imagine that you want to optimize when to charge your electric vehicle but you don't want it to charge at certain hours in a day. The example below works to avoid charging at the 4th and 5th hour. However, what if I want it to not charge for 15 hours of the day but don't feel like writing m.EVcharge[0]+m.EVcharge[1]+... putting m.EVcharge[:15] == 0 will not work because the constraints don't handle slices that well in pyomo.
def time_rule(m):
return m.EVcharge[4]+m.EVcharge[5] == 0
m.time_rule = Constraint(time, rule=time_rule)
Yes. There are a variety of ways to do this. You could make a subset of m.time and only pass that subset in to the constraint rule, which would constrain them to zero, or sum across the subset and make constrain it to zero (both assume negative charging is not possible.)
Or, you could do it more cleanly with data or a parameter that holds the limit for any arbitrary time block and use that, which keeps the data separate from the model, which is generally a good idea...
import pyomo.environ as pyo
# some background data on limits...
use_limit = { 0:3, # limited juice avial
1:3,
2:0, # no juice avail. :)
3:0}
m = pyo.ConcreteModel('EV Charge')
m.T = pyo.Set(initialize=range(6))
m.EV_charge = pyo.Var(m.T, domain=pyo.NonNegativeReals)
# Constraints
def charge_limit(m, time):
return m.EV_charge[time] <= use_limit[time]
m.C1 = pyo.Constraint(use_limit.keys(), rule=charge_limit)
m.pprint()
Yields:
2 Set Declarations
C1_index : Size=1, Index=None, Ordered=False
Key : Dimen : Domain : Size : Members
None : 1 : Any : 4 : {0, 1, 2, 3}
T : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 6 : {0, 1, 2, 3, 4, 5}
1 Var Declarations
EV_charge : Size=6, Index=T
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : 0 : None : None : False : True : NonNegativeReals
1 : 0 : None : None : False : True : NonNegativeReals
2 : 0 : None : None : False : True : NonNegativeReals
3 : 0 : None : None : False : True : NonNegativeReals
4 : 0 : None : None : False : True : NonNegativeReals
5 : 0 : None : None : False : True : NonNegativeReals
1 Constraint Declarations
C1 : Size=4, Index=C1_index, Active=True
Key : Lower : Body : Upper : Active
0 : -Inf : EV_charge[0] : 3.0 : True
1 : -Inf : EV_charge[1] : 3.0 : True
2 : -Inf : EV_charge[2] : 0.0 : True
3 : -Inf : EV_charge[3] : 0.0 : True

if statement causes type error elm

I want to update a record in Elm of type
type CubeData = {currDirection : Vec3, translation : Vec3, transform : Mat4}
Which I can use the following code to do.
updateCubeData : CubeData -> CubeData
updateCubeData cubeData = {cubeData | translation <- cubeData.currDirection,
transform <- translate cubeData.translation cubeData.transform}
However, I now want to update a field of the record differently depending on what the current value of the field is, so I tried doing this
updateCubeData : CubeData -> CubeData
updateCubeData cubeData = if abs cubeData.translation.x > 2.0 || abs cubeData.translation.y > 2.0
then {cubeData | translation <- cubeData.currDirection,
transform <- translate cubeData.translation cubeData.transform}
else {cubeData | currDirection <- negate cubeData.currDirection,
translation <- cubeData.currDirection,
transform <- translate cubeData.translation cubeData.transform}
This is a type error, which I've reproduced below. Note the type signature of updateCubeData is the 12th line of the file.
Type error on line 15, column 68 to 76:
cubeData
Expected Type: {a | y : Float, x : Float}
Actual Type: Vec3
Type error on line 18, column 68 to 76:
cubeData
Expected Type: {a | y : Float, x : Float}
Actual Type: Vec3
Type error between lines 13 and 18:
if | ((abs cubeData.translation.x) > 2.0) ||
((abs cubeData.translation.y) > 2.0) ->
{cubeData |
translation <- cubeData.currDirection,
transform <- translate cubeData.translation cubeData.transform}
| True ->
{cubeData |
currDirection <- negate cubeData.currDirection,
translation <- cubeData.currDirection,
transform <- translate cubeData.translation cubeData.transform}
Expected Type: Vec3
Actual Type: {a | y : Float, x : Float}
If I eliminate the if statement, and just set the function equal to the contents of the then and else the code compiles and runs as expected (if that block was the whole method). Why does adding an if statement as I have here introduce this type error, and how can I fix it?
The type error is coming from this part of your code:
abs cubeData.translation.x > 2.0 || abs cubeData.translation.y > 2.0
The compiler is complaining that cubeData.translation is of type Vec3 but you are treating it as though it is of type {a | y : Float, x : Float}. That is to say that Vec3 is not a record type containing the fields x and y.
I believe you actually want to use the getX and getY functions from Math.vector3:
abs (cubeData.translation |> getX) > 2.0 || abs (cubeData.translation |> getY) > 2.0