def test_find_grib_file_narr(self): """Ensures correct output from find_grib_file when model is NARR.""" this_grib_file_name = nwp_model_io.find_grib_file( INIT_TIME_UNIX_SEC, model_name=nwp_model_utils.NARR_MODEL_NAME, top_directory_name=TOP_GRIB_DIRECTORY_NAME, raise_error_if_missing=False) self.assertTrue(this_grib_file_name == GRIB_FILE_NAME_NARR)
def test_find_grib_file_ruc252(self): """Ensures correct output from find_grib_file. In this case, model is RUC on the 252 grid. """ this_grib_file_name = nwp_model_io.find_grib_file( INIT_TIME_UNIX_SEC, lead_time_hours=LEAD_TIME_HOURS, model_name=nwp_model_utils.RUC_MODEL_NAME, grid_id=nwp_model_utils.ID_FOR_252GRID, top_directory_name=TOP_GRIB_DIRECTORY_NAME, raise_error_if_missing=False) self.assertTrue(this_grib_file_name == GRIB_FILE_NAME_RUC252)
def test_find_grib_file_rap130(self): """Ensures correct output from find_grib_file. In this case, model is RAP on the 130 grid. """ this_file_name = nwp_model_io.find_grib_file( top_directory_name=TOP_GRIB_DIRECTORY_NAME, init_time_unix_sec=INIT_TIME_UNIX_SEC, model_name=nwp_model_utils.RAP_MODEL_NAME, grid_id=nwp_model_utils.NAME_OF_130GRID, lead_time_hours=LEAD_TIME_HOURS, raise_error_if_missing=False) self.assertTrue(this_file_name == GRIB_FILE_NAME_RAP130)
def _run(top_input_dir_name, first_time_string, last_time_string, input_field_name, pressure_level_mb, top_output_dir_name): """Converts NARR data to a more convenient file format. This is effectively the main method. :param top_input_dir_name: See documentation at top of file. :param first_time_string: Same. :param last_time_string: Same. :param input_field_name: Same. :param pressure_level_mb: Same. :param top_output_dir_name: Same. """ if pressure_level_mb <= 0: pressure_level_mb = None if pressure_level_mb is None: output_pressure_level_mb = DUMMY_PRESSURE_LEVEL_MB + 0 else: output_pressure_level_mb = pressure_level_mb + 0 first_time_unix_sec = time_conversion.string_to_unix_sec( first_time_string, INPUT_TIME_FORMAT) last_time_unix_sec = time_conversion.string_to_unix_sec( last_time_string, INPUT_TIME_FORMAT) valid_times_unix_sec = time_periods.range_and_interval_to_list( start_time_unix_sec=first_time_unix_sec, end_time_unix_sec=last_time_unix_sec, time_interval_sec=TIME_INTERVAL_SECONDS) if input_field_name == processed_narr_io.U_WIND_EARTH_RELATIVE_NAME: input_field_name_other = (processed_narr_io.V_WIND_EARTH_RELATIVE_NAME) elif input_field_name == processed_narr_io.V_WIND_EARTH_RELATIVE_NAME: input_field_name_other = (processed_narr_io.U_WIND_EARTH_RELATIVE_NAME) else: input_field_name_other = None input_field_name_grib1 = _std_to_grib1_field_name( field_name=input_field_name, pressure_level_mb=pressure_level_mb) if input_field_name in WIND_FIELD_NAMES: input_field_name_other_grib1 = _std_to_grib1_field_name( field_name=input_field_name_other, pressure_level_mb=pressure_level_mb) output_field_name = processed_narr_io.field_name_to_grid_relative( input_field_name) output_field_name_other = ( processed_narr_io.field_name_to_grid_relative( input_field_name_other)) (narr_latitude_matrix_deg, narr_longitude_matrix_deg ) = nwp_model_utils.get_latlng_grid_point_matrices( model_name=nwp_model_utils.NARR_MODEL_NAME) (narr_rotation_cosine_matrix, narr_rotation_sine_matrix) = nwp_model_utils.get_wind_rotation_angles( latitudes_deg=narr_latitude_matrix_deg, longitudes_deg=narr_longitude_matrix_deg, model_name=nwp_model_utils.NARR_MODEL_NAME) else: input_field_name_other_grib1 = None output_field_name = input_field_name + '' output_field_name_other = None num_times = len(valid_times_unix_sec) for i in range(num_times): if input_field_name in WIND_FIELD_NAMES: this_field_matrix_other = None if valid_times_unix_sec[i] > LAST_GRIB_TIME_UNIX_SEC: this_month_string = time_conversion.unix_sec_to_string( valid_times_unix_sec[i], MONTH_TIME_FORMAT) this_netcdf_file_name = narr_netcdf_io.find_file( top_directory_name=top_input_dir_name, field_name=input_field_name, month_string=this_month_string, is_surface=pressure_level_mb is None) print 'Reading data from: "{0:s}"...'.format(this_netcdf_file_name) this_field_matrix = narr_netcdf_io.read_file( netcdf_file_name=this_netcdf_file_name, field_name=input_field_name, valid_time_unix_sec=valid_times_unix_sec[i], pressure_level_mb=pressure_level_mb) if input_field_name in WIND_FIELD_NAMES: this_netcdf_file_name_other = narr_netcdf_io.find_file( top_directory_name=top_input_dir_name, field_name=input_field_name_other, month_string=this_month_string, is_surface=pressure_level_mb is None) print 'Reading data from: "{0:s}"...'.format( this_netcdf_file_name_other) this_field_matrix_other = narr_netcdf_io.read_file( netcdf_file_name=this_netcdf_file_name_other, field_name=input_field_name_other, valid_time_unix_sec=valid_times_unix_sec[i], pressure_level_mb=pressure_level_mb) else: this_grib_file_name = nwp_model_io.find_grib_file( top_directory_name=top_input_dir_name, model_name=nwp_model_utils.NARR_MODEL_NAME, init_time_unix_sec=valid_times_unix_sec[i], lead_time_hours=0) print 'Reading data from: "{0:s}"...'.format(this_grib_file_name) this_field_matrix = nwp_model_io.read_field_from_grib_file( grib_file_name=this_grib_file_name, field_name_grib1=input_field_name_grib1, model_name=nwp_model_utils.NARR_MODEL_NAME, wgrib_exe_name=WGRIB_EXE_NAME, wgrib2_exe_name=WGRIB2_EXE_NAME) if input_field_name in WIND_FIELD_NAMES: this_field_matrix_other = ( nwp_model_io.read_field_from_grib_file( grib_file_name=this_grib_file_name, field_name_grib1=input_field_name_other_grib1, model_name=nwp_model_utils.NARR_MODEL_NAME, wgrib_exe_name=WGRIB_EXE_NAME, wgrib2_exe_name=WGRIB2_EXE_NAME)) if input_field_name in WIND_FIELD_NAMES: print 'Rotating Earth-relative winds to grid-relative...' if input_field_name == processed_narr_io.U_WIND_EARTH_RELATIVE_NAME: this_field_matrix, this_field_matrix_other = ( nwp_model_utils.rotate_winds_to_grid_relative( u_winds_earth_relative_m_s01=this_field_matrix, v_winds_earth_relative_m_s01=this_field_matrix_other, rotation_angle_cosines=narr_rotation_cosine_matrix, rotation_angle_sines=narr_rotation_sine_matrix)) else: this_field_matrix_other, this_field_matrix = ( nwp_model_utils.rotate_winds_to_grid_relative( u_winds_earth_relative_m_s01=this_field_matrix_other, v_winds_earth_relative_m_s01=this_field_matrix, rotation_angle_cosines=narr_rotation_cosine_matrix, rotation_angle_sines=narr_rotation_sine_matrix)) this_output_file_name = processed_narr_io.find_file_for_one_time( top_directory_name=top_output_dir_name, field_name=output_field_name, pressure_level_mb=output_pressure_level_mb, valid_time_unix_sec=valid_times_unix_sec[i], raise_error_if_missing=False) print 'Writing processed data to: "{0:s}"...'.format( this_output_file_name) processed_narr_io.write_fields_to_file( pickle_file_name=this_output_file_name, field_matrix=numpy.expand_dims(this_field_matrix, axis=0), field_name=output_field_name, pressure_level_pascals=output_pressure_level_mb * MB_TO_PASCALS, valid_times_unix_sec=valid_times_unix_sec[[i]]) if input_field_name not in WIND_FIELD_NAMES: print '\n' continue this_output_file_name = processed_narr_io.find_file_for_one_time( top_directory_name=top_output_dir_name, field_name=output_field_name_other, pressure_level_mb=output_pressure_level_mb, valid_time_unix_sec=valid_times_unix_sec[i], raise_error_if_missing=False) print 'Writing processed data to: "{0:s}"...\n'.format( this_output_file_name) processed_narr_io.write_fields_to_file( pickle_file_name=this_output_file_name, field_matrix=numpy.expand_dims(this_field_matrix_other, axis=0), field_name=output_field_name_other, pressure_level_pascals=output_pressure_level_mb * MB_TO_PASCALS, valid_times_unix_sec=valid_times_unix_sec[[i]])