in analysis/webservice/algorithms/MapFetchHandler.py [0:0]
def generate(self, ds, granule_name, prefix, ct, interp, _min, _max, width, height, time_interval):
color_table_name = ct
if ct is None:
color_table_name = "smap"
color_table = colortables.__dict__[color_table_name]
interpolation = interp
if interp is None:
interpolation = "near"
force_min = _min
force_max = _max
if _min is None:
force_min = np.nan
if _max is None:
force_max = np.nan
temp_width = width
temp_height = height
if width is None:
temp_width = 1024
if height is None:
temp_height = 512
width = np.min([8192, temp_width])
height = np.min([8192, temp_height])
if time_interval == 'day':
time_interval = relativedelta(days=+1)
else:
time_interval = relativedelta(months=+1)
stats = self._get_tile_service().get_dataset_overall_stats(ds)
start_time, end_time = self._get_tile_service().get_min_max_time_by_granule(ds, granule_name)
MRF.create_all(ds, prefix)
# Make a temporary directory for storing the .png and .tif files
temp_dir = '/tmp/tmp/'
try:
os.makedirs(temp_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
while start_time <= end_time:
one_interval_later = start_time + time_interval
temp_end_time = one_interval_later - relativedelta(minutes=+1) # prevent getting tiles for 2 intervals
ds1_nexus_tiles = self._get_tile_service().find_tiles_in_box(-90.0, 90.0, -180.0, 180.0, ds, start_time,
temp_end_time)
if ds1_nexus_tiles is not None:
img = self.__create_global(ds1_nexus_tiles, stats, width, height, force_min, force_max, color_table,
interpolation)
else:
img = self.__create_no_data(width, height)
imgByteArr = io.BytesIO()
img.save(imgByteArr, format='PNG')
imgByteArr = imgByteArr.getvalue()
arr = str(start_time).split() # arr[0] should contain a string of the date in format 'YYYY-MM-DD'
fulldate = arr[0]
temp_png = temp_dir + fulldate + '.png'
temp_tif = temp_dir + fulldate + '.tif'
open(temp_png, 'wb').write(imgByteArr)
arr = fulldate.split('-')
year = arr[0]
month = calendar.month_abbr[int(arr[1])]
dt = month + '_' + year
retcode = MRF.png_to_tif(temp_png, temp_tif)
if retcode == 0:
retcode = MRF.geo_to_mrf(temp_tif, prefix, year, dt, ds)
if retcode == 0:
retcode = MRF.geo_to_arctic_mrf(temp_tif, prefix, year, dt, ds)
if retcode == 0:
retcode = MRF.geo_to_antarctic_mrf(temp_tif, prefix, year, dt, ds, interp)
if retcode != 0:
break
start_time = one_interval_later
tar_file = ds + '.tar.gz'
retcode = call(["tar", "-zcvf", tar_file, ds])
if retcode == 0:
# Delete temporary files/folders if tar.gz is created successfully
call(["rm", "-rf", ds])
call(["rm", "-rf", temp_dir])
else:
print("Error creating tar.gz")
# Upload the tar.gz file to the sea-level-mrf S3 bucket
s3bucket = 'sea-level-mrf'
s3client = boto3.client('s3')
try:
with open(tar_file, 'rb') as data:
s3client.upload_fileobj(data, s3bucket, tar_file)
except Exception as e:
print(("Unable to add tar.gz to S3: \n" + str(e)))
call(["rm", "-rf", tar_file]) # Delete the tar.gz from local storage