#!/bin/csh -f

if ($?SLURM_SUBMIT_DIR) then
  cd $SLURM_SUBMIT_DIR
endif

set label = masked
set gzip_output = Y

setenv SECTOR "ptfire_othna3D"
setenv RUN_MONTHS "1 2 3 4 5 6 7 8 9 10 11 12"
setenv BASE_YEAR "2016"
setenv PLATFORM "v7.2" # not used, other than to keep Assigns file happy

## Definitions for case name, directory structures, etc, that are used
#  by every sector in the case
#  Anything defined in directory_definitions can be overridden here 
#  if desired
source ../directory_definitions_36US3.csh

set indir = $INSTALL_DIR/$CASE/premerged/$SECTOR
set outdir = $indir
setenv GRID $REGION_ABBREV
setenv SPC $EMF_SPC

## source the ASSIGN file
source $ASSIGNS_FILE

set dimon = (31 29 31 30 31 30 31 31 30 31 30 31)

foreach mon ($RUN_MONTHS)
    if ($mon < 10) then
        set MM = 0${mon}
    else
        set MM = $mon
    endif
    set day = 1
    while ($day <= $dimon[$mon])
        if ($day < 10) then
            set DD = 0${day}
        else
            set DD = $day
        endif
        set gday = 2016${MM}${DD}
        set infile = $indir/emis_mole_${SECTOR}_${gday}_${GRID}_${EMF_SPC}_${CASE}.ncf
        set outfile = $outdir/emis_mole_${SECTOR}_${label}_${gday}_${GRID}_${EMF_SPC}_${CASE}.ncf
        set tmp = $outdir/masking_temp_file_$$.ncf
        if (-e $infile) then
	    ln -s $infile $tmp
	else if (-e $infile.gz) then
	    zcat ${infile}.gz > $tmp
	else
	    echo "skipping day $gday"
	    @ day++
	    continue
	endif
       $SCRIPTS/mask_36US3/mask_grid.py $tmp $outfile
        if ($gzip_output == Y) then
	    gzip -v $outfile
	endif
        @ day++
	rm $tmp
    end
end

end # foreach sector
