Skip to content

Instantly share code, notes, and snippets.

@briochemc
Last active May 28, 2019 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briochemc/6543dc9530dcbd9f4b4ef40a5e52c151 to your computer and use it in GitHub Desktop.
Save briochemc/6543dc9530dcbd9f4b4ef40a5e52c151 to your computer and use it in GitHub Desktop.
For prafter resampling script in Julia
length(ARGS) == 1 && (sheet = "Sheet1")
# Use the installed packages
using ExcelFiles, DataFrames, Interpolations
# Replace by your file with just the data!
df = DataFrame(load(abspath(ARGS[1]), sheet))
println("Resampling $(ARGS[1]) - $sheet")
# function to do all the work
function resample(df)
# the dataframe to be filled with the resampled data
df_resampled = DataFrame(station = Int64[], calendar_age = Float64[], C14_age = Float64[])
# the stations
stations = unique(df[:station])
for s in stations # do the resampling for each station
df_station = filter(row -> row[:station] == s, df) # data for station s only
calendar_age = df_station[Symbol("calendar age")] # calendar age
C14_age = df_station[Symbol("benthic.14C_age")] # ¹⁴C age
min_year = 1000*ceil(minimum(calendar_age/1000)) # minimum of resampled calendar ages
max_year = 1000*floor(maximum(calendar_age/1000)) # maximum of resampled calendar ages
calendar_age_resampled = min_year:1000:max_year # resampled cal ages every 1000yr
etp = LinearInterpolation(calendar_age, C14_age) # interpolation object
for yr in calendar_age_resampled
push!(df_resampled, (s, yr, etp(yr))) # add (push) the resampled ¹⁴C age
end
end
return df_resampled # return the new table with resampled ¹⁴C age
end
# Resample and save in new file
out_file = ARGS[1][1:end-4] * "_resampled" # new file to be created
if isfile(out_file * ".xlsx") # ask if need to replace
println("$out_file.xlsx already exists. Replace it? [y/n]")
replace_it = readline()
if replace_it == "y"
rm(out_file * ".xlsx")
resample(df) |> save(out_file * ".xlsx")
else
println("Why did you run this script then?")
end
else
resample(df) |> save(out_file * ".xlsx")
end
# Run this only once to install these packages
using Pkg
Pkg.add("ExcelFiles") # to read/write xlsx files
Pkg.add("DataFrames") # to create tables in Julia (called DataFrames)
Pkg.add("Interpolations") # to interpolate the data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment