00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00039
00040
00041
00042
00043 #include <cmath>
00044 using namespace std;
00045
00046 #include "arts.h"
00047 #include "arts_omp.h"
00048 #include "auto_md.h"
00049 #include "math_funcs.h"
00050 #include "physics_funcs.h"
00051 #include "xml_io.h"
00052
00053 extern const Numeric PI;
00054 extern const Numeric DEG2RAD;
00055 extern const Numeric RAD2DEG;
00056 extern const Index GFIELD3_P_GRID;
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 void ForLoop(Workspace& ws,
00068
00069 const Agenda& forloop_agenda,
00070
00071 const Index& start,
00072 const Index& stop,
00073 const Index& step)
00074 {
00075 for (Index i=start; i<=stop; i+=step)
00076 {
00077 out1 << " Executing for loop body, index: " << i << "\n";
00078 forloop_agendaExecute(ws, i, forloop_agenda);
00079 }
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 void VectorExtractFromMatrix(
00091
00092 Vector& v,
00093
00094
00095 const Matrix& m,
00096 const Index& index,
00097
00098 const String& direction )
00099 {
00100 if (direction=="row")
00101 {
00102 if( index >= m.nrows() )
00103 {
00104 ostringstream os;
00105 os << "The index " << index
00106 << " is outside the row range of the Matrix.";
00107 throw runtime_error( os.str() );
00108
00109 }
00110
00111 v.resize( m.ncols() );
00112 v = m( index, joker );
00113 }
00114 else if (direction=="column")
00115 {
00116 if( index >= m.ncols() )
00117 {
00118 ostringstream os;
00119 os << "The index " << index
00120 << " is outside the column range of the Matrix.";
00121 throw runtime_error( os.str() );
00122
00123 }
00124
00125 v.resize( m.nrows() );
00126 v = m( joker, index );
00127 }
00128 else
00129 {
00130 ostringstream os;
00131 os << "Keyword *direction* must be either *row* or *column*,"
00132 << "but you gave: " << direction << ".";
00133 throw runtime_error( os.str() );
00134 }
00135 }
00136
00137
00138
00139
00140 void ybatchCalc_implementation(Workspace& ws,
00141
00142 Matrix& ybatch,
00143
00144 const Index& ybatch_n,
00145 const Agenda& ybatch_calc_agenda,
00146
00147 const Index& robust)
00148 {
00149 Vector y;
00150 bool is_first = true;
00151 Index first_ybatch_index = 0;
00152
00153 while (is_first && first_ybatch_index < ybatch_n)
00154 {
00155 out2 << " Doing job " << first_ybatch_index+1 << " of " << ybatch_n << "\n";
00156 try
00157 {
00158 ybatch_calc_agendaExecute( ws, y, first_ybatch_index, ybatch_calc_agenda );
00159
00160
00161
00162
00163
00164 ybatch.resize( y.nelem(), ybatch_n);
00165
00166
00167
00168
00169 ybatch = -1;
00170
00171 is_first = false;
00172
00173 ybatch( joker, first_ybatch_index ) = y;
00174 }
00175 catch (runtime_error e)
00176 {
00177 if (robust)
00178 {
00179 out0 << "WARNING! Job failed. Output variable ybatch will be set\n"
00180 << "to -1 for this job. The runtime error produced was:\n"
00181 << e.what() << "\n";
00182
00183
00184
00185 }
00186 else
00187 {
00188
00189
00190 throw runtime_error(e.what());
00191 }
00192 }
00193 first_ybatch_index++;
00194 }
00195
00196
00197
00198 Workspace l_ws (ws);
00199 Agenda l_ybatch_calc_agenda (ybatch_calc_agenda);
00200
00201
00202
00203 #pragma omp parallel private(y) firstprivate(l_ws,l_ybatch_calc_agenda)
00204 #pragma omp for
00205 for(Index ybatch_index = first_ybatch_index;
00206 ybatch_index<ybatch_n;
00207 ybatch_index++ )
00208 {
00209 out2 << " Doing job " << ybatch_index+1 << " of " << ybatch_n << "\n";
00210 try
00211 {
00212 ybatch_calc_agendaExecute( l_ws, y, ybatch_index,
00213 l_ybatch_calc_agenda );
00214
00215
00216
00217 ybatch( joker, ybatch_index ) = y;
00218 }
00219 catch (runtime_error e)
00220 {
00221 if (robust)
00222 {
00223 out0 << "WARNING! Job failed. Output variable ybatch will be set\n"
00224 << "to -1 for this job. The runtime error produced was:\n"
00225 << e.what() << "\n";
00226
00227
00228
00229 }
00230 else
00231 {
00232
00233
00234 exit_or_rethrow(e.what());
00235 }
00236 }
00237 }
00238 }
00239
00240
00241
00242 void ybatchCalc(Workspace& ws,
00243
00244 Matrix& ybatch,
00245
00246 const Index& ybatch_n,
00247 const Agenda& ybatch_calc_agenda,
00248
00249 const Index& robust)
00250 {
00251 if (0==robust)
00252 out2 << " Robust option is off.\n";
00253 else if (1==robust)
00254 out2 << " Robust option is on,\n"
00255 << " batch calc will continue, even if one job fails.\n";
00256 else
00257 throw runtime_error("Keyword *robust* must be either 0 or 1.");
00258
00259 ybatchCalc_implementation( ws,
00260 ybatch,
00261 ybatch_n,
00262 ybatch_calc_agenda,
00263 robust );
00264 }
00265
00266
00267
00268 void ybatchMetProfiles(
00269 Workspace& ws,
00270
00271 Matrix& ybatch,
00272
00273 const ArrayOfArrayOfSpeciesTag& abs_species,
00274 const Agenda& met_profile_calc_agenda,
00275 const Vector& f_grid,
00276 const Matrix& met_amsu_data,
00277 const Matrix& sensor_pos,
00278 const Matrix& r_geoid,
00279 const Vector& lat_grid,
00280 const Vector& lon_grid,
00281 const Index& atmosphere_dim,
00282 const ArrayOfSingleScatteringData& scat_data_raw,
00283
00284 const Index& nelem_p_grid,
00285 const String& met_profile_path,
00286 const String& met_profile_pnd_path)
00287 {
00288 GField3 t_field_raw;
00289 GField3 z_field_raw;
00290 ArrayOfGField3 vmr_field_raw;
00291 ArrayOfGField3 pnd_field_raw;
00292 Vector p_grid;
00293 Matrix sensor_los;
00294 Index cloudbox_on;
00295 ArrayOfIndex cloudbox_limits;
00296 Matrix z_surface;
00297 Vector y;
00298 Index no_profiles = met_amsu_data.nrows();
00299
00300
00301
00302
00303
00304 vmr_field_raw.resize(abs_species.nelem());
00305
00306
00307
00308
00309 const Index N_pt = scat_data_raw.nelem();
00310
00311 pnd_field_raw.resize(N_pt);
00312
00313
00314
00315 ConstVectorView sat_za_from_data = met_amsu_data(Range(joker),3);
00316
00317 sensor_los.resize(1,1);
00318
00319
00320
00321 ConstVectorView lat = met_amsu_data(Range(joker),0);
00322 ConstVectorView lon = met_amsu_data(Range(joker),1);
00323
00324 z_surface.resize(1,1);
00325
00326
00327 y.resize(f_grid.nelem());
00328
00329
00330 ybatch.resize(no_profiles, f_grid.nelem());
00331
00332
00333 for (Index i = 0; i < no_profiles; ++ i)
00334 {
00335 ostringstream lat_os, lon_os;
00336
00337 Index lat_prec = 3;
00338 if(lat[i] < 0) lat_prec--;
00339 if(abs(lat[i])>=10 )
00340 {
00341 lat_prec--;
00342 if(abs(lat[i])>=100 ) lat_prec--;
00343 }
00344
00345 lat_os.setf (ios::showpoint | ios::fixed);
00346 lat_os << setprecision((int)lat_prec) << lat[i];
00347
00348 Index lon_prec = 4;
00349 if(lon[i] < 0) lon_prec--;
00350 if(abs(lon[i])>=10 )
00351 {
00352 lon_prec--;
00353 if(abs(lon[i])>=100 ) lon_prec--;
00354 }
00355 lon_os.setf (ios::showpoint | ios::fixed);
00356 lon_os << setprecision((int)lon_prec) << lon[i];
00357
00358 sensor_los(0,0) =
00359 180.0 - (asin(r_geoid(0,0) * sin(sat_za_from_data[i] * DEG2RAD) /sensor_pos(0,0)))* RAD2DEG;
00360
00361
00362 xml_read_from_file(met_profile_path +"profile.lat_"+lat_os.str()+".lon_"+lon_os.str() + ".t.xml",
00363 t_field_raw);
00364
00365
00366 xml_read_from_file(met_profile_path +"profile.lat_"+lat_os.str()+".lon_"+lon_os.str() + ".z.xml",
00367 z_field_raw);
00368
00369
00370 xml_read_from_file(met_profile_path +"profile.lat_"+lat_os.str()+".lon_"+lon_os.str() + ".H2O.xml",
00371 vmr_field_raw[0]);
00372
00373
00374
00375
00376
00377
00378 xml_read_from_file(met_profile_pnd_path +"lwc_reff15/profile.lat_"+lat_os.str()+".lon_"+lon_os.str() + ".pnd15.xml", pnd_field_raw[0]);
00379
00380
00381
00382
00383 z_surface(0,0) = z_field_raw(0,0,0);
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 vmr_field_raw[1].resize(vmr_field_raw[0]);
00398 vmr_field_raw[1].copy_grids(vmr_field_raw[0]);
00399 vmr_field_raw[1] = 0.782;
00400
00401
00402
00403
00404
00405 vmr_field_raw[2].resize(vmr_field_raw[0]);
00406 vmr_field_raw[2].copy_grids(vmr_field_raw[0]);
00407 vmr_field_raw[2] = 0.209;
00408
00409 const ConstVectorView tfr_p_grid = t_field_raw.get_numeric_grid(GFIELD3_P_GRID);
00410
00411 Index N_p = tfr_p_grid.nelem();
00412
00413
00414 VectorNLogSpace(p_grid,
00415 nelem_p_grid,
00416 tfr_p_grid[0],
00417 tfr_p_grid[N_p -1]);
00418
00419
00420
00421
00422
00423 Numeric cl_grid_min, cl_grid_max;
00424
00425
00426
00427 cl_grid_min = tfr_p_grid[0];
00428
00429
00430 Index level_counter = 0;
00431
00432
00433 for (Index ip = 0; ip< N_p; ++ip)
00434 {
00435
00436
00437
00438 if(pnd_field_raw[0](ip, 0, 0) > 0.001)
00439 {
00440 ++level_counter;
00441
00442
00443
00444
00445 cl_grid_max = tfr_p_grid[ip +1];
00446 }
00447 }
00448
00449
00450 cloudbox_limits.resize( atmosphere_dim*2 );
00451
00452
00453
00454
00455 if(level_counter == 0)
00456 {
00457 cl_grid_max = p_grid[1];
00458 }
00459
00460
00461 cloudboxSetManually(cloudbox_on,
00462 cloudbox_limits,
00463 atmosphere_dim,
00464 p_grid,
00465 lat_grid,
00466 lon_grid,
00467 cl_grid_min,
00468 cl_grid_max,
00469 0,0,0,0);
00470
00471
00472
00473
00474
00475
00476
00477
00478 met_profile_calc_agendaExecute (ws, y, t_field_raw, vmr_field_raw,
00479 z_field_raw, pnd_field_raw, p_grid,
00480 sensor_los, cloudbox_on,
00481 cloudbox_limits, z_surface,
00482 met_profile_calc_agenda );
00483
00484
00485
00486 ybatch(i, Range(joker)) = y;
00487
00488 }
00489 }
00490
00491
00492
00493 void ybatchMetProfilesClear(
00494 Workspace& ws,
00495
00496 Matrix& ybatch,
00497
00498 const ArrayOfArrayOfSpeciesTag& abs_species,
00499 const Agenda& met_profile_calc_agenda,
00500 const Vector& f_grid,
00501 const Matrix& met_amsu_data,
00502 const Matrix& sensor_pos,
00503 const Matrix& r_geoid,
00504
00505 const Index& nelem_p_grid,
00506 const String& met_profile_path)
00507 {
00508 GField3 t_field_raw;
00509 GField3 z_field_raw;
00510 ArrayOfGField3 vmr_field_raw;
00511 ArrayOfGField3 pnd_field_raw;
00512 Vector p_grid;
00513 Matrix sensor_los;
00514 Index cloudbox_on = 0;
00515 ArrayOfIndex cloudbox_limits;
00516 Matrix z_surface;
00517 Vector y;
00518 Index no_profiles = met_amsu_data.nrows();
00519
00520
00521
00522 GField3 vmr_field_raw_h2o;
00523
00524 vmr_field_raw.resize(abs_species.nelem());
00525
00526 y.resize(f_grid.nelem());
00527 ybatch.resize(no_profiles, f_grid.nelem());
00528
00529 Vector sat_za_from_profile;
00530 sat_za_from_profile = met_amsu_data(Range(joker),3);
00531 Numeric sat_za;
00532
00533 sensor_los.resize(1,1);
00534
00535 Vector lat, lon;
00536 lat = met_amsu_data(Range(joker),0);
00537 lon = met_amsu_data(Range(joker),1);
00538
00539 Vector oro_height;
00540 oro_height = met_amsu_data(Range(joker),5);
00541
00542 z_surface.resize(1,1);
00543 for (Index i = 0; i < no_profiles; ++ i)
00544 {
00545 ostringstream lat_os, lon_os;
00546
00547 Index lat_prec = 3;
00548 if(lat[i] < 0) lat_prec--;
00549 if(abs(lat[i])>=10 )
00550 {
00551 lat_prec--;
00552 if(abs(lat[i])>=100 ) lat_prec--;
00553 }
00554
00555 lat_os.setf (ios::showpoint | ios::fixed);
00556 lat_os << setprecision((int)lat_prec) << lat[i];
00557
00558 Index lon_prec = 4;
00559 if(lon[i] < 0) lon_prec--;
00560 if(abs(lon[i])>=10 )
00561 {
00562 lon_prec--;
00563 if(abs(lon[i])>=100 ) lon_prec--;
00564 }
00565 lon_os.setf (ios::showpoint | ios::fixed);
00566 lon_os << setprecision((int)lon_prec) << lon[i];
00567 cout<<lat_os.str()<<endl;
00568 cout<<lon_os.str()<<endl;
00569
00570
00571 sat_za = sat_za_from_profile[i];
00572
00573
00574
00575 sensor_los(Range(joker),0) =
00576 180.0 - (asin(r_geoid(0,0) * sin(sat_za * PI/180.) /sensor_pos(0,0)))*180./PI;
00577 cout<<"sensor_los"<<sat_za_from_profile[i]<<endl;
00578 cout<<"sensor_los"<<sat_za<<endl;
00579 cout<<"sensor_los"<<sensor_los<<endl;
00580
00581
00582 xml_read_from_file(met_profile_path +"profile.lat_"+lat_os.str()+".lon_"+lon_os.str() + ".t.xml",
00583 t_field_raw);
00584
00585 xml_read_from_file(met_profile_path +"profile.lat_"+lat_os.str()+".lon_"+lon_os.str() + ".z.xml",
00586 z_field_raw);
00587
00588
00589
00590
00591 xml_read_from_file(met_profile_path +"profile.lat_"+lat_os.str()+".lon_"+lon_os.str() + ".H2O.xml",
00592 vmr_field_raw_h2o);
00593
00594
00595 cout << "--------------------------------------------------------------------------"<<endl;
00596 cout << "The file" << met_profile_path +"profile.lat_"+lat_os.str()+".lon_"+lon_os.str()<< "is executed now"<<endl;
00597 cout << "--------------------------------------------------------------------------"<<endl;
00598 xml_write_to_file("profile_number.xml", i);
00599
00600
00601
00602
00603 z_surface(0,0) = z_field_raw(0,0,0);
00604 cout<<"z_surface"<<z_surface<<endl;
00605 const ConstVectorView tfr_p_grid = t_field_raw.get_numeric_grid(GFIELD3_P_GRID);
00606 Index N_p = tfr_p_grid.nelem();
00607
00608 vmr_field_raw[0] = vmr_field_raw_h2o;
00609
00610
00611
00612
00613
00614 vmr_field_raw[1].resize(vmr_field_raw[0]);
00615 vmr_field_raw[1].copy_grids(vmr_field_raw[0]);
00616 vmr_field_raw[1](joker, joker, joker) = 0.782;
00617
00618
00619
00620
00621
00622 vmr_field_raw[2].resize(vmr_field_raw[0]);
00623 vmr_field_raw[2].copy_grids(vmr_field_raw[0]);
00624 vmr_field_raw[2](joker, joker, joker) = 0.209;
00625
00626
00627
00628
00629
00630
00631
00632 VectorNLogSpace(p_grid,
00633 nelem_p_grid,
00634 tfr_p_grid[0],
00635 tfr_p_grid[N_p -1]);
00636 cout<<"t_field_raw[0](0,0,0)"<<tfr_p_grid[0]<<endl;
00637 cout<<"t_field_raw[0](N_p -1,0,0)"<<tfr_p_grid[N_p -1] <<endl;
00638 xml_write_to_file("p_grid.xml", p_grid);
00639
00640
00641 met_profile_calc_agendaExecute (ws, y, t_field_raw, vmr_field_raw,
00642 z_field_raw, pnd_field_raw, p_grid,
00643 sensor_los, cloudbox_on,
00644 cloudbox_limits, z_surface,
00645 met_profile_calc_agenda );
00646
00647
00648 ybatch(i, Range(joker)) = y;
00649
00650 }
00651 }
00652
00653
00654
00655
00656 void ybatchUnit(
00657 Matrix& ybatch,
00658 const String& y_unit,
00659 const Vector& y_f )
00660 {
00661 const Index nrows = ybatch.nrows();
00662 const Index ncols = ybatch.ncols();
00663
00664 if( y_f.nelem() != nrows )
00665 {
00666 ostringstream os;
00667 os << "The numberof rows in *ybatch* and the length *y_f*\n"
00668 << "must be equal.";
00669 throw runtime_error( os.str() );
00670 }
00671
00672 if( y_unit == "1" )
00673 {}
00674
00675 else if( y_unit == "RJBT" )
00676 {
00677
00678
00679
00680
00681
00682 for( Index ir=0; ir<nrows; ir++ )
00683 {
00684 const Numeric scfac = invrayjean( 1, y_f[ir] );
00685
00686 for( Index ic=0; ic<ncols; ic++ )
00687 {
00688 ybatch(ir,ic) = scfac * ybatch(ir,ic);
00689 }
00690 }
00691 }
00692
00693 else if( y_unit == "PlanckBT" )
00694 {
00695 for( Index ir=0; ir<nrows; ir++ )
00696 {
00697 for( Index ic=0; ic<ncols; ic++ )
00698 {
00699 ybatch(ir,ic) = invplanck( ybatch(ir,ic), y_f[ir] );
00700 }
00701 }
00702 }
00703
00704 else
00705 {
00706 ostringstream os;
00707 os << "Unknown option: y_unit = \"" << y_unit << "\"\n"
00708 << "Recognised choices are: \"1\", \"RJBT\" and \"PlanckBT\"";
00709 throw runtime_error( os.str() );
00710 }
00711
00712 }
00713