December 19, 2014

Interesting NASA Temperature Data

Comparison of NASA Temperature Data
This image shows a comparison of temperature difference data for the US that should be the same data set. The purple thin line is the archived data from web.archive.org which was archived May 7, 2001. The thin red line is the live data taken on December 19, 2014. The thicker lines are the 5-year means plotted against the middle year (e.g. 1971 - 1975 plotted at 1973). The dotted lines are second order polynomial fit curves to each raw data set.
Based on what I see, someone is modifying historical data. In general temperatures prior to 1970 in the 2014 data set are lower than those same years in the 2001 data set while temperatures after 1970 from the 2014 data set are higher than those same years in the 2001 data set. The effect of that data is dramatically illustrated by the fit curves, the early data suggests a leveling off while the later data set suggests a run away system.
Year2001 Snapshot2014 Live5-Year Mean (2001)5-Year Mean (2014)
1880-0.41-0.340.13 
18810.15-0.23-0.14 
1882-0.04-0.25-0.34-0.31
1883-0.7-0.31-0.36-0.31
1884-0.73-0.42-0.44-0.33
1885-0.5-0.35-0.48-0.35
1886-0.25-0.34-0.39-0.33
1887-0.21-0.34-0.19-0.27
1888-0.28-0.2-0.05-0.27
18890.28-0.12-0.04-0.26
18900.23-0.36-0.1-0.26
1891-0.24-0.27-0.17-0.31
1892-0.47-0.37-0.21-0.36
1893-0.66-0.42-0.39-0.34
18940.11-0.36-0.31-0.33
1895-0.69-0.28-0.24-0.30
18960.17-0.23-0.14-0.27
1897-0.12-0.19-0.25-0.24
1898-0.17-0.310-0.21
1899-0.43-0.2-0.02-0.19
19000.54-0.1-0.01-0.22
19010.07-0.13-0.11-0.22
1902-0.09-0.34-0.11-0.27
1903-0.65-0.34-0.31-0.31
1904-0.41-0.43-0.34-0.32
1905-0.47-0.29-0.37-0.35
1906-0.06-0.22-0.21-0.37
1907-0.22-0.48-0.18-0.38
19080.11-0.45-0.02-0.41
1909-0.25-0.450.01-0.45
19100.31-0.45-0.12-0.45
19110.11-0.4-0.17-0.45
1912-0.89-0.49-0.11-0.41
1913-0.13-0.47-0.21-0.35
19140.03-0.23-0.33-0.34
1915-0.16-0.14-0.36-0.35
1916-0.51-0.37-0.32-0.33
1917-1-0.52-0.36-0.35
19180.02-0.37-0.42-0.37
1919-0.15-0.34-0.1-0.32
1920-0.45-0.270.12-0.27
19211.08-0.10.1-0.23
19220.11-0.25-0.01-0.19
1923-0.09-0.190.15-0.15
1924-0.7-0.13-0.05-0.13
19250.38-0.1-0.04-0.09
19260.040.04-0.01-0.07
19270.16-0.090.02-0.09
19280.05-0.05-0.03-0.06
1929-0.54-0.270.16-0.05
19300.110.050.12-0.03
193110.090.24-0.06
1932-0.010.030.60.00
19330.66-0.210.58-0.02
19341.240.040.42-0.04
19350.05-0.040.4-0.01
19360.1800.430.08
1937-0.120.180.340.10
19380.780.220.340.13
19390.80.130.410.16
19400.040.140.450.15
19410.540.130.320.14
19420.070.110.180.16
19430.160.170.170.14
19440.090.240.20.11
1945-0.010.040.20.11
19460.670.010.150.07
19470.090.070.170.02
1948-0.08-0.010.13-0.03
19490.18-0.03-0.08-0.02
1950-0.23-0.17-0.04-0.02
1951-0.380.060.150.03
19520.30.060.280.03
19530.880.230.310.05
19540.82-0.030.44-0.01
1955-0.05-0.090.41-0.02
19560.28-0.240.25-0.03
19570.140.040.12-0.01
19580.070.150.090.02
19590.160.10.030.09
1960-0.220.0700.12
196100.090.030.12
196200.17-0.030.06
19630.190.17-0.010.02
1964-0.1-0.2-0.050.00
1965-0.13-0.13-0.07-0.03
1966-0.220-0.17-0.07
1967-0.080.03-0.19-0.03
1968-0.3-0.04-0.19-0.01
1969-0.21-0.01-0.17-0.04
1970-0.13-0.03-0.22-0.08
1971-0.11-0.13-0.11-0.05
1972-0.32-0.17-0.03-0.08
19730.230.11-0.05-0.09
19740.18-0.2-0.08-0.11
1975-0.22-0.050.05-0.05
1976-0.26-0.23-0.09-0.08
19770.350.1-0.24-0.03
1978-0.49-0.01-0.150.01
1979-0.580.040.030.12
19800.230.13-0.110.11
19810.640.35-0.020.15
1982-0.350.020.10.15
1983-0.030.23-0.030.12
19840.020.01-0.010.07
1985-0.4-0.030.210.11
19860.710.10.280.13
19870.760.230.250.18
19880.320.330.50.28
1989-0.150.250.490.33
19900.850.480.40.31
19910.650.380.250.28
19920.30.10.380.30
1993-0.40.170.280.31
19940.480.350.120.29
19950.360.570.070.38
1996-0.140.270.390.49
19970.050.540.490.52
19981.20.730.54
19990.950.510.62
20010.640.66
20020.70.64
20030.710.70
20040.650.73
20050.810.75
20060.760.73
20070.810.73
20080.620.74
20090.670.72
20100.840.71
20110.680.73
20120.73 
20130.72 

