Commit f570493c authored by Clément Haëck's avatar Clément Haëck
Browse files

Clean repo

Archive some files

Remove exclusions from .gitignore:
-files not used anymore
-files & folders that are workspace/user specific
parent 9fca3ea0
......@@ -11,17 +11,5 @@ __pycache__/
*.exx
*.log
Archive
Compute/find_images/filelist.txt
Compute/find_images/input.txt
lib/conf.ini
lib/_build_ds
lib/_build_hi
SOM-Toolbox
Test/
setup_env.sh
"""Regrid mask on CHL grid."""
from os import path
import xarray as xr
import numpy as np
import lib.data.globcolour
import lib.data.mask
from lib import root_data, get_args
from lib.dask_client import make_client
from lib.xarray_utils import save_chunks_by_date
from lib.downsample_f import downsample
def get_impact_area(mask):
"""Get impacted area."""
return 1. * np.sum(mask) / mask.size
def downsample_average(array, x_in, y_in, x_out, y_out,
threshold=0.4):
"""Down sample 2d array by average.
Parameters
----------
array: 2D Array
Dimensions corresponding to `x_in` and `x_out`.
If more than 2 dimensions, other axes are stacked.
x_in, y_in: Sequence[Float]
Coordinates of input array (higher res.).
x_out, x_out: Sequence[Float]
Coordinates of output array (lower res.).
"""
# output = np.ones((array.shape[0], y_out.size, x_out.size), 'bool')
output = downsample.downscale_average_stack(x_in, y_in,
x_out, y_out,
array) > threshold
# output = array[:, :y_out.size, :x_out.size]
return output
def regrid(mask, chl, m):
lon_in = mask.lon.values
lat_in = mask.lat.values
lon_out = chl.lon.values
lat_out = chl.lat.values
mask_chl = xr.apply_ufunc(downsample_average, mask[m],
kwargs=dict(x_in=lon_in, x_out=lon_out,
y_in=lat_in, y_out=lat_out,
threshold=0.4),
input_core_dims=[['lat', 'lon']],
output_core_dims=[['lat_', 'lon_']],
output_sizes={'lat_': lat_out.size,
'lon_': lon_out.size},
output_dtypes=[mask.front.dtype],
dask='parallelized')
mask_chl.coords['lon_'] = lon_out
mask_chl.coords['lat_'] = lat_out
mask_chl = mask_chl.rename(lon_='lon', lat_='lat')
return mask_chl
## Compute
if __name__ == '__main__':
client = make_client()
region = 'GS'
days = 8
year = 2007
scale = 10.
number = 2
wd = path.join(root_data,
region,
'HI/HI_{:.1f}_{:d}/masks_chl'.format(scale, number),
'{:d}days/{:d}'.format(days, year))
mask = lib.data.mask.get_data(region, days, year, scale, number, False)
chl = lib.data.globcolour.get_data(region, days, year)
chl = chl.reindex(lat=chl.lat[::-1])
chl = chl.sel(lat=slice(*mask.lat[[0, -1]].values),
lon=slice(*mask.lon[[0, -1]].values))
ds = regrid(mask, chl, 'front').to_dataset()
# masks = [regrid(mask, chl, m) for m in ['front', 'background', 'mask']]
# ds = xr.merge(masks)
# ds.isel(time=slice(0, 5)).to_netcdf('/home/clement/test/test.nc')
# filename = path.join(wd, 'masks_chl_{:s}.nc')
# encoding = {'_all': {'zlib': True}}
save_chunks_by_date(ds, '/home/clement/test/test_{}.nc')
module downsample
implicit none
public
contains
subroutine downscale_average(x_i, y_i, nx_i, ny_i, &
x_o, y_o, nx_o, ny_o, in, out)
! Down sample by average.
! nx_i > nx_o
! x_* and y_* increasing
implicit none
integer, intent(in) :: nx_i, ny_i, nx_o, ny_o
double precision, intent(in) :: x_i(nx_i), y_i(ny_i)
double precision, intent(in) :: x_o(nx_o), y_o(ny_o)
double precision, intent(in) :: in(ny_i, nx_i)
double precision, intent(out) :: out(ny_o, nx_o)
integer :: map_x(nx_i), map_y(ny_i)
integer :: n(ny_o, nx_o)
integer :: ix, iy
integer :: jx, jy
call down_coord(x_i, nx_i, x_o, nx_o, map_x)
call down_coord(y_i, ny_i, y_o, ny_o, map_y)
out = 0.
n = 0
do ix=1, nx_i
jx = map_x(ix)
do iy=1, ny_i
jy = map_y(iy)
out(jy, jx) = out(jy, jx) + in(iy, ix)
n(jy, jx) = n(jy, jx) + 1
end do
end do
out = out / n
end subroutine downscale_average
subroutine downscale_average_stack(x_i, y_i, nx_i, ny_i, nt, &
x_o, y_o, nx_o, ny_o, in, out)
implicit none
integer, intent(in) :: nx_i, ny_i, nt, nx_o, ny_o
double precision, intent(in) :: x_i(nx_i), y_i(ny_i)
double precision, intent(in) :: x_o(nx_o), y_o(ny_o)
double precision, intent(in) :: in(nt, ny_i, nx_i)
double precision, intent(out) :: out(nt, ny_o, nx_o)
integer :: map_x(nx_i), map_y(ny_i)
integer :: n(nt, ny_o, nx_o)
integer :: ix, iy, it
integer :: jx, jy
call down_coord(x_i, nx_i, x_o, nx_o, map_x)
call down_coord(y_i, ny_i, y_o, ny_o, map_y)
out = 0.
n = 0
do it=1, nt
do ix=1, nx_i
jx = map_x(ix)
do iy=1, ny_i
jy = map_y(iy)
out(it, jy, jx) = out(it, jy, jx) + in(it, iy, ix)
n(it, jy, jx) = n(it, jy, jx) + 1
end do
end do
end do
out = out / n
end subroutine downscale_average_stack
subroutine down_coord(x_i, n_i, x_o, n_o, map)
implicit none
integer, intent(in) :: n_i, n_o
double precision, intent(in) :: x_i(n_i), x_o(n_o)
integer, intent(out) :: map(n_i)
integer :: i, j
j = 1
do i=1, n_i
do while (j < n_o)
if (x_i(i) < x_o(j+1)) then
exit
endif
j = j + 1
enddo
map(i) = j
enddo
end subroutine down_coord
end module downsample
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment