#!/bin/bash

###Data
#アメリカ海洋大気庁(National Climatic Data Center)
#Data in Japan (2013)  


##Fix the data format

####ish-history.csv :: PlaceNumber and PlaceName
###JAPAN.txt :: Data in Japan 
###USAF($1) in ish-history.csv	=	SIN---($1)	in JAPAN.txt

sed -e "s/\"//g" ish-history.csv |\
awk 'BEGIN{FS=","; OFS=","}{if(NR!=1)print($1,$3)}' > number_list.dat

sed -e "s/\"//g" JAPAN.txt |\
awk 'BEGIN{OFS=",";}{if(NR!=1) print($1,$3,$4)}' > JAPAN_TEMP.dat




###Delete the place which has the missing value
del=$( awk -F"," '{print $1}' JAPAN_TEMP.dat | uniq -c | awk '{if($1!=365) printf("%d ",$2)}')

awk -v tmp="${del}" '
BEGIN{
OFS=",";
FS=",";
}
{
split(tmp, array , " ");
len = length(array);
flag = 0;
for( i=1;i<=len; i++){
if($1==array[i]) flag = 1;
}
if(flag == 0 ) print($0);
}
' JAPAN_TEMP.dat > JAPAN_TEMP_del.dat



##PlaceID -> Place Name
##Temprature : F -> C  :: C=5(F-32)/9
awk '

BEGIN{

	FS=",";

	OFS=",";

}
{
if(FILENAME ~/number/) country[$1] = $2;
else	print(country[$1],$2, 5*($3-32)/9 );
}
' number_list.dat  JAPAN_TEMP_del.dat > JAPAN_NAME_del_TEMP.dat