October 8, 2013

Bitfields in Managed Code

What are Bitfields?

If you're asking this question, then you probably should stay away, but I'll answer anyway. A bitfield is a method in C/C++ to map sub-components of a structure that is stored in a primitive type. For example, the LTC6946 synthesizer has several registers each of which is contained in a single byte of data.


ADDRMSB[6][5][4][3][2][1]LSBR/WDEFAULT
h08BSTFILTRFOODR/WhF9
The common method to deal with this register structure is to use bit ANDs and shifts. Ultimately the code is easier to read if we could use something like:
register.bst = true;

C/C++ has a way to make this happen, and at the same time it ensures that limits are set, that is I can't set register.filt to more than 3.
 
struct LTC6946_ReferenceOutput
{
   unsigned out_div : 3;       // Default 0x1 | Output divider value
   unsigned rf_out_pwr : 2;    // Default 0x3 | Output Power
   unsigned ref_buff_filt : 2; // Default 0x3 | Reference input buffer filter
   bool ref_buff_boost : 1;    // Default true | Reference input boost current
};

Now, endianess matters here, so structure the code for the endianess of the system this will run on. Typically managed code will run on a little endian machine so we start with the low order parts of the register.

Adding in a Union

This part is not strictly necessary, but it makes reading and writing to the device much simpler. A union in C/C++ allows two datatypes to occupy the same memory space. In the case of a register it allows us to get access to the underlying value without a bunch of pointer work.

template<typename regType>
union Reg32
{
UInt32 val;
regType reg;
};

template<typename regType>
union Reg16
{
UInt16 val;
regType reg;
};

template<typename regType>
union Reg8
{
Byte val;
regType reg;
};

To use this with the LTC6946_ReferenceOutputStructure we declare a variable of the type:
Reg8<LTC6946_ReferenceOutput>

Managed Code

If you try to use this structure in managed code it won't work. In managed C++ you'll get an error like "error C4368: cannot define 'register' as a member of managed...". The trick is in managed C++ you can use a pointer to the structure. Now we can make this work, but it takes a bit more code than straight C/C++.

Creating the Basics

public ref class LTC6946RefOut
{
public:
LTC6946RefOut() 
{
m_fields = new Reg8<LTC6946_ReferenceOutput>();
}

property unsigned out_div
{
unsigned get() { return m_fields->reg.out_div; }
void set(unsigned value) { m_fields->reg.out_div = value; }
};

property unsigned rf_out_pwr
{
unsigned get() { return m_fields->reg.rf_out_pwr; }
void set(unsigned value) { m_fields->reg.rf_out_pwr = value; }
};

property unsigned ref_buff_filt
{
unsigned get() { return m_fields->reg.ref_buff_filt; }
void set(unsigned value) { m_fields->reg.ref_buff_filt = value; }
};

property bool ref_buff_boost
{
bool get() { return m_fields->reg.ref_buff_boost; }
void set(bool value) { m_fields->reg.ref_buff_boost = value; }
};

property Byte val
{
Byte get() { return m_fields->val; }
void set(Byte value) { m_fields->val = value; }
}

 virtual String^ ToString() override
 {
  return m_fields->val.ToString();
 }

private:
Reg8<LTC6946_ReferenceOutput> *m_fields;
};

