How do I fix this grads script? - grads

This is my gs file.
'xloc = aminlocx(slp,lon=120,lon=135,lat=20,lat=30)'
'yloc = aminlocy(slp,lon=120,lon=135,lat=20,lat=30)'
'lonn = 39.58386 + 0.22522523*(xloc-1)'
'latt = -8.64826 + 0.22522523*(yloc-1)'
'lonmin = lonn-1'
'lonmax = lonn+1'
'latmin = latt-1'
'latmax = latt+1'
'q w2xy 'lonmin' 'latmin
xpos1=subwrd(result,3)
ypos1=subwrd(result,6)
'q w2xy 'lonmax' 'latmax
xpos2=subwrd(result,3)
ypos2=subwrd(result,6)
'set line 2 1 6'
'draw rec 'xpos1' 'ypos1' 'xpos2' 'ypos2
And I got error like this
Query Error: Syntax is QUERY W2XY Lon Lat
Query Error: Syntax is QUERY W2XY Lon Lat
DRAW error: Syntax is DRAW REC xlo ylo xhi yhi
How do I fix it?

It should be noted that there are two types of variables in the GrADS script. One is a script variable and the other is a GrADS variable. The former variable must be outside of the quotes and the latter is referenced inside the quotes.
For example, in the following line of your script
'q w2xy 'lonmin' 'latmin'
lonmin and latmin are treated as script variables.
On the other hand, in the following line
'xloc = aminlocx(cmsk,lon=120,lon=135,lat=20,lat=30)'
xloc is treated as a GrADS variable.
Now, if you want to get xloc as a script variable, you can write as the following.
'd aminlocx(cmsk,lon=120,lon=135,lat=20,lat=30)'
xloc = subwrd(result,4)
Alternatively, for contexts where display is not allowed, you can also write as the following.
'xloc = aminlocx(cmsk,lon=120,lon=135,lat=20,lat=30)'
'q defval xloc 1 1'
xloc = subwrd(result,3)
Finally, your script will be rewritten like this.
'd aminlocx(cmsk,lon=120,lon=135,lat=20,lat=30)'
xloc = subwrd(result,4)
'd aminlocy(cmsk,lon=120,lon=135,lat=20,lat=30)'
yloc = subwrd(result,4)
lonn = 120.0 + 0.22522523*(xloc-1)
latt = 20.0 + 0.22522523*(yloc-1)
lonmin = lonn-1
lonmax = lonn+1
latmin = latt-1
latmax = latt+1
'q w2xy 'lonmin' 'latmin
xpos1=subwrd(result,3)
ypos1=subwrd(result,6)
'q w2xy 'lonmax' 'latmax
xpos2=subwrd(result,3)
ypos2=subwrd(result,6)
'set line 2 1 6'
'draw rec 'xpos1' 'ypos1' 'xpos2' 'ypos2

Related

Unexplained Pinescript error shows when trying to parametrize a variable

This is the working version of the script:
//#version=5
indicator("compare MA", overlay= true)
var finallength = 0
Length1MA = input.int(defval = 10, title= "1 MA length")
Length2MA = input.int(defval = 20, title= "2 MA length")
sma1 = ta.sma(close, Length1MA)
sma2 = ta.sma(close, Length2MA)
var count1 = 0.0
var count2 = 0.0
for i = 1 to 10
if not na(sma1[i]) and not na(sma2[i])
count1 := count1 + math.abs(close[i] - sma1[i])
count2 := count2 + math.abs(close[i] - sma2[i])
if count1 > count2
finallength := Length1MA
else
finallength := Length2MA
plot(ta.sma(close, finallength), color= color.red, linewidth=2)
This is a different version that tries to parametrize the amount of Moving Averages, however there is an error i've been stuck to for a long time:
//#version=5
indicator("compare MA", overlay= true)
var finallength = 0
LengthsMA = input(defval=[10,20,30,40], title="MA lengths", type=input.integer)
counts = 0.0
for i = 1 to bar_index
for j = 0 to len(LengthsMA)-1
sma = ta.sma(close, LengthsMA[j])
if not na(sma[i])
counts[j] := counts[j] + math.abs(close[i] - sma[i])
finallength := LengthsMA[highest(counts)]
plot(ta.sma(close, finallength), color= color.red, linewidth=2)
you can run it to see the error first hand at line 12:
counts[j] = counts[j] + math.abs(close[i] - sma[i]) -> gives error -> Syntax error at input '=' (Error at 12:23)
changing it to := will not fix it! the goal is to use many Moving Averages and iterate through them all to find which is furthest from the price for the most time.
The goal is to use as many Moving Averages as needed without writing lines individually and cluttering the code with 20 else if statements, and iterate through them all to find which is furthest from the price.
the problem is this unexplainable error:
Syntax error at input '=' (Error at 12:23)
Unlike some other languages, [] in pinescript is not used for arrays. It is called history reference operator [] and is used to access historical data.
Obviously, you cannot re-write the history :)
So, doing something like
counts[j] := counts[j] + math.abs(close[i] - sma[i])
is not allowed.
If you want to work with arrays, you should use arrays.

