#!/usr/bin/perl 

use strict;

my $filehandle = shift;

open(NC_OUT, "<$filehandle") or die "Can not open this file\n";

# Retrieve the information(metadata)

my $info = "<pre>\n";
while (my $line=<NC_OUT>)
{
 if ($line =~ /{$/) {next;}
 if  ($line !~ /variables:/i)
 {$info .= $line;}
 if ($line =~ /variables:/i)
 { $info .= '</pre>';last;}
}
# Get fields, description and units information.
my (@param, @description, @units);

my ($k,@line); 
$k = 0;
while (my $line = <NC_OUT>)
{
 if ($line =~ /global attributes:/i)
 {last}
 $line[$k] = $line;
 $k++;
} 

my $i = 0;
for (my $j=0;$j<=$#line;$j++)
{
  $line[$j] =~/\s(\w+\d*)\(.*\)/;
  $param[$i] = $1;
  $j++;
  $description[$i] = "<pre>\n";
  while ( $line[$j] =~ /:/)
  {
   if ($line[$j] =~ /units = \"(.*)\"/i)
   { $units[$i] = $1; $j++; next;}
   $description[$i] .= $line[$j];
   $j++
  }
  $description[$i] .= "</pre>\n"; 
  $i++;
  if ($j == $#line) {last}
  $j--;
}

# Obtain the global attributes and treat them as the top level colums
$k = 0;
@line = ();
my ($comment, %top_level, @top_level);
while (my $line = <NC_OUT>)
{
 if ($line =~ /data:/i)
 {last}
 chomp($line);

 $line[$k] = $line;
 $comment .= "# " . substr($line, index($line,':')+1);
 my ($key, $value) = split('=',substr($line, index($line,':')+1));
 $key =~ s/^\s+//;
 $key =~ s/\s+$//;
 $value =~ s/^\s*\"//;
 $value =~ s/\;$//;
 $value =~ s/\s+$//;
 $value =~ s/\"$//;
 $value =~ s/\,/ /g;
 $value =~ s/\\\'s/ s/g;
 $top_level{$key} = $value;
 push @top_level, $key;
 $k++;
} 

# Specify current level fields, also make time, lat, lon as top level fields.
$k = 0;
@line = ();
my (%current_level, @keys);
while (my $line = <NC_OUT>)
{
 if ($line =~ /\}/i)
 {last;}
 $line[$k] = $line;
 $k++;
}
for (my $i=0;$i<=$#line;$i++)
{
  if ($line[$i] !~ /\w/) {next}
  my ($key, $value);
  chomp($line[$i]);

  if ($line[$i] =~ /(\w+.*)\=/)
  { $key = $1;
    $key =~ s/^\s+//;
    $key =~ s/\s+$//;
    $value = substr($line[$i],index($line[$i],'=')+1);
  }

  while (!($value=~/\;/))
  {   $i++;chomp($line[$i]);  $line[$i] =~ s/^\s+//;$value .= $line[$i];}
  
  $value =~ s/\;$//;
  $value =~ s/\s+//g;

  my @tmp = split(',', $value);
  if ($#tmp==0)
  {$top_level{$key} = $tmp[0];push @top_level, $key;}
   else {push @keys, $key;}
  @{$current_level{$key}} = @tmp;
  if ($i == $#line) {last}
}
close NC_OUT;
my $info_file = substr($filehandle,0, index($filehandle,'.')) . '.info';
open (INFO, ">$info_file") or die"Can not create this file\n";
print INFO $info;
print INFO "<table border=1>\n";
for (my $i=0;$i<=$#param;$i++)
{
  print INFO "<tr><td>" . $param[$i] . "</td><td>" . $description[$i] . "</td><td>" . $units[$i] . "</td></tr>\n";
}
print INFO "</table>\n";
close INFO;

my $top_dat = 'toplevel.dat';               #substr($filehandle,0, index($filehandle,'.')) . 
my ($line, @columns);
if (open (TOP, "<$top_dat"))
{ $line = <TOP>; }
close TOP;
chomp($line);
$line=~s/\t>//;
open (TOP_DAT, ">>$top_dat");

if (defined $line)
  { @columns = split(',',$line);}
  else 
 {  @columns = @top_level; print TOP_DAT join(',', @top_level),"\t>\n", join(',',@keys),"\n"; }

for (my $i=0;$i<=$#columns;$i++)
{  print TOP_DAT $top_level{$columns[$i]} ;
   if (!(defined $top_level{$columns[$i]}) or ($top_level{$columns[$i]} eq ''))
   { print TOP_DAT "nd";}

   if (!($i == $#columns)) {print TOP_DAT ',';print ',';}
   else
    {print TOP_DAT "\t", substr($filehandle,0, index($filehandle,'.')) . ".dat\n";}
}
close TOP_DAT;

my $current_dat = substr($filehandle,0, index($filehandle,'.')) . '.dat';               
open (CUR_DAT, ">$current_dat") or die"Can not create current .dat file\n";
print CUR_DAT join(',',@keys),"\n";
my @tmp = @{$current_level{$keys[0]}};
for (my $i=0;$i<=$#tmp;$i++)
{ 
  my @data;
  for my $key (@keys)
  { push @data, $current_level{$key}[$i];}
  print CUR_DAT join(',',@data),"\n";
}
close CUR_DAT;