This will allow the bitfield to be used in managed code including C# and Visual Basic. There are a few clean up tasks yet to do before the code is complete.

Garbage Collection

To get garbage collection to work properly we need to add the destructor and finalizer methods. Each method should delete the m_fields pointer.
!LTC6946RefOut() 
{
delete m_fields;
}
~LTC6946RefOut() 
{
!LTC6946RefOut();
}

By adding these to the class the managed garbage collection will properly clean up the unmanaged code.

Abstraction

There is a fair amount of code here that can be tedious to re-type. The individual properties that map to fields in the bitfield cannot be abstracted away, but the rest can.
template<typename regType>
public ref class CBitField8 abstract
{
public:
CBitField8()
{
m_fields = new Reg8<regType>();
}

!CBitField8()
{
delete m_fields;
}

~CBitField8()
{
!CBitField8();
}

property Byte val
{
Byte get() { return m_fields->val; }
void set(Byte value) { m_fields->val = value; }
}

virtual String^ ToString() override
{
return m_fields->val.ToString();
}

protected:
Reg8<regType> *m_fields;
};

Repeat for 16 and 32 bit bitfields.

Final Form

After the abstraction is in place our initial class is simplified to:
public ref class LTC6946RefOut : CBitField8<LTC6946_ReferenceOutput>
{
public:
LTC6946RefOut() : CBitField8()
{
}

property unsigned out_div
{
unsigned get() { return m_fields->reg.out_div; }
void set(unsigned value) { m_fields->reg.out_div = value; }
};

property unsigned rf_out_pwr
{
unsigned get() { return m_fields->reg.rf_out_pwr; }
void set(unsigned value) { m_fields->reg.rf_out_pwr = value; }
};

property unsigned ref_buff_filt
{
unsigned get() { return m_fields->reg.ref_buff_filt; }
void set(unsigned value) { m_fields->reg.ref_buff_filt = value; }
};

property bool ref_buff_boost
{
bool get() { return m_fields->reg.ref_buff_boost; }
void set(bool value) { m_fields->reg.ref_buff_boost = value; }
};
};

September 11, 2013

ChromeOS in VMWare Player

Background

Recently my kids have all gotten Chromebooks at school. The high school students bring their Chromebooks home which means I get to play a little with them. Since the Chromebook is the property of the school system I can't actually do much to them, and even the little I can do scares my wife.
So instead I did what all sane people do, I looked around to find out how I can run ChromeOS in a virtual machine.

Available Options

It turns out there isn't really much in the way of a straight up ChromeOS virtual machine. Google doesn't seem to be interested in that market. There are some ChromiumOS virtual machines but they don't integrate as well.
I ran across Jay Lee's post which contains some instructions on converting his supplied VMWare image into a real ChromeOS virtual machine. Pretty much the instructions show you how to run his script which downloads a recovery image from Google onto the virtual machine. The problem is the recent recovery images are bigger than the partition space in his virtual machine. I could likely move and resize partitions to get it to work, but that seemed too likely to fail.
Hexxeh, has made some very good ChromiumOS builds that can install on a real machine, and he has made VMWare and VirtualBox images as well. But these are ChromiumOS which aren't was tightly integrated into the Google ecosystem, plus they don't have flash and a few other things that are only distributed with the ChromeOS.

Creating a ChromeOS Virtual Machine

These instructions are for VMWare Player, but other than the initial changes to the virtual machine, the rest of the instructions are specific to the "guest" machine and have no dependency on the "host" virtualization software. In other words, the VirtualBox image will probably work just as well.

Downloading the Virtual Machine

Head on over to Hexxeh's Chromium OS Builds page. And download the VMWare image of the latest Vanilla build.

Extract the ZIP File

Extract the ZIP file to the location where you want your virtual machine to reside.
The extracted files will be the virtual machine definition (.vmx) and the virtual machine disk (.vmdk). Pay attention to the name of the virtual machine disk file, you will need it in the next step.

Fix the Virtual Machine

Using your favorite text editor, open the .vmx file. One line has to be changed and one line has to be added.

Edit the disk image name.