Creating graph on ggplot2 with facte_wrap using multiple seperate variables

For my thesis I am using R-studio. I want to make a graph on ggplot2 with x= age(H2_lft) and y = IMT value (Mean_IMT_alg). I want to plot a graph with multiple variables(cardiovascular risk factors) to see the relationship between a certain variable/cardiovascular risk factor (e.g. smoking(H2_roken)/gender(H1_geslacht)/ethnicity(H1_EtnTotaal) and the IMT value on a certain age.
First, I plotted multiple lines (each line represented a variable) in a graph. But I think this is a little too messy. I actually want to have multiple 'pannels/graphs' with x= age and y = IMT value. And in every graph I want to have a different variable.
I hope my explanation is clear enough and someone can help me :)
My first code (multiple lines in same plot) is:
t <- ggplot(data = Dataset, aes(x = H2_lft, y = MeanIMT_alg)) +
geom_smooth(se = FALSE, aes(group = H1_EtnTotaal, colour = H1_EtnTotaal)) +
geom_smooth(se = FALSE, aes(group = H2_Roken, colour = H2_Roken)) +
geom_smooth(se = FALSE, aes(group = H1_geslacht, colour = H1_geslacht)) +
stat_smooth(method = lm, se=FALSE) +
theme_classic()
t + labs(x = "Age (years)", y = "Mean IMT (mm)", title ="IMT", caption = "Figure 2: mean IMT", color = "cardiovascular risk factors", fil = "cardiovascular risk factors")
To accomplish multiple panels i used 'facet_wrap'. The problem however is that when using 'groups' in facet_Wrap, R makes groups that proceed on each other. But i want the groups to be unrelated of eachother. For example: I want one graph with a line for Marroccan ethnicity, one line with current smoking and one line with Male participants. I do not want a graph with: morroccan women that currently smoke or: Dutch men that never smoked. So, I want the graph with all the lines but split into several graphs.
The code that I used to accomplish this is:
t <- ggplot(data = Dataset, aes(x = H2_lft, y = MeanIMT_alg)) +
geom_smooth(se = FALSE, aes(group = H1_EtnTotaal, colour = H1_EtnTotaal)) +
geom_smooth(se = FALSE, aes(group = H2_Roken, colour = H2_Roken)) +
geom_smooth(se = FALSE, aes(group = H1_geslacht, colour = H1_geslacht)) +
stat_smooth(method = lm, se=FALSE)+
facet_wrap(~H1_EtnTotaal + ~H2_Roken + ~H1_geslacht, scales = "free_y") +
theme_classic()
t + labs(x = "Age (years)", y = "Mean IMT (mm)", title ="IMT", caption = "Figure 2: mean IMT", color = "cardiovascular risk factors", fil = "cardiovascular risk factors")
I think it might be generally easier to reshape the data to a long format for plotting with ggplot2. If you want seperate legends for each of the categories, you can use the {ggnewscale} package to do so. Is this (approximately) what you're looking for?
library(ggnewscale)
library(ggplot2)
# Dummy data
Dataset <- data.frame(
H2_lft = runif(100, 18, 90),
MeanIMT_alg = rnorm(100),
H1_EtnTotaal = sample(LETTERS[1:5], 100, replace = TRUE),
H2_Roken = sample(LETTERS[6:8], 100, replace = TRUE),
H1_geslacht = sample(c("M", "F"), 100, replace = TRUE)
)
# Reshape data to long format
new <- tidyr::pivot_longer(Dataset, c(H1_EtnTotaal, H2_Roken, H1_geslacht))
ggplot(new, aes(H2_lft, MeanIMT_alg, group = value)) +
geom_smooth(
data = ~ subset(.x, name == "H1_EtnTotaal"),
aes(colour = value),
se = FALSE
) +
scale_colour_discrete(name = "EtnTotaal") +
new_scale_colour() +
geom_smooth(
data = ~ subset(.x, name == "H1_geslacht"),
aes(colour = value),
se = FALSE
) +
scale_colour_discrete(name = "geslacht") +
new_scale_colour() +
geom_smooth(
data = ~ subset(.x, name == "H2_Roken"),
aes(colour = value),
se = FALSE
) +
scale_colour_discrete(name = "Roken") +
geom_smooth(
method = lm, se = FALSE,
aes(group = NULL)
) +
facet_wrap(~ name)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
Created on 2022-11-04 by the reprex package (v2.0.0)

Getting the charge of a single atom, per loop in MD Analysis

