def metgrid(**kwargs): logging.info('Running metgrid...') wrf_config = get_wrf_config(**kwargs) wps_dir = utils.get_wps_dir(wrf_config.get('wrf_home')) utils.run_subprocess('./metgrid.exe', cwd=wps_dir)
def process(self, *args, **kwargs): logging.info('Running metgrid...') wrf_config = self.get_config(**kwargs) logging.info('wrf conifg: ' + wrf_config.to_json_string()) wps_dir = utils.get_wps_dir(wrf_config.get('wrf_home')) utils.run_subprocess('./metgrid.exe', cwd=wps_dir)
def process(self, *args, **kwargs): wrf_config = self.get_config(**kwargs) logging.info('wrf conifg: ' + wrf_config.to_json_string()) wrf_home = wrf_config.get('wrf_home') em_real_dir = utils.get_em_real_dir(wrf_home) procs = wrf_config.get('procs') utils.run_subprocess('mpirun -np %d ./real.exe' % procs, cwd=em_real_dir)
def process(self, *args, **kwargs): logging.info('Running geogrid...') wrf_config = self.get_config(**kwargs) wps_dir = utils.get_wps_dir(wrf_config.get('wrf_home')) if not executor.check_geogrid_output(wps_dir): logging.info('Running Geogrid.exe') utils.run_subprocess('./geogrid.exe', cwd=wps_dir) else: logging.info('Geogrid output already available')
def process(self, *args, **kwargs): wrf_config = self.get_config(**kwargs) logging.info('wrf conifg: ' + wrf_config.to_json_string()) wrf_home = wrf_config.get('wrf_home') em_real_dir = utils.get_em_real_dir(wrf_home) procs = wrf_config.get('procs') # logging.info('Replacing namelist.input place-holders') # executor.replace_namelist_input(wrf_config) utils.run_subprocess('mpirun -np %d ./wrf.exe' % procs, cwd=em_real_dir)
def ungrib(**kwargs): logging.info('Running ungrib...') wrf_config = get_wrf_config(**kwargs) wps_dir = utils.get_wps_dir(wrf_config.get('wrf_home')) # Running link_grib.csh logging.info('Running link_grib.csh') gfs_date, gfs_cycle, start = utils.get_appropriate_gfs_inventory( wrf_config) dest = \ utils.get_gfs_data_url_dest_tuple(wrf_config.get('gfs_url'), wrf_config.get('gfs_inv'), gfs_date, gfs_cycle, '', wrf_config.get('gfs_res'), '')[1] # use get_gfs_data_url_dest_tuple to get utils.run_subprocess('csh link_grib.csh %s/%s' % (wrf_config.get('gfs_dir'), dest), cwd=wps_dir) utils.run_subprocess('./ungrib.exe', cwd=wps_dir)
def process(self, *args, **kwargs): logging.info('Running ungrib...') wrf_config = self.get_config(**kwargs) logging.info('wrf conifg: ' + wrf_config.to_json_string()) wps_dir = utils.get_wps_dir(wrf_config.get('wrf_home')) # Running link_grib.csh logging.info('Running link_grib.csh') gfs_date, gfs_cycle, start = utils.get_appropriate_gfs_inventory( wrf_config) # use get_gfs_data_url_dest_tuple to get dest = \ utils.get_gfs_data_url_dest_tuple(wrf_config.get('gfs_url'), wrf_config.get('gfs_inv'), gfs_date, gfs_cycle, '', wrf_config.get('gfs_res'), '')[1] utils.run_subprocess('csh link_grib.csh %s/%s' % (wrf_config.get('gfs_dir'), dest), cwd=wps_dir) utils.run_subprocess('./ungrib.exe', cwd=wps_dir)
def ncks_extract_variables(nc_file, variables, dest): v = ','.join(variables) logging.info('ncks extraction of %s for %s vars to %s' % (nc_file, v, dest)) ncks_query = 'ncks -v %s %s %s' % (v, nc_file, dest) utils.run_subprocess(ncks_query)
def get_weather_type(inventory, wgrib_path='/opt/lib/wgrib2', write_to_file=None): logging.info('Finding the weather type from %s' % inventory) tmp_dir = tempfile.mkdtemp(prefix='tmp_jaxa') dest_nc = os.path.join(tmp_dir, os.path.basename(inventory) + '.nc') cmd = '%s %s -netcdf %s' % (wgrib_path, inventory, dest_nc) utils.run_subprocess(cmd, cwd=tmp_dir) fh = Dataset(dest_nc, mode='r') msl = fh.variables['PRMSL_meansealevel'] g = msl[0, 230:271, 624:655] P1 = g[0, 10] P2 = g[0, 20] P3 = g[10, 0] P4 = g[10, 10] P5 = g[10, 20] P6 = g[10, 30] P7 = g[20, 0] P8 = g[20, 10] P9 = g[20, 20] P10 = g[20, 30] P11 = g[30, 0] P12 = g[30, 10] P13 = g[30, 20] P14 = g[30, 30] P15 = g[40, 10] P16 = g[40, 20] WF = (0.5 * (P12 + P13) - 0.5 * (P4 + P5)) SF = 1.35 * (0.25 * (P5 + 2 * P9 + P13) - 0.25 * (P4 + 2 * P8 + P12)) F = (SF ** 2 + WF ** 2) ** 0.5 WSV = 1.12 * (0.5 * (P15 + P16) - 0.5 * (P8 + P9)) - 0.91 * (0.5 * (P8 + P9) - 0.5 * (P1 + P2)) SSV = 0.85 * ( 0.25 * (P6 + 2 * P10 + P14) - 0.25 * (P5 + 2 * P9 + P13) - 0.25 * (P4 + 2 * P8 + P12) + 0.25 * ( P3 + 2 * P7 + P11)) Z = WSV + SSV A, C, D = None, None, None if abs(Z < F): F = 1 C = 0 if abs(Z) > (2 * F) and Z > 0: C = 1 A = 0 if abs(Z) > (2 * F) and Z < 0: A = 1 FT = "H" if F == 1 and WF > 0: D = np.arctan(WF / SF) + 180 if F == 1 and WF < 0: D = np.arctan(WF / SF) if F == 1 and -67.5 < D < -22.5: FT = 'NW' if F == 1 and -22.5 < D < 22.5: FT = 'N' if F == 1 and 22.5 < D < 67.5: FT = 'NE' if F == 1 and 67.5 < D < 112.5: FT = 'E' if F == 1 and 112.5 < D < 157.5: FT = 'SE' if F == 1 and 157.5 < D < 202.5: FT = 'S' if F == 1 and 202.5 < D < 247.5: FT = 'SW' if F == 1 and (D < -67.5 or D > 247.5): FT = 'W' if F == 0 and A > 0: FT = 'A' if F == 0 and C > 0: FT = 'C' if write_to_file is not None: with open(write_to_file, 'w') as fh: fh.write(str(FT) + '\n') shutil.rmtree(tmp_dir, ignore_errors=True) logging.info('Weather type %s' % FT) return FT
def run_em_real(wrf_config): logging.info('Running em_real...') wrf_home = wrf_config.get('wrf_home') em_real_dir = utils.get_em_real_dir(wrf_home) procs = wrf_config.get('procs') run_id = wrf_config.get('run_id') output_dir = utils.create_dir_if_not_exists( os.path.join(wrf_config.get('nfs_dir'), 'results', run_id, 'wrf')) archive_dir = utils.create_dir_if_not_exists( os.path.join(wrf_config.get('archive_dir'), 'results', run_id, 'wrf')) logging.info('Backup the output dir') utils.backup_dir(output_dir) logs_dir = utils.create_dir_if_not_exists(os.path.join(output_dir, 'logs')) logging.info('Copying metgrid.zip') metgrid_dir = os.path.join(wrf_config.get('nfs_dir'), 'metgrid') if wrf_config.is_set('wps_run_id'): logging.info('wps_run_id is set. Copying metgrid from ' + wrf_config.get('wps_run_id')) utils.copy_files_with_prefix( metgrid_dir, wrf_config.get('wps_run_id') + '_metgrid.zip', em_real_dir) metgrid_zip = os.path.join( em_real_dir, wrf_config.get('wps_run_id') + '_metgrid.zip') else: utils.copy_files_with_prefix(metgrid_dir, wrf_config.get('run_id') + '_metgrid.zip', em_real_dir) metgrid_zip = os.path.join(em_real_dir, wrf_config.get('run_id') + '_metgrid.zip') logging.info('Extracting metgrid.zip') ZipFile(metgrid_zip, 'r', compression=ZIP_DEFLATED).extractall(path=em_real_dir) # logs destination: nfs/logs/xxxx/rsl* try: try: logging.info('Starting real.exe') utils.run_subprocess( 'mpirun --allow-run-as-root -np %d ./real.exe' % procs, cwd=em_real_dir) finally: logging.info('Moving Real log files...') utils.create_zip_with_prefix(em_real_dir, 'rsl*', os.path.join(em_real_dir, 'real_rsl.zip'), clean_up=True) utils.move_files_with_prefix(em_real_dir, 'real_rsl.zip', logs_dir) try: logging.info('Starting wrf.exe') utils.run_subprocess( 'mpirun --allow-run-as-root -np %d ./wrf.exe' % procs, cwd=em_real_dir) finally: logging.info('Moving WRF log files...') utils.create_zip_with_prefix(em_real_dir, 'rsl*', os.path.join(em_real_dir, 'wrf_rsl.zip'), clean_up=True) utils.move_files_with_prefix(em_real_dir, 'wrf_rsl.zip', logs_dir) finally: logging.info('Moving namelist input file') utils.move_files_with_prefix(em_real_dir, 'namelist.input', output_dir) logging.info('WRF em_real: DONE! Moving data to the output dir') logging.info('Extracting rf from domain3') d03_nc = glob.glob(os.path.join(em_real_dir, 'wrfout_d03_*'))[0] ncks_query = 'ncks -v %s %s %s' % ('RAINC,RAINNC,XLAT,XLONG,Times', d03_nc, d03_nc + '_rf') utils.run_subprocess(ncks_query) logging.info('Extracting rf from domain1') d01_nc = glob.glob(os.path.join(em_real_dir, 'wrfout_d01_*'))[0] ncks_query = 'ncks -v %s %s %s' % ('RAINC,RAINNC,XLAT,XLONG,Times', d01_nc, d01_nc + '_rf') utils.run_subprocess(ncks_query) logging.info('Moving data to the output dir') utils.move_files_with_prefix(em_real_dir, 'wrfout_d03*_rf', output_dir) utils.move_files_with_prefix(em_real_dir, 'wrfout_d01*_rf', output_dir) logging.info('Moving data to the archive dir') utils.move_files_with_prefix(em_real_dir, 'wrfout_*', archive_dir) logging.info('Cleaning up files') utils.delete_files_with_prefix(em_real_dir, 'met_em*') utils.delete_files_with_prefix(em_real_dir, 'rsl*') os.remove(metgrid_zip)
def run_wps(wrf_config): logging.info('Running WPS: START') wrf_home = wrf_config.get('wrf_home') wps_dir = utils.get_wps_dir(wrf_home) output_dir = utils.create_dir_if_not_exists( os.path.join(wrf_config.get('nfs_dir'), 'results', wrf_config.get('run_id'), 'wps')) logging.info('Backup the output dir') utils.backup_dir(output_dir) logs_dir = utils.create_dir_if_not_exists(os.path.join(output_dir, 'logs')) logging.info('Cleaning up files') utils.delete_files_with_prefix(wps_dir, 'FILE:*') utils.delete_files_with_prefix(wps_dir, 'PFILE:*') utils.delete_files_with_prefix(wps_dir, 'met_em*') # Linking VTable if not os.path.exists(os.path.join(wps_dir, 'Vtable')): logging.info('Creating Vtable symlink') os.symlink(os.path.join(wps_dir, 'ungrib/Variable_Tables/Vtable.NAM'), os.path.join(wps_dir, 'Vtable')) # Running link_grib.csh gfs_date, gfs_cycle, start = utils.get_appropriate_gfs_inventory( wrf_config) dest = utils.get_gfs_data_url_dest_tuple(wrf_config.get('gfs_url'), wrf_config.get('gfs_inv'), gfs_date, gfs_cycle, '', wrf_config.get('gfs_res'), '')[1].replace('.grb2', '') utils.run_subprocess('csh link_grib.csh %s/%s' % (wrf_config.get('gfs_dir'), dest), cwd=wps_dir) try: # Starting ungrib.exe try: utils.run_subprocess('./ungrib.exe', cwd=wps_dir) finally: utils.move_files_with_prefix(wps_dir, 'ungrib.log', logs_dir) # Starting geogrid.exe' if not check_geogrid_output(wps_dir): logging.info('Geogrid output not available') try: utils.run_subprocess('./geogrid.exe', cwd=wps_dir) finally: utils.move_files_with_prefix(wps_dir, 'geogrid.log', logs_dir) # Starting metgrid.exe' try: utils.run_subprocess('./metgrid.exe', cwd=wps_dir) finally: utils.move_files_with_prefix(wps_dir, 'metgrid.log', logs_dir) finally: logging.info('Moving namelist wps file') utils.move_files_with_prefix(wps_dir, 'namelist.wps', output_dir) logging.info('Running WPS: DONE') logging.info('Zipping metgrid data') metgrid_zip = os.path.join(wps_dir, wrf_config.get('run_id') + '_metgrid.zip') utils.create_zip_with_prefix(wps_dir, 'met_em.d*', metgrid_zip) logging.info('Moving metgrid data') dest_dir = os.path.join(wrf_config.get('nfs_dir'), 'metgrid') utils.move_files_with_prefix(wps_dir, metgrid_zip, dest_dir)