| 1 |
Parsing DRS filenames |
| 2 |
===================== |
| 3 |
|
| 4 |
The drslib.cmip5.make_translator function takes a path or URL prefix as it's only |
| 5 |
argument |
| 6 |
|
| 7 |
>>> from drslib import cmip5 |
| 8 |
>>> cmip5_trans = cmip5.make_translator('http://example.com/cmip5') |
| 9 |
>>> cmip5_trans.prefix |
| 10 |
'http://example.com/cmip5' |
| 11 |
|
| 12 |
The DRS class is used to store DRS components. For instance to |
| 13 |
extract DRS components from a filename do: |
| 14 |
|
| 15 |
>>> drs = cmip5_trans.filename_to_drs('tas_Amon_HadCM3_historicalNat_r1_185001-200512.nc') |
| 16 |
>>> drs.variable |
| 17 |
'tas' |
| 18 |
>>> drs.table |
| 19 |
'Amon' |
| 20 |
>>> drs.model |
| 21 |
'HadCM3' |
| 22 |
>>> drs.subset |
| 23 |
((1850, 1, None, None, None, None), (2005, 12, None, None, None, None), None) |
| 24 |
|
| 25 |
Some DRS components can be deduced from the filename by consulting the |
| 26 |
CMIP5 MIP tables or using internally configured tables. These are |
| 27 |
automatically set during translation: |
| 28 |
|
| 29 |
>>> drs.institute |
| 30 |
'MOHC' |
| 31 |
>>> drs.realm |
| 32 |
'atmos' |
| 33 |
>>> drs.frequency |
| 34 |
'mon' |
| 35 |
|
| 36 |
Some DRS attributes cannot be deduced from the filename. These must |
| 37 |
be set in the drs object before you can generate a full DRS path. Use |
| 38 |
the DRS.is_complete() method to test if the DRS object is completely specified: |
| 39 |
|
| 40 |
>>> drs.is_complete() |
| 41 |
False |
| 42 |
>>> drs.version = 3 |
| 43 |
>>> drs.product = 'output' |
| 44 |
>>> drs.is_complete() |
| 45 |
True |
| 46 |
|
| 47 |
Once complete you can generate the full DRS path or URL: |
| 48 |
|
| 49 |
>>> cmip5_trans.drs_to_filepath(drs) |
| 50 |
'http://example.com/cmip5/output/MOHC/HadCM3/historicalNat/mon/atmos/Amon/r1/v3/tas/tas_Amon_HadCM3_historicalNat_r1_185001-200512.nc' |
| 51 |
|
| 52 |
|
| 53 |
Generating DRS paths and filenames |
| 54 |
=================================== |
| 55 |
|
| 56 |
The DRS class can be used to constructed from scratch like: |
| 57 |
|
| 58 |
>>> from drslib.drs import CmipDRS |
| 59 |
>>> drs2 = CmipDRS(activity='cmip5', product='requested', model='HadCM3', |
| 60 |
... experiment='historicalNat', variable='tas', |
| 61 |
... ensemble=(1, None, None), version=4, |
| 62 |
... table='3hr', frequency='3hr') |
| 63 |
|
| 64 |
>>> cmip5_trans.drs_to_path(drs2) |
| 65 |
'http://example.com/cmip5/requested/MOHC/HadCM3/historicalNat/3hr/atmos/3hr/r1/v4/tas' |
| 66 |
|
| 67 |
Extra information is needed to create the filename |
| 68 |
|
| 69 |
>>> cmip5_trans.drs_to_file(drs2) |
| 70 |
'tas_3hr_HadCM3_historicalNat_r1.nc' |
| 71 |
>>> drs2.subset = ((2010, None, None, None, None, None), (2011, None, None, None, None, None), None) |
| 72 |
>>> cmip5_trans.drs_to_file(drs2) |
| 73 |
'tas_3hr_HadCM3_historicalNat_r1_2010-2011.nc' |
| 74 |
|
| 75 |
|