#!/usr/bin/perl
   
use Switch;
use File::Basename;
use DBI;
use DBD::mysql;
#use DateTime;

$host = "ameriflux.ornl.gov";
$database = "Global";
$tablename = "site";
$user = "globalreader";
$pwd = "readameriflux";

#check command-line arguments
$h = 0;
$v = 1;
while ($_ = $ARGV[0], /^-/)
{
	shift;
        if (/^-h/ || /^-help/)
	{
		$h = 1;
	}
	if (/^-v/)
	{
		$v = 0;
	}
}
if (!$ARGV[0] || $h == 1)
{
	print <<QUOTE;
NAME
   tonetcdf - create a NetCDF file from a comma separated (.csv) file(s)

SYNOPSIS
   tonetcdf [-h] [-help] [-v] [./][/path/to/][csvfilename] [/path/to/][ncfilename]

DESCRIPTION
   Script creates a NetCDF file from specially formated csv file. It is not a generic tool.
   It is possible to process multiple csv files in a single specified diretory. 
   Destination directory for newly created nc file(s) can be specified.
   By default NetCDF file(s) will be created using original csv file(s) name(s)
   with .nc suffix. For example: test.csv -> test.nc, another.test.csv -> another.test.nc.
   
   [-h] or [-help]	Display this help and exit.

   [-v]			Verbose mode is ON by default. Use this to turn it OFF.

   [./]			Process all csv files in current directory. 
			If used no need to specify [csvfilename].

   [/path/to/]		Full path to csv file(s) if they are not in the current directory,
			or full path to directory where NetCDF file(s) will be created.
   
   [csvfilename]	For single csv file processing. If omitted, then bunch processing assumed
			[./] or [/path/to/] must be used  


   [ncfilename]		This option lets to specify another file name for result NetCDF file(s).
			In case of prosessing of single csv file it will be:
			test.csv -> [ncfilename].nc.
			If multiple files processed, renaming will be made as the following:
			test.csv -> [ncfilename]{n}.nc.
   			Where {n} is number of processed file.

EXAMPLES
   Process single file in current directory, create NetCDF file using the same file name in current directory:
	tonetcdf file.csv

   Process all files in current directory, create NetCDF files using the same file names in current directory:
	tonetcdf ./

   Process single file in a remote directory, create NetCDF file using the same file name in current directory:
	tonetcdf /path/to/remote/directory/file.csv

   Process all files in a remote directory, create NetCDF file using the same file name in current directory:
	tonetcdf /path/to/remote/directory/

   Process single file in current directory, create NetCDF file using new file name in current directory:
	tonetcdf file.csv newname.nc

   Process all files in current directory, create NetCDF files using new file names in current directory:
	tonetcdf ./ newname.ns
	Created files will be named newname0.nc, newname1.nc, newname3.ns etc.

   Process all files in a remote directory, create NetCDF files using new file names in current directory:
	tonetcdf /path/to/remote/directory/ newname.nc
	Created files will be named newname0.nc, newname1.nc, newname3.ns etc.

   Process single file in current directory, create NetCDF file using the same file name in a remote directory:
	tonetcdf file.csv /path/to/remote/directory/

   Process all files in current directory, create NetCDF files using the same file names in a remote directory:
	tonetcdf ./ /path/to/remote/directory/

  Process single file in current directory, create NetCDF file using new file name in a remote directory:
	tonetcdf file.csv /path/to/remote/directory/newname.nc

  Process all files in a remote directory, create NetCDF files using new file names in a remote directory:
	tonetcdf /path/to/remote/directory/ /path/to/remote/directory/newname.nc
	Created files will be named newname0.nc, newname1.nc, newname3.ns etc. 
   
QUOTE
	exit;
}
#----------------------------------------------------WHAT AND WRERE FROM-------------------------------------------------------------------#
#check what we have as a first argument
my($filename, $directory) = fileparse($ARGV[0]);
#if no filename we are goingn to have batch processing -> put them in to an array
#but even if we have only one file to process -> put it to an array too 
if (!$filename) # ./ or /home/
{
	if ($directory eq "./") #process all in current dir 
	{
		$from_dir = "./";
		$to_dir = "./";
		opendir(DIR, ".") || die ("Can't read current directory with files for processing");
		#all further operations will use @cvs_files array, it has only file names, no full path, so we may need $dest_dir
		@csv_files = grep(/\.csv$/, readdir(DIR));
		closedir(DIR);
		@to_file = @csv_files;
		#print "csv_files: @csv_files\nto_file:@to_file\n";
	}
	else #process all in a specified directory
	{
		$from_dir = $directory;
		$to_dir = $directory;
		opendir(DIR, $directory) || die ("Can't read $directory directory with files for processing");
		@csv_files = grep(/\.csv$/, readdir(DIR));
		closedir(DIR);
		@to_file = @csv_files;
	}
}
else # /home/test.csv or test.csv - only one file for processing
{
	$from_dir = $directory;
	$to_dir = $directory;
	@csv_files = ();
	$csv_files[0] = $filename;
	@to_file = @csv_files; 
}
#print "from_dir: $from_dir\ncsv_files: @csv_files\nto_dir: $to_dir\nto_file: @to_file\n";
$l = scalar(@csv_files);
#print "l(total files to process): $l\n";
#exit;
#------------------------------------------------------WHERE TO-----------------------------------------------------------------------------#
#if second arg is supplied we change destination directory and possibly processed file names
if (exists $ARGV[1]) 
{
	my($filename, $directory) = fileparse($ARGV[1]);
	if (!$filename) #/home/ - only dest. directory was specified
	{
		$to_dir =  $directory;
		#dest filenames will be same as orig. 
		@to_file = @csv_files;
	}
	else #/home/t.scv new dest dir and new dest filenames
	{
		$to_dir = $directory;
		#create new dist. file names
		@to_file = ();
		#if we have multiple files to process	
		if ($l > 1)
		{
			for ($i = 0; $i < $l; $i++)
			{
				$to_file[$i] = "$filename$i";
			}
		}
		else
		{
			$to_file[0] = "$filename$i";
		}		
	}
}
#print "--WHERE TO--\n";
#print "from_dir: $from_dir\ncsv_files: @csv_files\nto_dir: $to_dir\nto_file: @to_file\n";
#exit;
#if no second argument was supplied -> process to current dir and if filename was scpecified we already set it - $to_file
#--------------------------------------------------------SITE INFORMATION, PIs ----------------------------------------------------------------------------------#
#CSV file names include fluxnetid that can be used to get site related info from the database
#csv files are grouped by site in to a subdirectory -> at any time we deal with only one site
#print "$csv_files[0]\n";
$csv_files[0] =~ /(^AMF_(\w+)_\d{4}\w+(V\d{3}))\.csv$/;
$id = $1;
$fluxnetid = $2;
$version = $3;
$fluxnetid =~ s/^(US)(.)/$1-$2/g;
$dbh = DBI->connect('dbi:mysql:Global:ameriflux.ornl.gov', $user, $pwd) or die "Unable to connect to DB: $DBI::errstr\n";
$sql = "SELECT site.siteid, site.sitename, site.elevation, site.latitude, site.longitude, DATE(site.data_from), DATE(site.data_to), vegetation.vegetation, state.state, country.country FROM site, vegetation, state, country WHERE site.fluxnetid = '$fluxnetid' AND site.vegetationid = vegetation.vegetationid AND site.stateid = state.stateid AND state.countryid = country.countryid";  
$sth = $dbh->prepare($sql);
$sth->execute or die "SQL error: $DBI::errstr\n";
$sth->bind_columns(\$siteid, \$sitename, \$elevation, \$latitude, \$longitude, \$data_from, \$data_to, \$vegetation, \$state, \$country);
$sth->fetchrow_arrayref;
$sitenameforlink = $sitename;
$sitenameforlink =~ s/\s/_/;
$sql = "SELECT investigator.name, investigator.email, investigator.institution FROM site, investigator, site_investigator WHERE site.siteid = site_investigator.siteid AND site_investigator.investigatorid = investigator.investigatorid AND site_investigator.pi = 'yes' AND site.fluxnetid = '$fluxnetid' ORDER BY investigator.name";
$sth = $dbh->prepare($sql);
$sth->execute or die "SQL error: $DBI::errstr\n";
my %pi;
$sth->bind_columns( \( @pi{ @{$sth->{NAME_lc} } } ));
my $piinfo;
while ($sth->fetch) {
      $piinfo .= "$pi{name}; $pi{institution}; $pi{email}\n";
}
$piinfo =~ s/\n$//;
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time);
$Year += 1900;
$Month++;
$date = "$Month/$Day/$Year";
$time_resolution = "30 min";
#Some sites report data hourly or in mixed mode, this has to be reflected in global attributes in time_coverage_resolution
#UMBS (sitid 59) has both hourly and half hourly, they are in the differend subdirectories 
if ($siteid eq 59) {
	if ($ENV{PWD} =~ m/\/hourly/) {
        	$time_resolution = "60 min";
	} else {
		$time_resolution = "30 min";
	}
} else {
	@hourly = (210, 50, 48, 72, 74, 73, 100, 2);
	if (grep {$_ eq $siteid} @hourly) {
        	$time_resolution = "60 min" 
	}
}

