4 * DESCRIPTION: Executable program to automate download of L1A files in specified date-time range.
6 * USAGE: dtdb_oci.py -b [start date-time] -e [end date-time] -o [output directory]
8 * Created on August, 2021
10 * Author: Samuel Anderson
15 from datetime
import datetime, date
16 import viirs_files
as vf
17 import optparse
as optparse
19 LOG = logging.getLogger(
'dtdb_oci')
22 str_url =
"https://oceandata.sci.gsfc.nasa.gov/ob/getfile/"
23 str_wget =
"wget --load-cookies ~/.urs_cookies --save-cookies ~/.urs_cookies --auth-no-challenge=on --content-disposition "
28 dircontents = os.listdir(inpath)
33 infilepath = inpath + x
34 if not os.path.isfile(infilepath):
36 files.append(infilepath)
40 tot_min = hour*6000 + min
47 min = tot_min - h*6000
51 day_of_year = date(year, month, day).timetuple().tm_yday
55 daystr =
'{:03}'.format(day_of_year)
56 dt = datetime.strptime(
str(year)+
"-"+daystr,
"%Y-%j")
57 return dt.year, dt.month, dt.day
60 DtFilename = l1b_name.replace(
"L1B",
"L2.AER_DT")
64 DbFilename = l1b_name.replace(
"L1B",
"L2.AER_DB")
75 description =
'''Download L1A files in specified date-time range.'''
76 usage =
"usage: get_viirs_l1a.py -b [start date-time] -e [end date-time] -o [output directory]"
79 parser = optparse.OptionParser(description=description,usage=usage,version=version)
82 mandatoryGroup = optparse.OptionGroup(parser,
"Mandatory Arguments",
83 "At a minimum these arguments must be specified")
85 parser.add_option_group(mandatoryGroup)
87 mandatoryGroup.add_option(
'-b',
'--begin',
91 help=
"Begin date-time")
93 mandatoryGroup.add_option(
'-e',
'--end',
99 mandatoryGroup.add_option(
'-d',
'--alg',
103 help=
"Dark Target (dt) or Deep Blue (db)")
105 mandatoryGroup.add_option(
'--l2_par',
109 help=
"The full path of the L2 PCF")
112 optionalGroup = optparse.OptionGroup(parser,
"Extra Options",
113 "These options may be used to customize behavior of this program.")
115 optionalGroup.add_option(
'--geo',
119 help=
"Write geolocation group (default no)")
121 optionalGroup.add_option(
'--anc',
125 help=
"Write ancillary group (default no)")
127 optionalGroup.add_option(
'--obs',
131 help=
"Write observations group (default no)")
133 optionalGroup.add_option(
'--glint',
137 help=
"Do not mask glint (default no)")
139 optionalGroup.add_option(
'--cloud',
143 help=
"Do not mask clouds (default no)")
145 optionalGroup.add_option(
'--float',
149 help=
"Floating point rather than short integer format (default no)")
151 optionalGroup.add_option(
'-o',
'--output_path',
155 help=
"The full path of the target directory for L1A files")
157 parser.add_option(
'-v',
'--verbose',
161 help=
'each occurrence increases verbosity 1 level from ERROR: -v=WARNING -vv=INFO -vvv=DEBUG')
163 parser.add_option_group(optionalGroup)
166 (options, args) = parser.parse_args()
169 levels = [logging.ERROR, logging.WARN, logging.INFO, logging.DEBUG]
170 logging.basicConfig(level = levels[options.verbosity])
176 isMissingMand =
False
177 for m,m_err
in zip(mandatories,mand_errors):
178 if not options.__dict__[m]:
182 parser.error(
"Incomplete mandatory arguments, aborting...")
185 options.outputPath = os.path.expanduser(options.odir)
189 LOG.info(
'Downloading L1B in %s' % (options.odir) )
191 if os.path.isdir(options.outputPath):
192 o_path = options.outputPath
193 elif os.path.isdir(os.path.dirname(options.outputPath)):
194 o_path = options.outputPath
197 print (
"Output path invalid")
200 str_l1b_dir = o_path +
"/l1b/"
201 if not os.path.isdir(str_l1b_dir):
202 os.mkdir(str_l1b_dir)
203 str_dt_dir = o_path +
"/dt/"
204 if not os.path.isdir(str_dt_dir):
206 str_db_dir = o_path +
"/db/"
207 if not os.path.isdir(str_db_dir):
209 str_anc_dir = o_path +
"/anc/"
210 if not os.path.isdir(str_anc_dir):
211 os.mkdir(str_anc_dir)
212 str_cld_dir = o_path +
"/cld/"
213 if not os.path.isdir(str_cld_dir):
214 os.mkdir(str_cld_dir)
218 byear =
int(options.begin[0:4])
219 bday =
int(options.begin[4:7])
220 bhour =
int(options.begin[7:9])
221 bmin = ((
int(options.begin[9:13]) + 499)//500) * 500
223 eyear =
int(options.end[0:4])
224 eday =
int(options.end[4:7])
225 ehour =
int(options.end[7:9])
226 emin = (
int(options.end[9:13])//500) * 500
230 if options.alg ==
"darktarget":
231 algs = [
"darktarget"]
232 elif options.alg ==
"deepblue":
234 elif options.alg ==
"all":
235 algs = [
"darktarget",
"deepblue"]
237 print (
"No algorithm specified. Exiting ...")
241 print (
"Beginning and end years must be the same. Exiting ...")
246 tal = etmin + t24*(eday - bday)
250 year, month, day =
day2date(byear, bday+d)
251 str_date_time =
"%04d%02d%02dT%02d%04d" % (byear, month, day, h, s)
252 str_l1b_name =
"PACE_OCI_SIM." + str_date_time +
".L1B.V7.nc"
253 print (
"Processing: " + str_l1b_name)
254 sfl1b = str_l1b_dir + str_l1b_name
256 if (
not sfl1b
in l1b_files):
257 srpath = str_url + str_l1b_name
258 command = str_wget + str_wdir + str_l1b_dir +
" " + srpath
260 result = os.system(command)
262 str_cld_name =
"PACE_OCI_SIM." + str_date_time +
".L2.CLDMASK.nc"
263 sfcld = str_cld_dir + str_cld_name
265 if (
not sfcld
in cld_files):
266 srpath = str_url + str_cld_name
267 command = str_wget + str_wdir + str_cld_dir +
" " + srpath
269 result = os.system(command)
271 year, month, day =
day2date(byear, bday+d)
272 str_date_hour =
"%04d%02d%02dT%02d%04d" % (byear, month, day, h, 0)
273 str_anc_name =
"GMAO_MERRA2." + str_date_hour +
".MET.nc"
274 gdas1 = str_anc_dir + str_anc_name
276 if not gdas1
in anc_files:
277 arpath = str_url + str_anc_name
278 command = str_wget + str_wdir + str_anc_dir +
" " + arpath
280 result = os.system(command)
283 year, month, day =
day2date(byear, bday+d+1)
284 str_date_hour =
"%04d%02d%02dT%02d%04d" % (byear, month, day, 0, 0)
286 str_date_hour =
"%04d%02d%02dT%02d%04d" % (byear, month, day, h+1, 0)
287 str_anc_name =
"GMAO_MERRA2." + str_date_hour +
".MET.nc"
288 gdas2 = str_anc_dir + str_anc_name
289 if not gdas2
in anc_files:
290 arpath = str_url + str_anc_name
291 command = str_wget + str_wdir + str_anc_dir +
" " + arpath
293 result = os.system(command)
296 if alg ==
"deepblue":
297 alg_str =
"deepblue "
300 alg_str =
"darktarget "
302 l2par = options.l2_par
303 ialg =
" alg=" + alg_str
304 il1b =
" ifile=" + sfl1b
305 ianc1 =
" gdas1=" + gdas1
306 ianc2 =
" gdas2=" + gdas2
307 icld =
" cldmask=" + sfcld
308 opth =
" ofile=" + opath
309 ogeo =
" geolocation" if options.geo
else ""
310 oanc =
" ancillary" if options.anc
else ""
311 oobs =
" observations" if options.obs
else ""
312 oglnt =
" mask_glint=off" if options.glint
else ""
313 ocld =
" mask_cloud=off" if options.cloud
else ""
314 oflt=
" short_format=off" if options.float
else ""
315 lprw =
" lines_per_rw=10"
316 command =
"dtdb " + l2par + ialg + il1b + ianc1 + ianc2 + icld + opth + ogeo + oanc + oobs + oglnt + ocld + oflt + lprw
318 result = os.system(command)
320 result = os.system(command)
325 print (
"Invalid start and end dates specified. Exiting ...")
328 print (
"All L1A files downloaded")
330 LOG.info(
'Exiting...')
333 if __name__==
'__main__':