Find the ide0:0.fileName line, and change the "ChromeOS-Vanilla-VMWare.vmdk" to the name of the .vmdk file extracted from Hexxeh's ZIP file.

Add an Ethernet definition.


Add the following line anywhere in the .vmx file.
ethernet0.virtualDev = "e1000"
Save and close the .vmx file.

Mac Notes

In the following steps you will need to press the Control+Option+F2 and Control+Option+F1 keys. On most Mac keyboards this will require you to use the Fn key as well. It may be helpful to go into System Preferences -> Keyboard and enable the "Use all F1, F2, etc. keys as standard functions keys" option.

Boot the Virtual Machine

You can boot the virtual machine by double clicking on the .vmx file. This will boot into the ChromiumOS, not our final ChromeOS. Make sure that Ethernet is selected in the "Select a network" drop down box, then click "Continue"

Sign Into Chromium

You will have to sign into Chromium using a valid Google account. The guest account cannot make the changes we want to make.
Just sign in as though you were on a web page. Two step verification is also supported, use it.

Logged Into Chromium

Now you should see the pretty Chromium desktop.

Enable Developer Mode

Enabling developer mode is pretty easy, but there are a number of ways found in web searches that do not work.

Go into settings:


Click on the Help menu


Click on the "More info..." link


Under the Channel drop down, select the "Dev - Unstable" option

Prepare to Get the Recovery Image

From the virtual machine press Ctrl+Alt+F2 (or Control+Option+F2 on Mac) to switch to the text console. (You can use Ctrl+Alt+F1/Control+Option+F1 to switch back.)
The username is "chronos", the password is currently "facepunch". Alternate passwords could be "chronos", "password" or nothing.
Once you have successfully logged in, run "sudo bash" to become root. The password will be the same password used to login above.
As root run:
touch /mnt/stateful_partition/.developer_mode # may not be needed
cd /tmp # to get to a read/write partition
wget http://goo.gl/4suhf # to download the script
bash 4suhf # to run the script

An alternate script can be found at:
http://cr-48-ubuntu.googlecode.com/files/overwrite-chromiumos-with-chromeos.sh
This should fix the problem for people experiencing the unexpected token 'newline' error.

Select the Recovery Image 


Most of the images should work. I have only tested the Samsung Chromebook Series 5 550 image.

Once an image is selected it will be downloaded.

When the download is complete it will be unpacked and the image will be written to the virtual disks.

Finally the script will finish. The error on line 939 is not critical. Running any commands now will result in a segmentation fault. When the script is finished the system will be pretty much unresponsive, this is normal.

Restart the Guest Machine

Since the ChromiumOS has been overwritten it will be unresponsive and you will have to "hit the reset button" on the virtual machine. On VMware Player, click on the obvious pause button and choose "Restart Guest".

Ignore the Error Behind the Curtain

When the virtual machine starts you will get a warning that a factory error has been detected. Since you are the factory it is up to you to solve the warning. Just "Skip for now".

 Repeat the Sign-In Steps Above

This is a new ChromeOS, nothing on the previous virtual hard drive has been preserved. You will have to go through the login procedure from above again.

Chrome-Sweet-ChromeOS

Finally you have a complete ChromeOS system.

Caveats

I don't know if this is illegal, immoral or fattening. I can't guarantee that this will work, or that it will not trash every electronic memory you have ever made. I know it worked for me.
Automatic updates may or may not work, and may or may not continue to work. See the gotchas from Jay Lee's post.

October 26, 2012

This Must be an Impeachable Offense

The Obama regime killed four Americans by refusing to send backup, by ordering units who were ready to go to stand down. This has got to be an impeachable offense.

August 10, 2012

Public Debt - Where are We Going?

Public Debt Outstanding

The US Treasury has a nifty online application that tracks the Debt to the Penny since 1993. Despite the fact that the data presented is rather disheartening, I graphed the numbers to look for trends.


From first glance it would appear that the a linear fit is a pretty reasonable prediction of the trend per President. If this is the case we are rocketing toward $22 trillion at the end of a second Obama term. The slopes of the best fit lines are:

PresidentSlope
Clinton$539,396,929.90 / Day approximately $2.01 per person per day
Bush$1,568,359,722.35 / Day approximately $5.31 per person per day
Obama$4,045,430,463.22 / Day approximately $13.10 per person per day
A friend of mine suggested I plot the Y-axis on a log scale to see how the trends from President to President track.


