###########################
# Kristian S. Gleditsch
# Started: 23.03.2000
#
# This file creates annual country observations from the system data
#
# The output will be sorted numerically by cownumber and year 
#
# Requires Sort::Field module from CPAN
#
# current version: 14.05.2000
############################

# I. Creating ouput files
open(USEOUT,">states.asc") || die "Cannot create the target to use file";
open(ERRLOG,">err.log") || die "Cannot create the error log file";
print ERRLOG "The following errors ocurred\n";


# II. Get preferences
$annual = 'T';
$include_long_names = 'F';
$include_microstates = 'F';

$minyear = '1945';
$maxyear = '2002';

if (($include_long_names eq 'F') && ($annual eq 'T')){
   @variables = ("stateid","statenumber","year");
   $sep_char = " ";
}
if (($include_long_names eq 'T') && ($annual eq 'T')){
   @variables = ("stateid","statenumber","name","year");
   $sep_char = "\t";
}
if (($include_long_names eq 'F') && ($annual eq 'F')){
   @variables = ("stateid","statenumber","startyear","endyear");
   $sep_char = " ";
}
if (($include_long_names eq 'T') && ($annual eq 'F')){
   @variables = ("stateid","statenumber","name","startyear","endyear");
   $sep_char = "\t";
}

print USEOUT join($sep_char,@variables[0..(@variables-1)])."\n";


# III. Read in the raw data

if ($include_microstates eq 'T'){
 use Sort::Fields;
 open(IN1,"ksgmdw/iisystem.dat") || die "Cannot open iisystem file";
 open(IN2,"ksgmdw/microstatessystem.dat") || die "Cannot open iisystem file";
 @inlist1 = <IN1>; # read everything
 @inlist2 = <IN2>; # read everything
 @uselist = fieldsort['1n'], (@inlist1,@inlist2);
}

if ($include_microstates eq 'F'){
 use Sort::Fields;
 open(IN,"ksgmdw/iisystem.dat") || die "Cannot open input file";
 @inlist = <IN>;
 @uselist = fieldsort['1n'], @inlist;
}

foreach(@uselist) { # Loop over lines in list
  chop($_);			# get rid of newline
  @info = split(/\t/,$_);  	# read 
  $ctynum = $info[0];
  $ctyid = $info[1];
  $long_name = $info[2];
  $start = $info[3];
  $end = $info[4];

  @startinfo = split(/:/,$start); 
  @endinfo = split(/:/,$end); 

  $startyear = $startinfo[2];
  $startmonth = $startinfo[1];
  $startday = $startinfo[0];
  $endyear = $endinfo[2];
  $endmonth = $endinfo[1];
  $endday = $endinfo[0];

  $startdate = join("/",($startday,$startmonth,$startyear));
  $enddate = join("/",($endday,$endmonth,$endyear));



  if($annual eq 'T'){

   if ($include_long_names eq 'F'){
    for ($i = $startyear; $i <= $endyear; $i++) {
      $year = $start_year + $i;
      if(($year ge $minyear) & ($year le $maxyear)){
	print USEOUT $ctyid.' '.$ctynum.' '.$year."\n";
      } # end check for range of years
     } # end year loop	
    } # long names is F

   if ($include_long_names eq 'T'){
    for ($i = $startyear; $i <= $endyear; $i++) {
      $year = $start_year + $i;
      if(($year ge $minyear) & ($year le $maxyear)){
	print USEOUT $ctyid."\t".$ctynum."\t".$long_name."\t".$year."\n";
      } # end check for range of years
    } # end year loop	
   } # long names is F

 } # End annual eq T


  if($annual eq 'F'){

   if ($include_long_names eq 'F'){
	print USEOUT join($sep_char,
	     ($ctyid,$ctynum,$startdate,$enddate))."\n";
    } # long names is F

   if ($include_long_names eq 'T'){
	print USEOUT join($sep_char,
	     ($ctyid,$ctynum,$long_name,$startdate,$enddate))."\n";
   } # long names is F

 } # End annual eq T


} # End loop over @uselist


