pacificclimate / data-prep-actions

Data Preparation actions - record of ephemera used to prepare data for PCIC data portals and other tools
0 stars 0 forks source link

Add pdp time normalizing script #8

Closed corviday closed 5 years ago

corviday commented 5 years ago

This script was used to normalize time variables in netCDF files for the PDP.

Specifically, the PDP assumes that the reference time stamp given in the units attribute of the time variable is the same as the first timestamp in the data.

So if you load a dataset with this time variable:

netcdf pr_day_BCCAQv2+ANUSPLIN300_bcc-csm1-1-m_historical+rcp85_r1i1p1_19500101-21001231 {
dimensions:
    lon = 1068 ;
    lat = 510 ;
    time = 55115 ;
variables:
    double lon(lon) ;
    double lat(lat) ;
    double time(time) ;
        time:units = "days since 1850-01-01 00:00:00" ;
        time:calendar = "365_day" ;
        time:long_name = "Time" ;
        time:standard_name = "Time" ;
    short pr(time, lat, lon) ;
data:

 time = 36500.5, 36501.5, 36502.5, 36503.5, 36504.5, 36505.5, 36506.5,
...
}

It will show up on the portal date selection widget as spanning 1850-2000, not 1950-2100. The right number of days, but the wrong start date.

This is probably because the calendar is generated from a DAS call, which includes only metadata, not the actual timestamps. So the front end has easy access to the time:units attribute, but no access to the actual timestamps.

This script subtracts 100 years (or whatever the offset is) from each timestamp and adds a hundred years to the reference date in the units attribute, so that the reference date is the same as the first timestamp.

After running the script, the above time variable would look like this:

netcdf pr_day_BCCAQv2+ANUSPLIN300_bcc-csm1-1-m_historical+rcp85_r1i1p1_19500101-21001231 {
dimensions:
    lon = 1068 ;
    lat = 510 ;
    time = 55115 ;
variables:
    double lon(lon) ;
    double lat(lat) ;
    double time(time) ;
        time:units = "days since 1950-01-01 00:00:00" ;
        time:calendar = "365_day" ;
        time:long_name = "Time" ;
        time:standard_name = "Time" ;
    short pr(time, lat, lon) ;

data:

 time = 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5,
...
}