|
| 1 | +from glob import glob |
| 2 | +from subprocess import run |
| 3 | +from os import path |
| 4 | +import xarray |
| 5 | +from boundary import Segment |
| 6 | +import os |
| 7 | +import numpy |
| 8 | + |
| 9 | +# xarray gives a lot of unnecessary warnings |
| 10 | +import warnings |
| 11 | +warnings.filterwarnings('ignore') |
| 12 | + |
| 13 | + |
| 14 | +def write_year(year, glorys_dir, segments, is_first_year=False): |
| 15 | + glorys = ( |
| 16 | + xarray.open_mfdataset(sorted(glob(os.path.join(glorys_dir, f'{year}/GLORYS_REANALYSIS_{year}-*.nc')))) |
| 17 | + .rename({'latitude': 'lat', 'longitude': 'lon', 'depth': 'z'}) |
| 18 | + ) |
| 19 | + |
| 20 | + # Floor first time down to midnight so that it matches initial conditions |
| 21 | + if is_first_year: |
| 22 | + glorys.time.data[0] = numpy.datetime64(f'{year}-01-01T00:00:00.000000000') |
| 23 | + for seg in segments: |
| 24 | + seg.regrid_velocity(glorys['uo'], glorys['vo'], suffix=year, flood=False) |
| 25 | + for tr in ['thetao', 'so']: |
| 26 | + seg.regrid_tracer(glorys[tr], suffix=year, flood=False) |
| 27 | + seg.regrid_tracer(glorys['zos'], suffix=year, flood=False) |
| 28 | + |
| 29 | + |
| 30 | +# this is an xarray based way to concatenate the obc yearly files into one file (per variable of output) |
| 31 | +# the former way to do this was based on ncrcat from NCO tools |
| 32 | +def ncrcat_rename(nsegments, ncrcat_outdir,output_dir, delete_old_files=False): |
| 33 | + rename_dict={'thetao':'temp', 'so':'salt', 'zos':'zeta', 'uv':'uv'} |
| 34 | + for var in ['thetao', 'so', 'zos', 'uv']: |
| 35 | + for seg in range(1, nsegments+1): |
| 36 | + comb = xarray.open_mfdataset(f'{output_dir}{var}_00{seg}_*') |
| 37 | + if var!='uv': |
| 38 | + comb=comb.rename({f'{var}_segment_00{seg}':f'{rename_dict[var]}_segment_00{seg}'}) |
| 39 | + if var!='zos': |
| 40 | + comb=comb.rename({f'dz_{var}_segment_00{seg}':f'dz_{rename_dict[var]}_segment_00{seg}'}) |
| 41 | + # Fix output metadata, including removing all _FillValues. |
| 42 | + all_vars = list(comb.data_vars.keys()) + list(comb.coords.keys()) |
| 43 | + encodings = {v: {'_FillValue': None} for v in all_vars} |
| 44 | + encodings['time'].update({'dtype':'float64', 'calendar': 'gregorian'}) |
| 45 | + year1 = str(comb.time.values[2])[0:4] |
| 46 | + year_end = str(comb.time.values[-2])[0:4] |
| 47 | + print(year1,year_end) |
| 48 | + comb.to_netcdf(f'{ncrcat_outdir}{rename_dict[var]}_00{seg}_{year1}_{year_end}.nc', |
| 49 | + encoding=encodings, |
| 50 | + unlimited_dims='time', |
| 51 | + format='NETCDF3_64BIT') |
| 52 | + print(f'concatenated and renamed {rename_dict[var]}_00{seg}.nc') |
| 53 | + del(comb) |
| 54 | + if delete_old_files==True: |
| 55 | + os.remove(f'{output_dir}{var}_00{seg}_*') |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + first_year = 1996 |
| 60 | + |
| 61 | + # Original |
| 62 | + #glorys_dir = '/Volumes/A1/workdir/james/glorys/' |
| 63 | + #output_dir = '/Volumes/A1/workdir/james/nwa25_input/boundary/indiv_years/' |
| 64 | + #ncrcat_outdir = '/Volumes/A1/workdir/james/nwa25_input/boundary/boundary_final/' |
| 65 | + #hgrid = xarray.open_dataset('/home/james/gridInfo/nwa25/ocean_hgrid.nc') |
| 66 | + |
| 67 | + # Rob |
| 68 | + glorys_dir = '/Volumes/A1/workdir/james/glorys/' |
| 69 | + output_dir = '/home/cermak/workdir/configs/nwa25/OBC/indiv/' |
| 70 | + ncrcat_outdir = '/home/cermak/workdir/configs/nwa25/OBC/final/' |
| 71 | + hgrid = xarray.open_dataset('/home/cermak/workdir/configs/nwa25/INPUT/ocean_hgrid.nc') |
| 72 | + |
| 73 | + segments = [ |
| 74 | + Segment(1, 'south', hgrid, output_dir=output_dir), |
| 75 | + Segment(2, 'east', hgrid, output_dir=output_dir), |
| 76 | + Segment(3, 'north', hgrid, output_dir=output_dir) |
| 77 | + ] |
| 78 | + |
| 79 | + for y in range(1996, 2018): |
| 80 | + print(y) |
| 81 | + write_year(y, glorys_dir, segments, is_first_year=y==first_year) |
| 82 | + |
| 83 | + # Original |
| 84 | + #output_dir = '/Volumes/A1/workdir/james/nwa25_input/boundary/indiv_years/' |
| 85 | + #ncrcat_outdir = '/Volumes/A1/workdir/james/nwa25_input/boundary/boundary_final/' |
| 86 | + # Rob |
| 87 | + output_dir = '/home/cermak/workdir/configs/nwa25/OBC/indiv/' |
| 88 | + ncrcat_outdir = '/home/cermak/workdir/configs/nwa25/OBC/final/' |
| 89 | + |
| 90 | + ncrcat_rename(3, ncrcat_outdir, output_dir) |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + main() |
| 96 | + |
0 commit comments