OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
test_tmatrix.py
Go to the documentation of this file.
1 """
2 Sam Anderson
3 October 2017
4 """
5 
6 import unittest
7 import numpy as np
8 from pytmatrix.tmatrix import tmatrix
9 
10 
11 #some allowance for rounding errors etc
12 epsilon = 1e-7
13 
14 
15 def printUsage():
16  print ""
17  print "Usage: read_obc_cal_data BAND START_DATE END_DATE OUTPUT_DRECTORY"
18  print "Example: read_obc_cal_data M6 20131130 20131202 /home/output_dir"
19 
20 def getDaysInMonth(year,month):
21  DaysInMonth = [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
22 
23  if (year == 2012 or year == 2016) and month == 2:
24  return 29
25  else:
26  return DaysInMonth[month]
27 
28 def getDateString(year,month,day):
29  if day < 10:
30  str_day = "0" + str(day)
31  else:
32  str_day = str(day)
33 
34  if month < 10:
35  str_month = "0" + str(month)
36  else:
37  str_month = str(month)
38 
39  str_date = str(year) + str_month + str_day
40 
41  return str_date
42 
43 def getExtensibleAtom(dataset_dtype):
44  if dataset_dtype.name == "int8":
45  extensible_atom = tables.Int8Atom()
46  if dataset_dtype.name == "uint8":
47  extensible_atom = tables.UInt8Atom()
48  elif dataset_dtype.name == "int16":
49  extensible_atom = tables.Int16Atom()
50  elif dataset_dtype.name == "uint16":
51  extensible_atom = tables.UInt16Atom()
52  elif dataset_dtype.name == "int32":
53  extensible_atom = tables.Int32Atom()
54  elif dataset_dtype.name == "uint32":
55  extensible_atom = tables.UInt32Atom()
56  elif dataset_dtype.name == "int64":
57  extensible_atom = tables.Int64Atom()
58  elif dataset_dtype.name == "uint64":
59  extensible_atom = tables.UInt64Atom()
60  elif dataset_dtype.name == "float32":
61  extensible_atom = tables.Float32Atom()
62  elif dataset_dtype.name == "float64":
63  extensible_atom = tables.Float64Atom()
64 
65  return extensible_atom
66 def run_tests():
67  """Tests for the T-matrix code.
68 
69  Runs several tests that test the code. All tests should return ok.
70  If they don't, please contact the author.
71  """
72  suite = unittest.TestLoader().loadTestsFromTestCase(TMatrixTests)
73  unittest.TextTestRunner(verbosity=2).run(suite)
74 
75 
76 class TMatrixTests(unittest.TestCase):
77 
78  def test_reference(self):
79  """
80  Test basic table generation fortran code
81  """
82  tmr = tmatrix()
83  (reff,veff,cext,csca,albedo,asym,f) = tmr.generate_tables()
84 
85  print "\nEffective radius = " + str(reff)
86  print "Effective volume = " + str(veff)
87  print "Extinction Cross Section = " + str(cext)
88  print "Scattering Cross Section = " + str(csca)
89  print "Albedo = " + str(albedo)
90  print "Asymmetry = " + str(asym)
91  print "Scatter = " + str(f)
92 
93  def test_netcdf(self, output):
94  """
95  Test basic table generation fortran code
96  """
97  tmr = tmatrix()
98  (reff,veff,cext,csca,albedo,asym,f) = tmr.generate_tables()
99 
100  str_name = getDateString(2018,02,07)
101  str_out_dir = "/accounts/ssander2/test/t_matrix_random"
102  out_filename = "T-MATRIX_" + str_name + ".nc"
103  out_file_path = str_out_dir + "/" + out_filename
104 
105  try:
106  ncfileout = tables.openFile(out_file_path, "w", "Scattering matrices")
107  except Exception, e:
108  print "-- ERROR! Invalid output filename entered --"
109  sys.exit()
110 
111  extensible_float32 = tables.Float32Atom()
112  effrad_list = []
113  effvol_list = []
114  extx_list = []
115  sctrx_list = []
116  albedo_list = []
117  asymm_list = []
118  scatter_list = []
119 
120  tgroup = ncfileout.createGroup("/", "test1", 'test')
121 
122  effrad_list.append(ncfileout.createEArray(sv_group, 'Effective_Radius', extensible_float32, (0), "Effective Radius"))
123  effrad_list.append(ncfileout.createEArray(sv_group, 'Effective Volume', extensible_float32, (0), "Effective Volume"))
124  effrad_list.append(ncfileout.createEArray(sv_group, 'Extinction Cross Section', extensible_float32, (0), "Extinction Cross Section"))
125  effrad_list.append(ncfileout.createEArray(sv_group, 'Scattering Cross Section', extensible_float32, (0), "Scattering Cross Section"))
126  effrad_list.append(ncfileout.createEArray(sv_group, 'Asymmetry', extensible_float32, (0), "Asymmetry"))
127  effrad_list.append(ncfileout.createEArray(sv_group, 'Scatter', extensible_float32, (0,tmr.angles,4,4), "Scatter"))
128 
129 
130 
131 if __name__ == '__main__':
132 # unittest.main()
133  run_tests()
def getExtensibleAtom(dataset_dtype)
Definition: test_tmatrix.py:43
def getDateString(year, month, day)
Definition: test_tmatrix.py:28
def getDaysInMonth(year, month)
Definition: test_tmatrix.py:20
const char * str
Definition: l1c_msi.cpp:35