locator.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. """Locates input files and calculates namelist attributes
  2. Typically a user will have files distributed throughout the hard drive
  3. and it makes little or no sense to try to capture all use cases.
  4. >>> date = "20130101"
  5. >>> namobs = observations(date, types=["profbfiles"])
  6. >>> namooo, namcl4 = forecasts(date, types=["forecast", "persistence"], lead_times=[12, 36, 60])
  7. """
  8. def observations(date, types=None):
  9. """Responsible for locating observation files
  10. Valid **namobs** observation types.
  11. * profbfiles
  12. * sstfbfiles
  13. * slafbfiles
  14. :param date: The verification date in string format ``'%Y%m%d'``
  15. :param types: A list of namobs observation types
  16. :returns: namobs namelist dictionary
  17. """
  18. namobs = {"ln_t3d": False,
  19. "ln_s3d": False,
  20. "ln_ena": False,
  21. "ln_profb": False,
  22. "ln_sst": False,
  23. "ln_sstfb": False,
  24. "ln_reysst": False,
  25. "ln_ghrsst": False,
  26. "ln_sla": False,
  27. "ln_slafb": False,
  28. "ln_sladt": False}
  29. if types is None: types = []
  30. for obtype in types:
  31. if obtype == "profbfiles":
  32. namobs[obtype] = profbfiles(date)
  33. namobs["ln_t3d"] = True
  34. namobs["ln_s3d"] = True
  35. namobs["ln_profb"] = True
  36. elif obtype == "sstfbfiles":
  37. namobs[obtype] = sstfbfiles(date)
  38. namobs["ln_sst"] = True
  39. namobs["ln_sstfb"] = True
  40. elif obtype == "slafbfiles":
  41. namobs[obtype] = slafbfiles(date)
  42. namobs["ln_sla"] = True
  43. namobs["ln_slafb"] = True
  44. return namobs
  45. def profbfiles(date):
  46. """Observation file locator stub
  47. .. note:: User-specified stub
  48. :param date: The verification date in string format ``'%Y%m%d'``
  49. :returns: List of files
  50. """
  51. files = ['profb.nc']
  52. return files
  53. def sstfbfiles(date):
  54. """Observation file locator stub
  55. .. note:: User-specified stub
  56. :param date: The verification date in string format ``'%Y%m%d'``
  57. :returns: List of files
  58. """
  59. files = ['sstfb.nc']
  60. return files
  61. def slafbfiles(date):
  62. """Observation file locator stub
  63. .. note:: User-specified stub
  64. :param date: The verification date in string format ``'%Y%m%d'``
  65. :returns: List of files
  66. """
  67. files = ['slafb.nc']
  68. return files
  69. def forecasts(date, types=None, lead_times=None):
  70. """Responsible for locating forecast fields
  71. :param date: The verification date in string format ``'%Y%m%d'``
  72. :param types: A list of forecast system types
  73. :param lead_times: A list of lead_times to search for
  74. :returns: tuple of namelist data, (namooo, namcl4)
  75. """
  76. namooo = {}
  77. namcl4 = {}
  78. if types is None: types = []
  79. if lead_times is None: lead_times = []
  80. # Initialise data
  81. ooo_files = []
  82. nn_ooo_idx = []
  83. cl4_vars = []
  84. cl4_fcst_idx = []
  85. # Search for files
  86. for type in types:
  87. files = []
  88. in_indices = []
  89. out_indices = []
  90. for ilt, lead_time in enumerate(lead_times):
  91. file, index = field(date, type=type, lead_time=lead_time)
  92. files.append(file)
  93. in_indices.append(index)
  94. out_indices.append(ilt + 1)
  95. # Expand return lists
  96. ooo_files += files
  97. nn_ooo_idx += in_indices
  98. cl4_fcst_idx += out_indices
  99. cl4_vars += len(files) * [type]
  100. # Namoo
  101. namooo["ooo_files"] = ooo_files
  102. namooo["nn_ooo_idx"] = nn_ooo_idx
  103. # Namcl4
  104. namcl4["cl4_vars"] = cl4_vars
  105. namcl4["cl4_fcst_idx"] = cl4_fcst_idx
  106. return namooo, namcl4
  107. def field(date, type=None, lead_time=None):
  108. """Forecast field locator
  109. Maps verification date and lead time off set to file name and
  110. index along file *time_counter*
  111. .. note:: User-specified stub
  112. :param date: The verification date in string format ``'%Y%m%d'``
  113. :param type: Forecast type
  114. :param lead_time: Forecast off set
  115. :returns: (**path**, **index**)
  116. """
  117. # Worker function
  118. file = 'nofile'
  119. index = -1
  120. return file, index