CodeBreak 15/9/2021

Revision as of 19:52, 21 September 2021 by C.carouge (talk | contribs)

Summary of topics

  • How to analyse data weekly and creating weekly climatology
  • Curl calculations
  • Weighted function in xarray
  • How to organise calculations dynamically with python data structure (dict) e.g. looping over seasons

Weekly climatology

Weekly climatologies are tricky as there is not an integer number of weeks per year. The specifics of how to implement this are likely particular to the problem being analysed.

Curl calculations

Question regarding calculating a function near a land/sea boundary. Had to copy coordinates from another dataset as the grid values were masked, but led to NaN values. Turns out the coordinates had been coerced to integers when loading. This was an error. Xarray complained about duplicate values in the coordinate as a result. Worked when coercing to integer removed.

Weighted function

Calculating a total ice volume was the same if ice thickness multiplied by area of the cell as using the area of the cell in a weighted function. This is because the xarray weighted function does not normalise your weights for you, it just uses them as a multiplicative factor when using .sum().

See: http://xarray.pydata.org/en/stable/user-guide/computation.html#weighted-array-reductions


Python Data Structures and Looping over seasons

Rather than calling a function once for each season

pct_djf = np.percentile(sst.where(time.dt.season == 'DJF'))
pct_mam = np.percentile(sst.where(time.dt.season == MAM))
pct_jja = np.percentile(sst.where(time.dt.season == JJA))
pct_son = np.percentile(sst.where(time.dt.season == SON))

try using dictionaries

pct_season = {} # declare a dictionary 
for season, data in sst.groupby('time.season'): 
   pct_season[season] = np.percentile(data)

you can then access the data with e.g. pct_season['MAM']. Using loops means less to type and less chances of typos and easier extension to cases with a lot more instances.