When creating a location via the API, the API will occasionally associate a contact with the incorrect state when passing it a two-letter state abbreviation.
Using the following code to generate the location results in an incorrect state association:
// Insert the location record
$address = array(
'contact_id' => $contact['id'],
'location_type' => 'Home',
'street_address' => '123 test lane',
'supplemental_address_1' => '',
'city' => 'San Francisco',
'state_province' => 'CA',
'postal_code' => '94104',
'country' => 'US',
'is_primary' => 1,
'is_billing' => 1,
$location_result = &civicrm_location_add( $address );
This results in the state being marked as 'Ciego de Avila' - which incidentally has the same state abbreviation as California.
We discovered this when changing Civi to display states as full state names rather than abbreviations.
Also, if you do NOT include the country as a param (or pass a non-existent country as a param) to the location api, the result will be the country associated with the state Civi has selected for you. In the above case, if we had not passed in 'US' as the country, the API would set the contact's country as Cuba.
The work-around solution is to determine the full state name and correct country independently of the API, and then pass those correct, full values to the API.