redisplay_restart.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import numpy as np
  2. from netCDF4 import Dataset
  3. import os
  4. import sys
  5. # Inputs:
  6. # a) a set of processor-specific NEMO restart files defined with a certain domain decomposition, that you'll want to run at (2*n_proc, n_proc for oce and n_proc for ice). You can get this by doing a dummy run with the number of processors you'll eventually want.
  7. # b) a pair of rebuilt (processor-wise) NEMO restart files, which you'll want to get the actual restart data from
  8. # Outputs:
  9. # a set of processor-specific NEMO restart files, using the a) domain decomposition, with the data coming from b)
  10. # input a). The script expects the files to be <geom_in_stencil><oce,ice>_XXXX.nc, where XXXX are four numbers.
  11. geom_in_stencil = "/afast/pelletie/nemo_rs/geom_in/mpp_209_fET171-h40/so025_paraso_temoin_00000096_restart_"
  12. # Number of processors for destination geom (geom_in_stencil)
  13. n_newdisp = 209
  14. # input b) The script expects the files to be <rebuilt_rs_stencil><oce,ice>.nc
  15. rebuilt_rs_stencil = "/afast/pelletie/nemo_rs/rs_data/h40/org/so025_fET171_spin_h40_00035040_restart_"
  16. ## WARNING: be sure that your b) files contain all the fields you'll need.
  17. ## Some NEMO options (melt ponds, iscpl) create/require new restart fields.
  18. # Output files. The script will write <output_stencil><oce,ice>_XXXX.nc
  19. output_stencil = "/afast/pelletie/nemo_rs/rs_data/h40/redisp/mpp_209_fET171-h40/so025_fET171_spin_h40_00035040_remap209-mpp_restart_"
  20. nz = 75
  21. media = ['ice', 'oce']
  22. for medium in media:
  23. big_rs = Dataset(rebuilt_rs_stencil+medium+'.nc', mode='r')
  24. for m in range(0,n_newdisp):
  25. geom_nc = Dataset(geom_in_stencil+medium+'_%04d'%m+'.nc', mode='r')
  26. nx_nd, ny_nd = geom_nc.DOMAIN_size_local
  27. sx_beg, sy_beg = geom_nc.DOMAIN_position_first - 1
  28. sx_end, sy_end = geom_nc.DOMAIN_position_last
  29. nx_nd, ny_nd = geom_nc.DOMAIN_size_local
  30. if( sx_end - sx_beg != nx_nd or sy_end - sy_beg != ny_nd):
  31. print('error nx, beg end')
  32. sys.exit()
  33. fmt = "%04d"%m
  34. outfile = output_stencil+medium+'_'+fmt+'.nc'
  35. out_nc = Dataset(outfile, mode='w')
  36. out_nc.createDimension('x', nx_nd)
  37. out_nc.createDimension('y', ny_nd)
  38. out_nc.createDimension('z', nz)
  39. out_nc.createDimension('t', 0)
  40. print(sx_beg,sx_end,sy_beg,sy_end)
  41. for name, variable in big_rs.variables.items():
  42. varout = out_nc.createVariable(name, variable.datatype, variable.dimensions)
  43. len_tmp = len(variable.dimensions)
  44. if( 'x' in variable.dimensions):
  45. if(len_tmp == 2):
  46. varout[:] = big_rs.variables[name][sy_beg:sy_end,sx_beg:sx_end]
  47. elif(len_tmp == 3):
  48. varout[:] = big_rs.variables[name][:,sy_beg:sy_end,sx_beg:sx_end]
  49. elif(len_tmp == 4):
  50. varout[:] = big_rs.variables[name][:,:,sy_beg:sy_end,sx_beg:sx_end]
  51. else:
  52. varin = big_rs.variables[name][:]
  53. if(len_tmp > 0 ):
  54. varout[:] = varin[:]
  55. else:
  56. varout[:] = varin
  57. out_nc.setncatts(geom_nc.__dict__)
  58. out_nc.close()
  59. print('generated '+outfile)
  60. geom_nc.close()
  61. big_rs.close()