<jupyter_start><jupyter_text>Testing the accuracy and efficiency of interpolation via xarray  Add paths to run locally or off Cheyenne or Casper<jupyter_code>import sys
# Add common resources folder to path
sys.path.append('/glade/u/home/jonahshaw/Scripts/git_repos/CESM2_analysis')
sys.path.append('/glade/u/home/jonahshaw/Scripts/git_repos/CESM2_analysis/Common/')
# sys.path.append("/home/jonahks/git_repos/netcdf_analysis/Common/")

from imports import (
    pd, np, xr, mpl, plt, sns, os, 
    datetime, sys, crt, gridspec,
    ccrs, metrics, Iterable
    )

from functions import (
    masked_average, add_weights, sp_map,
    season_mean, get_dpm, leap_year, share_ylims,
    to_png
    )

from cloud_metric import Cloud_Metric
from collections import deque
%matplotlib inline<jupyter_output>The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload<jupyter_text>Check running location and adjust working directory appropriately.<jupyter_code>host = os.uname()[1]
if 'jupyter' in host.split('-'): # Check if running on NIRD through the Jupyter Hub
    print('Running through MC2 Jupyter Hub')
    model_dir = '/mnt/mcc-ns9600k/jonahks/'
    os.chdir(model_dir)
    
elif 'casper' in host: # Check if running on NIRD through the Jupyter Hub
    print('Running through CISL Casper')
    obs_dir = '/glade/u/home/jonahshaw/w/obs/'
#     model_dir = '/glade/p/cesm/pcwg/jenkay/COSP/'
    os.chdir(obs_dir)
    
elif 'r6i6' in host: # Check if running on NIRD through the Jupyter Hub
    print('Running through CISL Cheyenne')
    obs_dir = '/glade/u/home/jonahshaw/w/obs/'
#     model_dir = '/glade/p/cesm/pcwg/jenkay/COSP/'
    os.chdir(obs_dir)

else:  # Assume that we're running on a local machine and mounting NIRD
    print('Running on %s, attempting to mount ns9600k/jonahks/ from NIRD' % str(host))
    os.system('fusermount -zu ~/drivemount/')  # unmount first
    os.system('sshfs <EMAIL>@login.<EMAIL>.sigma2.no:"p/jonahks/" ~/drivemount/')    # Calling mountnird from .bashrc doesn't work
    os.chdir('/home/jonahks/drivemount/')
    save_dir = '~/DATAOUT/'
    save_to = os.path.expanduser(save_dir)

output_dir = '/glade/u/home/jonahshaw/figures/'
obs_dir = '/glade/u/home/jonahshaw/w/obs/'
model_dir = '/glade/p/cesm/pcwg/jenkay/COSP/'
# case_dir = '/glade/p/cesm/pcwg/jenkay/COSP/cesm21/' 

# Check that each important directory can be accessed:    
access_paths = os.path.exists(output_dir) and os.path.exists(model_dir) #and os.path.exists(conv_dir)
print('Can access all directory paths:', access_paths)
os.listdir()
os.chdir('CALIPSO/GOCCP/2Ddata/')
os.listdir()
raw_goccp = xr.open_dataset('2006/MapLowMidHigh_Phase330m_200606_avg_CFMIP2_sat_3.1.2.nc')
old_interp = xr.open_dataset('1.25x0.9_interpolation/2006/MapLowMidHigh_Phase330m_200606_avg_CFMIP2_sat_3.1.2.nc')
cesm2_09res = xr.open_dataset('%s/cesm21/cesm2p1_B1850_COSP2_PCWG/atm/hist/%s' % (model_dir,'cesm2p1_B1850_COSP2_PCWG.cam.h0.0003-05.nc'))
os.listdir('%s/cesm21/cesm2p1_B1850_COSP2_PCWG/atm/hist' % model_dir)
cesm2_09res
raw_goccp['cltcalipso_liq'].plot()<jupyter_output><empty_output><jupyter_text>The CDO interpolation with standard settings is not working very well.<jupyter_code>old_interp['cltcalipso_liq'].plot()<jupyter_output><empty_output><jupyter_text>Try interpolation via interp_like<jupyter_code>out = raw_goccp['cltcalipso_liq'].interp_like(cesm2_09res['CLDTOT_CAL_LIQ'])<jupyter_output><empty_output><jupyter_text>__This doesn't look right...__<jupyter_code>out<jupyter_output><empty_output><jupyter_text>Try with just the interp method:<jupyter_code>test2 = raw_goccp['cltcalipso_liq'].interp(longitude=cesm2_09res['lon'], latitude=cesm2_09res['lat'], method="linear")<jupyter_output><empty_output><jupyter_text>__This looks better.__<jupyter_code>test2<jupyter_output><empty_output><jupyter_text>__But this is fucked up:__<jupyter_code>test2.plot()<jupyter_output><empty_output><jupyter_text>Try with just interp method and lon offset:<jupyter_code>test3 = raw_goccp['cltcalipso_liq'].interp(longitude=(cesm2_09res['lon']-180), latitude=cesm2_09res['lat'], method="linear")<jupyter_output><empty_output><jupyter_text>__This looks better.__<jupyter_code>test3<jupyter_output><empty_output><jupyter_text>__But this is still a little weird:__<jupyter_code>test3.plot()<jupyter_output><empty_output><jupyter_text>I think that in order to interp_like effectively, I will have to be able to shift the lat and lon coords so that the domains line up.  http://xarray.pydata.org/en/stable/generated/xarray.DataArray.assign_coords.html  Try with just interp like after rotating longitude coordinate:<jupyter_code>test4_data = raw_goccp['cltcalipso_liq'].copy()
test4_data.longitude
test4_data = test4_data.assign_coords({"longitude": (((test4_data.longitude + 180) % 360))})
# test4_data.longitude
cesm2_09res.lon
test4_data
test4 = test4_data.interp_like(longitude=(cesm2_09res['lon']), latitude=cesm2_09res['lat'], method="linear")
test4 = test4_data.interp_like(cesm2_09res, method="linear")<jupyter_output><empty_output><jupyter_text>__This looks better.__<jupyter_code>test4<jupyter_output><empty_output><jupyter_text>__But this is still a little weird:__<jupyter_code>test4.plot()<jupyter_output><empty_output><jupyter_text>OLD<jupyter_code>cesm2_wrap = cesm2_09res['CLDTOT_CAL'].copy()
cesm2_wrap.lon
# How do I keep the attributes?
_attrs = cesm2_wrap.lon.attrs
cesm2_wrap.assign_coords({"lon": (((cesm2_wrap.lon + 180) % 360) - 180)})
# cesm2_wrap.assign_attrs(_attrs) # why does this reverse the assign_coords
# This also converts back to the 0-360 format. I should probably keep this and modify the obs then?
cesm2_wrap.sortby(cesm2_wrap.lon)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12359943977591037, 'fraction_numerical': 0.03046218487394958, 'mean_word_length': 6.314980793854033, 'pattern_counts': {'":': 2, '<': 62, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '383687', 'license': 'MIT', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2216, 'n_words': 385, 'path': 'Interpolation/interpolation_test.ipynb', 'repo_name': 'jshaw35/CESM2_analysis', 'size': '5709'}