#! /usr/bin/env python """ """ # region // imports import sys import os from SDToolBox import outputmessages as outputmessages from SDToolBox import data_acquisition from netCDF4 import Dataset import numpy as np # endregion # region // variables # endregion class ExtractSeaLevelPressureERA5: __lon_key = 'longitude' __lat_key = 'latitude' __input_lon = None __input_lat = None __area_longitude = None __area_latitude = None __input_data = None def __init__(self, input_data: data_acquisition.InputData, area_longitude, area_latitude) : """Initialize the waves extraction. Arguments: input_data {data_acquisition.InputData} -- Required. """ # verify input_data not none if not input_data or \ not input_data.coord_list or \ len(input_data.coord_list) < 1: raise IOError('No valid input data.') input_data.lon_key = self.__lon_key input_data.lat_key = self.__lat_key self.__input_data = input_data self.__input_lon, self.__input_lat = input_data.coord_list[0] self.__area_longitude = area_longitude self.__area_latitude = area_latitude def subset_slp_era5(self, area_latitude, area_longitude, year_from, year_to, pathSLP, resR, resT, redR, redT) : """Extracts a collection of netCDF4 subsets based on the input data set when creating the extract_sea_level_pressure object. Arguments: directory_path {str} -- Location of all the variable diretories. area_latitude {list} -- List with x,y coordinate for the latitude area_longitude {list} -- List with x,y coordinate for the longitude year_from {int} -- Start of time data to substract. year_to {int} -- End of time data to substract. TODO other parameters to be determined (probably floats as they are probably steps) Returns: list(Dataset) -- collection of netCDF4 subsets per variable. """ with Dataset(pathSLP, 'r', format='netCDF4') as case_dataset: filename = pathSLP + 'era5_Global_msl_p_1979.nc' latitude_ERA = case_dataset.variables[self.__lat_key][:] longitude_ERA = case_dataset.variables[self.__lon_key][:] position_longitude1 = find_indices_that_meet_condition(longitude_ERA, lambda longitude_ERA, area_longitude: longitude_ERA == area_longitude[0]) position_longitude2 = find_indices_that_meet_condition(longitude_ERA, lambda longitude_ERA, area_longitude : longitude_ERA == area_longitude[1]) position_latitude1 = find_indices_that_meet_condition(latitude_ERA, lambda latitude_ERA, area_latitude: latitude_ERA == area_latitude[0]) position_latitude2 = find_indices_that_meet_condition(latitude_ERA, lambda latitude_ERA, area_latitude: latitude_ERA == area_latitude[1]) latitude_in_selected_area = np.arange(position_latitude1, position_latitude2).tolist() longitude_in_selected_area = np.arange(position_longitude1, position_longitude2).tolist() latitude_ERA.clear() longitude_ERA.clear() [x, y] = np.meshgrid(longitude_in_selected_area, latitude_in_selected_area) longitude_in_selected_area.clear() latitude_in_selected_area.clear() X = [] Y = [] for element in range(len(x)) : X.append(element) for element in range(len(y)) : Y.append(element) x.clear() y.clear() def extract_slp_data(self, year_from, year_to, position_longitude1, position_latitude1, position_longitude2, position_latitude2): years = generate_years_array(year_from, year_to) #extracts for all the years for year_idx, year in range(len(years)): base_file_name = 'era5_Global_msl_p_{}.nc'.format(year) #with Dataset(base_file_name, 'r', format='netCDF4') as case_dataset: #position = case_dataset.variables['msl'][[position_longitude1, position_latitude1, 1] # don't really get the following parameter to ncread in the matlab script [((position_longitude2-position_longitude1)+1), ((position_latitude2-position_latitude1)+1), ~] def find_indices_that_meet_condition(self, input_list, condition): ''' find all the indices in a list that meet the condition (similar to matlab find()) ''' return [index for (index, val) in enumerate(input_list) if condition(val)] # this method could be moved in a generic utils stript outside here @staticmethod def generate_years_array(year_from, year_to): years = [] for i in range(year_to - year_from): years.append(year_from + i) # fills an array of years years.append(year_to) return years