Skip to content

Instantly share code, notes, and snippets.

@vermorel
Last active April 4, 2016 18:23
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 vermorel/6d828dcf5feb5b3830c73b2e111f28e8 to your computer and use it in GitHub Desktop.
Save vermorel/6d828dcf5feb5b3830c73b2e111f28e8 to your computer and use it in GitHub Desktop.
Random expansion of monthly data into daily data
# Random expansion of monthly data into daily data
# Syntax: convert-daily "c:\LokadData\foo.tsv"
# Expected columns are Id, Date, Quantity
# By Joannes Vermorel, April 2016
function convert-daily
{
param
(
[string] $file
)
# looking for the output path
$path = [System.IO.Path]::GetDirectoryName($file);
$output = [System.IO.Path]::GetFileNameWithoutExtension($file);
$outext = [System.IO.Path]::GetExtension($file);
$stream = [System.IO.StreamWriter] ($path + "\"+ $output + "-daily" + $outext);
$r = ([random]42);
$isFirst = $true;
foreach ($line in [System.IO.File]::ReadLines($file))
{
if($isFirst)
{
$isFirst = $false;
$header = $line;
$stream.WriteLine($header);
}
else
{
$tokens = $line.Split("`t");
$id = $tokens[0];
$dt = [datetime] $tokens[1];
$qt = [int] $tokens[2];
for($i = $qt; $i -gt 0; $i--)
{
$dti = $dt.AddDays($r.Next(30));
$stream.WriteLine($id + "`t" + $dti.ToString("yyyy-MM-dd") + "`t1");
}
}
}
$stream.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment