pacificclimate / climate-explorer-data-prep

0 stars 0 forks source link

Issue/123 generate_climos history string inserted to history attribute when sys.argv is empty #124

Closed sum1lim closed 4 years ago

sum1lim commented 4 years ago

This PR closes #123

A new pathway to update history metadata attribute has been implemented in generate_climos for broader usage of create_climo_files in other programs. As described in the issue, updating the history attribute did not function properly when no argument was passed through terminal standard input since sys.argv was used for command-line reconstruction. This problem is solved by the creation of args dictionary that has keys as options and values as arguments. When sys.argv is empty, the program reconstructs the command-line using the information in the args dictionary. Additionally, create_climo_files function takes the parameter of the Climatological period option(-c/--climo ) for an informative history record.

tmp.py used for testing:

from nchelpers import CFDataset, standard_climo_periods
from dp.generate_climos import create_climo_files

filepath = "./tests/data/tiny_daily_pr.nc"
input_file = CFDataset(filepath)
climo = standard_climo_periods().keys()
for period in input_file.climo_periods.keys() & climo:
    t_range = input_file.climo_periods[period]
    create_climo_files(
                    period,
                    "output",
                    input_file,
                    "mean",
                    *t_range,
                    )

An example output using the new pathway:

        :history = "Tue Jun 16 10:21:05 2020: end: generate_climos  ./tests/data/tiny_daily_pr.nc -c 2050 -o output -p mean -g True -v True -i True -r {\'seasonal\', \'yearly\', \'monthly\'}\n",
            "Tue Jun 16 10:21:04 2020: cdo -O -replace /tmp/cdoPyitmuee4a /tmp/cdoPyo_3lyoua /tmp/cdoPy32cxq5jv\n",
            "Tue Jun 16 10:21:04 2020: cdo -O -ymonmean /tmp/cdoPys4ypc09c /tmp/cdoPyitmuee4a\n",
            "Tue Jun 16 10:21:03 2020: cdo -O -seldate,2040-01-01,2069-12-31 ./tests/data/tiny_daily_pr.nc /tmp/cdoPys4ypc09c\n",
            "Tue Jun 16 10:21:03 2020: start: generate_climos  ./tests/data/tiny_daily_pr.nc -c 2050 -o output -p mean -g True -v True -i True -r {\'seasonal\', \'yearly\', \'monthly\'}\n",
            "Thu Mar 21 14:49:01 2019: cdo sellonlatbox,216.5625,219.375,40.4636506825932,48.8352434707287 /storage/data/climate/downscale/BCCAQ2/CMIP5/CanESM2/pr_day_CanESM2_historical+rcp26_r1i1p1_19500101-21001231.nc /storage/home/nrados/tiny_daily_pr.nc\n",
            "Thu Sep  1 14:34:03 2016: ncrcat -O /storage/data/climate/downscale/BCCAQ2/CMIP5/timetmp/time_subset_pr_day_CanESM2_historical_r1i1p1_19500101-20051231.nc /storage/data/climate/downscale/BCCAQ2/CMIP5/timetmp/time_subset_pr_day_CanESM2_rcp26_r1i1p1_20060101-21001231.nc /storage/data/climate/downscale/BCCAQ2/CMIP5/CanESM2/pr_day_CanESM2_historical+rcp26_r1i1p1_19500101-21001231.nc\n",
            "Thu Sep 01 14:33:15 2016: cdo -O seldate,1950-01-01T00:00,2005-12-31T23:59 /storage/data/climate/downscale/BCCAQ2/CMIP5/spacetmp/space_subset_pr_day_CanESM2_historical_r1i1p1_18500101-20051231.nc /storage/data/climate/downscale/BCCAQ2/CMIP5/timetmp/time_subset_pr_day_CanESM2_historical_r1i1p1_19500101-20051231.nc\n",
            "Thu Sep  1 14:32:42 2016: ncks -O -d lon,215.,310. -d lat,40.,85. /storage/data/climate/downscale/BCCAQ2/CMIP5/grouptmp/pr_day_CanESM2_historical_r1i1p1_18500101-20051231.nc /storage/data/climate/downscale/BCCAQ2/CMIP5/spacetmp/space_subset_pr_day_CanESM2_historical_r1i1p1_18500101-20051231.nc\n",
            "2011-04-13T23:04:41Z CMOR rewrote data to comply with CF standards and CMIP5 requirements." ;
jameshiebert commented 4 years ago

Looks like a reasonable approach to me. Just a couple minor changes that I saw. Good job :)