1515# -- import local classes for this script
1616from ctsm .site_and_regional .base_case import BaseCase , USRDAT_DIR , DatmFiles
1717from ctsm .utils import add_tag_to_filename , ensure_iterable
18+ from ctsm .longitude import detect_lon_type
1819
1920logger = logging .getLogger (__name__ )
2021
2122NAT_PFT = 15 # natural pfts
2223NUM_PFT = 17 # for runs with generic crops
2324MAX_PFT = 78 # for runs with explicit crops
2425
25- # -- constants to represent months of year
26- FIRST_MONTH = 1
27- LAST_MONTH = 12
28-
2926
3027class SinglePointCase (BaseCase ):
3128 """
@@ -151,6 +148,26 @@ def __init__(
151148 # self.check_nonveg()
152149 self .check_pct_pft ()
153150
151+ def convert_plon_to_filetype_if_needed (self , lon_da ):
152+ """
153+ Check that point and input file longitude types are equal. If not, convert point to match
154+ file.
155+ """
156+ plon_in = self .plon
157+ f_lon_type = detect_lon_type (lon_da )
158+ plon_type = plon_in .lon_type ()
159+ if f_lon_type == plon_type :
160+ plon_out = plon_in .get (plon_type )
161+ else :
162+ plon_orig = plon_in .get (plon_type )
163+ plon_out = plon_in .get (f_lon_type )
164+ if plon_orig != plon_out :
165+ print (
166+ f"Converted plon from type { plon_type } (value { plon_orig } ) "
167+ f"to type { f_lon_type } (value { plon_out } )"
168+ )
169+ return plon_out
170+
154171 def create_tag (self ):
155172 """
156173 Create a tag for single point which is the site name
@@ -363,8 +380,11 @@ def create_landuse_at_point(self, indir, file, user_mods_dir):
363380 # create 1d coordinate variables to enable sel() method
364381 f_in = self .create_1d_coord (fluse_in , "LONGXY" , "LATIXY" , "lsmlon" , "lsmlat" )
365382
383+ # get point longitude, converting to match file type if needed
384+ plon_float = self .convert_plon_to_filetype_if_needed (f_in ["lsmlon" ])
385+
366386 # extract gridcell closest to plon/plat
367- f_out = f_in .sel (lsmlon = self . plon , lsmlat = self .plat , method = "nearest" )
387+ f_out = f_in .sel (lsmlon = plon_float , lsmlat = self .plat , method = "nearest" )
368388
369389 # expand dimensions
370390 f_out = f_out .expand_dims (["lsmlat" , "lsmlon" ])
@@ -498,8 +518,11 @@ def create_surfdata_at_point(self, indir, file, user_mods_dir, specify_fsurf_out
498518 # create 1d coordinate variables to enable sel() method
499519 f_in = self .create_1d_coord (fsurf_in , "LONGXY" , "LATIXY" , "lsmlon" , "lsmlat" )
500520
521+ # get point longitude, converting to match file type if needed
522+ plon_float = self .convert_plon_to_filetype_if_needed (f_in ["lsmlon" ])
523+
501524 # extract gridcell closest to plon/plat
502- f_tmp = f_in .sel (lsmlon = self . plon , lsmlat = self .plat , method = "nearest" )
525+ f_tmp = f_in .sel (lsmlon = plon_float , lsmlat = self .plat , method = "nearest" )
503526
504527 # expand dimensions
505528 f_tmp = f_tmp .expand_dims (["lsmlat" , "lsmlon" ]).copy (deep = True )
@@ -525,10 +548,10 @@ def create_surfdata_at_point(self, indir, file, user_mods_dir, specify_fsurf_out
525548 # update lsmlat and lsmlon to match site specific instead of the nearest point
526549 # we do this so that if we create user_mods the PTS_LON and PTS_LAT in CIME match
527550 # the surface data coordinates - which is required
528- f_out ["lsmlon" ] = np .atleast_1d (self . plon )
551+ f_out ["lsmlon" ] = np .atleast_1d (plon_float )
529552 f_out ["lsmlat" ] = np .atleast_1d (self .plat )
530553 f_out ["LATIXY" ][:, :] = self .plat
531- f_out ["LONGXY" ][:, :] = self . plon
554+ f_out ["LONGXY" ][:, :] = plon_float
532555
533556 # update attributes
534557 self .update_metadata (f_out )
@@ -568,8 +591,11 @@ def create_datmdomain_at_point(self, datm_tuple: DatmFiles):
568591 # create 1d coordinate variables to enable sel() method
569592 f_in = self .create_1d_coord (fdatmdomain_in , "xc" , "yc" , "ni" , "nj" )
570593
594+ # get point longitude, converting to match file type if needed
595+ plon_float = self .convert_plon_to_filetype_if_needed (f_in ["lon" ])
596+
571597 # extract gridcell closest to plon/plat
572- f_out = f_in .sel (ni = self . plon , nj = self .plat , method = "nearest" )
598+ f_out = f_in .sel (ni = plon_float , nj = self .plat , method = "nearest" )
573599
574600 # expand dimensions
575601 f_out = f_out .expand_dims (["nj" , "ni" ])
@@ -591,14 +617,17 @@ def extract_datm_at(self, file_in, file_out):
591617 # create 1d coordinate variables to enable sel() method
592618 f_in = self .create_1d_coord (file_in , "LONGXY" , "LATIXY" , "lon" , "lat" )
593619
620+ # get point longitude, converting to match file type if needed
621+ plon_float = self .convert_plon_to_filetype_if_needed (f_in ["lon" ])
622+
594623 # extract gridcell closest to plon/plat
595- f_out = f_in .sel (lon = self . plon , lat = self .plat , method = "nearest" )
624+ f_out = f_in .sel (lon = plon_float , lat = self .plat , method = "nearest" )
596625
597626 # expand dimensions
598627 f_out = f_out .expand_dims (["lat" , "lon" ])
599628
600629 # specify dimension order
601- f_out = f_out .transpose ("scalar" , " time" , "lat" , "lon" )
630+ f_out = f_out .transpose ("time" , "lat" , "lon" )
602631
603632 # update attributes
604633 self .update_metadata (f_out )
@@ -653,46 +682,36 @@ def create_datm_at_point(self, datm_tuple: DatmFiles, datm_syr, datm_eyr, datm_s
653682 tpqwfiles = []
654683 for year in range (datm_syr , datm_eyr + 1 ):
655684 ystr = str (year )
656- for month in range (FIRST_MONTH , LAST_MONTH + 1 ):
657- mstr = str (month )
658- if month < 10 :
659- mstr = "0" + mstr
660-
661- dtag = ystr + "-" + mstr
662685
663- fsolar = os .path .join (
664- datm_tuple .indir ,
665- datm_tuple .dir_solar ,
666- "{}{}.nc" .format (datm_tuple .tag_solar , dtag ),
667- )
668- fsolar2 = "{}{}.{}.nc" .format (datm_tuple .tag_solar , self .tag , dtag )
669- fprecip = os .path .join (
670- datm_tuple .indir ,
671- datm_tuple .dir_prec ,
672- "{}{}.nc" .format (datm_tuple .tag_prec , dtag ),
673- )
674- fprecip2 = "{}{}.{}.nc" .format (datm_tuple .tag_prec , self .tag , dtag )
675- ftpqw = os .path .join (
676- datm_tuple .indir ,
677- datm_tuple .dir_tpqw ,
678- "{}{}.nc" .format (datm_tuple .tag_tpqw , dtag ),
679- )
680- ftpqw2 = "{}{}.{}.nc" .format (datm_tuple .tag_tpqw , self .tag , dtag )
681-
682- outdir = os .path .join (self .out_dir , datm_tuple .outdir )
683- infile += [fsolar , fprecip , ftpqw ]
684- outfile += [
685- os .path .join (outdir , fsolar2 ),
686- os .path .join (outdir , fprecip2 ),
687- os .path .join (outdir , ftpqw2 ),
688- ]
689- solarfiles .append (
690- os .path .join ("${}" .format (USRDAT_DIR ), datm_tuple .outdir , fsolar2 )
691- )
692- precfiles .append (
693- os .path .join ("${}" .format (USRDAT_DIR ), datm_tuple .outdir , fprecip2 )
694- )
695- tpqwfiles .append (os .path .join ("${}" .format (USRDAT_DIR ), datm_tuple .outdir , ftpqw2 ))
686+ fsolar = os .path .join (
687+ datm_tuple .indir ,
688+ datm_tuple .dir_solar ,
689+ "{}{}.nc" .format (datm_tuple .tag_solar , ystr ),
690+ )
691+ fsolar2 = "{}{}.{}.nc" .format (datm_tuple .tag_solar , self .tag , ystr )
692+ fprecip = os .path .join (
693+ datm_tuple .indir ,
694+ datm_tuple .dir_prec ,
695+ "{}{}.nc" .format (datm_tuple .tag_prec , ystr ),
696+ )
697+ fprecip2 = "{}{}.{}.nc" .format (datm_tuple .tag_prec , self .tag , ystr )
698+ ftpqw = os .path .join (
699+ datm_tuple .indir ,
700+ datm_tuple .dir_tpqw ,
701+ "{}{}.nc" .format (datm_tuple .tag_tpqw , ystr ),
702+ )
703+ ftpqw2 = "{}{}.{}.nc" .format (datm_tuple .tag_tpqw , self .tag , ystr )
704+
705+ outdir = os .path .join (self .out_dir , datm_tuple .outdir )
706+ infile += [fsolar , fprecip , ftpqw ]
707+ outfile += [
708+ os .path .join (outdir , fsolar2 ),
709+ os .path .join (outdir , fprecip2 ),
710+ os .path .join (outdir , ftpqw2 ),
711+ ]
712+ solarfiles .append (os .path .join ("${}" .format (USRDAT_DIR ), datm_tuple .outdir , fsolar2 ))
713+ precfiles .append (os .path .join ("${}" .format (USRDAT_DIR ), datm_tuple .outdir , fprecip2 ))
714+ tpqwfiles .append (os .path .join ("${}" .format (USRDAT_DIR ), datm_tuple .outdir , ftpqw2 ))
696715
697716 for idx , out_f in enumerate (outfile ):
698717 logger .debug (out_f )
0 commit comments