#!/usr/bin/perl -w
#
use File::Copy;
use Time::Local;


BEGIN
  {
  $ENV{LD_LIBRARY_PATH}="LD_LIBRARY_PATH:/data/starfs1/libs/netcdf-4.2/lib:/data/starfs1/libs/hdf5-1.8.7/lib";
  }



# This script is the start of the NPROVS collocation process. It copies radiosonde data from the
# unified.radiosonde format, creates date directories in the nprovs collocation directory, and
# create netCDF files from the data in the unified radiosonde.


$source_dir     = "/data/data065/nprovs/source/collocate/AddCosmic2StarToNprovs";
$work_dir       = "/data/data065/nprovs/source/collocate/AddCosmic2StarToNprovs/work_dir";
$input_data_dir = "/data/data065/nprovs/source/collocate/AddCosmic2StarToNprovs/work_dir";
#$input_data_dir = "/data/data065/nprovs/source/collocate/AddCosmic2StarToNprovs/data";
$output_dir     = "/data/data599/collocations/nprovs";


#print "\n\nProcessing the COSMIC-2 STAR data\n\n";

chdir($work_dir);


# Set the date to process

$date_to_process = 20250605;

$year  = $date_to_process / 10000;
$month = ($date_to_process % 10000) / 100;
$day   = $date_to_process % 100;


printf("\nProcessing COSMIC-2 STAR on %d\n", $date_to_process);


# Transfer the Wet and Atm files from Gpsmet

#printf("Transferring Wet files from gpsmet.umd...\n");

#$wget = sprintf("wget -q https://gpsmet.umd.edu/star_gnssro/data/cosmic2/wetPrf_STAR_1dvar/postProc/%04d/%04d-%02d-%02d.tar.gz --no-check-certificate", $year, $year, $month, $day);

#printf("%s\n", $wget);

#system $wget;

#$gzip_file_wet = sprintf("%s/%04d-%02d-%02d.tar.gz", $work_dir, $year, $month, $day);

#if (-e $gzip_file_wet)
#  {
#  $tar_file_wet  = sprintf("%s/%04d-%02d-%02d.tar", $work_dir, $year, $month, $day);

#  system "gunzip " . $gzip_file_wet;
#  system "tar -xvf " . $tar_file_wet . " --strip-components 1";
#  unlink $tar_file_wet;
#  }



#printf("\nTransferring Atm files from gpsmet.umd...\n");

#$wget = sprintf("wget -q https://gpsmet.umd.edu/star_gnssro/data/cosmic2/atmPrf_STAR_ROPP/postProc/%04d/%04d-%02d-%02d.tar.gz --no-check-certificate", $year, $year, $month, $day);

#printf("%s\n", $wget);

#system $wget;

#$gzip_file_atm = sprintf("%s/%04d-%02d-%02d.tar.gz", $work_dir, $year, $month, $day);

#if (-e $gzip_file_atm)
#  {
#  $tar_file_atm  = sprintf("%s/%04d-%02d-%02d.tar", $work_dir, $year, $month, $day);

#  system "gunzip " . $gzip_file_atm;
#  system "tar -xvf " . $tar_file_atm . " --strip-components 1";
#  unlink $tar_file_atm;
#  }


# Copy the daily collocation file to the working directory

$nprovs_daily_file = sprintf("%s/nprovs_%d.nc", $output_dir, $date_to_process);
$temp_daily_file   = sprintf("%s/nprovs_%d.nc", $work_dir, $date_to_process);

copy ($nprovs_daily_file, $temp_daily_file);


# Run the program to collocate the NUCAPS to the collocation

$temp_output_file = sprintf("%s/temp.nc", $work_dir);

symlink $temp_daily_file, "in.file" or warn "Cannot link $temp_daily_file to in.file";
symlink $temp_output_file, "out.file" or warn "Cannot link $temp_output_file to out.file";
#symlink $work_dir, "in.dir" or warn "Cannot link $work_dir to in.dir";
symlink $input_data_dir, "in.dir" or warn "Cannot link $input_data_dir to in.dir";


system $source_dir . "/AddCosmic2StarToNprovs.x " . $date_to_process . " 502 'COSMIC-2 STAR'";


unlink "in.dir" or warn "Cannot unlink in.dir";
unlink "out.file" or warn "Cannot unlink out.file";
unlink "in.file" or warn "Cannot unlink in.file";


# end of file
