@@ -1700,7 +1700,7 @@ def test_topographic_index(tmp_path):
17001700 "grid_bounds" : bounds ,
17011701 "variable" : "topographic_index" ,
17021702 }
1703- hf .get_gridded_files ( options , filename_template = "foo.nc" )
1703+ hf .get_gridded_file ( "foo.nc" , options )
17041704 ds = xr .open_dataset ("foo.nc" )
17051705 da = ds ["topographic_index" ]
17061706 assert da .shape == (5 , 5 )
@@ -2580,3 +2580,121 @@ def test_get_bounds_limits():
25802580 with pytest .raises (ValueError ) as info :
25812581 hf .get_gridded_data (options )
25822582 assert "is outside the grid" in str (info .value )
2583+
2584+
2585+ def test_gridded_file_2wy ():
2586+ """Test downloading subset in 2 waters years using get_gridded_file."""
2587+
2588+ with tempfile .TemporaryDirectory () as tempdirname :
2589+ bounds = [1399 , 1784 , 1447 , 1803 ]
2590+ date_start = "1990-09-28"
2591+ date_end = "1990-10-04"
2592+ options = {
2593+ "dataset" : "CW3E" ,
2594+ "variable" : "air_temp" ,
2595+ "grid_bounds" : bounds ,
2596+ "temporal_resolution" : "daily" ,
2597+ "date_start" : date_start ,
2598+ "date_end" : date_end ,
2599+ }
2600+
2601+ # Download to 2 different files for each water year
2602+ file_path = tempdirname + "/" + "{dataset}_{variable}_{wy}.nc"
2603+ hf .get_gridded_file (file_path , options )
2604+ path = f"{ tempdirname } /CW3E_air_temp_1990.nc"
2605+ if not os .path .exists (path ):
2606+ raise ValueError (f"File not created: '{ path } '" )
2607+ ds = xr .open_dataset (path )
2608+ assert ds ["time" ].shape [0 ] == 3
2609+ assert str (ds ["time" ].values [0 ]).startswith ("1990-09-28" )
2610+ assert ds ["x" ].shape [0 ] == 48
2611+ assert ds ["y" ].shape [0 ] == 19
2612+
2613+ # Download to one file for both water years
2614+ file_path = tempdirname + "/" + "{dataset}_{variable}.nc"
2615+ hf .get_gridded_file (file_path , options )
2616+ path = f"{ tempdirname } /CW3E_air_temp.nc"
2617+ if not os .path .exists (path ):
2618+ raise ValueError (f"File not created: '{ path } '" )
2619+ ds = xr .open_dataset (path )
2620+ assert ds ["time" ].shape [0 ] == 6
2621+ assert str (ds ["time" ].values [0 ]).startswith ("1990-09-28" )
2622+ assert ds ["x" ].shape [0 ] == 48
2623+ assert ds ["y" ].shape [0 ] == 19
2624+
2625+ # Download as one tiff (this should be an error)
2626+ with pytest .raises (ValueError ):
2627+ file_path = tempdirname + "/" + "{dataset}_{variable}.tif"
2628+ hf .get_gridded_file (file_path , options )
2629+ path = f"{ tempdirname } /CW3E_air_temp.nc"
2630+
2631+
2632+ @pytest .mark .private_dataset
2633+ def test_gridded_file_wtd ():
2634+ """Test downloading subset of 30m WTD usign get_gridded_file."""
2635+
2636+ with tempfile .TemporaryDirectory () as tempdirname :
2637+ tempdirname = "."
2638+ bounds = [3000 , 3000 , 3010 , 3020 ]
2639+ options = {
2640+ "dataset" : "ma_2025" ,
2641+ "variable" : "water_table_depth" ,
2642+ "grid_bounds" : bounds ,
2643+ "grid" : "conus2_wtd" ,
2644+ }
2645+
2646+ # Download it as a tif
2647+ file_path = tempdirname + "/" + "{dataset}_{variable}.tif"
2648+ hf .get_gridded_file (file_path , options )
2649+ path = f"{ tempdirname } /ma_2025_water_table_depth.tif"
2650+ if not os .path .exists (path ):
2651+ raise ValueError (f"File not created: '{ path } '" )
2652+ ds = xr .open_dataset (path )
2653+ assert ds ["x" ].shape [0 ] == 10
2654+ assert ds ["y" ].shape [0 ] == 20
2655+
2656+ # Download it as netcdf
2657+ file_path = tempdirname + "/" + "{dataset}_{variable}.nc"
2658+ hf .get_gridded_file (file_path , options )
2659+ path = f"{ tempdirname } /ma_2025_water_table_depth.nc"
2660+ if not os .path .exists (path ):
2661+ raise ValueError (f"File not created: '{ path } '" )
2662+ ds = xr .open_dataset (path )
2663+ assert ds ["x" ].shape [0 ] == 10
2664+ assert ds ["y" ].shape [0 ] == 20
2665+
2666+
2667+ def test_gridded_file_pfb ():
2668+ """Test downloading subset in 2 waters years using get_gridded_file to pfb."""
2669+
2670+ with tempfile .TemporaryDirectory () as tempdirname :
2671+ bounds = [1399 , 1784 , 1447 , 1803 ]
2672+ date_start = "1990-09-28"
2673+ date_end = "1990-10-04"
2674+ options = {
2675+ "dataset" : "CW3E" ,
2676+ "variable" : "air_temp" ,
2677+ "grid_bounds" : bounds ,
2678+ "temporal_resolution" : "hourly" ,
2679+ "date_start" : date_start ,
2680+ "date_end" : date_end ,
2681+ }
2682+
2683+ # Download to 6 files for each day in separate water year directories
2684+ file_path = (
2685+ tempdirname
2686+ + "/"
2687+ + "WY{wy}/{dataset}_{variable}.{wy_start_24hr:06d}_to_{wy_end_24hr:06}.pfb"
2688+ )
2689+ hf .get_gridded_file (file_path , options )
2690+ path = f"{ tempdirname } /WY1990/CW3E_air_temp.008689_to_008712.pfb"
2691+ if not os .path .exists (path ):
2692+ raise ValueError (f"File not created: '{ path } '" )
2693+ data = hf .gridded .read_fast_pfb (path )
2694+ assert data .shape == (1 , 24 , 19 , 48 )
2695+
2696+ # Download to one file
2697+ file_path = tempdirname + "precip.pfb"
2698+ hf .get_gridded_file (file_path , options )
2699+ data = hf .gridded .read_fast_pfb (file_path )
2700+ assert data .shape == (1 , 144 , 19 , 48 )
0 commit comments