I have been trying to use the partial charge of one particular ion to go through a calculation within mdanalysis.
I have tried(This is just a snippet from the code that I know is throwing the error):
Cl = u.select_atoms('resname CLA and prop z <= 79.14')
Lz = 79.14 #Determined from system set-up
Q_sum = 0
COM = 38.42979431152344 #Determined from VMD
file_object1 = open(fors, 'a')
print(dcd, file = file_object1)
for ts in u.trajectory[200:]:
frame = u.trajectory.frame
time = u.trajectory.time
for coord in Cl.positions:
q= Cl.total_charge(Cl.position[coord][2])
coords = coord - (Lz/COM)
q_prof = q * (coords + (Lz / 2)) / Lz
Q_sum = Q_sum + q_prof
print(q)
But I keep getting an error associated with this.
How would I go about selecting this particular atom as it goes through the loop to get the charge of it in MD Analysis? Before I was setting q to equal a constant and the code ran fine so I know it is only this line that is throwing the error:
q = Cl.total_charge(Cl.position[coord][2])
Thanks for the help!
I figured it out with:
def Q_code(dcd, topo):
Lz = u.dimensions[2]
Q_sum = 0
count = 0
CLAs = u.select_atoms('segid IONS or segid PROA or segid PROB or segid MEMB')
ini_frames = -200
n_frames = len(u.trajectory[ini_frames:])
for ts in u.trajectory[ini_frames:]:
count += 1
membrane = u.select_atoms('segid PROA or segid PROB or segid MEMB')
COM = membrane.atoms.center_of_mass()[2]
q_prof = CLAs.atoms.charges * (CLAs.positions[:,2] + (Lz/2 - COM))/Lz
Q_instant = np.sum(q_prof)
Q_sum += Q_instant
Q_av = Q_sum / n_frames
with open('Q_av.txt', 'a') as f:
print('The Q_av for {} is {}'.format(s, Q_av), file = f)
return Q_av

Create Dataframe name from 2 strings or variables pandas

i am extracting selected pages from a pdf file. and want to assign dataframe name based on the pages extracted:
file = "abc"
selected_pages = ['10','11'] #can be any combination eg ['6','14','20]
for i in selected_pages():
df{str(i)} = read_pdf(path + file + ".pdf",encoding = 'ISO-8859-1', stream = True,area = [100,10,740,950],pages= (i), index = False)
print (df{str(i)} )
The idea, ultimately, as in above example, is to have dataframes: df10, df11. I have tried "df" + str(i), "df" & str(i) & df{str(i)}. however all are giving error msg: SyntaxError: invalid syntax
Or any better way of doing it is most welcome. thanks
This is where a dictionary would be a much better option.
Also note the error you have at the start of the loop. selected_pages is a list, so you can't do selected_pages().
file = "abc"
selected_pages = ['10','11'] #can be any combination eg ['6','14','20]
df = {}
for i in selected_pages:
df[i] = read_pdf(path + file + ".pdf",encoding = 'ISO-8859-1', stream = True, area = [100,10,740,950], pages= (i), index = False)
i = int(i) - 1 # this will bring it to 10
dfB = df[str(i)]
#select row number to drop: 0:4
dfB.drop(dfB.index[0:4],axis =0, inplace = True)
dfB.columns = ['col1','col2','col3','col4','col5']

how to get the sum of alphabetical characters Shannon entropy

I am trying to add up all the Shannon entropy of all the alphabetical characters in a word document.
Instead of it adding the characters it gives me what I put for character(27) as an answer.
Dim characters(1 To 27) As Double
Dim x As Integer 'for looping
Dim tot As Double 'The final value
characters(1) = 0.1859 'Space
characters(2) = 0.0856 'A
characters(3) = 0.0139 'B
characters(4) = 0.0279 'C
characters(5) = 0.0378 'D
characters(6) = 0.1304 'E
characters(7) = 0.0289 'F
characters(8) = 0.0199 'G
characters(9) = 0.0528 'H
characters(10) = 0.0627 'I
characters(11) = 0.0013 'J
characters(12) = 0.0042 'K
characters(13) = 0.0339 'L
characters(14) = 0.0249 'M
characters(15) = 0.0707 'N
characters(16) = 0.0797 'O
characters(17) = 0.0199 'P
characters(18) = 0.0012 'Q
characters(19) = 0.0677 'R
characters(20) = 0.0607 'S
characters(21) = 0.1045 'T
characters(22) = 0.0249 'U
characters(23) = 0.0092 'V
characters(24) = 0.0149 'W
characters(25) = 0.0017 'X
characters(26) = 0.0199 'Y
characters(27) = 0.0008 'Z
For x = 1 To 27
tot = tol + characters(x)
Next
MsgBox "The Shannon entropy of the alphabetic characters in this document is " & tot
What I am getting
Today was a good day
The Shannon entropy of the characters in this document is 0.0008
What I am trying to get
Today was a good day
The Shannon entropy of the characters in this document is 1.2798542258337
I don't know if you have noticed that you wrote this:
For x = 1 To 27
tot = tol + characters(x)
Next
...while you might have wanted to write this:
For x = 1 To 27
tot = tot + characters(x)
Next
In fact, what you want is that tot is iteratively summing to itself a new value. But if you write
tot = tol + characters(x)
...what happens is that tol is always = 0 (because it doesn't preserve it's value and gets its 0 value by default) and so tot will be obviously equal to 0 + the last element because it does not keep its changes either. It's a typo, just change tot = tol + characters(x) with tot = tot + characters(x) and the code will work.