Clinton doesn't fit the 21st century trends, but from Bush to Obama the trend does seem to go exponential. Bush added quite a bit at the end of his term.

Clinton's second term is interesting, public debt seemed to be plateauing. The best fit for this is a second order polynomial, but I just can't imagine that the slope would have gone negative if he had stayed in office. I suspect having a combative Congress contributed to that trend.

Bush and Obama's curves are both pretty much exponential.  If this is the case we should expect Obama's second term (if he has one) to close with the total public debt near $27 Trillion, or roughly $81,800 per person.

With this small sample size it seems that the rate of increase of the public debt increases three-fold when a new President is elected. However since the sample size is so small this cannot be reliably predicted.

Senate

After adding in the party control of the Senate there is an interesting trend.


While few Republican controlled Senates have much success in reducing the rate of the debt increase, the rate of the debt increase has increased only under a Democrat controlled Senate.

Raw Data

The raw data is available at the US Treasury's site: http://www.treasurydirect.gov/NP/BPDLogin?application=np. I've separated the data per President in this Google Spreadsheet..

May 1, 2012

PDFCAT

I've looked around for a solution to concatenate PDFs in the past and never found one. It appears to be a niche need, but I've got a number of schematic PDFs that were created sheet by sheet which need to be combined into single sets of schematics. I can use PDF Creator which works really well for printing PDFs, but I've still got to open each PDF and print it.

So I created a simple script that probably makes some poor PDFs but it works.


#!/bin/sh

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <=http://www.gnu.org/licenses/>.

OUTPUT=-
VERBOSE=false
PDFTOPS=/usr/bin/pdftops
PS2PDF=/usr/bin/ps2pdf

usage() {
 echo Usage: `basename $0` [-v] [-o output file] files
 echo pdfcat concatenats PDF files in argument order into a
 echo single PDF file or to stdout. Beware when specifying
 echo wildcard arguments as the pages will come in the order
 echo your shell picks them up.
 echo
 echo Arguments:
 echo "     -h            Show this help screen"
 echo "     -l            Show the GPL license"
 echo "     -v            Use verbose mode...not very verbose"
 echo "     -o OUTPUT     Send the output PDF to the specified"
 echo "                   file. If missing output is generated"
 echo "                   to stdout"
}

license() {
 echo "This program is free software: you can redistribute it and/or modify"
 echo "it under the terms of the GNU General Public License as published by"
 echo "the Free Software Foundation, either version 2 of the License, or"
 echo "(at your option) any later version."
 echo 
 echo "This program is distributed in the hope that it will be useful,"
 echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
 echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
 echo "GNU General Public License for more details."
 echo 
 echo "You should have received a copy of the GNU General Public License"
 echo "along with this program.  If not, see <=http://www.gnu.org/licenses/>."
}

while getopts "vhlo:" OPTION
do
 case $OPTION in
  h)
   usage
   exit 1
   ;;
  l)
   license
   exit 1
   ;;
  v)
   VERBOSE=true
   ;;
  o)
   OUTPUT=$OPTARG
   ;;
  ?)
   usage
   exit1
   ;;
 esac
done

shift $(( OPTIND-1 ))

(
for pdf in $*
do
 if $VERBOSE
 then
  echo -n Processing `basename $pdf` >&2
 fi
 $PDFTOPS $pdf -
 if $VERBOSE
 then
  echo " DONE" >&2
 fi
done
) | $PS2PDF - $OUTPUT



July 8, 2010

Constructor Parameters in C#

I might be the only person coding in C# on a regular basis who doesn't know this, but constructors can have a : this(param1, param2,...) as well as a : base... This means no more creating a private Init method that all the constructors call.

This is what I used to do:


public class Server
{
private int m_port;
private IPAddress m_address;

public Server()
{
Init(IPAddress.Any, 8181);
}

public Server(int port)
{
Init(IPAddress.Any, port);
}

public Server(IPAddress address, int port)
{
Init(address, port);
}

private void Init(IPAddress address, int port)
{
m_port = port;
m_address = address;
}
}

But this is what I can do now:


public class Server
{
private int m_port;
private IPAddress m_address;

public Server()
: this(8181) { }

public Server(int port)
: this(IPAddress.Any, port) { }

public Server(IPAddress address, int port)
{
m_port = port;
m_address = address;
}
}