OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
dtdb.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # encoding: utf-8
3 
4 import os
5 import sys
6 import math
7 import time
8 import argparse
9 import xarray as xr
10 import numpy as np
11 from dtocean import dtocean
12 from dbocean import dbocean
13 
14 NWL = 7
15 
16 class input(object):
17 
18  def __init__(self, l1b_filepath):
19  self.ifile = l1b_filepath
20  print ("Reading sensor data: " + self.ifile)
21  try:
22  self.rfl = np.append(xr.load_dataset(l1b_filepath,group='/reflectance')['toa_reflectance'][2:7,:,:],
23  xr.load_dataset(l1b_filepath,group='/reflectance')['toa_reflectance'][8:,:,:],axis=0)
24  self.sza = xr.load_dataset(l1b_filepath,group='/geolocation')['solar_zenith'].values
25  self.vza = xr.load_dataset(l1b_filepath,group='/geolocation')['sensor_zenith'].values
26  self.raa = xr.load_dataset(l1b_filepath,group='/geolocation')['relative_azimuth'].values
27  self.lat = xr.load_dataset(l1b_filepath,group='/navigation_data')['latitude'].values
28  self.lon = xr.load_dataset(l1b_filepath,group='/navigation_data')['longitude'].values
29  self.cld = xr.load_dataset(l1b_filepath,group='/ancillary')['cloud_mask'].values
30  self.wnd = xr.load_dataset(l1b_filepath,group='/ancillary')['wind_speed'].values
31  self.chl = xr.load_dataset(l1b_filepath,group='/ancillary')['chlorophyll'].values
32  self.rfl = np.transpose(self.rfl, (1,2,0))
33  self.shape = self.sza.shape
34  except Exception as inst:
35  print(type(inst))
36  print(inst)
37  print ("Unable to read from file ... exiting")
38  sys.exit()
39 
40 class output(object):
41 
42  def __init__(self, out_filepath, ydim, xdim):
43  self.ofile = out_filepath
44  self.ydim = ydim
45  self.xdim = xdim
46  try:
47  rfl = xr.DataArray(np.zeros((NWL,ydim,xdim)),dims=('wl','y', 'x'))
48  lat = xr.DataArray(np.zeros((ydim,xdim)),dims=('y', 'x'))
49  lon = xr.DataArray(np.zeros((ydim,xdim)),dims=('y', 'x'))
50  sza = xr.DataArray(np.zeros((ydim,xdim)),dims=('y', 'x'))
51  vza = xr.DataArray(np.zeros((ydim,xdim)),dims=('y', 'x'))
52  raa = xr.DataArray(np.zeros((ydim,xdim)),dims=('y', 'x'))
53  wnd = xr.DataArray(np.zeros((ydim,xdim)),dims=('y', 'x'))
54  chl = xr.DataArray(np.zeros((ydim,xdim)),dims=('y', 'x'))
55  aot = xr.DataArray(np.zeros((ydim,xdim)),dims=('y', 'x'))
56  fmf = xr.DataArray(np.zeros((ydim,xdim)),dims=('y', 'x'))
57  sse = xr.DataArray(np.zeros((ydim,xdim)),dims=('y', 'x'))
58  self.ds = xr.Dataset({'rfl': rfl, 'lat': lat, 'lon': lon,
59  'sza': sza, 'vza': vza, 'raa': raa,
60  'wnd': wnd, 'chl': chl, 'aot': aot,
61  'fmf': fmf, 'sse': sse, })
62  except Exception as inst:
63  print(type(inst))
64  print(inst)
65  print ("Unable to initialize output file ... exiting")
66  sys.exit()
67 
68  def write(self):
69  print ("Writing to file: " + self.ofile)
70  self.ds.to_netcdf(self.ofile)
71 
72 def main():
73 
74  parser = argparse.ArgumentParser()
75  parser.add_argument("-af", "--alg",
76  help="algorithm name", required=True)
77  parser.add_argument("-if", "--ifile", type=argparse.FileType('r'),
78  help="input file", required=True)
79  parser.add_argument("-of", "--ofile", type=argparse.FileType('w'),
80  help="output file", required=False)
81  parser.add_argument("-lf", "--lut", type=argparse.FileType('r'),
82  help="lookup table file", required=True)
83  parser.add_argument("-pf", "--plot", type=bool, default=False,
84  help="plot pixel data", required=False)
85  parser.add_argument("-mf", "--mode", type=int, default=0,
86  help="mode option", required=False)
87  parser.add_argument("y", type=int, help="start line")
88  parser.add_argument("x", type=int, help="start pixel")
89  parser.add_argument("z", type=int, help="square side ")
90 
91  args = parser.parse_args()
92  vin = input(args.ifile.name)
93  dimx = dimy = args.z
94  if (args.z <= 0):
95  args.y = args.x = 0
96  dimy = vin.shape[0]
97  dimx = vin.shape[1]
98 
99  vout = output(args.ofile.name, dimy, dimx)
100 
101  if (args.alg == "deepblue"):
102  dtdb = dbocean(args.lut.name, args.mode)
103  elif (args.alg == "darktarget"):
104  dtdb = dtocean(args.lut.name, args.mode)
105  else:
106  print ("invalid algorithm")
107  sys.exit()
108 
109  print ("Processing mode {0}".format(args.mode))
110  for iy in range(args.y, args.y+dimy):
111  tic = time.perf_counter()
112  tip =0
113  for ix in range(args.x, args.x+dimx):
114 
115  if(vin.cld[iy,ix] or vin.chl[iy,ix]<-999):
116  fmf,aot,chl,wnd,sse = -999.9, -999.9, -999.9, -999.9, -999.9
117  else:
118  try:
119  fmf,aot,chl,wnd,sse = dtdb.process(vin.rfl[iy,ix],vin.sza[iy,ix],
120  vin.vza[iy,ix],vin.raa[iy,ix],
121  vin.wnd[iy,ix],vin.chl[iy,ix])
122  except Exception as inst:
123  print(type(inst))
124  print(inst)
125  print ("processing error at pixel", iy, ix)
126 
127  if(args.plot):
128  dbdt.plot(iy,ix)
129 
130  vout.ds.rfl[:,iy-args.y,ix-args.x] = vin.rfl[iy,ix]
131  vout.ds.lat[iy-args.y,ix-args.x] = vin.lat[iy,ix]
132  vout.ds.lon[iy-args.y,ix-args.x] = vin.lon[iy,ix]
133  vout.ds.sza[iy-args.y,ix-args.x] = vin.sza[iy,ix]
134  vout.ds.vza[iy-args.y,ix-args.x] = vin.vza[iy,ix]
135  vout.ds.raa[iy-args.y,ix-args.x] = vin.raa[iy,ix]
136  vout.ds.wnd[iy-args.y,ix-args.x] = wnd
137  vout.ds.chl[iy-args.y,ix-args.x] = chl
138  vout.ds.aot[iy-args.y,ix-args.x] = aot
139  vout.ds.fmf[iy-args.y,ix-args.x] = fmf
140  vout.ds.sse[iy-args.y,ix-args.x] = sse
141 
142  toc = time.perf_counter()
143  tpp = (toc-tic)/dimx
144 # print('.', end = '', flush=True)
145  print(iy, tpp, flush=True)
146  print(' Done!')
147  vout.write()
148 
149 if __name__ == "__main__":
150 
151  main()
def write(self)
Definition: dtdb.py:68
def main()
Definition: dtdb.py:72
character(len=1000) if
Definition: names.f90:13
def __init__(self, out_filepath, ydim, xdim)
Definition: dtdb.py:42
def __init__(self, l1b_filepath)
Definition: dtdb.py:18