Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision a71f4aa5dd65c9b8bf679213700c009a498c3ce7 authored by S M T Chua on 02 March 2022, 04:59:20 UTC, committed by GitHub on 02 March 2022, 04:59:20 UTC
Merge pull request #381 from GeoscienceAustralia/sc/ghpages
Update the documentation / github pages workflow
2 parent s 8a8fc15 + 39c81a0
  • Files
  • Changes
  • bda5d26
  • /
  • utils
  • /
  • make_tscuml_animation.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:a71f4aa5dd65c9b8bf679213700c009a498c3ce7
directory badge
swh:1:dir:c7a680498eef8588cab00ef884f2ebfc956b70f7
content badge
swh:1:cnt:827a41f9e0361d50e37a221d5688cbbae3ce0615

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
make_tscuml_animation.py
#   This Python module is part of the PyRate software package.
#
#   Copyright 2022 Geoscience Australia
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
"""
This python script can be used to make an animated gif of PyRate cumulative time series products.

Usage: python3 utils/make_tscuml_animation.py <path to PyRate outdir>
"""
import rasterio
import matplotlib.pyplot as plt
import matplotlib.backend_bases
import numpy as np
import os, sys, re
import xarray as xr
from datetime import datetime as dt
import matplotlib.animation as animation

if len(sys.argv) != 2:
    print('Exiting: Provide abs path to <outdir> as command line argument')
    exit()
else:
    path = sys.argv[1]
    print(f"Looking for PyRate products in: {path}")
#################################

# Reading velocity data
with rasterio.open(os.path.join(path, 'velocity_dir', 'linear_rate.tif')) as src2:
    vel = src2.read()
    bounds2 = src2.bounds
    x_coord2 = np.linspace(bounds2[0], bounds2[2], src2.width)
    y_coord2 = np.linspace(bounds2[1], bounds2[3], src2.height)
    # grab list of time series dates from metadata
    ed = src2.tags()['EPOCH_DATE']

# convert metadata string to list of strings
date_str = re.findall(r'\'(.+?)\'', ed)

# make velocity xarray (also apply deramp to the velocity map directly)
dac2 = xr.DataArray(vel[0,:,:], coords={'lon': x_coord2, 'lat': y_coord2}, dims=['lat', 'lon'])
vs = xr.Dataset()
vs['vel'] = dac2
longitude = vs.coords['lon']
latitude = vs.coords['lat']

# pre-allocate a 3D numpy array to read the tscuml tiff raster bands to
# include first 'zero' time slice, which PyRate does not save to disk
tscuml = np.zeros((len(date_str), vel.shape[1], vel.shape[2])) # commented Chandra

# reading *tif files and generate cumulative variable
print('Reading tscuml files:')
for i, d in enumerate(date_str[1:]):
    print(i+1, 'tscuml_' + d + '.tif')
    with rasterio.open(os.path.join(path, 'timeseries_dir', 'tscuml_' + d + '.tif')) as src:
        data = src.read()
        bounds = src.bounds
        x_coord = np.linspace(bounds[0], bounds[2], src.width)
        y_coord = np.linspace(bounds[1], bounds[3], src.height)
    tscuml[i+1, :, :] = np.squeeze(data, axis=(0,)) # commented chandra

# copy Nans in first time slice to zero epoch
zeroepoch = np.zeros((vel.shape[1], vel.shape[2]))
zeroepoch[np.isnan(tscuml[1,:,:])] = np.nan
tscuml[0,:,:] = zeroepoch

# convert date strings to datetime objects
imdates_dt = [dt.strptime(x, '%Y-%m-%d') for x in date_str]

# make tscuml xarray
dac = xr.DataArray(tscuml, coords={'time': imdates_dt, 'lon': x_coord, 'lat': y_coord}, dims=['time', 'lat', 'lon'])
ds = xr.Dataset()
ds['tscuml'] = dac
n_im, length, width = tscuml.shape

# Add max and min displacement range
#refx1 = int(len(x_coord2)/ 2)
#refx2 = int(len(x_coord2)/ 2) + 1
#refy1 = int(len(y_coord2)/ 2)
#refy2 = int(len(y_coord2)/ 2) + 1
#refvalue_lastepoch = np.nanmean(tscuml[-1, refy1:refy2, refx1:refx2]) # reference values

auto_crange: float = 100
dmin_auto = np.nanpercentile(tscuml, 100 - auto_crange)
dmax_auto = np.nanpercentile(tscuml, auto_crange)
# find the absolute max displacement; round to nearest 10 units, for colour bar limits
lim = np.round(np.amax(np.array([np.abs(dmin_auto), np.abs(dmax_auto)])), decimals=-1)
#dmin = dmin_auto - refvalue_lastepoch
#dmax = dmax_auto - refvalue_lastepoch

####  ANIMATION of LOS Cumulative displacement TS
fig = plt.figure('PyRate Cumulative Displacement Animation', figsize=(5,5))
faxv = fig.add_axes([0.15,0.15,0.75,0.75])
cmap = matplotlib.cm.Spectral_r
cmap.set_bad('grey',1.) # filled grey color to nan value
ims = [] # pre-allocate list for appending time slices

# loop over all timeslices, including zero epoch
for ii in range(0,len(imdates_dt)):
    im = faxv.imshow(ds.tscuml[ii], cmap=cmap, alpha=1, origin='upper',extent=[ds.coords['lon'].min(),
         ds.coords['lon'].max(), ds.coords['lat'].min(), ds.coords['lat'].max()], clim=[-lim, lim])
    title = fig.text(0.40, 0.90, "Date: {}".format(imdates_dt[ii].date()), fontsize=12, va='bottom' )
    ims.append([im, title])

fcbr = fig.colorbar(im, orientation='horizontal')
fcbr.set_label('LOS Displacement [mm]')
ani = animation.ArtistAnimation(fig, ims, interval=500, blit=False)
#plt.show()
file = path + '/timeseries_dir/' +'tscuml_animation.gif'
ani.save(file, writer='imagemagick', fps=10, dpi=100)
print('Animation saved to ' + file)
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API