OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
GIBSmetadataUtils.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 #import hashlib
3 import datetime
4 from os import path
5 import sys
6 import argparse
7 from netCDF4 import Dataset
8 
9 __version__ = '1.1.0_2019-10-28'
10 
11 def gring_to_geopolygon(lats, lons):
12  merged = [None]*(len(lons)+len(lats))
13  merged[::2] = lons
14  merged[1::2] = lats
15 
16 # yeah, not very pythonic, but it works mate ;)
17  gPolygon = ''
18  i=-1
19  for element in merged:
20  delimiter = ' '
21  if i%2:
22  delimiter = ','
23  if i==-1:
24  delimiter = ''
25  gPolygon = gPolygon + delimiter + str(element)
26  i = i+1
27  gPolygon = gPolygon + ',' + str(merged[0]) + ' ' + str(merged[1])
28  return gPolygon
29 
30 
31 
32 def create_granule_metadata(browseFileName, fileBaseName, starttime, stoptime, datadays):
33  '''This method writes the xml metadata for the GIBS imagery'''
34 
35  productionTime = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
36  xmlbody = '''<ImageryMetadata
37  xmlns="http://www.w3.org/2005/Atom"
38  xmlns:georss="http://www.georss.org/georss/10">
39  <ProviderProductId>%s</ProviderProductId>
40  <ProductionDateTime>%s</ProductionDateTime>
41  <DataStartDateTime>%s</DataStartDateTime>
42  <DataEndDateTime>%s</DataEndDateTime>
43  <DataDay>%s</DataDay>
44 </ImageryMetadata>
45  ''' % (path.basename(browseFileName),
46  productionTime,
47  starttime,
48  stoptime,datadays[0].strftime("%Y%j"))
49 
50  return xmlbody
51 
52 
53 
54 #def generateSHA1(filepath):
55 # BLOCKSIZE = 65536
56 # sha1 = hashlib.sha1()
57 # with open(filepath, 'rb') as afile:
58 # buf = afile.read(BLOCKSIZE)
59 # while len(buf) > 0:
60 # sha1.update(buf)
61 # buf = afile.read(BLOCKSIZE)
62 # return sha1.hexdigest()
63 # sha1sum "$1"|cut -d' ' -f1
64 # cmd = 'sha1sum %s' % filepath
65 # sha1 = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
66 # return sha1.split(' ')[0]
67 
68 
69 
70 def main():
71  """
72  Write XML metadata file for GIBS browse imagery
73  """
74  parser = argparse.ArgumentParser(description=\
75  'Generate XML metadata file for GIBS browse imagery')
76  parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
77  parser.add_argument('-i','--input_file', type=str, required=True)
78  parser.add_argument('-t','--tiff_file', type=str, required=True)
79  parser.add_argument('-o','--output_file', type=str)
80  parser.add_argument('-d','--datadays', nargs='+', type=str, help="dataday(s) in YYYDDD or YYYY-MM-DD format")
81  parser.add_argument('-v','--verbose', action='store_true')
82 
83  args = parser.parse_args()
84 
85  rootgrp = Dataset(args.input_file, "r", format="NETCDF4")
86  starttime = rootgrp.getncattr("time_coverage_start")
87  stoptime = rootgrp.getncattr("time_coverage_end")
88 # navgrp = rootgrp.createGroup("navigation_data")
89 # gRingLats = navgrp.getncattr("gringpointlatitude");
90 # gRingLons = navgrp.getncattr("gringpointlongitude");
91 # gPolygon = gring_to_geopolygon(gRingLats,gRingLons)
92 
93  datadays = []
94  if args.datadays:
95  for dataday in args.datadays:
96  if len(dataday) == 7:
97  datadays.append(datetime.datetime.strptime(dataday, "%Y%j"))
98  elif len(dataday) == 10:
99  datadays.append(datetime.datetime.strptime(dataday, "%Y-%m-%d"))
100  else:
101  print("Could not parse data day: %s" % dataday)
102  sys.exit(1)
103  else:
104  datadays.append(datetime.datetime.strptime(starttime[:10], "%Y-%m-%d"))
105 
106  outputFile = args.tiff_file + '.xml'
107  if args.output_file:
108  outputFile = args.output_file
109 
110  if args.verbose:
111  print("StartTime: %s" % starttime)
112  print("StopTime: %s" % stoptime)
113  print("Input file: %s" % args.input_file)
114  print("TIFF file: %s" % args.tiff_file)
115  print("Output file: %s" % outputFile)
116  for cnt,dataday in enumerate(datadays):
117  print("DataDay[%d]: %s" % (cnt,dataday.strftime("%Y%j")))
118 # print("gRingLats: %s" % gRingLats)
119 # print("gRingLons: %s" % gRingLons)
120 # print("gPolygon: %s" % gPolygon)
121 
122  xmlbody = create_granule_metadata(args.tiff_file, args.input_file, starttime, stoptime, datadays)
123 
124  xmlfile = open(outputFile,'w')
125  xmlfile.write(xmlbody)
126  xmlfile.close()
127 
128  if args.verbose:
129  print("XML for %s:" % outputFile)
130  print(xmlbody)
131 
132 # The following allows the file to be imported without immediately executing.
133 if __name__ == '__main__':
134  sys.exit(main())
135 
def create_granule_metadata(browseFileName, fileBaseName, starttime, stoptime, datadays)
def gring_to_geopolygon(lats, lons)
const char * str
Definition: l1c_msi.cpp:35