#-----------------------------------------------------GLOBAL ATTRIBUTES-----------------------------------------------------------------------------#
$ga = <<End;
//global attributes:
    :title = "$sitename meteorological and micrometeorological non gap-filled data";
    :id = "$id";
    :project = "AmeriFlux";
    :summary = "Recognizing the need and convenience for some users to be able to easily obtain data files containing long-term records
                from AmeriFlux sites in a common, standardized format, the FLUXNET and AmeriFlux data activities at Oak Ridge National Laboratory
                have collaborated to produce the standardized files. These files contain data provided by individual site investigators that have been
                checked and reformatted at ORNL. These Level 2 files contain the same data available from
                the AmeriFlux Site and Data Exploration System (http://ameriflux.ornl.gov).";
    :creator_name = "Carbon Dioxide Information Analysis Center (CDIAC), Oak Ridge National Laboratory, Oak Ridge, TN 37831, USA";
    :creator_url = "http://cdiac.ornl.gov";
    :creator_emai = "bodenta\@ornl.gov";
    :contributor_name = "$piinfo";
    :contributor_role = "$sitename Primary Investigator";
    :date_created = "$date";
    :date_modified = "$date";
    :naming_authotiry = "gov.ornl.cdiac";
    :Metadata_Conventions = "Unidata Dataset Discovery v1.0";
    :metadata_link = "http://ameriflux.ornl.gov/fullsiteinfo.php?sid=$siteid";
    :history = "Data received from site is reviewed and incorporated into a network-wide AmeriFlux database.
                The review process includes checks for consistent units, naming conventions, and reporting intervals
                and reformatting is often necessary to maintain consistency within the larger network-wide database.
                Audit trail for modificationos to the original data:
                http://cdiac.ornl.gov/ftp/ameriflux/data/Level2/Sites_ByName/$sitenameforlink/with_gaps/$sitenameforlink\_SummaryReport.htm
                http://cdiac.ornl.gov/ftp/ameriflux/data/Level2/Sites_ByName/Fort_Peck/with_gaps/history_changes.txt
                http://cdiac.ornl.gov/ftp/ameriflux/data/Level2/Sites_ByName/Fort_Peck/with_gaps/report_$sitenameforlink.html";
    :geospatial_lat_min = "$latitude";
    :geospatial_lat_max = "$latitude";
    :geospatial_lon_min = "$longitude";
    :geospatial_lon_min = "$longitude";
    :geospatial_lat_units = "degrees North";
    :geospatial_lon_units = "degrees East";
    :ecosystem_type = "$vegetation";
    :elevation = "$elevation meters osl";
    :time_coverage_start = "$data_from";
    :time_coverage_end = "$data_to";
    :time_coverage_resolution = "$time_resolution";
    :time_coverage_units = "decimal year";
    :publisher = "Carbon Dioxide Information Analysis Center (CDIAC), Oak Ridge National Laboratory, Oak Ridge, TN";
    :publisher_url = "http://cdiac.ornl.gov";
    :publisher_email = "Boden, Thomas A. <bodenta\@ornl.gov>; Yang, Bai <yangb\@ornl.gov>";
    :processing_level = "Level 2 Data Checked & Formatted by CDIAC: Data received from individual sites are reviewed and incorporated
                        into a network-wide AmeriFlux database. The review process includes checks for consistent units, naming conventions,
                        and reporting intervals and reformatting is often necessary to maintain consistency within the larger network-wide database";
    :license = "The AmeriFlux data provided are freely available and were furnished by individual AmeriFlux scientists who encourage their use.
                Please kindly inform the appropriate AmeriFlux scientist(s) of how you are using the data and of any publication plans.  Please acknowledge the
                data source as a citation or in the acknowledgments if the data are not yet published.  If the AmeriFlux Principal Investigators (PIs) feel that
                they should be acknowledged or offered participation as authors, they will let you know and we assume that an agreement on such matters will
                be reached before publishing and/or use of the data for publication. If your work directly competes with the PI's analysis they may ask
                that they have the opportunity to submit a manuscript before you submit one that uses unpublished data.  In addition, when publishing,
                please acknowledge the agency that supported the research.  Lastly, we kindly request that those publishing papers using AmeriFlux data provide
                preprints to the PIs providing the data and to the data archive at the Carbon Dioxide Information Analysis Center (CDIAC).";
    :cmd_data_type = "Station";
End
#print "$ga\n";
#----------------------------------------------------PROCESSING CSV FILES---------------------------------------------------------------------------#
for ($i = 0; $i < $l; $i++)
{
	$f = "$from_dir$csv_files[$i]";
	#print "f: $f\n";
	open(FH, $f) || die "Can't open CSV file: $csv_files[$i]\n If it isn't in local directory, use full path\n$!\n";
	#if ($to_dir eq "./") {$to_dir = ""};
	$csv_files[$i] =~ s/\.\w+$//;
	$to_file[$i] =~ s/\.\w+$//;
	if ($v == 1)
	{
		print "Converting file: $csv_files[$i]\nCreating cdl file ...\n";
	}
	$cdlfile = "$to_dir$csv_files[$i].cdl";
	$ncfile = "$to_dir$to_file[$i].nc";
	#print "cdlfile: $cdlfile\nncfile: $ncfile\n";
	open(FH1, ">>", $cdlfile) || die "Can't create cdl file: $csv_files[$i]\nBe sure you have appropriate rights for distination directory\n$!\n";
	#go through the whole file to count lines and gev var. names and var. units
	while (defined ($line = <FH>))						
	{
		#read line 18 to an array for variable names
		$line =~ s/\s+$//;
		if ($. == 18)
		{
			#chomp $line;
			@variables = split(/\s*,\s*/, $line);
		}
		#read line 19 to an array for units
		if ($. == 19)
		{
			#chomp $line;
			@units = split(/\s*,\s*/, $line);
		}
		#read data availability %
		if ($. == 20)
		{
			#chomp $line;
			@data_pr = split(/\s*,\s*/, $line);
		}
		#get the year, first value in each data line
		if ($. == 21)
		{
			$line =~ m/(^\s*\d{4}\s*)/;
			$year = $1;
		}
	}
	#total number of lines (minus first 17 lines that are used for comments, var. names and var. units)
	$tot_lines = $.;
	$time_start = $year - 1;
	#start output to CDL file
	print {FH1} "netcdf $csv_files[$i] {  //CDL with CF convention\n";
	#---------------------------------------------------------DIMENSIONS, VARIABLES ND ATTRIBUTES-------------------------------------------------------------#
	#all variables will have one dimension - time, in data set it is DTIME
	print {FH1} <<QUOTE;
dimensions:
	$variables[2] = unlimited;
variables:
	int $variables[0]($variables[2]) ;
                $variables[0]:long_name = "YEAR" ;
		$variables[0]:_FillValue = -6999 ;
                $variables[0]:valid_min = $year ;
                $variables[0]:valid_max = $year ;
                $variables[0]:comment =  "$data_pr[0]% = non-missing data availability, -9999 = missing value, -6999 = unreported value" ;
	int $variables[1]($variables[2]) ;
		$variables[1]:long_name = "GAP" ;
		$variables[1]:_FillValue = -6999 ;
                $variables[1]:valid_min = -1 ;
                $variables[1]:valid_max = 0 ;
                $variables[1]:comment = "$data_pr[1]% = non-missing data availability, -1 denotes no FC measurement, -6999 = unreported value" ;
	float $variables[2]($variables[2]) ;
                $variables[2]:long_name = "Day_of_Year" ;
		$variables[2]:units = "days since $time_start-12-31" ;
                $variables[2]:_FillValue = -6999.f ;
                $variables[2]:valid_min = 0.f ;
                $variables[2]:calendar="standard" ;
                $variables[2]:comment = "$data_pr[2]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	int $variables[3]($variables[2]) ;
                $variables[3]:long_name = "Day_of_Year" ;
                $variables[3]:_FillValue = -6999 ;
                $variables[3]:valid_min = 0 ;
                $variables[3]:valid_max = 366 ;
                $variables[3]:comment = "$data_pr[3]% = non-missing data availability, -9999 = missing value, -6999 = unreported value" ;
	int $variables[4]($variables[2]) ;
		$variables[4]:long_name = "Local_Time_of_Day" ;
                $variables[4]:_FillValue = -6999 ;
                $variables[4]:valid_min = 0 ;
                $variables[4]:valid_max = 2400 ;
                $variables[4]:comment = "$data_pr[4]% = non-missing data availability, -9999 = missing value, -6999 = unreported value" ;
	float $variables[5]($variables[2]) ;
                $variables[5]:long_name = "ustar";
                $variables[5]:units = "m s-1";
                $variables[5]:_FillValue = -6999.f ;
                $variables[5]:valid_min=0.f ;
                $variables[5]:valid_max=14.f ;
                $variables[5]:comment = "$data_pr[5]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[6]($variables[2]) ;
                $variables[6]:long_name = "Air_Temperature" ;
		$variables[6]:units = "degrees C" ;
                $variables[6]:_FillValue = -6999.f ;
                $variables[6]:valid_min = -63.f ;
                $variables[6]:valid_max = 50.f ;
                $variables[6]:comment = "$data_pr[6]% = non-missing data availability, -9999 = missing value, -6999 = unreported value" ;
	float $variables[7]($variables[2]) ;
		$variables[7]:long_name = "Wind_direction" ;
                $variables[7]:units = "degrees" ;
                $variables[7]:_FillValue = -6999 ;
                $variables[7]:valid_min = 0.f ;
                $variables[7]:valid_max = 360.f ;
                $variables[7]:comment = "$data_pr[7]% = non-missing data availability, -9999 = missing value, -6999 = unreported value" ;
	float $variables[8]($variables[2]) ;
                $variables[8]:long_name = "Wind Speed" ;
		$variables[8]:units = "m/s" ;
                $variables[8]:_FillValue = -6999.f ;
                $variables[8]:valid_min = 0.f ;
                $variables[8]:valid_max = 36.f ;
                $variables[8]:comment = "$data_pr[8]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value";
	float $variables[9]($variables[2]) ;
                $variables[9]:long_name = "Net Ecosystem Exchange CO2" ;
		$variables[9]:units = "umol/m2/s" ;
                $variables[9]:_FillValue = -6999.f ;
                $variables[9]:valid_min = -118.f ; 
                $variables[9]:valid_max = 138.f ; 
                $variables[9]:comment = "$data_pr[9]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value";
	float $variables[10]($variables[2]) ;
                $variables[10]:long_name = "Vertical Transfer CO2" ;
		$variables[10]:units = "umol/m2/s" ;
                $variables[10]:_FillValue = -6999.f ;
                $variables[10]:valid_min = -224.f ;
                $variables[10]:valid_max = 173.f ;
                $variables[10]:comment = "$data_pr[10]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value";
	float $variables[11]($variables[2]) ;
                $variables[11]:long_name = "Storage CO2" ;
		$variables[11]:units = "umol/m2/s" ;
                $variables[11]:_FillValue = -6999.f ;
                $variables[11]:valid_min = -75.f ; 
                $variables[11]:valid_max = 75.f ; 
                $variables[11]:comment = "$data_pr[11]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[12]($variables[2]) ;
		$variables[12]:long_name = "Sensible Heat Flux" ;
		$variables[12]:units = "W/m2" ;
                $variables[12]:_FillValue = -6999.f ;
                $variables[12]:valid_min = -1198.f ;
                $variables[12]:valid_max = 976.f ;
                $variables[12]:comment = "$data_pr[12]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[13]($variables[2]) ;
		$variables[13]:long_name = "Sensible Heat Storage" ;
		$variables[13]:units = "W/m2" ;
                $variables[13]:_FillValue = -6999.f ;
                $variables[13]:valid_min = -191.f ; 
                $variables[13]:valid_max = 126.f ; 
                $variables[13]:comment = "$data_pr[13]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[14]($variables[2]) ;
		$variables[14]:long_name = "Latent Heat Flux" ;
		$variables[14]:units = "W/m2" ;
                $variables[14]:_FillValue = -6999.f ;
                $variables[14]:valid_min = -332.f ;
                $variables[14]:valid_max = 934.f ;
                $variables[14]:comment = "$data_pr[14]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[15]($variables[2]) ;
		$variables[15]:long_name = "Latent Heat Storage" ;
		$variables[15]:units = "W/m2" ;
                $variables[15]:_FillValue = -6999.f ;
                $variables[15]:valid_min = -500.f ; 
                $variables[15]:valid_max = 500.f ; 
                $variables[15]:comment = "$data_pr[15]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[16]($variables[2]) ;
		$variables[16]:long_name = "Soil Heat Flux" ;
		$variables[16]:units = "W/m2" ;
                $variables[16]:_FillValue = -6999.f ;
                $variables[16]:valid_min = -63.f ;
                $variables[16]:valid_max = 179.f ;
                $variables[16]:comment = "$data_pr[16]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[17]($variables[2]) ;
                $variables[17]:long_name = "Soil Temperature1" ;
		$variables[17]:units = "degrees C" ;
                $variables[17]:_FillValue = -6999.f ;
                $variables[17]:valid_min = -14.f ;
                $variables[17]:valid_max = 53.f ;
                $variables[17]:comment = "$data_pr[17]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[18]($variables[2]) ;
                $variables[18]:long_name = "Soil Temperature1 Depth" ;
		$variables[18]:units = "cm" ;
                $variables[18]:_FillValue = -6999.f ;
                $variables[18]:valid_min = 0.f ; 
                $variables[18]:valid_max = 100.f ; 
                $variables[18]:comment = "$data_pr[18]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[19]($variables[2]) ;
                $variables[19]:long_name = "Soil Temperature2" ;
		$variables[19]:units = "degrees C" ;
                $variables[19]:_FillValue = -6999.f ;
                $variables[19]:valid_min = -14.f ;
                $variables[19]:valid_max = 53.f ;
                $variables[19]:comment = "$data_pr[19]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[20]($variables[2]) ;
                $variables[20]:long_name = "Soil Temperature2 Depth" ;
		$variables[20]:units = "cm" ;
                $variables[20]:_FillValue = -6999.f ;
                $variables[20]:valid_min = 0.f ; 
                $variables[20]:valid_max = 100.f ; 
                $variables[20]:comment = "$data_pr[20]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[21]($variables[2]) ;
                $variables[21]:long_name = "Precipitation" ;
		$variables[21]:units = "mm" ;
                $variables[21]:_FillValue = -6999 ;
                $variables[21]:valid_min = 0.f ; 
                $variables[21]:valid_max = 54.f ;
                $variables[21]:comment = "$data_pr[21]% = non-missing data availability, -9999 = missing value, -6999 = unreported value" ;
	float $variables[22]($variables[2]) ;
                $variables[22]:long_name = "Relative Humidity" ;
		$variables[22]:units = "percentage" ;
                $variables[22]:_FillValue = -6999.f ;
                $variables[22]:valid_min = 0.f ; 
                $variables[22]:valid_max = 100.f ;
                $variables[22]:comment = "$data_pr[22]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[23]($variables[2]) ;
                $variables[23]:long_name = "Barometric Pressure" ;
		$variables[23]:units = "kPa" ;
                $variables[23]:_FillValue = -6999.f ;
                $variables[23]:valid_min = 84.f ; 
                $variables[23]:valid_max = 103.f ;
                $variables[23]:comment = "$data_pr[23]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[24]($variables[2]) ;
                $variables[24]:long_name = "CO2 Concentration Air" ;
		$variables[24]:units = "umol/mol" ;
                $variables[24]:_FillValue = -6999.f ;
                $variables[24]:valid_min = 41.f ; 
                $variables[24]:valid_max = 2008.f ;
                $variables[24]:comment = "$data_pr[24]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[25]($variables[2]) ;
                $variables[25]:long_name = "Vapor Pressure Deficit" ;
		$variables[25]:units = "kPa" ;
                $variables[25]:_FillValue = -6999.f ;
                $variables[25]:valid_min = 0.f ; 
                $variables[25]:valid_max = 6.f ;
                $variables[25]:comment = "$data_pr[25]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[26]($variables[2]) ;
                $variables[26]:long_name = "Soil Water Content Depth1" ;
		$variables[26]:units = "percent" ;
                $variables[26]:_FillValue = -6999.f ;
                $variables[26]:valid_min = 0.f ; 
                $variables[26]:valid_max = 1.f ;
                $variables[26]:comment = "$data_pr[26]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[27]($variables[2]) ;
                $variables[27]:long_name = "Soil Water Content Depth2" ;
		$variables[27]:units = "m3/m3" ;
                $variables[27]:_FillValue = -6999.f ;
                $variables[27]:valid_min = 0.f ; 
                $variables[27]:valid_max = 1.f ;
                $variables[27]:comment = "$data_pr[27]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[28]($variables[2]) ;
                $variables[28]:long_name = "Net Radiation" ;
		$variables[28]:units = "W/m2" ;
                $variables[28]:_FillValue = -6999.f ;
                $variables[28]:valid_min = -397.f ;
                $variables[28]:valid_max = 957.f ;
                $variables[28]:comment = "$data_pr[28]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[29]($variables[2]) ;
                $variables[29]:long_name = "Incoming Photosynthetically Active Radiation" ;
		$variables[29]:units = "umol/m2/s" ;
                $variables[29]:_FillValue = -6999.f ;
                $variables[29]:valid_min = -41.f ;
                $variables[29]:valid_max = 2454.f ;
                $variables[29]:comment = "$data_pr[29]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[30]($variables[2]) ;
                $variables[30]:long_name = "Incoming Global Solar Radiation" ;
		$variables[30]:units = "W/m2" ;
                $variables[30]:_FillValue = -6999.f ;
                $variables[30]:valid_min = 0.f ; 
                $variables[30]:valid_max = 1462.f ;
                $variables[30]:comment = "$data_pr[30]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[31]($variables[2]) ;
                $variables[31]:long_name = "Incoming Diffuse Global Solar Radiation" ;
		$variables[31]:units = "W/m2" ;
                $variables[31]:_FillValue = -6999.f ;
                $variables[31]:valid_min = 0.f ; 
                $variables[31]:valid_max = 1200.f ;
                $variables[31]:comment = "$data_pr[31]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[32]($variables[2]) ;
                $variables[32]:long_name = "Reflected Photosynthetically Active Radiation" ;
		$variables[32]:units = "umol/m2/s1" ;
                $variables[32]:_FillValue = -6999.f ;
                $variables[32]:valid_min = -19.f ; 
                $variables[32]:valid_max = 1072.f ; 
                $variables[32]:comment = "$data_pr[32]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[33]($variables[2]) ;
                $variables[33]:long_name = "Outgoing Global Solar Radiation" ;
		$variables[33]:units = "W/m2" ;
                $variables[33]:_FillValue = -6999.f ;
                $variables[33]:valid_min = -3.f ;
                $variables[33]:valid_max = 506.f ; 
                $variables[33]:comment = "$data_pr[33]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[34]($variables[2]) ;
                $variables[34]:long_name = "Incoming Longwave Radiation" ;
		$variables[34]:units = "W/m2" ;
                $variables[34]:_FillValue = -6999.f ;
                $variables[34]:valid_min = 0.f ;
                $variables[34]:valid_max = 505.f ;
                $variables[34]:comment = "$data_pr[34]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[35]($variables[2]) ;
                $variables[35]:long_name = "Outgoing Longwave Radiation" ;
		$variables[35]:units = "W/m2" ;
                $variables[35]:_FillValue = -6999.f ;
                $variables[35]:valid_min = -33.f ;
                $variables[35]:valid_max = 565.f ; 
                $variables[35]:comment = "$data_pr[35]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[36]($variables[2]) ;
                $variables[36]:long_name = "Water Vapor Concentration" ;
		$variables[36]:units = "mmol/mol" ;
                $variables[36]:_FillValue = -6999.f ;
                $variables[36]:valid_min = 6.f ;
                $variables[36]:valid_max = 3021.f ; 
                $variables[36]:comment = "$data_pr[36]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[37]($variables[2]) ;
                $variables[37]:long_name = "Total Ecosystem Respiration" ;
		$variables[37]:units = "umol/m2/s" ;
                $variables[37]:_FillValue = -6999.f ;
                $variables[37]:valid_min = -28.f ;
                $variables[37]:valid_max = 70.f ; 
                $variables[37]:comment = "$data_pr[37]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[38]($variables[2]) ;
                $variables[38]:long_name = "Gross Primary Production" ;
		$variables[38]:units = "umol/m2/s" ;
                $variables[38]:_FillValue = -6999.f ;
                $variables[38]:valid_min = -170.f ; 
                $variables[38]:valid_max = 229.f ; 
                $variables[38]:comment = "$data_pr[38]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[39]($variables[2]) ;
                $variables[39]:long_name = "CO2 Concentration Top" ;
		$variables[39]:units = "umol/mol" ;
                $variables[39]:_FillValue = -6999.f ;
                $variables[39]:valid_min = 0.f ;
                $variables[39]:valid_max = 2000.f ;
                $variables[39]:comment = "$data_pr[39]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[40]($variables[2]) ;
                $variables[40]:long_name = "CO2_top Height" ;
 		$variables[40]:units = "m" ;
                $variables[40]:_FillValue = -6999.f ;
                $variables[40]:valid_min = 0.f ; 
                $variables[40]:valid_max = 500.f ; 
                $variables[40]:comment = "$data_pr[40]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[41]($variables[2]) ;
                $variables[41]:long_name = "Assimilated photosinthetically active radiation" ;
		$variables[41]:units = "umol/m2/s" ;
                $variables[41]:_FillValue = -6999.f ;
                $variables[41]:valid_min = 0.f ; 
                $variables[41]:valid_max = 2000.f ; 
                $variables[41]:comment = "$data_pr[41]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[42]($variables[2]) ;
                $variables[42]:long_name = "Diffuse photosyntetically active radiation" ;
		$variables[42]:units = "umol/m2/s" ;
                $variables[42]:_FillValue = -6999.f ;
                $variables[42]:valid_min = 0.f ; 
                $variables[42]:valid_max = 500.f ; 
                $variables[42]:comment = "$data_pr[42]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[43]($variables[2]) ;
                $variables[43]:long_name = "Assimilated incoming photosyntetically active radiation" ;
		$variables[43]:units = "percentage" ;
                $variables[43]:_FillValue = -6999.f ;
                $variables[43]:valid_min = 0.f ; 
                $variables[43]:valid_max = 100.f ; 
                $variables[43]:comment = "$data_pr[43]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
	float $variables[44]($variables[2]) ;
                $variables[44]:long_name = "Atmospheric stability parameter" ;
		$variables[44]:units = "unitless" ;
                $variables[44]:_FillValue = -6999.f ;
                $variables[44]:valid_min = 0.f ; 
                $variables[44]:valid_max = 25000.f ; 
                $variables[44]:comment = "$data_pr[44]% = non-missing data availability, -9999.f = missing value, -6999.f = unreported value" ;
QUOTE
	#delete data % avaliability array
	undef @data_pr;
	#---------------------------------------------------------GLOBAL ATTRIBUTES----------------------------------------------------------------------#
	print {FH1} "$ga";
	#-----------------------------------------------------------DATA---------------------------------------------------------------------------------#
	#number of columns = length of data
	$len = scalar(@variables);
	#reset pointer in to the beginning o file
	seek(FH, 0, 0);
	#reset line counter
	$. = 0;
	@new_line = ();
	while (defined ($line = <FH>))
	{
		#skip header, change this if header is changed!!!		
		next if ($. < 21);
		if ($v == 1)
		{
			$message = "Processing line: ". $.;
			print $message;
		}
		$line =~ s/\s+$//;
		@data = split(/\s*,\s*/, $line);
		for ($m = 0; $m < $len; $m++)
		{
			if ($. == $tot_lines) {$new_line[$m] = $new_line[$m].$data[$m].";\n";}
			else {$new_line[$m] = $new_line[$m].$data[$m].", ";}
		}
		$k = length $message;
		for ($j = 0; $j < $k; $j++)
		{
			print "\b";
		}
	}
	print {FH1} "\ndata:\n";
	for ($n = 0; $n < $len; $n++)
	{
		print {FH1} "\t$variables[$n] = $new_line[$n]";
	}
	print {FH1} "}\n";
	close(FH);								
	close(FH1);
	#delete all arrays
	undef @data;
	undef @variables;
	#--------------------------------------------------------------NetCDF------------------------------------------------------------------------------------#
	if ($v == 1)
	{
		print "\nCreating nc file ...\n";
	}
	system("ncgen -o $ncfile $cdlfile");
	if ($? != 0)
	{
		#unlink $cdlfile;
		die "File $csv_files[$i] has been not processed due to an error above\nScript is stopped\n";
	}
	unlink $cdlfile || die "Can't delete cdl file\n";
	if ($v == 1)
	{
		print "$ncfile is ready\n\n";
	}
}
print "All files are done\n";
#unlink $ncfile;
#print "nc file deleted\n";
