I would like to multiply a dataset with a real number, e.g.

data = ct.catalogue.retrieve(...)
desired_result = data*3

This gives me the following error message:

Is there any way to perform this operation?

Thank you!

6 Comments

  1. Dear Georg,

    Any additional detail on your workflow would help to understand the  nature of data and why this operation fails.

    You could try to use the multiplication operator:

    desired_result = ct.operator.mul(data, 3)

    Regards.

    Vivien


    1. Thank you Vivien, your code proposal worked perfectly!

  2. Great, glad to know.

    I am curious to understand why the * did not work, would you mind sharing the code that was failing?


  3. Ok, here's some part of the code:

    def power_curve(xar):
    '''	
    	Simplified power curve of wind turbine
    	Cut-in speed 3 m/s
    	Nominal power (3450 kw) at 12 m/s
      	Cut-out speed 25 m/s
    '''
        xar1 = ct.cube.where(xar > 25, 0, xar)
       	xar1 = ct.cube.where(xar1 <= 25, 3450, xar1)    
       	xar1 = ct.cube.where(xar1 <= 12, 3450/9*(xar1-3), xar1)      # <=== This is the part that fails
       	xar1 = ct.cube.where(xar1 <= 3, 0, xar1)
       	return(xar1)
    
    # various decorators...
    def application():
    
    	data = ct.catalogue.retrieve(
            'reanalysis-era5-single-levels',
            {
                'variable': [
                    '100m_u_component_of_wind', '100m_v_component_of_wind'
                ],
                'product_type': 'reanalysis',
                'year': '2019'
                'month': months, # All months
                'day': days,     # All days
                'time': time,    # All hours
                'area': [max_lat, min_lon, min_lat, max_lon] # Some 9 values
            }
        )
    
    	wind_speed = ct.math.sqrt(data[0]**2 + data[1]**2)
    	power = power_curve(wind_speed)
  4. Hi Georg, if you do data * 3 instead of 3 * data the * operator actually works.

    This is still a bug that need to be fixed but it still allows you to use * and make your code shorter and more readable.

    Note that ct.operator has the advantage of handling the units where units are preserved while the +, -, *, / operations always drop the units.

    Regards.

    Vivien

  5. Good to know, thanks!