<jupyter_start><jupyter_text>resolutionなので、timeは荒くしない。荒くするのは3次元の空間。でも高度は一般にそこまでポイントがないので、とりあえず荒くしなくてよくって、二次元平面を荒くすることを考える。引数には落とす線の割合を$[0, 1]$で指定<jupyter_code>drop_rate = 0.1
cube1.coords()[0].name()
lat_num = len(cube1.coord("grid_latitude").points)
lon_num = len(cube1.coord("grid_longitude").points)
lat_num
import math
span = math.ceil((lat_num - math.ceil((lat_num*drop_rate))) /(math.ceil((lat_num*drop_rate)) + 1))
last = math.ceil((lat_num - span)/(span+1))
last
last_ind = span + (last - 1)*(span + 1)
last_ind
# 抜く奴はこれ
drop_ind = np.linspace(span, last_ind, 20)
# 残るのはこれ
remain_ind = set(range(lat_num)).difference(set(drop_ind))
# extractとかいうメソッドがあったのでそっちを使う
# 緯度経度の名前を"grid_latitude"と"grid_longitude"に変更してから使うこと。
import math
import numpy as np
from iris.coords import DimCoord
from iris.cube import Cube

def lat_id(cube, drop_rate):
    lat_num = len(cube.coord("grid_latitude").points)
    span = math.ceil((lat_num - math.ceil((lat_num*drop_rate))) /(math.ceil((lat_num*drop_rate)) + 1))
    last = math.ceil((lat_num - span)/(span+1))
    last_ind = span + (last - 1)*(span + 1)
    
    drop_ind = np.linspace(span, last_ind, last)
    return list(set(range(lat_num)).difference(set(drop_ind)))

def lon_id( cube, drop_rate):
    lon_num = len(cube.coord("grid_longitude").points)
    span = math.ceil((lon_num - math.ceil((lon_num*drop_rate))) /(math.ceil((lon_num*drop_rate)) + 1))
    last = math.ceil((lon_num - span)/(span+1))
    last_ind = span + (last - 1)*(span + 1)
    
    drop_ind = np.linspace(span, last_ind, last)
    return list(set(range(lon_num)).difference(set(drop_ind)))
import iris
filename = iris.sample_data_path('uk_hires.pp')
cubes = iris.load(filename)
cube1 = cubes[0][0,0]
lat = iris.Constraint(grid_latitude = cube1.coord("grid_latitude").points[lat_id(cube1, 0.1)])
lon = iris.Constraint(grid_longitude = cube1.coord("grid_longitude").points[lon_id(cube1, 0.1)])
print(cube1.extract(lat & lon))<jupyter_output>air_potential_temperature / (K)     (grid_latitude: 184; grid_longitude: 169)
     Dimension coordinates:
          grid_latitude                           x                    -
          grid_longitude                          -                    x
     Auxiliary coordinates:
          surface_altitude                        x                    x
     Derived coordinates:
          altitude                                x                    x
     Scalar coordinates:
          forecast_period: 6.0 hours
          forecast_reference_time: 2009-11-19 04:00:00
          level_height: 5.0 m, bound=(0.0, 13.3333) m
          model_level_number: 1
          sigma: 0.999424, bound=(1.0, 0.998464)
          time: 2009-11-19 10:00:00
     Attributes:
          STASH: m01s00i004
          source: Data from Met Office Unified Model
          um_version: 7.3<jupyter_text>下のようなことをしなくてもcube.extract(Constraint)メソッドでできるわ<jupyter_code># cubeは二次元のみ扱うことにする
def change_resolution(cube, drop_rate):
    lat_remain = lat_id(cube, drop_rate)
    lon_remain = lon_id(cube, drop_rate)
    
    # cubeに対してはsliceしかできない（np.ix_()で要素にアクセスすることはできない）ので、numpy arrayまで落として要素だけ取ってくる
    # 他のcoordinatesはそのまま新しいcubeに入れ込む
    extracted_data = cube.data[np.ix_(lat_remain, lon_remain)]
    
    # 新しいcubeようにlatとlonをクラスに入れる
    original_lat = cube.coord('grid_latitude').points
    original_lon = cube.coord('grid_longitude').points
    latitude = DimCoord(original_lat[lat_remain], standard_name='grid_latitude', units='degrees')
    longitude = DimCoord(original_lon[lon_remain], standard_name='grid_longitude', units='degrees')
    
    # lat, lon以外のcoordinateはそのまま新しいcubeに引き継ぐ
    # 上で作った新しいcubeに足していく
    # のはやめよう。cubeの次元を追加するのは難しいので一気に作る。
    dimcoords = [coord for coord in cube.coords() if coord.name() != "grid_latitude" and coord.name() != "grid_longitude" and str(type(coord)) == "<class 'iris.coords.DimCoord'>"]
    auxcoords = [coord for coord in cube.coords() if coord.name() != "grid_latitude" and coord.name() != "grid_longitude" and str(type(coord)) == "<class 'iris.coords.AuxCoord'>"]

    dims = [(i, n) for i, n in zip(dimcoords, range(2, len(dimcoords)))]
    lat_lon = [(latitude, 0),(longitude, 1)]
    dim_coords_list = lat_lon + dims
    aux_coords_list = [(i, n) for i, n in zip(auxcoords, range(len(auxcoords)))]
    
    # これだと次元を持つcoordinateの値がextracted_dataの中に含まれてないのでcube二できない
    # 入れるデータにそれぞれのcoordsが持つ値を入れてあげてdataをcubeに渡す。
    return Cube(extracted_data, dim_coords_and_dims=dim_coords_list, aux_coords_and_dims = aux_coords_list)<jupyter_output><empty_output><jupyter_text>moduleを使う<jupyter_code>import grib_handling as gh
import iris
filename = iris.sample_data_path('uk_hires.pp')
cubes = iris.load(filename)[0][0,0]
newcube = gh.change_resolution(cubes, 0.1)
print(newcube)
try:
    print("l")
except:
    print("k")<jupyter_output>l
{'fraction_non_alphanumeric': 0.12780548628428928, 'fraction_numerical': 0.024106400665004156, 'mean_word_length': 3.891260162601626, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '18128', 'license': 'MIT', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1960, 'n_words': 288, 'path': 'resolution.ipynb', 'repo_name': 'keiikegami/weather', 'size': '4812'}